various bug fixes.
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstmserver.c
1 /* Coordinator => Machine that initiates the transaction request call for commiting a transaction
2  * Participant => Machines that host the objects involved in a transaction commit */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <pthread.h>
8 #include <netdb.h>
9 #include <fcntl.h>
10 #include "dstm.h"
11 #include "mlookup.h"
12 #include "llookup.h"
13 #ifdef COMPILER
14 #include "thread.h"
15 #endif
16
17
18 #define LISTEN_PORT 2156
19 #define BACKLOG 10 //max pending connections
20 #define RECEIVE_BUFFER_SIZE 2048
21 #define PRE_BUF_SIZE 2048
22
23 extern int classsize[];
24
25 objstr_t *mainobjstore;
26 pthread_mutex_t mainobjstore_mutex;
27
28 /* This function initializes the main objects store and creates the 
29  * global machine and location lookup table */
30
31 int dstmInit(void)
32 {
33         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
34         pthread_mutex_init(&mainobjstore_mutex, NULL);
35         if (mhashCreate(HASH_SIZE, LOADFACTOR))
36                 return 1; //failure
37         
38         if (lhashCreate(HASH_SIZE, LOADFACTOR))
39                 return 1; //failure
40         
41         return 0;
42 }
43
44 /* This function starts the thread to listen on a socket 
45  * for tranaction calls */
46 void *dstmListen()
47 {
48         int listenfd, acceptfd;
49         struct sockaddr_in my_addr;
50         struct sockaddr_in client_addr;
51         socklen_t addrlength = sizeof(struct sockaddr);
52         pthread_t thread_dstm_accept;
53         int i;
54         int setsockflag=1;
55
56         listenfd = socket(AF_INET, SOCK_STREAM, 0);
57         if (listenfd == -1)
58         {
59                 perror("socket");
60                 exit(1);
61         }
62
63         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
64           perror("socket");
65           exit(1);
66         }
67 #ifdef MAC
68         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
69           perror("socket");
70           exit(1);
71         }
72 #endif
73
74         my_addr.sin_family = AF_INET;
75         my_addr.sin_port = htons(LISTEN_PORT);
76         my_addr.sin_addr.s_addr = INADDR_ANY;
77         memset(&(my_addr.sin_zero), '\0', 8);
78
79         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
80         {
81                 perror("bind");
82                 exit(1);
83         }
84         
85         if (listen(listenfd, BACKLOG) == -1)
86         {
87                 perror("listen");
88                 exit(1);
89         }
90
91         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
92         while(1)
93         {
94                 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
95                 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
96         }
97 }
98 /* This function accepts a new connection request, decodes the control message in the connection 
99  * and accordingly calls other functions to process new requests */
100 void *dstmAccept(void *acceptfd)
101 {
102         int numbytes,i, val, retval;
103         unsigned int oid;
104         char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
105         char *ptr;
106         void *srcObj;
107         objheader_t *h;
108         trans_commit_data_t transinfo;
109         unsigned short objType;
110         
111         transinfo.objlocked = NULL;
112         transinfo.objnotfound = NULL;
113         transinfo.modptr = NULL;
114         transinfo.numlocked = 0;
115         transinfo.numnotfound = 0;
116
117         int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
118
119         /* Receive control messages from other machines */
120         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
121                 if (retval == 0) {
122                         pthread_exit(NULL); // Testing connection
123                 }
124                 perror("Error in receiving control from coordinator\n");
125                 pthread_exit(NULL);
126         }
127         
128         switch(control) {
129                 case READ_REQUEST:
130                         /* Read oid requested and search if available */
131                         if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
132                                 perror("Error receiving object from cooridnator\n");
133                                 pthread_exit(NULL);
134                         }
135                         if((srcObj = mhashSearch(oid)) == NULL) {
136                                 printf("Object not found in Main Object Store %s %d\n", __FILE__, __LINE__);
137                         }
138                         h = (objheader_t *) srcObj;
139                         GETSIZE(size, h);
140                         size += sizeof(objheader_t);
141
142                         if (h == NULL) {
143                                 ctrl = OBJECT_NOT_FOUND;
144                                 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
145                                         perror("Error sending control msg to coordinator\n");
146                                         pthread_exit(NULL);
147                                 }
148                         } else {
149                                 /* Type */
150                                 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
151                                 *((int *)&msg[1])=size;
152                                 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
153                                         perror("Error sending size of object to coordinator\n");
154                                         pthread_exit(NULL);
155                                 }
156                                 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
157                                         perror("Error in sending object\n");
158                                         pthread_exit(NULL);
159                                 }
160                         }
161                         break;
162                 
163                 case READ_MULT_REQUEST:
164                         printf("DEBUG-> READ_MULT_REQUEST\n");
165                         break;
166         
167                 case MOVE_REQUEST:
168                         printf("DEBUG -> MOVE_REQUEST\n");
169                         break;
170
171                 case MOVE_MULT_REQUEST:
172                         printf("DEBUG -> MOVE_MULT_REQUEST\n");
173                         break;
174
175                 case TRANS_REQUEST:
176                         /* Read transaction request */
177                         printf("DEBUG -> Recv TRANS_REQUEST\n");
178                         if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
179                                 printf("Error in readClientReq\n");
180                                 pthread_exit(NULL);
181                         }
182                         break;
183                 case TRANS_PREFETCH:
184                         printf("DEBUG -> Recv TRANS_PREFETCH\n");
185                         if((val = prefetchReq((int)acceptfd)) != 0) {
186                                 printf("Error in readClientReq\n");
187                                 pthread_exit(NULL);
188                         }
189                         break;
190                 case START_REMOTE_THREAD:
191                         retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
192                         if (retval <= 0)
193                                 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
194                         else if (retval != sizeof(unsigned int))
195                                 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
196                                         retval);
197                         else
198                         { //TODO: execute run method on this global thread object
199                                 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
200                                 objType = getObjType(oid);
201                                 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
202                                 startDSMthread(oid, objType);
203
204
205                         }
206                         break;
207
208                 default:
209                         printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
210         }
211
212         /* Close connection */
213         if (close((int)acceptfd) == -1)
214                 perror("close");
215         
216         pthread_exit(NULL);
217 }
218
219 /* This function reads the information available in a transaction request
220  * and makes a function call to process the request */
221 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
222         char *ptr;
223         void *modptr;
224         unsigned int *oidmod, oid;
225         fixed_data_t fixed;
226         objheader_t *headaddr;
227         int sum = 0, i, N, n, val;
228
229         oidmod = NULL;
230
231         /* Read fixed_data_t data structure */ 
232         N = sizeof(fixed) - 1;
233         ptr = (char *)&fixed;;
234         fixed.control = TRANS_REQUEST;
235         do {
236                 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
237                 sum += n;
238         } while(sum < N && n != 0); 
239
240         /* Read list of mids */
241         int mcount = fixed.mcount;
242         N = mcount * sizeof(unsigned int);
243         unsigned int listmid[mcount];
244         ptr = (char *) listmid;
245         sum = 0;
246         do {
247                 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
248                 sum += n;
249         } while(sum < N && n != 0);
250
251         /* Read oid and version tuples for those objects that are not modified in the transaction */
252         int numread = fixed.numread;
253         N = numread * (sizeof(unsigned int) + sizeof(short));
254         char objread[N];
255         if(numread != 0) { //If pile contains more than one object to be read, 
256                           // keep reading all objects
257                 sum = 0;
258                 do {
259                         n = recv((int)acceptfd, (void *) objread, N, 0);
260                         sum += n;
261                 } while(sum < N && n != 0);
262         }
263         
264         /* Read modified objects */
265         if(fixed.nummod != 0) { // If pile contains more than one modified object,
266                                 // allocate new object store and recv all modified objects
267                                 // TODO deallocate this space
268                 pthread_mutex_lock(&mainobjstore_mutex);
269                 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
270                         printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
271                         pthread_mutex_unlock(&mainobjstore_mutex);
272                         return 1;
273                 }
274                 pthread_mutex_unlock(&mainobjstore_mutex);
275                 sum = 0;
276                 do { // Recv the objs that are modified by the Coordinator
277                         n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
278                         sum += n;
279                 } while (sum < fixed.sum_bytes && n != 0);
280         }
281
282         /* Create an array of oids for modified objects */
283         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
284         if (oidmod == NULL)
285         {
286                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
287                 return 1;
288         }
289         ptr = (char *) modptr;
290         for(i = 0 ; i < fixed.nummod; i++) {
291           int tmpsize;
292           headaddr = (objheader_t *) ptr;
293           oid = OID(headaddr);
294           oidmod[i] = oid;
295           GETSIZE(tmpsize, headaddr);
296           ptr += sizeof(objheader_t) + tmpsize;
297         }
298         
299         /*Process the information read */
300         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
301                 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
302                 /* Free resources */
303                 if(oidmod != NULL) {
304                         free(oidmod);
305                         oidmod = NULL;
306                 }
307                 return 1;
308         }
309
310         /* Free resources */
311         if(oidmod != NULL) {
312                 free(oidmod);
313                 oidmod = NULL;
314         }
315
316         return 0;
317 }
318
319 /* This function processes the Coordinator's transaction request using "handleTransReq" 
320  * function and sends a reply to the co-ordinator.
321  * Following this it also receives a new control message from the co-ordinator and processes this message*/
322 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
323                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
324         char *ptr, control, sendctrl;
325         objheader_t *tmp_header;
326         void *header;
327         int  i = 0, val, retval;
328
329         /* Send reply to the Coordinator */
330         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
331                 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
332                 return 1;
333         }
334
335         /* Read new control message from Coordiator */
336         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
337                 perror("Error in receiving control message\n");
338                 return 1;
339         }
340
341         /* Process the new control message */
342         switch(control) {
343                 case TRANS_ABORT:
344                         /* Set all ref counts as 1 and do garbage collection */
345                         ptr = modptr;
346                         for(i = 0; i< fixed->nummod; i++) {
347                           int tmpsize;
348                           tmp_header = (objheader_t *)ptr;
349                           tmp_header->rcount = 0;
350                           GETSIZE(tmpsize, tmp_header);
351                           ptr += sizeof(objheader_t) + tmpsize;
352                         }
353                         /* Unlock objects that was locked due to this transaction */
354                         for(i = 0; i< transinfo->numlocked; i++) {
355                                 header = mhashSearch(transinfo->objlocked[i]);// find the header address
356                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
357                         }
358
359                         /* Send ack to Coordinator */
360                         printf("DEBUG -> Recv TRANS_ABORT\n");
361                         sendctrl = TRANS_SUCESSFUL;
362                         if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
363                                 perror("Error sending ACK to coordinator\n");
364                                 if (transinfo->objlocked != NULL) {
365                                         free(transinfo->objlocked);
366                                         transinfo->objlocked = NULL;
367                                 }
368                                 if (transinfo->objnotfound != NULL) {
369                                         free(transinfo->objnotfound);
370                                         transinfo->objnotfound = NULL;
371                                 }
372
373                                 return 1;
374                         }
375                         ptr = NULL;
376                         break;
377
378                 case TRANS_COMMIT:
379                         /* Invoke the transCommit process() */
380                         printf("DEBUG -> Recv TRANS_COMMIT \n");
381                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
382                                 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
383                                 /* Free memory */
384                                 printf("DEBUG -> Freeing...\n");
385                                 fflush(stdout);
386                                 if (transinfo->objlocked != NULL) {
387                                         free(transinfo->objlocked);
388                                         transinfo->objlocked = NULL;
389                                 }
390                                 if (transinfo->objnotfound != NULL) {
391                                         free(transinfo->objnotfound);
392                                         transinfo->objnotfound = NULL;
393                                 }
394                                 return 1;
395                         }
396                         break;
397
398                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
399                         //TODO expect another transrequest from client
400                         printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
401                         break;
402                 default:
403                         printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
404                         //TODO Use fixed.trans_id  TID since Client may have died
405                         break;
406         }
407
408         /* Free memory */
409         printf("DEBUG -> Freeing...\n");
410         fflush(stdout);
411
412         if (transinfo->objlocked != NULL) {
413                 free(transinfo->objlocked);
414                 transinfo->objlocked = NULL;
415         }
416         if (transinfo->objnotfound != NULL) {
417                 free(transinfo->objnotfound);
418                 transinfo->objnotfound = NULL;
419         }
420
421         return 0;
422 }
423
424 /* This function increments counters while running a voting decision on all objects involved 
425  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
426 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
427         int val, i = 0;
428         short version;
429         char control = 0, *ptr;
430         unsigned int oid;
431         unsigned int *oidnotfound, *oidlocked;
432         void *mobj;
433         objheader_t *headptr;
434
435         /* Counters and arrays to formulate decision on control message to be sent */
436         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
437         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
438         int objnotfound = 0, objlocked = 0;
439         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
440
441         /* modptr points to the beginning of the object store 
442          * created at the Pariticipant. 
443          * Object store holds the modified objects involved in the transaction request */ 
444         ptr = (char *) modptr;
445         
446         /* Process each oid in the machine pile/ group per thread */
447         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
448                 if (i < fixed->numread) {//Objs only read and not modified
449                         int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
450                         incr *= i;
451                         oid = *((unsigned int *)(objread + incr));
452                         incr += sizeof(unsigned int);
453                         version = *((short *)(objread + incr));
454                 } else {//Objs modified
455                   int tmpsize;
456                   headptr = (objheader_t *) ptr;
457                   oid = OID(headptr);
458                   version = headptr->version;
459                   GETSIZE(tmpsize, headptr);
460                   ptr += sizeof(objheader_t) + tmpsize;
461                 }
462                 
463                 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
464
465                 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
466                         /* Save the oids not found and number of oids not found for later use */
467                         oidnotfound[objnotfound] = oid;
468                         objnotfound++;
469                 } else { /* If Obj found in machine (i.e. has not moved) */
470                         /* Check if Obj is locked by any previous transaction */
471                         if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {           
472                                 if (version == ((objheader_t *)mobj)->version) {      /* If not locked then match versions */
473                                         v_matchlock++;
474                                 } else {/* If versions don't match ...HARD ABORT */
475                                         v_nomatch++;
476                                         /* Send TRANS_DISAGREE to Coordinator */
477                                         control = TRANS_DISAGREE;
478                                         if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
479                                                 perror("Error in sending control to the Coordinator\n");
480                                                 return 0;
481                                         }
482                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
483                                         return control;
484                                 }
485                         } else {/* If Obj is not locked then lock object */
486                                 STATUS(((objheader_t *)mobj)) |= LOCK;
487                                
488                                 /*TESTING Add random wait to make transactions run for a long time such that
489                                  * we can test for soft abort case */
490                         
491                                 //randomdelay();
492
493                                 /* Save all object oids that are locked on this machine during this transaction request call */
494                                 oidlocked[objlocked] = OID(((objheader_t *)mobj));
495                                 objlocked++;
496                                 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
497                                         v_matchnolock++;
498                                 } else { /* If versions don't match ...HARD ABORT */
499                                         v_nomatch++;
500                                         control = TRANS_DISAGREE;
501                                         /* Send TRANS_DISAGREE to Coordinator */
502                                         if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
503                                                 perror("Error in sending control to the Coordinator\n");
504                                                 return 0;
505                                         }
506                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
507                                         return control;
508                                 }
509                         }
510                 }
511         }
512         
513         /* Decide what control message to send to Coordinator */
514         if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
515                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
516                 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
517                 return 0;
518         }
519         
520         return val;
521
522 }
523 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
524  * to send to Coordinator based on the votes of oids involved in the transaction */
525 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
526                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
527                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
528         int val;
529         char control = 0;
530         /* Condition to send TRANS_AGREE */
531         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
532                 control = TRANS_AGREE;
533                 /* Send control message */
534                 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
535                         perror("Error in sending control to Coordinator\n");
536                         return 0;
537                 }
538                 printf("DEBUG -> Sending TRANS_AGREE\n");
539         }
540         /* Condition to send TRANS_SOFT_ABORT */
541         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
542                 control = TRANS_SOFT_ABORT;
543                 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
544                 *((int*)&msg[1])= *(objnotfound);
545
546                 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
547                 /* Send control message */
548                 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
549                         perror("Error in sending no of objects that are not found\n");
550                         return 0;
551                 }
552                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
553                 if(*(objnotfound) != 0) { 
554                         int size = sizeof(unsigned int)* *(objnotfound);
555                         if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
556                                 perror("Error in sending objects that are not found\n");
557                                 return 0;
558                         }
559                 }
560         }
561
562         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
563          * if Participant receives a TRANS_COMMIT */
564         transinfo->objlocked = oidlocked;
565         transinfo->objnotfound = oidnotfound;
566         transinfo->modptr = modptr;
567         transinfo->numlocked = *(objlocked);
568         transinfo->numnotfound = *(objnotfound);
569         
570         return control;
571 }
572
573 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer 
574  * addresses in lookup table and also changes version number
575  * Sends an ACK back to Coordinator */
576 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
577         objheader_t *header;
578         int i = 0, offset = 0;
579         char control;
580
581         /* Process each modified object saved in the mainobject store */
582         for(i = 0; i < nummod; i++) {
583                 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
584                         printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
585                         return 1;
586                 }
587                 /* Change reference count of older address and free space in objstr ?? */
588                 header->rcount = 0;
589
590                 /* Change ptr address in mhash table */
591                 mhashRemove(oidmod[i]);
592                 mhashInsert(oidmod[i], (((char *)modptr) + offset));
593                 {
594                   int tmpsize;
595                   GETSIZE(tmpsize,header);
596                   offset += sizeof(objheader_t) + tmpsize;
597                 }
598                 /* Update object version number */
599                 header = (objheader_t *) mhashSearch(oidmod[i]);
600                 header->version += 1; 
601         }
602
603         /* Unlock locked objects */
604         for(i = 0; i < numlocked; i++) {
605                 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
606                         printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
607                         return 1;
608                 }
609                 STATUS(header) &= ~(LOCK);
610         }
611         //TODO Update location lookup table
612
613         /* Send ack to coordinator */
614         control = TRANS_SUCESSFUL;
615         printf("DEBUG-> TRANS_SUCESSFUL\n");
616         if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
617                 perror("Error sending ACK to coordinator\n");
618         }
619         
620         return 0;
621 }
622
623 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
624  * Looks for the objects to be prefetched in the main object store.
625  * If objects are not found then record those and if objects are found
626  * then use offset values to prefetch references to other objects */
627
628 int prefetchReq(int acceptfd) {
629   int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
630   unsigned int oid, index = 0;
631   char *ptr, buffer[PRE_BUF_SIZE];
632   void *mobj;
633   unsigned int objoid;
634   char control;
635   objheader_t * header;
636   int bytesRecvd;
637   
638   /* Repeatedly recv the oid and offset pairs sent for prefetch */
639   while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
640     count++;
641     if(length == -1)
642       break;
643     sum = 0;
644     index = sizeof(unsigned int); // Index starts with sizeof  unsigned int because the 
645     // first 4 bytes are saved to send the
646     // size of the buffer (that is computed at the end of the loop)
647     bytesRecvd = 0;
648     do {
649       bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
650                          sizeof(unsigned int) - bytesRecvd, 0);
651     } while (bytesRecvd < sizeof(unsigned int));
652     numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
653     N = numoffset * sizeof(short);
654     short offset[numoffset];
655     ptr = (char *)&offset;
656     /* Recv the offset values per oid */ 
657     do {
658       n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0); 
659       sum += n; 
660     } while(sum < N && n != 0); 
661     
662     /* Process each oid */
663     if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
664       /* Save the oids not found in buffer for later use */
665       *(buffer + index) = OBJECT_NOT_FOUND;
666       index += sizeof(char);
667       memcpy(buffer+index, &oid, sizeof(unsigned int));
668       index += sizeof(unsigned int);
669     } else { /* If Obj found in machine (i.e. has not moved) */
670       /* send the oid, it's size, it's header and data */
671       header = mobj;
672       GETSIZE(size, header);
673       size += sizeof(objheader_t);
674       *(buffer + index) = OBJECT_FOUND;
675       index += sizeof(char);
676       memcpy(buffer+index, &oid, sizeof(unsigned int));
677       index += sizeof(unsigned int);
678       memcpy(buffer+index, &size, sizeof(int));
679       index += sizeof(int);
680       memcpy(buffer + index, header, size);
681       index += size;
682       /* Calculate the oid corresponding to the offset value */
683       for(i = 0 ; i< numoffset ; i++) {
684         objoid = *((int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
685         if((header = mhashSearch(objoid)) == NULL) {
686           /* Obj not found, send oid */
687           *(buffer + index) = OBJECT_NOT_FOUND;
688           index += sizeof(char);
689           memcpy(buffer+index, &oid, sizeof(unsigned int));
690           index += sizeof(unsigned int);
691           break;
692         } else {/* Obj Found */
693           /* send the oid, it's size, it's header and data */
694           GETSIZE(size, header);
695           size+=sizeof(objheader_t);
696           *(buffer + index) = OBJECT_FOUND;
697           index += sizeof(char);
698           memcpy(buffer+index, &oid, sizeof(unsigned int));
699           index += sizeof(unsigned int);
700           memcpy(buffer+index, &size, sizeof(int));
701           index += sizeof(int);
702           memcpy(buffer + index, header, size);
703           index += size;
704           continue;
705         }
706       }
707     }
708     /* Check for overflow in the buffer */
709     if (index >= PRE_BUF_SIZE) {
710       printf("Char buffer is overflowing\n");
711       return 1;
712     }
713     /* Send Prefetch response control message only once*/
714     if(count == 1) {
715       control = TRANS_PREFETCH_RESPONSE;
716       if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
717         perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
718         return 1;
719       }
720     }
721     
722     /* Add the buffer size into buffer as a parameter */
723     *((unsigned int *)buffer)=index;
724     /* Send the entire buffer with its size and oids found and not found */
725     if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
726       perror("Error sending oids found\n");
727       return 1;
728     }
729   }
730   return 0;
731 }
732