forked from C5T/Current
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_karl.h
382 lines (318 loc) · 18.1 KB
/
schema_karl.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2016 Dmitry "Dima" Korolev <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef KARL_SCHEMA_KARL_H
#define KARL_SCHEMA_KARL_H
#include "../port.h"
#include "schema_claire.h"
#include "locator.h"
#include "../storage/storage.h"
namespace current {
namespace karl {
// Karl's nginx setup parameters.
CURRENT_STRUCT(KarlNginxParameters) {
CURRENT_FIELD(port, uint16_t);
CURRENT_FIELD_DESCRIPTION(port, "The port for keepalives- and browsability-enabling nginx to listen on.");
CURRENT_FIELD(config_file, std::string);
CURRENT_FIELD_DESCRIPTION(config_file, "The nginx config file to manipulate.");
CURRENT_FIELD(route_prefix, std::string);
CURRENT_FIELD_DESCRIPTION(route_prefix, "Non-standard top-level route, if set.");
CURRENT_CONSTRUCTOR(KarlNginxParameters)
(uint16_t port, const std::string& config_file, const std::string& route_prefix = "/live")
: port(port), config_file(config_file), route_prefix(route_prefix) {}
};
// Karl's startup parameters.
constexpr static const char* kDefaultFleetViewURL = "http://localhost:%d"; // Defaults to the nginx port.
constexpr static std::chrono::microseconds k45Seconds = std::chrono::microseconds(1000ll * 1000ll * 45);
CURRENT_STRUCT(KarlParameters) {
CURRENT_FIELD(keepalives_port, uint16_t);
CURRENT_FIELD_DESCRIPTION(keepalives_port, "The port on which keepalives are listened to.");
CURRENT_FIELD(fleet_view_port, uint16_t);
CURRENT_FIELD_DESCRIPTION(fleet_view_port, "The port fleet browsability requests are served from.");
CURRENT_FIELD(stream_persistence_file, std::string);
CURRENT_FIELD_DESCRIPTION(stream_persistence_file, "The file name to store the stream of keepalives.");
CURRENT_FIELD(storage_persistence_file, std::string);
CURRENT_FIELD_DESCRIPTION(
storage_persistence_file,
"The file name to store the persisted event log of keepalives post-processed into servers and services.");
CURRENT_FIELD(keepalives_url, std::string, "/");
CURRENT_FIELD_DESCRIPTION(keepalives_url, "The endpoint to listen to keepalives on, on the `keepalives_port` port.");
CURRENT_FIELD(fleet_view_url, std::string, "/");
CURRENT_FIELD_DESCRIPTION(fleet_view_url, "The endpoint to listen to fleet view requests on.");
CURRENT_FIELD(public_url, std::string, kDefaultFleetViewURL);
CURRENT_FIELD_DESCRIPTION(public_url,
"The prefix of the URL to append to have the JSONs containing per-service proxies "
"URLs browsable in one click.");
CURRENT_FIELD(svg_name, std::string, "Karl");
CURRENT_FIELD_DESCRIPTION(svg_name, "The top-level image name on the GraphViz-rendered SVG.");
CURRENT_FIELD(github_repo_url, std::string, "");
CURRENT_FIELD_DESCRIPTION(github_repo_url, "The top-level GitHub repo URL to have git commit hashes browsable.");
CURRENT_FIELD(nginx_parameters, KarlNginxParameters, KarlNginxParameters(0, ""));
CURRENT_FIELD_DESCRIPTION(nginx_parameters, "Nginx manager config.");
CURRENT_FIELD(service_timeout_interval, std::chrono::microseconds, k45Seconds);
CURRENT_FIELD_DESCRIPTION(service_timeout_interval,
"The default period of keepalive-free inactivity, after which a service is "
"considered down for fleet browsability purposes.");
KarlParameters& SetKeepalivesPort(uint16_t port) {
keepalives_port = port;
return *this;
}
KarlParameters& SetFleetViewPort(uint16_t port) {
fleet_view_port = port;
return *this;
}
KarlParameters& SetStreamFile(const std::string& value) {
stream_persistence_file = value;
return *this;
}
KarlParameters& SetStorageFile(const std::string& value) {
storage_persistence_file = value;
return *this;
}
KarlParameters& SetPublicURL(const std::string& value) {
public_url = value;
return *this;
}
KarlParameters& SetSVGName(const std::string& value) {
svg_name = value;
return *this;
}
KarlParameters& SetGitHubURL(const std::string& value) {
github_repo_url = value;
return *this;
}
KarlParameters& SetNginxParameters(const KarlNginxParameters& value) {
nginx_parameters = value;
return *this;
}
};
// Karl's persisted storage schema.
// System info, startup/teardown.
CURRENT_STRUCT(KarlInfo) {
CURRENT_FIELD(timestamp, std::chrono::microseconds, current::time::Now());
CURRENT_FIELD_DESCRIPTION(
timestamp, "Unix epoch microseconds of the time this report was generated. Can be used to measure time skew.");
CURRENT_USE_FIELD_AS_KEY(timestamp);
CURRENT_FIELD(up, bool, true);
CURRENT_FIELD_DESCRIPTION(up, "`true` for starting up, `false` for a graceful shutdown.");
CURRENT_FIELD(persisted_keepalives_info, Optional<idxts_t>);
CURRENT_FIELD_DESCRIPTION(persisted_keepalives_info, "Details on the stream which stores the received keepalives.");
CURRENT_FIELD(karl_build_info, build::BuildInfo, build::BuildInfo());
CURRENT_FIELD_DESCRIPTION(karl_build_info, "Build information of Karl itself.");
};
CURRENT_ENUM(ClaireRegisteredState, uint8_t){Active = 0u, Deregistered = 1u, DisconnectedByTimeout = 2u};
// Per-service static info -- ideally, what changes once during its startup, [ WTF ] but also dependencies.
CURRENT_STRUCT(ClaireInfo) {
CURRENT_FIELD(codename, std::string);
CURRENT_FIELD_DESCRIPTION(codename, "The unique codename of an instance of a running service.");
CURRENT_USE_FIELD_AS_KEY(codename);
CURRENT_FIELD(service, std::string);
CURRENT_FIELD_DESCRIPTION(service, "The name of the service.");
CURRENT_FIELD(location, ClaireServiceKey);
CURRENT_FIELD_DESCRIPTION(location, "The location of the service.");
CURRENT_FIELD(reported_timestamp, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(reported_timestamp,
"The time when persisted information about this service has been last updated.");
CURRENT_FIELD(url_status_page_direct, std::string);
CURRENT_FIELD_DESCRIPTION(url_status_page_direct,
"The direct, world-inaccessible, URL for the status page of this service.");
CURRENT_FIELD(registered_state, ClaireRegisteredState);
CURRENT_FIELD_DESCRIPTION(registered_state, "Active = 0, Deregistered = 1, DisconnectedByTimeout = 2.");
};
CURRENT_STRUCT(ClaireBuildInfo) {
CURRENT_FIELD(codename, std::string);
CURRENT_USE_FIELD_AS_KEY(codename);
CURRENT_FIELD(build, current::build::BuildInfo);
};
const uint64_t kUpdateServerInfoThresholdByTimeSkewDifference = 50000;
// Per-server status info.
CURRENT_STRUCT(ServerInfo) {
CURRENT_FIELD(ip, std::string);
CURRENT_FIELD_DESCRIPTION(ip, "The IP address of the server.");
CURRENT_USE_FIELD_AS_KEY(ip);
CURRENT_FIELD(behind_this_by, std::chrono::microseconds, std::chrono::microseconds(0));
CURRENT_FIELD_DESCRIPTION(behind_this_by,
"How much this server is behind the server that generated this report "
"local-time-wise.");
CURRENT_FIELD(cloud_instance_name, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(cloud_instance_name,
"The name of the instance in the cloud as reported by any of the services.");
CURRENT_FIELD(cloud_availability_group, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(cloud_availability_group,
"The availability group in the cloud as reported by any of the services.");
};
CURRENT_STORAGE_FIELD_ENTRY(OrderedDictionary, KarlInfo, KarlInfoDictionary);
CURRENT_STORAGE_FIELD_ENTRY(UnorderedDictionary, ClaireInfo, ClaireInfoDictionary);
CURRENT_STORAGE_FIELD_ENTRY(UnorderedDictionary, ClaireBuildInfo, BuildInfoDictionary);
CURRENT_STORAGE_FIELD_ENTRY(UnorderedDictionary, ServerInfo, ServerInfoDictionary);
CURRENT_STORAGE(ServiceStorage) {
CURRENT_STORAGE_FIELD(karl, KarlInfoDictionary);
CURRENT_STORAGE_FIELD(claires, ClaireInfoDictionary);
CURRENT_STORAGE_FIELD(builds, BuildInfoDictionary);
CURRENT_STORAGE_FIELD(servers, ServerInfoDictionary);
};
// Karl's HTTP response(s) schema.
namespace current_service_state {
CURRENT_STRUCT(Error) {
CURRENT_FIELD(error, std::string);
CURRENT_FIELD_DESCRIPTION(error, "The error message, human-readable.");
CURRENT_CONSTRUCTOR(Error)(const std::string& error = "Error.") : error(error) {}
};
CURRENT_STRUCT(up) {
CURRENT_FIELD(start_time_epoch_microseconds, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(start_time_epoch_microseconds,
"Unix epoch microseconds of the time the service was started.");
CURRENT_FIELD(last_keepalive_received, std::string);
CURRENT_FIELD_DESCRIPTION(
last_keepalive_received,
"Time elapsed since the most recent keepalive message was received from this service, human-readable.");
CURRENT_FIELD(last_keepalive_received_epoch_microseconds, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(last_keepalive_received_epoch_microseconds,
"Unix epoch microseconds of the most recent keepalive message received from this service.");
CURRENT_FIELD(uptime, std::string);
CURRENT_FIELD_DESCRIPTION(uptime, "The uptime of the service based on the most recent keepalive, human-readable.");
CURRENT_DEFAULT_CONSTRUCTOR(up) {}
CURRENT_CONSTRUCTOR(up)
(const std::chrono::microseconds start_time_epoch_microseconds,
const std::string& last_keepalive_received,
const std::chrono::microseconds last_keepalive_received_epoch_microseconds,
const std::string& uptime)
: start_time_epoch_microseconds(start_time_epoch_microseconds),
last_keepalive_received(last_keepalive_received),
last_keepalive_received_epoch_microseconds(last_keepalive_received_epoch_microseconds),
uptime(uptime) {}
};
CURRENT_STRUCT(down) {
CURRENT_FIELD(start_time_epoch_microseconds, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(start_time_epoch_microseconds,
"Unix epoch microseconds of the time the service was started.");
CURRENT_FIELD(last_keepalive_received, std::string);
CURRENT_FIELD_DESCRIPTION(last_keepalive_received,
"Time elapsed since the most recent keepalive message was received from this "
"service before it went offline, human-readable.");
CURRENT_FIELD(last_keepalive_received_epoch_microseconds, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(last_keepalive_received_epoch_microseconds,
"Unix epoch microseconds of the most recent keepalive message received from this "
"service before it went offline.");
CURRENT_FIELD(last_confirmed_uptime, std::string);
CURRENT_FIELD_DESCRIPTION(last_confirmed_uptime,
"The uptime of the service as of right before it went offline, human-readable.");
CURRENT_DEFAULT_CONSTRUCTOR(down) {}
CURRENT_CONSTRUCTOR(down)
(const std::chrono::microseconds start_time_epoch_microseconds,
const std::string& last_keepalive_received,
const std::chrono::microseconds last_keepalive_received_epoch_microseconds,
const std::string& last_confirmed_uptime)
: start_time_epoch_microseconds(start_time_epoch_microseconds),
last_keepalive_received(last_keepalive_received),
last_keepalive_received_epoch_microseconds(last_keepalive_received_epoch_microseconds),
last_confirmed_uptime(last_confirmed_uptime) {}
};
// TODO(dkorolev): `Variant<up, down, done>`, where `done` == "has been shut down gracefully".
using state_variant_t = Variant<up, down>;
} // namespace current_service_state
CURRENT_STRUCT_T(ServiceToReport) {
CURRENT_FIELD(currently, current_service_state::state_variant_t);
CURRENT_FIELD_DESCRIPTION(currently, "`up` or `down`, with respective details.");
CURRENT_FIELD(service, std::string);
CURRENT_FIELD_DESCRIPTION(service, "The service name.");
CURRENT_FIELD(codename, std::string);
CURRENT_FIELD_DESCRIPTION(codename,
"The codename of this instance of the running service, assigned randomly at its startup.");
CURRENT_FIELD(location, ClaireServiceKey);
CURRENT_FIELD_DESCRIPTION(location, "The information identifying where is this service running and how to reach it.");
CURRENT_FIELD(dependencies, std::vector<std::string>);
CURRENT_FIELD_DESCRIPTION(dependencies, "Other services this one depends on, resolved to their codenames.");
CURRENT_FIELD(unresolved_dependencies, std::vector<std::string>);
CURRENT_FIELD_DESCRIPTION(unresolved_dependencies,
"Other services this one depends on, which can not be resolved into codenames, and "
"thus are reported as the URLs of their internal status pages.");
CURRENT_FIELD(url_status_page_direct, std::string);
CURRENT_FIELD_DESCRIPTION(url_status_page_direct,
"The direct, internal, URL to access the status page of this service.");
CURRENT_FIELD(url_status_page_proxied, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(url_status_page_proxied,
"The proxied, external, URL to access to status page of this service.");
CURRENT_FIELD(build_time, std::string);
CURRENT_FIELD_DESCRIPTION(build_time,
"An exempt of this service's build information, to help visually locating a "
"specific, usually recently started, service.");
CURRENT_FIELD(build_time_epoch_microseconds, std::chrono::microseconds, std::chrono::microseconds(0));
CURRENT_FIELD_DESCRIPTION(build_time_epoch_microseconds,
"An exempt of this service's build information, to help visually locating a "
"specific, usually recently started, service.");
CURRENT_FIELD(git_commit, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(git_commit,
"An exempt of this service's build information, to help visually locating a "
"specific, usually recently started, service.");
CURRENT_FIELD(git_branch, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(git_branch,
"An exempt of this service's build information, to help visually locating a "
"specific, usually recently started, service.");
CURRENT_FIELD(git_dirty, Optional<bool>);
CURRENT_FIELD_DESCRIPTION(git_dirty,
"An exempt of this service's build information, to help visually locating a "
"specific, usually recently started, service.");
CURRENT_FIELD(runtime, Optional<T>);
CURRENT_FIELD_DESCRIPTION(runtime, "The service-specific runtime information about this service.");
};
CURRENT_STRUCT_T(ServerToReport) {
CURRENT_FIELD(time_skew, std::string);
CURRENT_FIELD_DESCRIPTION(
time_skew,
"Whether this server is on par, ahead, or behind the server that generated this report local-time-wise.");
CURRENT_FIELD(cloud_instance_name, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(cloud_instance_name,
"The name of the instance in the cloud as reported by any of the services.");
CURRENT_FIELD(cloud_availability_group, Optional<std::string>);
CURRENT_FIELD_DESCRIPTION(cloud_availability_group,
"The availability group in the cloud as reported by any of the services.");
CURRENT_FIELD(services, (std::map<std::string, ServiceToReport<T>>));
CURRENT_FIELD_DESCRIPTION(services, "The list of services running on this server.");
};
CURRENT_STRUCT_T(GenericKarlStatus) {
CURRENT_FIELD(now, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(now, "Unix epoch microseconds of when this report has been generated.");
CURRENT_FIELD(from, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(from, "Unix epoch microseconds of the beginning of the time range this report covers.");
CURRENT_FIELD(to, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(to, "Unix epoch microseconds of the end of the time range this report covers.");
CURRENT_FIELD(generation_time, std::chrono::microseconds);
CURRENT_FIELD_DESCRIPTION(generation_time, "How long did the generation of this report take, in microseconds.");
CURRENT_FIELD(machines, (std::map<std::string, ServerToReport<T>>));
CURRENT_FIELD_DESCRIPTION(machines, "The actual report, grouped by servers.");
};
CURRENT_STRUCT(KarlUpStatus) {
CURRENT_FIELD(up, bool, true);
CURRENT_FIELD(parameters, Optional<KarlParameters>);
CURRENT_DEFAULT_CONSTRUCTOR(KarlUpStatus) {}
CURRENT_CONSTRUCTOR(KarlUpStatus)(const KarlParameters& parameters) : parameters(parameters) {}
};
CURRENT_STRUCT_T(SnapshotOfKeepalive) {
CURRENT_FIELD(age, std::string);
CURRENT_FIELD_DESCRIPTION(age, "How long ago was this snapshot saved, human-readable.");
CURRENT_FIELD(snapshot, ClaireServiceStatus<T>);
CURRENT_FIELD_DESCRIPTION(snapshot, "The snapshot itself.");
CURRENT_CONSTRUCTOR_T(SnapshotOfKeepalive)
(std::chrono::microseconds dt, const ClaireServiceStatus<T>& data = ClaireServiceStatus<T>())
: age(strings::TimeDifferenceAsHumanReadableString(dt)), snapshot(data) {}
};
} // namespace karl
} // namespace current
#endif // KARL_SCHEMA_KARL_H