forked from gree/flare
/
cluster.cc
1928 lines (1680 loc) · 63.6 KB
/
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
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
/**
* cluster.cc
*
* implementation of gree::flare::cluster
*
* @author Masaki Fujimoto <fujimoto@php.net>
*
* $Id$
*/
#include "cluster.h"
#include "handler_monitor.h"
#include "handler_proxy.h"
#include "handler_reconstruction.h"
#include "key_resolver_modular.h"
#include "op_meta.h"
#include "op_node_add.h"
#include "op_node_role.h"
#include "op_node_state.h"
#include "op_shutdown.h"
#include "op_proxy_read.h"
#include "op_proxy_write.h"
#include "queue_node_sync.h"
#include "queue_proxy_read.h"
#include "queue_proxy_write.h"
#include "queue_update_monitor_option.h"
namespace gree {
namespace flare {
// {{{ global functions
// }}}
// {{{ ctor/dtor
/**
* ctor for cluster
*/
cluster::cluster(thread_pool* tp, string data_dir, string server_name, int server_port):
_thread_pool(tp),
_key_resolver(NULL),
_storage(NULL),
_data_dir(data_dir),
_master_reconstruction(0),
_server_name(server_name),
_server_port(server_port),
_monitor_threshold(0),
_monitor_interval(0),
_monitor_read_timeout(0),
_partition_size(default_partition_size),
_thread_type(default_thread_type),
#ifdef ENABLE_MYSQL_REPLICATION
_mysql_replication(false),
#endif
_index_server_name(""),
_index_server_port(0),
_proxy_concurrency(0),
_reconstruction_interval(0),
_reconstruction_bwlimit(0),
_proxy_prior_netmask(0),
_max_total_thread_queue(0) {
this->_node_key = this->to_node_key(server_name, server_port);
pthread_mutex_init(&this->_mutex_serialization, NULL);
pthread_mutex_init(&this->_mutex_master_reconstruction, NULL);
pthread_rwlock_init(&this->_mutex_node_map, NULL);
pthread_rwlock_init(&this->_mutex_node_partition_map, NULL);
}
/**
* dtor for cluster
*/
cluster::~cluster() {
if (this->_key_resolver != NULL) {
_delete_(this->_key_resolver);
}
}
// }}}
// {{{ operator overloads
// }}}
// {{{ public methods
/**
* parse node sync line
*/
int cluster::node::parse(const char* p) {
char q[BUFSIZ];
try {
int i = util::next_word(p, q, sizeof(q));
if (strcmp(q, "NODE") != 0) {
log_warning("unknown first token [%s]", q);
return -1;
}
// node_server_name
i += util::next_word(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no server name (required)", 0);
return -1;
}
this->node_server_name = q;
// node_server_port
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no server port (required)", 0);
return -1;
}
this->node_server_port = lexical_cast<int>(q);
// node_role
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no role (required)", 0);
return -1;
}
this->node_role = static_cast<cluster::role>(lexical_cast<int>(q));
// node_state
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no state (required)", 0);
return -1;
}
this->node_state = static_cast<cluster::state>(lexical_cast<int>(q));
// node_partition
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no partition (required)", 0);
return -1;
}
this->node_partition = lexical_cast<int>(q);
// node_balance
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no balance (required)", 0);
return -1;
}
this->node_balance = lexical_cast<int>(q);
// node_thread_type
i += util::next_digit(p+i, q, sizeof(q));
if (q[0] == '\0') {
log_warning("no thread_type (required)", 0);
return -1;
}
this->node_thread_type = lexical_cast<int>(q);
} catch (bad_lexical_cast e) {
log_warning("invalid digit [%s]", e.what());
return -1;
}
return 0;
}
/**
* startup proc for index process
*/
int cluster::startup_index(key_resolver::type key_resolver_type, int key_resolver_modular_hint, int key_resolver_modular_virtual) {
this->_type = type_index;
// load
if (this->_load() < 0) {
log_err("failed to load serialized vars (node map, etc)", 0);
return -1;
}
if (this->_reconstruct_node_partition() < 0) {
log_err("failed to reconstruct node partition map", 0);
return -1;
}
// key resolver
if (key_resolver_type == key_resolver::type_modular) {
this->_key_resolver = _new_ key_resolver_modular(this->_partition_size, key_resolver_modular_hint, key_resolver_modular_virtual);
} else {
log_err("unknown key resolver type [%s]", key_resolver::type_cast(key_resolver_type).c_str());
return -1;
}
if (this->_key_resolver->startup() < 0) {
return -1;
}
// monitoring threads
for (node_map::iterator it = this->_node_map.begin(); it != this->_node_map.end(); it++) {
shared_thread t = this->_thread_pool->get(it->second.node_thread_type);
handler_monitor* h = _new_ handler_monitor(t, this, it->second.node_server_name, it->second.node_server_port);
h->set_monitor_threshold(this->_monitor_threshold);
h->set_monitor_interval(this->_monitor_interval);
h->set_monitor_read_timeout(this->_monitor_read_timeout);
t->trigger(h);
}
return 0;
}
/**
* startup proc for node process
*/
int cluster::startup_node(string index_server_name, int index_server_port, uint32_t proxy_prior_netmask) {
this->_type = type_node;
this->_index_server_name = index_server_name;
this->_index_server_port = index_server_port;
if (proxy_prior_netmask != 0) {
this->_proxy_prior_netmask = htonl((uint32_t)0xffffffff << (32-proxy_prior_netmask)); // length -> mask bits
struct in_addr in;
in.s_addr = util::inet_addr(this->_server_name.c_str(), this->_proxy_prior_netmask);
log_notice("my network address (server_name=%s, proxy_prior_netmask=0x%x, prior_network_address=%s)", this->_server_name.c_str(), ntohl(this->_proxy_prior_netmask), inet_ntoa(in));
}
log_notice("setting up cluster node... (type=%d, index_server_name=%s, index_server_port=%d)", this->_type, this->_index_server_name.c_str(), this->_index_server_port);
shared_connection c(new connection());
if (c->open(this->_index_server_name, this->_index_server_port) < 0) {
log_err("failed to connect to index server", 0);
return -1;
}
op_node_add* p_na = _new_ op_node_add(c, this);
vector<node> v;
if (p_na->run_client(v) < 0) {
log_err("failed to add node to index server", 0);
_delete_(p_na);
return -1;
}
_delete_(p_na);
// get meta data from index server
key_resolver::type key_resolver_type;
int partition_size = cluster::default_partition_size;
int key_resolver_modular_hint = 0;
int key_resolver_modular_virtual = cluster::default_key_resolver_modular_virtual;
op_meta* p_m = _new_ op_meta(c, this);
if (p_m->run_client(partition_size, key_resolver_type, key_resolver_modular_hint, key_resolver_modular_virtual) < 0) {
log_err("failed to get meta data from index server", 0);
_delete_(p_m);
return -1;
}
_delete_(p_m);
log_notice("meta data from index server:", 0);
log_notice(" partition_size: %d", partition_size);
log_notice(" key_resolver_type: %s", key_resolver::type_cast(key_resolver_type).c_str());
log_notice(" key_resolver_modular_hint: %d", key_resolver_modular_hint);
log_notice(" key_resolver_modular_virtual: %d", key_resolver_modular_virtual);
this->_partition_size = partition_size;
// startup key resolver
if (key_resolver_type == key_resolver::type_modular) {
this->_key_resolver = _new_ key_resolver_modular(partition_size, key_resolver_modular_hint, key_resolver_modular_virtual);
} else {
log_err("unknown key resolver type [%s]", key_resolver::type_cast(key_resolver_type).c_str());
return -1;
}
if (this->_key_resolver->startup() < 0) {
return -1;
}
// set state and other nodes
if (this->reconstruct_node(v) < 0) {
log_err("failed to reconstruct node map", 0);
return -1;
}
return 0;
}
/**
* get node info vector
*/
vector<cluster::node> cluster::get_node() {
vector<node> v;
pthread_rwlock_rdlock(&this->_mutex_node_map);
for (node_map::iterator it = this->_node_map.begin(); it != this->_node_map.end(); it++) {
v.push_back(it->second);
}
pthread_rwlock_unlock(&this->_mutex_node_map);
return v;
}
/**
* get slave node info vector
*/
vector<cluster::node> cluster::get_slave_node() {
vector<node> v;
node n = this->get_node(this->_node_key);
if (n.node_role != role_master) {
return v;
}
pthread_rwlock_rdlock(&this->_mutex_node_partition_map);
vector<partition_node> slave;
if (this->_node_partition_map.count(n.node_partition) > 0) {
slave = this->_node_partition_map[n.node_partition].slave;
} else if (this->_node_partition_prepare_map.count(n.node_partition) > 0) {
slave = this->_node_partition_prepare_map[n.node_partition].slave;
} else {
// something is going wrong
log_warning("not found node_partition [%d]", n.node_partition);
}
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
for (vector<partition_node>::iterator it = slave.begin(); it != slave.end(); it++) {
node tmp = this->get_node(it->node_key);
v.push_back(tmp);
}
return v;
}
/**
* [index] add new node
*/
int cluster::add_node(string node_server_name, int node_server_port) {
if (this->_type != type_index) {
log_err("node add request for non-index server", 0);
return -1;
}
string node_key = this->to_node_key(node_server_name, node_server_port);
log_debug("adding new node (server_name=%s, server_port=%d, node_key=%s)", node_server_name.c_str(), node_server_port, node_key.c_str());
// add node to map
pthread_rwlock_wrlock(&this->_mutex_node_map);
int thread_type;
bool replace = false;
try {
if (this->_node_map.count(node_key) > 0) {
if (this->_node_map[node_key].node_state == state_down) {
log_notice("node_key [%s] is already in node map (state is down -> continue processing)", node_key.c_str());
thread_type = this->_node_map[node_key].node_thread_type;
replace = true;
} else {
log_warning("node_key [%s] is already in node map", node_key.c_str());
throw -1;
}
} else {
thread_type = this->_thread_type;
this->_thread_type++;
}
if (replace == false) {
// initial node setup
node n;
n.node_server_name = node_server_name;
n.node_server_port = node_server_port;
n.node_role = role_proxy;
n.node_state = state_active;
n.node_partition = -1;
n.node_balance = 0;
n.node_thread_type = thread_type;
this->_node_map[node_key] = n;
log_debug("node is [%s] added to node map (state=%d, thread_type=%d)", node_key.c_str(), n.node_state, n.node_thread_type, replace);
} else {
log_debug("node is already in node map (perhaps node is restarting) (state=%d, thread_type=%d", this->_node_map[node_key].node_state, this->_node_map[node_key].node_thread_type);
}
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_map);
// create monitoring thread
if (replace == false) {
shared_thread t = this->_thread_pool->get(thread_type);
handler_monitor* h = _new_ handler_monitor(t, this, node_server_name, node_server_port);
h->set_monitor_threshold(this->_monitor_threshold);
h->set_monitor_interval(this->_monitor_interval);
h->set_monitor_read_timeout(this->_monitor_read_timeout);
t->trigger(h);
}
// notify other nodes
shared_queue_node_sync q(new queue_node_sync(this));
vector<string> dummy;
this->_broadcast(q, false, dummy);
return 0;
}
/**
* [index] node down event handler
*/
int cluster::down_node(string node_server_name, int node_server_port, bool shutdown_mode) {
string node_key = this->to_node_key(node_server_name, node_server_port);
string exclude_node_key;
if (shutdown_mode) {
exclude_node_key = node_key; // skip broadcasting to a terminated node.
} else {
log_notice("handling node down event [node_key=%s]", node_key.c_str());
exclude_node_key = "";
}
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
vector<string> prior_node_key;
try {
if (this->_node_map.count(node_key) == 0) {
log_warning("no such node (node_key=%s)", node_key.c_str());
throw -1;
}
if (this->_node_map[node_key].node_state == state_down) {
log_notice("node is already down (node_key=%s)", node_key.c_str());
throw 0;
}
node& n = this->_node_map[node_key];
bool preserve = false;
if (n.node_role == role_master) {
if (n.node_state == state_active) {
log_err("master node down -> finding an active slave and shift its role to master", 0);
if (shutdown_mode) {
log_err("master node[%s] maybe send shutdown op", node_key.c_str());
exclude_node_key = ""; // try to send down message, and shift role master -> proxy.
}
if (this->_node_partition_map[n.node_partition].slave.size() == 0) {
log_err("no slave for this partition (partition=%d) -> all requests for this partition will fail!", n.node_partition);
preserve = true; // leave role as master to keep partition count (*important*)
} else {
string failover_node_key = "";
int balance = 0;
int prev_balance = 0;
for (vector<partition_node>::iterator it = this->_node_partition_map[n.node_partition].slave.begin(); it != this->_node_partition_map[n.node_partition].slave.end(); it++) {
if (this->_node_map[it->node_key].node_state == state_active) {
if (failover_node_key.empty() || it->node_balance > prev_balance) { // first slave(balance == 0) or balance is largest.
failover_node_key = it->node_key;
prev_balance = it->node_balance;
}
}
balance += it->node_balance;
}
if (failover_node_key.empty()) {
log_err("no active slave:( -> all requests for this partition will fail!", 0);
preserve = true; // leave role as master to keep partition count (*important*)
} else {
// shift target slave role to master
this->_node_map[failover_node_key].node_role = role_master;
if (balance == 0) {
this->_node_map[failover_node_key].node_balance++;
}
log_notice("found new master node (node_key=%s, partition=%d, balance=%d)", failover_node_key.c_str(), n.node_partition, this->_node_map[failover_node_key].node_balance);
prior_node_key.push_back(failover_node_key);
}
}
} else if (n.node_state == state_prepare || n.node_state == state_ready) {
log_err("master node (prepare|ready) down -> clearing all preparing nodes in this partition (partition=%d)", n.node_partition);
for (vector<partition_node>::iterator it = this->_node_partition_prepare_map[n.node_partition].slave.begin(); it != this->_node_partition_prepare_map[n.node_partition].slave.end(); it++) {
log_debug(" -> node_key: %s", it->node_key.c_str());
this->_node_map[it->node_key].node_role = role_proxy;
this->_node_map[it->node_key].node_state = state_active;
this->_node_map[it->node_key].node_balance = 0;
this->_node_map[it->node_key].node_partition = -1;
}
}
}
log_notice("setting node state to down (and clearing role, etc)", 0);
n.node_state = state_down;
if (preserve == false) {
n.node_role = role_proxy;
n.node_partition = -1;
n.node_balance = 0;
}
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_reconstruct_node_partition(false);
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
// notify
shared_queue_node_sync q(new queue_node_sync(this));
this->_broadcast(q, false, prior_node_key, exclude_node_key);
return 0;
}
/**
* [index] node shutdown event handler
*/
int cluster::shutdown_node(string node_server_name, int node_server_port) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("node shutdown event [node_key=%s]", node_key.c_str());
return this->down_node(node_server_name, node_server_port, true);
}
/**
* [index] node ready event handler
*/
int cluster::ready_node(string node_server_name, int node_server_port) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("handling node ready event [node_key=%s]", node_key.c_str());
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
try {
if (this->_node_map.count(node_key) == 0) {
log_warning("no such node (node_key=%s)", node_key.c_str());
throw -1;
}
node& n = this->_node_map[node_key];
if (n.node_state != state_prepare) {
log_notice("node state is not prepare (node_key=%s, node_state=%s)", node_key.c_str(), cluster::state_cast(n.node_state).c_str());
throw -1;
}
this->_node_map[node_key].node_state = state_ready;
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_reconstruct_node_partition(false);
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
// notify
shared_queue_node_sync q(new queue_node_sync(this));
vector<string> dummy;
this->_broadcast(q, false, dummy);
return 0;
}
/**
* [index] node up event handler
*/
int cluster::up_node(string node_server_name, int node_server_port) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("handling node up event [node_key=%s]", node_key.c_str());
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
vector<string> prior_node_key;
try {
if (this->_node_map.count(node_key) == 0) {
log_warning("no such node (node_key=%s)", node_key.c_str());
throw -1;
}
node& n = this->_node_map[node_key];
if (n.node_state == state_active) {
log_notice("node is already active (node_key=%s, node_state=%s)", node_key.c_str(), cluster::state_cast(n.node_state).c_str());
throw 0;
} else if (n.node_role != role_slave && n.node_state == state_prepare) {
log_notice("node is not ready to be active (current state=prepare), skip this operation (node_key=%s, node_state=%s)", node_key.c_str(), cluster::state_cast(n.node_state).c_str());
throw -1;
}
if (n.node_state == state_ready || (n.node_role == role_slave && n.node_state == state_prepare)) {
// just shift state to active
if (n.node_state == state_ready && n.node_role == role_master) {
// when new master shifts from ready to active, we need to activate new master prior to other nodes (to avoid dead lock between nodes)
prior_node_key.push_back(node_key);
for (vector<partition_node>::iterator it = this->_node_partition_prepare_map[n.node_partition].slave.begin(); it != this->_node_partition_prepare_map[n.node_partition].slave.end(); it++) {
prior_node_key.push_back(it->node_key);
}
}
} else {
if (n.node_role == role_master) {
for (node_map::iterator it = this->_node_map.begin(); it != this->_node_map.end(); it++) {
if (it->first == node_key) {
continue;
}
if (it->second.node_partition == n.node_partition && it->second.node_state != state_down) {
log_warning("preserved master node is up, but already have another master -> role is set to proxy (node_key=%s, node_partition=%d, node_state=%s)", it->first.c_str(), it->second.node_partition, cluster::state_cast(it->second.node_state).c_str());
n.node_role = role_proxy;
n.node_partition = -1;
n.node_balance = 0;
}
}
} else {
log_notice("node role is set to proxy for safe...", 0);
n.node_role = role_proxy;
n.node_partition = -1;
}
}
this->_node_map[node_key].node_state = state_active;
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_reconstruct_node_partition(false);
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
// notify
shared_queue_node_sync q(new queue_node_sync(this));
this->_broadcast(q, false, prior_node_key);
return 0;
}
/**
* [index] remove node from map
*
* removing node is only available when node is down and not preserved
*/
int cluster::remove_node(string node_server_name, int node_server_port) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("removing node [node_key=%s]", node_key.c_str());
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
try {
if (this->_node_map.count(node_key) == 0) {
log_warning("no such node (node_key=%s)", node_key.c_str());
throw -1;
}
node& n = this->_node_map[node_key];
// see if node is really down and removable
if (n.node_state != state_down) {
log_warning("cannot remove active or preparing node (node_key=%s, node_stae=%s, node_role=%s)", node_key.c_str(), cluster::state_cast(n.node_state).c_str(), cluster::role_cast(n.node_role).c_str());
throw -1;
}
bool need_preserving = true;
if (n.node_role == role_master) {
for (node_map::iterator it = this->_node_map.begin(); it != this->_node_map.end(); it++) {
if (it->first == node_key) {
continue;
}
if (it->second.node_partition == n.node_partition && it->second.node_state != state_down) {
need_preserving = false;
break;
}
}
if (need_preserving) {
log_warning("cannot remove preserved node (node_key=%s, node_stae=%s, node_role=%s)", node_key.c_str(), cluster::state_cast(n.node_state).c_str(), cluster::role_cast(n.node_role).c_str());
throw -1;
}
}
// remove a node
thread_pool::local_map m = this->_thread_pool->get_active(n.node_thread_type);
for (thread_pool::local_map::iterator it = m.begin(); it != m.end(); it++) {
log_notice("killing monitoring thread(s)... (node_thread_type=%d, thread_id=%d)", n.node_thread_type, it->second->get_id());
it->second->set_state("killed");
it->second->shutdown(true, false);
}
this->_node_map.erase(node_key);
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_reconstruct_node_partition(false);
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
// notify
shared_queue_node_sync q(new queue_node_sync(this));
vector<string> dummy;
this->_broadcast(q, false, dummy);
return 0;
}
/**
* [index] set node role
*
* node_partition is (currently) only available in case of [role=proxy, state=active] -> [role=master|slave]
*
* @todo too long, too complicated -> refactoring
*/
int cluster::set_node_role(string node_server_name, int node_server_port, role node_role, int node_balance, int node_partition) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("set node role (node_key=%s, node_role=%d, node_balance=%d, node_partition=%d)", node_key.c_str(), node_role, node_balance, node_partition);
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
try {
if (this->_node_map.count(node_key) == 0) {
log_warning("no such node (node_key=%s)", node_key.c_str());
throw -1;
}
node& n = this->_node_map[node_key];
// state=down -> nothing to do
if (n.node_state == state_down) {
log_notice("failed to set node role [role=*, state=down] -> [role=*] not allowed", 0);
throw -1;
}
// state=prepare|ready
if (n.node_state == state_prepare || n.node_state == state_ready) {
// current role=master
if (n.node_role == role_master) {
if (node_role == role_master) {
if (this->_check_node_balance(node_key, node_balance) < 0) {
throw -1;
}
n.node_balance = node_balance;
log_notice("setting node balance [node_key=%s, node_balance=%d -> %d]", node_key.c_str(), n.node_balance, node_balance);
} else if (node_role == role_slave) {
log_notice("failed to set node role [role=master, state=prepare|ready] -> [role=slave] not allowed", 0);
throw -1;
} else if (node_role == role_proxy) {
log_notice("clearing all slave nodes (partition=%d)", n.node_partition);
for (vector<partition_node>::iterator it = this->_node_partition_prepare_map[n.node_partition].slave.begin(); it != this->_node_partition_prepare_map[n.node_partition].slave.end(); it++) {
log_notice(" -> node_key: %s", it->node_key.c_str());
this->_node_map[it->node_key].node_role = role_proxy;
this->_node_map[it->node_key].node_state = state_active;
this->_node_map[it->node_key].node_balance = 0;
this->_node_map[it->node_key].node_partition = -1;
}
n.node_role = node_role;
n.node_state = state_active;
n.node_balance = 0;
n.node_partition = -1;
log_notice("setting node role [role=master, state=prepare] -> [role=proxy, state=active]", 0);
}
// role=slave
} else if (n.node_role == role_slave) {
if (node_role == role_master) {
log_notice("failed to set node role [role=slave, state=prepare] -> [role=master] not allowed", 0);
throw -1;
} else if (node_role == role_slave) {
bool preparing = false;
if (this->_check_node_partition(n.node_partition, preparing) < 0) {
throw -1;
}
if (preparing == false) {
log_notice("updating node_balance is allowed only when master is also preparing (otherwise, it should always be 0)", 0);
throw -1;
}
if (this->_check_node_balance(node_key, node_balance) < 0) {
throw -1;
}
n.node_balance = node_balance;
log_notice("setting node balance [node_key=%s, node_balance=%d -> %d]", node_key.c_str(), n.node_balance, node_balance);
} else if (node_role == role_proxy) {
// we do not have to check node balance here (<- state=prepare|ready)
n.node_role = node_role;
n.node_state = state_active;
n.node_balance = 0;
n.node_partition = -1;
log_notice("setting node role [role=slave, state=prepare] -> [role=proxy, state=active]", 0);
}
} else if (n.node_role == role_proxy) {
n.node_state = state_active;
log_notice("setting node role [role=proxy, state=prepare|ready] -> something inconsistent -> shift state to active", 0);
}
}
// state=active
if (n.node_state == state_active) {
// role=master
if (n.node_role == role_master) {
if (node_role == role_master) {
if (this->_check_node_balance(node_key, node_balance) < 0) {
throw -1;
}
n.node_balance = node_balance;
log_notice("setting node balance [node_key=%s, node_balance=%d -> %d]", node_key.c_str(), n.node_balance, node_balance);
} else {
log_notice("failed to set node role [role=master, state=active] -> [role=slave|proxy] not allowed", 0);
throw -1;
}
// role=slave
} else if (n.node_role == role_slave) {
if (node_role == role_master) {
log_notice("failed to set node role [role=slave, state=active] -> [role=master] not allowed", 0);
throw -1;
} else if (node_role == role_slave) {
if (this->_check_node_balance(node_key, node_balance) < 0) {
throw -1;
}
n.node_balance = node_balance;
log_notice("setting node balance [node_key=%s, node_balance=%d -> %d]", node_key.c_str(), n.node_balance, node_balance);
} else if (node_role == role_proxy) {
if (this->_check_node_balance(node_key, 0) < 0) {
throw -1;
}
n.node_role = node_role;
n.node_state = state_active;
n.node_balance = 0;
n.node_partition = -1;
log_notice("setting node role [role=slave, state=active] -> [role=proxy, state=active]", 0);
}
// role=proxy
} else if (n.node_role == role_proxy) {
if (node_role == role_master) {
// only new (next) partition is allowed
bool preparing = false;
if (this->_check_node_partition_for_new(node_partition, preparing) < 0) {
throw -1;
}
if (node_balance <= 0) {
log_notice("node_balance should be positive when adding new master node (node_balance=%d)", node_balance);
throw -1;
}
n.node_role = node_role;
// first partition (== 0) can start as active
n.node_state = preparing ? (node_partition == 0 ? state_active : state_prepare) : state_active;
n.node_balance = node_balance;
n.node_partition = node_partition;
log_notice("setting node role [role=proxy, state=active] -> [role=master, state=%s]", cluster::state_cast(n.node_state).c_str());
} else if (node_role == role_slave) {
bool preparing = false;
if (this->_check_node_partition(node_partition, preparing) < 0) {
throw -1;
}
if (preparing && node_balance < 0) {
log_info("node_balance is set to 0 (given node_balance=%d)", node_balance);
node_balance = 0;
}
n.node_role = node_role;
n.node_state = state_prepare;
n.node_balance = preparing ? node_balance : 0;
n.node_partition = node_partition;
log_notice("setting node role [role=proxy, state=active] -> [role=slave, state=prepare]", 0);
} else if (node_role == role_proxy) {
// nothing to do
}
}
}
} catch (int e) {
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return e;
}
this->_reconstruct_node_partition(false);
this->_save();
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
// notify
shared_queue_node_sync q(new queue_node_sync(this));
vector<string> dummy;
this->_broadcast(q, false, dummy);
return 0;
}
/**
* [index] set node state
*/
int cluster::set_node_state(string node_server_name, int node_server_port, state node_state) {
string node_key = this->to_node_key(node_server_name, node_server_port);
log_notice("set node state (node_key=%s, node_state=%d)", node_key.c_str(), node_state);
if (node_state == state_prepare) {
log_warning("currently (force) state shift to prepare is not yet supported", 0);
return -1;
}
if (node_state == state_down) {
return this->down_node(node_server_name, node_server_port);
} else if (node_state == state_active) {
return this->up_node(node_server_name, node_server_port);
} else if (node_state == state_ready) {
return this->ready_node(node_server_name, node_server_port);
}
return 0;
}
/**
* reconstruct all node map
*/
int cluster::reconstruct_node(vector<node> v) {
stack<node_shift_state> shift_state_stack;
stack<node_shift_role> shift_role_stack;
pthread_rwlock_wrlock(&this->_mutex_node_map);
pthread_rwlock_wrlock(&this->_mutex_node_partition_map);
log_notice("reconstructing node map... (%d entries)", v.size());
node_map nm;
for (vector<node>::iterator it = v.begin(); it != v.end(); it++) {
string node_key = this->to_node_key(it->node_server_name, it->node_server_port);
log_debug("node_key: %s%s", node_key.c_str(), node_key == this->_node_key ? " (myself)" : "");
if (this->_type == type_node) {
if (this->_node_map.count(node_key) == 0) {
// in this case, logically we do not have to handle anything
// - myself -> should be role=proxy
// - others -> this node does not have to care about anything
log_debug("-> new node", 0);
} else {
log_debug("-> existing node", 0);
if (it->node_state != this->_node_map[node_key].node_state) {
node_shift_state tmp = { node_key, this->_node_map[node_key].node_state, it->node_state};
shift_state_stack.push(tmp);
}
if (it->node_role != this->_node_map[node_key].node_role || it->node_partition != this->_node_map[node_key].node_partition) {
node_shift_role tmp = {node_key, this->_node_map[node_key].node_role, this->_node_map[node_key].node_partition, it->node_role, it->node_partition};
shift_role_stack.push(tmp);
}
if (it->node_balance != this->_node_map[node_key].node_balance) {
log_notice("update: node_balance (%d -> %d)", this->_node_map[node_key].node_balance, it->node_balance);
}
if (it->node_thread_type != this->_node_map[node_key].node_thread_type) {
// this should not happen
log_err("update: node_thread_type (%d -> %d)", this->_node_map[node_key].node_thread_type, it->node_thread_type);
}
this->_node_map.erase(node_key);
}
}
nm[node_key] = *it;
}
// check out removed nodes
for (node_map::iterator it = this->_node_map.begin(); it != this->_node_map.end(); it++) {
log_notice("detecting removed node [node_key=%s]", it->first.c_str());
// do something?
}
this->_node_map = nm;
this->_reconstruct_node_partition(false);
while (shift_state_stack.size() > 0) {
node_shift_state tmp = shift_state_stack.top();
this->_shift_node_state(tmp.node_key, tmp.old_state, tmp.new_state);
shift_state_stack.pop();
}
while (shift_role_stack.size() > 0) {
node_shift_role tmp = shift_role_stack.top();
this->_shift_node_role(tmp.node_key, tmp.old_role, tmp.old_partition, tmp.new_role, tmp.new_partition);
shift_role_stack.pop();
}
pthread_rwlock_unlock(&this->_mutex_node_partition_map);
pthread_rwlock_unlock(&this->_mutex_node_map);
return 0;
}
/**
* [index] set node server monitoring threshold
*/
int cluster::set_monitor_threshold(int monitor_threshold) {
this->_monitor_threshold = monitor_threshold;
// notify current threads
shared_queue_update_monitor_option q(new queue_update_monitor_option(this->_monitor_threshold, this->_monitor_interval, this->_monitor_read_timeout));
vector<string> dummy;
this->_broadcast(q, true, dummy);
return 0;
}
/**
* [index] set node server monitoring interval
*/
int cluster::set_monitor_interval(int monitor_interval) {
this->_monitor_interval = monitor_interval;
// notify current threads
shared_queue_update_monitor_option q(new queue_update_monitor_option(this->_monitor_threshold, this->_monitor_interval, this->_monitor_read_timeout));
vector<string> dummy;
this->_broadcast(q, true, dummy);
return 0;
}