forked from JeffersonLab/ersap-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlb_cplane_async.h
275 lines (192 loc) · 8.23 KB
/
lb_cplane_async.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// Copyright 2023, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.
//
// EPSCI Group
// Thomas Jefferson National Accelerator Facility
// 12000, Jefferson Ave, Newport News, VA 23606
// (757)-269-7100
/**
* @file Contains code to implement ERSAP backend communication to EJFAT LB's control plane.
*/
#ifndef LB_CONTROL_PLANE_H
#define LB_CONTROL_PLANE_H
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <mutex>
#include <unordered_map>
#include <chrono>
#include <thread>
#include <atomic>
#include <chrono>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <google/protobuf/util/time_util.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#ifdef BAZEL_BUILD
#include "examples/protos/loadbalancer.pb.h"
#include "examples/protos/loadbalancer_enum.grpc.pb.h"
#else
#include "loadbalancer.grpc.pb.h"
#include "loadbalancer_enum.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientContext;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using grpc::CompletionQueue;
using grpc::ServerAsyncResponseWriter;
using loadbalancer::BackendReport;
using loadbalancer::RegisterRequest;
using loadbalancer::DeregisterRequest;
using loadbalancer::SendStateRequest;
using loadbalancer::RegisterReply;
using loadbalancer::DeregisterReply;
using loadbalancer::SendStateReply;
//using google::protobuf::util;
// Class to represent a single backend and store its state in the control plane / server.
class BackEnd {
public:
BackEnd(const RegisterRequest* req);
void update(const SendStateRequest* state);
void printBackendState() const;
const std::string & getAuthToken() const;
const std::string & getSessionToken() const;
const std::string & getName() const;
google::protobuf::Timestamp getTimestamp() const;
int64_t getTime() const;
int64_t getLocalTime() const;
uint32_t getCpus() const;
uint32_t getRamBytes() const;
uint32_t getBufCount() const;
uint32_t getBufSizeBytes() const;
float getFillPercent() const;
float getSetPointPercent() const;
float getPidError() const;
const std::string & getTargetIP() const;
uint32_t getTargetPort() const;
uint32_t getTargetPortRange() const;
bool getIsReady() const;
bool getIsActive() const;
void setIsActive(bool active);
private:
// Data to return to from control-plane/client
/** Backend's authentication token. */
std::string authToken;
/** Backend's session token. */
std::string sessionToken;
/** Backend's name. */
std::string name;
/** Time in milliseconds past epoch that this data was taken by backend. */
google::protobuf::Timestamp timestamp;
/** Time in milliseconds past epoch that this data was taken by backend.
* Same as timestamp but in different format. */
int64_t time = 0;
/** Local time in milliseconds past epoch corresponding to backend time.
* Hopefully this takes care of time delays and nodes not setting their clocks properly.
* Set locally when SendState msg arrives, this helps find how long ago the backend reported data. */
int64_t localTime = 0;
/** Backend's cpu count. */
uint32_t cpus;
/** Backend's RAM. */
uint32_t ramBytes;
/** Number of backend's fifo entries. */
uint32_t bufCount;
/** Bytes in each backend fifo entry. */
uint32_t bufSizeBytes;
/** Percent of fifo entries filled with unprocessed data (0-1). */
float fillPercent;
/** PID loop set point (0-1). */
float setPointPercent;
/** PID error term in percentage of backend's fifo entries (0 - +/-0.5). */
float pidError;
/** Receiving IP address of backend. */
std::string targetIP;
/** Receiving UDP port of backend. */
uint16_t targetPort;
/** Receiving UDP port range of backend. */
uint16_t targetPortRange;
/** Ready to receive more data if true. */
bool isReady;
/** Is active (reported its status on time). */
bool isActive;
};
/** Class implementing logic and data behind the control plane / server's behavior. */
class BackendReportServiceImpl final : public BackendReport::AsyncService {
public:
Status SendStateAsync(ServerContext* context, const SendStateRequest* state, ServerAsyncResponseWriter<SendStateReply> *responder,
CompletionQueue *cq1, CompletionQueue *cq2, void *ptr);
Status SendState (ServerContext* context, const SendStateRequest* state, SendStateReply* reply);
Status Register (ServerContext* context, const RegisterRequest* request, RegisterReply* reply);
Status DeRegister (ServerContext* context, const DeregisterRequest* request, DeregisterReply* reply);
std::shared_ptr<std::unordered_map<std::string, BackEnd>> getBackEnds();
void runServer(uint16_t port, BackendReportServiceImpl *service);
private:
// Another instance when java is soooo much easier, C++ has no thread-safe containers :(
// Since the control plane will be accessing this map while potentially multiple threads are
// writing to it SIMULTANEOUSLY, we'll need to protect its access.
// Easiest to protect writing into it (only in the SendState, Register, and UnRegister methods
// above) with a mutex. For the control plane reading it, we can return a copy when asked for
// it in getBackEnds()
// Store data reported from backends to this server
std::unordered_map<std::string, BackEnd> data;
std::mutex map_mutex;
std::unique_ptr<grpc::ServerCompletionQueue> cq;
};
/** Class client sends data from backend to control plane (server). */
class LbControlPlaneClient {
public:
LbControlPlaneClient(const std::string& targetIP, uint16_t targetPort, const std::string& _name,
uint32_t _bufferSize, uint32_t _bufferCount, float _setPoint);
int Register();
int Deregister() const;
int SendState() const;
void update(float fill, float pidErr);
const std::string & getIP() const;
const std::string & getName() const;
uint16_t getPort() const;
uint32_t getBufCount() const;
uint32_t getBufSizeBytes() const;
float getSetPointPercent() const;
float getFillPercent() const;
float getPidError() const;
bool getIsReady() const;
private:
/** Object used to call backend's grpc API routines. */
std::unique_ptr<BackendReport::Stub> stub_;
// Used to connect to backend
/** Control plane's IP address (dotted decimal format). */
std::string ipAddr = "localhost";
/** Control plane's grpc port. */
uint16_t port = 56789;
/** Target's name (ipaddr:port). */
std::string target;
// Fixed data to send-to/get-from control plane during registration
/** Token used to register. */
std::string authToken;
/** Token used to send state and to deregister. */
std::string sessionToken;
/** Client/caller's name. */
std::string name;
/** PID loop set point. */
float setPointPercent;
/** Number of backend's fifo entries. */
uint32_t bufCount;
/** Bytes in each backend fifo entry. */
uint32_t bufSizeBytes;
// Transcient data to send to control plane
/** Percent of fifo entries filled with unprocessed data. */
float fillPercent;
/** PID error term in percentage of backend's fifo entries. */
float pidError;
/** Ready to receive more data or not. */
bool isReady;
};
#endif