forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtcpu.c
285 lines (249 loc) · 8.07 KB
/
rtcpu.c
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
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <dirent.h>
#include "cdb2api.h"
#include "nodemap.h"
#include "intern_strings.h"
#include "list.h"
#include "cdb2_constants.h"
#include "util.h"
#include "epochlib.h"
#include "nodemap.h"
#include "machclass.h"
#include "logmsg.h"
#include "locks_wrap.h"
static int machine_is_up_default(const char *host);
static int machine_status_init(void);
static int machine_class_default(const char *host);
static int machine_dc_default(const char *host);
static int machine_num_default(const char *host);
static int (*machine_is_up_cb)(const char *host) = machine_is_up_default;
static int (*machine_status_init_cb)(void) = machine_status_init;
static int (*machine_class_cb)(const char *host) = machine_class_default;
static int (*machine_dc_cb)(const char *host) = machine_dc_default;
static int (*machine_num_cb)(const char *host) = machine_num_default;
static int inited = 0;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static void do_once(void)
{
inited = 1;
machine_status_init_cb();
}
static void init_once(void)
{
pthread_once(&once, do_once);
}
void register_rtcpu_callbacks(int (*a)(const char *), int (*b)(void),
int (*c)(const char *), int (*d)(const char *),
int (*e)(const char *))
{
if (inited) {
logmsg(LOGMSG_WARN, "rtcpu already initialized\n");
return;
}
machine_is_up_cb = a;
machine_status_init_cb = b;
machine_class_cb = c;
machine_dc_cb = d;
machine_num_cb = e;
}
int machine_is_up(const char *host)
{
init_once();
if (!isinterned(host))
abort();
return machine_is_up_cb(host);
}
int machine_class(const char *host)
{
return machine_class_cb(host);
}
int machine_dc(const char *host)
{
return machine_dc_cb(host);
}
int machine_num(const char *host)
{
return machine_num_cb(host);
}
static int machine_is_up_default(const char *host)
{
return 1;
}
static int machine_status_init(void)
{
return 0;
}
extern char *gbl_mynode;
/* pthread_once? */
static int machine_class_default(const char *host)
{
static enum mach_class my_class = CLASS_UNKNOWN;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
Pthread_mutex_lock(&mtx);
if (my_class == CLASS_UNKNOWN) {
char *envclass;
cdb2_hndl_tp *db = NULL;
envclass = getenv("COMDB2_CLASS");
if (envclass) {
if (strcmp(envclass, "dev") == 0 || strcmp(envclass, "test") == 0)
my_class = CLASS_TEST;
else if (strcmp(envclass, "alpha") == 0)
my_class = CLASS_ALPHA;
else if (strcmp(envclass, "beta") == 0)
my_class = CLASS_BETA;
else if (strcmp(envclass, "prod") == 0)
my_class = CLASS_PROD;
else
logmsg(LOGMSG_ERROR,
"envclass set to \"%s\", don't recognize it\n",
envclass);
} else {
/* Try comdb2db */
char *sql = "select class from machines where name=@name";
char *envclass;
int rc;
cdb2_init_ssl(0, 0);
rc = cdb2_open(&db, "comdb2db", "default", 0);
if (rc) {
logmsg(LOGMSG_INFO, "%s(%s) open rc %d %s!\n", __func__, host,
rc, cdb2_errstr(db));
goto done;
}
rc = cdb2_bind_param(db, "name", CDB2_CSTRING, gbl_mynode,
strlen(gbl_mynode));
if (rc) {
logmsg(LOGMSG_ERROR, "%s(%s) bind rc %d %s!\n", __func__, host,
rc, cdb2_errstr(db));
goto done;
}
rc = cdb2_run_statement(db, sql);
if (rc) {
logmsg(LOGMSG_ERROR, "run rc %d %s, while trying to discover "
"current machine class\n",
rc, sql);
goto done;
}
rc = cdb2_next_record(db);
if (rc && rc != CDB2_OK_DONE) {
logmsg(LOGMSG_ERROR, "next rc %d %s, while trying to discover "
"current machine class\n",
rc, sql);
goto done;
}
envclass = cdb2_column_value(db, 0);
if (strcmp(envclass, "dev") == 0 || strcmp(envclass, "test") == 0)
my_class = CLASS_TEST;
else if (strcmp(envclass, "alpha") == 0)
my_class = CLASS_ALPHA;
else if (strcmp(envclass, "beta") == 0)
my_class = CLASS_BETA;
else if (strcmp(envclass, "prod") == 0)
my_class = CLASS_PROD;
do {
rc = cdb2_next_record(db);
} while (rc == CDB2_OK);
/* Shouldn't happen, so warn - but not an error. */
if (rc != CDB2_OK_DONE)
logmsg(LOGMSG_ERROR,
"consume rc %d %s, while trying to discover "
"current machine class\n",
rc, sql);
}
done:
/* Error if can't find class? */
if (my_class == CLASS_UNKNOWN)
my_class = CLASS_PROD;
if (db)
cdb2_close(db);
}
Pthread_mutex_unlock(&mtx);
return my_class;
}
static int machine_dcs[MAXNODES];
static int resolve_dc(const char *host)
{
int rc;
char *dcstr;
int dc = 99; /* Default to something, 0 would make us do the same lookup. */
int types[] = {CDB2_CSTRING};
cdb2_hndl_tp *db = NULL;
cdb2_init_ssl(0, 0);
rc = cdb2_open(&db, "comdb2db", "default", 0);
if (rc) {
logmsg(LOGMSG_INFO, "%s(%s) open rc %d %s!\n", __func__, host,
rc, cdb2_errstr(db));
goto done;
}
rc = cdb2_bind_param(db, "name", CDB2_CSTRING, host, strlen(host));
if (rc) {
logmsg(LOGMSG_ERROR, "%s(%s) bind rc %d %s!\n", __func__, host, rc,
cdb2_errstr(db));
goto done;
}
rc = cdb2_run_statement_typed(db,
"select room from machines where name=@name",
sizeof(types) / sizeof(types), types);
if (rc) {
logmsg(LOGMSG_ERROR, "%s(%s) run rc %d %s!\n", __func__, host, rc,
cdb2_errstr(db));
goto done;
}
rc = cdb2_next_record(db);
if (rc && rc != CDB2_OK_DONE) {
logmsg(LOGMSG_ERROR, "%s(%s) next rc %d %s!\n", __func__, host, rc,
cdb2_errstr(db));
goto done;
}
if (rc == 0) {
dcstr = (char *)cdb2_column_value(db, 0);
if (dcstr) {
if (strcmp(dcstr, "NY") == 0)
dc = 1;
else if (strcmp(dcstr, "NJ") == 0)
dc = 5;
else if (strcmp(dcstr, "ORG") == 0)
dc = 6;
}
rc = cdb2_next_record(db);
if (rc != CDB2_OK_DONE) {
logmsg(LOGMSG_ERROR, "%s(%s) next (last) rc %d %s\n!", __func__,
host, rc, cdb2_errstr(db));
goto done;
}
}
done:
if (db != NULL)
cdb2_close(db);
// printf("%s->%d\n", host, dc);
return dc;
}
static int machine_dc_default(const char *host)
{
int ix;
ix = nodeix(host);
if (machine_dcs[ix] == 0)
machine_dcs[ix] = resolve_dc(host);
return machine_dcs[ix];
}
static int machine_num_default(const char *host)
{
return nodeix(host);
}