hacks to speed up prefetching...doesn't really help though..
[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 <netinet/tcp.h>
5 #include "dstm.h"
6 #include "mlookup.h"
7 #include "llookup.h"
8 #include "threadnotify.h"
9 #ifdef COMPILER
10 #include "thread.h"
11 #endif
12
13 #define BACKLOG 10 //max pending connections
14 #define RECEIVE_BUFFER_SIZE 2048
15
16 extern int classsize[];
17 extern int numHostsInSystem;
18 extern pthread_mutex_t notifymutex;
19
20 objstr_t *mainobjstore;
21 pthread_mutex_t mainobjstore_mutex;
22 pthread_mutex_t lockObjHeader;
23 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
24
25 sockPoolHashTable_t *transPResponseSocketPool;
26
27 /* This function initializes the main objects store and creates the 
28  * global machine and location lookup table */
29
30 int dstmInit(void)
31 {
32         mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
33         /* Initialize attribute for mutex */
34         pthread_mutexattr_init(&mainobjstore_mutex_attr);
35         pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
36         pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
37         pthread_mutex_init(&lockObjHeader,NULL);
38         if (mhashCreate(HASH_SIZE, LOADFACTOR))
39                 return 1; //failure
40         
41         if (lhashCreate(HASH_SIZE, LOADFACTOR))
42                 return 1; //failure
43
44         if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
45                 return 1; //failure
46
47     //Initialize socket pool
48     if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, 2*numHostsInSystem+1)) == NULL) {
49         printf("Error in creating new socket pool at  %s line %d\n", __FILE__, __LINE__);
50         return 0;
51     }
52
53         return 0;
54 }
55
56 /* This function starts the thread to listen on a socket 
57  * for tranaction calls */
58 void *dstmListen()
59 {
60         int listenfd, acceptfd;
61         struct sockaddr_in my_addr;
62         struct sockaddr_in client_addr;
63         socklen_t addrlength = sizeof(struct sockaddr);
64         pthread_t thread_dstm_accept;
65         int i;
66         int setsockflag=1;
67
68         listenfd = socket(AF_INET, SOCK_STREAM, 0);
69         if (listenfd == -1)
70         {
71                 perror("socket");
72                 exit(1);
73         }
74
75         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
76           perror("socket");
77           exit(1);
78         }
79 #ifdef MAC
80         if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
81           perror("socket");
82           exit(1);
83         }
84 #endif
85
86         my_addr.sin_family = AF_INET;
87         my_addr.sin_port = htons(LISTEN_PORT);
88         my_addr.sin_addr.s_addr = INADDR_ANY;
89         memset(&(my_addr.sin_zero), '\0', 8);
90
91         if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
92         {
93                 perror("bind");
94                 exit(1);
95         }
96         
97         if (listen(listenfd, BACKLOG) == -1)
98         {
99                 perror("listen");
100                 exit(1);
101         }
102
103         printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
104         while(1)
105         {
106           int retval;
107           int flag=1;
108           acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
109           setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
110           do {
111             retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
112           } while(retval!=0);
113           pthread_detach(thread_dstm_accept);
114         }
115 }
116 /* This function accepts a new connection request, decodes the control message in the connection 
117  * and accordingly calls other functions to process new requests */
118 void *dstmAccept(void *acceptfd) {
119   int val, retval, size, sum, sockid;
120   unsigned int oid;
121   char *buffer;
122   char control,ctrl;
123   char *ptr;
124   void *srcObj;
125   objheader_t *h;
126   trans_commit_data_t transinfo;
127   unsigned short objType, *versionarry, version;
128   unsigned int *oidarry, numoid, mid, threadid;
129   
130   /* Receive control messages from other machines */
131   while(1) {
132     int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
133     if (ret==0)
134       return;
135     if (ret==-1) {
136       printf("DEBUG -> RECV Error!.. retrying\n");
137       break;
138     }
139     switch(control) {
140     case READ_REQUEST:
141       /* Read oid requested and search if available */
142       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
143       if((srcObj = mhashSearch(oid)) == NULL) {
144         printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
145         break;
146       }
147       h = (objheader_t *) srcObj;
148       GETSIZE(size, h);
149       size += sizeof(objheader_t);
150       sockid = (int) acceptfd;
151       
152       if (h == NULL) {
153         ctrl = OBJECT_NOT_FOUND;
154         send_data(sockid, &ctrl, sizeof(char));
155       } else {
156         /* Type */
157         char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
158         *((int *)&msg[1])=size;
159         send_data(sockid, &msg, sizeof(msg));
160         send_data(sockid, h, size);
161       }
162       break;
163       
164     case READ_MULT_REQUEST:
165       break;
166       
167     case MOVE_REQUEST:
168       break;
169       
170     case MOVE_MULT_REQUEST:
171       break;
172       
173     case TRANS_REQUEST:
174       /* Read transaction request */
175       transinfo.objlocked = NULL;
176       transinfo.objnotfound = NULL;
177       transinfo.modptr = NULL;
178       transinfo.numlocked = 0;
179       transinfo.numnotfound = 0;
180       if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
181         printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
182         pthread_exit(NULL);
183       }
184       break;
185     case TRANS_PREFETCH:
186       if((val = prefetchReq((int)acceptfd)) != 0) {
187         printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
188         break;
189       }
190       break;
191     case TRANS_PREFETCH_RESPONSE:
192       if((val = getPrefetchResponse((int) acceptfd)) != 0) {
193         printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
194         break;
195       }
196       break;
197     case START_REMOTE_THREAD:
198       recv_data((int)acceptfd, &oid, sizeof(unsigned int));
199       objType = getObjType(oid);
200       startDSMthread(oid, objType);
201       break;
202       
203     case THREAD_NOTIFY_REQUEST:
204       recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
205       size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
206       if((buffer = calloc(1,size)) == NULL) {
207         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
208         pthread_exit(NULL);
209       }
210       
211       recv_data((int)acceptfd, buffer, size);
212       
213       oidarry = calloc(numoid, sizeof(unsigned int)); 
214       memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
215       size = sizeof(unsigned int) * numoid;
216       versionarry = calloc(numoid, sizeof(unsigned short));
217       memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
218       size += sizeof(unsigned short) * numoid;
219       mid = *((unsigned int *)(buffer+size));
220       size += sizeof(unsigned int);
221       threadid = *((unsigned int *)(buffer+size));
222       processReqNotify(numoid, oidarry, versionarry, mid, threadid);
223       free(buffer);
224       
225       break;
226
227     case THREAD_NOTIFY_RESPONSE:
228       size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
229       if((buffer = calloc(1,size)) == NULL) {
230         printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
231         pthread_exit(NULL);
232       }
233       
234       recv_data((int)acceptfd, buffer, size);
235       
236       oid = *((unsigned int *)buffer);
237       size = sizeof(unsigned int);
238       version = *((unsigned short *)(buffer+size));
239       size += sizeof(unsigned short);
240       threadid = *((unsigned int *)(buffer+size));
241       threadNotify(oid,version,threadid);
242       free(buffer);
243       break;
244
245     case CLOSE_CONNECTION:
246       goto closeconnection;
247
248     default:
249       printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
250     }
251   }
252
253  closeconnection:
254   /* Close connection */
255   if (close((int)acceptfd) == -1)
256     perror("close");
257   pthread_exit(NULL);
258 }
259   
260 /* This function reads the information available in a transaction request
261  * and makes a function call to process the request */
262 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
263         char *ptr;
264         void *modptr;
265         unsigned int *oidmod, oid;
266         fixed_data_t fixed;
267         objheader_t *headaddr;
268         int sum, i, size, n, val;
269
270         oidmod = NULL;
271
272         /* Read fixed_data_t data structure */ 
273         size = sizeof(fixed) - 1;
274         ptr = (char *)&fixed;;
275         fixed.control = TRANS_REQUEST;
276         recv_data((int)acceptfd, ptr+1, size);
277
278         /* Read list of mids */
279         int mcount = fixed.mcount;
280         size = mcount * sizeof(unsigned int);
281         unsigned int listmid[mcount];
282         ptr = (char *) listmid;
283         recv_data((int)acceptfd, ptr, size);
284         
285         /* Read oid and version tuples for those objects that are not modified in the transaction */
286         int numread = fixed.numread;
287         size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
288         char objread[size];
289         if(numread != 0) { //If pile contains more than one object to be read, 
290                           // keep reading all objects
291                 recv_data((int)acceptfd, objread, size);        
292         }
293         
294         /* Read modified objects */
295         if(fixed.nummod != 0) {
296                 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
297                         printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
298                         return 1;
299                 }
300                 size = fixed.sum_bytes;
301                 recv_data((int)acceptfd, modptr, size); 
302         }
303
304         /* Create an array of oids for modified objects */
305         oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
306         if (oidmod == NULL)
307         {
308                 printf("calloc error %s, %d\n", __FILE__, __LINE__);
309                 return 1;
310         }
311         ptr = (char *) modptr;
312         for(i = 0 ; i < fixed.nummod; i++) {
313           int tmpsize;
314           headaddr = (objheader_t *) ptr;
315           oid = OID(headaddr);
316           oidmod[i] = oid;
317           GETSIZE(tmpsize, headaddr);
318           ptr += sizeof(objheader_t) + tmpsize;
319         }
320         
321         /*Process the information read */
322         if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
323                 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
324                 /* Free resources */
325                 if(oidmod != NULL) {
326                         free(oidmod);
327                 }
328                 return 1;
329         }
330
331         /* Free resources */
332         if(oidmod != NULL) {
333                 free(oidmod);
334         }
335
336         return 0;
337 }
338
339 /* This function processes the Coordinator's transaction request using "handleTransReq" 
340  * function and sends a reply to the co-ordinator.
341  * Following this it also receives a new control message from the co-ordinator and processes this message*/
342 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
343                 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
344         char control, sendctrl, retval;
345         objheader_t *tmp_header;
346         void *header;
347         int  i = 0, val;
348
349         /* Send reply to the Coordinator */
350         if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
351                 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
352                 return 1;
353         }
354
355         recv_data((int)acceptfd, &control, sizeof(char));
356         
357         /* Process the new control message */
358         switch(control) {
359                 case TRANS_ABORT:
360                         if (fixed->nummod > 0)
361                                 free(modptr);
362                         /* Unlock objects that was locked due to this transaction */
363                         for(i = 0; i< transinfo->numlocked; i++) {
364                                 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
365                                         printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
366                                         return 1;
367                                 }
368                                 STATUS(((objheader_t *)header)) &= ~(LOCK);             
369                         }
370
371                         /* Send ack to Coordinator */
372                         sendctrl = TRANS_UNSUCESSFUL;
373                         send_data((int)acceptfd, &sendctrl, sizeof(char));
374                         break;
375
376                 case TRANS_COMMIT:
377                         /* Invoke the transCommit process() */
378                         if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
379                                 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
380                                 /* Free memory */
381                                 if (transinfo->objlocked != NULL) {
382                                         free(transinfo->objlocked);
383                                 }
384                                 if (transinfo->objnotfound != NULL) {
385                                         free(transinfo->objnotfound);
386                                 }
387                                 return 1;
388                         }
389                         break;
390
391                 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
392                         break;
393                 default:
394                         printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
395                         //TODO Use fixed.trans_id  TID since Client may have died
396                         break;
397         }
398
399         /* Free memory */
400         if (transinfo->objlocked != NULL) {
401                 free(transinfo->objlocked);
402         }
403         if (transinfo->objnotfound != NULL) {
404                 free(transinfo->objnotfound);
405         }
406
407         return 0;
408 }
409
410 /* This function increments counters while running a voting decision on all objects involved 
411  * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
412 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
413         int val, i = 0, j;
414         unsigned short version;
415         char control = 0, *ptr;
416         unsigned int oid;
417         unsigned int *oidnotfound, *oidlocked;
418         void *mobj;
419         objheader_t *headptr;
420
421         /* Counters and arrays to formulate decision on control message to be sent */
422         oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
423         oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int)); 
424         int objnotfound = 0, objlocked = 0;
425         int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
426
427         /* modptr points to the beginning of the object store 
428          * created at the Pariticipant. 
429          * Object store holds the modified objects involved in the transaction request */ 
430         ptr = (char *) modptr;
431         
432         /* Process each oid in the machine pile/ group per thread */
433         for (i = 0; i < fixed->numread + fixed->nummod; i++) {
434                 if (i < fixed->numread) {//Objs only read and not modified
435                         int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
436                         incr *= i;
437                         oid = *((unsigned int *)(objread + incr));
438                         incr += sizeof(unsigned int);
439                         version = *((unsigned short *)(objread + incr));
440                 } else {//Objs modified
441                   int tmpsize;
442                   headptr = (objheader_t *) ptr;
443                   oid = OID(headptr);
444           version = headptr->version;
445           GETSIZE(tmpsize, headptr);
446           ptr += sizeof(objheader_t) + tmpsize;
447         }
448
449         /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
450
451         if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
452           /* Save the oids not found and number of oids not found for later use */
453           oidnotfound[objnotfound] = oid;
454           objnotfound++;
455         } else { /* If Obj found in machine (i.e. has not moved) */
456           /* Check if Obj is locked by any previous transaction */
457           pthread_mutex_lock(&lockObjHeader);
458           if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {                 
459             pthread_mutex_unlock(&lockObjHeader);
460             if (version == ((objheader_t *)mobj)->version) {      /* If locked then match versions */
461               v_matchlock++;
462             } else {/* If versions don't match ...HARD ABORT */
463               v_nomatch++;
464               /* Send TRANS_DISAGREE to Coordinator */
465               control = TRANS_DISAGREE;
466               if (objlocked > 0) {
467                 for(j = 0; j < objlocked; j++) {
468                   if((headptr = mhashSearch(oidlocked[j])) == NULL) {
469                     printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
470                     return 0;
471                   }
472                   STATUS(headptr) &= ~(LOCK);
473                 }
474                 free(oidlocked);
475               }
476               send_data(acceptfd, &control, sizeof(char));
477               return control;
478             }
479           } else {/* If Obj is not locked then lock object */
480             STATUS(((objheader_t *)mobj)) |= LOCK;
481             pthread_mutex_unlock(&lockObjHeader);
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               if (objlocked > 0) {
491                 for(j = 0; j < objlocked; j++) {
492                   if((headptr = mhashSearch(oidlocked[j])) == NULL) {
493                     printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
494                     return 0;
495                   }
496                   STATUS(headptr) &= ~(LOCK);
497                 }
498                 free(oidlocked);
499               }
500
501               /* Send TRANS_DISAGREE to Coordinator */
502               send_data(acceptfd, &control, sizeof(char));
503               return control;
504             }
505           }
506         }
507         }
508         
509         /* Decide what control message to send to Coordinator */
510         if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
511                                         modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
512                 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
513                 return 0;
514         }
515         
516         return control;
517
518 }
519 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
520  * to send to Coordinator based on the votes of oids involved in the transaction */
521 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock, 
522                 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr, 
523                 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
524         int val;
525         char control = 0;
526
527         /* Condition to send TRANS_AGREE */
528         if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
529                 control = TRANS_AGREE;
530                 /* Send control message */
531                 send_data(acceptfd, &control, sizeof(char));
532         }
533         /* Condition to send TRANS_SOFT_ABORT */
534         if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
535                 control = TRANS_SOFT_ABORT;
536
537                 /* Send control message */
538                 send_data(acceptfd, &control, sizeof(char));
539         
540                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
541                 if(*(objnotfound) != 0) { 
542                         int msg[1];
543                         msg[0] = *(objnotfound);
544                         send_data(acceptfd, &msg, sizeof(int));
545                         int size = sizeof(unsigned int)* *(objnotfound);
546                         send_data(acceptfd, oidnotfound, size);
547                 }
548         }
549
550         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
551          * if Participant receives a TRANS_COMMIT */
552         transinfo->objlocked = oidlocked;
553         transinfo->objnotfound = oidnotfound;
554         transinfo->modptr = modptr;
555         transinfo->numlocked = *(objlocked);
556         transinfo->numnotfound = *(objnotfound);
557
558         return control;
559 }
560
561 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer 
562  * addresses in lookup table and also changes version number
563  * Sends an ACK back to Coordinator */
564 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
565         objheader_t *header;
566         objheader_t *newheader;
567         int i = 0, offset = 0;
568         char control;
569         int tmpsize;
570
571         /* Process each modified object saved in the mainobject store */
572         for(i = 0; i < nummod; i++) {
573                 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
574                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
575                         return 1;
576                 }
577                 GETSIZE(tmpsize,header);
578                 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
579                 header->version += 1; 
580                 /* If threads are waiting on this object to be updated, notify them */
581         pthread_mutex_lock(&notifymutex);
582                 if(header->notifylist != NULL) {
583                         notifyAll(&header->notifylist, OID(header), header->version);
584                 }
585         pthread_mutex_unlock(&notifymutex);
586                 offset += sizeof(objheader_t) + tmpsize;
587         }
588
589         if (nummod > 0)
590                 free(modptr);
591
592         /* Unlock locked objects */
593         for(i = 0; i < numlocked; i++) {
594                 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
595                         printf("Error: 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         send_data((int)acceptfd, &control, sizeof(char));
605         return 0;
606 }
607
608 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
609  * Looks for the objects to be prefetched in the main object store.
610  * If objects are not found then record those and if objects are found
611  * then use offset values to prefetch references to other objects */
612
613 int prefetchReq(int acceptfd) {
614   int i, size, objsize, numoffset = 0;
615   int length;
616   char *recvbuffer, control;
617   unsigned int oid, mid=-1;
618   objheader_t *header;
619   oidmidpair_t oidmid;
620   int sd = -1;
621       
622   while(1) {
623     recv_data((int)acceptfd, &numoffset, sizeof(int));
624     if(numoffset == -1) 
625       break;
626     recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
627     oid = oidmid.oid;
628     if (mid != oidmid.mid) {
629       if (mid!=-1) {
630         freeSockWithLock(transPResponseSocketPool, mid, sd);
631       }
632       mid=oidmid.mid;
633       sd = getSockWithLock(transPResponseSocketPool, mid);
634     }
635     short offsetarry[numoffset];
636     recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
637     
638     /*Process each oid */
639     if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
640       /* Save the oids not found in buffer for later use */
641       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
642       char sendbuffer[size];
643       *((int *) sendbuffer) = size;
644       *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
645       *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
646       control = TRANS_PREFETCH_RESPONSE;
647       sendPrefetchResponse(sd, &control, sendbuffer, &size);
648     } else { /* Object Found */
649       int incr = 0;
650       GETSIZE(objsize, header);
651       size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
652       char sendbuffer[size];
653       *((int *) (sendbuffer + incr)) = size;
654       incr += sizeof(int);
655       *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
656       incr += sizeof(char);
657       *((unsigned int *)(sendbuffer+incr)) = oid;
658       incr += sizeof(unsigned int);
659       memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
660       
661       control = TRANS_PREFETCH_RESPONSE;
662       sendPrefetchResponse(sd, &control, sendbuffer, &size);
663       
664       /* Calculate the oid corresponding to the offset value */
665       for(i = 0 ; i< numoffset ; i++) {
666         /* Check for arrays  */
667         if(TYPE(header) > NUMCLASSES) {
668           int elementsize = classsize[TYPE(header)];
669           struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
670           unsigned short length = ao->___length___;
671           /* Check if array out of bounds */
672           if(offsetarry[i]< 0 || offsetarry[i] >= length) {
673             break;
674           }
675           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
676         } else {
677           oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
678         }
679         
680         /* Don't continue if we hit a NULL pointer */
681         if (oid==0)
682           break;
683
684         if((header = mhashSearch(oid)) == NULL) {
685           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
686           char sendbuffer[size];
687           *((int *) sendbuffer) = size;
688           *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
689           *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
690           
691           control = TRANS_PREFETCH_RESPONSE;
692           sendPrefetchResponse(sd, &control, sendbuffer, &size);
693           break;
694         } else {/* Obj Found */
695           int incr = 0;
696           GETSIZE(objsize, header);
697           size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
698           char sendbuffer[size];
699           *((int *) (sendbuffer + incr)) = size;
700           incr += sizeof(int);
701           *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
702           incr += sizeof(char);
703           *((unsigned int *)(sendbuffer+incr)) = oid;
704           incr += sizeof(unsigned int);
705           memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
706           
707           control = TRANS_PREFETCH_RESPONSE;
708           sendPrefetchResponse(sd, &control, sendbuffer, &size);
709         }
710       }
711     }
712   }
713   //Release socket
714   if (mid!=-1)
715     freeSockWithLock(transPResponseSocketPool, mid, sd);
716     
717   return 0;
718 }
719
720 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
721         send_data(sd, control, sizeof(char));
722         /* Send the buffer with its size */
723         int length = *(size);
724         send_data(sd, sendbuffer, length);
725 }
726
727 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
728         objheader_t *header;
729         unsigned int oid;
730         unsigned short newversion;
731         char msg[1+  2 * sizeof(unsigned int) + sizeof(unsigned short)];
732         int sd;
733         struct sockaddr_in remoteAddr;
734         int bytesSent;
735         int size;
736         int i = 0;
737
738         while(i < numoid) {
739                 oid = *(oidarry + i);
740                 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
741                         printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
742                         return;
743                 } else {
744                         /* Check to see if versions are same */
745 checkversion:
746                         if ((STATUS(header) & LOCK) != LOCK) {          
747                 pthread_mutex_lock(&notifymutex);
748                                 STATUS(header) |= LOCK;
749                                 newversion = header->version;
750                                 if(newversion == *(versionarry + i)) {
751                                         //Add to the notify list 
752                                         if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
753                                                 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__); 
754                         pthread_mutex_unlock(&notifymutex);
755                                                 return;
756                                         }
757                                         STATUS(header) &= ~(LOCK);              
758                     pthread_mutex_unlock(&notifymutex);
759                                 } else {
760                                         STATUS(header) &= ~(LOCK);              
761                     pthread_mutex_unlock(&notifymutex);
762                                         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
763                                                 perror("processReqNotify():socket()");
764                                                 return;
765                                         }
766                                         bzero(&remoteAddr, sizeof(remoteAddr));
767                                         remoteAddr.sin_family = AF_INET;
768                                         remoteAddr.sin_port = htons(LISTEN_PORT);
769                                         remoteAddr.sin_addr.s_addr = htonl(mid);
770
771                                         if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
772                                                 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
773                                                                 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
774                                                 close(sd);
775                                                 return;
776                                         } else {
777                                                 //Send Update notification
778                                                 msg[0] = THREAD_NOTIFY_RESPONSE;
779                                                 *((unsigned int *)&msg[1]) = oid;
780                                                 size = sizeof(unsigned int);
781                                                 *((unsigned short *)(&msg[1]+size)) = newversion;
782                                                 size += sizeof(unsigned short);
783                                                 *((unsigned int *)(&msg[1]+size)) = threadid;
784                                                 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
785                                                 send_data(sd, msg, size);
786                                         }
787                                         close(sd);
788                                 }
789                         } else {
790                                 randomdelay();
791                                 goto checkversion;
792                         }
793                 }
794                 i++;
795         }
796         free(oidarry);
797         free(versionarry);
798 }