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 */
15 #include "threadnotify.h"
21 #define LISTEN_PORT 2156
22 #define BACKLOG 10 //max pending connections
23 #define RECEIVE_BUFFER_SIZE 2048
25 extern int classsize[];
27 objstr_t *mainobjstore;
28 pthread_mutex_t mainobjstore_mutex;
29 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
30 pthread_mutex_t threadnotify_mutex = PTHREAD_MUTEX_INITIALIZER;
32 /* This function initializes the main objects store and creates the
33 * global machine and location lookup table */
37 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
38 /* Initialize attribute for mutex */
39 pthread_mutexattr_init(&mainobjstore_mutex_attr);
40 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
41 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
42 if (mhashCreate(HASH_SIZE, LOADFACTOR))
45 if (lhashCreate(HASH_SIZE, LOADFACTOR))
48 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
54 /* This function starts the thread to listen on a socket
55 * for tranaction calls */
58 int listenfd, acceptfd;
59 struct sockaddr_in my_addr;
60 struct sockaddr_in client_addr;
61 socklen_t addrlength = sizeof(struct sockaddr);
62 pthread_t thread_dstm_accept;
66 listenfd = socket(AF_INET, SOCK_STREAM, 0);
73 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
78 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
84 my_addr.sin_family = AF_INET;
85 my_addr.sin_port = htons(LISTEN_PORT);
86 my_addr.sin_addr.s_addr = INADDR_ANY;
87 memset(&(my_addr.sin_zero), '\0', 8);
89 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
95 if (listen(listenfd, BACKLOG) == -1)
101 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
105 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
107 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
109 pthread_detach(thread_dstm_accept);
112 /* This function accepts a new connection request, decodes the control message in the connection
113 * and accordingly calls other functions to process new requests */
114 void *dstmAccept(void *acceptfd)
116 int val, retval, size, sum;
123 trans_commit_data_t transinfo;
124 unsigned short objType, *versionarry, version;
125 unsigned int *oidarry, numoid, mid, threadid;
127 transinfo.objlocked = NULL;
128 transinfo.objnotfound = NULL;
129 transinfo.modptr = NULL;
130 transinfo.numlocked = 0;
131 transinfo.numnotfound = 0;
133 /* Receive control messages from other machines */
134 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
135 printf("%s() Error: Receiving control = %d at %s, %d\n", __func__, control, __FILE__, __LINE__);
141 /* Read oid requested and search if available */
142 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
143 perror("Error: receiving 0x0 object from cooridnator\n");
146 if((srcObj = mhashSearch(oid)) == NULL) {
147 printf("Error: Object 0x%x is not found in Main Object Store %s, %d\n", oid, __FILE__, __LINE__);
150 h = (objheader_t *) srcObj;
152 size += sizeof(objheader_t);
155 ctrl = OBJECT_NOT_FOUND;
156 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
157 perror("Error sending control msg to coordinator\n");
162 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
163 *((int *)&msg[1])=size;
164 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
165 perror("Error sending size of object to coordinator\n");
168 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
169 perror("Error in sending object\n");
175 case READ_MULT_REQUEST:
181 case MOVE_MULT_REQUEST:
185 /* Read transaction request */
186 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
187 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
192 if((val = prefetchReq((int)acceptfd)) != 0) {
193 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
197 case TRANS_PREFETCH_RESPONSE:
198 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
199 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
203 case START_REMOTE_THREAD:
204 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
206 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
207 else if (retval != sizeof(unsigned int))
208 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD %s, %d\n",
209 retval, __FILE__, __LINE__);
212 objType = getObjType(oid);
213 startDSMthread(oid, objType);
217 case THREAD_NOTIFY_REQUEST:
218 retval = recv((int)acceptfd, &numoid, sizeof(unsigned int), 0);
219 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
220 if((buffer = calloc(1,size)) == NULL) {
221 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
226 sum += recv((int)acceptfd, buffer+sum, size-sum, 0);
229 oidarry = calloc(numoid, sizeof(unsigned int));
230 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
231 size = sizeof(unsigned int) * numoid;
232 versionarry = calloc(numoid, sizeof(unsigned short));
233 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
234 size += sizeof(unsigned short) * numoid;
235 mid = *((unsigned int *)(buffer+size));
236 size += sizeof(unsigned int);
237 threadid = *((unsigned int *)(buffer+size));
238 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
243 case THREAD_NOTIFY_RESPONSE:
244 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
245 if((buffer = calloc(1,size)) == NULL) {
246 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
252 sum += recv((int)acceptfd, buffer+sum, size-sum, 0);
255 oid = *((unsigned int *)buffer);
256 size = sizeof(unsigned int);
257 version = *((unsigned short *)(buffer+size));
258 size += sizeof(unsigned short);
259 threadid = *((unsigned int *)(buffer+size));
260 threadNotify(oid,version,threadid);
265 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
268 /* Close connection */
269 if (close((int)acceptfd) == -1)
275 /* This function reads the information available in a transaction request
276 * and makes a function call to process the request */
277 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
280 unsigned int *oidmod, oid;
282 objheader_t *headaddr;
283 int sum = 0, i, N, n, val;
287 /* Read fixed_data_t data structure */
288 N = sizeof(fixed) - 1;
289 ptr = (char *)&fixed;;
290 fixed.control = TRANS_REQUEST;
292 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
294 } while(sum < N && n != 0);
296 /* Read list of mids */
297 int mcount = fixed.mcount;
298 N = mcount * sizeof(unsigned int);
299 unsigned int listmid[mcount];
300 ptr = (char *) listmid;
303 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
305 } while(sum < N && n != 0);
307 /* Read oid and version tuples for those objects that are not modified in the transaction */
308 int numread = fixed.numread;
309 N = numread * (sizeof(unsigned int) + sizeof(short));
311 if(numread != 0) { //If pile contains more than one object to be read,
312 // keep reading all objects
315 n = recv((int)acceptfd, (void *) objread+sum, N-sum, 0);
317 } while(sum < N && n != 0);
320 /* Read modified objects */
321 if(fixed.nummod != 0) {
322 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
323 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
327 do { // Recv the objs that are modified by the Coordinator
328 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
330 } while (sum < fixed.sum_bytes && n != 0);
333 /* Create an array of oids for modified objects */
334 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
337 printf("calloc error %s, %d\n", __FILE__, __LINE__);
340 ptr = (char *) modptr;
341 for(i = 0 ; i < fixed.nummod; i++) {
343 headaddr = (objheader_t *) ptr;
346 GETSIZE(tmpsize, headaddr);
347 ptr += sizeof(objheader_t) + tmpsize;
350 /*Process the information read */
351 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
352 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
368 /* This function processes the Coordinator's transaction request using "handleTransReq"
369 * function and sends a reply to the co-ordinator.
370 * Following this it also receives a new control message from the co-ordinator and processes this message*/
371 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
372 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
373 char control, sendctrl, retval;
374 objheader_t *tmp_header;
378 /* Send reply to the Coordinator */
379 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
380 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
385 retval = recv((int)acceptfd, &control, sizeof(char), 0);
386 } while(retval < sizeof(char));
388 /* Process the new control message */
391 if (fixed->nummod > 0)
393 /* Unlock objects that was locked due to this transaction */
394 for(i = 0; i< transinfo->numlocked; i++) {
395 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
396 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);// find the header address
399 STATUS(((objheader_t *)header)) &= ~(LOCK);
402 /* Send ack to Coordinator */
403 sendctrl = TRANS_UNSUCESSFUL;
404 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
405 perror("Error: In sending ACK to coordinator\n");
406 if (transinfo->objlocked != NULL) {
407 free(transinfo->objlocked);
409 if (transinfo->objnotfound != NULL) {
410 free(transinfo->objnotfound);
418 /* Invoke the transCommit process() */
419 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
420 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
422 if (transinfo->objlocked != NULL) {
423 free(transinfo->objlocked);
425 if (transinfo->objnotfound != NULL) {
426 free(transinfo->objnotfound);
432 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
435 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
436 //TODO Use fixed.trans_id TID since Client may have died
441 if (transinfo->objlocked != NULL) {
442 free(transinfo->objlocked);
444 if (transinfo->objnotfound != NULL) {
445 free(transinfo->objnotfound);
451 /* This function increments counters while running a voting decision on all objects involved
452 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
453 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
455 unsigned short version;
456 char control = 0, *ptr;
458 unsigned int *oidnotfound, *oidlocked;
460 objheader_t *headptr;
462 /* Counters and arrays to formulate decision on control message to be sent */
463 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
464 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
465 int objnotfound = 0, objlocked = 0;
466 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
468 /* modptr points to the beginning of the object store
469 * created at the Pariticipant.
470 * Object store holds the modified objects involved in the transaction request */
471 ptr = (char *) modptr;
473 /* Process each oid in the machine pile/ group per thread */
474 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
475 if (i < fixed->numread) {//Objs only read and not modified
476 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
478 oid = *((unsigned int *)(objread + incr));
479 incr += sizeof(unsigned int);
480 version = *((unsigned short *)(objread + incr));
481 } else {//Objs modified
483 headptr = (objheader_t *) ptr;
485 version = headptr->version;
486 GETSIZE(tmpsize, headptr);
487 ptr += sizeof(objheader_t) + tmpsize;
490 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
492 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
493 /* Save the oids not found and number of oids not found for later use */
494 oidnotfound[objnotfound] = oid;
496 } else { /* If Obj found in machine (i.e. has not moved) */
497 /* Check if Obj is locked by any previous transaction */
498 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
499 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
501 } else {/* If versions don't match ...HARD ABORT */
503 /* Send TRANS_DISAGREE to Coordinator */
504 control = TRANS_DISAGREE;
506 for(j = 0; j < objlocked; j++) {
507 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
508 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
511 STATUS(headptr) &= ~(LOCK);
515 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
516 perror("Error in sending control to the Coordinator\n");
521 } else {/* If Obj is not locked then lock object */
522 STATUS(((objheader_t *)mobj)) |= LOCK;
523 /* Save all object oids that are locked on this machine during this transaction request call */
524 oidlocked[objlocked] = OID(((objheader_t *)mobj));
526 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
528 } else { /* If versions don't match ...HARD ABORT */
530 control = TRANS_DISAGREE;
532 for(j = 0; j < objlocked; j++) {
533 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
534 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
537 STATUS(headptr) &= ~(LOCK);
542 /* Send TRANS_DISAGREE to Coordinator */
543 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
544 perror("Error in sending control to the Coordinator\n");
554 /* Decide what control message to send to Coordinator */
555 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
556 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
557 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
564 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
565 * to send to Coordinator based on the votes of oids involved in the transaction */
566 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
567 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
568 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
572 /* Condition to send TRANS_AGREE */
573 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
574 control = TRANS_AGREE;
575 /* Send control message */
576 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
577 perror("Error in sending control to Coordinator\n");
581 /* Condition to send TRANS_SOFT_ABORT */
582 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
583 control = TRANS_SOFT_ABORT;
585 /* Send control message */
586 if((val = send(acceptfd, &control, sizeof(char),MSG_NOSIGNAL)) < sizeof(char)) {
587 perror("Error in sending TRANS_SOFT_ABORT control\n");
591 /* Send number of oids not found and the missing oids if objects are missing in the machine */
592 if(*(objnotfound) != 0) {
594 msg[0] = *(objnotfound);
595 if((val = send(acceptfd, msg, sizeof(int) ,MSG_NOSIGNAL)) < sizeof(int)) {
596 perror("Error in sending objects that are not found\n");
599 int size = sizeof(unsigned int)* *(objnotfound);
600 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
601 perror("Error in sending objects that are not found\n");
607 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
608 * if Participant receives a TRANS_COMMIT */
609 transinfo->objlocked = oidlocked;
610 transinfo->objnotfound = oidnotfound;
611 transinfo->modptr = modptr;
612 transinfo->numlocked = *(objlocked);
613 transinfo->numnotfound = *(objnotfound);
618 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
619 * addresses in lookup table and also changes version number
620 * Sends an ACK back to Coordinator */
621 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
623 objheader_t *newheader;
624 int i = 0, offset = 0;
628 /* Process each modified object saved in the mainobject store */
629 for(i = 0; i < nummod; i++) {
630 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
631 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
634 GETSIZE(tmpsize,header);
635 pthread_mutex_lock(&mainobjstore_mutex);
636 memcpy((char*)header + sizeof(objheader_t), ((char *)modptr + sizeof(objheader_t) + offset), tmpsize);
637 header->version += 1;
638 /* If threads are waiting on this object to be updated, notify them */
639 if(header->notifylist != NULL) {
640 notifyAll(&header->notifylist, OID(header), header->version);
642 pthread_mutex_unlock(&mainobjstore_mutex);
643 offset += sizeof(objheader_t) + tmpsize;
649 /* Unlock locked objects */
650 for(i = 0; i < numlocked; i++) {
651 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
652 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
655 STATUS(header) &= ~(LOCK);
657 //TODO Update location lookup table
659 /* Send ack to coordinator */
660 control = TRANS_SUCESSFUL;
661 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
662 perror("Error sending ACK to coordinator\n");
669 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
670 * Looks for the objects to be prefetched in the main object store.
671 * If objects are not found then record those and if objects are found
672 * then use offset values to prefetch references to other objects */
674 int prefetchReq(int acceptfd) {
675 int i, size, objsize, numbytes = 0, isArray = 0, numoffset = 0;
677 char *recvbuffer, *sendbuffer, control;
678 unsigned int oid, mid;
679 unsigned short *offsetarry;
681 struct sockaddr_in remoteAddr;
683 while((numbytes = recv((int)acceptfd, &length, sizeof(int), 0)) != 0) {
684 if(length == -1) { //-1 is special character to represent end of sending oids and offsets
688 size = length - sizeof(int);
689 if((recvbuffer = calloc(1, size)) == NULL) {
690 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
693 while(numbytes < size) {
694 numbytes += recv((int)acceptfd, recvbuffer+numbytes, size-numbytes, 0);
697 oid = *((unsigned int *) recvbuffer);
698 mid = *((unsigned int *) (recvbuffer + sizeof(unsigned int)));
699 size = size - (2 * sizeof(unsigned int));
700 numoffset = size / sizeof(short);
701 if((offsetarry = calloc(numoffset, sizeof(unsigned short))) == NULL) {
702 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
706 memcpy(offsetarry, recvbuffer + (2 * sizeof(unsigned int)), size);
709 /* Create socket to send information */
710 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
711 perror("prefetchReq():socket()");
714 bzero(&remoteAddr, sizeof(remoteAddr));
715 remoteAddr.sin_family = AF_INET;
716 remoteAddr.sin_port = htons(LISTEN_PORT);
717 remoteAddr.sin_addr.s_addr = htonl(mid);
719 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
720 printf("Error: prefetchReq():error %d connecting to %s:%d\n", errno,
721 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
726 /*Process each oid */
727 if ((header = mhashSearch(oid)) == NULL) {/* Obj not found */
728 /* Save the oids not found in buffer for later use */
729 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
730 if((sendbuffer = calloc(1, size)) == NULL) {
731 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
736 *((int *) sendbuffer) = size;
737 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
738 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
740 control = TRANS_PREFETCH_RESPONSE;
741 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
743 printf("Error: %s() in sending prefetch response at %s, %d\n",
744 __func__, __FILE__, __LINE__);
748 } else { /* Object Found */
750 GETSIZE(objsize, header);
751 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
752 if((sendbuffer = calloc(1, size)) == NULL) {
753 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
758 *((int *) (sendbuffer + incr)) = size;
760 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
761 incr += sizeof(char);
762 *((unsigned int *)(sendbuffer+incr)) = oid;
763 incr += sizeof(unsigned int);
764 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
766 control = TRANS_PREFETCH_RESPONSE;
767 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
769 printf("Error: %s() in sending prefetch response at %s, %d\n",
770 __func__, __FILE__, __LINE__);
774 /* Calculate the oid corresponding to the offset value */
775 for(i = 0 ; i< numoffset ; i++) {
776 /* Check for arrays */
777 if(TYPE(header) > NUMCLASSES) {
781 int elementsize = classsize[TYPE(header)];
782 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
784 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
787 if((header = mhashSearch(oid)) == NULL) {
788 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
789 if((sendbuffer = calloc(1, size)) == NULL) {
790 printf("Calloc error at %s,%d\n", __FILE__, __LINE__);
795 *((int *) sendbuffer) = size;
796 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
797 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
799 control = TRANS_PREFETCH_RESPONSE;
800 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
802 printf("Error: %s() in sending prefetch response at %s, %d\n",
808 } else {/* Obj Found */
810 GETSIZE(objsize, header);
811 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
812 if((sendbuffer = calloc(1, size)) == NULL) {
813 printf("Calloc error at %s,%d\n", __func__, __FILE__, __LINE__);
818 *((int *) (sendbuffer + incr)) = size;
820 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
821 incr += sizeof(char);
822 *((unsigned int *)(sendbuffer+incr)) = oid;
823 incr += sizeof(unsigned int);
824 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
826 control = TRANS_PREFETCH_RESPONSE;
827 if(sendPrefetchResponse(sd, &control, sendbuffer, &size) != 0) {
829 printf("Error: %s() in sending prefetch response at %s, %d\n",
830 __func__, __FILE__, __LINE__);
845 int sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
848 if((numbytes = send(sd, control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
849 printf("%s() Error: in sending PREFETCH RESPONSE to Coordinator at %s, %d\n", __func__, __FILE__, __LINE__);
854 /* Send the buffer with its size */
855 if((numbytes = send(sd, sendbuffer, *(size), MSG_NOSIGNAL)) < *(size)) {
856 printf("%s() Error: in sending oid found at %s, %d size sent = %d, actual size = %d\n",
857 __func__, __FILE__, __LINE__, numbytes, *(size));
866 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
869 unsigned short newversion;
870 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
872 struct sockaddr_in remoteAddr;
878 oid = *(oidarry + i);
879 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
880 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
883 /* Check to see if versions are same */
885 if ((STATUS(header) & LOCK) != LOCK) {
886 //FIXME make locking atomic
887 STATUS(header) |= LOCK;
888 newversion = header->version;
889 if(newversion == *(versionarry + i)) {
890 //Add to the notify list
891 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
892 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
895 STATUS(header) &= ~(LOCK);
897 STATUS(header) &= ~(LOCK);
898 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
899 perror("processReqNotify():socket()");
902 bzero(&remoteAddr, sizeof(remoteAddr));
903 remoteAddr.sin_family = AF_INET;
904 remoteAddr.sin_port = htons(LISTEN_PORT);
905 remoteAddr.sin_addr.s_addr = htonl(mid);
907 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
908 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
909 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
913 //Send Update notification
914 msg[0] = THREAD_NOTIFY_RESPONSE;
915 *((unsigned int *)&msg[1]) = oid;
916 size = sizeof(unsigned int);
917 *((unsigned short *)(&msg[1]+size)) = newversion;
918 size += sizeof(unsigned short);
919 *((unsigned int *)(&msg[1]+size)) = threadid;
920 bytesSent = send(sd, msg, 1+ 2*sizeof(unsigned int) + sizeof(unsigned short), 0);
922 perror("processReqNotify():send()");
925 } else if (bytesSent != 1 + sizeof(unsigned short) + 2*sizeof(unsigned int)){
926 printf("Error: processReqNotify(): error, sent %d bytes %s, %d\n",
927 bytesSent, __FILE__, __LINE__);