-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathsc_logic.c
1802 lines (1588 loc) · 58.1 KB
/
sc_logic.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 <unistd.h>
#include <poll.h>
#include <memory_sync.h>
#include <str0.h>
#include <views.h>
#include "schemachange.h"
#include "sc_global.h"
#include "sc_logic.h"
#include "sc_util.h"
#include "sc_struct.h"
#include "sc_queues.h"
#include "sc_schema.h"
#include "sc_lua.h"
#include "sc_add_table.h"
#include "sc_alter_table.h"
#include "sc_fastinit_table.h"
#include "sc_stripes.h"
#include "sc_drop_table.h"
#include "sc_rename_table.h"
#include "sc_view.h"
#include "logmsg.h"
#include "comdb2_atomic.h"
#include "sc_callbacks.h"
#include "views.h"
#include <debug_switches.h>
void comdb2_cheapstack_sym(FILE *f, char *fmt, ...);
extern int gbl_is_physical_replicant;
int gbl_multitable_ddl = 0;
/**** Utility functions */
static enum thrtype prepare_sc_thread(struct schema_change_type *s)
{
struct thr_handle *thr_self = thrman_self();
enum thrtype oldtype = THRTYPE_UNKNOWN;
if (s->kind != SC_PARTIALUPRECS)
logmsg(LOGMSG_DEBUG, "Starting a schemachange thread\n");
if (!s->nothrevent) {
if (thr_self) {
thread_started("schema change");
oldtype = thrman_get_type(thr_self);
thrman_change_type(thr_self, THRTYPE_SCHEMACHANGE);
} else {
thr_self = thrman_register(THRTYPE_SCHEMACHANGE);
}
backend_thread_event(thedb, COMDB2_THR_EVENT_START_RDWR);
logmsg(LOGMSG_INFO, "Preparing schema change read write thread\n");
}
return oldtype;
}
static void reset_sc_thread(enum thrtype oldtype, struct schema_change_type *s)
{
struct thr_handle *thr_self = thrman_self();
if (!s->nothrevent) {
backend_thread_event(thedb, COMDB2_THR_EVENT_DONE_RDWR);
/* restore our thread type to what it was before */
if (oldtype != THRTYPE_UNKNOWN) thrman_change_type(thr_self, oldtype);
}
}
/* if we're using the low level meta table and we are doing a normal change,
* mark the table as being in a schema change so that if we are interrupted
* the new master knows to resume
* We mark schemachange over in mark_schemachange_over()
*/
static int mark_sc_in_llmeta_tran(struct schema_change_type *s, void *trans)
{
int bdberr;
int rc = SC_OK;
void *packed_sc_data = NULL;
size_t packed_sc_data_len;
uuidstr_t us;
comdb2uuidstr(s->uuid, us);
logmsg(LOGMSG_INFO, "%s: table '%s' rqid [%llx %s]\n", __func__,
s->tablename, s->rqid, us);
if (pack_schema_change_type(s, &packed_sc_data, &packed_sc_data_len)) {
sc_errf(s, "could not pack the schema change data for storage in "
"low level meta table\n");
return SC_LLMETA_ERR;
} else {
unsigned retries;
unsigned max_retries = 10;
/* mark the schema change in progress in the low level meta table,
* retry several times */
for (retries = 0;
retries < max_retries &&
(bdb_set_in_schema_change(trans, s->tablename, packed_sc_data,
packed_sc_data_len, &bdberr) ||
bdb_set_schema_change_status(
trans, s->tablename, s->iq->sc_seed, 0, packed_sc_data,
packed_sc_data_len, BDB_SC_RUNNING, NULL, &bdberr) ||
bdberr != BDBERR_NOERROR);
++retries) {
sc_errf(s, "could not mark schema change in progress in the "
"low level meta table, retrying ...\n");
sleep(1);
}
if (retries >= max_retries) {
sc_errf(s, "could not mark schema change in progress in the "
"low level meta table, giving up after %u retries\n",
retries);
rc = SC_LLMETA_ERR;
if (s->resume) {
sc_errf(s, "failed to resume schema change, downgrading to "
"give another master a shot\n");
bdb_transfermaster(thedb->static_table.handle);
}
}
}
free(packed_sc_data);
return rc;
}
static int mark_sc_in_llmeta(struct schema_change_type *s)
{
return mark_sc_in_llmeta_tran(s, NULL);
}
static int propose_sc(struct schema_change_type *s)
{
/* Check that all nodes are ready to do this schema change. */
int rc = broadcast_sc_start(s->tablename, s->iq->sc_seed, s->iq->sc_host,
time(NULL));
if (rc != 0) {
rc = SC_PROPOSE_FAIL;
sc_errf(s, "unable to gain agreement from all nodes to do schema "
"change\n");
sc_errf(s, "check that all nodes are connected ('send bdb "
"cluster')\n");
} else {
/* if we're not actually changing the schema then everything is fully
* replicated so we don't actually need all the replicants online to
* do this safely. this helps save fastinit. */
if (!s->same_schema) {
if (check_sc_ok(s) != 0) {
rc = SC_PROPOSE_FAIL;
} else {
rc = broadcast_sc_ok();
if (rc != 0) {
sc_errf(s, "cannot perform schema change; not all nodes "
"acknowledged readiness\n");
rc = SC_PROPOSE_FAIL;
}
}
}
if (s->force) {
sc_printf(s, "Performing schema change regardless in force "
"mode\n");
rc = SC_OK;
}
}
return rc;
}
static int master_downgrading(struct schema_change_type *s)
{
if (get_stopsc(__func__, __LINE__)) {
if (!s->nothrevent)
backend_thread_event(thedb, COMDB2_THR_EVENT_DONE_RDWR);
if (s->sb) {
sbuf2printf(s->sb, "!Master node downgrading - new master will "
"resume schemachange\n");
sbuf2close(s->sb);
s->sb = NULL;
}
logmsg(
LOGMSG_WARN,
"Master node downgrading - new master will resume schemachange\n");
return SC_MASTER_DOWNGRADE;
}
return SC_OK;
}
static void free_sc(struct schema_change_type *s)
{
free_schema_change_type(s);
/* free any memory csc2 allocated when parsing schema */
/* Bail out if we're in a time partition rollout otherwise
we may deadlock with a regular schema change. The time partition
rollout will invoke csc2_free_all() without holding views_lk. */
if (s->views_locked)
return;
csc2_free_all();
}
static void stop_and_free_sc(struct ireq *iq, int rc,
struct schema_change_type *s, int do_free)
{
if (s->kind != SC_PARTIALUPRECS) {
if (rc != 0) {
logmsg(LOGMSG_INFO, "Schema change returning FAILED\n");
sbuf2printf(s->sb, "FAILED\n");
} else {
logmsg(LOGMSG_INFO, "Schema change returning SUCCESS\n");
sbuf2printf(s->sb, "SUCCESS\n");
}
}
if (rc && iq->sc->kind == SC_ADDTABLE && rc != SC_TABLE_ALREADY_EXIST) {
if (iq->sc->db) {
delete_temp_table(iq, iq->sc->db);
}
if (iq->sc->already_finalized) {
rem_dbtable_from_thedb_dbs(iq->sc->db);
}
}
if (do_free) {
/* this is for non-bplog schema changes; if we free, mark this done too */
sc_set_running(iq, s, s->tablename, 0, NULL, 0, __func__, __LINE__);
free_sc(s);
}
}
static int set_original_tablename(struct schema_change_type *s)
{
struct dbtable *db = get_dbtable_by_name(s->tablename);
if (db) {
strncpy0(s->tablename, db->tablename, sizeof(s->tablename));
return 0;
}
return 1;
}
int do_upgrade_table(struct schema_change_type *s, struct ireq *unused)
{
int rc;
set_original_tablename(s);
if (!s->resume) set_sc_flgs(s);
if ((rc = mark_sc_in_llmeta(s))) return rc;
if (rc == SC_OK) rc = do_upgrade_table_int(s);
if (rc) {
mark_schemachange_over(s->tablename);
} else if (s->finalize) {
rc = finalize_upgrade_table(s);
} else {
rc = SC_COMMIT_PENDING;
}
return rc;
}
/*
** Start transaction if not passed in (comdb2sc.tsk)
** If started transaction, then
** 1. also commit it
** 2. log scdone here
*/
static inline int replication_only_error_code(int rc)
{
switch (rc) {
case -1:
case BDBERR_NOT_DURABLE:
return 1;
}
return 0;
}
int llog_scdone_rename_wrapper(bdb_state_type *bdb_state,
struct schema_change_type *s, tran_type *tran,
int *bdberr)
{
int rc;
char *mashup = NULL;
int oldlen = 0;
int newlen = 0;
if (s->db) {
mashup = s->tablename;
oldlen = strlen(s->tablename) + 1;
/* embed new name after old name if rename */
if (s->done_type == rename_table || s->done_type == alias_table) {
char *dst;
if (s->done_type == alias_table && s->db->sqlaliasname) {
dst = s->db->sqlaliasname;
} else {
dst = s->db->tablename;
}
newlen = strlen(dst) + 1;
mashup = alloca(oldlen + newlen);
memcpy(mashup, s->tablename, oldlen); /* old */
memcpy(mashup + oldlen, dst, newlen); /* new */
}
}
if (!tran)
rc = bdb_llog_scdone(bdb_state, s->done_type, mashup, oldlen + newlen,
1, bdberr);
else
rc = bdb_llog_scdone_tran(bdb_state, s->done_type, tran, mashup,
oldlen + newlen, bdberr);
return rc;
}
static int do_finalize(ddl_t func, struct ireq *iq,
struct schema_change_type *s, tran_type *input_tran)
{
int rc, bdberr = 0;
tran_type *tran = input_tran;
tran_type *ltran, *ptran;
if (input_tran == NULL) {
rc = get_schema_change_txns(iq, <ran, &ptran, &tran);
if (rc) {
sc_errf(s, "Failed to start finalize transaction %d\n", -rc);
return -1;
}
}
uint64_t sc_nrecs = 0;
if (s->db)
sc_nrecs = s->db->sc_nrecs; // take a copy, func will clear to 0
rc = func(iq, s, tran);
if (rc) {
if (input_tran == NULL)
goto abort;
return rc;
}
if ((rc = mark_schemachange_over_tran(s->tablename, tran))) {
if (input_tran == NULL)
goto abort;
return rc;
}
if (bdb_set_schema_change_status(tran, s->tablename, iq->sc_seed, sc_nrecs,
NULL, 0, BDB_SC_COMMITTED, NULL,
&bdberr) ||
bdberr != BDBERR_NOERROR) {
logmsg(LOGMSG_ERROR,
"%s: failed to set bdb schema change status, bdberr %d\n",
__func__, bdberr);
}
if (input_tran == NULL) {
// void all_locks(void*);
// all_locks(thedb->bdb_env);
if (debug_switch_fake_sc_replication_timeout()) {
logmsg(LOGMSG_USER,
"Forcing replication error table %s '%s' for tran %p\n",
bdb_get_scdone_str(s->done_type), s->tablename, tran);
}
rc = llog_scdone_rename_wrapper(thedb->bdb_env, s, tran, &bdberr);
if (rc || bdberr != BDBERR_NOERROR) {
sc_errf(s, "Failed to send scdone rc=%d bdberr=%d\n", rc, bdberr);
rc = -1;
goto abort;
} else {
if (s->keep_locked) {
rc = trans_commit(iq, tran, gbl_myhostname);
} else {
rc = trans_commit_adaptive(iq, tran, gbl_myhostname);
}
if (debug_switch_fake_sc_replication_timeout())
rc = -1;
if (rc && !replication_only_error_code(rc)) {
sc_errf(s, "Failed to commit finalize transaction\n");
trans_abort_logical(iq, ltran, NULL, 0, NULL, 0);
return rc;
}
rc = trans_commit_logical(iq, ltran, gbl_myhostname, 0, 1, NULL, 0, NULL, 0);
if (rc) {
abort();
}
}
sc_del_unused_files(s->db);
} else {
int bdberr = 0;
rc = llog_scdone_rename_wrapper(thedb->bdb_env, s, tran, &bdberr);
if (rc || bdberr != BDBERR_NOERROR) {
sc_errf(s, "Failed to send scdone rc=%d bdberr=%d\n", rc, bdberr);
return -1;
}
}
return rc;
abort:
trans_abort(iq, tran);
trans_abort_logical(iq, ltran, NULL, 0, NULL, 0);
mark_schemachange_over(s->tablename);
sc_del_unused_files(s->db);
return rc;
}
static int check_table_version(struct ireq *iq, struct schema_change_type *sc)
{
if (sc->kind == SC_ADDTABLE || sc->resume || sc->fix_tp_badvers)
return 0;
int rc, bdberr;
unsigned long long version;
rc = bdb_table_version_select(sc->tablename, NULL, &version, &bdberr);
if (rc != 0) {
errstat_set_strf(&iq->errstat,
"failed to get version for table:%s rc:%d",
sc->tablename, rc);
iq->errstat.errval = ERR_SC;
return SC_INTERNAL_ERROR;
}
if (sc->usedbtablevers != version) {
errstat_set_strf(&iq->errstat,
"stale version for table:%s master:%llu replicant:%d",
sc->tablename, version, sc->usedbtablevers);
iq->errstat.errval = ERR_SC;
return SC_INTERNAL_ERROR;
}
return 0;
}
static int do_ddl(ddl_t pre, ddl_t post, struct ireq *iq,
struct schema_change_type *s, tran_type *tran)
{
int rc, bdberr = 0;
if (s->resume == SC_OSQL_RESUME) {
return s->sc_rc;
}
if (s->done_type != user_view) {
set_original_tablename(s);
if ((rc = check_table_version(iq, s)) != 0) { // non-tran ??
goto end;
}
}
if (!s->resume) {
set_sc_flgs(s);
rc = trim_sc_history_entries(NULL, s->tablename);
if (rc)
logmsg(LOGMSG_ERROR,
"Cant cleanup comdb2_sc_history and keep last entries\n");
}
if ((rc = mark_sc_in_llmeta_tran(s, NULL))) // non-tran ??
goto end;
if (!s->resume && s->done_type == alter &&
bdb_attr_get(thedb->bdb_attr, BDB_ATTR_SC_DETACHED)) {
sc_printf(s, "Starting Schema Change with seed 0x%llx\n",
flibc_htonll(iq->sc_seed));
sbuf2printf(s->sb, "SUCCESS\n");
s->sc_rc = SC_DETACHED;
}
rc = pre(iq, s, NULL); // non-tran ??
if (s->done_type == alter && master_downgrading(s)) {
s->sc_rc = SC_MASTER_DOWNGRADE;
errstat_set_strf(
&iq->errstat,
"Master node downgrading - new master will resume schemachange\n");
return SC_MASTER_DOWNGRADE;
}
if (rc == SC_PAUSED) {
errstat_set_strf(&iq->errstat, "Schema change was paused");
if (bdb_set_schema_change_status(NULL, s->tablename, iq->sc_seed, 0,
NULL, 0, BDB_SC_PAUSED, NULL,
&bdberr) ||
bdberr != BDBERR_NOERROR) {
logmsg(LOGMSG_ERROR,
"%s: failed to set bdb schema change status, bdberr %d\n",
__func__, bdberr);
}
return rc;
} else if (rc == SC_PREEMPTED) {
errstat_set_strf(&iq->errstat, "Schema change was preempted");
return rc;
} else if (rc) {
if (rc == SC_ABORTED)
errstat_set_strf(&iq->errstat, "Schema change was aborted");
mark_schemachange_over_tran(s->tablename, NULL); // non-tran ??
if (bdb_set_schema_change_status(
NULL, s->tablename, iq->sc_seed, 0, NULL, 0, BDB_SC_ABORTED,
errstat_get_str(&iq->errstat), &bdberr) ||
bdberr != BDBERR_NOERROR) {
logmsg(LOGMSG_ERROR,
"%s: failed to set bdb schema change status, bdberr %d\n",
__func__, bdberr);
}
} else if (s->preempted == SC_ACTION_RESUME ||
s->kind == SC_ALTERTABLE_PENDING) {
s->finalize = 0;
rc = SC_COMMIT_PENDING;
} else if (s->finalize) {
int local_lock = 0;
if (!iq->sc_locked) {
wrlock_schema_lk();
local_lock = 1;
}
rc = do_finalize(post, iq, s, tran);
if (!rc && !s->is_osql) {
create_sqlmaster_records(tran);
create_sqlite_master();
}
if (local_lock)
unlock_schema_lk();
if (s->done_type == fastinit && gbl_replicate_local)
local_replicant_write_clear(iq, tran, s->db);
} else {
rc = SC_COMMIT_PENDING;
}
end:
s->sc_rc = rc;
return rc;
}
int do_alter_queues(struct schema_change_type *s, struct ireq *unused)
{
int rc;
set_original_tablename(s);
if (!s->resume) set_sc_flgs(s);
rc = propose_sc(s);
if (rc == SC_OK) rc = do_alter_queues_int(s);
if (master_downgrading(s)) return SC_MASTER_DOWNGRADE;
if (gbl_pushlogs_after_sc)
push_next_log();
return rc;
}
int do_alter_stripes(struct schema_change_type *s, struct ireq *unused)
{
int rc;
set_original_tablename(s);
if (!s->resume) set_sc_flgs(s);
rc = do_alter_stripes_int(s);
if (master_downgrading(s)) return SC_MASTER_DOWNGRADE;
if (gbl_pushlogs_after_sc)
push_next_log();
s->finalize = 1;
return rc;
}
char *get_ddl_type_str(struct schema_change_type *s)
{
char *sc_labels[] = {
"UNKNOWN", "ALTER QUEUE", "ALTER STRIPE", "QUEUE_DB",
"QUEUE_DB", "VIEW", "VIEW", "ADDSP",
"DELSP", "DEFAULTSP", "SHOWSP", "IS_TRIGGER",
"IS_TRIGGER", "IS_SFUNC", "IS_SFUNC", "IS_AFUNC",
"IS_AFUNC", "UPGRADE", "UPGRADE", "DROP",
"TRUNCATE", "CREATE", "RENAME", "ALIAS",
"ALTER", "ALTER_PENDING", "REBUILD", "ALTER_INDEX",
"DROP_INDEX", "REBUILD_INDEX"};
return sc_labels[s->kind];
}
char *get_ddl_csc2(struct schema_change_type *s)
{
return s->newcsc2 ? s->newcsc2 : "";
}
int do_add_view(struct ireq *iq, struct schema_change_type *s, tran_type *tran);
int do_drop_view(struct ireq *iq, struct schema_change_type *s,
tran_type *tran);
/* in sync with enum schema_change_kind ! */
struct {
int run_do_ddl;
scdone_t type;
ddl_t pre;
ddl_t post;
int (*non_std_pre)(struct schema_change_type *, struct ireq *);
int (*non_std_post)(struct schema_change_type *);
} do_schema_change_if[] = {
{0, 0, NULL, NULL, NULL, NULL},
{0, 0, NULL, NULL, do_alter_queues, NULL},
{0, 0, NULL, NULL, do_alter_stripes, NULL},
{1, add_queue_file, do_add_qdb_file, finalize_add_qdb_file, NULL, NULL},
{1, del_queue_file, do_del_qdb_file, finalize_del_qdb_file, NULL, NULL},
{1, user_view, do_add_view, finalize_add_view, NULL, NULL},
{1, user_view, do_drop_view, finalize_drop_view, NULL, NULL},
{0, 0, NULL, NULL, do_add_sp, finalize_add_sp},
{0, 0, NULL, NULL, do_del_sp, finalize_del_sp},
{0, 0, NULL, NULL, do_default_sp, finalize_default_sp},
{0, 0, NULL, NULL, do_show_sp, NULL},
{0, 0, NULL, NULL, perform_trigger_update, finalize_trigger},
{0, 0, NULL, NULL, perform_trigger_update, finalize_trigger},
{0, 0, NULL, NULL, do_lua_sfunc, finalize_lua_sfunc},
{0, 0, NULL, NULL, do_lua_sfunc, finalize_lua_sfunc},
{0, 0, NULL, NULL, do_lua_afunc, finalize_lua_afunc},
{0, 0, NULL, NULL, do_lua_afunc, finalize_lua_afunc},
{0, 0, NULL, NULL, do_upgrade_table, finalize_upgrade_table},
{0, 0, NULL, NULL, do_upgrade_table, finalize_upgrade_table},
{1, drop, do_drop_table, finalize_drop_table, NULL, NULL},
{1, fastinit, do_fastinit, finalize_fastinit_table, NULL, NULL},
{1, add, do_add_table, finalize_add_table, NULL, NULL},
{1, rename_table, do_rename_table, finalize_rename_table, NULL, NULL},
{1, alias_table, do_rename_table, finalize_alias_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
{1, alter, do_alter_table, finalize_alter_table, NULL, NULL},
};
static int do_schema_change_tran_int(sc_arg_t *arg)
{
struct ireq *iq = arg->iq;
tran_type *trans = arg->trans;
int rc = SC_OK;
struct schema_change_type *s = arg->sc;
free(arg);
if (iq == NULL) {
abort();
}
Pthread_mutex_lock(&s->mtx);
Pthread_mutex_lock(&s->mtxStart);
s->started = 1;
Pthread_cond_signal(&s->condStart);
Pthread_mutex_unlock(&s->mtxStart);
int detached = 0;
if (!bdb_iam_master(thedb->bdb_env) || thedb->master != gbl_myhostname) {
logmsg(LOGMSG_INFO, "%s downgraded master\n", __func__);
rc = SC_MASTER_DOWNGRADE;
goto downgraded;
}
s->iq = iq;
if (s->kind == SC_ALTERTABLE_PENDING || s->preempted == SC_ACTION_RESUME)
detached = 1;
if (s->resume && IS_ALTERTABLE(s) && s->preempted == SC_ACTION_PAUSE)
detached = 1;
/* initialize the scdone callback type here;
* we can updated it dynamically if needed
*/
s->done_type = do_schema_change_if[s->kind].type;
if (do_schema_change_if[s->kind].run_do_ddl) {
rc = do_ddl(do_schema_change_if[s->kind].pre,
do_schema_change_if[s->kind].post, iq, s, trans);
} else {
rc = do_schema_change_if[s->kind].non_std_pre(s, iq);
}
downgraded:
if (rc == SC_MASTER_DOWNGRADE) {
while (s->logical_livesc) {
poll(NULL, 0, 100);
}
if (s && s->newdb && s->newdb->handle) {
int bdberr;
if (!trans)
backend_thread_event(thedb, COMDB2_THR_EVENT_START_RDWR);
/* return NOMASTER for live schemachange writes */
sc_set_downgrading(s);
bdb_close_only(s->newdb->handle, &bdberr);
freedb(s->newdb);
s->newdb = NULL;
if (!trans)
backend_thread_event(thedb, COMDB2_THR_EVENT_DONE_RDWR);
}
}
if (rc && rc != SC_COMMIT_PENDING && s->preempted != SC_ACTION_RESUME) {
logmsg(LOGMSG_ERROR, ">>> SCHEMA CHANGE ERROR: TABLE %s, RC %d\n",
s->tablename, rc);
iq->sc_should_abort = 1;
}
if (detached || rc == SC_PREEMPTED)
s->sc_rc = SC_DETACHED;
else if (rc == SC_COMMIT_PENDING)
s->sc_rc = SC_OK;
else
s->sc_rc = rc;
if (!s->nothrevent) {
Pthread_mutex_lock(&sc_async_mtx);
sc_async_threads--;
Pthread_cond_broadcast(&sc_async_cond);
Pthread_mutex_unlock(&sc_async_mtx);
}
if (rc == SC_COMMIT_PENDING && (s->preempted == SC_ACTION_RESUME ||
s->kind == SC_ALTERTABLE_PENDING)) {
int bdberr = 0;
add_ongoing_alter(s);
if (bdb_set_schema_change_status(NULL, s->tablename, iq->sc_seed, 0,
NULL, 0, BDB_SC_COMMIT_PENDING, NULL,
&bdberr) ||
bdberr != BDBERR_NOERROR) {
logmsg(LOGMSG_ERROR,
"%s: failed to set bdb schema change status, bdberr %d\n",
__func__, bdberr);
}
Pthread_mutex_unlock(&s->mtx);
return 0;
} else if (s->resume == SC_NEW_MASTER_RESUME || rc == SC_COMMIT_PENDING ||
rc == SC_PREEMPTED || rc == SC_PAUSED ||
(!s->nothrevent && !s->finalize)) {
Pthread_mutex_unlock(&s->mtx);
return rc;
}
Pthread_mutex_unlock(&s->mtx);
if (!s->is_osql) {
if (rc == SC_MASTER_DOWNGRADE) {
sc_set_running(iq, s, s->tablename, 0, NULL, 0, __func__,
__LINE__);
free_sc(s);
} else {
stop_and_free_sc(iq, rc, s, 1);
}
}
return rc;
}
int do_schema_change_tran(sc_arg_t *arg)
{
struct schema_change_type *s = arg->sc;
enum thrtype oldtype = prepare_sc_thread(s);
int rc;
rc = do_schema_change_tran_int(arg);
reset_sc_thread(oldtype, s);
return rc;
}
int do_schema_change_tran_thd(sc_arg_t *arg)
{
comdb2_name_thread(__func__);
int rc;
bdb_state_type *bdb_state = thedb->bdb_env;
thread_started("schema_change");
bdb_thread_event(bdb_state, 1);
rc = do_schema_change_tran_int(arg);
bdb_thread_event(bdb_state, 0);
if (rc == SC_COMMIT_PENDING) {
rc = SC_OK;
}
return rc;
}
int do_schema_change_locked(struct schema_change_type *s, void *tran)
{
comdb2_name_thread(__func__);
int rc = 0;
struct ireq *iq = NULL;
iq = (struct ireq *)calloc(1, sizeof(*iq));
if (iq == NULL) {
logmsg(LOGMSG_ERROR, "%s: failed to malloc ireq\n", __func__);
return -1;
}
init_fake_ireq(thedb, iq);
iq->sc = s;
if (s->db == NULL) {
s->db = get_dbtable_by_name(s->tablename);
}
iq->usedb = s->db;
iq->sc_running = (s->set_running ? 1 : 0);
s->usedbtablevers = s->db ? s->db->tableversion : 0;
sc_arg_t *arg = malloc(sizeof(sc_arg_t));
arg->iq = iq;
arg->sc = s;
arg->trans = tran;
/* the only callers are lightweight timepartition events,
which already have schema lock */
arg->iq->sc_locked = 1;
rc = do_schema_change_tran(arg);
free(iq);
return rc;
}
int finalize_schema_change(struct ireq *iq, tran_type *trans)
{
if (iq == NULL || iq->sc == NULL) abort();
assert(iq->sc->tran == NULL || iq->sc->tran == trans);
struct schema_change_type *s = iq->sc;
Pthread_mutex_lock(&s->mtx);
enum thrtype oldtype = prepare_sc_thread(s);
int rc = SC_OK;
if (gbl_test_scindex_deadlock) {
logmsg(LOGMSG_INFO, "%s: sleeping for 30s\n", __func__);
sleep(30);
logmsg(LOGMSG_INFO, "%s: slept 30s\n", __func__);
}
if (do_schema_change_if[s->kind].run_do_ddl) {
rc = do_finalize(do_schema_change_if[s->kind].post, iq, s, trans);
} else {
rc = do_schema_change_if[s->kind].non_std_post(s);
}
reset_sc_thread(oldtype, s);
Pthread_mutex_unlock(&s->mtx);
if (!s->is_osql)
stop_and_free_sc(iq, rc, s, 0 /*free_sc*/);
return rc;
}
void *sc_resuming_watchdog(void *p)
{
comdb2_name_thread(__func__);
struct ireq iq;
struct schema_change_type *stored_sc = NULL;
int time = bdb_attr_get(thedb->bdb_attr, BDB_ATTR_SC_RESUME_WATCHDOG_TIMER);
logmsg(LOGMSG_INFO, "%s: started, sleeping %d seconds\n", __func__, time);
sleep(time);
logmsg(LOGMSG_INFO, "%s: waking up\n", __func__);
bdb_thread_event(thedb->bdb_env, BDBTHR_EVENT_START_RDWR);
init_fake_ireq(thedb, &iq);
Pthread_mutex_lock(&sc_resuming_mtx);
stored_sc = sc_resuming;
while (stored_sc) {
iq.sc = stored_sc;
if (iq.sc->db)
iq.sc->db->sc_abort = 1;
Pthread_mutex_lock(&(iq.sc->mtx));
stored_sc = stored_sc->sc_next;
logmsg(LOGMSG_INFO, "%s: aborting schema change of table '%s'\n",
__func__, iq.sc->tablename);
mark_schemachange_over(iq.sc->tablename);
if (iq.sc->kind == SC_ADDTABLE) {
delete_temp_table(&iq, iq.sc->db);
if (iq.sc->already_finalized) {
rem_dbtable_from_thedb_dbs(iq.sc->db);
}
}
/* TODO: (NC) Also delete view? */
sc_del_unused_files(iq.sc->db);
Pthread_mutex_unlock(&(iq.sc->mtx));
free_schema_change_type(iq.sc);
iq.sc = NULL;
}
sc_resuming = NULL;
logmsg(LOGMSG_INFO, "%s: existing\n", __func__);
bdb_thread_event(thedb->bdb_env, BDBTHR_EVENT_DONE_RDWR);
Pthread_mutex_unlock(&sc_resuming_mtx);
return NULL;
}
struct timepart_sc_resuming {
char *viewname;
struct schema_change_type *s;
int nshards;
};
void *resume_sc_multiddl_txn_finalize(void *p);
static int process_tpt_sc_hash(void *obj, void *arg)
{
struct timepart_sc_resuming *tpt_sc = (struct timepart_sc_resuming *)obj;
int rc;
pthread_t tid;
logmsg(LOGMSG_INFO, "%s: processing view '%s'\n", __func__,
tpt_sc->viewname);
struct ireq *iq = calloc(1, sizeof(struct ireq));
init_fake_ireq(thedb, iq);
iq->sc_pending = tpt_sc->s;
free(tpt_sc);
iq->tranddl = 1;
struct schema_change_type *s = iq->sc_pending;
while (s) {
if (s->set_running)
iq->sc_running++;
s = s->sc_next;
}
rc = pthread_create(&tid, &gbl_pthread_attr_detached,
resume_sc_multiddl_txn_finalize, iq);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to launch finalizing thread rc %d\n",
__func__, rc);
free(iq);
rc = -1;
}
return rc;
}
static int verify_sc_resumed_for_shard(const char *shardname,
timepart_view_t **pview,
timepart_sc_arg_t *arg)
{
struct schema_change_type *sc = NULL;
int rc;
assert(arg->s);
sc = arg->s;
/*shardname can be an alias*/
struct dbtable *db = get_dbtable_by_name(shardname);
if (db->sqlaliasname)
shardname = db->tablename; /* use the actual table name */
while (sc) {
/* already resumed */
if (strcasecmp(shardname, sc->tablename) == 0)
return 0;
sc = sc->sc_next;
}
/* start a new sc for shard that was not already resumed */
struct schema_change_type *new_sc = clone_schemachange_type(arg->s);
strncpy0(new_sc->tablename, shardname, sizeof(new_sc->tablename));
new_sc->iq = NULL;
new_sc->tran = NULL;
new_sc->resume = 0;
new_sc->must_resume = 1; /* this is a shard, we cannot complete partition sc without it */
new_sc->nothrevent = 0;
new_sc->finalize = 0;
/* put new_sc to linked list */
new_sc->sc_next = arg->s;
arg->s = new_sc;
logmsg(LOGMSG_USER, "Restarting schema change for view '%s' shard '%s'\n",
arg->part_name, new_sc->tablename);
rc = start_schema_change(new_sc);
if (rc != SC_OK) {
logmsg(LOGMSG_ERROR, "%s: failed to restart shard '%s', rc %d\n",
__func__, shardname, rc);
/* resume_sc_multiddl_txn_finalize will check rc */
new_sc->sc_rc = rc;
}
return 0;
}
static int verify_sc_resumed_for_all_shards(void *obj, void *arg)
{
struct timepart_sc_resuming *tpt_sc = (struct timepart_sc_resuming *)obj;
/* all shards resumed, including the next shard if any */
if (tpt_sc->nshards > timepart_get_num_shards(tpt_sc->viewname))
return 0;
timepart_sc_arg_t sc_arg = {0};
sc_arg.s = tpt_sc->s;
sc_arg.part_name = tpt_sc->viewname;
/* start new sc for shards that were not resumed */
sc_arg.check_extra_shard = 1;
sc_arg.lockless = 1;
timepart_foreach_shard(verify_sc_resumed_for_shard, &sc_arg);
tpt_sc->s = sc_arg.s;
return 0;
}
extern const uint8_t *osqlcomm_scl_get_key(struct sc_list *scl,
const uint8_t *p_buf, const uint8_t *p_buf_end);
extern const uint8_t *osqlcomm_scl_get(struct sc_list *scl,
const uint8_t *p_buf, const uint8_t *p_buf_end);
extern int osql_delete_sc_list(uuid_t uuid, tran_type *trans);
/**
* Receive an array of serialized sclists and removes them
*
*/