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