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;
197 /* If object is write locked, just wait */
198 /* May want to remove at some point */
199 while((*(volatile int *)STATUSPTR(h))<=0)
202 size += sizeof(objheader_t);
203 sockid = (int) acceptfd;
205 ctrl = OBJECT_NOT_FOUND;
206 send_data(sockid, &ctrl, sizeof(char));
209 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
210 *((int *)&msg[1])=size;
211 send_data(sockid, &msg, sizeof(msg));
212 send_data(sockid, h, size);
216 case READ_MULT_REQUEST:
222 case MOVE_MULT_REQUEST:
226 /* Read transaction request */
227 transinfo.objlocked = NULL;
228 transinfo.objnotfound = NULL;
229 transinfo.modptr = NULL;
230 transinfo.numlocked = 0;
231 transinfo.numnotfound = 0;
232 if((val = readClientReq(&transinfo, (int)acceptfd, &readbuffer)) != 0) {
233 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
240 if((val = rangePrefetchReq((int)acceptfd, &readbuffer)) != 0) {
241 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
245 LOGTIME('X',0,0,myrdtsc(),0);
246 if((val = prefetchReq((int)acceptfd, &readbuffer)) != 0) {
247 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
253 case TRANS_PREFETCH_RESPONSE:
255 if((val = getRangePrefetchResponse((int)acceptfd, &readbuffer)) != 0) {
256 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
260 if((val = getPrefetchResponse((int) acceptfd, &readbuffer)) != 0) {
261 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
267 case START_REMOTE_THREAD:
268 recv_data_buf((int)acceptfd, &readbuffer, &oid, sizeof(unsigned int));
269 objType = getObjType(oid);
270 startDSMthread(oid, objType);
273 case THREAD_NOTIFY_REQUEST:
274 recv_data_buf((int)acceptfd, &readbuffer, &numoid, sizeof(unsigned int));
275 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
276 if((buffer = calloc(1,size)) == NULL) {
277 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
281 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
283 oidarry = calloc(numoid, sizeof(unsigned int));
284 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
285 size = sizeof(unsigned int) * numoid;
286 versionarry = calloc(numoid, sizeof(unsigned short));
287 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
288 size += sizeof(unsigned short) * numoid;
289 mid = *((unsigned int *)(buffer+size));
290 size += sizeof(unsigned int);
291 threadid = *((unsigned int *)(buffer+size));
292 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
296 case THREAD_NOTIFY_RESPONSE:
297 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
298 if((buffer = calloc(1,size)) == NULL) {
299 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
303 recv_data_buf((int)acceptfd, &readbuffer, buffer, size);
305 oid = *((unsigned int *)buffer);
306 size = sizeof(unsigned int);
307 version = *((unsigned short *)(buffer+size));
308 size += sizeof(unsigned short);
309 threadid = *((unsigned int *)(buffer+size));
310 threadNotify(oid,version,threadid);
315 case CHECK_OBJECTS: // check if versions of objects match
316 size = sizeof(odata) - 1;
318 recv_data_buf((int)acceptfd, &readbuffer, ptr+1, size);
319 numread = odata.numread;
320 nummod = odata.nummod;
321 checkObjVersion(&readbuffer, (int) acceptfd, numread, nummod);
325 case CLOSE_CONNECTION:
326 goto closeconnection;
329 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
334 /* Close connection */
335 if (close((int)acceptfd) == -1)
340 /* This function reads the information available in a transaction request
341 * and makes a function call to process the request */
342 int readClientReq(trans_commit_data_t *transinfo, int acceptfd, struct readstruct * readbuffer) {
345 unsigned int *oidmod, oid;
347 objheader_t *headaddr;
348 int sum, i, size, n, val;
352 /* Read fixed_data_t data structure */
353 size = sizeof(fixed) - 1;
354 ptr = (char *)&fixed;
355 fixed.control = TRANS_REQUEST;
356 recv_data_buf((int)acceptfd, readbuffer, ptr+1, size);
358 /* Read list of mids */
359 int mcount = fixed.mcount;
360 size = mcount * sizeof(unsigned int);
361 unsigned int listmid[mcount];
362 ptr = (char *) listmid;
363 recv_data_buf((int)acceptfd, readbuffer, ptr, size);
365 /* Read oid and version tuples for those objects that are not modified in the transaction */
366 int numread = fixed.numread;
367 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
369 if(numread != 0) { //If pile contains more than one object to be read,
370 // keep reading all objects
371 recv_data_buf((int)acceptfd, readbuffer, objread, size);
374 /* Read modified objects */
375 if(fixed.nummod != 0) {
376 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
377 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
380 size = fixed.sum_bytes;
381 recv_data_buf((int)acceptfd, readbuffer, modptr, size);
384 /* Create an array of oids for modified objects */
385 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
386 if (oidmod == NULL) {
387 printf("calloc error %s, %d\n", __FILE__, __LINE__);
390 ptr = (char *) modptr;
391 for(i = 0 ; i < fixed.nummod; i++) {
392 headaddr = (objheader_t *) ptr;
396 GETSIZE(tmpsize, headaddr);
397 ptr += sizeof(objheader_t) + tmpsize;
400 /*Process the information read */
401 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd, readbuffer)) != 0) {
402 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
418 /* This function processes the Coordinator's transaction request using "handleTransReq"
419 * function and sends a reply to the co-ordinator.
420 * Following this it also receives a new control message from the co-ordinator and processes this message*/
421 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
422 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd, struct readstruct *readbuffer) {
424 char control, sendctrl, retval;
425 objheader_t *tmp_header;
429 /* Send reply to the Coordinator */
430 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
431 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
435 recv_data_buf((int)acceptfd, readbuffer, &control, sizeof(char));
436 /* Process the new control message */
439 if (fixed->nummod > 0)
441 /* Unlock objects that was locked due to this transaction */
442 int useWriteUnlock = 0; //TODO verify is this piece of unlocking code ever used
443 for(i = 0; i< transinfo->numlocked; i++) {
444 if(transinfo->objlocked[i] == -1) {
448 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
449 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
453 write_unlock(STATUSPTR(header));
455 read_unlock(STATUSPTR(header));
461 /* Invoke the transCommit process() */
462 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
463 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
465 if (transinfo->objlocked != NULL) {
466 free(transinfo->objlocked);
468 if (transinfo->objnotfound != NULL) {
469 free(transinfo->objnotfound);
475 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
479 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol control = %d %s, %d\n", control, __FILE__, __LINE__);
480 //TODO Use fixed.trans_id TID since Client may have died
485 if (transinfo->objlocked != NULL) {
486 free(transinfo->objlocked);
488 if (transinfo->objnotfound != NULL) {
489 free(transinfo->objnotfound);
495 /* This function increments counters while running a voting decision on all objects involved
496 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
497 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
499 unsigned short version;
500 char control = 0, *ptr;
502 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
503 objheader_t *headptr;
505 /* Counters and arrays to formulate decision on control message to be sent */
506 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
507 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
508 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
509 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
510 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
512 /* modptr points to the beginning of the object store
513 * created at the Pariticipant.
514 * Object store holds the modified objects involved in the transaction request */
515 ptr = (char *) modptr;
519 /* Process each oid in the machine pile/ group per thread */
520 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
521 if (i < fixed->numread) { //Objs only read and not modified
522 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
524 oid = *((unsigned int *)(objread + incr));
526 incr += sizeof(unsigned int);
527 version = *((unsigned short *)(objread + incr));
528 retval=getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
529 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
530 } else { //Objs modified
531 if(i == fixed->numread) {
532 oidlocked[objlocked++] = -1;
535 headptr = (objheader_t *) ptr;
537 version = headptr->version;
538 GETSIZE(tmpsize, headptr);
539 ptr += sizeof(objheader_t) + tmpsize;
540 retval=getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
541 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
542 &numBytes, &control, oid, version);
544 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
545 //unlock objects as soon versions mismatch or locks cannot be acquired)
547 int useWriteUnlock = 0;
548 for(j = 0; j < objlocked; j++) {
549 if(oidlocked[j] == -1) {
553 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
554 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
558 write_unlock(STATUSPTR(headptr));
560 read_unlock(STATUSPTR(headptr));
570 //go through rest of the objects for version mismatches
571 if(retval==TRANS_DISAGREE || retval==TRANS_SOFT_ABORT) {
573 procRestObjs(objread, ptr, i, fixed->numread, fixed->nummod, oidnotfound, oidvernotmatch, &objnotfound, &objvernotmatch, &v_nomatch, &numBytes);
576 /* send TRANS_DISAGREE and objs*/
579 char *objs = calloc(1, numBytes);
581 for(j = 0; j<objvernotmatch; j++) {
582 objheader_t *header = mhashSearch(oidvernotmatch[j]);
584 GETSIZE(size, header);
585 size += sizeof(objheader_t);
586 memcpy(objs+offset, header, size);
592 int useWriteUnlock = 0;
593 for(j = 0; j < objlocked; j++) {
594 if(oidlocked[j] == -1) {
598 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
599 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
603 write_unlock(STATUSPTR(headptr));
605 read_unlock(STATUSPTR(headptr));
611 control=TRANS_DISAGREE;
612 send_data(acceptfd, &control, sizeof(char));
614 send_data(acceptfd, &numBytes, sizeof(int));
615 send_data(acceptfd, objs, numBytes);
616 transinfo->objvernotmatch = oidvernotmatch;
617 transinfo->numvernotmatch = objvernotmatch;
619 free(transinfo->objvernotmatch);
624 /* Decide what control message to send to Coordinator */
625 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
626 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
627 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
633 /* Update Commit info for objects that are read */
634 char getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
635 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
636 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
637 char *control, unsigned int oid, unsigned short version) {
639 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
641 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
642 /* Save the oids not found and number of oids not found for later use */
643 oidnotfound[*objnotfound] = oid;
645 *control = TRANS_DISAGREE;
646 } else { /* If Obj found in machine (i.e. has not moved) */
647 /* Check if Obj is locked by any previous transaction */
648 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
649 if (version == ((objheader_t *)mobj)->version) { /* match versions */
651 *control = TRANS_AGREE;
652 } else { /* If versions don't match ...HARD ABORT */
654 oidvernotmatch[*objvernotmatch] = oid;
658 size += sizeof(objheader_t);
660 /* Send TRANS_DISAGREE to Coordinator */
661 *control = TRANS_DISAGREE;
663 //Keep track of oid locked
664 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
665 } else { //we are locked
666 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
668 *control=TRANS_SOFT_ABORT;
669 } else { /* If versions don't match ...HARD ABORT */
671 oidvernotmatch[*objvernotmatch] = oid;
675 size += sizeof(objheader_t);
677 *control = TRANS_DISAGREE;
684 /* Update Commit info for objects that are read */
685 char getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
686 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
687 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
689 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
690 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
691 /* Save the oids not found and number of oids not found for later use */
692 oidnotfound[*objnotfound] = oid;
694 *control = TRANS_DISAGREE;
695 } else { /* If Obj found in machine (i.e. has not moved) */
696 /* Check if Obj is locked by any previous transaction */
697 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
698 if (version == ((objheader_t *)mobj)->version) { /* match versions */
700 *control=TRANS_AGREE;
701 } else { /* If versions don't match ...HARD ABORT */
703 oidvernotmatch[(*objvernotmatch)++] = oid;
706 size += sizeof(objheader_t);
708 /* Send TRANS_DISAGREE to Coordinator */
709 *control = TRANS_DISAGREE;
711 //Keep track of oid locked
712 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
713 } else { /* Some other transaction has aquired a write lock on this object */
714 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
716 *control=TRANS_SOFT_ABORT;
717 } else { /* If versions don't match ...HARD ABORT */
719 oidvernotmatch[*objvernotmatch] = oid;
723 size += sizeof(objheader_t);
725 *control = TRANS_DISAGREE;
732 void procRestObjs(char *objread,
737 unsigned int *oidnotfound,
738 unsigned int *oidvernotmatch,
745 unsigned short version;
747 /* Process each oid in the machine pile/ group per thread */
748 for (i = index; i < numread+nummod; i++) {
749 if (i < numread) { //Objs only read and not modified
750 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
752 oid = *((unsigned int *)(objread + incr));
753 incr += sizeof(unsigned int);
754 version = *((unsigned short *)(objread + incr));
755 } else { //Objs modified
756 objheader_t *headptr;
757 headptr = (objheader_t *) objmod;
759 version = headptr->version;
761 GETSIZE(tmpsize, headptr);
762 objmod += sizeof(objheader_t) + tmpsize;
764 processVerNoMatch(oidnotfound,
776 void processVerNoMatch(unsigned int *oidnotfound,
777 unsigned int *oidvernotmatch,
783 unsigned short version) {
785 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
787 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
788 /* Save the oids not found and number of oids not found for later use */
789 oidnotfound[*objnotfound] = oid;
791 } else { /* If Obj found in machine (i.e. has not moved) */
792 /* Check if Obj is locked by any previous transaction */
793 //if (!write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
794 if (version != ((objheader_t *)mobj)->version) { /* match versions */
796 oidvernotmatch[*objvernotmatch] = oid;
800 size += sizeof(objheader_t);
806 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
807 * to send to Coordinator based on the votes of oids involved in the transaction */
808 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
809 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
810 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
814 /* Condition to send TRANS_AGREE */
815 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
816 control = TRANS_AGREE;
817 /* Send control message */
818 send_data(acceptfd, &control, sizeof(char));
820 /* Condition to send TRANS_SOFT_ABORT */
821 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
822 control = TRANS_SOFT_ABORT;
824 /* Send control message */
825 send_data(acceptfd, &control, sizeof(char));
827 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
828 if(*(objnotfound) != 0) {
830 msg[0] = *(objnotfound);
831 send_data(acceptfd, &msg, sizeof(int));
832 int size = sizeof(unsigned int)* *(objnotfound);
833 send_data(acceptfd, oidnotfound, size);
837 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
838 * if Participant receives a TRANS_COMMIT */
839 transinfo->objlocked = oidlocked;
840 transinfo->objnotfound = oidnotfound;
841 transinfo->modptr = modptr;
842 transinfo->numlocked = *(objlocked);
843 transinfo->numnotfound = *(objnotfound);
847 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
848 * addresses in lookup table and also changes version number
849 * Sends an ACK back to Coordinator */
850 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
852 objheader_t *newheader;
853 int i = 0, offset = 0;
857 /* Process each modified object saved in the mainobject store */
858 for(i = 0; i < nummod; i++) {
859 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
860 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
863 GETSIZE(tmpsize,header);
866 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
867 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
869 dst->___cachedCode___=src->___cachedCode___;
870 dst->___cachedHash___=src->___cachedHash___;
871 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
877 header->version += 1;
878 /* If threads are waiting on this object to be updated, notify them */
879 if(header->notifylist != NULL) {
880 notifyAll(&header->notifylist, OID(header), header->version);
882 offset += sizeof(objheader_t) + tmpsize;
888 /* Unlock locked objects */
889 int useWriteUnlock = 0;
890 for(i = 0; i < numlocked; i++) {
891 if(oidlocked[i] == -1) {
895 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
896 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
901 write_unlock(STATUSPTR(header));
903 read_unlock(STATUSPTR(header));
906 //TODO Update location lookup table
910 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
911 * Looks for the objects to be prefetched in the main object store.
912 * If objects are not found then record those and if objects are found
913 * then use offset values to prefetch references to other objects */
914 int prefetchReq(int acceptfd, struct readstruct * readbuffer) {
915 int i, size, objsize, numoffset = 0, gid=0;
917 char *recvbuffer, control;
918 unsigned int oid, mid=-1;
921 struct writestruct writebuffer;
925 recv_data_buf((int)acceptfd, readbuffer, &numoffset, sizeof(int));
928 recv_data_buf((int)acceptfd, readbuffer, &oidmid, 2*sizeof(unsigned int));
930 if (mid != oidmid.mid) {
932 forcesend_buf(sd, &writebuffer, NULL, 0);
933 freeSockWithLock(transPResponseSocketPool, mid, sd);
936 sd = getSockWithLock(transPResponseSocketPool, mid);
937 writebuffer.offset=0;
939 short offsetarry[numoffset];
940 recv_data_buf((int)acceptfd, readbuffer, &gid, sizeof(int));
941 recv_data_buf((int) acceptfd, readbuffer, offsetarry, numoffset*sizeof(short));
942 LOGTIME('A',oid ,0,myrdtsc(),gid); //after recv the entire prefetch request
944 /*Process each oid */
945 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
946 /* Save the oids not found in buffer for later use */
947 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
948 char sendbuffer[size+1];
949 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
950 *((int *) (sendbuffer+sizeof(char))) = size;
951 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
952 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char)+sizeof(char))) = oid;
953 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
954 send_buf(sd, &writebuffer, sendbuffer, size+1);
955 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
956 } else { /* Object Found */
958 GETSIZE(objsize, header);
959 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
960 char sendbuffer[size+1];
961 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
962 *((int *)(sendbuffer + incr)) = size;
964 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
965 incr += sizeof(char);
966 *((unsigned int *)(sendbuffer+incr)) = oid;
967 incr += sizeof(unsigned int);
968 *((int *)(sendbuffer+incr)) = gid;
970 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
971 send_buf(sd, &writebuffer, sendbuffer, size+1);
972 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
973 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
975 /* Calculate the oid corresponding to the offset value */
976 for(i = 0 ; i< numoffset ; i++) {
977 /* Check for arrays */
978 if(TYPE(header) >= NUMCLASSES) {
979 int elementsize = classsize[TYPE(header)];
980 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
981 unsigned short length = ao->___length___;
982 /* Check if array out of bounds */
983 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
986 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
988 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
991 /* Don't continue if we hit a NULL pointer */
995 LOGTIME('B',oid,0,myrdtsc(),gid); //send next oid found from prefetch request
997 if((header = mhashSearch(oid)) == NULL) {
998 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
999 char sendbuffer[size+1];
1000 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
1001 *((int *) (sendbuffer+1)) = size;
1002 *((char *)(sendbuffer + sizeof(char)+sizeof(int))) = OBJECT_NOT_FOUND;
1003 *((unsigned int *)(sendbuffer + sizeof(char)+sizeof(int) + sizeof(char))) = oid;
1004 *((int *)(sendbuffer+sizeof(int) + sizeof(char)+sizeof(char)+sizeof(unsigned int))) = gid;
1006 send_buf(sd, &writebuffer, sendbuffer, size+1);
1007 LOGTIME('J',oid, 0,myrdtsc(), gid); //send first oid not found prefetch request
1009 } else { /* Obj Found */
1011 GETSIZE(objsize, header);
1012 size = sizeof(int)+sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
1013 char sendbuffer[size+1];
1014 sendbuffer[0]=TRANS_PREFETCH_RESPONSE;
1015 *((int *)(sendbuffer + incr)) = size;
1016 incr += sizeof(int);
1017 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
1018 incr += sizeof(char);
1019 *((unsigned int *)(sendbuffer+incr)) = oid;
1020 incr += sizeof(unsigned int);
1021 *((int *)(sendbuffer+incr)) = gid;
1022 incr += sizeof(int);
1023 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
1024 send_buf(sd, &writebuffer, sendbuffer, size+1);
1025 LOGOIDTYPE("SRES", oid, TYPE(header), (myrdtsc()-clockoffset));
1026 LOGTIME('C',oid,TYPE(header),myrdtsc(), gid); //send first oid found from prefetch request
1034 forcesend_buf(sd, &writebuffer, NULL, 0);
1035 freeSockWithLock(transPResponseSocketPool, mid, sd);
1040 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
1041 send_data(sd, control, sizeof(char));
1042 /* Send the buffer with its size */
1043 int length = *(size);
1044 send_data(sd, sendbuffer, length);
1047 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
1048 objheader_t *header;
1050 unsigned short newversion;
1051 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
1053 struct sockaddr_in remoteAddr;
1059 oid = *(oidarry + i);
1060 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
1061 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1064 /* Check to see if versions are same */
1066 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
1067 newversion = header->version;
1068 if(newversion == *(versionarry + i)) {
1069 //Add to the notify list
1070 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
1071 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
1074 write_unlock(STATUSPTR(header));
1076 write_unlock(STATUSPTR(header));
1077 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1078 perror("processReqNotify():socket()");
1081 bzero(&remoteAddr, sizeof(remoteAddr));
1082 remoteAddr.sin_family = AF_INET;
1083 remoteAddr.sin_port = htons(LISTEN_PORT);
1084 remoteAddr.sin_addr.s_addr = htonl(mid);
1086 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1087 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
1088 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1093 //Send Update notification
1094 msg[0] = THREAD_NOTIFY_RESPONSE;
1095 *((unsigned int *)&msg[1]) = oid;
1096 size = sizeof(unsigned int);
1097 *((unsigned short *)(&msg[1]+size)) = newversion;
1098 size += sizeof(unsigned short);
1099 *((unsigned int *)(&msg[1]+size)) = threadid;
1100 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
1101 send_data(sd, msg, size);