-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordination_server.cpp
1041 lines (941 loc) · 31.2 KB
/
coordination_server.cpp
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
#include "coordination_server.hpp"
void sigchld_handler(int s)
{
// waitpid() might overwrite errno, so we save and restore it:
int saved_errno = errno;
while(waitpid(-1, NULL, WNOHANG) > 0);
errno = saved_errno;
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int CCoord_server::alwaysListen()
{
ifstream fin("coord_info.txt");
if(!fin)
{
cout<<"Error openeing coord_info.txt";
return -1;
}
// read coord. server's ip and port num from file
while(!fin.eof())
{
string ip, port;
fin>>ip>>port;
if(fin.eof())
break;
coord_info.IPaddr=ip;
coord_info.portnum=port;
}
int sockfd; // listen on sock_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; // stream protocol, TCP
hints.ai_flags = AI_PASSIVE; // use machine's IP
// get all the addrinfo structures, each of which contains an Internet address that can be specified in a call to bind() or connect()
if ((rv = getaddrinfo(NULL, coord_info.portnum.c_str(), &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
{
perror("setsockopt");
return -1;
//exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("server: bind");
continue;
}
//cout<<((struct sockaddr_in*)p->ai_addr)->sin_port<<endl;
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (p == NULL)
{
fprintf(stderr, "server: failed to bind\n");
return -1;
//exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
return -1;
//exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
return -1;
//exit(1);
}
printf("server: waiting for connections...\n");
while(1)
{ // main accept() loop
sin_size = sizeof their_addr;
int *new_fd = new int; // new connection on new_fd
*new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (*new_fd == -1)
{
perror("accept");
//continue;
}
inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s, sizeof s);
printf("server: got connection from %s\n", s);
//string connectionIP (s);
string senderPort = to_string(ntohs(((struct sockaddr_in *)&their_addr)->sin_port));
char buf[BUFFERSIZE];
int numbytes;
// First receive
memset(buf,0,BUFFERSIZE);
if ((numbytes = recv(*new_fd, buf, BUFFERSIZE, 0)) == -1)
{
perror("recv");
return -1;
//exit(1);
}
// JSON parsing
cout<<buf<<endl;
Document document;
document.Parse(buf);
string type=document["type"].GetString();
int ty;
if(type=="client")
{
//cout<<"client: "<<*new_fd<<endl;
string sendstr="{\n\t\"status\":\"connected\"\n}";
if(send(*new_fd, sendstr.c_str(), sendstr.length(), 0) == -1)
perror("send");
thread typethread(&CCoord_server::clientHandle, this, *new_fd);
typethread.detach();
}
else if(type=="slave")
{
string slaveIP(s);
string slavePort = document["port"].GetString();;
cout<<slaveIP<<" "<<slavePort<<endl;
thread typethread(&CCoord_server::slaveHandle, this, slaveIP, slavePort, *new_fd);
typethread.detach();
}
}
}
int CCoord_server::clientHandle(int fd)
{
int numbytes;
// First receive
Document document;
while(1)
{
char buf[BUFFERSIZE];
memset(buf,0,BUFFERSIZE);
if ((numbytes = recv(fd, buf, BUFFERSIZE, 0)) == -1)
{
perror("recv");
return -1;
//exit(1);
}
string tmp(buf);
cout<<tmp<<endl<<endl<<endl;
document.Parse(tmp.c_str());
assert(document.HasMember("purpose"));
assert(document["purpose"].IsString());
string purpose=document["purpose"].GetString();
if(purpose=="create_user")
{
assert(document.HasMember("username"));
assert(document["username"].IsString());
string username = document["username"].GetString();
assert(document.HasMember("password"));
assert(document["password"].IsString());
string password = document["password"].GetString();
create_user(username, password, fd);
}
else if(purpose=="login")
{
assert(document.HasMember("username"));
assert(document["username"].IsString());
string username = document["username"].GetString();
assert(document.HasMember("password"));
assert(document["password"].IsString());
string password = document["password"].GetString();
login(username, password, fd);
}
else if(purpose=="get")
{
string bufstr(buf);
getData(bufstr, fd); //passing file desc and command string
}
else if(purpose=="put")
{
string bufstr(buf);
putData(bufstr, fd);
}
else if(purpose=="delete")
{
string bufstr(buf);
deleteData(bufstr, fd);
}
else if(purpose=="update")
{
string bufstr(buf);
updateData(bufstr, fd);
}
}
}
int CCoord_server::slaveHandle(string slaveIP, string slavePort, int fd)
{
// Receive port num
char buf[BUFFERSIZE];
memset(buf,0,BUFFERSIZE);
int numbytes;
//if ((numbytes = recv(fd, buf, BUFFERSIZE, 0)) == -1)
//{
// perror("recv");
// return -1;
// //exit(1);
//}
//string slaveport(buf,numbytes);
//memset(buf,0,BUFFERSIZE);
slaveData* newSlave = new slaveData;
newSlave->IPaddr = slaveIP;
newSlave->portnum = slavePort;
newSlave->hashvalue = hashSlave(newSlave);
newSlave->isActive = true;
cout<<slaveIP<<": "<<newSlave->hashvalue<<endl;
insertBST(&treeRoot,newSlave);
inorder(treeRoot);
cout<<endl;
std::pair<std::map<string,slaveData>::iterator,bool> ret;
ret = slavemap.insert(make_pair(newSlave->IPaddr, *newSlave));
if (ret.second==false)
{
// If element already exists
//----Initiate migration-----//
//migrationInitUp(newSlave);
string sendstr="{\n\t\"status\":\"connected\"\n}";
if(send(fd, sendstr.c_str(), sendstr.length(), 0) == -1)
perror("send");
return 0;
}
else
{
string sendstr="{\n\t\"status\":\"connected\"\n}";
if(send(fd, sendstr.c_str(), sendstr.length(), 0) == -1)
perror("send");
}
fcntl(fd, F_SETFL, O_NONBLOCK);
clock_t startTime = clock(); //Start timer
double secondsPassed;
double secondsToDelay = SECTODELAY;
while(1)
{
memset(buf,0,BUFFERSIZE);
//sleep(2);
if ((numbytes = recv(fd, buf, BUFFERSIZE, 0)) == -1)
{
perror("recv");
//return -1;
//exit(1);
}
if(numbytes>0)
{
//puts(buf);
startTime=clock();
}
else
cout<<"No receive from: "<<newSlave->IPaddr<<endl;
secondsPassed = ((double)(clock() - startTime) / CLOCKS_PER_SEC)*1000;
//cout<<clock()<<" "<<startTime<<endl;
if(secondsPassed >= secondsToDelay)
{
cout<<"Slave down. Socket addr: "<<newSlave->IPaddr<<":"<<newSlave->portnum<<endl<<"Initiating migration..."<<endl;
migrationInitDown(newSlave);
close(fd);
break;
}
sleep(2);
//listenHeartbeat()
}
}
int CCoord_server::create_user(string username, string password, int sock_fd)
{
clientData *newClient = new clientData;
newClient->username = username;
newClient->password = password;
//newClient->IPaddr = IPaddr;
//newClient->portnum = portnum;
newClient->isActive = false;
std::pair<std::map<string,clientData>::iterator,bool> ret;
ret = clientmap.insert(make_pair(username, *newClient));
if (ret.second==false)
{
// If element already exists
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("value","exists"));
string tosend = create_json_string(sendjson);
if(send(sock_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
return 0;
}
else
{
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("value","success"));
string tosend = create_json_string(sendjson);
if(send(sock_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
return 1;
}
}
int CCoord_server::login(string username, string password, int sock_fd)
{
map<string, clientData>:: iterator itr;
if((itr = clientmap.find(username)) != clientmap.end())
{
if(itr->second.password==password)
{
//itr->second.IPaddr=IPaddr;
//itr->second.password=password;
itr->second.isActive=true;
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("status","success"));
string tosend = create_json_string(sendjson);
if(send(sock_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
return true; // User present, might not be active;
}
else
{
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("status","invalid"));
string tosend = create_json_string(sendjson);
if(send(sock_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
return false;
}
}
else
{
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("status","failure"));
string tosend = create_json_string(sendjson);
if(send(sock_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
return false; // user not registered with tracker
}
}
int CCoord_server::getData(string bufstr, int client_fd)
{
int slave_fd, numbytes;
char buf[BUFFERSIZE];
Document document;
document.Parse(bufstr.c_str());
assert(document.HasMember("key"));
assert(document["key"].IsString());
string key = document["key"].GetString();
char* str = new char[key.length()+1];
strcpy(str, key.c_str());
Fnv32_t hash_val = fnv_32_str(str, FNV1_32_INIT);
bstNode* targetNode = bst_upperBound(treeRoot, hash_val);
if(targetNode == NULL) // If hash value of key is greater than the last slave server, search slave with smallest hash value
{
targetNode = findMinimum(treeRoot);
}
#if CACHE_ENABLE
string cachestr = targetNode->data.cache.getValue(key);
if(cachestr != "none")
{
string response = "{\n\t\"value\":\""+cachestr+"\"\n}";
if(send(client_fd, response.c_str(), response.length(), 0) == -1)
perror("send");
}
else
#endif
{
if(targetNode->data.isActive)
{
slave_fd = connect_to_slave(&targetNode->data);
if(send(slave_fd, bufstr.c_str(), bufstr.length(), 0) == -1)
perror("send");
memset(buf,0,BUFFERSIZE);
if ((numbytes = recv(slave_fd, buf, BUFFERSIZE, 0)) == -1)
{
perror("recv");
return -1;
//exit(1);
}
}
else
{
cout<<"Target slave down"<<endl;
}
if(send(client_fd, buf, numbytes, 0) == -1)
perror("send");
}
}
int CCoord_server::putData(string bufstr, int client_fd)
{
bstNode* primary = put_update_delete_handle(bufstr, client_fd);
#if CACHE_ENABLE
if(primary != NULL)
{
Document document_p;
document_p.Parse(bufstr.c_str());
assert(document_p.HasMember("key"));
assert(document_p["key"].IsString());
string key = document_p["key"].GetString();
assert(document_p.HasMember("value"));
assert(document_p["value"].IsString());
string value = document_p["value"].GetString();
primary->data.cache.putInSet(key, value);
}
else
{
cout<<"Primary node NULL for putting data"<<endl;
}
#endif
}
int CCoord_server::deleteData(string bufstr, int client_fd)
{
bstNode* primary = put_update_delete_handle(bufstr, client_fd);
#if CACHE_ENABLE
if(primary != NULL)
{
Document document_p;
document_p.Parse(bufstr.c_str());
assert(document_p.HasMember("key"));
assert(document_p["key"].IsString());
string key = document_p["key"].GetString();
primary->data.cache.deleteKey(key);
}
else
{
cout<<"Primary node NULL for deleting data"<<endl;
}
#endif
}
int CCoord_server::updateData(string bufstr, int client_fd)
{
bstNode* primary = put_update_delete_handle(bufstr, client_fd);
#if CACHE_ENABLE
if(primary !=NULL)
{
Document document_p;
document_p.Parse(bufstr.c_str());
assert(document_p.HasMember("key"));
assert(document_p["key"].IsString());
string key = document_p["key"].GetString();
primary->data.cache.deleteKey(key);
assert(document_p.HasMember("value"));
assert(document_p["value"].IsString());
string value = document_p["value"].GetString();
primary->data.cache.putInSet(key, value);
}
else
{
cout<<"Primary node NULL for updating data"<<endl;
}
#endif
}
int CCoord_server::connect_to_slave(slaveData* slave)
{
int sockfd, numbytes;
char buf[BUFFERSIZE];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
char* server_addr = new char[slave->IPaddr.size()+1];
strcpy(server_addr,slave->IPaddr.c_str());
char* server_port = new char[slave->portnum.size()+1];
strcpy(server_port, slave->portnum.c_str());
if ((rv = getaddrinfo(server_addr, server_port, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return -1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect");
cout<<"Slave IP: "<<slave->IPaddr<<endl;
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "client: failed to connect\n");
return -2;
}
//string senderPort = to_string(ntohs(((struct sockaddr_in *)p->ai_addr)->sin_port));
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
printf("client: connecting to %s:%s\n", s, server_port);
freeaddrinfo(servinfo); // all done with this structure
return sockfd;
}
bstNode* CCoord_server::put_update_delete_handle(string bufstr, int client_fd)
{
int primary_fd, secondary_fd,numbytes1, numbytes2;
char primaryResp[BUFFERSIZE], secondaryResp[BUFFERSIZE];
Document document_p, document_s;
document_p.Parse(bufstr.c_str());
assert(document_p.HasMember("key"));
assert(document_p["key"].IsString());
string key = document_p["key"].GetString();
char* str = new char[key.length()+1];
strcpy(str, key.c_str());
Fnv32_t hash_val = fnv_32_str(str, FNV1_32_INIT);
bstNode* primaryNode = bst_upperBound(treeRoot, hash_val);
if(primaryNode == NULL) // If hash value of key is greater than the last slave server, search slave with smallest hash value
{
primaryNode = findMinimum(treeRoot);
}
assert(document_p.HasMember("addAs"));
assert(document_p["addAs"].IsString());
document_p["addAs"] = "1";
//cout<<document_p["addAs"].GetString()<<endl;
bstNode* secondaryNode;
findSuccessor(treeRoot, secondaryNode, primaryNode->data.hashvalue);
if(secondaryNode == NULL) // If hash value of key is greater than the last slave server, search slave with smallest hash value
{
secondaryNode = findMinimum(treeRoot);
}
document_s.Parse(bufstr.c_str());
assert(document_s.HasMember("addAs"));
assert(document_s["addAs"].IsString());
document_s["addAs"] = "0";
//cout<<document_s["addAs"].GetString()<<endl;
StringBuffer p_buffer;
Writer<StringBuffer> p_writer(p_buffer);
document_p.Accept(p_writer);
const char* p_output = p_buffer.GetString();
string p_Sendbuf(p_output);
StringBuffer s_buffer;
Writer<StringBuffer> s_writer(s_buffer);
document_s.Accept(s_writer);
const char* s_output = s_buffer.GetString();
string s_Sendbuf(s_output);
if(primaryNode->data.isActive && secondaryNode->data.isActive)
{
memset(primaryResp,0,BUFFERSIZE);
memset(secondaryResp,0,BUFFERSIZE);
primary_fd = connect_to_slave(&(primaryNode->data));
secondary_fd = connect_to_slave(&(secondaryNode->data));
//cout<<"prim: "<<primary_fd<<"\tsec: "<<secondary_fd<<"\tclient_fd: "<<endl;
thread primarythread(&CCoord_server::data_modify_ThreadFn, this, p_Sendbuf, primaryResp, &numbytes1, primary_fd);
thread secondarythread(&CCoord_server::data_modify_ThreadFn, this, s_Sendbuf, secondaryResp, &numbytes2, secondary_fd);
primarythread.join();
secondarythread.join();
}
else
{
if(!primaryNode->data.isActive)
cout<<"Primary slave down"<<endl;
if(!secondaryNode->data.isActive)
cout<<"Secondary slave down"<<endl;
return NULL;
}
if(send(client_fd, primaryResp, numbytes1, 0) == -1)
perror("send");
return primaryNode;
}
int CCoord_server::data_modify_ThreadFn(string bufstr, char* response, int* numbytes, int fd)
{
if(send(fd, bufstr.c_str(), bufstr.length(), 0) == -1)
perror("send");
memset(response,0,BUFFERSIZE);
if ((*numbytes = recv(fd, response, BUFFERSIZE, 0)) == -1)
{
perror("recv");
return -1;
//exit(1);
}
return 1;
}
int CCoord_server::migrationInitDown(slaveData* slave_down)
{
bstNode* pre=NULL, *succ=NULL, *succsucc=NULL;
char buf[BUFFERSIZE];
int numbytes;
//findPreSuc(treeRoot, pre, succ, slave_down->hashvalue);
findPredecessor(treeRoot, pre, slave_down->hashvalue);
if(pre==NULL)
pre = findMaximum(treeRoot);
treeRoot = deleteBST(&treeRoot, slave_down->hashvalue);
succ = bst_upperBound(treeRoot, slave_down->hashvalue);
if(succ==NULL)
succ = findMinimum(treeRoot);
findSuccessor(treeRoot, succsucc, succ->data.hashvalue);
if(succsucc==NULL)
succsucc = findMinimum(treeRoot);
cout<<"pred: "<<pre->data.IPaddr;
cout<<" succ : "<<succ->data.IPaddr;
cout<<" succsucc: "<<succsucc->data.IPaddr<<endl;
int pred_fd, succ_fd, succsucc_fd;
// Step 1: Connect to succsucc and ask it to GET data from succ to update it's own PREV
succsucc_fd = connect_to_slave(&(succsucc->data));
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","get"));
sendjson.push_back(make_pair("ip",succ->data.IPaddr));
sendjson.push_back(make_pair("port",succ->data.portnum));
sendjson.push_back(make_pair("what","prev"));
sendjson.push_back(make_pair("addTo","prev"));
string tosend = create_json_string(sendjson);
if(send(succsucc_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
//memset(buf,0,BUFFERSIZE);
//if ((numbytes = recv(succsucc_fd, buf, BUFFERSIZE, 0)) == -1)
//{
// perror("recv");
// return -1;
// //exit(1);
//}
sleep(4);
cout<<"Migration: Step-1 complete"<<endl;
close(succsucc_fd);
// Step 2: Ask succ to merge its PREV table with its CURR
// Step 3: Ask succ to GET pred's CURR to update its PREV
succ_fd = connect_to_slave(&(succ->data));
sendjson.clear();
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","merge"));
tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
//memset(buf,0,BUFFERSIZE);
//if ((numbytes = recv(succ_fd, buf, BUFFERSIZE, 0)) == -1)
//{
// perror("recv");
// return -1;
// //exit(1);
//}
sleep(4);
cout<<"Migration: Step-2 complete"<<endl;
close(succ_fd);
succ_fd = connect_to_slave(&(succ->data));
sendjson.clear();
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","get"));
sendjson.push_back(make_pair("ip",pre->data.IPaddr));
sendjson.push_back(make_pair("port",pre->data.portnum));
sendjson.push_back(make_pair("what","curr"));
sendjson.push_back(make_pair("addTo","prev"));
tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
//memset(buf,0,BUFFERSIZE);
//if ((numbytes = recv(succsucc_fd, buf, BUFFERSIZE, 0)) == -1)
//{
// perror("recv");
// return -1;
// //exit(1);
//}
sleep(4);
cout<<"Migration: Step-3 complete"<<endl;
close(succ_fd);
}
int CCoord_server::migrationInitUp(slaveData* slave_up)
{
bstNode* pre=NULL, *succ=NULL, *succsucc=NULL;
char buf[BUFFERSIZE];
int numbytes;
findPreSuc(treeRoot, pre, succ, slave_up->hashvalue);
if(pre==NULL)
pre = findMaximum(treeRoot);
if(succ==NULL)
succ = findMinimum(treeRoot);
findSuccessor(treeRoot, succsucc, succ->data.hashvalue);
if(succsucc==NULL)
succsucc = findMinimum(treeRoot);
int pred_fd, succ_fd, succsucc_fd;
succ_fd = connect_to_slave(&(succ->data));
vector<pair<string,string>> sendjson;
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","send"));
sendjson.push_back(make_pair("ip",slave_up->IPaddr));
sendjson.push_back(make_pair("port",slave_up->portnum));
sendjson.push_back(make_pair("what","prev"));
sendjson.push_back(make_pair("addTo","prev"));
string tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
sendjson.clear();
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","split"));
tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
sendjson.clear();
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","send"));
sendjson.push_back(make_pair("ip",slave_up->IPaddr));
sendjson.push_back(make_pair("port",slave_up->portnum));
sendjson.push_back(make_pair("what","prev"));
sendjson.push_back(make_pair("addTo","curr"));
tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
sendjson.clear();
sendjson.push_back(make_pair("purpose","migration"));
sendjson.push_back(make_pair("task","send"));
sendjson.push_back(make_pair("ip",succsucc->data.IPaddr));
sendjson.push_back(make_pair("port",succsucc->data.portnum));
sendjson.push_back(make_pair("what","curr"));
sendjson.push_back(make_pair("addTo","prev"));
tosend = create_json_string(sendjson);
if(send(succ_fd, tosend.c_str(), tosend.length(), 0) == -1)
perror("send");
close(succ_fd);
}
string CCoord_server::create_json_string(vector<pair<string,string>> &data)
{
string json_string="{";
int i=0;
for(i=0;i<data.size()-1;i++)
{
json_string+="\n\t";
json_string+="\""+data[i].first+"\":\""+data[i].second+"\",";
}
json_string+="\n\t";
json_string+="\""+data[i].first+"\":\""+data[i].second+"\"";
json_string+="\n}";
return json_string;
}
Fnv32_t CCoord_server::hashSlave(slaveData* slave)
{
string ip_port = slave->IPaddr+slave->portnum;
char* str = new char[ip_port.length()+1];
strcpy(str, ip_port.c_str());
//char* str = new char[slave->IPaddr.length()+1];
//strcpy(str, slave->IPaddr.c_str());
Fnv32_t hash_val = fnv_32_str(str, FNV1_32_INIT);
return hash_val;
}
void CCoord_server::insertBST(bstNode** root,slaveData* slave)
{
if(*root==NULL)
{
bstNode* newnode = new bstNode;
newnode->data = *slave;
newnode->leftchild = NULL;
newnode->rightchild = NULL;
*root = newnode;
nodecount++;
return;
}
if(slave->hashvalue < (*root)->data.hashvalue)
insertBST(&((*root)->leftchild), slave);
else
insertBST(&((*root)->rightchild), slave);
}
struct bstNode * minValueNode(struct bstNode* node)
{
struct bstNode* current = node;
while (current && current->leftchild != NULL)
current = current->leftchild;
return current;
}
bstNode* CCoord_server::deleteBST(bstNode** root,Fnv32_t key)
{
// base case
if (*root == NULL) return *root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < (*root)->data.hashvalue)
(*root)->leftchild = deleteBST(&((*root)->leftchild), key);
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > (*root)->data.hashvalue)
(*root)->rightchild = deleteBST(&((*root)->rightchild), key);
// if key is same as root's key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if ((*root)->leftchild == NULL)
{
struct bstNode *temp = (*root)->rightchild;
free(*root);
return temp;
}
else if ((*root)->rightchild == NULL)
{
struct bstNode *temp = (*root)->leftchild;
free(*root);
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
struct bstNode* temp = minValueNode((*root)->rightchild);
// Copy the inorder successor's content to this node
(*root)->data = temp->data;
// Delete the inorder successor
(*root)->rightchild = deleteBST(&((*root)->rightchild), temp->data.hashvalue);
}
return *root;
}
bstNode* CCoord_server::bst_upperBound(bstNode* root,Fnv32_t val)
{
if(root==NULL)
return NULL;
if (root->leftchild == NULL && root->rightchild == NULL && root->data.hashvalue < val)
return NULL;
if ((root->data.hashvalue >= val && root->leftchild == NULL) || (root->data.hashvalue >= val && root->leftchild->data.hashvalue < val))
return root;
if (root->data.hashvalue <= val)
return bst_upperBound(root->rightchild, val);
else
return bst_upperBound(root->leftchild, val);
}
void CCoord_server::findPreSuc(bstNode* root, bstNode*& pre, bstNode*& suc, Fnv32_t key)
{
// Base case
if (root == NULL) return ;
// If key is present at root
if (root->data.hashvalue == key)
{
// the maximum value in left subtree is predecessor
if (root->leftchild != NULL)
{
bstNode* tmp = root->leftchild;
while (tmp->rightchild)
tmp = tmp->rightchild;
pre = tmp ;
}
// the minimum value in right subtree is successor
if (root->rightchild != NULL)
{
bstNode* tmp = root->rightchild ;
while (tmp->leftchild)
tmp = tmp->leftchild ;
suc = tmp ;
}
return ;
}
// If key is smaller than root's key, go to left subtree
if (root->data.hashvalue > key)
{
suc = root ;
findPreSuc(root->leftchild, pre, suc, key) ;
}
else // go to right subtree
{
pre = root ;
findPreSuc(root->rightchild, pre, suc, key) ;
}
}
bstNode* CCoord_server::findMinimum(bstNode* root)
{
while (root->leftchild)
root = root->leftchild;
return root;
}
bstNode* CCoord_server::findMaximum(bstNode* root)
{
while (root->rightchild)
root = root->rightchild;
return root;
}
void CCoord_server::findPredecessor(bstNode* root, bstNode*& pre, Fnv32_t key)
{
// base case
if (root == NULL) {
pre = NULL;
return;
}
// if node with key's value is found, the predecessor is maximum value
// node in its left subtree (if any)
if (root->data.hashvalue == key)
{
if (root->leftchild)
pre = findMaximum(root->leftchild);
}
// if given key is less than the root node, recur for left subtree
else if (key < root->data.hashvalue)
{
findPredecessor(root->leftchild, pre, key);
}
// if given key is more than the root node, recur for right subtree
else
{
// update predecessor to current node before recursing
// in right subtree
pre = root;
findPredecessor(root->rightchild, pre, key);
}
}