-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathname_server_impl.cc
More file actions
10531 lines (10221 loc) · 482 KB
/
name_server_impl.cc
File metadata and controls
10531 lines (10221 loc) · 482 KB
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 2021 4Paradigm
*
* 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 "nameserver/name_server_impl.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <set>
#include <vector>
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/time/time.h"
#include "nameserver/system_table.h"
#include "sdk/db_sdk.h"
#include "statistics/query_response_time/deploy_query_response_time.h"
#ifdef DISALLOW_COPY_AND_ASSIGN
#undef DISALLOW_COPY_AND_ASSIGN
#endif
#include <snappy.h>
#include <utility>
#include "base/glog_wrapper.h"
#include "base/proto_util.h"
#include "base/status.h"
#include "base/strings.h"
#include "boost/algorithm/string.hpp"
#include "boost/bind.hpp"
#include "codec/row_codec.h"
#include "gflags/gflags.h"
#include "schema/index_util.h"
#include "schema/schema_adapter.h"
DECLARE_string(endpoint);
DECLARE_string(zk_cluster);
DECLARE_string(zk_root_path);
DECLARE_string(tablet);
DECLARE_int32(zk_session_timeout);
DECLARE_int32(zk_keep_alive_check_interval);
DECLARE_int32(get_task_status_interval);
DECLARE_int32(name_server_task_pool_size);
DECLARE_int32(name_server_task_wait_time);
DECLARE_int32(max_op_num);
DECLARE_uint32(partition_num);
DECLARE_uint32(replica_num);
DECLARE_bool(auto_failover);
DECLARE_uint32(tablet_heartbeat_timeout);
DECLARE_uint32(tablet_offline_check_interval);
DECLARE_uint32(get_table_status_interval);
DECLARE_uint32(name_server_task_max_concurrency);
DECLARE_uint32(check_binlog_sync_progress_delta);
DECLARE_uint32(name_server_op_execute_timeout);
DECLARE_uint32(get_replica_status_interval);
DECLARE_int32(make_snapshot_time);
DECLARE_int32(make_snapshot_check_interval);
DECLARE_bool(use_name);
DECLARE_bool(enable_distsql);
DECLARE_uint32(sync_deploy_stats_timeout);
namespace openmldb {
namespace nameserver {
using ::openmldb::base::ReturnCode;
const std::string OFFLINE_LEADER_ENDPOINT = "OFFLINE_LEADER_ENDPOINT"; // NOLINT
constexpr uint8_t MAX_ADD_TABLE_FIELD_COUNT = 63;
constexpr uint32_t SEQ_TASK_CHECK_INTERVAL = 100; // 100ms
void NameServerImpl::CheckSyncExistTable(const std::string& alias,
const std::vector<::openmldb::nameserver::TableInfo>& tables_remote,
const std::shared_ptr<::openmldb::client::NsClient> ns_client) {
for (const TableInfo& table_info_remote : tables_remote) {
std::string name = table_info_remote.name();
std::string db = table_info_remote.db();
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info_local;
{
std::lock_guard<std::mutex> lock(mu_);
if (!GetTableInfoUnlock(name, db, &table_info_local)) {
PDLOG(WARNING, "table[%s] does not exist!", name.c_str());
continue;
}
}
bool is_continue = false;
// remote table
for (int idx = 0; idx < table_info_remote.table_partition_size(); idx++) {
const ::openmldb::nameserver::TablePartition& table_partition = table_info_remote.table_partition(idx);
for (int midx = 0; midx < table_partition.partition_meta_size(); midx++) {
if (table_partition.partition_meta(midx).is_leader() &&
(!table_partition.partition_meta(midx).is_alive())) {
PDLOG(WARNING,
"remote table [%s] has a no alive leader partition "
"pid[%u]",
name.c_str(), table_partition.pid());
is_continue = true;
break;
}
}
}
if (is_continue) {
PDLOG(WARNING, "table [%s] does not sync to replica cluster [%s]", name.c_str(), alias.c_str());
continue;
}
for (int idx = 0; idx < table_info_local->table_partition_size(); idx++) {
const ::openmldb::nameserver::TablePartition& table_partition_local =
table_info_local->table_partition(idx);
for (int midx = 0; midx < table_partition_local.partition_meta_size(); midx++) {
if (table_partition_local.partition_meta(midx).is_leader() &&
(!table_partition_local.partition_meta(midx).is_alive())) {
PDLOG(WARNING, "table [%s] pid [%u] has a no alive leader partition", name.c_str(),
table_partition_local.pid());
is_continue = true;
break;
}
}
}
if (is_continue) {
PDLOG(WARNING, "table [%s] does not sync to replica cluster [%s]", name.c_str(), alias.c_str());
continue;
}
{
std::lock_guard<std::mutex> lock(mu_);
for (int idx = 0; idx < table_info_remote.table_partition_size(); idx++) {
const ::openmldb::nameserver::TablePartition& table_partition = table_info_remote.table_partition(idx);
uint32_t cur_pid = table_partition.pid();
for (int midx = 0; midx < table_partition.partition_meta_size(); midx++) {
if (table_partition.partition_meta(midx).is_leader() &&
table_partition.partition_meta(midx).is_alive()) {
if (AddReplicaSimplyRemoteOP(alias, name, db, table_partition.partition_meta(midx).endpoint(),
table_info_remote.tid(), cur_pid) < 0) {
PDLOG(WARNING,
"create AddReplicasSimplyRemoteOP failed. "
"table[%s] pid[%u] alias[%s]",
name.c_str(), cur_pid, alias.c_str());
break;
}
}
}
}
}
}
}
void NameServerImpl::TableInfoToVec(
const std::map<std::string, std::shared_ptr<::openmldb::nameserver::TableInfo>>& table_infos,
const std::vector<uint32_t>& table_tid_vec, std::vector<::openmldb::nameserver::TableInfo>* local_table_info_vec) {
for (const auto& kv : table_infos) {
if (std::find(table_tid_vec.begin(), table_tid_vec.end(), kv.second->tid()) == table_tid_vec.end()) {
bool has_no_alive_leader_partition = false;
for (int idx = 0; idx < kv.second->table_partition_size(); idx++) {
const ::openmldb::nameserver::TablePartition& table_partition_local = kv.second->table_partition(idx);
for (int midx = 0; midx < table_partition_local.partition_meta_size(); midx++) {
if (table_partition_local.partition_meta(midx).is_leader() &&
(!table_partition_local.partition_meta(midx).is_alive())) {
has_no_alive_leader_partition = true;
PDLOG(WARNING,
"table [%s] pid [%u] has a no alive leader "
"partition",
kv.second->name().c_str(), table_partition_local.pid());
break;
}
}
if (has_no_alive_leader_partition) {
break;
}
}
if (!has_no_alive_leader_partition) {
local_table_info_vec->push_back(*(kv.second));
}
}
}
}
void NameServerImpl::CheckSyncTable(const std::string& alias,
const std::vector<::openmldb::nameserver::TableInfo> tables,
const std::shared_ptr<::openmldb::client::NsClient> ns_client) {
{
std::lock_guard<std::mutex> lock(mu_);
if (table_info_.empty() && db_table_info_.empty()) {
PDLOG(INFO, "leader cluster has no table");
return;
}
}
std::vector<uint32_t> table_tid_vec;
for (auto& rkv : tables) {
table_tid_vec.push_back(rkv.tid());
}
std::vector<::openmldb::nameserver::TableInfo> local_table_info_vec;
{
std::lock_guard<std::mutex> lock(mu_);
TableInfoToVec(table_info_, table_tid_vec, &local_table_info_vec);
for (const auto& kv : db_table_info_) {
TableInfoToVec(kv.second, table_tid_vec, &local_table_info_vec);
}
}
for (const auto& table_tmp : local_table_info_vec) {
::openmldb::nameserver::TableInfo table_info(table_tmp);
// get remote table_info: tid and leader partition info
std::string msg;
if (!ns_client->CreateRemoteTableInfo(zone_info_, table_info, msg)) {
PDLOG(WARNING, "create remote table_info erro, wrong msg is [%s]", msg.c_str());
return;
}
std::lock_guard<std::mutex> lock(mu_);
for (int idx = 0; idx < table_info.table_partition_size(); idx++) {
const ::openmldb::nameserver::TablePartition& table_partition = table_info.table_partition(idx);
AddReplicaRemoteOP(alias, table_info.name(), table_info.db(), table_partition, table_info.tid(),
table_partition.pid());
}
}
}
void NameServerImpl::CheckTableInfo(std::shared_ptr<ClusterInfo>& ci,
const std::vector<::openmldb::nameserver::TableInfo>& tables) {
for (const auto& table : tables) {
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info;
if (!GetTableInfoUnlock(table.name(), table.db(), &table_info)) {
PDLOG(WARNING, "table [%u][%s] not found in table_info", table.tid(), table.name().c_str());
continue;
}
auto status_iter = ci->last_status[table.db()].find(table.name());
if (status_iter == ci->last_status[table.db()].end()) {
std::vector<TablePartition> tbs;
for (const auto& part : table_info->table_partition()) {
for (const auto& meta : part.remote_partition_meta()) {
if (meta.alias() == ci->cluster_add_.alias()) {
TablePartition tb;
tb.set_pid(part.pid());
PartitionMeta* m = tb.add_partition_meta();
m->CopyFrom(meta);
tbs.push_back(tb);
break;
}
}
}
if (tbs.size() != table.partition_num()) {
continue;
}
ci->last_status[table.db()].insert(std::make_pair(table.name(), tbs));
} else {
// cache endpoint
std::set<uint32_t> parts;
for (const auto& part : table_info->table_partition()) {
for (auto& meta : part.partition_meta()) {
if (meta.is_leader() && meta.is_alive()) {
parts.insert(part.pid());
}
}
}
// cache endpoint && part reference
std::map<uint32_t, std::vector<TablePartition>::iterator> part_refer;
for (auto iter = status_iter->second.begin(); iter != status_iter->second.end(); iter++) {
part_refer.insert(std::make_pair(iter->pid(), iter));
}
for (const auto& part : table.table_partition()) {
if (parts.find(part.pid()) == parts.end()) {
PDLOG(WARNING, "table [%s] pid [%u] partition leader is offline", table.name().c_str(), part.pid());
continue; // leader partition is offline, can't add talbe
// replica
}
for (auto& meta : part.partition_meta()) {
if (meta.is_leader() && meta.is_alive()) {
auto iter = part_refer.find(part.pid());
if (iter == part_refer.end()) {
PDLOG(WARNING, "table [%s] pid [%u] not found", table.name().c_str(), part.pid());
break;
}
if (iter->second->partition_meta_size() < 1) {
PDLOG(WARNING, "table [%s] pid [$u] meta size is %d", table.name().c_str(), part.pid(),
iter->second->partition_meta_size());
break;
}
std::string endpoint = iter->second->partition_meta(0).endpoint();
if (meta.endpoint() == endpoint) {
break;
}
PDLOG(INFO, "table [%s] pid[%u] will remove endpoint %s", table.name().c_str(), part.pid(),
endpoint.c_str());
DelReplicaRemoteOP(endpoint, table.name(), table.db(), part.pid());
iter->second->clear_partition_meta();
iter->second->add_partition_meta()->CopyFrom(meta);
PDLOG(INFO, "table [%s] pid[%u] will add remote endpoint %s", table.name().c_str(), part.pid(),
meta.endpoint().c_str());
AddReplicaSimplyRemoteOP(ci->cluster_add_.alias(), table.name(), table.db(), meta.endpoint(),
table.tid(), part.pid());
break;
}
}
}
}
}
}
bool NameServerImpl::CompareSnapshotOffset(
const std::vector<TableInfo>& tables, std::string& msg, int& code,
std::map<std::string, std::map<uint32_t, std::map<uint32_t, uint64_t>>>& table_part_offset) {
for (const auto& table : tables) {
// iter == table_info_.end() is impossible, because CompareTableInfo has
// checked it
std::map<uint32_t, uint64_t> pid_offset;
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info;
if (!GetTableInfoUnlock(table.name(), table.db(), &table_info)) {
PDLOG(WARNING, "table [%s] not found in table_info", table.name().c_str());
return false;
}
int32_t tid = table_info->tid();
for (const auto& part : table_info->table_partition()) {
for (const auto& meta : part.partition_meta()) {
if (meta.is_alive() && meta.is_leader()) {
auto tablet_it = table_part_offset.find(meta.endpoint());
if (tablet_it == table_part_offset.end()) {
PDLOG(WARNING, "%s not found in table info", meta.endpoint().c_str());
msg = "tablet endpoint not found";
code = 411;
return false;
}
auto tid_it = tablet_it->second.find(tid);
if (tid_it == tablet_it->second.end()) {
PDLOG(WARNING, "tid [%u] not found on tablet %s", tid, meta.endpoint().c_str());
msg = "tid not found";
code = 412;
return false;
}
auto pid_it = tid_it->second.find(part.pid());
if (pid_it == tid_it->second.end()) {
PDLOG(WARNING, "tid [%u] pid [%u] not found on tablet %s", tid, part.pid(),
meta.endpoint().c_str());
msg = "pid not found";
code = 413;
return false;
}
pid_offset.insert(std::make_pair(part.pid(), pid_it->second));
}
}
}
// remote table
for (auto& part : table.table_partition()) {
auto offset_iter = pid_offset.find(part.pid());
if (offset_iter == pid_offset.end()) {
PDLOG(WARNING, "table [%s] pid [%u] is not found", table.name().c_str(), part.pid());
msg = "partition offline";
code = 407;
return false;
}
for (auto& meta : part.partition_meta()) {
if (meta.is_leader() && meta.is_alive()) {
if (meta.offset() < offset_iter->second) {
PDLOG(WARNING,
"table [%s] pid [%u] offset less than local "
"table snapshot",
table.name().c_str(), part.pid());
msg = "rep cluster offset too small";
code = 406;
return false;
}
break;
}
}
}
}
return true;
}
bool NameServerImpl::CompareTableInfo(const std::vector<::openmldb::nameserver::TableInfo>& tables, bool period_check) {
for (auto& table : tables) {
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info;
if (!GetTableInfoUnlock(table.name(), table.db(), &table_info)) {
PDLOG(WARNING, "table [%s] not found in table_info_", table.name().c_str());
if (period_check) {
continue;
}
return false;
}
if (table.table_partition_size() != table_info->table_partition_size()) {
PDLOG(WARNING, "table [%s] partition num not equal, remote [%d] local [%d]", table.name().c_str(),
table.table_partition_size(), table_info->table_partition_size());
return false;
}
if (table.compress_type() != table_info->compress_type()) {
PDLOG(WARNING, "table [%s] compress type not equal", table.name().c_str());
return false;
}
if (table.column_desc_size() != table_info->column_desc_size()) {
PDLOG(WARNING, "table [%s] column desc size not equal", table.name().c_str());
return false;
}
{
std::map<std::string, std::string> tmp_map;
for (int i = 0; i < table_info->column_desc_size(); i++) {
std::string name = table_info->column_desc(i).name();
std::string value;
table_info->column_desc(i).SerializeToString(&value);
tmp_map.insert(std::make_pair(name, value));
}
for (auto& column : table.column_desc()) {
auto iter = tmp_map.find(column.name());
if (iter == tmp_map.end()) {
PDLOG(WARNING,
"table [%s] not found column desc [%s] in local "
"cluster",
table.name().c_str(), column.name().c_str());
return false;
}
if (column.SerializeAsString() != iter->second) {
PDLOG(WARNING, "table [%s] column desc [%s] not equal", table.name().c_str(),
column.name().c_str());
return false;
}
}
}
if (table.column_desc_size() != table_info->column_desc_size()) {
PDLOG(WARNING, "table [%s] column desc v1 size not equal", table.name().c_str());
return false;
}
{
std::map<std::string, std::string> tmp_map;
for (int i = 0; i < table_info->column_desc_size(); i++) {
std::string name = table_info->column_desc(i).name();
std::string value;
table_info->column_desc(i).SerializeToString(&value);
tmp_map.insert(std::make_pair(name, value));
}
for (auto& column_v1 : table.column_desc()) {
auto iter = tmp_map.find(column_v1.name());
if (iter == tmp_map.end()) {
PDLOG(WARNING,
"table [%s] not found column desc [%s] in local "
"cluster",
table.name().c_str(), column_v1.name().c_str());
return false;
}
if (column_v1.SerializeAsString() != iter->second) {
PDLOG(WARNING, "table [%s] column desc [%s] not equal", table.name().c_str(),
column_v1.name().c_str());
return false;
}
}
}
if (table.column_key_size() != table_info->column_key_size()) {
PDLOG(WARNING, "table [%s] column key size not equal", table.name().c_str());
return false;
}
{
std::map<std::string, std::string> tmp_map;
for (int i = 0; i < table_info->column_key_size(); i++) {
std::string name = table_info->column_key(i).index_name();
std::string value;
table_info->column_key(i).SerializeToString(&value);
tmp_map.insert(std::make_pair(name, value));
}
for (auto& key : table.column_key()) {
auto iter = tmp_map.find(key.index_name());
if (iter == tmp_map.end()) {
PDLOG(WARNING,
"table [%s] not found column desc [%s] in local "
"cluster",
table.name().c_str(), key.index_name().c_str());
return false;
}
if (key.SerializeAsString() != iter->second) {
PDLOG(WARNING, "table [%s] column desc [%s] not equal", table.name().c_str(),
key.index_name().c_str());
return false;
}
}
}
if (table.added_column_desc_size() != table_info->added_column_desc_size()) {
PDLOG(WARNING, "table [%s] added column desc size not equal", table.name().c_str());
return false;
}
{
std::map<std::string, std::string> tmp_map;
for (int i = 0; i < table_info->added_column_desc_size(); i++) {
std::string name = table_info->added_column_desc(i).name();
std::string value;
table_info->added_column_desc(i).SerializeToString(&value);
tmp_map.insert(std::make_pair(name, value));
}
for (auto& added_column : table.added_column_desc()) {
auto iter = tmp_map.find(added_column.name());
if (iter == tmp_map.end()) {
PDLOG(WARNING,
"table [%s] not found column desc [%s] in local "
"cluster",
table.name().c_str(), added_column.name().c_str());
return false;
}
if (added_column.SerializeAsString() != iter->second) {
PDLOG(WARNING, "table [%s] column desc [%s] not equal", table.name().c_str(),
added_column.name().c_str());
return false;
}
}
}
}
return true;
}
NameServerImpl::NameServerImpl()
: zk_client_(nullptr),
dist_lock_(nullptr),
thread_pool_(1),
task_thread_pool_(FLAGS_name_server_task_pool_size),
rand_(0xdeadbeef),
startup_mode_(::openmldb::type::StartupMode::kStandalone) {}
NameServerImpl::~NameServerImpl() {
running_.store(false, std::memory_order_release);
thread_pool_.Stop(true);
task_thread_pool_.Stop(true);
if (dist_lock_ != NULL) {
dist_lock_->Stop();
delete dist_lock_;
}
delete zk_client_;
}
// become name server leader
bool NameServerImpl::Recover() {
if (startup_mode_ == ::openmldb::type::StartupMode::kStandalone) {
PDLOG(INFO, "skip recover in standalone mode");
return true;
}
std::vector<std::string> endpoints;
if (!zk_client_->GetNodes(endpoints)) {
PDLOG(WARNING, "get endpoints node failed!");
return false;
}
{
std::lock_guard<std::mutex> lock(mu_);
std::string value;
if (zk_client_->GetNodeValue(zk_path_.zone_data_path_ + "/follower", value)) {
zone_info_.ParseFromString(value);
mode_.store(zone_info_.mode(), std::memory_order_release);
PDLOG(WARNING, "recover zone info : %s", value.c_str());
}
UpdateTablets(endpoints);
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.table_index_node_, value)) {
if (!zk_client_->CreateNode(zk_path_.table_index_node_, "1")) {
PDLOG(WARNING, "create table index node failed!");
return false;
}
table_index_ = 1;
PDLOG(INFO, "init table_index[%u]", table_index_);
} else {
table_index_ = std::stoull(value);
PDLOG(INFO, "recover table_index[%u]", table_index_);
}
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.term_node_, value)) {
if (!zk_client_->CreateNode(zk_path_.term_node_, "1")) {
PDLOG(WARNING, "create term node failed!");
return false;
}
term_ = 1;
PDLOG(INFO, "init term[%lu]", term_);
} else {
term_ = std::stoull(value);
PDLOG(INFO, "recover term[%u]", term_);
}
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.op_index_node_, value)) {
if (!zk_client_->CreateNode(zk_path_.op_index_node_, "0")) {
PDLOG(WARNING, "create op index node failed!");
return false;
}
op_index_ = 0;
PDLOG(INFO, "init op_index[%u]", op_index_);
} else {
op_index_ = std::stoull(value);
PDLOG(INFO, "recover op_index[%u]", op_index_);
}
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.table_changed_notify_node_, value)) {
if (!zk_client_->CreateNode(zk_path_.table_changed_notify_node_, "1")) {
PDLOG(WARNING, "create zk table changed notify node failed");
return false;
}
}
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.globalvar_changed_notify_node_, value)) {
if (!zk_client_->CreateNode(zk_path_.globalvar_changed_notify_node_, "1")) {
PDLOG(WARNING, "create globalvar changed notify node failed");
return false;
}
}
if (!zk_client_->GetNodeValue(zk_path_.auto_failover_node_, value)) {
auto_failover_.load(std::memory_order_acquire) ? value = "true" : value = "false";
if (!zk_client_->CreateNode(zk_path_.auto_failover_node_, value)) {
PDLOG(WARNING, "create auto failover node failed!");
return false;
}
PDLOG(INFO, "set zk_auto_failover_node[%s]", value.c_str());
} else {
value == "true" ? auto_failover_.store(true, std::memory_order_release)
: auto_failover_.store(false, std::memory_order_release);
PDLOG(INFO, "get zk_auto_failover_node[%s]", value.c_str());
}
if (!RecoverDb()) {
PDLOG(WARNING, "recover db failed!");
return false;
}
if (!RecoverTableInfo()) {
PDLOG(WARNING, "recover table info failed!");
return false;
}
if (!RecoverProcedureInfo()) {
PDLOG(WARNING, "recover store procedure info failed!");
return false;
}
UpdateSdkEpMap();
}
UpdateTableStatus();
{
std::lock_guard<std::mutex> lock(mu_);
RecoverClusterInfo();
if (!RecoverOPTask()) {
PDLOG(WARNING, "recover task failed!");
return false;
}
RecoverOfflineTablet();
}
UpdateRealEpMapToTablet(false);
if (FLAGS_use_name) {
UpdateRemoteRealEpMap();
}
UpdateTaskStatus(true);
if (!RecoverExternalFunction()) {
return false;
}
return true;
}
bool NameServerImpl::RecoverExternalFunction() {
std::vector<std::string> functions;
if (zk_client_->IsExistNode(zk_path_.external_function_path_) == 0) {
if (!zk_client_->GetChildren(zk_path_.external_function_path_, functions)) {
LOG(WARNING) << "fail to get function list with path " << zk_path_.external_function_path_;
return false;
}
}
external_fun_.clear();
if (functions.empty()) {
return true;
}
for (const auto& name : functions) {
std::string value;
if (!zk_client_->GetNodeValue(zk_path_.external_function_path_ + "/" + name, value)) {
LOG(WARNING) << "fail to get function data. function: " << name;
continue;
}
auto fun = std::make_shared<::openmldb::common::ExternalFun>();
if (!fun->ParseFromString(value)) {
LOG(WARNING) << "fail to parse external function. function: " << name << " value: " << value;
continue;
}
external_fun_.emplace(name, fun);
LOG(INFO) << "recover function " << name;
}
return true;
}
bool NameServerImpl::RecoverDb() {
databases_.clear();
std::vector<std::string> db_vec;
if (!zk_client_->GetChildren(zk_path_.db_path_, db_vec)) {
if (zk_client_->IsExistNode(zk_path_.db_path_) > 0) {
PDLOG(WARNING, "db node does not exist");
return true;
}
PDLOG(WARNING, "get db failed!");
return false;
}
PDLOG(INFO, "recover db num[%d]", db_vec.size());
databases_.insert(db_vec.begin(), db_vec.end());
return true;
}
void NameServerImpl::RecoverOfflineTablet() {
offline_endpoint_map_.clear();
for (const auto& tablet : tablets_) {
if (tablet.second->state_ != ::openmldb::type::EndpointState::kHealthy) {
offline_endpoint_map_.insert(std::make_pair(tablet.first, tablet.second->ctime_));
thread_pool_.DelayTask(FLAGS_tablet_offline_check_interval,
boost::bind(&NameServerImpl::OnTabletOffline, this, tablet.first, false));
PDLOG(INFO, "recover offlinetablet. endpoint %s", tablet.first.c_str());
}
}
}
void NameServerImpl::RecoverClusterInfo() {
nsc_.clear();
std::vector<std::string> cluster_vec;
if (!zk_client_->GetChildren(zk_path_.zone_data_path_ + "/replica", cluster_vec)) {
if (zk_client_->IsExistNode(zk_path_.zone_data_path_ + "/replica") > 0) {
PDLOG(WARNING, "cluster info node does not exist");
return;
}
PDLOG(WARNING, "get cluster info failed!");
return;
}
PDLOG(INFO, "need to recover cluster info[%d]", cluster_vec.size());
std::string value, rpc_msg;
for (const auto& alias : cluster_vec) {
value.clear();
if (!zk_client_->GetNodeValue(zk_path_.zone_data_path_ + "/replica/" + alias, value)) {
PDLOG(WARNING, "get cluster info failed! name[%s]", alias.c_str());
continue;
}
::openmldb::nameserver::ClusterAddress cluster_add;
cluster_add.ParseFromString(value);
std::shared_ptr<::openmldb::nameserver::ClusterInfo> cluster_info =
std::make_shared<::openmldb::nameserver::ClusterInfo>(cluster_add);
PDLOG(INFO, "zk add %s|%s", cluster_add.zk_endpoints().c_str(), cluster_add.zk_path().c_str());
cluster_info->state_ = kClusterHealthy;
if (cluster_info->Init(rpc_msg) != 0) {
PDLOG(WARNING, "%s init failed, error: %s", alias.c_str(), rpc_msg.c_str());
// todo :: add cluster status, need show in showreplica
cluster_info->state_ = kClusterOffline;
}
nsc_.insert(std::make_pair(alias, cluster_info));
}
}
bool NameServerImpl::RecoverTableInfo() {
table_info_.clear();
db_table_info_.clear();
std::vector<std::string> table_vec;
std::vector<std::string> db_table_vec;
if (!zk_client_->GetChildren(zk_path_.table_data_path_, table_vec)) {
if (zk_client_->IsExistNode(zk_path_.table_data_path_) > 0) {
PDLOG(WARNING, "table data node does not exist");
} else {
PDLOG(WARNING, "get table name failed!");
return false;
}
}
PDLOG(INFO, "need to recover default table num[%d]", table_vec.size());
for (const auto& table_name : table_vec) {
std::string table_name_node = zk_path_.table_data_path_ + "/" + table_name;
std::string value;
if (!zk_client_->GetNodeValue(table_name_node, value)) {
PDLOG(WARNING, "get table info failed! name[%s] table node[%s]", table_name.c_str(),
table_name_node.c_str());
continue;
}
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info =
std::make_shared<::openmldb::nameserver::TableInfo>();
if (!table_info->ParseFromString(value)) {
PDLOG(WARNING, "parse table info failed! name[%s] value[%s] value size[%d]", table_name.c_str(),
value.c_str(), value.length());
continue;
}
table_info_.insert(std::make_pair(table_name, table_info));
PDLOG(INFO, "recover table[%s] success", table_name.c_str());
}
if (!zk_client_->GetChildren(zk_path_.db_table_data_path_, db_table_vec)) {
if (zk_client_->IsExistNode(zk_path_.db_table_data_path_) > 0) {
PDLOG(WARNING, "db table data node does not exist");
} else {
PDLOG(WARNING, "get db table id failed!");
return false;
}
}
PDLOG(INFO, "need to recover db table num[%d]", db_table_vec.size());
for (const auto& tid : db_table_vec) {
std::string tid_node = zk_path_.db_table_data_path_ + "/" + tid;
std::string value;
if (!zk_client_->GetNodeValue(tid_node, value)) {
PDLOG(WARNING, "get db table info failed! tid[%s] table node[%s]", tid.c_str(), tid_node.c_str());
continue;
}
std::shared_ptr<::openmldb::nameserver::TableInfo> table_info =
std::make_shared<::openmldb::nameserver::TableInfo>();
if (!table_info->ParseFromString(value)) {
PDLOG(WARNING, "parse table info failed! tid[%s] value[%s] value size[%d]", tid.c_str(), value.c_str(),
value.length());
continue;
}
if (databases_.find(table_info->db()) != databases_.end()) {
db_table_info_[table_info->db()].insert(std::make_pair(table_info->name(), table_info));
LOG(INFO) << "recover table tid " << tid << " with name " << table_info->name() << " in db "
<< table_info->db();
} else {
LOG(WARNING) << "table " << table_info->name() << " not exist on recovering in db " << table_info->db();
}
}
return true;
}
bool NameServerImpl::RecoverOPTask() {
for (auto& op_list : task_vec_) {
op_list.clear();
}
std::vector<std::string> op_vec;
if (!zk_client_->GetChildren(zk_path_.op_data_path_, op_vec)) {
if (zk_client_->IsExistNode(zk_path_.op_data_path_) > 0) {
PDLOG(WARNING, "op data node does not exist");
return true;
}
PDLOG(WARNING, "get op failed!");
return false;
}
PDLOG(INFO, "need to recover op num[%d]", op_vec.size());
for (const auto& op_id : op_vec) {
std::string op_node = zk_path_.op_data_path_ + "/" + op_id;
std::string value;
if (!zk_client_->GetNodeValue(op_node, value)) {
PDLOG(WARNING, "get table info failed! table node[%s]", op_node.c_str());
continue;
}
std::shared_ptr<OPData> op_data = std::make_shared<OPData>();
if (!op_data->op_info_.ParseFromString(value)) {
PDLOG(WARNING, "parse op info failed! value[%s]", value.c_str());
continue;
}
if (op_data->op_info_.task_status() == ::openmldb::api::TaskStatus::kDone) {
DEBUGLOG("op status is kDone. op_id[%lu]", op_data->op_info_.op_id());
continue;
}
if (op_data->op_info_.task_status() == ::openmldb::api::TaskStatus::kCanceled) {
DEBUGLOG("op status is kCanceled. op_id[%lu]", op_data->op_info_.op_id());
continue;
}
std::string op_type_str = ::openmldb::api::OPType_Name(op_data->op_info_.op_type());
switch (op_data->op_info_.op_type()) {
case ::openmldb::api::OPType::kMakeSnapshotOP:
if (CreateMakeSnapshotOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kAddReplicaOP:
if (CreateAddReplicaOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kChangeLeaderOP:
if (CreateChangeLeaderOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kMigrateOP:
if (CreateMigrateTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kRecoverTableOP:
if (CreateRecoverTableOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kOfflineReplicaOP:
if (CreateOfflineReplicaTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kDelReplicaOP:
if (CreateDelReplicaOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kReAddReplicaOP:
if (CreateReAddReplicaTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kReAddReplicaNoSendOP:
if (CreateReAddReplicaNoSendTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kReAddReplicaWithDropOP:
if (CreateReAddReplicaWithDropTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kReAddReplicaSimplifyOP:
if (CreateReAddReplicaSimplifyTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kReLoadTableOP:
if (CreateReLoadTableTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kUpdatePartitionStatusOP:
if (CreateUpdatePartitionStatusOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kCreateTableRemoteOP:
if (CreateTableRemoteTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kDropTableRemoteOP:
if (DropTableRemoteTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kDelReplicaRemoteOP:
if (CreateDelReplicaRemoteOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kAddReplicaSimplyRemoteOP:
if (CreateAddReplicaSimplyRemoteOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kAddReplicaRemoteOP:
if (CreateAddReplicaRemoteOPTask(op_data) < 0) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
case ::openmldb::api::OPType::kAddIndexOP:
if (!CreateAddIndexOPTask(op_data).OK()) {
PDLOG(WARNING, "recover op[%s] failed. op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
break;
default:
PDLOG(WARNING, "unsupport recover op[%s]! op_id[%lu]", op_type_str.c_str(), op_id);
continue;
}
if (!SkipDoneTask(op_data)) {
PDLOG(WARNING, "SkipDoneTask task failed. op_id[%lu] task_index[%u]", op_data->op_info_.op_id(),
op_data->op_info_.task_index());
continue;
}
if (op_data->op_info_.task_status() == ::openmldb::api::TaskStatus::kFailed ||
op_data->op_info_.task_status() == ::openmldb::api::TaskStatus::kCanceled) {
done_op_list_.push_back(op_data);
} else {
uint32_t idx = 0;
if (op_data->op_info_.for_replica_cluster() == 1) {
idx = op_data->op_info_.vec_idx();
PDLOG(INFO,
"current task is for replica cluster, op_index [%lu] "
"op_type[%s]",
op_data->op_info_.op_id(), ::openmldb::api::OPType_Name(op_data->op_info_.op_type()).c_str());
} else {
idx = op_data->op_info_.pid() % task_vec_.size();
if (op_data->op_info_.has_vec_idx() && op_data->op_info_.vec_idx() < task_vec_.size()) {
idx = op_data->op_info_.vec_idx();
}
}
task_vec_[idx].push_back(op_data);
}
PDLOG(INFO, "recover op[%s] success. op_id[%lu]",
::openmldb::api::OPType_Name(op_data->op_info_.op_type()).c_str(), op_data->op_info_.op_id());
}
for (auto& op_list : task_vec_) {
op_list.sort([](const std::shared_ptr<OPData>& a, const std::shared_ptr<OPData>& b) {
if (a->op_info_.parent_id() < b->op_info_.parent_id()) {
return true;
} else if (a->op_info_.parent_id() > b->op_info_.parent_id()) {