Fixed the trans retry bug
[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 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
28
29 /* This function initializes the main objects store and creates the 
30  * global machine and location lookup table */
31
32 int dstmInit(void)
33 {
34         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
35         /* Initialize attribute for mutex */
36         pthread_mutexattr_init(&mainobjstore_mutex_attr);
37         pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
38         pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
39         if (mhashCreate(HASH_SIZE, LOADFACTOR))
40                 return 1; //failure
41         
42         if (lhashCreate(HASH_SIZE, LOADFACTOR))
43                 return 1; //failure
44         
45         return 0;
46 }
47
48 /* This function starts the thread to listen on a socket 
49  * for tranaction calls */
50 void *dstmListen()
51 {
52         int listenfd, acceptfd;
53         struct sockaddr_in my_addr;
54         struct sockaddr_in client_addr;
55         socklen_t addrlength = sizeof(struct sockaddr);
56         pthread_t thread_dstm_accept;
57         int i;
58         int setsockflag=1;
59
60         listenfd = socket(AF_INET, SOCK_STREAM, 0);
61         if (listenfd == -1)
62         {
63                 perror("socket");
64                 exit(1);
65         }
66
67         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
68           perror("socket");
69           exit(1);
70         }
71 #ifdef MAC
72         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
73           perror("socket");
74           exit(1);
75         }
76 #endif
77
78         my_addr.sin_family = AF_INET;
79         my_addr.sin_port = htons(LISTEN_PORT);
80         my_addr.sin_addr.s_addr = INADDR_ANY;
81         memset(&(my_addr.sin_zero), '\0', 8);
82
83         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
84         {
85                 perror("bind");
86                 exit(1);
87         }
88         
89         if (listen(listenfd, BACKLOG) == -1)
90         {
91                 perror("listen");
92                 exit(1);
93         }
94
95         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
96         while(1)
97         {
98           int retval;
99           acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
100           do {
101             retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
102           } while(retval!=0);
103           pthread_detach(thread_dstm_accept);
104         }
105 }
106 /* This function accepts a new connection request, decodes the control message in the connection 
107  * and accordingly calls other functions to process new requests */
108 void *dstmAccept(void *acceptfd)
109 {
110         int numbytes,i, val, retval;
111         unsigned int oid;
112         char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
113         char *ptr;
114         void *srcObj;
115         objheader_t *h;
116         trans_commit_data_t transinfo;
117         unsigned short objType;
118         
119         transinfo.objlocked = NULL;
120         transinfo.objnotfound = NULL;
121         transinfo.modptr = NULL;
122         transinfo.numlocked = 0;
123         transinfo.numnotfound = 0;
124
125         int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
126
127         /* Receive control messages from other machines */
128         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
129                 if (retval == 0) {
130                         pthread_exit(NULL); // Testing connection
131                 }
132                 perror("Error in receiving control from coordinator\n");
133                 pthread_exit(NULL);
134         }
135         
136         switch(control) {
137                 case READ_REQUEST:
138                         /* Read oid requested and search if available */
139                         if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
140                                 perror("Error receiving object from cooridnator\n");
141                                 pthread_exit(NULL);
142                         }
143                         if((srcObj = mhashSearch(oid)) == NULL) {
144                                 printf("Object not found in Main Object Store %s %d\n", __FILE__, __LINE__);
145                         }
146                         h = (objheader_t *) srcObj;
147                         GETSIZE(size, h);
148                         size += sizeof(objheader_t);
149
150                         if (h == NULL) {
151                                 ctrl = OBJECT_NOT_FOUND;
152                                 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
153                                         perror("Error sending control msg to coordinator\n");
154                                         pthread_exit(NULL);
155                                 }
156                         } else {
157                                 /* Type */
158                                 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
159                                 *((int *)&msg[1])=size;
160                                 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
161                                         perror("Error sending size of object to coordinator\n");
162                                         pthread_exit(NULL);
163                                 }
164                                 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
165                                         perror("Error in sending object\n");
166                                         pthread_exit(NULL);
167                                 }
168                         }
169                         break;
170                 
171                 case READ_MULT_REQUEST:
172                         printf("DEBUG-> READ_MULT_REQUEST\n");
173                         break;
174         
175                 case MOVE_REQUEST:
176                         printf("DEBUG -> MOVE_REQUEST\n");
177                         break;
178
179                 case MOVE_MULT_REQUEST:
180                         printf("DEBUG -> MOVE_MULT_REQUEST\n");
181                         break;
182
183                 case TRANS_REQUEST:
184                         /* Read transaction request */
185                         printf("DEBUG -> Recv TRANS_REQUEST\n");
186                         if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
187                                 printf("Error in readClientReq\n");
188                                 pthread_exit(NULL);
189                         }
190                         break;
191                 case TRANS_PREFETCH:
192                         printf("DEBUG -> Recv TRANS_PREFETCH\n");
193                         if((val = prefetchReq((int)acceptfd)) != 0) {
194                                 printf("Error in readClientReq\n");
195                                 pthread_exit(NULL);
196                         }
197                         break;
198                 case START_REMOTE_THREAD:
199                         retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
200                         if (retval <= 0)
201                                 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
202                         else if (retval != sizeof(unsigned int))
203                                 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
204                                         retval);
205                         else
206                         { //TODO: execute run method on this global thread object
207                                 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
208                                 objType = getObjType(oid);
209                                 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
210                                 startDSMthread(oid, objType);
211
212
213                         }
214                         break;
215
216                 default:
217                         printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
218         }
219
220         /* Close connection */
221         if (close((int)acceptfd) == -1)
222                 perror("close");
223         
224         pthread_exit(NULL);
225 }
226
227 /* This function reads the information available in a transaction request
228  * and makes a function call to process the request */
229 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
230         char *ptr;
231         void *modptr;
232         unsigned int *oidmod, oid;
233         fixed_data_t fixed;
234         objheader_t *headaddr;
235         int sum = 0, i, N, n, val;
236
237         oidmod = NULL;
238
239         /* Read fixed_data_t data structure */ 
240         N = sizeof(fixed) - 1;
241         ptr = (char *)&fixed;;
242         fixed.control = TRANS_REQUEST;
243         do {
244                 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
245                 sum += n;
246         } while(sum < N && n != 0); 
247
248         /* Read list of mids */
249         int mcount = fixed.mcount;
250         N = mcount * sizeof(unsigned int);
251         unsigned int listmid[mcount];
252         ptr = (char *) listmid;
253         sum = 0;
254         do {
255                 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
256                 sum += n;
257         } while(sum < N && n != 0);
258
259         /* Read oid and version tuples for those objects that are not modified in the transaction */
260         int numread = fixed.numread;
261         N = numread * (sizeof(unsigned int) + sizeof(short));
262         char objread[N];
263         if(numread != 0) { //If pile contains more than one object to be read, 
264                           // keep reading all objects
265                 sum = 0;
266                 do {
267                         n = recv((int)acceptfd, (void *) objread, N, 0);
268                         sum += n;
269                 } while(sum < N && n != 0);
270         }
271         
272         /* Read modified objects */
273         if(fixed.nummod != 0) { // If pile contains more than one modified object,
274                                 // allocate new object store and recv all modified objects
275                                 // TODO deallocate this space
276                 pthread_mutex_lock(&mainobjstore_mutex);
277                 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
278                         printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
279                         pthread_mutex_unlock(&mainobjstore_mutex);
280                         return 1;
281                 }
282                 pthread_mutex_unlock(&mainobjstore_mutex);
283                 sum = 0;
284                 do { // Recv the objs that are modified by the Coordinator
285                         n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
286                         sum += n;
287                 } while (sum < fixed.sum_bytes && n != 0);
288         }
289
290         /* Create an array of oids for modified objects */
291         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
292         if (oidmod == NULL)
293         {
294                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
295                 return 1;
296         }
297         ptr = (char *) modptr;
298         for(i = 0 ; i < fixed.nummod; i++) {
299           int tmpsize;
300           headaddr = (objheader_t *) ptr;
301           oid = OID(headaddr);
302           oidmod[i] = oid;
303           GETSIZE(tmpsize, headaddr);
304           ptr += sizeof(objheader_t) + tmpsize;
305         }
306         
307         /*Process the information read */
308         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
309                 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
310                 /* Free resources */
311                 if(oidmod != NULL) {
312                         free(oidmod);
313                         oidmod = NULL;
314                 }
315                 return 1;
316         }
317
318         /* Free resources */
319         if(oidmod != NULL) {
320                 free(oidmod);
321                 oidmod = NULL;
322         }
323
324         return 0;
325 }
326
327 /* This function processes the Coordinator's transaction request using "handleTransReq" 
328  * function and sends a reply to the co-ordinator.
329  * Following this it also receives a new control message from the co-ordinator and processes this message*/
330 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
331                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
332         char *ptr, control, sendctrl;
333         objheader_t *tmp_header;
334         void *header;
335         int  i = 0, val, retval;
336
337         /* Send reply to the Coordinator */
338         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
339                 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
340                 return 1;
341         }
342
343         /* Read new control message from Coordiator */
344         if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
345                 perror("Error in receiving control message\n");
346                 return 1;
347         }
348
349         /* Process the new control message */
350         switch(control) {
351                 case TRANS_ABORT:
352                         /* Set all ref counts as 1 and do garbage collection */
353                         ptr = modptr;
354                         for(i = 0; i< fixed->nummod; i++) {
355                           int tmpsize;
356                           tmp_header = (objheader_t *)ptr;
357                           tmp_header->rcount = 0;
358                           GETSIZE(tmpsize, tmp_header);
359                           ptr += sizeof(objheader_t) + tmpsize;
360                         }
361                         /* Unlock objects that was locked due to this transaction */
362                         for(i = 0; i< transinfo->numlocked; i++) {
363                                 header = mhashSearch(transinfo->objlocked[i]);// find the header address
364                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
365                         }
366
367                         /* Send ack to Coordinator */
368                         sendctrl = TRANS_SUCESSFUL;
369                         if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
370                                 perror("Error sending ACK to coordinator\n");
371                                 if (transinfo->objlocked != NULL) {
372                                         free(transinfo->objlocked);
373                                 }
374                                 if (transinfo->objnotfound != NULL) {
375                                         free(transinfo->objnotfound);
376                                 }
377
378                                 return 1;
379                         }
380                         ptr = NULL;
381                         break;
382
383                 case TRANS_COMMIT:
384                         /* Invoke the transCommit process() */
385                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
386                                 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
387                                 /* Free memory */
388                                 printf("DEBUG -> Freeing...\n");
389                                 fflush(stdout);
390                                 if (transinfo->objlocked != NULL) {
391                                         free(transinfo->objlocked);
392                                 }
393                                 if (transinfo->objnotfound != NULL) {
394                                         free(transinfo->objnotfound);
395                                 }
396                                 return 1;
397                         }
398                         break;
399
400                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
401                         //TODO expect another transrequest from client
402                         printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
403                         break;
404                 default:
405                         printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
406                         //TODO Use fixed.trans_id  TID since Client may have died
407                         break;
408         }
409
410         /* Free memory */
411         printf("DEBUG -> Freeing...\n");
412         fflush(stdout);
413
414         if (transinfo->objlocked != NULL) {
415                 free(transinfo->objlocked);
416         }
417         if (transinfo->objnotfound != NULL) {
418                 free(transinfo->objnotfound);
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 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                                         return control;
483                                 }
484                         } else {/* If Obj is not locked then lock object */
485                                 STATUS(((objheader_t *)mobj)) |= LOCK;
486                                
487                                 /*TESTING Add random wait to make transactions run for a long time such that
488                                  * we can test for soft abort case */
489                         
490                                 //randomdelay();
491
492                                 /* Save all object oids that are locked on this machine during this transaction request call */
493                                 oidlocked[objlocked] = OID(((objheader_t *)mobj));
494                                 objlocked++;
495                                 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
496                                         v_matchnolock++;
497                                 } else { /* If versions don't match ...HARD ABORT */
498                                         v_nomatch++;
499                                         control = TRANS_DISAGREE;
500                                         /* Send TRANS_DISAGREE to Coordinator */
501                                         if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
502                                                 perror("Error in sending control to the Coordinator\n");
503                                                 return 0;
504                                         }
505                                         if (objlocked > 0) {
506                                                 STATUS(((objheader_t *)mobj)) &= ~(LOCK);
507                                                 free(oidlocked);
508                                         }
509                                         return control;
510                                 }
511                         }
512                 }
513         }
514         
515         /* Decide what control message to send to Coordinator */
516         if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
517                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
518                 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
519                 return 0;
520         }
521         
522         return val;
523
524 }
525 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
526  * to send to Coordinator based on the votes of oids involved in the transaction */
527 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
528                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
529                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
530         int val;
531         char control = 0;
532         /* Condition to send TRANS_AGREE */
533         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
534                 control = TRANS_AGREE;
535                 /* Send control message */
536                 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
537                         perror("Error in sending control to Coordinator\n");
538                         return 0;
539                 }
540         }
541         /* Condition to send TRANS_SOFT_ABORT */
542         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
543                 control = TRANS_SOFT_ABORT;
544                 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
545                 *((int*)&msg[1])= *(objnotfound);
546
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         if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
616                 perror("Error sending ACK to coordinator\n");
617         }
618         
619         return 0;
620 }
621
622 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
623  * Looks for the objects to be prefetched in the main object store.
624  * If objects are not found then record those and if objects are found
625  * then use offset values to prefetch references to other objects */
626
627 int prefetchReq(int acceptfd) {
628   int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
629   unsigned int oid, index = 0;
630   char *ptr, buffer[PRE_BUF_SIZE];
631   void *mobj;
632   unsigned int objoid;
633   char control;
634   objheader_t * header;
635   int bytesRecvd;
636   
637   /* Repeatedly recv the oid and offset pairs sent for prefetch */
638   while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
639     count++;
640     if(length == -1)
641       break;
642     sum = 0;
643     index = sizeof(unsigned int); // Index starts with sizeof  unsigned int because the 
644     // first 4 bytes are saved to send the
645     // size of the buffer (that is computed at the end of the loop)
646     bytesRecvd = 0;
647     do {
648       bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
649                          sizeof(unsigned int) - bytesRecvd, 0);
650     } while (bytesRecvd < sizeof(unsigned int));
651     numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
652     N = numoffset * sizeof(short);
653     short offset[numoffset];
654     ptr = (char *)&offset;
655     /* Recv the offset values per oid */ 
656     do {
657       n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0); 
658       sum += n; 
659     } while(sum < N && n != 0); 
660     
661     /* Process each oid */
662     if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
663       /* Save the oids not found in buffer for later use */
664       *(buffer + index) = OBJECT_NOT_FOUND;
665       index += sizeof(char);
666       memcpy(buffer+index, &oid, sizeof(unsigned int));
667       index += sizeof(unsigned int);
668     } else { /* If Obj found in machine (i.e. has not moved) */
669       /* send the oid, it's size, it's header and data */
670       header = mobj;
671       GETSIZE(size, header);
672       size += sizeof(objheader_t);
673       *(buffer + index) = OBJECT_FOUND;
674       index += sizeof(char);
675       memcpy(buffer+index, &oid, sizeof(unsigned int));
676       index += sizeof(unsigned int);
677       memcpy(buffer+index, &size, sizeof(int));
678       index += sizeof(int);
679       memcpy(buffer + index, header, size);
680       index += size;
681       /* Calculate the oid corresponding to the offset value */
682       for(i = 0 ; i< numoffset ; i++) {
683         objoid = *((int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
684         if((header = mhashSearch(objoid)) == NULL) {
685           /* Obj not found, send oid */
686           *(buffer + index) = OBJECT_NOT_FOUND;
687           index += sizeof(char);
688           memcpy(buffer+index, &oid, sizeof(unsigned int));
689           index += sizeof(unsigned int);
690           break;
691         } else {/* Obj Found */
692           /* send the oid, it's size, it's header and data */
693           GETSIZE(size, header);
694           size+=sizeof(objheader_t);
695           *(buffer + index) = OBJECT_FOUND;
696           index += sizeof(char);
697           memcpy(buffer+index, &oid, sizeof(unsigned int));
698           index += sizeof(unsigned int);
699           memcpy(buffer+index, &size, sizeof(int));
700           index += sizeof(int);
701           memcpy(buffer + index, header, size);
702           index += size;
703           continue;
704         }
705       }
706     }
707     /* Check for overflow in the buffer */
708     if (index >= PRE_BUF_SIZE) {
709       printf("Char buffer is overflowing\n");
710       return 1;
711     }
712     /* Send Prefetch response control message only once*/
713     if(count == 1) {
714       control = TRANS_PREFETCH_RESPONSE;
715       if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
716         perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
717         return 1;
718       }
719     }
720     
721     /* Add the buffer size into buffer as a parameter */
722     *((unsigned int *)buffer)=index;
723     /* Send the entire buffer with its size and oids found and not found */
724     if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
725       perror("Error sending oids found\n");
726       return 1;
727     }
728   }
729   return 0;
730 }
731