Commit 93938e76 authored by Mayank Manoj's avatar Mayank Manoj

Merge branch 'mayank' into 'master'

code running properly

See merge request !5
parents e140c269 0dc1284e
......@@ -26,7 +26,7 @@ public:
{
return;
}
virtual string getKeyValuePairs(int id)
virtual string getKeyValuePairs(int new_id,int id)
{
return "This will never run";
}
......@@ -75,9 +75,9 @@ public:
mycache.pushAll();
}
string getKeyValuePairs(int id)
string getKeyValuePairs(int new_id,int id)
{
return mycache.getKeyValuePairs(id);
return mycache.getKeyValuePairs(new_id,id);
}
};
......@@ -124,8 +124,8 @@ public:
}
// key1;key2;key3;;value1;value2;value3;
string getKeyValuePairs(int id)
string getKeyValuePairs(int new_id,int id)
{
return mycache.getKeyValuePairs(id);
return mycache.getKeyValuePairs(new_id,id);
}
};
\ No newline at end of file
......@@ -160,7 +160,7 @@ void RunClient() {
Info info;
ClientContext context;
Status status=stub->GETADDRESS(&context,null,&info);
std::cout<<"Server: "<<info.address()<<std::endl;
std::cout<<"Connected to Server: "<<info.address()<<std::endl;
std::string target_address(info.address());
// Instantiates the client
KeyValueServicesClient client(
......
......@@ -105,12 +105,10 @@ public:
new ServerData(service, cq, reqType);
if (reqType == NEW) {
cout<<"New Server to join:"<<info.address()<<endl;
//calculate id of node, return it's successor and predecessor
string address=info.address();
int id=stoi(address.substr(address.find(':')+1));
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -162,7 +160,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -175,21 +172,20 @@ public:
nextpred=y.id();
}
if(fl==false&&next!=my_id&&nextpred==node) {
cout<<"Yes it is. So we found the successor and the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<x.id()<<endl;
cout<<"Predecessor: "<<nextpred<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(x.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(nextpred));
}
else if(fl==false&&next==my_id&&mypred==node) {
cout<<"Yes it is. So we found the successor and the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<x.id()<<endl;
cout<<"Predecessor: "<<mypred<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(x.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(mypred));
}
else if(fl==false){
cout<<"No it is not. We will ask the possible predecessor to find the successor of new node"<<endl;
if(node==my_id) {
int mysucc;
fin.open(NEIGHBOURS);
......@@ -199,6 +195,9 @@ public:
fin.close();
mysucc=stoi(temp.substr(temp.find(':')+1));
if(mysucc>=id) {
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<mysucc<<endl;
cout<<"Predecessor: "<<my_id<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(mysucc));
successorInfo.set_predaddress("0.0.0.0:"+to_string(my_id));
}
......@@ -208,15 +207,15 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<mysucc<<" for the successor of new node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
Id z;
string t_address("0.0.0.0:"+to_string(y.id()));
channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
ClientContext context2;
stub->GETPREDECESSOR(&context2,y,&z);
cout<<"Yes. We got the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<y.id()<<endl;
cout<<"Predecessor: "<<z.id()<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
......@@ -229,15 +228,15 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" for the successor of the new node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
Id z;
string t_address("0.0.0.0:"+to_string(y.id()));
channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
ClientContext context2;
stub->GETPREDECESSOR(&context2,y,&z);
cout<<"Yes. We got the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<y.id()<<endl;
cout<<"Predecessor: "<<z.id()<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
......@@ -253,6 +252,9 @@ public:
fin.close();
int xi=stoi(xyz.substr(xyz.find(':')+1));
if(xi==node) {
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<next<<endl;
cout<<"Predecessor: "<<node<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(next));
successorInfo.set_predaddress("0.0.0.0:"+to_string(node));
}
......@@ -262,8 +264,8 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" for the successor of new node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
Id z;
string t_address("0.0.0.0:"+to_string(y.id()));
......@@ -271,13 +273,16 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context2;
stub->GETPREDECESSOR(&context2,y,&z);
cout<<"Yes. We got the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<y.id()<<endl;
cout<<"Predecessor: "<<z.id()<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(z.id()));
}
else {
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<next<<endl;
cout<<"Predecessor: "<<node<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(next));
successorInfo.set_predaddress("0.0.0.0:"+to_string(node));
}
......@@ -292,6 +297,9 @@ public:
fin1.close();
int t1i=stoi(t1.substr(t1.find(':')+1));
if(t1==t2||t1i==node) {
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<node<<endl;
cout<<"Predecessor: "<<next<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(node));
successorInfo.set_predaddress("0.0.0.0:"+to_string(next));
}
......@@ -301,8 +309,8 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" for the successor of new node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
Id z;
string t_address("0.0.0.0:"+to_string(y.id()));
......@@ -310,13 +318,16 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context2;
stub->GETPREDECESSOR(&context2,y,&z);
cout<<"Yes. We got the predecessor"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<y.id()<<endl;
cout<<"Predecessor: "<<z.id()<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(z.id()));
}
else {
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<next<<endl;
cout<<"Predecessor: "<<node<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(next));
successorInfo.set_predaddress("0.0.0.0:"+to_string(node));
}
......@@ -325,12 +336,12 @@ public:
}
}
else {
cout<<"We got no node with id greater than new node"<<endl;
cout<<"Only one node present right now, that is me"<<endl;
cout<<"We found the successor and predecessor for the new node:-"<<endl;
cout<<"Successor: "<<my_id<<endl;
cout<<"Predecessor: "<<my_id<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(my_id));
successorInfo.set_predaddress("0.0.0.0:"+to_string(my_id));
}
cout<<"Sending the successor and predecessor back to the new server"<<endl;
newResponder.Finish(successorInfo,Status::OK,this);
}
else if(reqType==INFORMSUCCESSOR){
......@@ -340,7 +351,6 @@ public:
string keyvalues=memManager->getKeyValuePairs(id,stoi(params["LISTENING_PORT"]));
string keys=keyvalues.substr(0,keyvalues.find(";;")+1);
string values=keyvalues.substr(keyvalues.find(";;")+2);
cout<<"Okay, my new predecessor is: "<<info.address()<<endl;
ifstream fin;
fin.open(NEIGHBOURS);
string successor,predecessor;
......@@ -355,15 +365,15 @@ public:
fout.close();
keyValues.set_keys(keys);
keyValues.set_values(values);
cout<<"Done making changes accordingly"<<endl;
cout<<"My new Predecessor: "<<id<<endl;
cout<<"Keys given to "<<id<<endl;
informSuccessorResponder.Finish(keyValues,Status::OK,this);
}
else if(reqType==GETSUCCESSOR) {
cout<<"Some server asked me to find the successor of "<<idvar1.id()<<endl;
cout<<"Request to find successor of "<<idvar1.id()<<endl;
int id=idvar1.id();
int fingers[16];
ifstream fin;
cout<<"Getting the finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
for(int i=0;i<16;i++) {
......@@ -372,6 +382,7 @@ public:
if(temp=="null"||temp.size()==0)
break;
fingers[i]=stoi(temp);
nums++;
}
fin.close();
int node=-1;
......@@ -415,7 +426,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -427,10 +437,14 @@ public:
stub->GETPREDECESSOR(&context,x,&y);
nextpred=y.id();
}
if(fl==false&&next!=my_id&&nextpred==node)
if(fl==false&&next!=my_id&&nextpred==node) {
cout<<"Successor found: "<<x.id()<<endl;
idvar2.set_id(x.id());
else if(fl==false&&next==my_id&&mypred==node)
}
else if(fl==false&&next==my_id&&mypred==node) {
cout<<"Successor found: "<<x.id()<<endl;
idvar2.set_id(x.id());
}
else if(fl==false){
if(node==my_id) {
int mysucc;
......@@ -440,15 +454,19 @@ public:
getline(fin,temp);
fin.close();
mysucc=stoi(temp.substr(temp.find(':')+1));
if(mysucc>=id)
if(mysucc>=id) {
cout<<"Successor found: "<<mysucc<<endl;
idvar2.set_id(mysucc);
}
else {
string tar_address("0.0.0.0:"+to_string(mysucc));
channel=grpc::CreateChannel(tar_address, grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<mysucc<<" to find successor of given node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Successor found: "<<y.id()<<endl;
idvar2.set_id(y.id());
}
}
......@@ -458,7 +476,9 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" to find successor of given node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Successor found: "<<y.id()<<endl;
idvar2.set_id(y.id());
}
}
......@@ -471,6 +491,7 @@ public:
fin.close();
int xi=stoi(xyz.substr(xyz.find(':')+1));;
if(xi==node) {
cout<<"Successor found: "<<next<<endl;
idvar2.set_id(next);
}
else {
......@@ -479,12 +500,14 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" to find the successor of given node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
cout<<"Successor found: "<<y.id()<<endl;
idvar2.set_id(y.id());
}
else {
cout<<"Successor found: "<<next<<endl;
idvar2.set_id(next);
}
}
......@@ -498,7 +521,7 @@ public:
fin1.close();
int t1i=stoi(t1.substr(t1.find(':')+1));
if(t1==t2||t1i==node) {
cout<<"Here"<<endl;
cout<<"Successor found: "<<node<<endl;
idvar2.set_id(node);
}
else {
......@@ -507,12 +530,14 @@ public:
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(id);
cout<<"Asking node "<<node<<" to find the successor of given node"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
cout<<"Successor found: "<<y.id()<<endl;
idvar2.set_id(y.id());
}
else {
cout<<"Successor found: "<<next<<endl;
idvar2.set_id(next);
}
}
......@@ -522,7 +547,7 @@ public:
getSuccessorResponder.Finish(idvar2,Status::OK,this);
}
else if(reqType==GETPREDECESSOR) {
cout<<"Someone asked me for my predecessor. Sending them"<<endl;
cout<<"Request for my Predecessor"<<endl;
ifstream fin;
fin.open(NEIGHBOURS);
string successor,predecessor;
......@@ -530,12 +555,11 @@ public:
getline(fin,predecessor);
fin.close();
idvar2.set_id(stoi(predecessor.substr(predecessor.find(':')+1)));
cout<<"Sent my predecessor"<<endl;
cout<<"Sent my predecessor: "<<idvar2.id()<<endl;
getPredecessorResponder.Finish(idvar2,Status::OK,this);
}
else if(reqType==INFORMPREDECESSOR) {
cout<<"Okay, i got the information that my successor has changed"<<endl;
cout<<"My new successor: "<<info.address()<<endl;
cout<<"My new Successor: "<<info.address()<<endl;
ifstream fin;
fin.open(NEIGHBOURS);
string successor,predecessor;
......@@ -549,10 +573,10 @@ public:
fout<<predecessor<<endl;
fout.close();
null.set_nothing(0);
cout<<"Okay, i made the necessary changes"<<endl;
informPredecessorResponder.Finish(null,Status::OK,this);
}
else {
cout<<"Updating finger table"<<endl;
string addresses=addressarr.addresses();
int num=addressarr.servers();
int ids[num];
......@@ -602,6 +626,7 @@ public:
fout.close();
Null n;
n.set_nothing(0);
cout<<"Finger table updated"<<endl;
updateTableResponder.Finish(n,Status::OK,this);
}
status = FINISH;
......@@ -671,10 +696,10 @@ public:
else
pred_id=stoi(pred.substr(pred.find(':')+1));
if(my_id<key_id&&!(pred_id<key_id&&pred_id>my_id)) {
cout<<"I don't have the key"<<endl;
//transfer request
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -726,7 +751,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -791,7 +815,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -818,7 +841,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -833,6 +855,7 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->GET(&cont1,key,&value);
}
else if(my_id==key_id||(my_id<key_id&&pred_id<key_id&&pred_id>my_id)) {
......@@ -850,6 +873,7 @@ public:
value.set_error(v);
}
cout << " RETURN VALUE : " << value.value() << endl;
memManager->traverse();
}
else {
if(pred_id==-1||pred_id<key_id) {
......@@ -867,12 +891,13 @@ public:
value.set_error(v);
}
cout << " RETURN VALUE : " << value.value() << endl;
memManager->traverse();
}
else {
cout<<"I don't have the key"<<endl;
//transfer the request
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -924,7 +949,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -989,7 +1013,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1016,7 +1039,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1031,6 +1053,7 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->GET(&cont1,key,&value);
}
}
......@@ -1055,10 +1078,10 @@ public:
else
succ_id=stoi(scc.substr(scc.find(':')+1));
if(my_id<key_id&&!(pred_id<key_id&&pred_id>my_id)) {
cout<<"I don't have the key"<<endl;
//transfer request
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -1110,7 +1133,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -1175,7 +1197,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1202,7 +1223,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1217,6 +1237,7 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->PUT(&cont1,keyvalue,&stat);
}
else if(my_id==key_id||(my_id<key_id&&pred_id<key_id&&pred_id>my_id)) {
......@@ -1228,6 +1249,7 @@ public:
pthread_mutex_unlock(&_masterLock);
stat.set_status(200);
memManager->traverse();
}
else {
if(pred_id==-1||(pred_id<key_id&&my_id>=key_id)||(pred_id>my_id&&my_id>=key_id)) {
......@@ -1239,12 +1261,13 @@ public:
pthread_mutex_unlock(&_masterLock);
stat.set_status(200);
memManager->traverse();
}
else {
cout<<"I don't have the key"<<endl;
//transfer the request
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -1296,7 +1319,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -1361,7 +1383,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1388,7 +1409,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1403,6 +1423,7 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->PUT(&cont1,keyvalue,&stat);
}
}
......@@ -1423,10 +1444,10 @@ public:
else
pred_id=stoi(pred.substr(pred.find(':')+1));
if(my_id<key_id&&!(pred_id<key_id&&pred_id>my_id)) {
cout<<"I don't have the key"<<endl;
//transfer request
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -1478,7 +1499,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -1543,7 +1563,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1570,7 +1589,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1585,6 +1603,7 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->DEL(&cont1,key,&stat);
}
else if(my_id==key_id||(my_id<key_id&&pred_id<key_id&&pred_id>my_id)) {
......@@ -1602,6 +1621,7 @@ public:
stat.set_status(400);
stat.set_error("KEY NOT EXIST");
}
memManager->traverse();
}
else {
if(pred_id==-1||pred_id<key_id) {
......@@ -1619,11 +1639,12 @@ public:
stat.set_status(400);
stat.set_error("KEY NOT EXIST");
}
memManager->traverse();
}
else {
cout<<"I don't have the key"<<endl;
int fingers[16];
ifstream fin;
cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
do {
......@@ -1675,7 +1696,6 @@ public:
Id x;
x.set_id(next);
Id y;
cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
int mypred,nextpred;
fin.open(NEIGHBOURS);
string temp;
......@@ -1740,7 +1760,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1767,7 +1786,6 @@ public:
ClientContext context1;
x.set_id(key_id);
stub->GETSUCCESSOR(&context1,x,&y);
cout<<"Yes. We got the successor"<<endl;
if(y.id()!=my_id) {
succ=y.id();
}
......@@ -1782,13 +1800,13 @@ public:
shared_ptr<Channel> channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext cont1;
cout<<"Forwarding request to node "<<succ<<endl;
stub->DEL(&cont1,key,&stat);
}
}
delResponder.Finish(stat, Status::OK, this);
}
/* --------------------------------CONTENT OF CACHE ONLY KEY-------------------------------- */
memManager->traverse();
status = FINISH;
} else {
GPR_ASSERT(status == FINISH);
......@@ -1947,7 +1965,6 @@ void updateAllFingerTables() {
}
void register_server_DNS(string my_address) {
cout<<"Registering to DNS"<<endl;
string target_address(DNS_SERVER);
shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub;
......@@ -1957,29 +1974,22 @@ void register_server_DNS(string my_address) {
Info info;
ClientContext context;
Status status=stub->GETADDRESS(&context,null,&info);
cout<<"Address received:"<<info.address()<<endl;
string old_server;
if(status.ok()) {
old_server=info.address();
info.set_address(my_address);
ClientContext new_context;
cout<<"Adding address to DNS"<<endl;
stub->ADDADDRESS(&new_context,info,&null);
cout<<"Address added to DNS"<<endl;
ofstream fout;
cout<<"Generating initial finger table"<<endl;
fout.open(FINGER_TABLE);
for(int i=0;i<16;i++)
fout<<"null"<<endl;
fout.close();
cout<<"Initial finger table generated"<<endl;
if(old_server=="null") {
cout<<"Initializing initial neighbours"<<endl;
fout.open(NEIGHBOURS);
fout<<"-1"<<endl;
fout<<"-1"<<endl;
fout.close();
cout<<"Initialized initial neighbours"<<endl;
return;
}
channel=grpc::CreateChannel(old_server,grpc::InsecureChannelCredentials());
......@@ -1989,25 +1999,21 @@ void register_server_DNS(string my_address) {
ClientContext context1;
cout<<"Sending request to server: "<<old_server<<endl;
status=stub->NEW(&context1,info,&successorInfo);
cout<<"Request sent. Successor and predecessor info received"<<endl;
if(status.ok()) {
string successor=successorInfo.succaddress();
string predecessor=successorInfo.predaddress();
ofstream fout;
cout<<"Storing neighbours info"<<endl;
fout.open(NEIGHBOURS);
fout<<successor<<endl;
fout<<predecessor<<endl;
fout.close();
cout<<"Successor: "<<successor<<endl;
cout<<"Predecessor: "<<predecessor<<endl;
cout<<"Stored neighbours info"<<endl;
channel=grpc::CreateChannel(successor,grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
info.set_address(my_address);
KeyValues keyValues;
ClientContext context2;
cout<<"Informing successor about my presence"<<endl;
status=stub->INFORMSUCCESSOR(&context2,info,&keyValues);
string keys=keyValues.keys();
string values=keyValues.values();
......@@ -2022,14 +2028,11 @@ void register_server_DNS(string my_address) {
values=values.substr(values.find(';')+1);
memManager->put(key,value);
}
cout<<"Informed succesor"<<endl;
channel=grpc::CreateChannel(predecessor,grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
info.set_address(my_address);
ClientContext context3;
cout<<"Informing predecessor about my presence"<<endl;
status=stub->INFORMPREDECESSOR(&context3,info,&null);
cout<<"Informed predecessor"<<endl;
updateAllFingerTables();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment