forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpfunc.c
523 lines (411 loc) · 13.6 KB
/
bpfunc.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <pb_alloc.h>
#include <bpfunc.h>
#include <comdb2.h>
#include <osqlcomm.h>
#include <views.h>
#include <net_types.h>
#include <bdb_access.h>
#include <bpfunc.pb-c.h>
#include <bdb_schemachange.h>
#include <logmsg.h>
extern int gbl_check_access_controls;
/* Automatically create 'default' user when authentication is enabled. */
int gbl_create_default_user;
/* FUNCTION DECLARATIONS */
static int prepare_create_timepart(bpfunc_t *tp);
static int prepare_drop_timepart(bpfunc_t *tp);
static int prepare_timepart_retention(bpfunc_t *tp);
static int exec_grant(void *tran, bpfunc_t *func, char *err);
static int exec_authentication(void *tran, bpfunc_t *func, char *err);
static int exec_password(void *tran, bpfunc_t *func, char *err);
static int exec_alias(void *tran, bpfunc_t *func, char *err);
static int exec_analyze_threshold(void *tran, bpfunc_t *func, char *err);
static int exec_analyze_coverage(void *tran, bpfunc_t *func, char *err);
static int exec_rowlocks_enable(void *tran, bpfunc_t *func, char *err);
static int exec_genid48_enable(void *tran, bpfunc_t *func, char *err);
static int exec_set_skipscan(void *tran, bpfunc_t *func, char *err);
/******************** UTILITIES ***********************/
static int empty(void *tran, bpfunc_t *func, char *err) { return 0; }
void free_bpfunc(bpfunc_t *func)
{
if (unlikely(!func)) return;
free_bpfunc_arg(func->arg);
if (func)
free(func);
}
void free_bpfunc_arg(BpfuncArg *arg)
{
bpfunc_arg__free_unpacked(arg, &pb_alloc);
}
static int init_bpfunc(bpfunc_t *bpf)
{
memset(bpf, 0, sizeof(*bpf));
return 0;
}
static int prepare_methods(bpfunc_t *func, bpfunc_info *info)
{
func->exec = empty;
func->success = empty;
func->fail = empty;
func->info = info;
switch (func->arg->type) {
case BPFUNC_CREATE_TIMEPART:
prepare_create_timepart(func);
break;
case BPFUNC_DROP_TIMEPART:
prepare_drop_timepart(func);
break;
case BPFUNC_GRANT:
func->exec = exec_grant;
break;
case BPFUNC_PASSWORD:
func->exec = exec_password;
break;
case BPFUNC_AUTHENTICATION:
func->exec = exec_authentication;
break;
case BPFUNC_ALIAS:
func->exec = exec_alias;
break;
case BPFUNC_ANALYZE_THRESHOLD:
func->exec = exec_analyze_threshold;
break;
case BPFUNC_ANALYZE_COVERAGE:
func->exec = exec_analyze_coverage;
break;
case BPFUNC_TIMEPART_RETENTION:
prepare_timepart_retention(func);
break;
case BPFUNC_ROWLOCKS_ENABLE:
func->exec = exec_rowlocks_enable;
break;
case BPFUNC_GENID48_ENABLE:
func->exec = exec_genid48_enable;
break;
case BPFUNC_SET_SKIPSCAN:
func->exec = exec_set_skipscan;
break;
default:
logmsg(LOGMSG_ERROR, "Unknown function_id in bplog function\n");
return -1;
break;
}
return 0;
}
int bpfunc_prepare(bpfunc_t **f, int32_t data_len, uint8_t *data,
bpfunc_info *info)
{
bpfunc_t *func = *f = (bpfunc_t *)malloc(sizeof(bpfunc_t));
if (func == NULL)
goto end;
init_bpfunc(func);
func->arg = bpfunc_arg__unpack(&pb_alloc, data_len, data);
if (!func->arg)
goto fail_arg;
if (!prepare_methods(func, info))
return 0;
fail_arg:
free_bpfunc(func);
end:
return -1;
}
/******************************** TIME PARTITIONS
* ***********************************/
/************************ CREATE TIME PARTITIONS
* ***********************************/
int exec_create_timepart(void *tran, bpfunc_t *func, char *err)
{
char *json_cmd = NULL;
int len = 0;
int rc;
struct errstat errst;
build_createcmd_json(
&json_cmd, &len, func->arg->crt_tp->partition_name,
func->arg->crt_tp->tablename, func->arg->crt_tp->period,
func->arg->crt_tp->retention, func->arg->crt_tp->start);
assert(json_cmd);
rc =
views_do_partition(tran, thedb->timepart_views,
func->arg->crt_tp->partition_name, json_cmd, &errst);
free(json_cmd);
return rc;
}
int success_create_timepart(void *tran, bpfunc_t *func, char *err)
{
int rc = 0;
int bdberr = 0;
rc = bdb_llog_views(thedb->bdb_env, func->arg->crt_tp->partition_name, 1,
&bdberr);
if (rc) {
logmsg(LOGMSG_ERROR, "%s -- bdb_llog_views rc:%d bdberr:%d\n", __func__, rc,
bdberr);
}
return rc;
}
static int prepare_create_timepart(bpfunc_t *tp)
{
tp->exec = exec_create_timepart;
tp->success = success_create_timepart;
return 0;
}
/************************ DROP TIME PARTITIONS
* ***********************************/
int exec_drop_timepart(void *tran, bpfunc_t *func, char *err)
{
char *json_cmd = NULL;
int len = 0;
int rc;
struct errstat errst;
build_dropcmd_json(&json_cmd, &len, func->arg->drop_tp->partition_name);
assert(json_cmd);
rc = views_do_partition(tran, thedb->timepart_views,
func->arg->drop_tp->partition_name, json_cmd,
&errst);
free(json_cmd);
return rc;
}
int success_drop_timepart(void *tran, bpfunc_t *func, char *err)
{
int rc = 0;
int bdberr = 0;
rc = bdb_llog_views(thedb->bdb_env, func->arg->drop_tp->partition_name, 1,
&bdberr);
if (rc) {
logmsg(LOGMSG_ERROR, "%s -- bdb_llog_views rc:%d bdberr:%d\n", __func__, rc,
bdberr);
}
return rc;
}
static int prepare_drop_timepart(bpfunc_t *tp)
{
tp->exec = exec_drop_timepart;
tp->success = success_drop_timepart;
return 0;
}
/*********************************** GRANT
* ***********************************************/
static int grantAuth(void *tran, int permission, int command_type,
char *tablename, char *userschema, char *username)
{
int rc = 0;
int bdberr = 0;
bdb_state_type *bdb_state = thedb->bdb_env;
switch (permission) {
case AUTH_READ:
rc = bdb_tbl_access_read_set(bdb_state, tran, tablename, username,
&bdberr);
break;
case AUTH_WRITE:
rc = bdb_tbl_access_write_set(bdb_state, tran, tablename, username,
&bdberr);
break;
case AUTH_USERSCHEMA:
rc = bdb_tbl_access_userschema_set(bdb_state, tran, userschema,
username, &bdberr);
break;
case AUTH_OP:
rc = bdb_tbl_op_access_set(bdb_state, tran, command_type, tablename,
username, &bdberr);
break;
default:
rc = SQLITE_INTERNAL;
break;
}
if (bdberr == BDBERR_ADD_DUPE)
rc = 0;
return rc;
}
static int revokeAuth(void *tran, int permission, int command_type,
char *tablename, char *userschema, char *username)
{
int rc = 0;
int bdberr = 0;
bdb_state_type *bdb_state = thedb->bdb_env;
switch (permission) {
case AUTH_READ:
rc = bdb_tbl_access_read_delete(bdb_state, tran, tablename, username,
&bdberr);
break;
case AUTH_WRITE:
rc = bdb_tbl_access_write_delete(bdb_state, tran, tablename, username,
&bdberr);
break;
case AUTH_OP:
rc = bdb_tbl_op_access_delete(bdb_state, tran, command_type, tablename,
username, &bdberr);
break;
case AUTH_USERSCHEMA:
rc = bdb_tbl_access_userschema_delete(bdb_state, tran, userschema,
username, &bdberr);
break;
}
if (bdberr == BDBERR_DELNOTFOUND)
rc = 0;
return rc;
}
static int exec_grant(void *tran, bpfunc_t *func, char *err)
{
BpfuncGrant *grant = func->arg->grant;
int rc = 0;
if (!grant->yesno)
rc = grantAuth(tran, grant->perm, 0, grant->table, grant->userschema,
grant->username);
else
rc = revokeAuth(tran, grant->perm, 0, grant->table, grant->userschema,
grant->username);
return rc;
}
/************************ PASSWORD *********************/
static int exec_password(void *tran, bpfunc_t *func, char *err)
{
int rc;
BpfuncPassword *pwd = func->arg->pwd;
rc = pwd->disable ? bdb_user_password_delete(tran, pwd->user)
: bdb_user_password_set(tran, pwd->user, pwd->password);
if (rc == 0)
rc = net_send_authcheck_all(thedb->handle_sibling);
++gbl_bpfunc_auth_gen;
return rc;
}
/************************ AUTHENTICATION *********************/
static int exec_authentication(void *tran, bpfunc_t *func, char *err)
{
BpfuncAuthentication *auth = func->arg->auth;
int bdberr = 0;
int valid_user;
if (auth->enabled)
bdb_user_password_check(DEFAULT_USER, DEFAULT_PASSWORD, &valid_user);
/* Check if there is already op password. */
int rc = bdb_authentication_set(thedb->bdb_env, tran, auth->enabled, &bdberr);
if (gbl_create_default_user && auth->enabled && valid_user == 0 && rc == 0)
rc = bdb_user_password_set(tran, DEFAULT_USER, DEFAULT_PASSWORD);
if (rc == 0)
rc = net_send_authcheck_all(thedb->handle_sibling);
gbl_check_access_controls = 1;
++gbl_bpfunc_auth_gen;
return rc;
}
/************************ ALIAS ******************************/
static int exec_alias(void *tran, bpfunc_t *func, char *err)
{
int rc = 0;
char *error;
BpfuncAlias *alias = func->arg->alias;
rc = llmeta_set_tablename_alias(NULL, alias->name, alias->remote, &error);
if (error) {
// todo should be send upstream ....
free(error);
}
return rc;
}
/******************************* ANALYZE *****************************/
static int exec_analyze_threshold(void *tran, bpfunc_t *func, char *err)
{
BpfuncAnalyzeThreshold *thr_f = func->arg->an_thr;
int bdberr;
int rc;
if (thr_f->newvalue == -1)
rc = bdb_clear_analyzethreshold_table(tran, thr_f->tablename, &bdberr);
else
rc = bdb_set_analyzethreshold_table(tran, thr_f->tablename,
thr_f->newvalue, &bdberr);
// TODO bdberr should not be ignored also a better way to pass err msg
// upstream
// would be nice
return rc;
}
static int exec_analyze_coverage(void *tran, bpfunc_t *func, char *err)
{
BpfuncAnalyzeCoverage *cov_f = func->arg->an_cov;
int bdberr;
int rc;
if (cov_f->newvalue == -1)
rc = bdb_clear_analyzecoverage_table(tran, cov_f->tablename, &bdberr);
else
rc = bdb_set_analyzecoverage_table(tran, cov_f->tablename,
cov_f->newvalue, &bdberr);
// TODO bdberr should not be ignored also a better way to pass err msg
// upstream
// would be nice
return rc;
}
static int exec_timepart_retention(void *tran, bpfunc_t *func, char *err)
{
BpfuncTimepartRetention *ret_f = func->arg->tp_ret;
struct errstat xerr = {0};
int rc;
rc = timepart_update_retention( tran, ret_f->timepartname, ret_f->newvalue, &xerr);
// TODO bdberr should not be ignored also a better way to pass err msg upstream
// would be nice
return rc;
}
int success_timepart_retention(void *tran, bpfunc_t *func, char *err)
{
int rc = 0;
int bdberr = 0;
rc = bdb_llog_views(thedb->bdb_env, func->arg->tp_ret->timepartname, 1, &bdberr);
if(rc)
{
logmsg(LOGMSG_ERROR, "%s -- bdb_llog_views rc:%d bdberr:%d\n",
__func__, rc, bdberr);
}
return rc;
}
static int prepare_timepart_retention(bpfunc_t *tp)
{
tp->exec = exec_timepart_retention;
tp->success = success_timepart_retention;
return 0;
}
static int exec_set_skipscan(void *tran, bpfunc_t *func, char *err)
{
BpfuncAnalyzeCoverage *cov_f = func->arg->an_cov;
int bdberr;
int rc;
if (cov_f->newvalue == 1) //enable skipscan means clear llmeta entry
rc = bdb_clear_table_parameter(tran, cov_f->tablename, "disableskipscan");
else {
const char *value = "true";
rc = bdb_set_table_parameter(tran, cov_f->tablename, "disableskipscan", value);
}
bdb_llog_analyze(thedb->bdb_env, 1, &bdberr);
return rc;
}
static int exec_genid48_enable(void *tran, bpfunc_t *func, char *err)
{
BpfuncGenid48Enable *gn = func->arg->gn_enable;
int format = bdb_genid_format(thedb->bdb_env);
if (gn->enable && format == LLMETA_GENID_48BIT) {
fprintf(stderr, "%s -- genid48 is already enabled\n", __func__);
return -1;
}
if (!gn->enable && format != LLMETA_GENID_48BIT) {
fprintf(stderr, "%s -- genid48 is already disabled\n", __func__);
return -1;
}
/* Set flags: we'll actually set the format in the block processor */
struct ireq *iq = (struct ireq *)func->info->iq;
iq->osql_genid48_enable = gn->enable;
iq->osql_flags |= OSQL_FLAGS_GENID48;
return 0;
}
static int exec_rowlocks_enable(void *tran, bpfunc_t *func, char *err)
{
BpfuncRowlocksEnable *rl = func->arg->rl_enable;
int rc;
if (rl->enable && gbl_rowlocks) {
fprintf(stderr, "%s -- rowlocks is already enabled\n", __func__);
return -1;
}
if (!rl->enable && !gbl_rowlocks) {
fprintf(stderr, "%s -- rowlocks is already disabled\n", __func__);
return -1;
}
rc = set_rowlocks(tran, rl->enable);
if (!rc) {
struct ireq *iq = (struct ireq *)func->info->iq;
iq->osql_rowlocks_enable = rl->enable;
iq->osql_flags |= OSQL_FLAGS_ROWLOCKS;
}
return rc;
}