forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_cluster.cc
813 lines (747 loc) · 24.2 KB
/
pika_cluster.cc
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
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_rm.h"
#include "include/pika_table.h"
#include "include/pika_server.h"
#include "include/pika_cluster.h"
#include "include/pika_cmd_table_manager.h"
extern PikaReplicaManager* g_pika_rm;
extern PikaServer* g_pika_server;
extern PikaConf* g_pika_conf;
const std::string PkClusterInfoCmd::kSlotSection = "slot";
const std::string PkClusterInfoCmd::kTableSection = "table";
// pkcluster info slot table:slot
// pkcluster info table
// pkcluster info node
// pkcluster info cluster
void PkClusterInfoCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePkClusterInfo);
return;
}
if (g_pika_conf->classic_mode()) {
res_.SetRes(CmdRes::kErrOther, "PkClusterInfo only support on sharding mode");
return;
}
if (!strcasecmp(argv_[2].data(), kSlotSection.data())) {
info_section_ = kInfoSlot;
if (!ParseInfoSlotSubCmd()) {
return;
}
} else if (!strcasecmp(argv_[2].data(), kTableSection.data())) {
info_section_ = kInfoTable;
if (!ParseInfoTableSubCmd()) {
return;
}
} else {
info_section_ = kInfoErr;
}
return;
}
void PkClusterInfoCmd::Do(std::shared_ptr<Partition> partition) {
std::string info;
switch (info_section_) {
case kInfoSlot:
if (info_range_ == kAll) {
ClusterInfoSlotAll(&info);
} else if (info_range_ == kRange) {
// doesn't process error, if error return nothing
ClusterInfoSlotRange(table_name_, slots_, &info);
}
break;
case kInfoTable:
if (info_range_ == kAll) {
ClusterInfoTableAll(&info);
} else if (info_range_ == kSingle) {
ClusterInfoTable(&info);
}
default:
break;
}
res_.AppendStringLen(info.size());
res_.AppendContent(info);
return;
}
void PkClusterInfoCmd::ClusterInfoTableAll(std::string* info) {
std::stringstream tmp_stream;
std::vector<TableStruct> table_structs = g_pika_conf->table_structs();
std::unordered_map<std::string, QpsStatistic> table_stat = g_pika_server->ServerAllTableStat();
for (const auto& table_struct : table_structs) {
std::string table_id = table_struct.table_name.substr(2);
tmp_stream << "table_id: " << table_id << "\r\n";
tmp_stream << " partition_num: " << table_struct.partition_num << "\r\n";
QpsStatistic qps = table_stat[table_struct.table_name];
tmp_stream << " total_commands_processed:" << qps.querynum.load() << "\r\n";
tmp_stream << " total_write_commands_processed:" << qps.write_querynum.load() << "\r\n";
tmp_stream << " qps: " << qps.last_sec_querynum << "\r\n";
tmp_stream << " write_qps: " << qps.last_sec_write_querynum << "\r\n";
tmp_stream << " read_qps: " << qps.last_sec_querynum - qps.last_sec_write_querynum << "\r\n";
}
info->append(tmp_stream.str());
}
void PkClusterInfoCmd::ClusterInfoTable(std::string* info) {
std::stringstream tmp_stream;
std::vector<TableStruct> table_structs = g_pika_conf->table_structs();
for (const auto& table_struct : table_structs) {
if (table_struct.table_name == table_name_) {
std::string table_id = table_struct.table_name.substr(2);
tmp_stream << "table_id: " << table_id << "\r\n";
tmp_stream << " partition_num: " << table_struct.partition_num << "\r\n";
QpsStatistic qps = g_pika_server->ServerTableStat(table_name_);
tmp_stream << " qps: " << qps.last_sec_querynum << "\r\n";
tmp_stream << " write_qps: " << qps.last_sec_write_querynum << "\r\n";
tmp_stream << " read_qps: " << qps.last_sec_querynum - qps.last_sec_write_querynum << "\r\n";
break;
}
}
info->append(tmp_stream.str());
}
bool PkClusterInfoCmd::ParseInfoSlotSubCmd() {
if (argv_.size() > 3) {
if (argv_.size() == 4) {
info_range_ = kRange;
std::string tmp(argv_[3]);
size_t pos = tmp.find(':');
std::string slot_num_str;
if (pos == std::string::npos) {
table_name_ = g_pika_conf->default_table();
slot_num_str = tmp;
} else {
table_name_ = tmp.substr(0, pos);
slot_num_str = tmp.substr(pos + 1);
}
if (!ParseSlotGroup(slot_num_str, &slots_).ok()) {
res_.SetRes(CmdRes::kInvalidParameter, kCmdNamePkClusterInfo);
return false;
}
} else {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePkClusterInfo);
return false;
}
}
return true;
}
bool PkClusterInfoCmd::ParseInfoTableSubCmd() {
if (argv_.size() == 3) {
info_range_ = kAll;
} else if (argv_.size() == 4) {
std::string tmp(argv_[3]);
uint64_t table_id;
if (!slash::string2ul(tmp.c_str(), tmp.size(), &table_id)) {
res_.SetRes(CmdRes::kInvalidParameter, kCmdNamePkClusterInfo);
return false;
}
table_name_ = "db" + tmp;
info_range_ = kSingle;
} else if (argv_.size() > 4) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePkClusterInfo);
return false;
}
return true;
}
void PkClusterInfoCmd::ClusterInfoSlotRange(const std::string& table_name,
const std::set<uint32_t> slots, std::string* info) {
std::stringstream tmp_stream;
for (auto partition_id : slots) {
std::string p_info;
Status s = GetSlotInfo(table_name, partition_id, &p_info);
if (!s.ok()) {
continue;
}
tmp_stream << p_info;
}
info->append(tmp_stream.str());
}
void PkClusterInfoCmd::ClusterInfoSlotAll(std::string* info) {
std::stringstream tmp_stream;
for (const auto& table_item : g_pika_server->tables_) {
slash::RWLock partition_rwl(&table_item.second->partitions_rw_, false);
for (const auto& partition_item : table_item.second->partitions_) {
std::string table_name = table_item.second->GetTableName();
uint32_t partition_id = partition_item.second->GetPartitionId();
std::string p_info;
Status s = GetSlotInfo(table_name, partition_id, &p_info);
if (!s.ok()) {
continue;
}
tmp_stream << p_info;
}
}
info->append(tmp_stream.str());
}
Status PkClusterInfoCmd::GetSlotInfo(const std::string table_name,
uint32_t partition_id,
std::string* info) {
std::shared_ptr<SyncMasterPartition> partition =
g_pika_rm->GetSyncMasterPartitionByName(PartitionInfo(table_name, partition_id));
if (!partition) {
return Status::NotFound("not found");
}
Status s;
std::stringstream tmp_stream;
// binlog offset section
uint32_t filenum = 0;
uint64_t offset = 0;
partition->Logger()->GetProducerStatus(&filenum, &offset);
tmp_stream << partition->PartitionName() << " binlog_offset="
<< filenum << " " << offset;
// safety purge section
std::string safety_purge;
std::shared_ptr<SyncMasterPartition> master_partition =
g_pika_rm->GetSyncMasterPartitionByName(PartitionInfo(table_name, partition_id));
if (!master_partition) {
LOG(WARNING) << "Sync Master Partition: " << table_name << ":" << partition_id
<< ", NotFound";
s = Status::NotFound("SyncMasterPartition NotFound");
} else {
master_partition->GetSafetyPurgeBinlog(&safety_purge);
}
tmp_stream << ",safety_purge=" << (s.ok() ? safety_purge : "error") << "\r\n";
if (g_pika_conf->consensus_level()) {
LogOffset last_log = master_partition->ConsensusLastIndex();
tmp_stream << " consensus_last_log=" << last_log.ToString() << "\r\n";
}
// partition info section
std::string p_info;
s = g_pika_rm->GetPartitionInfo(table_name, partition_id, &p_info);
if (!s.ok()) {
return s;
}
tmp_stream << p_info;
info->append(tmp_stream.str());
return Status::OK();
}
Status ParseSlotGroup(const std::string& slot_group,
std::set<uint32_t>* slots) {
std::set<uint32_t> tmp_slots;
int64_t slot_idx, start_idx, end_idx;
std::string::size_type pos;
std::vector<std::string> elems;
slash::StringSplit(slot_group, COMMA, elems);
for (const auto& elem : elems) {
if ((pos = elem.find("-")) == std::string::npos) {
if (!slash::string2l(elem.data(), elem.size(), &slot_idx)
|| slot_idx < 0) {
return Status::Corruption("syntax error");
} else {
tmp_slots.insert(static_cast<uint32_t>(slot_idx));
}
} else {
if (pos == 0 || pos == (elem.size() - 1)) {
return Status::Corruption("syntax error");
} else {
std::string start_pos = elem.substr(0, pos);
std::string end_pos = elem.substr(pos + 1, elem.size() - pos);
if (!slash::string2l(start_pos.data(), start_pos.size(), &start_idx)
|| !slash::string2l(end_pos.data(), end_pos.size(), &end_idx)
|| start_idx < 0 || end_idx < 0 || start_idx > end_idx) {
return Status::Corruption("syntax error");
}
for (int64_t idx = start_idx; idx <= end_idx; ++idx) {
tmp_slots.insert(static_cast<uint32_t>(idx));
}
}
}
}
slots->swap(tmp_slots);
return Status::OK();
}
void SlotParentCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
if (g_pika_conf->classic_mode()) {
res_.SetRes(CmdRes::kErrOther, "PkClusterAddSlots/PkClusterDelSlots only support on sharding mode");
return;
}
Status s = ParseSlotGroup(argv_[2], &slots_);
if (!s.ok()) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
if (argv_.size() == 3) {
table_name_ = g_pika_conf->default_table();
} else if (argv_.size() == 4) {
uint64_t table_id;
if (!slash::string2ul(argv_[3].data(), argv_[3].size(), &table_id)) {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
table_name_ = "db";
table_name_ += std::to_string(table_id);
} else {
res_.SetRes(CmdRes::kSyntaxErr, "too many argument");
return;
}
for (const auto& slot_id : slots_) {
p_infos_.insert(PartitionInfo(table_name_, slot_id));
}
}
/*
* pkcluster addslots 0-3,8-11
* pkcluster addslots 0-3,8,9,10,11
* pkcluster addslots 0,2,4,6,8,10,12,14
*/
void PkClusterAddSlotsCmd::DoInitial() {
SlotParentCmd::DoInitial();
if (!res_.ok()) {
return;
}
}
void PkClusterAddSlotsCmd::Do(std::shared_ptr<Partition> partition) {
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (!table_ptr) {
res_.SetRes(CmdRes::kErrOther, "Internal error: table not found!");
return;
}
SlotState expected = INFREE;
if (!std::atomic_compare_exchange_strong(&g_pika_server->slot_state_,
&expected, INBUSY)) {
res_.SetRes(CmdRes::kErrOther,
"Slot in syncing or a change operation is under way, retry later");
return;
}
bool pre_success = true;
Status s = AddSlotsSanityCheck();
if (!s.ok()) {
LOG(WARNING) << "Addslots sanity check failed: " << s.ToString();
pre_success = false;
}
if (pre_success) {
s = g_pika_conf->AddTablePartitions(table_name_, slots_);
if (!s.ok()) {
LOG(WARNING) << "Addslots add to pika conf failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = table_ptr->AddPartitions(slots_);
if (!s.ok()) {
LOG(WARNING) << "Addslots add to table partition failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = g_pika_rm->AddSyncPartition(p_infos_);
if (!s.ok()) {
LOG(WARNING) << "Addslots add to sync partition failed: " << s.ToString();
pre_success = false;
}
}
g_pika_server->slot_state_.store(INFREE);
if (!pre_success) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
res_.SetRes(CmdRes::kOk);
LOG(INFO) << "Pika meta file overwrite success";
}
Status PkClusterAddSlotsCmd::AddSlotsSanityCheck() {
Status s = g_pika_conf->TablePartitionsSanityCheck(table_name_, slots_, true);
if (!s.ok()) {
return s;
}
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (!table_ptr) {
return Status::NotFound("table not found!");
}
for (uint32_t id : slots_) {
std::shared_ptr<Partition> partition_ptr = table_ptr->GetPartitionById(id);
if (partition_ptr != nullptr) {
return Status::Corruption("partition " + std::to_string(id) + " already exist");
}
}
s = g_pika_rm->AddSyncPartitionSanityCheck(p_infos_);
if (!s.ok()) {
return s;
}
return Status::OK();
}
/* pkcluster delslots 0-3,8-11
* pkcluster delslots 0-3,8,9,10,11
* pkcluster delslots 0,2,4,6,8,10,12,14
*/
void PkClusterDelSlotsCmd::DoInitial() {
SlotParentCmd::DoInitial();
if (!res_.ok()) {
return;
}
}
void PkClusterDelSlotsCmd::Do(std::shared_ptr<Partition> partition) {
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (!table_ptr) {
res_.SetRes(CmdRes::kErrOther, "Internal error: default table not found!");
return;
}
SlotState expected = INFREE;
if (!std::atomic_compare_exchange_strong(&g_pika_server->slot_state_,
&expected, INBUSY)) {
res_.SetRes(CmdRes::kErrOther,
"Slot in syncing or a change operation is under way, retry later");
return;
}
bool pre_success = true;
Status s = RemoveSlotsSanityCheck();
if (!s.ok()) {
LOG(WARNING) << "Removeslots sanity check failed: " << s.ToString();
pre_success = false;
}
// remove order maters
if (pre_success) {
s = g_pika_conf->RemoveTablePartitions(table_name_, slots_);
if (!s.ok()) {
LOG(WARNING) << "Removeslots remove from pika conf failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = g_pika_rm->RemoveSyncPartition(p_infos_);
if (!s.ok()) {
LOG(WARNING) << "Remvoeslots remove from sync partition failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = table_ptr->RemovePartitions(slots_);
if (!s.ok()) {
LOG(WARNING) << "Removeslots remove from table partition failed: " << s.ToString();
pre_success = false;
}
}
g_pika_server->slot_state_.store(INFREE);
if (!pre_success) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
res_.SetRes(CmdRes::kOk);
LOG(INFO) << "Pika meta file overwrite success";
}
Status PkClusterDelSlotsCmd::RemoveSlotsSanityCheck() {
Status s = g_pika_conf->TablePartitionsSanityCheck(table_name_, slots_, false);
if (!s.ok()) {
return s;
}
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (!table_ptr) {
return Status::NotFound("table not found");
}
for (uint32_t id : slots_) {
std::shared_ptr<Partition> partition_ptr = table_ptr->GetPartitionById(id);
if (partition_ptr == nullptr) {
return Status::Corruption("partition " + std::to_string(id) + " not found");
}
}
s = g_pika_rm->RemoveSyncPartitionSanityCheck(p_infos_);
if (!s.ok()) {
return s;
}
return Status::OK();
}
/* pkcluster slotsslaveof no one [0-3,8-11 | all] [table_id]
* pkcluster slotsslaveof ip port [0-3,8,9,10,11 | all] [table_id]
* pkcluster slotsslaveof ip port [0,2,4,6,7,8,9 | all] force
* pkcluster slotsslaveof ip port [0,2,4,6,7,8,9 | all] force [table_id]
*/
void PkClusterSlotsSlaveofCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePkClusterSlotsSlaveof);
return;
}
if (g_pika_conf->classic_mode()) {
res_.SetRes(CmdRes::kErrOther, "PkClusterSlotSync only support on sharding mode");
return;
}
if (!strcasecmp(argv_[2].data(), "no")
&& !strcasecmp(argv_[3].data(), "one")) {
is_noone_ = true;
} else {
ip_ = argv_[2];
if (!slash::string2l(argv_[3].data(), argv_[3].size(), &port_)
|| port_ <= 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
if ((ip_ == "127.0.0.1" || ip_ == g_pika_server->host())
&& port_ == g_pika_server->port()) {
res_.SetRes(CmdRes::kErrOther, "You fucked up");
return;
}
}
bool all_slots = false;
if (!strcasecmp(argv_[4].data(), "all")) {
// not know which table yet
all_slots = true;
} else {
Status s = ParseSlotGroup(argv_[4], &slots_);
if (!s.ok()) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
}
uint64_t table_id;
switch (argv_.size()) {
case 5:
table_name_ = g_pika_conf->default_table();
break;
case 6:
if (!strcasecmp(argv_[5].data(), "force")) {
force_sync_ = true;
table_name_ = g_pika_conf->default_table();
} else if (slash::string2ul(argv_[5].data(), argv_[5].size(), &table_id)) {
table_name_ = "db";
table_name_ += std::to_string(table_id);
} else {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
break;
case 7:
if ((!strcasecmp(argv_[5].data(), "force"))
&& (slash::string2ul(argv_[6].data(), argv_[6].size(), &table_id))) {
force_sync_ = true;
table_name_ = "db";
table_name_ += std::to_string(table_id);
} else {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
break;
default:
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
if (!g_pika_server->IsTableExist(table_name_)) {
res_.SetRes(CmdRes::kInvalidTable);
return;
}
if (all_slots) {
slots_ = g_pika_server->GetTablePartitionIds(table_name_);
}
if (slots_.empty()) {
res_.SetRes(CmdRes::kErrOther, "Slots set empty");
}
}
void PkClusterSlotsSlaveofCmd::Do(std::shared_ptr<Partition> partition) {
std::vector<uint32_t> to_del_slots;
for (const auto& slot : slots_) {
std::shared_ptr<SyncSlavePartition> slave_partition =
g_pika_rm->GetSyncSlavePartitionByName(
PartitionInfo(table_name_, slot));
if (!slave_partition) {
res_.SetRes(CmdRes::kErrOther, "Slot " + std::to_string(slot) + " not found!");
return;
}
if (is_noone_) {
// check okay
} else if (slave_partition->State() == ReplState::kConnected
&& slave_partition->MasterIp() == ip_ && slave_partition->MasterPort() == port_) {
to_del_slots.push_back(slot);
}
}
for (auto to_del : to_del_slots) {
slots_.erase(to_del);
}
Status s = Status::OK();
ReplState state = force_sync_
? ReplState::kTryDBSync : ReplState::kTryConnect;
for (const auto& slot : slots_) {
std::shared_ptr<SyncSlavePartition> slave_partition =
g_pika_rm->GetSyncSlavePartitionByName(
PartitionInfo(table_name_, slot));
if (slave_partition->State() == ReplState::kConnected) {
s = g_pika_rm->SendRemoveSlaveNodeRequest(table_name_, slot);
}
if (!s.ok()) {
break;
}
if (slave_partition->State() != ReplState::kNoConnect) {
// reset state
slave_partition->SetReplState(ReplState::kNoConnect);
}
if (is_noone_) {
} else {
s = g_pika_rm->ActivateSyncSlavePartition(
RmNode(ip_, port_, table_name_, slot), state);
if (!s.ok()) {
break;
}
}
}
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
/*
* pkcluster addtable table_id slot_num
* pkcluster addtable 1 1024
*/
void PkClusterAddTableCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
if (g_pika_conf->classic_mode()) {
res_.SetRes(CmdRes::kErrOther, "PkClusterTable Cmd only support on sharding mode");
return;
}
uint64_t table_id;
if (!slash::string2ul(argv_[2].data(), argv_[2].size(), &table_id)) {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
table_name_ = "db";
table_name_ += std::to_string(table_id);
if (!slash::string2ul(argv_[3].data(), argv_[3].size(), &slot_num_)
|| slot_num_ == 0) {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
}
void PkClusterAddTableCmd::Do(std::shared_ptr<Partition> partition) {
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (table_ptr) {
res_.SetRes(CmdRes::kErrOther, "Internal error: table already exist!");
return;
}
SlotState expected = INFREE;
if (!std::atomic_compare_exchange_strong(&g_pika_server->slot_state_,
&expected, INBUSY)) {
res_.SetRes(CmdRes::kErrOther,
"Table/Slot in syncing or a change operation is under way, retry later");
return;
}
bool pre_success = true;
Status s = AddTableSanityCheck();
if (!s.ok()) {
LOG(WARNING) << "AddTable sanity check failed: " << s.ToString();
pre_success = false;
}
if (pre_success) {
s = g_pika_conf->AddTable(table_name_,slot_num_);
if (!s.ok()) {
LOG(WARNING) << "Addslots add to pika conf failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = g_pika_server->AddTableStruct(table_name_,slot_num_);
if (!s.ok()) {
LOG(WARNING) << "Addslots add to pika conf failed: " << s.ToString();
pre_success = false;
}
}
g_pika_server->slot_state_.store(INFREE);
if (!pre_success) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
res_.SetRes(CmdRes::kOk);
LOG(INFO) << "Pika meta file overwrite success";
}
Status PkClusterAddTableCmd::AddTableSanityCheck() {
Status s = g_pika_conf->AddTableSanityCheck(table_name_);
if (!s.ok()) {
return s;
}
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (table_ptr) {
return Status::Corruption("table already exist!");
}
s = g_pika_rm->SyncTableSanityCheck(table_name_);
return s;
}
/*
* pkcluster deltable table_id
*/
void PkClusterDelTableCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
if (g_pika_conf->classic_mode()) {
res_.SetRes(CmdRes::kErrOther, "PkClusterTable Cmd only support on sharding mode");
return;
}
uint64_t table_id;
if (!slash::string2ul(argv_[2].data(), argv_[2].size(), &table_id)) {
res_.SetRes(CmdRes::kErrOther, "syntax error");
return;
}
table_name_ = "db";
table_name_ += std::to_string(table_id);
}
void PkClusterDelTableCmd::Do(std::shared_ptr<Partition> partition) {
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name_);
if (!table_ptr) {
res_.SetRes(CmdRes::kErrOther, "Internal error: table not found!");
return;
}
if (!table_ptr->TableIsEmpty()) {
table_ptr->GetAllPartitions(slots_);
for (const auto& slot_id : slots_) {
p_infos_.insert(PartitionInfo(table_name_, slot_id));
}
PkClusterDelSlotsCmd::Do();
}
SlotState expected = INFREE;
if (!std::atomic_compare_exchange_strong(&g_pika_server->slot_state_,
&expected, INBUSY)) {
res_.SetRes(CmdRes::kErrOther,
"Table/Slot in syncing or a change operation is under way, retry later");
return;
}
bool pre_success = true;
Status s = DelTableSanityCheck(table_name_);
if (!s.ok()) {
LOG(WARNING) << "DelTable sanity check failed: " << s.ToString();
pre_success = false;
}
// remove order maters
if (pre_success) {
s = g_pika_conf->DelTable(table_name_);
if (!s.ok()) {
LOG(WARNING) << "DelTable remove from pika conf failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = g_pika_rm->DelSyncTable(table_name_);
if (!s.ok()) {
LOG(WARNING) << "DelTable remove from pika rm failed: " << s.ToString();
pre_success = false;
}
}
if (pre_success) {
s = g_pika_server->DelTableStruct(table_name_);
if (!s.ok()) {
LOG(WARNING) << "DelTable remove from pika server failed: " << s.ToString();
pre_success = false;
}
}
g_pika_server->slot_state_.store(INFREE);
if (!pre_success) {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
res_.SetRes(CmdRes::kOk);
LOG(INFO) << "Pika meta file overwrite success";
}
Status PkClusterDelTableCmd::DelTableSanityCheck(const std::string &table_name) {
Status s = g_pika_conf->DelTableSanityCheck(table_name);
if (!s.ok()) {
return s;
}
std::shared_ptr<Table> table_ptr = g_pika_server->GetTable(table_name);
if (!table_ptr) {
return Status::Corruption("table not found!");
}
if (!table_ptr->TableIsEmpty()) {
return Status::Corruption("table have slots!");
}
s = g_pika_rm->SyncTableSanityCheck(table_name);
return s;
}