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 */
4 #include <netinet/tcp.h>
8 #include "threadnotify.h"
15 #include "readstruct.h"
17 #define BACKLOG 10 //max pending connections
18 #define RECEIVE_BUFFER_SIZE 2048
20 extern int classsize[];
21 extern int numHostsInSystem;
22 extern pthread_mutex_t notifymutex;
24 objstr_t *mainobjstore;
25 pthread_mutex_t mainobjstore_mutex;
26 pthread_mutex_t lockObjHeader;
27 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
29 sockPoolHashTable_t *transPResponseSocketPool;
31 /* This function initializes the main objects store and creates the
32 * global machine and location lookup table */
35 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
36 /* Initialize attribute for mutex */
37 pthread_mutexattr_init(&mainobjstore_mutex_attr);
38 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
39 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
40 pthread_mutex_init(&lockObjHeader,NULL);
41 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
44 if (lhashCreate(HASH_SIZE, LOADFACTOR))
47 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
50 //Initialize socket pool
51 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
52 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
60 int startlistening() {
62 struct sockaddr_in my_addr;
63 socklen_t addrlength = sizeof(struct sockaddr);
66 listenfd = socket(AF_INET, SOCK_STREAM, 0);
72 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
77 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
83 my_addr.sin_family = AF_INET;
84 my_addr.sin_port = htons(LISTEN_PORT);
85 my_addr.sin_addr.s_addr = INADDR_ANY;
86 memset(&(my_addr.sin_zero), '\0', 8);
88 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
93 if (listen(listenfd, BACKLOG) == -1) {
100 /* This function starts the thread to listen on a socket
101 * for tranaction calls */
102 void *dstmListen(void *lfd) {
103 int listenfd=(int)lfd;
105 struct sockaddr_in client_addr;
106 socklen_t addrlength = sizeof(struct sockaddr);
107 pthread_t thread_dstm_accept;
109 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
113 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
114 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
116 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
118 pthread_detach(thread_dstm_accept);
121 /* This function accepts a new connection request, decodes the control message in the connection
122 * and accordingly calls other functions to process new requests */
123 void *dstmAccept(void *acceptfd) {
124 int val, retval, size, sum, sockid;
131 trans_commit_data_t transinfo;
132 unsigned short objType, *versionarry, version;
133 unsigned int *oidarry, numoid, mid, threadid;
134 struct readstruct readbuffer;
138 /* Receive control messages from other machines */
140 int ret=recv_data_errorcode_buf((int)acceptfd, &readbuffer, &control, sizeof(char));
144 printf("DEBUG -> RECV Error!.. retrying\n");
149 /* Read oid requested and search if available */
150 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
151 while((srcObj = mhashSearch(oid)) == NULL) {
153 if((ret = sched_yield()) != 0) {
154 printf("%s(): error no %d in thread yield\n", __func__, errno);
157 h = (objheader_t *) srcObj;
159 size += sizeof(objheader_t);
160 sockid = (int) acceptfd;
162 ctrl = OBJECT_NOT_FOUND;
163 send_data(sockid, &ctrl, sizeof(char));
166 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
167 *((int *)&msg[1])=size;
168 send_data(sockid, &msg, sizeof(msg));
169 send_data(sockid, h, size);
173 case READ_MULT_REQUEST:
179 case MOVE_MULT_REQUEST:
183 /* Read transaction request */
184 transinfo.objlocked = NULL;
185 transinfo.objnotfound = NULL;
186 transinfo.modptr = NULL;
187 transinfo.numlocked = 0;
188 transinfo.numnotfound = 0;
189 if((val = readClientReq(&transinfo, (int)acceptfd, &readbuffer)) != 0) {
190 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
197 if((val = rangePrefetchReq((int)acceptfd, &readbuffer)) != 0) {
198 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
202 if((val = prefetchReq((int)acceptfd, &readbuffer)) != 0) {
203 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
209 case TRANS_PREFETCH_RESPONSE:
211 if((val = getRangePrefetchResponse((int)acceptfd, &readbuffer)) != 0) {
212 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
216 if((val = getPrefetchResponse((int) acceptfd, &readbuffer)) != 0) {
217 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
223 case START_REMOTE_THREAD:
224 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
225 objType = getObjType(oid);
226 startDSMthread(oid, objType);
229 case THREAD_NOTIFY_REQUEST:
230 recv_data_buf((int)acceptfd, &readbuffer, &numoid, sizeof(unsigned int));
231 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
232 if((buffer = calloc(1,size)) == NULL) {
233 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
237 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
239 oidarry = calloc(numoid, sizeof(unsigned int));
240 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
241 size = sizeof(unsigned int) * numoid;
242 versionarry = calloc(numoid, sizeof(unsigned short));
243 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
244 size += sizeof(unsigned short) * numoid;
245 mid = *((unsigned int *)(buffer+size));
246 size += sizeof(unsigned int);
247 threadid = *((unsigned int *)(buffer+size));
248 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
252 case THREAD_NOTIFY_RESPONSE:
253 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
254 if((buffer = calloc(1,size)) == NULL) {
255 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
259 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
261 oid = *((unsigned int *)buffer);
262 size = sizeof(unsigned int);
263 version = *((unsigned short *)(buffer+size));
264 size += sizeof(unsigned short);
265 threadid = *((unsigned int *)(buffer+size));
266 threadNotify(oid,version,threadid);
270 case CLOSE_CONNECTION:
271 goto closeconnection;
274 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
279 /* Close connection */
280 if (close((int)acceptfd) == -1)
285 /* This function reads the information available in a transaction request
286 * and makes a function call to process the request */
287 int readClientReq(trans_commit_data_t *transinfo, int acceptfd, struct readstruct * readbuffer) {
290 unsigned int *oidmod, oid;
292 objheader_t *headaddr;
293 int sum, i, size, n, val;
297 /* Read fixed_data_t data structure */
298 size = sizeof(fixed) - 1;
299 ptr = (char *)&fixed;;
300 fixed.control = TRANS_REQUEST;
301 recv_data_buf((int)acceptfd, readbuffer, ptr+1, size);
303 /* Read list of mids */
304 int mcount = fixed.mcount;
305 size = mcount * sizeof(unsigned int);
306 unsigned int listmid[mcount];
307 ptr = (char *) listmid;
308 recv_data_buf((int)acceptfd, readbuffer, ptr, size);
310 /* Read oid and version tuples for those objects that are not modified in the transaction */
311 int numread = fixed.numread;
312 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
314 if(numread != 0) { //If pile contains more than one object to be read,
315 // keep reading all objects
316 recv_data_buf((int)acceptfd, readbuffer, objread, size);
319 /* Read modified objects */
320 if(fixed.nummod != 0) {
321 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
322 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
325 size = fixed.sum_bytes;
326 recv_data_buf((int)acceptfd, readbuffer, modptr, size);
329 /* Create an array of oids for modified objects */
330 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
331 if (oidmod == NULL) {
332 printf("calloc error %s, %d\n", __FILE__, __LINE__);
335 ptr = (char *) modptr;
336 for(i = 0 ; i < fixed.nummod; i++) {
338 headaddr = (objheader_t *) ptr;
341 GETSIZE(tmpsize, headaddr);
342 ptr += sizeof(objheader_t) + tmpsize;
345 /*Process the information read */
346 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd, readbuffer)) != 0) {
347 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
363 /* This function processes the Coordinator's transaction request using "handleTransReq"
364 * function and sends a reply to the co-ordinator.
365 * Following this it also receives a new control message from the co-ordinator and processes this message*/
366 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
367 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd, struct readstruct *readbuffer) {
369 char control, sendctrl, retval;
370 objheader_t *tmp_header;
374 /* Send reply to the Coordinator */
375 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
376 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
380 recv_data_buf((int)acceptfd, readbuffer, &control, sizeof(char));
381 /* Process the new control message */
384 if (fixed->nummod > 0)
386 /* Unlock objects that was locked due to this transaction */
387 int useWriteUnlock = 0;
388 for(i = 0; i< transinfo->numlocked; i++) {
389 if(transinfo->objlocked[i] == -1) {
393 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
394 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
398 write_unlock(STATUSPTR(header));
400 read_unlock(STATUSPTR(header));
406 /* Invoke the transCommit process() */
407 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
408 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
410 if (transinfo->objlocked != NULL) {
411 free(transinfo->objlocked);
413 if (transinfo->objnotfound != NULL) {
414 free(transinfo->objnotfound);
420 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
424 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
425 //TODO Use fixed.trans_id TID since Client may have died
430 if (transinfo->objlocked != NULL) {
431 free(transinfo->objlocked);
433 if (transinfo->objnotfound != NULL) {
434 free(transinfo->objnotfound);
440 /* This function increments counters while running a voting decision on all objects involved
441 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
442 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
444 unsigned short version;
445 char control = 0, *ptr;
447 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
448 objheader_t *headptr;
450 /* Counters and arrays to formulate decision on control message to be sent */
451 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
452 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
453 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
454 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
455 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
457 /* modptr points to the beginning of the object store
458 * created at the Pariticipant.
459 * Object store holds the modified objects involved in the transaction request */
460 ptr = (char *) modptr;
462 /* Process each oid in the machine pile/ group per thread */
463 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
464 if (i < fixed->numread) { //Objs only read and not modified
465 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
467 oid = *((unsigned int *)(objread + incr));
468 incr += sizeof(unsigned int);
469 version = *((unsigned short *)(objread + incr));
470 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
471 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
472 } else { //Objs modified
473 if(i == fixed->numread) {
474 oidlocked[objlocked++] = -1;
477 headptr = (objheader_t *) ptr;
479 version = headptr->version;
480 GETSIZE(tmpsize, headptr);
481 ptr += sizeof(objheader_t) + tmpsize;
482 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
483 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
484 &numBytes, &control, oid, version);
488 /* send TRANS_DISAGREE and objs*/
491 char *objs = calloc(1, numBytes);
493 for(j = 0; j<objvernotmatch; j++) {
494 objheader_t *header = mhashSearch(oidvernotmatch[j]);
496 GETSIZE(size, header);
497 size += sizeof(objheader_t);
498 memcpy(objs+offset, header, size);
503 int useWriteUnlock = 0;
504 for(j = 0; j < objlocked; j++) {
505 if(oidlocked[j] == -1) {
509 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
510 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
514 write_unlock(STATUSPTR(headptr));
516 read_unlock(STATUSPTR(headptr));
521 send_data(acceptfd, &control, sizeof(char));
523 send_data(acceptfd, &numBytes, sizeof(int));
524 send_data(acceptfd, objs, numBytes);
525 transinfo->objvernotmatch = oidvernotmatch;
526 transinfo->numvernotmatch = objvernotmatch;
528 free(transinfo->objvernotmatch);
533 /* Decide what control message to send to Coordinator */
534 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
535 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
536 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
542 /* Update Commit info for objects that are read */
543 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
544 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
545 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
546 char *control, unsigned int oid, unsigned short version) {
548 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
550 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
551 /* Save the oids not found and number of oids not found for later use */
552 oidnotfound[*objnotfound] = oid;
554 } else { /* If Obj found in machine (i.e. has not moved) */
555 /* Check if Obj is locked by any previous transaction */
556 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
557 if (version == ((objheader_t *)mobj)->version) { /* match versions */
559 } else { /* If versions don't match ...HARD ABORT */
561 oidvernotmatch[*objvernotmatch] = oid;
565 size += sizeof(objheader_t);
567 /* Send TRANS_DISAGREE to Coordinator */
568 *control = TRANS_DISAGREE;
569 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
571 //Keep track of oid locked
572 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
573 } else { //we are locked
574 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
576 } else { /* If versions don't match ...HARD ABORT */
578 oidvernotmatch[*objvernotmatch] = oid;
582 size += sizeof(objheader_t);
584 *control = TRANS_DISAGREE;
585 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
591 /* Update Commit info for objects that are read */
592 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
593 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
594 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
596 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
597 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
598 /* Save the oids not found and number of oids not found for later use */
599 oidnotfound[*objnotfound] = oid;
601 } else { /* If Obj found in machine (i.e. has not moved) */
602 /* Check if Obj is locked by any previous transaction */
603 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
604 if (version == ((objheader_t *)mobj)->version) { /* match versions */
606 } else { /* If versions don't match ...HARD ABORT */
608 oidvernotmatch[(*objvernotmatch)++] = oid;
611 size += sizeof(objheader_t);
613 /* Send TRANS_DISAGREE to Coordinator */
614 *control = TRANS_DISAGREE;
615 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
617 //Keep track of oid locked
618 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
619 } else { /* Some other transaction has aquired a write lock on this object */
620 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
622 } else { /* If versions don't match ...HARD ABORT */
624 oidvernotmatch[*objvernotmatch] = oid;
628 size += sizeof(objheader_t);
630 *control = TRANS_DISAGREE;
631 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
637 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
638 * to send to Coordinator based on the votes of oids involved in the transaction */
639 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
640 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
641 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
645 /* Condition to send TRANS_AGREE */
646 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
647 control = TRANS_AGREE;
648 /* Send control message */
649 send_data(acceptfd, &control, sizeof(char));
651 /* Condition to send TRANS_SOFT_ABORT */
652 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
653 control = TRANS_SOFT_ABORT;
655 /* Send control message */
656 send_data(acceptfd, &control, sizeof(char));
658 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
659 if(*(objnotfound) != 0) {
661 msg[0] = *(objnotfound);
662 send_data(acceptfd, &msg, sizeof(int));
663 int size = sizeof(unsigned int)* *(objnotfound);
664 send_data(acceptfd, oidnotfound, size);
668 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
669 * if Participant receives a TRANS_COMMIT */
670 transinfo->objlocked = oidlocked;
671 transinfo->objnotfound = oidnotfound;
672 transinfo->modptr = modptr;
673 transinfo->numlocked = *(objlocked);
674 transinfo->numnotfound = *(objnotfound);
678 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
679 * addresses in lookup table and also changes version number
680 * Sends an ACK back to Coordinator */
681 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
683 objheader_t *newheader;
684 int i = 0, offset = 0;
688 /* Process each modified object saved in the mainobject store */
689 for(i = 0; i < nummod; i++) {
690 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
691 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
694 GETSIZE(tmpsize,header);
697 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
698 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
700 dst->___cachedCode___=src->___cachedCode___;
701 dst->___cachedHash___=src->___cachedHash___;
702 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
704 header->version += 1;
705 /* If threads are waiting on this object to be updated, notify them */
706 if(header->notifylist != NULL) {
707 notifyAll(&header->notifylist, OID(header), header->version);
709 offset += sizeof(objheader_t) + tmpsize;
715 /* Unlock locked objects */
716 int useWriteUnlock = 0;
717 for(i = 0; i < numlocked; i++) {
718 if(oidlocked[i] == -1) {
722 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
723 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
728 write_unlock(STATUSPTR(header));
730 read_unlock(STATUSPTR(header));
733 //TODO Update location lookup table
737 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
738 * Looks for the objects to be prefetched in the main object store.
739 * If objects are not found then record those and if objects are found
740 * then use offset values to prefetch references to other objects */
742 int prefetchReq(int acceptfd, struct readstruct * readbuffer) {
743 int i, size, objsize, numoffset = 0;
745 char *recvbuffer, control;
746 unsigned int oid, mid=-1;
749 struct writestruct writebuffer;
752 recv_data_buf((int)acceptfd, readbuffer, &numoffset, sizeof(int));
755 recv_data_buf((int)acceptfd, readbuffer, &oidmid, 2*sizeof(unsigned int));
757 if (mid != oidmid.mid) {
759 forcesend_buf(sd, &writebuffer, NULL, 0);
760 freeSockWithLock(transPResponseSocketPool, mid, sd);
763 sd = getSockWithLock(transPResponseSocketPool, mid);
764 writebuffer.offset=0;
766 short offsetarry[numoffset];
767 recv_data_buf((int) acceptfd, readbuffer, offsetarry, numoffset*sizeof(short));
769 /*Process each oid */
770 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
771 /* Save the oids not found in buffer for later use */
772 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
773 char sendbuffer[size+1];
774 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
775 *((int *) (sendbuffer+sizeof(char))) = size;
776 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
777 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char)+sizeof(char))) = oid;
778 send_buf(sd, &writebuffer, sendbuffer, size+1);
779 } else { /* Object Found */
781 GETSIZE(objsize, header);
782 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
783 char sendbuffer[size+1];
784 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
785 *((int *)(sendbuffer + incr)) = size;
787 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
788 incr += sizeof(char);
789 *((unsigned int *)(sendbuffer+incr)) = oid;
790 incr += sizeof(unsigned int);
791 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
792 send_buf(sd, &writebuffer, sendbuffer, size+1);
794 /* Calculate the oid corresponding to the offset value */
795 for(i = 0 ; i< numoffset ; i++) {
796 /* Check for arrays */
797 if(TYPE(header) >= NUMCLASSES) {
798 int elementsize = classsize[TYPE(header)];
799 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
800 unsigned short length = ao->___length___;
801 /* Check if array out of bounds */
802 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
805 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
807 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
810 /* Don't continue if we hit a NULL pointer */
814 if((header = mhashSearch(oid)) == NULL) {
815 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
816 char sendbuffer[size+1];
817 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
818 *((int *) (sendbuffer+1)) = size;
819 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
820 *((unsigned int *)(sendbuffer + sizeof(char)+sizeof(int) + sizeof(char))) = oid;
822 send_buf(sd, &writebuffer, sendbuffer, size+1);
824 } else { /* Obj Found */
826 GETSIZE(objsize, header);
827 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
828 char sendbuffer[size+1];
829 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
830 *((int *)(sendbuffer + incr)) = size;
832 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
833 incr += sizeof(char);
834 *((unsigned int *)(sendbuffer+incr)) = oid;
835 incr += sizeof(unsigned int);
836 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
837 send_buf(sd, &writebuffer, sendbuffer, size+1);
844 forcesend_buf(sd, &writebuffer, NULL, 0);
845 freeSockWithLock(transPResponseSocketPool, mid, sd);
850 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
851 send_data(sd, control, sizeof(char));
852 /* Send the buffer with its size */
853 int length = *(size);
854 send_data(sd, sendbuffer, length);
857 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
860 unsigned short newversion;
861 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
863 struct sockaddr_in remoteAddr;
869 oid = *(oidarry + i);
870 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
871 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
874 /* Check to see if versions are same */
876 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
877 newversion = header->version;
878 if(newversion == *(versionarry + i)) {
879 //Add to the notify list
880 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
881 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
884 write_unlock(STATUSPTR(header));
886 write_unlock(STATUSPTR(header));
887 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
888 perror("processReqNotify():socket()");
891 bzero(&remoteAddr, sizeof(remoteAddr));
892 remoteAddr.sin_family = AF_INET;
893 remoteAddr.sin_port = htons(LISTEN_PORT);
894 remoteAddr.sin_addr.s_addr = htonl(mid);
896 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
897 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
898 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
902 //Send Update notification
903 msg[0] = THREAD_NOTIFY_RESPONSE;
904 *((unsigned int *)&msg[1]) = oid;
905 size = sizeof(unsigned int);
906 *((unsigned short *)(&msg[1]+size)) = newversion;
907 size += sizeof(unsigned short);
908 *((unsigned int *)(&msg[1]+size)) = threadid;
909 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
910 send_data(sd, msg, size);