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>
6 #include "altmlookup.h"
8 #include "threadnotify.h"
15 #include "readstruct.h"
16 #include "debugmacro.h"
21 #define BACKLOG 10 //max pending connections
22 #define RECEIVE_BUFFER_SIZE 2048
24 extern int classsize[];
25 extern int numHostsInSystem;
26 extern pthread_mutex_t notifymutex;
27 extern unsigned long long clockoffset;
28 long long startreq, endreq, diff;
32 extern char bigarray1[6*1024*1024];
33 extern unsigned int bigarray2[6*1024*1024];
34 extern unsigned int bigarray3[6*1024*1024];
35 extern long long bigarray4[6*1024*1024];
36 extern int bigarray5[6*1024*1024];
38 #define LOGTIME(x,y,z,a,b) {\
48 #define LOGTIME(x,y,z,a,b)
52 long long myrdtsc(void)
55 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
56 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
59 objstr_t *mainobjstore;
60 pthread_mutex_t mainobjstore_mutex;
61 pthread_mutex_t lockObjHeader;
62 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
64 sockPoolHashTable_t *transPResponseSocketPool;
66 /* This function initializes the main objects store and creates the
67 * global machine and location lookup table */
70 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
71 /* Initialize attribute for mutex */
72 pthread_mutexattr_init(&mainobjstore_mutex_attr);
73 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
74 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
75 pthread_mutex_init(&lockObjHeader,NULL);
76 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
79 if (lhashCreate(HASH_SIZE, LOADFACTOR))
82 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
85 //Initialize socket pool
86 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
87 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
95 int startlistening() {
97 struct sockaddr_in my_addr;
98 socklen_t addrlength = sizeof(struct sockaddr);
101 listenfd = socket(AF_INET, SOCK_STREAM, 0);
102 if (listenfd == -1) {
107 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
112 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
118 my_addr.sin_family = AF_INET;
119 my_addr.sin_port = htons(LISTEN_PORT);
120 my_addr.sin_addr.s_addr = INADDR_ANY;
121 memset(&(my_addr.sin_zero), '\0', 8);
123 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
128 if (listen(listenfd, BACKLOG) == -1) {
135 /* This function starts the thread to listen on a socket
136 * for tranaction calls */
137 void *dstmListen(void *lfd) {
138 int listenfd=(int)lfd;
140 struct sockaddr_in client_addr;
141 socklen_t addrlength = sizeof(struct sockaddr);
142 pthread_t thread_dstm_accept;
144 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
148 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
149 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
151 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
153 pthread_detach(thread_dstm_accept);
156 /* This function accepts a new connection request, decodes the control message in the connection
157 * and accordingly calls other functions to process new requests */
158 void *dstmAccept(void *acceptfd) {
159 int val, retval, size, sum, sockid;
165 trans_commit_data_t transinfo;
166 unsigned short objType, *versionarry, version;
167 unsigned int *oidarry, numoid, mid, threadid;
168 struct readstruct readbuffer;
171 unsigned int numread=0, nummod=0;
177 /* Receive control messages from other machines */
179 int ret=recv_data_errorcode_buf((int)acceptfd, &readbuffer, &control, sizeof(char));
183 printf("DEBUG -> RECV Error!.. retrying\n");
188 /* Read oid requested and search if available */
189 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
190 while((srcObj = mhashSearch(oid)) == NULL) {
192 if((ret = sched_yield()) != 0) {
193 printf("%s(): error no %d in thread yield\n", __func__, errno);
196 h = (objheader_t *) srcObj;
198 size += sizeof(objheader_t);
199 sockid = (int) acceptfd;
201 ctrl = OBJECT_NOT_FOUND;
202 send_data(sockid, &ctrl, sizeof(char));
205 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
206 *((int *)&msg[1])=size;
207 send_data(sockid, &msg, sizeof(msg));
208 send_data(sockid, h, size);
212 case READ_MULT_REQUEST:
218 case MOVE_MULT_REQUEST:
222 /* Read transaction request */
223 transinfo.objlocked = NULL;
224 transinfo.objnotfound = NULL;
225 transinfo.modptr = NULL;
226 transinfo.numlocked = 0;
227 transinfo.numnotfound = 0;
228 if((val = readClientReq(&transinfo, (int)acceptfd, &readbuffer)) != 0) {
229 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
236 if((val = rangePrefetchReq((int)acceptfd, &readbuffer)) != 0) {
237 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
241 LOGTIME('X',0,0,myrdtsc(),0);
242 if((val = prefetchReq((int)acceptfd, &readbuffer)) != 0) {
243 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
249 case TRANS_PREFETCH_RESPONSE:
251 if((val = getRangePrefetchResponse((int)acceptfd, &readbuffer)) != 0) {
252 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
256 if((val = getPrefetchResponse((int) acceptfd, &readbuffer)) != 0) {
257 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
263 case START_REMOTE_THREAD:
264 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
265 objType = getObjType(oid);
266 startDSMthread(oid, objType);
269 case THREAD_NOTIFY_REQUEST:
270 recv_data_buf((int)acceptfd, &readbuffer, &numoid, sizeof(unsigned int));
271 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
272 if((buffer = calloc(1,size)) == NULL) {
273 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
277 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
279 oidarry = calloc(numoid, sizeof(unsigned int));
280 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
281 size = sizeof(unsigned int) * numoid;
282 versionarry = calloc(numoid, sizeof(unsigned short));
283 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
284 size += sizeof(unsigned short) * numoid;
285 mid = *((unsigned int *)(buffer+size));
286 size += sizeof(unsigned int);
287 threadid = *((unsigned int *)(buffer+size));
288 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
292 case THREAD_NOTIFY_RESPONSE:
293 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
294 if((buffer = calloc(1,size)) == NULL) {
295 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
299 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
301 oid = *((unsigned int *)buffer);
302 size = sizeof(unsigned int);
303 version = *((unsigned short *)(buffer+size));
304 size += sizeof(unsigned short);
305 threadid = *((unsigned int *)(buffer+size));
306 threadNotify(oid,version,threadid);
311 case CHECK_OBJECTS: // check if versions of objects match
312 size = sizeof(odata) - 1;
314 recv_data_buf((int)acceptfd, &readbuffer, ptr+1, size);
315 numread = odata.numread;
316 nummod = odata.nummod;
317 checkObjVersion(&readbuffer, (int) acceptfd, numread, nummod);
321 case CLOSE_CONNECTION:
322 goto closeconnection;
325 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
330 /* Close connection */
331 if (close((int)acceptfd) == -1)
336 /* This function reads the information available in a transaction request
337 * and makes a function call to process the request */
338 int readClientReq(trans_commit_data_t *transinfo, int acceptfd, struct readstruct * readbuffer) {
341 unsigned int *oidmod, oid;
343 objheader_t *headaddr;
344 int sum, i, size, n, val;
348 /* Read fixed_data_t data structure */
349 size = sizeof(fixed) - 1;
350 ptr = (char *)&fixed;
351 fixed.control = TRANS_REQUEST;
352 recv_data_buf((int)acceptfd, readbuffer, ptr+1, size);
354 /* Read list of mids */
355 int mcount = fixed.mcount;
356 size = mcount * sizeof(unsigned int);
357 unsigned int listmid[mcount];
358 ptr = (char *) listmid;
359 recv_data_buf((int)acceptfd, readbuffer, ptr, size);
361 /* Read oid and version tuples for those objects that are not modified in the transaction */
362 int numread = fixed.numread;
363 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
365 if(numread != 0) { //If pile contains more than one object to be read,
366 // keep reading all objects
367 recv_data_buf((int)acceptfd, readbuffer, objread, size);
370 /* Read modified objects */
371 if(fixed.nummod != 0) {
372 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
373 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
376 size = fixed.sum_bytes;
377 recv_data_buf((int)acceptfd, readbuffer, modptr, size);
380 /* Create an array of oids for modified objects */
381 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
382 if (oidmod == NULL) {
383 printf("calloc error %s, %d\n", __FILE__, __LINE__);
386 ptr = (char *) modptr;
387 for(i = 0 ; i < fixed.nummod; i++) {
389 headaddr = (objheader_t *) ptr;
392 GETSIZE(tmpsize, headaddr);
393 ptr += sizeof(objheader_t) + tmpsize;
396 /*Process the information read */
397 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd, readbuffer)) != 0) {
398 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
414 /* This function processes the Coordinator's transaction request using "handleTransReq"
415 * function and sends a reply to the co-ordinator.
416 * Following this it also receives a new control message from the co-ordinator and processes this message*/
417 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
418 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd, struct readstruct *readbuffer) {
420 char control, sendctrl, retval;
421 objheader_t *tmp_header;
425 /* Send reply to the Coordinator */
426 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
427 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
431 recv_data_buf((int)acceptfd, readbuffer, &control, sizeof(char));
432 /* Process the new control message */
435 if (fixed->nummod > 0)
437 /* Unlock objects that was locked due to this transaction */
438 int useWriteUnlock = 0; //TODO verify is this piece of unlocking code ever used
439 for(i = 0; i< transinfo->numlocked; i++) {
440 if(transinfo->objlocked[i] == -1) {
444 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
445 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
449 write_unlock(STATUSPTR(header));
451 read_unlock(STATUSPTR(header));
457 /* Invoke the transCommit process() */
458 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
459 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
461 if (transinfo->objlocked != NULL) {
462 free(transinfo->objlocked);
464 if (transinfo->objnotfound != NULL) {
465 free(transinfo->objnotfound);
471 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
475 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol control = %d %s, %d\n", control, __FILE__, __LINE__);
476 //TODO Use fixed.trans_id TID since Client may have died
481 if (transinfo->objlocked != NULL) {
482 free(transinfo->objlocked);
484 if (transinfo->objnotfound != NULL) {
485 free(transinfo->objnotfound);
491 /* This function increments counters while running a voting decision on all objects involved
492 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
493 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
495 unsigned short version;
496 char control = 0, *ptr;
498 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
499 objheader_t *headptr;
501 /* Counters and arrays to formulate decision on control message to be sent */
502 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
503 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
504 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
505 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
506 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
508 /* modptr points to the beginning of the object store
509 * created at the Pariticipant.
510 * Object store holds the modified objects involved in the transaction request */
511 ptr = (char *) modptr;
515 /* Process each oid in the machine pile/ group per thread */
516 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
517 if (i < fixed->numread) { //Objs only read and not modified
518 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
520 oid = *((unsigned int *)(objread + incr));
521 incr += sizeof(unsigned int);
522 version = *((unsigned short *)(objread + incr));
523 retval=getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
524 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
525 } else { //Objs modified
526 if(i == fixed->numread) {
527 oidlocked[objlocked++] = -1;
530 headptr = (objheader_t *) ptr;
532 version = headptr->version;
533 GETSIZE(tmpsize, headptr);
534 ptr += sizeof(objheader_t) + tmpsize;
535 retval=getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
536 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
537 &numBytes, &control, oid, version);
539 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
540 //unlock objects as soon versions mismatch or locks cannot be acquired)
542 int useWriteUnlock = 0;
543 for(j = 0; j < objlocked; j++) {
544 if(oidlocked[j] == -1) {
548 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
549 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
553 write_unlock(STATUSPTR(headptr));
555 read_unlock(STATUSPTR(headptr));
565 //go through rest of the objects for version mismatches
566 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
568 procRestObjs(objread, ptr, i, fixed->numread, fixed->nummod, oidnotfound, oidvernotmatch, &objnotfound, &objvernotmatch, &v_nomatch, &numBytes);
571 /* send TRANS_DISAGREE and objs*/
574 char *objs = calloc(1, numBytes);
576 for(j = 0; j<objvernotmatch; j++) {
577 objheader_t *header = mhashSearch(oidvernotmatch[j]);
579 GETSIZE(size, header);
580 size += sizeof(objheader_t);
581 memcpy(objs+offset, header, size);
587 int useWriteUnlock = 0;
588 for(j = 0; j < objlocked; j++) {
589 if(oidlocked[j] == -1) {
593 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
594 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
598 write_unlock(STATUSPTR(headptr));
600 read_unlock(STATUSPTR(headptr));
606 control=TRANS_DISAGREE;
607 send_data(acceptfd, &control, sizeof(char));
609 send_data(acceptfd, &numBytes, sizeof(int));
610 send_data(acceptfd, objs, numBytes);
611 transinfo->objvernotmatch = oidvernotmatch;
612 transinfo->numvernotmatch = objvernotmatch;
614 free(transinfo->objvernotmatch);
619 /* Decide what control message to send to Coordinator */
620 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
621 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
622 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
628 /* Update Commit info for objects that are read */
629 char getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
630 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
631 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
632 char *control, unsigned int oid, unsigned short version) {
634 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
636 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
637 /* Save the oids not found and number of oids not found for later use */
638 oidnotfound[*objnotfound] = oid;
640 *control = TRANS_DISAGREE;
641 } else { /* If Obj found in machine (i.e. has not moved) */
642 /* Check if Obj is locked by any previous transaction */
643 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
644 if (version == ((objheader_t *)mobj)->version) { /* match versions */
646 *control = TRANS_AGREE;
647 } else { /* If versions don't match ...HARD ABORT */
649 oidvernotmatch[*objvernotmatch] = oid;
653 size += sizeof(objheader_t);
655 /* Send TRANS_DISAGREE to Coordinator */
656 *control = TRANS_DISAGREE;
658 //Keep track of oid locked
659 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
660 } else { //we are locked
661 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
663 *control=TRANS_SOFT_ABORT;
664 } else { /* If versions don't match ...HARD ABORT */
666 oidvernotmatch[*objvernotmatch] = oid;
670 size += sizeof(objheader_t);
672 *control = TRANS_DISAGREE;
679 /* Update Commit info for objects that are read */
680 char getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
681 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
682 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
684 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
685 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
686 /* Save the oids not found and number of oids not found for later use */
687 oidnotfound[*objnotfound] = oid;
689 *control = TRANS_DISAGREE;
690 } else { /* If Obj found in machine (i.e. has not moved) */
691 /* Check if Obj is locked by any previous transaction */
692 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
693 if (version == ((objheader_t *)mobj)->version) { /* match versions */
695 *control=TRANS_AGREE;
696 } else { /* If versions don't match ...HARD ABORT */
698 oidvernotmatch[(*objvernotmatch)++] = oid;
701 size += sizeof(objheader_t);
703 /* Send TRANS_DISAGREE to Coordinator */
704 *control = TRANS_DISAGREE;
706 //Keep track of oid locked
707 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
708 } else { /* Some other transaction has aquired a write lock on this object */
709 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
711 *control=TRANS_SOFT_ABORT;
712 } else { /* If versions don't match ...HARD ABORT */
714 oidvernotmatch[*objvernotmatch] = oid;
718 size += sizeof(objheader_t);
720 *control = TRANS_DISAGREE;
727 void procRestObjs(char *objread,
732 unsigned int *oidnotfound,
733 unsigned int *oidvernotmatch,
740 unsigned short version;
742 /* Process each oid in the machine pile/ group per thread */
743 for (i = index; i < numread+nummod; i++) {
744 if (i < numread) { //Objs only read and not modified
745 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
747 oid = *((unsigned int *)(objread + incr));
748 incr += sizeof(unsigned int);
749 version = *((unsigned short *)(objread + incr));
750 } else { //Objs modified
751 objheader_t *headptr;
752 headptr = (objheader_t *) objmod;
754 version = headptr->version;
756 GETSIZE(tmpsize, headptr);
757 objmod += sizeof(objheader_t) + tmpsize;
759 processVerNoMatch(oidnotfound,
771 void processVerNoMatch(unsigned int *oidnotfound,
772 unsigned int *oidvernotmatch,
778 unsigned short version) {
780 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
782 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
783 /* Save the oids not found and number of oids not found for later use */
784 oidnotfound[*objnotfound] = oid;
786 } else { /* If Obj found in machine (i.e. has not moved) */
787 /* Check if Obj is locked by any previous transaction */
788 //if (!write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
789 if (version != ((objheader_t *)mobj)->version) { /* match versions */
791 oidvernotmatch[*objvernotmatch] = oid;
795 size += sizeof(objheader_t);
801 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
802 * to send to Coordinator based on the votes of oids involved in the transaction */
803 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
804 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
805 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
809 /* Condition to send TRANS_AGREE */
810 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
811 control = TRANS_AGREE;
812 /* Send control message */
813 send_data(acceptfd, &control, sizeof(char));
815 /* Condition to send TRANS_SOFT_ABORT */
816 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
817 control = TRANS_SOFT_ABORT;
819 /* Send control message */
820 send_data(acceptfd, &control, sizeof(char));
822 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
823 if(*(objnotfound) != 0) {
825 msg[0] = *(objnotfound);
826 send_data(acceptfd, &msg, sizeof(int));
827 int size = sizeof(unsigned int)* *(objnotfound);
828 send_data(acceptfd, oidnotfound, size);
832 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
833 * if Participant receives a TRANS_COMMIT */
834 transinfo->objlocked = oidlocked;
835 transinfo->objnotfound = oidnotfound;
836 transinfo->modptr = modptr;
837 transinfo->numlocked = *(objlocked);
838 transinfo->numnotfound = *(objnotfound);
842 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
843 * addresses in lookup table and also changes version number
844 * Sends an ACK back to Coordinator */
845 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
847 objheader_t *newheader;
848 int i = 0, offset = 0;
852 /* Process each modified object saved in the mainobject store */
853 for(i = 0; i < nummod; i++) {
854 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
855 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
858 GETSIZE(tmpsize,header);
861 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
862 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
864 dst->___cachedCode___=src->___cachedCode___;
865 dst->___cachedHash___=src->___cachedHash___;
866 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
868 header->version += 1;
869 /* If threads are waiting on this object to be updated, notify them */
870 if(header->notifylist != NULL) {
871 notifyAll(&header->notifylist, OID(header), header->version);
873 offset += sizeof(objheader_t) + tmpsize;
879 /* Unlock locked objects */
880 int useWriteUnlock = 0;
881 for(i = 0; i < numlocked; i++) {
882 if(oidlocked[i] == -1) {
886 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
887 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
892 write_unlock(STATUSPTR(header));
894 read_unlock(STATUSPTR(header));
897 //TODO Update location lookup table
901 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
902 * Looks for the objects to be prefetched in the main object store.
903 * If objects are not found then record those and if objects are found
904 * then use offset values to prefetch references to other objects */
905 int prefetchReq(int acceptfd, struct readstruct * readbuffer) {
906 int i, size, objsize, numoffset = 0, gid=0;
908 char *recvbuffer, control;
909 unsigned int oid, mid=-1;
912 struct writestruct writebuffer;
916 recv_data_buf((int)acceptfd, readbuffer, &numoffset, sizeof(int));
919 recv_data_buf((int)acceptfd, readbuffer, &oidmid, 2*sizeof(unsigned int));
921 if (mid != oidmid.mid) {
923 forcesend_buf(sd, &writebuffer, NULL, 0);
924 freeSockWithLock(transPResponseSocketPool, mid, sd);
927 sd = getSockWithLock(transPResponseSocketPool, mid);
928 writebuffer.offset=0;
930 short offsetarry[numoffset];
931 recv_data_buf((int)acceptfd, readbuffer, &gid, sizeof(int));
932 recv_data_buf((int) acceptfd, readbuffer, offsetarry, numoffset*sizeof(short));
933 LOGTIME('A',oid ,0,myrdtsc(),gid); //after recv the entire prefetch request
935 /*Process each oid */
936 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
937 /* Save the oids not found in buffer for later use */
938 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
939 char sendbuffer[size+1];
940 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
941 *((int *) (sendbuffer+sizeof(char))) = size;
942 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
943 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char)+sizeof(char))) = oid;
944 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
945 send_buf(sd, &writebuffer, sendbuffer, size+1);
946 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
947 } else { /* Object Found */
949 GETSIZE(objsize, header);
950 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
951 char sendbuffer[size+1];
952 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
953 *((int *)(sendbuffer + incr)) = size;
955 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
956 incr += sizeof(char);
957 *((unsigned int *)(sendbuffer+incr)) = oid;
958 incr += sizeof(unsigned int);
959 *((int *)(sendbuffer+incr)) = gid;
961 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
962 send_buf(sd, &writebuffer, sendbuffer, size+1);
963 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
964 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
966 /* Calculate the oid corresponding to the offset value */
967 for(i = 0 ; i< numoffset ; i++) {
968 /* Check for arrays */
969 if(TYPE(header) >= NUMCLASSES) {
970 int elementsize = classsize[TYPE(header)];
971 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
972 unsigned short length = ao->___length___;
973 /* Check if array out of bounds */
974 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
977 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
979 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
982 /* Don't continue if we hit a NULL pointer */
986 LOGTIME('B',oid,0,myrdtsc(),gid); //send next oid found from prefetch request
988 if((header = mhashSearch(oid)) == NULL) {
989 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
990 char sendbuffer[size+1];
991 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
992 *((int *) (sendbuffer+1)) = size;
993 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
994 *((unsigned int *)(sendbuffer + sizeof(char)+sizeof(int) + sizeof(char))) = oid;
995 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
997 send_buf(sd, &writebuffer, sendbuffer, size+1);
998 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
1000 } else { /* Obj Found */
1002 GETSIZE(objsize, header);
1003 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
1004 char sendbuffer[size+1];
1005 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
1006 *((int *)(sendbuffer + incr)) = size;
1007 incr += sizeof(int);
1008 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
1009 incr += sizeof(char);
1010 *((unsigned int *)(sendbuffer+incr)) = oid;
1011 incr += sizeof(unsigned int);
1012 *((int *)(sendbuffer+incr)) = gid;
1013 incr += sizeof(int);
1014 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
1015 send_buf(sd, &writebuffer, sendbuffer, size+1);
1016 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
1017 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
1025 forcesend_buf(sd, &writebuffer, NULL, 0);
1026 freeSockWithLock(transPResponseSocketPool, mid, sd);
1031 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
1032 send_data(sd, control, sizeof(char));
1033 /* Send the buffer with its size */
1034 int length = *(size);
1035 send_data(sd, sendbuffer, length);
1038 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
1039 objheader_t *header;
1041 unsigned short newversion;
1042 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
1044 struct sockaddr_in remoteAddr;
1050 oid = *(oidarry + i);
1051 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
1052 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1055 /* Check to see if versions are same */
1057 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
1058 newversion = header->version;
1059 if(newversion == *(versionarry + i)) {
1060 //Add to the notify list
1061 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
1062 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
1065 write_unlock(STATUSPTR(header));
1067 write_unlock(STATUSPTR(header));
1068 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1069 perror("processReqNotify():socket()");
1072 bzero(&remoteAddr, sizeof(remoteAddr));
1073 remoteAddr.sin_family = AF_INET;
1074 remoteAddr.sin_port = htons(LISTEN_PORT);
1075 remoteAddr.sin_addr.s_addr = htonl(mid);
1077 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1078 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
1079 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1084 //Send Update notification
1085 msg[0] = THREAD_NOTIFY_RESPONSE;
1086 *((unsigned int *)&msg[1]) = oid;
1087 size = sizeof(unsigned int);
1088 *((unsigned short *)(&msg[1]+size)) = newversion;
1089 size += sizeof(unsigned short);
1090 *((unsigned int *)(&msg[1]+size)) = threadid;
1091 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
1092 send_data(sd, msg, size);