3 #include "machinepile.h"
8 #include "threadnotify.h"
10 #include "addUdpEnhance.h"
11 #include "addPrefetchEnhance.h"
19 #include "abortreaders.h"
24 #define CONFIG_FILENAME "dstm.conf"
26 /* Thread transaction variables */
28 __thread objstr_t *t_cache;
29 __thread struct ___Object___ *revertlist;
32 __thread jmp_buf aborttrans;
36 /* Global Variables */
37 extern int classsize[];
38 pfcstats_t *evalPrefetch;
39 extern int numprefetchsites; //Global variable containing number of prefetch sites
40 extern pthread_mutex_t mainobjstore_mutex; // Mutex to lock main Object store
41 pthread_mutex_t prefetchcache_mutex; // Mutex to lock Prefetch Cache
42 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
43 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
44 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
45 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
46 extern objstr_t *mainobjstore;
47 unsigned int myIpAddr;
48 unsigned int *hostIpAddrs;
51 int myIndexInHostArray;
52 unsigned int oidsPerBlock;
56 sockPoolHashTable_t *transReadSockPool;
57 sockPoolHashTable_t *transPrefetchSockPool;
58 sockPoolHashTable_t *transRequestSockPool;
59 pthread_mutex_t notifymutex;
60 pthread_mutex_t atomicObjLock;
62 /***********************************
63 * Global Variables for statistics
64 **********************************/
65 int numTransCommit = 0;
66 int numTransAbort = 0;
69 int nprehashSearch = 0;
75 int sendRemoteReq = 0;
78 void printhex(unsigned char *, int);
79 plistnode_t *createPiles();
80 plistnode_t *sortPiles(plistnode_t *pileptr);
84 char bigarray[16*1024*1024];
86 #define LOGEVENT(x) { \
94 /*******************************
95 * Send and Recv function calls
96 *******************************/
97 void send_data(int fd, void *buf, int buflen) {
98 char *buffer = (char *)(buf);
102 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
103 bytesSent = bytesSent + numbytes;
104 if (numbytes == -1) {
113 void send_buf(int fd, struct writestruct * sendbuffer, void *buffer, int buflen) {
114 if (buflen+sendbuffer->offset>WMAXBUF) {
115 send_data(fd, sendbuffer->buf, sendbuffer->offset);
116 sendbuffer->offset=0;
117 send_data(fd, buffer, buflen);
120 memcpy(&sendbuffer->buf[sendbuffer->offset], buffer, buflen);
121 sendbuffer->offset+=buflen;
122 if (sendbuffer->offset>WTOP) {
123 send_data(fd, sendbuffer->buf, sendbuffer->offset);
124 sendbuffer->offset=0;
128 void forcesend_buf(int fd, struct writestruct * sendbuffer, void *buffer, int buflen) {
129 if (buflen+sendbuffer->offset>WMAXBUF) {
130 send_data(fd, sendbuffer->buf, sendbuffer->offset);
131 sendbuffer->offset=0;
132 send_data(fd, buffer, buflen);
135 memcpy(&sendbuffer->buf[sendbuffer->offset], buffer, buflen);
136 sendbuffer->offset+=buflen;
137 send_data(fd, sendbuffer->buf, sendbuffer->offset);
138 sendbuffer->offset=0;
141 int recvw(int fd, void *buf, int len, int flags) {
142 return recv(fd, buf, len, flags);
145 void recv_data_buf(int fd, struct readstruct * readbuffer, void *buffer, int buflen) {
146 char *buf=(char *)buffer;
147 int numbytes=readbuffer->head-readbuffer->tail;
151 memcpy(buf, &readbuffer->buf[readbuffer->tail], numbytes);
152 readbuffer->tail+=numbytes;
159 if (buflen>=MAXBUF) {
160 recv_data(fd, buf, buflen);
169 int numbytes = recvw(fd, &readbuffer->buf[readbuffer->head], maxbuf, 0);
170 if (numbytes == -1) {
176 readbuffer->head+=numbytes;
179 memcpy(buf,readbuffer->buf,obufflen);
180 readbuffer->tail=obufflen;
183 int recv_data_errorcode_buf(int fd, struct readstruct * readbuffer, void *buffer, int buflen) {
184 char *buf=(char *)buffer;
186 int numbytes=readbuffer->head-readbuffer->tail;
190 memcpy(buf, &readbuffer->buf[readbuffer->tail], numbytes);
191 readbuffer->tail+=numbytes;
198 if (buflen>=MAXBUF) {
199 return recv_data_errorcode(fd, buf, buflen);
207 int numbytes = recvw(fd, &readbuffer->buf[readbuffer->head], maxbuf, 0);
217 readbuffer->head+=numbytes;
220 memcpy(buf,readbuffer->buf,obufflen);
221 readbuffer->tail=obufflen;
226 void recv_data(int fd, void *buf, int buflen) {
227 char *buffer = (char *)(buf);
231 numbytes = recvw(fd, buffer, size, 0);
232 bytesRecv = bytesRecv + numbytes;
233 if (numbytes == -1) {
242 int recv_data_errorcode(int fd, void *buf, int buflen) {
243 char *buffer = (char *)(buf);
247 numbytes = recvw(fd, buffer, size, 0);
250 if (numbytes == -1) {
261 void printhex(unsigned char *ptr, int numBytes) {
263 for (i = 0; i < numBytes; i++) {
265 printf("0%x ", ptr[i]);
267 printf("%x ", ptr[i]);
273 inline int arrayLength(int *array) {
275 for(i=0 ; array[i] != -1; i++)
280 inline int findmax(int *array, int arraylength) {
283 for(i = 0; i < arraylength; i++) {
291 //#define INLINEPREFETCH
292 #define PREFTHRESHOLD 4
294 /* This function is a prefetch call generated by the compiler that
295 * populates the shared primary prefetch queue*/
296 void prefetch(int siteid, int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
297 /* Allocate for the queue node*/
298 int qnodesize = 2*sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
300 #ifdef INLINEPREFETCH
304 node=getmemory(qnodesize);
305 if (node==NULL&&attempted)
309 char *node=getmemory(qnodesize);
311 int top=endoffsets[ntuples-1];
317 /* Set queue node values */
319 /* TODO: Remove this after testing */
320 evalPrefetch[siteid].callcount++;
322 *((int *)(node))=siteid;
323 *((int *)(node + sizeof(int))) = ntuples;
325 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
326 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
327 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
329 #ifdef INLINEPREFETCH
332 int numpref=numavailable();
335 if (node==NULL && numpref!=0 || numpref==PREFTHRESHOLD) {
337 prefetchpile_t *pilehead = foundLocal(node,numpref);
338 if (pilehead!=NULL) {
339 // Get sock from shared pool
341 /* Send Prefetch Request */
342 prefetchpile_t *ptr = pilehead;
344 int sd = getSock2(transPrefetchSockPool, ptr->mid);
345 sendPrefetchReq(ptr, sd);
352 }//end do prefetch if condition
355 /* Lock and insert into primary prefetch queue */
360 /* This function starts up the transaction runtime. */
361 int dstmStartup(const char * option) {
362 pthread_t thread_Listen, udp_thread_Listen;
364 int master=option!=NULL && strcmp(option, "master")==0;
368 if (processConfigFile() != 0)
369 return 0; //TODO: return error value, cause main program to exit
376 printf("Trans stats is on\n");
383 //Initialize socket pool
384 transReadSockPool = createSockPool(transReadSockPool, DEFAULTSOCKPOOLSIZE);
385 transPrefetchSockPool = createSockPool(transPrefetchSockPool, DEFAULTSOCKPOOLSIZE);
386 transRequestSockPool = createSockPool(transRequestSockPool, DEFAULTSOCKPOOLSIZE);
392 pthread_attr_init(&attr);
393 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
396 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
399 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
402 dstmListen((void *)fd);
407 //TODO Use this later
408 void *pCacheAlloc(objstr_t *store, unsigned int size) {
414 while(ptr->next != NULL) {
415 /* check if store is empty */
416 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
431 /* This function initiates the prefetch thread A queue is shared
432 * between the main thread of execution and the prefetch thread to
433 * process the prefetch call Call from compiler populates the shared
434 * queue with prefetch requests while prefetch thread processes the
435 * prefetch requests */
438 //Create and initialize prefetch cache structure
441 if((evalPrefetch = initPrefetchStats()) == NULL) {
442 printf("%s() Error allocating memory at %s, %d\n", __func__, __FILE__, __LINE__);
447 /* Initialize attributes for mutex */
448 pthread_mutexattr_init(&prefetchcache_mutex_attr);
449 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
451 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
452 pthread_mutex_init(¬ifymutex, NULL);
453 pthread_mutex_init(&atomicObjLock, NULL);
455 //Create prefetch cache lookup table
456 if(prehashCreate(PHASH_SIZE, PLOADFACTOR)) {
461 //Initialize primary shared queue
463 //Initialize machine pile w/prefetch oids and offsets shared queue
466 //Create the primary prefetch thread
470 retval=pthread_create(&tPrefetch, NULL, transPrefetchNew, NULL);
473 #ifndef INLINEPREFETCH
475 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
479 #ifndef INLINEPREFETCH
480 pthread_detach(tPrefetch);
485 /* This function stops the threads spawned */
489 pthread_cancel(tPrefetch);
490 for(t = 0; t < NUM_THREADS; t++)
491 pthread_cancel(wthreads[t]);
497 /* This functions inserts randowm wait delays in the order of msec
498 * Mostly used when transaction commits retry*/
505 req.tv_nsec = (long)(1000 + (t%10000)); //1-11 microsec
506 nanosleep(&req, NULL);
510 /* This function initializes things required in the transaction start*/
512 t_cache = objstrCreate(1048576);
513 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
520 // Search for an address for a given oid
521 /*#define INLINE inline __attribute__((always_inline))
523 INLINE void * chashSearchI(chashtable_t *table, unsigned int key) {
524 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
525 chashlistnode_t *node = &table->table[(key & table->mask)>>1];
528 if(node->key == key) {
532 } while(node != NULL);
540 /* This function finds the location of the objects involved in a transaction
541 * and returns the pointer to the object if found in a remote location */
542 __attribute__((pure)) objheader_t *transRead(unsigned int oid) {
543 unsigned int machinenumber;
544 objheader_t *tmp, *objheader;
545 objheader_t *objcopy;
548 chashlistnode_t *node;
555 node= &c_table[(oid & c_mask)>>1];
557 if(node->key == oid) {
562 return &((objheader_t*)node->val)[1];
568 } while(node != NULL);
572 if((objheader = chashSearchI(record->lookupTable, oid)) != NULL) {
577 return &objheader[1];
586 //abort this transaction
587 //printf("ABORTING\n");
588 removetransactionhash();
589 objstrDelete(t_cache);
591 _longjmp(aborttrans,1);
596 if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
600 /* Look up in machine lookup table and copy into cache*/
601 GETSIZE(size, objheader);
602 size += sizeof(objheader_t);
603 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
604 memcpy(objcopy, objheader, size);
605 /* Insert into cache's lookup table */
607 t_chashInsert(OID(objheader), objcopy);
615 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
619 /* Look up in prefetch cache */
621 size+=sizeof(objheader_t);
622 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
623 memcpy(objcopy, tmp, size);
624 /* Insert into cache's lookup table */
625 t_chashInsert(OID(tmp), objcopy);
633 /* Get the object from the remote location */
634 if((machinenumber = lhashSearch(oid)) == 0) {
635 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
638 objcopy = getRemoteObj(machinenumber, oid);
640 if(objcopy == NULL) {
641 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
649 //Copy object to prefetch cache
650 pthread_mutex_lock(&prefetchcache_mutex);
651 objheader_t *headerObj;
653 GETSIZE(size, objcopy);
654 if((headerObj = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
655 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
657 pthread_mutex_unlock(&prefetchcache_mutex);
660 pthread_mutex_unlock(&prefetchcache_mutex);
661 memcpy(headerObj, objcopy, size+sizeof(objheader_t));
662 //make an entry in prefetch lookup hashtable
664 if((oldptr = prehashSearch(oid)) != NULL) {
666 prehashInsert(oid, headerObj);
668 prehashInsert(oid, headerObj);
681 /* This function finds the location of the objects involved in a transaction
682 * and returns the pointer to the object if found in a remote location */
683 __attribute__((pure)) objheader_t *transRead2(unsigned int oid) {
684 unsigned int machinenumber;
685 objheader_t *tmp, *objheader;
686 objheader_t *objcopy;
691 //abort this transaction
692 //printf("ABORTING\n");
693 removetransactionhash();
694 objstrDelete(t_cache);
696 _longjmp(aborttrans,1);
701 if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
705 /* Look up in machine lookup table and copy into cache*/
706 GETSIZE(size, objheader);
707 size += sizeof(objheader_t);
708 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
709 memcpy(objcopy, objheader, size);
710 /* Insert into cache's lookup table */
712 t_chashInsert(OID(objheader), objcopy);
720 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
725 /* Look up in prefetch cache */
727 size+=sizeof(objheader_t);
728 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
729 memcpy(objcopy, tmp, size);
730 /* Insert into cache's lookup table */
731 t_chashInsert(OID(tmp), objcopy);
739 /* Get the object from the remote location */
740 if((machinenumber = lhashSearch(oid)) == 0) {
741 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
744 objcopy = getRemoteObj(machinenumber, oid);
746 if(objcopy == NULL) {
747 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
757 //Copy object to prefetch cache
758 pthread_mutex_lock(&prefetchcache_mutex);
759 objheader_t *headerObj;
761 GETSIZE(size, objcopy);
762 if((headerObj = prefetchobjstrAlloc(size+sizeof(objheader_t))) == NULL) {
763 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
765 pthread_mutex_unlock(&prefetchcache_mutex);
768 pthread_mutex_unlock(&prefetchcache_mutex);
769 memcpy(headerObj, objcopy, size+sizeof(objheader_t));
770 //make an entry in prefetch lookup hashtable
772 if((oldptr = prehashSearch(oid)) != NULL) {
774 prehashInsert(oid, headerObj);
776 prehashInsert(oid, headerObj);
788 /* This function creates objects in the transaction record */
789 objheader_t *transCreateObj(unsigned int size) {
790 objheader_t *tmp = (objheader_t *) objstrAlloc(&t_cache, (sizeof(objheader_t) + size));
791 OID(tmp) = getNewOID();
795 t_chashInsert(OID(tmp), tmp);
798 return &tmp[1]; //want space after object header
805 /* This function creates machine piles based on all machines involved in a
806 * transaction commit request */
807 plistnode_t *createPiles() {
809 plistnode_t *pile = NULL;
810 unsigned int machinenum;
811 objheader_t *headeraddr;
812 chashlistnode_t * ptr = c_table;
813 /* Represents number of bins in the chash table */
814 unsigned int size = c_size;
816 for(i = 0; i < size ; i++) {
817 chashlistnode_t * curr = &ptr[i];
818 /* Inner loop to traverse the linked list of the cache lookupTable */
819 while(curr != NULL) {
820 //if the first bin in hash table is empty
823 headeraddr=(objheader_t *) curr->val;
825 //Get machine location for object id (and whether local or not)
826 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
827 machinenum = myIpAddr;
828 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
829 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
833 //Make machine groups
834 pile = pInsert(pile, headeraddr, machinenum, c_numelements);
841 /* This function creates machine piles based on all machines involved in a
842 * transaction commit request */
843 plistnode_t *createPiles() {
845 plistnode_t *pile = NULL;
846 unsigned int machinenum;
847 objheader_t *headeraddr;
848 struct chashentry * ptr = c_table;
849 /* Represents number of bins in the chash table */
850 unsigned int size = c_size;
852 for(i = 0; i < size ; i++) {
853 struct chashentry * curr = & ptr[i];
854 /* Inner loop to traverse the linked list of the cache lookupTable */
855 //if the first bin in hash table is empty
858 headeraddr=(objheader_t *) curr->ptr;
860 //Get machine location for object id (and whether local or not)
861 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
862 machinenum = myIpAddr;
863 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
864 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
868 //Make machine groups
869 pile = pInsert(pile, headeraddr, machinenum, c_numelements);
875 /* This function initiates the transaction commit process
876 * Spawns threads for each of the new connections with Participants
877 * and creates new piles by calling the createPiles(),
878 * Sends a transrequest() to each remote machines for objects found remotely
879 * and calls handleLocalReq() to process objects found locally */
881 unsigned int tot_bytes_mod, *listmid;
882 plistnode_t *pile, *pile_ptr;
883 char treplyretry; /* keeps track of the common response that needs to be sent */
885 trans_commit_data_t transinfo; /* keeps track of objs locked during transaction */
890 for(iii=0;iii<bigindex;iii++) {
891 printf("%c", bigarray[iii]);
897 //abort this transaction
899 * printf("ABORTING TRANSACTION AT COMMIT\n");
901 removetransactionhash();
902 objstrDelete(t_cache);
912 /* Look through all the objects in the transaction record and make piles
913 * for each machine involved in the transaction*/
915 pile_ptr = pile = createPiles();
916 pile_ptr = pile = sortPiles(pile);
921 /* Create the packet to be sent in TRANS_REQUEST */
923 /* Count the number of participants */
925 pilecount = pCount(pile);
927 /* Create a list of machine ids(Participants) involved in transaction */
928 listmid = calloc(pilecount, sizeof(unsigned int));
929 pListMid(pile, listmid);
931 /* Create a socket and getReplyCtrl array, initialize */
932 int socklist[pilecount];
934 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
935 socklist[loopcount] = 0;
936 char getReplyCtrl[pilecount];
937 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
938 getReplyCtrl[loopcount] = 0;
940 /* Process each machine pile */
942 trans_req_data_t *tosend;
943 tosend = calloc(pilecount, sizeof(trans_req_data_t));
944 while(pile != NULL) {
945 tosend[sockindex].f.control = TRANS_REQUEST;
946 tosend[sockindex].f.mcount = pilecount;
947 tosend[sockindex].f.numread = pile->numread;
948 tosend[sockindex].f.nummod = pile->nummod;
949 tosend[sockindex].f.numcreated = pile->numcreated;
950 tosend[sockindex].f.sum_bytes = pile->sum_bytes;
951 tosend[sockindex].listmid = listmid;
952 tosend[sockindex].objread = pile->objread;
953 tosend[sockindex].oidmod = pile->oidmod;
954 tosend[sockindex].oidcreated = pile->oidcreated;
956 if(pile->mid != myIpAddr) {
957 if((sd = getSock2WithLock(transRequestSockPool, pile->mid)) < 0) {
958 printf("transRequest(): socket create error\n");
963 socklist[sockindex] = sd;
964 /* Send bytes of data with TRANS_REQUEST control message */
965 send_data(sd, &(tosend[sockindex].f), sizeof(fixed_data_t));
967 /* Send list of machines involved in the transaction */
969 int size=sizeof(unsigned int)*(tosend[sockindex].f.mcount);
970 send_data(sd, tosend[sockindex].listmid, size);
973 /* Send oids and version number tuples for objects that are read */
975 int size=(sizeof(unsigned int)+sizeof(unsigned short))*(tosend[sockindex].f.numread);
976 send_data(sd, tosend[sockindex].objread, size);
979 /* Send objects that are modified */
981 if((modptr = calloc(1, tosend[sockindex].f.sum_bytes)) == NULL) {
982 printf("Calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
989 for(i = 0; i < tosend[sockindex].f.nummod ; i++) {
991 objheader_t *headeraddr;
992 if((headeraddr = t_chashSearch(tosend[sockindex].oidmod[i])) == NULL) {
993 printf("%s() Error: No such oid %s, %d\n", __func__, __FILE__, __LINE__);
999 GETSIZE(size,headeraddr);
1000 size+=sizeof(objheader_t);
1001 memcpy(modptr+offset, headeraddr, size);
1004 send_data(sd, modptr, tosend[sockindex].f.sum_bytes);
1006 } else { //handle request locally
1007 handleLocalReq(&tosend[sockindex], &transinfo, &getReplyCtrl[sockindex]);
1011 } //end of pile processing
1012 /* Recv Ctrl msgs from all machines */
1014 for(i = 0; i < pilecount; i++) {
1015 int sd = socklist[i];
1018 recv_data(sd, &control, sizeof(char));
1019 //Update common data structure with new ctrl msg
1020 getReplyCtrl[i] = control;
1021 /* Recv Objects if participant sends TRANS_DISAGREE */
1023 if(control == TRANS_DISAGREE) {
1025 recv_data(sd, &length, sizeof(int));
1027 pthread_mutex_lock(&prefetchcache_mutex);
1028 if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) {
1029 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1032 pthread_mutex_unlock(&prefetchcache_mutex);
1035 pthread_mutex_unlock(&prefetchcache_mutex);
1036 recv_data(sd, newAddr, length);
1038 while(length != 0) {
1039 unsigned int oidToPrefetch;
1040 objheader_t * header;
1041 header = (objheader_t *)(((char *)newAddr) + offset);
1042 oidToPrefetch = OID(header);
1045 GETSIZE(size, header);
1046 size += sizeof(objheader_t);
1047 //make an entry in prefetch hash table
1049 if((oldptr = prehashSearch(oidToPrefetch)) != NULL) {
1050 prehashRemove(oidToPrefetch);
1051 prehashInsert(oidToPrefetch, header);
1053 prehashInsert(oidToPrefetch, header);
1056 length = length - size;
1059 } //end of receiving objs
1063 /* Decide the final response */
1064 if((finalResponse = decideResponse(getReplyCtrl, &treplyretry, pilecount)) == 0) {
1065 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1071 /* Send responses to all machines */
1072 for(i = 0; i < pilecount; i++) {
1073 int sd = socklist[i];
1076 if(finalResponse == TRANS_COMMIT) {
1078 /* Update prefetch cache */
1079 if((retval = updatePrefetchCache(&(tosend[i]))) != 0) {
1080 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1087 /* Invalidate objects in other machine cache */
1088 if(tosend[i].f.nummod > 0) {
1089 if((retval = invalidateObj(&(tosend[i]))) != 0) {
1090 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
1097 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
1098 removethisreadtransaction(tosend[i].objread, tosend[i].f.numread);
1102 else if (!treplyretry) {
1103 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod);
1104 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1108 send_data(sd, &finalResponse, sizeof(char));
1110 /* Complete local processing */
1111 doLocalProcess(finalResponse, &(tosend[i]), &transinfo);
1113 if(finalResponse == TRANS_COMMIT) {
1114 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
1115 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1116 } else if (!treplyretry) {
1117 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod);
1118 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1124 /* Free resources */
1129 /* wait a random amount of time before retrying to commit transaction*/
1136 /* Retry trans commit procedure during soft_abort case */
1137 } while (treplyretry);
1139 if(finalResponse == TRANS_ABORT) {
1140 //printf("Aborting trans\n");
1145 /* Free Resources */
1146 objstrDelete(t_cache);
1149 } else if(finalResponse == TRANS_COMMIT) {
1154 /* Free Resources */
1155 objstrDelete(t_cache);
1159 //TODO Add other cases
1160 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1166 /* This function handles the local objects involved in a transaction
1167 * commiting process. It also makes a decision if this local machine
1168 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator */
1169 void handleLocalReq(trans_req_data_t *tdata, trans_commit_data_t *transinfo, char *getReplyCtrl) {
1170 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
1171 int numoidnotfound = 0, numoidlocked = 0;
1172 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
1175 unsigned short version;
1177 /* Counters and arrays to formulate decision on control message to be sent */
1178 oidnotfound = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod), sizeof(unsigned int));
1179 oidlocked = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod +1), sizeof(unsigned int)); // calloc additional 1 byte for
1180 //setting a divider between read and write locks
1181 numread = tdata->f.numread;
1182 /* Process each oid in the machine pile/ group per thread */
1183 for (i = 0; i < tdata->f.numread + tdata->f.nummod; i++) {
1184 if (i < tdata->f.numread) {
1185 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
1187 oid = *((unsigned int *)(((char *)tdata->objread) + incr));
1188 version = *((unsigned short *)(((char *)tdata->objread) + incr + sizeof(unsigned int)));
1189 commitCountForObjRead(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
1190 } else { // Objects Modified
1191 if(i == tdata->f.numread) {
1192 oidlocked[numoidlocked++] = -1;
1195 objheader_t *headptr;
1196 headptr = (objheader_t *) t_chashSearch(tdata->oidmod[i-numread]);
1197 if (headptr == NULL) {
1198 printf("Error: handleLocalReq() returning NULL, no such oid %s, %d\n", __FILE__, __LINE__);
1202 version = headptr->version;
1203 commitCountForObjMod(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
1207 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
1208 * if Participant receives a TRANS_COMMIT */
1209 transinfo->objlocked = oidlocked;
1210 transinfo->objnotfound = oidnotfound;
1211 transinfo->modptr = NULL;
1212 transinfo->numlocked = numoidlocked;
1213 transinfo->numnotfound = numoidnotfound;
1215 /* Condition to send TRANS_AGREE */
1216 if(v_matchnolock == tdata->f.numread + tdata->f.nummod) {
1217 *getReplyCtrl = TRANS_AGREE;
1219 /* Condition to send TRANS_SOFT_ABORT */
1220 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
1221 *getReplyCtrl = TRANS_SOFT_ABORT;
1225 void doLocalProcess(char finalResponse, trans_req_data_t *tdata, trans_commit_data_t *transinfo) {
1226 if(finalResponse == TRANS_ABORT) {
1227 if(transAbortProcess(transinfo) != 0) {
1228 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
1232 } else if(finalResponse == TRANS_COMMIT) {
1234 /* Invalidate objects in other machine cache */
1235 if(tdata->f.nummod > 0) {
1237 if((retval = invalidateObj(tdata)) != 0) {
1238 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
1243 if(transComProcess(tdata, transinfo) != 0) {
1244 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1249 printf("ERROR...No Decision\n");
1253 if (transinfo->objlocked != NULL) {
1254 free(transinfo->objlocked);
1256 if (transinfo->objnotfound != NULL) {
1257 free(transinfo->objnotfound);
1261 /* This function decides the reponse that needs to be sent to
1262 * all Participant machines after the TRANS_REQUEST protocol */
1263 char decideResponse(char *getReplyCtrl, char *treplyretry, int pilecount) {
1264 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
1266 for (i = 0 ; i < pilecount; i++) {
1268 control = getReplyCtrl[i];
1271 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
1273 /* treat as disagree, pass thru */
1274 case TRANS_DISAGREE:
1282 case TRANS_SOFT_ABORT:
1288 if(transdisagree > 0) {
1293 /* clear objects from prefetch cache */
1296 } else if(transagree == pilecount) {
1299 return TRANS_COMMIT;
1301 /* Send Abort in soft abort case followed by retry commiting transaction again*/
1308 /* This function opens a connection, places an object read request to
1309 * the remote machine, reads the control message and object if
1310 * available and copies the object and its header to the local
1313 void *getRemoteObj(unsigned int mnum, unsigned int oid) {
1315 struct sockaddr_in serv_addr;
1319 void *objcopy = NULL;
1321 int sd = getSock2(transReadSockPool, mnum);
1322 char readrequest[sizeof(char)+sizeof(unsigned int)];
1323 readrequest[0] = READ_REQUEST;
1324 *((unsigned int *)(&readrequest[1])) = oid;
1325 send_data(sd, readrequest, sizeof(readrequest));
1327 /* Read response from the Participant */
1328 recv_data(sd, &control, sizeof(char));
1330 if (control==OBJECT_NOT_FOUND) {
1333 /* Read object if found into local cache */
1334 recv_data(sd, &size, sizeof(int));
1335 objcopy = objstrAlloc(&t_cache, size);
1336 recv_data(sd, objcopy, size);
1338 /* Insert into cache's lookup table */
1339 t_chashInsert(oid, objcopy);
1341 totalObjSize += size;
1348 /* Commit info for objects modified */
1349 void commitCountForObjMod(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1350 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1352 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1353 /* Save the oids not found and number of oids not found for later use */
1354 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1355 /* Save the oids not found and number of oids not found for later use */
1356 oidnotfound[*numoidnotfound] = oid;
1357 (*numoidnotfound)++;
1358 } else { /* If Obj found in machine (i.e. has not moved) */
1359 /* Check if Obj is locked by any previous transaction */
1360 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
1361 if (version == ((objheader_t *)mobj)->version) { /* match versions */
1363 //Keep track of what is locked
1364 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1365 } else { /* If versions don't match ...HARD ABORT */
1367 /* Send TRANS_DISAGREE to Coordinator */
1368 *getReplyCtrl = TRANS_DISAGREE;
1370 //Keep track of what is locked
1371 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1372 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1375 } else { //A lock is acquired some place else
1376 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1378 } else { /* If versions don't match ...HARD ABORT */
1380 /* Send TRANS_DISAGREE to Coordinator */
1381 *getReplyCtrl = TRANS_DISAGREE;
1382 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1389 /* Commit info for objects modified */
1390 void commitCountForObjRead(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1391 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1393 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1394 /* Save the oids not found and number of oids not found for later use */
1395 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1396 /* Save the oids not found and number of oids not found for later use */
1397 oidnotfound[*numoidnotfound] = oid;
1398 (*numoidnotfound)++;
1399 } else { /* If Obj found in machine (i.e. has not moved) */
1400 /* Check if Obj is locked by any previous transaction */
1401 if (read_trylock(STATUSPTR(mobj))) { // Can further acquire read locks
1402 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
1404 //Keep track of what is locked
1405 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1406 } else { /* If versions don't match ...HARD ABORT */
1408 /* Send TRANS_DISAGREE to Coordinator */
1409 *getReplyCtrl = TRANS_DISAGREE;
1410 //Keep track of what is locked
1411 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1412 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1415 } else { //Has reached max number of readers or some other transaction
1416 //has acquired a lock on this object
1417 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1419 } else { /* If versions don't match ...HARD ABORT */
1421 /* Send TRANS_DISAGREE to Coordinator */
1422 *getReplyCtrl = TRANS_DISAGREE;
1423 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1430 /* This function completes the ABORT process if the transaction is aborting */
1431 int transAbortProcess(trans_commit_data_t *transinfo) {
1433 unsigned int *objlocked;
1436 numlocked = transinfo->numlocked;
1437 objlocked = transinfo->objlocked;
1439 int useWriteUnlock = 0;
1440 for (i = 0; i < numlocked; i++) {
1441 if(objlocked[i] == -1) {
1445 if((header = mhashSearch(objlocked[i])) == NULL) {
1446 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1449 if(!useWriteUnlock) {
1450 read_unlock(STATUSPTR(header));
1452 write_unlock(STATUSPTR(header));
1459 /*This function completes the COMMIT process if the transaction is commiting*/
1460 int transComProcess(trans_req_data_t *tdata, trans_commit_data_t *transinfo) {
1461 objheader_t *header, *tcptr;
1462 int i, nummod, tmpsize, numcreated, numlocked;
1463 unsigned int *oidmod, *oidcreated, *oidlocked;
1466 nummod = tdata->f.nummod;
1467 oidmod = tdata->oidmod;
1468 numcreated = tdata->f.numcreated;
1469 oidcreated = tdata->oidcreated;
1470 numlocked = transinfo->numlocked;
1471 oidlocked = transinfo->objlocked;
1473 for (i = 0; i < nummod; i++) {
1474 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1475 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1478 /* Copy from transaction cache -> main object store */
1479 if ((tcptr = ((objheader_t *) t_chashSearch(oidmod[i]))) == NULL) {
1480 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1483 GETSIZE(tmpsize, header);
1484 char *tmptcptr = (char *) tcptr;
1486 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
1487 struct ___Object___ *src=(struct ___Object___*)((char*)tmptcptr+sizeof(objheader_t));
1488 dst->___cachedCode___=src->___cachedCode___;
1489 dst->___cachedHash___=src->___cachedHash___;
1491 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
1494 header->version += 1;
1495 if(header->notifylist != NULL) {
1496 notifyAll(&header->notifylist, OID(header), header->version);
1499 /* If object is newly created inside transaction then commit it */
1500 for (i = 0; i < numcreated; i++) {
1501 if ((header = ((objheader_t *) t_chashSearch(oidcreated[i]))) == NULL) {
1502 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1505 GETSIZE(tmpsize, header);
1506 tmpsize += sizeof(objheader_t);
1507 pthread_mutex_lock(&mainobjstore_mutex);
1508 if ((ptrcreate = objstrAlloc(&mainobjstore, tmpsize)) == NULL) {
1509 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1510 pthread_mutex_unlock(&mainobjstore_mutex);
1513 pthread_mutex_unlock(&mainobjstore_mutex);
1514 /* Initialize read and write locks */
1515 initdsmlocks(STATUSPTR(header));
1516 memcpy(ptrcreate, header, tmpsize);
1517 mhashInsert(oidcreated[i], ptrcreate);
1518 lhashInsert(oidcreated[i], myIpAddr);
1520 /* Unlock locked objects */
1521 int useWriteUnlock = 0;
1522 for(i = 0; i < numlocked; i++) {
1523 if(oidlocked[i] == -1) {
1527 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1528 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1531 if(!useWriteUnlock) {
1532 read_unlock(STATUSPTR(header));
1534 write_unlock(STATUSPTR(header));
1540 prefetchpile_t *foundLocal(char *ptr, int numprefetches) {
1543 prefetchpile_t * head=NULL;
1545 for(j=0;j<numprefetches;j++) {
1546 int siteid = *(GET_SITEID(ptr));
1547 int ntuples = *(GET_NTUPLES(ptr));
1548 unsigned int * oidarray = GET_PTR_OID(ptr);
1549 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1550 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1553 for(i=0; i<ntuples; i++) {
1554 unsigned short baseindex=(i==0) ? 0 : endoffsets[i-1];
1555 unsigned short endindex=endoffsets[i];
1556 unsigned int oid=oidarray[i];
1562 //Look up fields locally
1563 for(newbase=baseindex; newbase<endindex; newbase++) {
1564 if (!lookupObject(&oid, arryfields[newbase]))
1566 //Ended in a null pointer...
1570 //Entire prefetch is local
1571 if (newbase==endindex&&checkoid(oid)) {
1575 //Add to remote requests
1576 machinenum=lhashSearch(oid);
1577 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1582 /* handle dynamic prefetching */
1583 handleDynPrefetching(numLocal, ntuples, siteid);
1584 ptr=((char *)&arryfields[endoffsets[ntuples-1]])+sizeof(int);
1590 int checkoid(unsigned int oid) {
1591 objheader_t *header;
1592 if ((header=mhashSearch(oid))!=NULL) {
1595 } else if ((header=prehashSearch(oid))!=NULL) {
1603 int lookupObject(unsigned int * oid, short offset) {
1604 objheader_t *header;
1605 if ((header=mhashSearch(*oid))!=NULL) {
1608 } else if ((header=prehashSearch(*oid))!=NULL) {
1615 if(TYPE(header) >= NUMCLASSES) {
1616 int elementsize = classsize[TYPE(header)];
1617 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1618 int length = ao->___length___;
1619 /* Check if array out of bounds */
1620 if(offset < 0 || offset >= length) {
1621 //if yes treat the object as found
1625 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1628 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1634 /* This function is called by the thread calling transPrefetch */
1635 void *transPrefetch(void *t) {
1637 /* read from prefetch queue */
1638 void *node=gettail();
1639 /* Check if the tuples are found locally, if yes then reduce them further*/
1640 /* and group requests by remote machine ids by calling the makePreGroups() */
1641 int count=numavailable();
1642 prefetchpile_t *pilehead = foundLocal(node, count);
1644 if (pilehead!=NULL) {
1645 // Get sock from shared pool
1647 /* Send Prefetch Request */
1648 prefetchpile_t *ptr = pilehead;
1649 while(ptr != NULL) {
1650 int sd = getSock2(transPrefetchSockPool, ptr->mid);
1651 sendPrefetchReq(ptr, sd);
1655 /* Release socket */
1656 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1658 /* Deallocated pilehead */
1659 mcdealloc(pilehead);
1661 // Deallocate the prefetch queue pile node
1666 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1669 int size=sizeof(char)+sizeof(int);
1670 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1671 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1676 *buf=TRANS_PREFETCH;
1679 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1680 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1683 *((unsigned int *)buf)=tmp->oid;
1684 buf+=sizeof(unsigned int);
1685 *((unsigned int *)(buf)) = myIpAddr;
1686 buf+=sizeof(unsigned int);
1687 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1688 buf+=tmp->numoffset*sizeof(short);
1691 send_data(sd, buft, size);
1695 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1699 struct writestruct writebuffer;
1700 writebuffer.offset=0;
1702 /* Send TRANS_PREFETCH control message */
1705 /* Send Oids and offsets in pairs */
1706 tmp = mcpilenode->objpiles;
1707 while(tmp != NULL) {
1708 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1709 char oidnoffset[len+5];
1710 char *buf=oidnoffset;
1712 *buf=TRANS_PREFETCH;
1716 *((int*)buf) = tmp->numoffset;
1718 *((unsigned int *)buf) = tmp->oid;
1722 buf+=sizeof(unsigned int);
1723 *((unsigned int *)buf) = myIpAddr;
1724 buf += sizeof(unsigned int);
1725 memcpy(buf, tmp->offset, (tmp->numoffset)*sizeof(short));
1728 *((int *)(&oidnoffset[len]))=-1;
1732 send_buf(sd, & writebuffer, oidnoffset, len);
1734 forcesend_buf(sd, & writebuffer, oidnoffset, len);
1741 int getPrefetchResponse(int sd, struct readstruct *readbuffer) {
1742 int length = 0, size = 0;
1745 void *modptr, *oldptr;
1747 recv_data_buf(sd, readbuffer, &length, sizeof(int));
1748 size = length - sizeof(int);
1749 char recvbuffer[size];
1754 recv_data_buf(sd, readbuffer, recvbuffer, size);
1755 control = *((char *) recvbuffer);
1756 if(control == OBJECT_FOUND) {
1757 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1758 size = size - (sizeof(char) + sizeof(unsigned int));
1759 pthread_mutex_lock(&prefetchcache_mutex);
1760 if ((modptr = prefetchobjstrAlloc(size)) == NULL) {
1761 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1762 pthread_mutex_unlock(&prefetchcache_mutex);
1765 pthread_mutex_unlock(&prefetchcache_mutex);
1766 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1769 /* Insert the oid and its address into the prefetch hash lookup table */
1770 /* Do a version comparison if the oid exists */
1771 if((oldptr = prehashSearch(oid)) != NULL) {
1772 /* If older version then update with new object ptr */
1773 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1775 prehashInsert(oid, modptr);
1777 } else { /* Else add the object ptr to hash table*/
1778 prehashInsert(oid, modptr);
1780 /* Lock the Prefetch Cache look up table*/
1781 pthread_mutex_lock(&pflookup.lock);
1782 /* Broadcast signal on prefetch cache condition variable */
1783 pthread_cond_broadcast(&pflookup.cond);
1784 /* Unlock the Prefetch Cache look up table*/
1785 pthread_mutex_unlock(&pflookup.lock);
1786 } else if(control == OBJECT_NOT_FOUND) {
1787 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1788 /* TODO: For each object not found query DHT for new location and retrieve the object */
1789 /* Throw an error */
1790 //printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1793 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1799 unsigned short getObjType(unsigned int oid) {
1800 objheader_t *objheader;
1801 unsigned short numoffset[] ={0};
1802 short fieldoffset[] ={};
1804 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1806 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1808 unsigned int mid = lhashSearch(oid);
1809 int sd = getSock2(transReadSockPool, mid);
1810 char remotereadrequest[sizeof(char)+sizeof(unsigned int)];
1811 remotereadrequest[0] = READ_REQUEST;
1812 *((unsigned int *)(&remotereadrequest[1])) = oid;
1813 send_data(sd, remotereadrequest, sizeof(remotereadrequest));
1815 /* Read response from the Participant */
1817 recv_data(sd, &control, sizeof(char));
1819 if (control==OBJECT_NOT_FOUND) {
1820 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1824 /* Read object if found into local cache */
1826 recv_data(sd, &size, sizeof(int));
1828 pthread_mutex_lock(&prefetchcache_mutex);
1829 if ((objheader = prefetchobjstrAlloc(size)) == NULL) {
1830 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1833 pthread_mutex_unlock(&prefetchcache_mutex);
1834 recv_data(sd, objheader, size);
1835 prehashInsert(oid, objheader);
1836 return TYPE(objheader);
1839 if((buffer = calloc(1, size)) == NULL) {
1840 printf("%s() Calloc Error %s at line %d\n", __func__, __FILE__, __LINE__);
1844 recv_data(sd, buffer, size);
1845 objheader = (objheader_t *)buffer;
1846 unsigned short type = TYPE(objheader);
1855 return TYPE(objheader);
1858 int startRemoteThread(unsigned int oid, unsigned int mid) {
1860 struct sockaddr_in remoteAddr;
1861 char msg[1 + sizeof(unsigned int)];
1865 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1866 perror("startRemoteThread():socket()");
1870 bzero(&remoteAddr, sizeof(remoteAddr));
1871 remoteAddr.sin_family = AF_INET;
1872 remoteAddr.sin_port = htons(LISTEN_PORT);
1873 remoteAddr.sin_addr.s_addr = htonl(mid);
1875 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1876 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1877 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1881 msg[0] = START_REMOTE_THREAD;
1882 *((unsigned int *) &msg[1]) = oid;
1883 send_data(sock, msg, 1 + sizeof(unsigned int));
1890 //TODO: when reusing oids, make sure they are not already in use!
1891 static unsigned int id = 0xFFFFFFFF;
1892 unsigned int getNewOID(void) {
1894 if (id > oidMax || id < oidMin) {
1900 int processConfigFile() {
1902 const int maxLineLength = 200;
1903 char lineBuffer[maxLineLength];
1905 const char *delimiters = " \t\n";
1909 configFile = fopen(CONFIG_FILENAME, "r");
1910 if (configFile == NULL) {
1911 printf("error opening %s:\n", CONFIG_FILENAME);
1916 numHostsInSystem = 0;
1917 sizeOfHostArray = 8;
1918 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1920 while(fgets(lineBuffer, maxLineLength, configFile) != NULL) {
1921 commentBegin = strchr(lineBuffer, '#');
1922 if (commentBegin != NULL)
1923 *commentBegin = '\0';
1924 token = strtok(lineBuffer, delimiters);
1925 while (token != NULL) {
1926 tmpAddr = inet_addr(token);
1927 if ((int)tmpAddr == -1) {
1928 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1932 addHost(htonl(tmpAddr));
1933 token = strtok(NULL, delimiters);
1939 if (numHostsInSystem < 1) {
1940 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1944 myIpAddr = getMyIpAddr("en1");
1946 myIpAddr = getMyIpAddr("eth0");
1948 myIndexInHostArray = findHost(myIpAddr);
1949 if (myIndexInHostArray == -1) {
1950 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1953 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1954 oidMin = oidsPerBlock * myIndexInHostArray;
1955 if (myIndexInHostArray == numHostsInSystem - 1)
1956 oidMax = 0xFFFFFFFF;
1958 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1963 void addHost(unsigned int hostIp) {
1964 unsigned int *tmpArray;
1966 if (findHost(hostIp) != -1)
1969 if (numHostsInSystem == sizeOfHostArray) {
1970 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1971 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1973 hostIpAddrs = tmpArray;
1976 hostIpAddrs[numHostsInSystem++] = hostIp;
1981 int findHost(unsigned int hostIp) {
1983 for (i = 0; i < numHostsInSystem; i++)
1984 if (hostIpAddrs[i] == hostIp)
1991 /* This function sends notification request per thread waiting on object(s) whose version
1993 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1995 objheader_t *objheader;
1996 struct sockaddr_in remoteAddr;
1997 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
2001 unsigned short version;
2002 unsigned int oid,mid;
2003 static unsigned int threadid = 0;
2004 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
2005 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
2006 notifydata_t *ndata;
2009 if((mid = lhashSearch(oid)) == 0) {
2010 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
2014 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2015 perror("reqNotify():socket()");
2019 bzero(&remoteAddr, sizeof(remoteAddr));
2020 remoteAddr.sin_family = AF_INET;
2021 remoteAddr.sin_port = htons(LISTEN_PORT);
2022 remoteAddr.sin_addr.s_addr = htonl(mid);
2024 /* Generate unique threadid */
2027 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
2028 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
2029 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
2032 ndata->numoid = numoid;
2033 ndata->threadid = threadid;
2034 ndata->oidarry = oidarry;
2035 ndata->versionarry = versionarry;
2036 ndata->threadcond = threadcond;
2037 ndata->threadnotify = threadnotify;
2038 if((status = notifyhashInsert(threadid, ndata)) != 0) {
2039 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
2044 /* Send number of oids, oidarry, version array, machine id and threadid */
2045 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
2046 printf("reqNotify():error %d connecting to %s:%d\n", errno,
2047 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
2051 msg[0] = THREAD_NOTIFY_REQUEST;
2052 *((unsigned int *)(&msg[1])) = numoid;
2053 /* Send array of oids */
2054 size = sizeof(unsigned int);
2056 for(i = 0;i < numoid; i++) {
2058 *((unsigned int *)(&msg[1] + size)) = oid;
2059 size += sizeof(unsigned int);
2062 /* Send array of version */
2063 for(i = 0;i < numoid; i++) {
2064 version = versionarry[i];
2065 *((unsigned short *)(&msg[1] + size)) = version;
2066 size += sizeof(unsigned short);
2069 *((unsigned int *)(&msg[1] + size)) = myIpAddr; size += sizeof(unsigned int);
2070 *((unsigned int *)(&msg[1] + size)) = threadid;
2071 pthread_mutex_lock(&(ndata->threadnotify));
2072 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
2073 send_data(sock, msg, size);
2074 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
2075 pthread_mutex_unlock(&(ndata->threadnotify));
2078 pthread_cond_destroy(&threadcond);
2079 pthread_mutex_destroy(&threadnotify);
2085 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
2086 notifydata_t *ndata;
2087 int i, objIsFound = 0, index;
2090 //Look up the tid and call the corresponding pthread_cond_signal
2091 if((ndata = notifyhashSearch(tid)) == NULL) {
2092 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
2095 for(i = 0; i < ndata->numoid; i++) {
2096 if(ndata->oidarry[i] == oid) {
2101 if(objIsFound == 0) {
2102 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
2105 if(version <= ndata->versionarry[index]) {
2106 printf("threadNotify(): New version %d has not changed since last version for oid = %d, %s, %d\n", version, oid, __FILE__, __LINE__);
2110 /* Clear from prefetch cache and free thread related data structure */
2111 if((ptr = prehashSearch(oid)) != NULL) {
2115 pthread_mutex_lock(&(ndata->threadnotify));
2116 pthread_cond_signal(&(ndata->threadcond));
2117 pthread_mutex_unlock(&(ndata->threadnotify));
2124 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
2127 struct sockaddr_in remoteAddr;
2128 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
2129 int sock, status, size, bytesSent;
2131 while(*head != NULL) {
2134 //create a socket connection to that machine
2135 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2136 perror("notifyAll():socket()");
2140 bzero(&remoteAddr, sizeof(remoteAddr));
2141 remoteAddr.sin_family = AF_INET;
2142 remoteAddr.sin_port = htons(LISTEN_PORT);
2143 remoteAddr.sin_addr.s_addr = htonl(mid);
2144 //send Thread Notify response and threadid to that machine
2145 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
2146 printf("notifyAll():error %d connecting to %s:%d\n", errno,
2147 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
2151 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
2152 msg[0] = THREAD_NOTIFY_RESPONSE;
2153 *((unsigned int *)&msg[1]) = oid;
2154 size = sizeof(unsigned int);
2155 *((unsigned short *)(&msg[1]+ size)) = version;
2156 size+= sizeof(unsigned short);
2157 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
2159 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
2160 send_data(sock, msg, size);
2173 removetransactionhash();
2175 objstrDelete(t_cache);
2179 /* This function inserts necessary information into
2180 * a machine pile data structure */
2181 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
2182 plistnode_t *ptr, *tmp;
2183 int found = 0, offset = 0;
2186 //Add oid into a machine that is already present in the pile linked list structure
2187 while(tmp != NULL) {
2188 if (tmp->mid == mid) {
2191 if (STATUS(headeraddr) & NEW) {
2192 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
2194 GETSIZE(tmpsize, headeraddr);
2195 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
2196 } else if (STATUS(headeraddr) & DIRTY) {
2197 tmp->oidmod[tmp->nummod] = OID(headeraddr);
2199 GETSIZE(tmpsize, headeraddr);
2200 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
2202 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
2203 *((unsigned int *)(((char *)tmp->objread) + offset))=OID(headeraddr);
2204 offset += sizeof(unsigned int);
2205 *((short *)(((char *)tmp->objread) + offset)) = headeraddr->version;
2213 //Add oid for any new machine
2216 if((ptr = pCreate(num_objs)) == NULL) {
2220 if (STATUS(headeraddr) & NEW) {
2221 ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
2223 GETSIZE(tmpsize, headeraddr);
2224 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
2225 } else if (STATUS(headeraddr) & DIRTY) {
2226 ptr->oidmod[ptr->nummod] = OID(headeraddr);
2228 GETSIZE(tmpsize, headeraddr);
2229 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
2231 *((unsigned int *)ptr->objread)=OID(headeraddr);
2232 offset = sizeof(unsigned int);
2233 *((short *)(((char *)ptr->objread) + offset)) = headeraddr->version;
2241 STATUS(headeraddr) =0;
2247 plistnode_t *sortPiles(plistnode_t *pileptr) {
2248 plistnode_t *head, *ptr, *tail;
2251 /* Get tail pointer */
2257 plistnode_t *prev = pileptr;
2258 /* Arrange local machine processing at the end of the pile list */
2259 while(ptr != NULL) {
2261 if(ptr->mid == myIpAddr && (prev != pileptr)) {
2262 prev->next = ptr->next;
2267 if((ptr->mid == myIpAddr) && (prev == pileptr)) {