5d292c9d8b2c4f2ec0a7eb91b42e2257d1bbbaf6
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstmserver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pthread.h>
5 #include <netdb.h>
6 #include <fcntl.h>
7 #include "dstm.h"
8 #include "mlookup.h"
9 #include "llookup.h"
10
11 #define LISTEN_PORT 2156
12 #define BACKLOG 10 //max pending connections
13 #define RECEIVE_BUFFER_SIZE 2048
14
15 extern int classsize[];
16
17 objstr_t *mainobjstore;
18
19 int dstmInit(void)
20 {
21         //Initialize main object store
22         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);    
23         if (mhashCreate(HASH_SIZE, LOADFACTOR))
24                 return 1; //failure
25         
26         if (lhashCreate(HASH_SIZE, LOADFACTOR))
27                 return 1; //failure
28         
29         //pthread_t threadListen;
30         //pthread_create(&threadListen, NULL, dstmListen, NULL);
31         
32         return 0;
33 }
34
35 void *dstmListen()
36 {
37         int listenfd, acceptfd;
38         struct sockaddr_in my_addr;
39         struct sockaddr_in client_addr;
40         socklen_t addrlength = sizeof(struct sockaddr);
41         pthread_t thread_dstm_accept;
42         int i;
43
44         listenfd = socket(AF_INET, SOCK_STREAM, 0);
45         if (listenfd == -1)
46         {
47                 perror("socket");
48                 exit(1);
49         }
50
51         my_addr.sin_family = AF_INET;
52         my_addr.sin_port = htons(LISTEN_PORT);
53         my_addr.sin_addr.s_addr = INADDR_ANY;
54         memset(&(my_addr.sin_zero), '\0', 8);
55
56         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
57         {
58                 perror("bind");
59                 exit(1);
60         }
61         
62         if (listen(listenfd, BACKLOG) == -1)
63         {
64                 perror("listen");
65                 exit(1);
66         }
67
68         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
69         while(1)
70         {
71                 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
72                 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
73         }
74         pthread_exit(NULL);
75 }
76
77 void *dstmAccept(void *acceptfd)
78 {
79         int numbytes,i, val, retval;
80         unsigned int oid;
81         char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
82         char *ptr;
83         void *srcObj;
84         objheader_t *h;
85         trans_commit_data_t transinfo;
86         
87         int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
88
89         printf("Recieved connection: fd = %d\n", (int)acceptfd);
90         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
91                 perror("Error in receiving control from coordinator\n");
92                 return;
93         }
94         switch(control) {
95                 case READ_REQUEST:
96                         printf("DEBUG -> Recv READ_REQUEST from Coordinator\n");
97                         if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
98                                 perror("Error receiving object from cooridnator\n");
99                                 return;
100                         }
101                         srcObj = mhashSearch(oid);
102                         h = (objheader_t *) srcObj;
103                         size = sizeof(objheader_t) + sizeof(classsize[h->type]);
104                         if (h == NULL) {
105                                 ctrl = OBJECT_NOT_FOUND;
106                                 if(send((int)acceptfd, &ctrl, sizeof(char), 0) < 0) {
107                                         perror("Error sending control msg to coordinator\n");
108                                 }
109                         } else {
110                                 //char responsemessage[sizeof(char)+sizeof(int)];
111                                 /* Type */
112                                 ctrl = OBJECT_FOUND;
113                                 if(send((int)acceptfd, &ctrl, sizeof(char), 0) < 0) {
114                                         perror("Error sending control msg to coordinator\n");
115                                 }
116
117                                 //responsemessage[0]=OBJECT_FOUND;
118                                 /* Size of object */
119                                 //*((int *)(&responsemessage[1])) = sizeof(objheader_t) + classsize[h->type];
120                                 //if(send((int)acceptfd, &responsemessage, sizeof(responsemessage), 0) < 0) {
121                                 //      perror("Error sending control msg to coordinator\n");
122                                 //}
123
124                                 /* Size of object */
125                                 if(send((int)acceptfd, &size, sizeof(int), 0) < 0) {
126                                         perror("Error sending size of object to coordinator\n");
127                                 }
128                                 if(send((int)acceptfd, h, size, 0) < 0) {
129                                         perror("Error in sending object\n");
130                                 }
131                         }
132                         break;
133                 
134                 case READ_MULT_REQUEST:
135                         printf("DEBUG-> READ_MULT_REQUEST\n");
136                         break;
137         
138                 case MOVE_REQUEST:
139                         printf("DEBUG -> MOVE_REQUEST\n");
140                         break;
141
142                 case MOVE_MULT_REQUEST:
143                         printf("DEBUG -> MOVE_MULT_REQUEST\n");
144                         break;
145
146                 case TRANS_REQUEST:
147                         printf("DEBUG -> Recv TRANS_REQUEST from Coordinator\n");
148                         if((val = readClientReq((int)acceptfd, &transinfo)) != 0) {
149                                 printf("Error in readClientReq\n");
150                         }
151                         break;
152
153                 default:
154                         printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
155         }
156         if (close((int)acceptfd) == -1)
157         {
158                 perror("close");
159         }
160         else
161                 printf("Closed connection: fd = %d\n", (int)acceptfd);
162         
163         //Free memory
164         free(transinfo.objmod);
165         free(transinfo.objlocked);
166         free(transinfo.objnotfound);
167         pthread_exit(NULL);
168 }
169
170 int readClientReq(int acceptfd, trans_commit_data_t *transinfo) {
171         char *ptr, control, prevctrl, sendctrl, newctrl;
172         void *modptr, *header;
173         objheader_t *tmp_header;
174         fixed_data_t fixed;
175         int sum = 0, i, N, n, val, retval;
176
177         //Reads to process the TRANS_REQUEST protocol further
178         // Read fixed_data
179         N = sizeof(fixed) - 1;
180         ptr = (char *)&fixed;;
181         fixed.control = TRANS_REQUEST;
182         do {
183                 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
184         //      printf("DEBUG -> 1. Reading %d bytes \n", n);
185                 sum += n;
186         } while(sum < N && n != 0); 
187
188         //printf("Machine count = %d\tnumread = %d\tnummod = %d\tsum_bytes = %d\n", fixed.mcount, fixed.numread, fixed.nummod, fixed.sum_bytes);
189         // Read list of mids
190         int mcount = fixed.mcount;
191         N = mcount * sizeof(unsigned int);
192         unsigned int listmid[mcount];
193         ptr = (char *) listmid;
194         sum = 0;
195         do {
196                 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
197         //      printf("DEBUG -> 2. Reading %d bytes cap = %d\n", n, N);
198                 sum += n;
199         } while(sum < N && n != 0);
200
201         // Read oid and version tuples
202         int numread = fixed.numread;
203         N = numread * (sizeof(unsigned int) + sizeof(short));
204         char objread[N];
205         if(numread != 0) { // If pile contains objects to be read 
206         //      N = numread * (sizeof(unsigned int) + sizeof(short));
207         //      char objread[N];
208                 sum = 0;
209                 do {
210                         n = recv((int)acceptfd, (void *) objread, N, 0);
211                 //      printf("DEBUG -> 3. Reading %d bytes cap = %d\n", n, N);
212                         sum += n;
213                 } while(sum < N && n != 0);
214 //              printf("DEBUG -> Recv objs from Coordinator %d %d %d %d\n", *objread, *(objread + 6), *(objread + 12), *(objread + 18));
215         }
216         
217         // Read modified objects
218         if(fixed.nummod != 0) { // If pile contains modified objects 
219                 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
220                         printf("objstrAlloc error for modified objects %s, %d", __FILE__, __LINE__);
221                         return 1;
222                 }
223                 sum = 0;
224                 do { // Recv the objs that are modified at Coordinator
225                         n = recv((int)acceptfd, modptr+sum, fixed.sum_bytes-sum, 0);
226                 //      printf("DEBUG -> 4. Reading %d bytes cap = %d, oid = %d\n", n, fixed.sum_bytes, *((int *)modptr));
227                         sum += n;
228                 } while (sum < fixed.sum_bytes && n != 0);
229         }
230
231         //Send control message as per all votes from all oids in the machine
232         if((prevctrl = handleTransReq(acceptfd, &fixed, transinfo, listmid, objread, modptr)) == 0 ) {
233                 printf("Handle req error\n");
234         }
235
236         //Read for new control message from Coordiator
237         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
238                 perror("Error in receiving control message");
239                 return 1;
240         }
241
242         switch(control) {
243                 case TRANS_ABORT:
244                         printf("DEBUG -> Recv TRANS_ABORT from Coordinator\n");
245                         //send ack to coordinator
246                         sendctrl = TRANS_SUCESSFUL;
247                         if(send((int)acceptfd, &sendctrl, sizeof(char), 0) < 0) {
248                                 perror("Error sending ACK to coordinator\n");
249                                 return 1;
250                         }
251                         //Mark all ref counts as 1 and do garbage collection
252                         ptr = modptr;
253                         for(i = 0; i< fixed.nummod; i++) {
254                                 tmp_header = (objheader_t *)ptr;
255                                 tmp_header->rcount = 1;
256                                 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
257                         }
258                         //Unlock objects that was locked in this machine due to this transaction
259                         for(i = 0; i< transinfo->numlocked; i++) {
260                                 header = mhashSearch(transinfo->objlocked[i]);// find the header address
261                                 ((objheader_t *)header)->status &= ~(LOCK);             
262                         }
263                         ptr = NULL;
264                         return 0;
265
266                 case TRANS_COMMIT:
267                         printf("DEBUG -> Recv TRANS_COMMIT from Coordinator\n");
268                         if((val = transCommitProcess(transinfo, (int)acceptfd)) != 0) {
269                                 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
270                         }
271                         break;
272                 case TRANS_ABORT_BUT_RETRY_COMMIT:
273                         printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT from Coordinator\n");
274                         //Process again after waiting for sometime and on prev control message sent
275                         switch(prevctrl) {
276                                 case TRANS_AGREE:
277                                         sendctrl = TRANS_AGREE;
278                                         if(send((int)acceptfd, &sendctrl, sizeof(char), 0) < 0) {
279                                                 perror("Error sending ACK to coordinator\n");
280                                         }
281                                         sleep(5);
282                                         break;
283                                 case TRANS_SOFT_ABORT:
284                                         if((newctrl = handleTransReq(acceptfd, &fixed, transinfo, listmid, objread, modptr)) == 0 ) {
285                                                 printf("Handle req error\n");
286                                         }
287                                         if(newctrl == prevctrl){
288                                                 //Send ABORT
289                                                 newctrl = TRANS_DISAGREE;
290                                                 if(send((int)acceptfd, &newctrl, sizeof(char), 0) < 0) {
291                                                         perror("Error sending ACK to coordinator\n");
292                                                 }
293                                                 //Set the reference count of the object to 1 in mainstore for garbage collection
294                                                 ptr = modptr;
295                                                 for(i = 0; i< fixed.nummod; i++) {
296                                                         tmp_header = (objheader_t *) ptr;
297                                                         tmp_header->rcount = 1;
298                                                         ptr += sizeof(objheader_t) + classsize[tmp_header->type];
299                                                 }
300                                                 //Unlock objects that was locked in this machine due to this transaction
301                                                 for(i = 0; i< transinfo->numlocked; i++) {
302                                                         ptr = mhashSearch(transinfo->objlocked[i]);// find the header address
303                                                         ((objheader_t *)ptr)->status &= ~(LOCK);                
304                                                 }
305                                                 return 0;
306                                         } else {
307                                                 //Send new control message
308                                                 if(send((int)acceptfd, &newctrl, sizeof(char), 0) < 0) {
309                                                         perror("Error sending ACK to coordinator\n");
310                                                 }
311                                         }
312
313                                         break;
314                         }
315
316                         break;
317                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
318                         //TODO expect another transrequest from client
319                         printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING from Coordinator\n");
320                         break;
321                 default:
322                         printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
323                         //TODO Use fixed.trans_id  TID since Client may have died
324                         break;
325         }
326
327         return 0;
328 }
329
330 //This function runs a decision after all objects are weighed under one of the 4 possibilities 
331 //and returns the appropriate control message to the Ccordinator 
332 char handleTransReq(int acceptfd, fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr) {
333         int val;
334         short version;
335         char control = 0, ctrlmissoid, *ptr;
336         int i, j = 0;
337         unsigned int oid;
338         unsigned int *oidnotfound, *oidlocked, *oidmod;
339
340         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
341         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
342         oidmod = (unsigned int *) calloc(fixed->nummod, sizeof(unsigned int));
343
344         // Counters and arrays to formulate decision on control message to be sent
345         int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
346         int objmodnotfound = 0, nummodfound = 0;
347         void *mobj;
348         objheader_t *headptr;
349         
350         //Process each object present in the pile 
351         ptr = modptr;
352         //printf("DEBUG -> Total objs involved in trans is %d\n",fixed->nummod + fixed->numread);
353         fflush(stdout);
354         //Process each oid in the machine pile/ group
355         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
356                 if (i < fixed->numread) {//Object is read
357                         int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
358                         incr *= i;
359                         oid = *((unsigned int *)(objread + incr));
360                         incr += sizeof(unsigned int);
361                         version = *((short *)(objread + incr));
362                 } else {//Obj is modified
363                         headptr = (objheader_t *) ptr;
364                         oid = headptr->oid;
365                         oidmod[objmod] = oid;//Array containing modified oids
366                         objmod++;
367                         version = headptr->version;
368                         ptr += sizeof(objheader_t) + classsize[headptr->type];
369                 }
370                 //Check if object is still present in the machine since the beginning of TRANS_REQUEST
371                 if ((mobj = mhashSearch(oid)) == NULL) {// Obj not found
372                         //Save the oids not found for later use
373                         oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
374                         objnotfound++;
375                 } else { // If obj found in machine (i.e. has not moved)
376                         //Check if obj is locked
377                         if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {           
378                                 if (version == ((objheader_t *)mobj)->version) {      // If version match
379                                         v_matchlock++;
380                                 } else {//If versions don't match ..HARD ABORT
381                                         v_nomatch++;
382                                         //send TRANS_DISAGREE to Coordinator
383                                         control = TRANS_DISAGREE;
384                                         if((val = write(acceptfd, &control, sizeof(char))) <= 0) {
385                                                 perror("Error in sending control to the Coordinator\n");
386                                                 return 0;
387                                         }
388                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
389                                         return control;
390                                 }
391                         } else {//Obj is not locked , so lock object
392                                 ((objheader_t *)mobj)->status |= LOCK;
393                                 //Save all object oids that are locked on this machine during this transaction request call
394                                 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
395                                 objlocked++;
396                                 if (version == ((objheader_t *)mobj)->version) { //If versions match
397                                         v_matchnolock++;
398                                 } else { //If versions don't match
399                                         v_nomatch++;
400                                         //send TRANS_DISAGREE to Coordinator
401                                         control = TRANS_DISAGREE;
402                                         if((val = write(acceptfd, &control, sizeof(char))) <= 0) {
403                                                 perror("Error in sending control to the Coordinator\n");
404                                                 return 0;
405                                         }
406                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
407                                         return control;
408                                 }
409                         }
410                 }
411         }
412
413         //printf("No of objs locked = %d\n", objlocked);
414         //printf("No of v_nomatch = %d\n", v_nomatch);
415         //printf("No of objs v_match but are did not have locks before = %d\n", v_matchnolock);
416         //printf("No of objs v_match but had locks before = %d\n", v_matchlock);
417         //printf("No of objs not found = %d\n", objnotfound);
418         //printf("No of objs modified but not found = %d\n", objmodnotfound);
419
420         //Decide what control message(s) to send
421         if(v_matchnolock == fixed->numread + fixed->nummod) {
422                 //send TRANS_AGREE to Coordinator
423                 control = TRANS_AGREE;
424                 if((val = write(acceptfd, &control, sizeof(char))) <= 0) {
425                         perror("Error in sending control to Coordinator\n");
426                         return 0;
427                 }
428                 printf("DEBUG -> Sending TRANS_AGREE\n");
429         }
430
431         if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
432                 //send TRANS_SOFT_ABORT to Coordinator
433                 control = TRANS_SOFT_ABORT;
434                 if((val = write(acceptfd, &control, sizeof(char))) <=0 ) {
435                         perror("Error in sending control back to coordinator\n");
436                         return 0;
437                 }
438                 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
439                 //send number of oids not found and the missing oids 
440                 if((val = write(acceptfd, &objnotfound, sizeof(int))) <= 0) {
441                         perror("Error in sending no of objects that are not found\n");
442                         return 0;
443                 }
444                 if(objnotfound != 0) { 
445                         if((val = write(acceptfd, oidnotfound, (sizeof(unsigned int) * objnotfound))) <= 0) {
446                                 perror("Error in sending objects that are not found\n");
447                                 return 0;
448                         }
449                 }
450         }
451         
452         //Do the following when TRANS_DISAGREE is sent
453         if(control == TRANS_DISAGREE) {
454                 //Set the reference count of the object to 1 in mainstore for garbage collection
455                 ptr = modptr;
456                 for(i = 0; i< fixed->nummod; i++) {
457                         headptr = (objheader_t *) ptr;
458                         headptr->rcount = 1;
459                         ptr += sizeof(objheader_t) + classsize[headptr->type];
460                 }
461                 //Unlock objects that was locked in the trans
462                 for(i = 0; i< objlocked ; i++) {
463                         mobj = mhashSearch(oidlocked[i]);// find the header address
464                         ((objheader_t *)mobj)->status &= ~(LOCK);               
465                 }       
466         }       
467
468         //Fill out the structure required for a trans commit process if pile receives a TRANS_COMMIT
469         transinfo->objmod = oidmod;
470         transinfo->objlocked = oidlocked;
471         transinfo->objnotfound = oidnotfound;
472         transinfo->modptr = modptr;
473         transinfo->nummod = fixed->nummod;
474         transinfo->numlocked = objlocked;
475         transinfo->numnotfound = objnotfound;
476         
477         return control;
478 }
479
480 //Processes oids in the TRANS_COMMIT request at the participant end and sends an ack back
481 int transCommitProcess(trans_commit_data_t *transinfo, int acceptfd) {
482         objheader_t *header;
483         int i = 0, offset = 0;
484         char control;
485         //Process each modified object saved in the mainobject store
486         for(i=0; i<transinfo->nummod; i++) {
487                 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
488                         printf("mhashserach returns NULL\n");
489                 }
490                 //change reference count of older address and free space in objstr ??
491                 header->rcount = 1; //Not sure what would be th val
492                 //change ptr address in mhash table
493                 mhashRemove(transinfo->objmod[i]);
494                 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
495                 offset += sizeof(objheader_t) + classsize[header->type];
496                 //update object version
497                 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
498                 header->version += 1; 
499         }
500         for(i=0; i<transinfo->numlocked; i++) {
501                 //unlock objects
502                 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
503                 header->status &= ~(LOCK);
504         }
505
506         //TODO Update location lookup table
507
508         //send ack to coordinator
509         control = TRANS_SUCESSFUL;
510         if(send((int)acceptfd, &control, sizeof(char), 0) < 0) {
511                 perror("Error sending ACK to coordinator\n");
512         }
513
514         return 0;
515 }
516