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) {
54 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
55 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
58 objstr_t *mainobjstore;
59 pthread_mutex_t mainobjstore_mutex;
60 pthread_mutex_t lockObjHeader;
61 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
63 sockPoolHashTable_t *transPResponseSocketPool;
65 /* This function initializes the main objects store and creates the
66 * global machine and location lookup table */
69 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
70 /* Initialize attribute for mutex */
71 pthread_mutexattr_init(&mainobjstore_mutex_attr);
72 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
73 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
74 pthread_mutex_init(&lockObjHeader,NULL);
75 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
78 if (lhashCreate(HASH_SIZE, LOADFACTOR))
81 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
84 //Initialize socket pool
85 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
86 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
94 int startlistening() {
96 struct sockaddr_in my_addr;
97 socklen_t addrlength = sizeof(struct sockaddr);
100 listenfd = socket(AF_INET, SOCK_STREAM, 0);
101 if (listenfd == -1) {
106 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
111 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
117 my_addr.sin_family = AF_INET;
118 my_addr.sin_port = htons(LISTEN_PORT);
119 my_addr.sin_addr.s_addr = INADDR_ANY;
120 memset(&(my_addr.sin_zero), '\0', 8);
122 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
127 if (listen(listenfd, BACKLOG) == -1) {
134 /* This function starts the thread to listen on a socket
135 * for tranaction calls */
136 void *dstmListen(void *lfd) {
137 int listenfd=(int)lfd;
139 struct sockaddr_in client_addr;
140 socklen_t addrlength = sizeof(struct sockaddr);
141 pthread_t thread_dstm_accept;
143 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
147 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
148 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
150 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
152 pthread_detach(thread_dstm_accept);
155 /* This function accepts a new connection request, decodes the control message in the connection
156 * and accordingly calls other functions to process new requests */
157 void *dstmAccept(void *acceptfd) {
158 int val, retval, size, sum, sockid;
164 trans_commit_data_t transinfo;
165 unsigned short objType, *versionarry, version;
166 unsigned int *oidarry, numoid, mid, threadid;
167 struct readstruct readbuffer;
170 unsigned int numread=0, nummod=0;
176 /* Receive control messages from other machines */
178 int ret=recv_data_errorcode_buf((int)acceptfd, &readbuffer, &control, sizeof(char));
182 printf("DEBUG -> RECV Error!.. retrying\n");
187 /* Read oid requested and search if available */
188 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
189 while((srcObj = mhashSearch(oid)) == NULL) {
191 if((ret = sched_yield()) != 0) {
192 printf("%s(): error no %d in thread yield\n", __func__, errno);
195 h = (objheader_t *) srcObj;
196 /* If object is write locked, just wait */
197 /* May want to remove at some point */
198 while((*(volatile int *)STATUSPTR(h))<=0)
201 size += sizeof(objheader_t);
202 sockid = (int) acceptfd;
204 ctrl = OBJECT_NOT_FOUND;
205 send_data(sockid, &ctrl, sizeof(char));
208 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
209 *((int *)&msg[1])=size;
210 send_data(sockid, &msg, sizeof(msg));
211 send_data(sockid, h, size);
215 case READ_MULT_REQUEST:
221 case MOVE_MULT_REQUEST:
225 /* Read transaction request */
226 transinfo.objlocked = NULL;
227 transinfo.objnotfound = NULL;
228 transinfo.modptr = NULL;
229 transinfo.numlocked = 0;
230 transinfo.numnotfound = 0;
231 if((val = readClientReq(&transinfo, (int)acceptfd, &readbuffer)) != 0) {
232 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
239 if((val = rangePrefetchReq((int)acceptfd, &readbuffer)) != 0) {
240 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
244 LOGTIME('X',0,0,myrdtsc(),0);
245 if((val = prefetchReq((int)acceptfd, &readbuffer)) != 0) {
246 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
252 case TRANS_PREFETCH_RESPONSE:
254 if((val = getRangePrefetchResponse((int)acceptfd, &readbuffer)) != 0) {
255 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
259 if((val = getPrefetchResponse((int) acceptfd, &readbuffer)) != 0) {
260 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
266 case START_REMOTE_THREAD:
267 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
268 objType = getObjType(oid);
269 startDSMthread(oid, objType);
272 case THREAD_NOTIFY_REQUEST:
273 recv_data_buf((int)acceptfd, &readbuffer, &numoid, sizeof(unsigned int));
274 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
275 if((buffer = calloc(1,size)) == NULL) {
276 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
280 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
282 oidarry = calloc(numoid, sizeof(unsigned int));
283 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
284 size = sizeof(unsigned int) * numoid;
285 versionarry = calloc(numoid, sizeof(unsigned short));
286 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
287 size += sizeof(unsigned short) * numoid;
288 mid = *((unsigned int *)(buffer+size));
289 size += sizeof(unsigned int);
290 threadid = *((unsigned int *)(buffer+size));
291 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
295 case THREAD_NOTIFY_RESPONSE:
296 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
297 if((buffer = calloc(1,size)) == NULL) {
298 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
302 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
304 oid = *((unsigned int *)buffer);
305 size = sizeof(unsigned int);
306 version = *((unsigned short *)(buffer+size));
307 size += sizeof(unsigned short);
308 threadid = *((unsigned int *)(buffer+size));
309 threadNotify(oid,version,threadid);
314 case CHECK_OBJECTS: // check if versions of objects match
315 size = sizeof(odata) - 1;
317 recv_data_buf((int)acceptfd, &readbuffer, ptr+1, size);
318 numread = odata.numread;
319 nummod = odata.nummod;
320 checkObjVersion(&readbuffer, (int) acceptfd, numread, nummod);
324 case CLOSE_CONNECTION:
325 goto closeconnection;
328 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
333 /* Close connection */
334 if (close((int)acceptfd) == -1)
339 /* This function reads the information available in a transaction request
340 * and makes a function call to process the request */
341 int readClientReq(trans_commit_data_t *transinfo, int acceptfd, struct readstruct * readbuffer) {
344 unsigned int *oidmod, oid;
346 objheader_t *headaddr;
347 int sum, i, size, n, val;
351 /* Read fixed_data_t data structure */
352 size = sizeof(fixed) - 1;
353 ptr = (char *)&fixed;
354 fixed.control = TRANS_REQUEST;
355 recv_data_buf((int)acceptfd, readbuffer, ptr+1, size);
357 /* Read list of mids */
358 int mcount = fixed.mcount;
359 size = mcount * sizeof(unsigned int);
360 unsigned int listmid[mcount];
361 ptr = (char *) listmid;
362 recv_data_buf((int)acceptfd, readbuffer, ptr, size);
364 /* Read oid and version tuples for those objects that are not modified in the transaction */
365 int numread = fixed.numread;
366 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
368 if(numread != 0) { //If pile contains more than one object to be read,
369 // keep reading all objects
370 recv_data_buf((int)acceptfd, readbuffer, objread, size);
373 /* Read modified objects */
374 if(fixed.nummod != 0) {
375 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
376 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
379 size = fixed.sum_bytes;
380 recv_data_buf((int)acceptfd, readbuffer, modptr, size);
383 /* Create an array of oids for modified objects */
384 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
385 if (oidmod == NULL) {
386 printf("calloc error %s, %d\n", __FILE__, __LINE__);
389 ptr = (char *) modptr;
390 for(i = 0; i < fixed.nummod; i++) {
391 headaddr = (objheader_t *) ptr;
395 GETSIZE(tmpsize, headaddr);
396 ptr += sizeof(objheader_t) + tmpsize;
399 /*Process the information read */
400 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd, readbuffer)) != 0) {
401 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
417 /* This function processes the Coordinator's transaction request using "handleTransReq"
418 * function and sends a reply to the co-ordinator.
419 * Following this it also receives a new control message from the co-ordinator and processes this message*/
420 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
421 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd, struct readstruct *readbuffer) {
423 char control, sendctrl, retval;
424 objheader_t *tmp_header;
428 /* Send reply to the Coordinator */
429 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
430 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
434 recv_data_buf((int)acceptfd, readbuffer, &control, sizeof(char));
435 /* Process the new control message */
438 if (fixed->nummod > 0)
440 /* Unlock objects that was locked due to this transaction */
441 int useWriteUnlock = 0; //TODO verify is this piece of unlocking code ever used
442 for(i = 0; i< transinfo->numlocked; i++) {
443 if(transinfo->objlocked[i] == -1) {
447 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
448 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
452 write_unlock(STATUSPTR(header));
454 read_unlock(STATUSPTR(header));
460 /* Invoke the transCommit process() */
461 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
462 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
464 if (transinfo->objlocked != NULL) {
465 free(transinfo->objlocked);
467 if (transinfo->objnotfound != NULL) {
468 free(transinfo->objnotfound);
474 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
478 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol control = %d %s, %d\n", control, __FILE__, __LINE__);
479 //TODO Use fixed.trans_id TID since Client may have died
484 if (transinfo->objlocked != NULL) {
485 free(transinfo->objlocked);
487 if (transinfo->objnotfound != NULL) {
488 free(transinfo->objnotfound);
494 /* This function increments counters while running a voting decision on all objects involved
495 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
496 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
498 unsigned short version;
499 char control = 0, *ptr;
501 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
502 objheader_t *headptr;
504 /* Counters and arrays to formulate decision on control message to be sent */
505 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
506 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
507 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
508 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
509 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
511 /* modptr points to the beginning of the object store
512 * created at the Pariticipant.
513 * Object store holds the modified objects involved in the transaction request */
514 ptr = (char *) modptr;
518 /* Process each oid in the machine pile/ group per thread */
519 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
520 if (i < fixed->numread) { //Objs only read and not modified
521 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
523 oid = *((unsigned int *)(objread + incr));
525 incr += sizeof(unsigned int);
526 version = *((unsigned short *)(objread + incr));
527 retval=getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
528 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
529 } else { //Objs modified
530 if(i == fixed->numread) {
531 oidlocked[objlocked++] = -1;
534 headptr = (objheader_t *) ptr;
536 version = headptr->version;
537 GETSIZE(tmpsize, headptr);
538 ptr += sizeof(objheader_t) + tmpsize;
539 retval=getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
540 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
541 &numBytes, &control, oid, version);
543 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
544 //unlock objects as soon versions mismatch or locks cannot be acquired)
546 int useWriteUnlock = 0;
547 for(j = 0; j < objlocked; j++) {
548 if(oidlocked[j] == -1) {
552 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
553 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
557 write_unlock(STATUSPTR(headptr));
559 read_unlock(STATUSPTR(headptr));
569 //go through rest of the objects for version mismatches
570 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
572 procRestObjs(objread, ptr, i, fixed->numread, fixed->nummod, oidnotfound, oidvernotmatch, &objnotfound, &objvernotmatch, &v_nomatch, &numBytes);
575 /* send TRANS_DISAGREE and objs*/
578 char *objs = calloc(1, numBytes);
580 for(j = 0; j<objvernotmatch; j++) {
581 objheader_t *header = mhashSearch(oidvernotmatch[j]);
583 GETSIZE(size, header);
584 size += sizeof(objheader_t);
585 memcpy(objs+offset, header, size);
591 int useWriteUnlock = 0;
592 for(j = 0; j < objlocked; j++) {
593 if(oidlocked[j] == -1) {
597 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
598 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
602 write_unlock(STATUSPTR(headptr));
604 read_unlock(STATUSPTR(headptr));
610 control=TRANS_DISAGREE;
611 send_data(acceptfd, &control, sizeof(char));
613 send_data(acceptfd, &numBytes, sizeof(int));
614 send_data(acceptfd, objs, numBytes);
615 transinfo->objvernotmatch = oidvernotmatch;
616 transinfo->numvernotmatch = objvernotmatch;
618 free(transinfo->objvernotmatch);
623 /* Decide what control message to send to Coordinator */
624 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
625 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
626 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
632 /* Update Commit info for objects that are read */
633 char getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
634 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
635 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
636 char *control, unsigned int oid, unsigned short version) {
638 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
640 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
641 /* Save the oids not found and number of oids not found for later use */
642 oidnotfound[*objnotfound] = oid;
644 *control = TRANS_DISAGREE;
645 } else { /* If Obj found in machine (i.e. has not moved) */
646 /* Check if Obj is locked by any previous transaction */
647 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
648 if (version == ((objheader_t *)mobj)->version) { /* match versions */
650 *control = TRANS_AGREE;
651 } else { /* If versions don't match ...HARD ABORT */
653 oidvernotmatch[*objvernotmatch] = oid;
657 size += sizeof(objheader_t);
659 /* Send TRANS_DISAGREE to Coordinator */
660 *control = TRANS_DISAGREE;
662 //Keep track of oid locked
663 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
664 } else { //we are locked
665 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
667 *control=TRANS_SOFT_ABORT;
668 } else { /* If versions don't match ...HARD ABORT */
670 oidvernotmatch[*objvernotmatch] = oid;
674 size += sizeof(objheader_t);
676 *control = TRANS_DISAGREE;
683 /* Update Commit info for objects that are read */
684 char getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
685 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
686 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
688 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
689 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
690 /* Save the oids not found and number of oids not found for later use */
691 oidnotfound[*objnotfound] = oid;
693 *control = TRANS_DISAGREE;
694 } else { /* If Obj found in machine (i.e. has not moved) */
695 /* Check if Obj is locked by any previous transaction */
696 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
697 if (version == ((objheader_t *)mobj)->version) { /* match versions */
699 *control=TRANS_AGREE;
700 } else { /* If versions don't match ...HARD ABORT */
702 oidvernotmatch[(*objvernotmatch)++] = oid;
705 size += sizeof(objheader_t);
707 /* Send TRANS_DISAGREE to Coordinator */
708 *control = TRANS_DISAGREE;
710 //Keep track of oid locked
711 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
712 } else { /* Some other transaction has aquired a write lock on this object */
713 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
715 *control=TRANS_SOFT_ABORT;
716 } else { /* If versions don't match ...HARD ABORT */
718 oidvernotmatch[*objvernotmatch] = oid;
722 size += sizeof(objheader_t);
724 *control = TRANS_DISAGREE;
731 void procRestObjs(char *objread,
736 unsigned int *oidnotfound,
737 unsigned int *oidvernotmatch,
744 unsigned short version;
746 /* Process each oid in the machine pile/ group per thread */
747 for (i = index; i < numread+nummod; i++) {
748 if (i < numread) { //Objs only read and not modified
749 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
751 oid = *((unsigned int *)(objread + incr));
752 incr += sizeof(unsigned int);
753 version = *((unsigned short *)(objread + incr));
754 } else { //Objs modified
755 objheader_t *headptr;
756 headptr = (objheader_t *) objmod;
758 version = headptr->version;
760 GETSIZE(tmpsize, headptr);
761 objmod += sizeof(objheader_t) + tmpsize;
763 processVerNoMatch(oidnotfound,
775 void processVerNoMatch(unsigned int *oidnotfound,
776 unsigned int *oidvernotmatch,
782 unsigned short version) {
784 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
786 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
787 /* Save the oids not found and number of oids not found for later use */
788 oidnotfound[*objnotfound] = oid;
790 } else { /* If Obj found in machine (i.e. has not moved) */
791 /* Check if Obj is locked by any previous transaction */
792 //if (!write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
793 if (version != ((objheader_t *)mobj)->version) { /* match versions */
795 oidvernotmatch[*objvernotmatch] = oid;
799 size += sizeof(objheader_t);
805 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
806 * to send to Coordinator based on the votes of oids involved in the transaction */
807 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
808 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
809 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
813 /* Condition to send TRANS_AGREE */
814 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
815 control = TRANS_AGREE;
816 /* Send control message */
817 send_data(acceptfd, &control, sizeof(char));
819 /* Condition to send TRANS_SOFT_ABORT */
820 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
821 control = TRANS_SOFT_ABORT;
823 /* Send control message */
824 send_data(acceptfd, &control, sizeof(char));
826 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
827 if(*(objnotfound) != 0) {
829 msg[0] = *(objnotfound);
830 send_data(acceptfd, &msg, sizeof(int));
831 int size = sizeof(unsigned int)* *(objnotfound);
832 send_data(acceptfd, oidnotfound, size);
836 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
837 * if Participant receives a TRANS_COMMIT */
838 transinfo->objlocked = oidlocked;
839 transinfo->objnotfound = oidnotfound;
840 transinfo->modptr = modptr;
841 transinfo->numlocked = *(objlocked);
842 transinfo->numnotfound = *(objnotfound);
846 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
847 * addresses in lookup table and also changes version number
848 * Sends an ACK back to Coordinator */
849 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
851 objheader_t *newheader;
852 int i = 0, offset = 0;
856 /* Process each modified object saved in the mainobject store */
857 for(i = 0; i < nummod; i++) {
858 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
859 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
862 GETSIZE(tmpsize,header);
865 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
866 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
868 dst->___cachedCode___=src->___cachedCode___;
869 dst->___cachedHash___=src->___cachedHash___;
870 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
876 header->version += 1;
877 /* If threads are waiting on this object to be updated, notify them */
878 if(header->notifylist != NULL) {
879 notifyAll(&header->notifylist, OID(header), header->version);
881 offset += sizeof(objheader_t) + tmpsize;
887 /* Unlock locked objects */
888 int useWriteUnlock = 0;
889 for(i = 0; i < numlocked; i++) {
890 if(oidlocked[i] == -1) {
894 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
895 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
900 write_unlock(STATUSPTR(header));
902 read_unlock(STATUSPTR(header));
905 //TODO Update location lookup table
909 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
910 * Looks for the objects to be prefetched in the main object store.
911 * If objects are not found then record those and if objects are found
912 * then use offset values to prefetch references to other objects */
913 int prefetchReq(int acceptfd, struct readstruct * readbuffer) {
914 int i, size, objsize, numoffset = 0, gid=0;
916 char *recvbuffer, control;
917 unsigned int oid, mid=-1;
920 struct writestruct writebuffer;
924 recv_data_buf((int)acceptfd, readbuffer, &numoffset, sizeof(int));
927 recv_data_buf((int)acceptfd, readbuffer, &oidmid, 2*sizeof(unsigned int));
929 if (mid != oidmid.mid) {
931 forcesend_buf(sd, &writebuffer, NULL, 0);
932 freeSockWithLock(transPResponseSocketPool, mid, sd);
935 sd = getSockWithLock(transPResponseSocketPool, mid);
936 writebuffer.offset=0;
938 short offsetarry[numoffset];
939 recv_data_buf((int)acceptfd, readbuffer, &gid, sizeof(int));
940 recv_data_buf((int) acceptfd, readbuffer, offsetarry, numoffset*sizeof(short));
941 LOGTIME('A',oid,0,myrdtsc(),gid); //after recv the entire prefetch request
943 /*Process each oid */
944 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
945 /* Save the oids not found in buffer for later use */
946 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int);
947 char sendbuffer[size+1];
948 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
949 *((int *) (sendbuffer+sizeof(char))) = size;
950 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
951 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char)+sizeof(char))) = oid;
952 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
953 send_buf(sd, &writebuffer, sendbuffer, size+1);
954 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
955 } else { /* Object Found */
957 GETSIZE(objsize, header);
958 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
959 char sendbuffer[size+1];
960 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
961 *((int *)(sendbuffer + incr)) = size;
963 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
964 incr += sizeof(char);
965 *((unsigned int *)(sendbuffer+incr)) = oid;
966 incr += sizeof(unsigned int);
967 *((int *)(sendbuffer+incr)) = gid;
969 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
970 send_buf(sd, &writebuffer, sendbuffer, size+1);
971 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
972 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
974 /* Calculate the oid corresponding to the offset value */
975 for(i = 0; i< numoffset; i++) {
976 /* Check for arrays */
977 if(TYPE(header) >= NUMCLASSES) {
978 int elementsize = classsize[TYPE(header)];
979 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
980 unsigned short length = ao->___length___;
981 /* Check if array out of bounds */
982 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
985 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
987 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
990 /* Don't continue if we hit a NULL pointer */
994 LOGTIME('B',oid,0,myrdtsc(),gid); //send next oid found from prefetch request
996 if((header = mhashSearch(oid)) == NULL) {
997 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int);
998 char sendbuffer[size+1];
999 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
1000 *((int *) (sendbuffer+1)) = size;
1001 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
1002 *((unsigned int *)(sendbuffer + sizeof(char)+sizeof(int) + sizeof(char))) = oid;
1003 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
1005 send_buf(sd, &writebuffer, sendbuffer, size+1);
1006 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
1008 } else { /* Obj Found */
1010 GETSIZE(objsize, header);
1011 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
1012 char sendbuffer[size+1];
1013 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
1014 *((int *)(sendbuffer + incr)) = size;
1015 incr += sizeof(int);
1016 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
1017 incr += sizeof(char);
1018 *((unsigned int *)(sendbuffer+incr)) = oid;
1019 incr += sizeof(unsigned int);
1020 *((int *)(sendbuffer+incr)) = gid;
1021 incr += sizeof(int);
1022 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
1023 send_buf(sd, &writebuffer, sendbuffer, size+1);
1024 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
1025 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
1033 forcesend_buf(sd, &writebuffer, NULL, 0);
1034 freeSockWithLock(transPResponseSocketPool, mid, sd);
1039 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
1040 send_data(sd, control, sizeof(char));
1041 /* Send the buffer with its size */
1042 int length = *(size);
1043 send_data(sd, sendbuffer, length);
1046 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
1047 objheader_t *header;
1049 unsigned short newversion;
1050 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
1052 struct sockaddr_in remoteAddr;
1058 oid = *(oidarry + i);
1059 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
1060 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1063 /* Check to see if versions are same */
1065 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
1066 newversion = header->version;
1067 if(newversion == *(versionarry + i)) {
1068 //Add to the notify list
1069 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
1070 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
1073 write_unlock(STATUSPTR(header));
1075 write_unlock(STATUSPTR(header));
1076 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1077 perror("processReqNotify():socket()");
1080 bzero(&remoteAddr, sizeof(remoteAddr));
1081 remoteAddr.sin_family = AF_INET;
1082 remoteAddr.sin_port = htons(LISTEN_PORT);
1083 remoteAddr.sin_addr.s_addr = htonl(mid);
1085 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1086 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
1087 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1092 //Send Update notification
1093 msg[0] = THREAD_NOTIFY_RESPONSE;
1094 *((unsigned int *)&msg[1]) = oid;
1095 size = sizeof(unsigned int);
1096 *((unsigned short *)(&msg[1]+size)) = newversion;
1097 size += sizeof(unsigned short);
1098 *((unsigned int *)(&msg[1]+size)) = threadid;
1099 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
1100 send_data(sd, msg, size);