2 #include "debugmacro.h"
4 #include "machinepile.h"
5 #include "altmlookup.h"
8 #include "altprelookup.h"
9 #include "threadnotify.h"
11 #include "addUdpEnhance.h"
12 #include "addPrefetchEnhance.h"
20 #include "abortreaders.h"
25 #define CONFIG_FILENAME "dstm.conf"
27 //#define LOGEVENTS //turn on Logging events
29 char bigarray[16*1024*1024];
31 #define LOGEVENT(x) { \
41 char bigarray1[6*1024*1024];
42 unsigned int bigarray2[6*1024*1024];
43 unsigned int bigarray3[6*1024*1024];
44 long long bigarray4[6*1024*1024];
45 int bigarray5[6*1024*1024];
47 #define LOGTIME(x,y,z,a,b) {\
57 #define LOGTIME(x,y,z,a,b)
60 /* Thread transaction variables */
62 __thread objstr_t *t_cache;
63 __thread struct ___Object___ *revertlist;
66 __thread jmp_buf aborttrans;
69 int globalid=0; /* This variable is a unique global identifier for a sendPrefetch request */
71 /* Global Variables */
72 extern int classsize[];
73 pfcstats_t *evalPrefetch;
74 extern int numprefetchsites; //Global variable containing number of prefetch sites
75 extern pthread_mutex_t mainobjstore_mutex; // Mutex to lock main Object store
76 pthread_mutex_t prefetchcache_mutex; // Mutex to lock Prefetch Cache
77 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
78 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
79 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
80 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
81 extern objstr_t *mainobjstore;
82 unsigned int myIpAddr;
83 unsigned int *hostIpAddrs;
86 int myIndexInHostArray;
87 unsigned int oidsPerBlock;
90 sockPoolHashTable_t *transReadSockPool;
91 sockPoolHashTable_t *transPrefetchSockPool;
92 sockPoolHashTable_t *transRequestSockPool;
93 pthread_mutex_t notifymutex;
94 pthread_mutex_t atomicObjLock;
95 struct timespec exponential_backoff;
96 static int count_exponential_backoff = 0;
97 static const int max_exponential_backoff = 1000; // safety limit
100 /***********************************
101 * Global Variables for statistics
102 **********************************/
103 int numTransCommit = 0;
104 int numTransAbort = 0;
105 int nchashSearch = 0;
106 int nmhashSearch = 0;
107 int nprehashSearch = 0;
108 int ndirtyCacheObj = 0;
113 int totalObjSize = 0;
114 int sendRemoteReq = 0;
117 void printhex(unsigned char *, int);
118 plistnode_t *createPiles();
119 plistnode_t *sortPiles(plistnode_t *pileptr);
123 /*******************************
124 * Send and Recv function calls
125 *******************************/
126 void send_data(int fd, void *buf, int buflen) {
127 char *buffer = (char *)(buf);
131 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
132 bytesSent = bytesSent + numbytes;
133 if (numbytes == -1) {
142 void send_buf(int fd, struct writestruct * sendbuffer, void *buffer, int buflen) {
143 if (buflen+sendbuffer->offset>WMAXBUF) {
144 send_data(fd, sendbuffer->buf, sendbuffer->offset);
145 sendbuffer->offset=0;
146 send_data(fd, buffer, buflen);
149 memcpy(&sendbuffer->buf[sendbuffer->offset], buffer, buflen);
150 sendbuffer->offset+=buflen;
151 if (sendbuffer->offset>WTOP) {
152 send_data(fd, sendbuffer->buf, sendbuffer->offset);
153 sendbuffer->offset=0;
157 void forcesend_buf(int fd, struct writestruct * sendbuffer, void *buffer, int buflen) {
158 if (buflen+sendbuffer->offset>WMAXBUF) {
159 send_data(fd, sendbuffer->buf, sendbuffer->offset);
160 sendbuffer->offset=0;
161 send_data(fd, buffer, buflen);
164 memcpy(&sendbuffer->buf[sendbuffer->offset], buffer, buflen);
165 sendbuffer->offset+=buflen;
166 send_data(fd, sendbuffer->buf, sendbuffer->offset);
167 sendbuffer->offset=0;
170 int recvw(int fd, void *buf, int len, int flags) {
171 return recv(fd, buf, len, flags);
174 void recv_data_buf(int fd, struct readstruct * readbuffer, void *buffer, int buflen) {
175 char *buf=(char *)buffer;
176 int numbytes=readbuffer->head-readbuffer->tail;
180 memcpy(buf, &readbuffer->buf[readbuffer->tail], numbytes);
181 readbuffer->tail+=numbytes;
188 if (buflen>=MAXBUF) {
189 recv_data(fd, buf, buflen);
198 int numbytes = recvw(fd, &readbuffer->buf[readbuffer->head], maxbuf, 0);
199 if (numbytes == -1) {
205 readbuffer->head+=numbytes;
208 memcpy(buf,readbuffer->buf,obufflen);
209 readbuffer->tail=obufflen;
212 int recv_data_errorcode_buf(int fd, struct readstruct * readbuffer, void *buffer, int buflen) {
213 char *buf=(char *)buffer;
215 int numbytes=readbuffer->head-readbuffer->tail;
219 memcpy(buf, &readbuffer->buf[readbuffer->tail], numbytes);
220 readbuffer->tail+=numbytes;
227 if (buflen>=MAXBUF) {
228 return recv_data_errorcode(fd, buf, buflen);
236 int numbytes = recvw(fd, &readbuffer->buf[readbuffer->head], maxbuf, 0);
246 readbuffer->head+=numbytes;
249 memcpy(buf,readbuffer->buf,obufflen);
250 readbuffer->tail=obufflen;
255 void recv_data(int fd, void *buf, int buflen) {
256 char *buffer = (char *)(buf);
260 numbytes = recvw(fd, buffer, size, 0);
261 bytesRecv = bytesRecv + numbytes;
262 if (numbytes == -1) {
271 int recv_data_errorcode(int fd, void *buf, int buflen) {
272 char *buffer = (char *)(buf);
276 numbytes = recvw(fd, buffer, size, 0);
279 if (numbytes == -1) {
290 void printhex(unsigned char *ptr, int numBytes) {
292 for (i = 0; i < numBytes; i++) {
294 printf("0%x ", ptr[i]);
296 printf("%x ", ptr[i]);
302 inline int arrayLength(int *array) {
304 for(i=0 ; array[i] != -1; i++)
309 inline int findmax(int *array, int arraylength) {
312 for(i = 0; i < arraylength; i++) {
320 //#define INLINEPREFETCH
321 #define PREFTHRESHOLD 0
323 /* This function is a prefetch call generated by the compiler that
324 * populates the shared primary prefetch queue*/
325 void prefetch(int siteid, int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
326 /* Allocate for the queue node*/
327 int qnodesize = 2*sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
329 #ifdef INLINEPREFETCH
333 node=getmemory(qnodesize);
334 if (node==NULL&&attempted)
338 char *node=getmemory(qnodesize);
340 int top=endoffsets[ntuples-1];
346 /* Set queue node values */
348 /* TODO: Remove this after testing */
349 evalPrefetch[siteid].callcount++;
351 *((int *)(node))=siteid;
352 *((int *)(node + sizeof(int))) = ntuples;
354 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
355 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
356 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
358 #ifdef INLINEPREFETCH
361 int numpref=numavailable();
364 if (node==NULL && numpref!=0 || numpref>=PREFTHRESHOLD) {
366 prefetchpile_t *pilehead = foundLocal(node,numpref,siteid);
367 if (pilehead!=NULL) {
368 // Get sock from shared pool
370 /* Send Prefetch Request */
371 prefetchpile_t *ptr = pilehead;
374 int sd = getSock2(transPrefetchSockPool, ptr->mid);
375 sendPrefetchReq(ptr, sd, globalid);
382 }//end do prefetch if condition
385 /* Lock and insert into primary prefetch queue */
390 /* This function starts up the transaction runtime. */
391 int dstmStartup(const char * option) {
392 pthread_t thread_Listen, udp_thread_Listen;
394 int master=option!=NULL && strcmp(option, "master")==0;
398 if (processConfigFile() != 0)
399 return 0; //TODO: return error value, cause main program to exit
406 printf("Trans stats is on\n");
413 //Initialize socket pool
414 transReadSockPool = createSockPool(transReadSockPool, DEFAULTSOCKPOOLSIZE);
415 transPrefetchSockPool = createSockPool(transPrefetchSockPool, DEFAULTSOCKPOOLSIZE);
416 transRequestSockPool = createSockPool(transRequestSockPool, DEFAULTSOCKPOOLSIZE);
422 pthread_attr_init(&attr);
423 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
426 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
429 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
432 dstmListen((void *)fd);
437 //TODO Use this later
438 void *pCacheAlloc(objstr_t *store, unsigned int size) {
444 while(ptr->next != NULL) {
445 /* check if store is empty */
446 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
461 /* This function initiates the prefetch thread A queue is shared
462 * between the main thread of execution and the prefetch thread to
463 * process the prefetch call Call from compiler populates the shared
464 * queue with prefetch requests while prefetch thread processes the
465 * prefetch requests */
468 //Create and initialize prefetch cache structure
471 if((evalPrefetch = initPrefetchStats()) == NULL) {
472 printf("%s() Error allocating memory at %s, %d\n", __func__, __FILE__, __LINE__);
477 /* Initialize attributes for mutex */
478 pthread_mutexattr_init(&prefetchcache_mutex_attr);
479 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
481 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
482 pthread_mutex_init(¬ifymutex, NULL);
483 pthread_mutex_init(&atomicObjLock, NULL);
485 //Create prefetch cache lookup table
486 if(prehashCreate(PHASH_SIZE, PLOADFACTOR)) {
491 //Initialize primary shared queue
493 //Initialize machine pile w/prefetch oids and offsets shared queue
496 //Create the primary prefetch thread
500 retval=pthread_create(&tPrefetch, NULL, transPrefetchNew, NULL);
503 #ifndef INLINEPREFETCH
505 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
509 #ifndef INLINEPREFETCH
510 pthread_detach(tPrefetch);
515 /* This function stops the threads spawned */
519 pthread_cancel(tPrefetch);
520 for(t = 0; t < NUM_THREADS; t++)
521 pthread_cancel(wthreads[t]);
527 /* This functions inserts randowm wait delays in the order of msec
528 * Mostly used when transaction commits retry*/
535 req.tv_nsec = (long)(1000 + (t%10000)); //1-11 microsec
536 nanosleep(&req, NULL);
540 void exponentialdelay() {
541 exponential_backoff.tv_nsec = exponential_backoff.tv_nsec * 2;
542 nanosleep(&exponential_backoff, NULL);
543 ++count_exponential_backoff;
544 if (count_exponential_backoff >= max_exponential_backoff) {
545 printf(" reached max_exponential_backoff at %s, %s(), %d\n", __FILE__, __func__, __LINE__);
551 /* This function initializes things required in the transaction start*/
553 t_cache = objstrCreate(1048576);
554 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
561 // Search for an address for a given oid
562 /*#define INLINE inline __attribute__((always_inline))
564 INLINE void * chashSearchI(chashtable_t *table, unsigned int key) {
565 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
566 chashlistnode_t *node = &table->table[(key & table->mask)>>1];
569 if(node->key == key) {
573 } while(node != NULL);
581 /* This function finds the location of the objects involved in a transaction
582 * and returns the pointer to the object if found in a remote location */
583 __attribute__((pure)) objheader_t *transRead(unsigned int oid) {
584 unsigned int machinenumber;
585 objheader_t *tmp, *objheader;
586 objheader_t *objcopy;
589 chashlistnode_t *node;
596 node= &c_table[(oid & c_mask)>>1];
598 if(node->key == oid) {
603 return &((objheader_t*)node->val)[1];
609 } while(node != NULL);
613 if((objheader = chashSearchI(record->lookupTable, oid)) != NULL) {
618 return &objheader[1];
627 //abort this transaction
628 removetransactionhash();
629 objstrDelete(t_cache);
631 _longjmp(aborttrans,1);
636 if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
640 /* Look up in machine lookup table and copy into cache*/
641 GETSIZE(size, objheader);
642 size += sizeof(objheader_t);
643 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
644 memcpy(objcopy, objheader, size);
645 /* Insert into cache's lookup table */
647 t_chashInsert(OID(objheader), objcopy);
655 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
656 if(STATUS(tmp) & DIRTY) {
665 /* Look up in prefetch cache */
667 size+=sizeof(objheader_t);
668 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
669 memcpy(objcopy, tmp, size);
670 /* Insert into cache's lookup table */
671 t_chashInsert(OID(tmp), objcopy);
680 /* Get the object from the remote location */
681 if((machinenumber = lhashSearch(oid)) == 0) {
682 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
685 objcopy = getRemoteObj(machinenumber, oid);
687 if(objcopy == NULL) {
688 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
696 //Copy object to prefetch cache
697 pthread_mutex_lock(&prefetchcache_mutex);
698 objheader_t *headerObj;
700 GETSIZE(size, objcopy);
701 if((headerObj = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
702 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
704 pthread_mutex_unlock(&prefetchcache_mutex);
707 pthread_mutex_unlock(&prefetchcache_mutex);
708 memcpy(headerObj, objcopy, size+sizeof(objheader_t));
709 //make an entry in prefetch lookup hashtable
710 prehashInsert(oid, headerObj);
722 /* This function finds the location of the objects involved in a transaction
723 * and returns the pointer to the object if found in a remote location */
724 __attribute__((pure)) objheader_t *transRead2(unsigned int oid) {
725 //DEBUG: __attribute__((pure)) objheader_t *transRead2(unsigned int oid, char tmpptr[]) {
726 unsigned int machinenumber;
727 objheader_t *tmp, *objheader;
728 objheader_t *objcopy;
733 //abort this transaction
734 removetransactionhash();
735 objstrDelete(t_cache);
737 _longjmp(aborttrans,1);
742 if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
746 /* Look up in machine lookup table and copy into cache*/
747 GETSIZE(size, objheader);
748 size += sizeof(objheader_t);
749 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
750 memcpy(objcopy, objheader, size);
751 /* Insert into cache's lookup table */
753 t_chashInsert(OID(objheader), objcopy);
761 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
762 if(STATUS(tmp) & DIRTY) {
772 /* Look up in prefetch cache */
774 size+=sizeof(objheader_t);
775 objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
776 memcpy(objcopy, tmp, size);
777 LOGOIDTYPE("P",oid, TYPE(objcopy), myrdtsc());
778 /* Insert into cache's lookup table */
779 t_chashInsert(OID(tmp), objcopy);
788 /* Get the object from the remote location */
789 if((machinenumber = lhashSearch(oid)) == 0) {
790 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
793 objcopy = getRemoteObj(machinenumber, oid);
799 if(objcopy == NULL) {
800 printf("Error: Object %u not found in Remote location %s, %d\n", oid,__FILE__, __LINE__);
805 LOGOIDTYPE("RR",oid, TYPE(objcopy),myrdtsc());
806 LOGTIME('r', oid, TYPE(objcopy),myrdtsc(),0);
807 //Copy object to prefetch cache
808 pthread_mutex_lock(&prefetchcache_mutex);
809 objheader_t *headerObj;
811 GETSIZE(size, objcopy);
812 if((headerObj = prefetchobjstrAlloc(size+sizeof(objheader_t))) == NULL) {
813 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
815 pthread_mutex_unlock(&prefetchcache_mutex);
818 pthread_mutex_unlock(&prefetchcache_mutex);
819 memcpy(headerObj, objcopy, size+sizeof(objheader_t));
820 //make an entry in prefetch lookup hashtable
821 prehashInsert(oid, headerObj);
832 /* This function creates objects in the transaction record */
833 objheader_t *transCreateObj(unsigned int size) {
834 objheader_t *tmp = (objheader_t *) objstrAlloc(&t_cache, (sizeof(objheader_t) + size));
835 OID(tmp) = getNewOID();
839 t_chashInsert(OID(tmp), tmp);
842 return &tmp[1]; //want space after object header
849 /* This function creates machine piles based on all machines involved in a
850 * transaction commit request */
851 plistnode_t *createPiles() {
853 plistnode_t *pile = NULL;
854 unsigned int machinenum;
855 objheader_t *headeraddr;
856 chashlistnode_t * ptr = c_table;
857 /* Represents number of bins in the chash table */
858 unsigned int size = c_size;
860 for(i = 0; i < size ; i++) {
861 chashlistnode_t * curr = &ptr[i];
862 /* Inner loop to traverse the linked list of the cache lookupTable */
863 while(curr != NULL) {
864 //if the first bin in hash table is empty
867 headeraddr=(objheader_t *) curr->val;
869 //Get machine location for object id (and whether local or not)
870 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
871 machinenum = myIpAddr;
872 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
873 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
877 //Make machine groups
878 pile = pInsert(pile, headeraddr, machinenum, c_numelements);
885 /* This function creates machine piles based on all machines involved in a
886 * transaction commit request */
887 plistnode_t *createPiles() {
889 plistnode_t *pile = NULL;
890 unsigned int machinenum;
891 objheader_t *headeraddr;
892 struct chashentry * ptr = c_table;
893 /* Represents number of bins in the chash table */
894 unsigned int size = c_size;
896 for(i = 0; i < size ; i++) {
897 struct chashentry * curr = & ptr[i];
898 /* Inner loop to traverse the linked list of the cache lookupTable */
899 //if the first bin in hash table is empty
902 headeraddr=(objheader_t *) curr->ptr;
904 //Get machine location for object id (and whether local or not)
905 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
906 machinenum = myIpAddr;
907 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
908 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
912 //Make machine groups
913 pile = pInsert(pile, headeraddr, machinenum, c_numelements);
919 /* This function initiates the transaction commit process
920 * Spawns threads for each of the new connections with Participants
921 * and creates new piles by calling the createPiles(),
922 * Sends a transrequest() to each remote machines for objects found remotely
923 * and calls handleLocalReq() to process objects found locally */
925 unsigned int tot_bytes_mod, *listmid;
926 plistnode_t *pile, *pile_ptr;
927 char treplyretry; /* keeps track of the common response that needs to be sent */
929 trans_commit_data_t transinfo; /* keeps track of objs locked during transaction */
937 for(iii=0;iii<bigindex;iii++) {
938 printf("%c", bigarray[iii]);
944 for(jjj=0; jjj<bigindex1; jjj++) {
945 printf("[%c %u %u %lld %d]\n", bigarray1[jjj], bigarray2[jjj], bigarray3[jjj], bigarray4[jjj], bigarray5[jjj]);
951 //abort this transaction
952 removetransactionhash();
953 objstrDelete(t_cache);
960 int treplyretryCount = 0;
961 exponential_backoff.tv_sec = 0;
962 exponential_backoff.tv_nsec = (long)(10000);//10 microsec
966 /* Look through all the objects in the transaction record and make piles
967 * for each machine involved in the transaction*/
969 pile_ptr = pile = createPiles();
970 pile_ptr = pile = sortPiles(pile);
975 /* Create the packet to be sent in TRANS_REQUEST */
977 /* Count the number of participants */
979 pilecount = pCount(pile);
981 /* Create a list of machine ids(Participants) involved in transaction */
982 listmid = calloc(pilecount, sizeof(unsigned int));
983 pListMid(pile, listmid);
985 /* Create a socket and getReplyCtrl array, initialize */
986 int socklist[pilecount];
987 char getReplyCtrl[pilecount];
989 for(loopcount = 0 ; loopcount < pilecount; loopcount++){
990 socklist[loopcount] = 0;
991 getReplyCtrl[loopcount] = 0;
994 /* Process each machine pile */
996 trans_req_data_t *tosend;
997 tosend = calloc(pilecount, sizeof(trans_req_data_t));
998 while(pile != NULL) {
999 tosend[sockindex].f.control = TRANS_REQUEST;
1000 tosend[sockindex].f.mcount = pilecount;
1001 tosend[sockindex].f.numread = pile->numread;
1002 tosend[sockindex].f.nummod = pile->nummod;
1003 tosend[sockindex].f.numcreated = pile->numcreated;
1004 tosend[sockindex].f.sum_bytes = pile->sum_bytes;
1005 tosend[sockindex].listmid = listmid;
1006 tosend[sockindex].objread = pile->objread;
1007 tosend[sockindex].oidmod = pile->oidmod;
1008 tosend[sockindex].oidcreated = pile->oidcreated;
1010 if(pile->mid != myIpAddr) {
1011 if((sd = getSock2WithLock(transRequestSockPool, pile->mid)) < 0) {
1012 printf("transRequest(): socket create error\n");
1017 socklist[sockindex] = sd;
1018 /* Send bytes of data with TRANS_REQUEST control message */
1019 send_data(sd, &(tosend[sockindex].f), sizeof(fixed_data_t));
1021 /* Send list of machines involved in the transaction */
1023 int size=sizeof(unsigned int)*(tosend[sockindex].f.mcount);
1024 send_data(sd, tosend[sockindex].listmid, size);
1027 /* Send oids and version number tuples for objects that are read */
1029 int size=(sizeof(unsigned int)+sizeof(unsigned short))*(tosend[sockindex].f.numread);
1030 send_data(sd, tosend[sockindex].objread, size);
1033 /* Send objects that are modified */
1035 if((modptr = calloc(1, tosend[sockindex].f.sum_bytes)) == NULL) {
1036 printf("Calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
1043 for(i = 0; i < tosend[sockindex].f.nummod ; i++) {
1045 objheader_t *headeraddr;
1046 if((headeraddr = t_chashSearch(tosend[sockindex].oidmod[i])) == NULL) {
1047 printf("%s() Error: No such oid %s, %d\n", __func__, __FILE__, __LINE__);
1053 GETSIZE(size,headeraddr);
1054 size+=sizeof(objheader_t);
1055 memcpy(modptr+offset, headeraddr, size);
1058 send_data(sd, modptr, tosend[sockindex].f.sum_bytes);
1060 } else { //handle request locally
1061 handleLocalReq(&tosend[sockindex], &transinfo, &getReplyCtrl[sockindex]);
1065 } //end of pile processing
1066 /* Recv Ctrl msgs from all machines */
1068 for(i = 0; i < pilecount; i++) {
1069 int sd = socklist[i];
1072 recv_data(sd, &control, sizeof(char));
1073 //Update common data structure with new ctrl msg
1074 getReplyCtrl[i] = control;
1075 /* Recv Objects if participant sends TRANS_DISAGREE */
1077 if(control == TRANS_DISAGREE) {
1079 recv_data(sd, &length, sizeof(int));
1081 pthread_mutex_lock(&prefetchcache_mutex);
1082 if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) {
1083 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1086 pthread_mutex_unlock(&prefetchcache_mutex);
1089 pthread_mutex_unlock(&prefetchcache_mutex);
1090 recv_data(sd, newAddr, length);
1092 while(length != 0) {
1093 unsigned int oidToPrefetch;
1094 objheader_t * header;
1095 header = (objheader_t *)(((char *)newAddr) + offset);
1096 oidToPrefetch = OID(header);
1099 GETSIZE(size, header);
1100 size += sizeof(objheader_t);
1101 //make an entry in prefetch hash table
1102 prehashInsert(oidToPrefetch, header);
1104 length = length - size;
1107 } //end of receiving objs
1111 /* Decide the final response */
1112 if((finalResponse = decideResponse(getReplyCtrl, &treplyretry, pilecount)) == 0) {
1113 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1119 /* Send responses to all machines */
1120 for(i = 0; i < pilecount; i++) {
1121 int sd = socklist[i];
1124 if(finalResponse == TRANS_COMMIT) {
1126 /* Update prefetch cache */
1127 if((retval = updatePrefetchCache(&(tosend[i]))) != 0) {
1128 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1135 /* Invalidate objects in other machine cache */
1136 if(tosend[i].f.nummod > 0) {
1137 if((retval = invalidateObj(&(tosend[i]))) != 0) {
1138 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
1145 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
1146 removethisreadtransaction(tosend[i].objread, tosend[i].f.numread);
1150 else if (!treplyretry) {
1151 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod);
1152 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1156 send_data(sd, &finalResponse, sizeof(char));
1158 /* Complete local processing */
1159 doLocalProcess(finalResponse, &(tosend[i]), &transinfo);
1161 if(finalResponse == TRANS_COMMIT) {
1162 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
1163 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1164 } else if (!treplyretry) {
1165 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod);
1166 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread);
1172 /* Free resources */
1177 /* wait a random amount of time before retrying to commit transaction*/
1180 if(treplyretryCount >= NUM_TRY_TO_COMMIT)
1188 /* Retry trans commit procedure during soft_abort case */
1189 } while (treplyretry);
1191 /* Reset to initial timeout for exponential delay */
1192 exponential_backoff.tv_sec = 0;
1193 exponential_backoff.tv_nsec = (long)(10000);//10 microsec_
1194 count_exponential_backoff = 0;
1196 if(finalResponse == TRANS_ABORT) {
1201 /* Free Resources */
1202 objstrDelete(t_cache);
1208 } else if(finalResponse == TRANS_COMMIT) {
1213 /* Free Resources */
1214 objstrDelete(t_cache);
1218 //TODO Add other cases
1219 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1225 /* This function handles the local objects involved in a transaction
1226 * commiting process. It also makes a decision if this local machine
1227 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator */
1228 void handleLocalReq(trans_req_data_t *tdata, trans_commit_data_t *transinfo, char *getReplyCtrl) {
1229 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
1230 int numoidnotfound = 0, numoidlocked = 0;
1231 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
1234 unsigned short version;
1236 /* Counters and arrays to formulate decision on control message to be sent */
1237 oidnotfound = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod), sizeof(unsigned int));
1238 oidlocked = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod +1), sizeof(unsigned int)); // calloc additional 1 byte for
1239 //setting a divider between read and write locks
1240 numread = tdata->f.numread;
1241 /* Process each oid in the machine pile/ group per thread */
1242 for (i = 0; i < tdata->f.numread + tdata->f.nummod; i++) {
1243 if (i < tdata->f.numread) {
1244 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
1246 oid = *((unsigned int *)(((char *)tdata->objread) + incr));
1247 version = *((unsigned short *)(((char *)tdata->objread) + incr + sizeof(unsigned int)));
1248 commitCountForObjRead(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
1249 } else { // Objects Modified
1250 if(i == tdata->f.numread) {
1251 oidlocked[numoidlocked++] = -1;
1254 objheader_t *headptr;
1255 headptr = (objheader_t *) t_chashSearch(tdata->oidmod[i-numread]);
1256 if (headptr == NULL) {
1257 printf("Error: handleLocalReq() returning NULL, no such oid %s, %d\n", __FILE__, __LINE__);
1261 version = headptr->version;
1262 commitCountForObjMod(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
1266 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
1267 * if Participant receives a TRANS_COMMIT */
1268 transinfo->objlocked = oidlocked;
1269 transinfo->objnotfound = oidnotfound;
1270 transinfo->modptr = NULL;
1271 transinfo->numlocked = numoidlocked;
1272 transinfo->numnotfound = numoidnotfound;
1274 /* Condition to send TRANS_AGREE */
1275 if(v_matchnolock == tdata->f.numread + tdata->f.nummod) {
1276 *getReplyCtrl = TRANS_AGREE;
1278 /* Condition to send TRANS_SOFT_ABORT */
1279 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
1280 *getReplyCtrl = TRANS_SOFT_ABORT;
1284 void doLocalProcess(char finalResponse, trans_req_data_t *tdata, trans_commit_data_t *transinfo) {
1285 if(finalResponse == TRANS_ABORT) {
1286 if(transAbortProcess(transinfo) != 0) {
1287 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
1291 } else if(finalResponse == TRANS_COMMIT) {
1293 /* Invalidate objects in other machine cache */
1294 if(tdata->f.nummod > 0) {
1296 if((retval = invalidateObj(tdata)) != 0) {
1297 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
1302 if(transComProcess(tdata, transinfo) != 0) {
1303 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1308 printf("ERROR...No Decision\n");
1312 if (transinfo->objlocked != NULL) {
1313 free(transinfo->objlocked);
1315 if (transinfo->objnotfound != NULL) {
1316 free(transinfo->objnotfound);
1320 /* This function decides the reponse that needs to be sent to
1321 * all Participant machines after the TRANS_REQUEST protocol */
1322 char decideResponse(char *getReplyCtrl, char *treplyretry, int pilecount) {
1323 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
1325 for (i = 0 ; i < pilecount; i++) {
1327 control = getReplyCtrl[i];
1330 printf("Participant sent unknown message %d in %s, %d\n", control, __FILE__, __LINE__);
1332 /* treat as disagree, pass thru */
1333 case TRANS_DISAGREE:
1341 case TRANS_SOFT_ABORT:
1347 if(transdisagree > 0) {
1352 /* clear objects from prefetch cache */
1355 } else if(transagree == pilecount) {
1358 return TRANS_COMMIT;
1360 /* Send Abort in soft abort case followed by retry commiting transaction again*/
1367 /* This function opens a connection, places an object read request to
1368 * the remote machine, reads the control message and object if
1369 * available and copies the object and its header to the local
1372 void *getRemoteObj(unsigned int mnum, unsigned int oid) {
1374 struct sockaddr_in serv_addr;
1378 void *objcopy = NULL;
1380 int sd = getSock2(transReadSockPool, mnum);
1381 char readrequest[sizeof(char)+sizeof(unsigned int)];
1382 readrequest[0] = READ_REQUEST;
1383 *((unsigned int *)(&readrequest[1])) = oid;
1384 send_data(sd, readrequest, sizeof(readrequest));
1386 /* Read response from the Participant */
1387 recv_data(sd, &control, sizeof(char));
1389 if (control==OBJECT_NOT_FOUND) {
1392 /* Read object if found into local cache */
1393 recv_data(sd, &size, sizeof(int));
1394 objcopy = objstrAlloc(&t_cache, size);
1395 recv_data(sd, objcopy, size);
1397 /* Insert into cache's lookup table */
1398 t_chashInsert(oid, objcopy);
1400 totalObjSize += size;
1407 /* Commit info for objects modified */
1408 void commitCountForObjMod(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1409 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1411 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1412 /* Save the oids not found and number of oids not found for later use */
1413 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1414 /* Save the oids not found and number of oids not found for later use */
1415 oidnotfound[*numoidnotfound] = oid;
1416 (*numoidnotfound)++;
1417 } else { /* If Obj found in machine (i.e. has not moved) */
1418 /* Check if Obj is locked by any previous transaction */
1419 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
1420 if (version == ((objheader_t *)mobj)->version) { /* match versions */
1422 //Keep track of what is locked
1423 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1424 } else { /* If versions don't match ...HARD ABORT */
1426 /* Send TRANS_DISAGREE to Coordinator */
1427 *getReplyCtrl = TRANS_DISAGREE;
1429 //Keep track of what is locked
1430 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1433 } else { //A lock is acquired some place else
1434 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1436 } else { /* If versions don't match ...HARD ABORT */
1438 /* Send TRANS_DISAGREE to Coordinator */
1439 *getReplyCtrl = TRANS_DISAGREE;
1446 /* Commit info for objects modified */
1447 void commitCountForObjRead(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1448 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1450 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1451 /* Save the oids not found and number of oids not found for later use */
1452 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1453 /* Save the oids not found and number of oids not found for later use */
1454 oidnotfound[*numoidnotfound] = oid;
1455 (*numoidnotfound)++;
1456 } else { /* If Obj found in machine (i.e. has not moved) */
1457 /* Check if Obj is locked by any previous transaction */
1458 if (read_trylock(STATUSPTR(mobj))) { // Can further acquire read locks
1459 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
1461 //Keep track of what is locked
1462 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1463 } else { /* If versions don't match ...HARD ABORT */
1465 /* Send TRANS_DISAGREE to Coordinator */
1466 *getReplyCtrl = TRANS_DISAGREE;
1467 //Keep track of what is locked
1468 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1471 } else { //Has reached max number of readers or some other transaction
1472 //has acquired a lock on this object
1473 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1475 } else { /* If versions don't match ...HARD ABORT */
1477 /* Send TRANS_DISAGREE to Coordinator */
1478 *getReplyCtrl = TRANS_DISAGREE;
1485 /* This function completes the ABORT process if the transaction is aborting */
1486 int transAbortProcess(trans_commit_data_t *transinfo) {
1488 unsigned int *objlocked;
1491 numlocked = transinfo->numlocked;
1492 objlocked = transinfo->objlocked;
1494 int useWriteUnlock = 0;
1495 for (i = 0; i < numlocked; i++) {
1496 if(objlocked[i] == -1) {
1500 if((header = mhashSearch(objlocked[i])) == NULL) {
1501 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1504 if(!useWriteUnlock) {
1505 read_unlock(STATUSPTR(header));
1507 write_unlock(STATUSPTR(header));
1514 /*This function completes the COMMIT process if the transaction is commiting*/
1515 int transComProcess(trans_req_data_t *tdata, trans_commit_data_t *transinfo) {
1516 objheader_t *header, *tcptr;
1517 int i, nummod, tmpsize, numcreated, numlocked;
1518 unsigned int *oidmod, *oidcreated, *oidlocked;
1521 nummod = tdata->f.nummod;
1522 oidmod = tdata->oidmod;
1523 numcreated = tdata->f.numcreated;
1524 oidcreated = tdata->oidcreated;
1525 numlocked = transinfo->numlocked;
1526 oidlocked = transinfo->objlocked;
1528 for (i = 0; i < nummod; i++) {
1529 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1530 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1533 /* Copy from transaction cache -> main object store */
1534 if ((tcptr = ((objheader_t *) t_chashSearch(oidmod[i]))) == NULL) {
1535 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1538 GETSIZE(tmpsize, header);
1539 char *tmptcptr = (char *) tcptr;
1541 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
1542 struct ___Object___ *src=(struct ___Object___*)((char*)tmptcptr+sizeof(objheader_t));
1543 dst->___cachedCode___=src->___cachedCode___;
1544 dst->___cachedHash___=src->___cachedHash___;
1546 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
1549 header->version += 1;
1550 if(header->notifylist != NULL) {
1551 notifyAll(&header->notifylist, OID(header), header->version);
1554 /* If object is newly created inside transaction then commit it */
1555 for (i = 0; i < numcreated; i++) {
1556 if ((header = ((objheader_t *) t_chashSearch(oidcreated[i]))) == NULL) {
1557 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1560 GETSIZE(tmpsize, header);
1561 tmpsize += sizeof(objheader_t);
1562 pthread_mutex_lock(&mainobjstore_mutex);
1563 if ((ptrcreate = objstrAlloc(&mainobjstore, tmpsize)) == NULL) {
1564 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1565 pthread_mutex_unlock(&mainobjstore_mutex);
1568 pthread_mutex_unlock(&mainobjstore_mutex);
1569 /* Initialize read and write locks */
1570 initdsmlocks(STATUSPTR(header));
1571 memcpy(ptrcreate, header, tmpsize);
1572 mhashInsert(oidcreated[i], ptrcreate);
1573 lhashInsert(oidcreated[i], myIpAddr);
1575 /* Unlock locked objects */
1576 int useWriteUnlock = 0;
1577 for(i = 0; i < numlocked; i++) {
1578 if(oidlocked[i] == -1) {
1582 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1583 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1586 if(!useWriteUnlock) {
1587 read_unlock(STATUSPTR(header));
1589 write_unlock(STATUSPTR(header));
1595 prefetchpile_t *foundLocal(char *ptr, int numprefetches, int mysiteid) {
1598 prefetchpile_t * head=NULL;
1600 for(j=0;j<numprefetches;j++) {
1601 int siteid = *(GET_SITEID(ptr));
1602 int ntuples = *(GET_NTUPLES(ptr));
1603 unsigned int * oidarray = GET_PTR_OID(ptr);
1604 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1605 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1608 for(i=0; i<ntuples; i++) {
1609 unsigned short baseindex=(i==0) ? 0 : endoffsets[i-1];
1610 unsigned short endindex=endoffsets[i];
1611 unsigned int oid=oidarray[i];
1614 int countInvalidObj=0;
1620 //Look up fields locally
1624 for(newbase=baseindex; newbase<endindex; newbase++) {
1625 if(newbase==(endindex-1))
1627 if (!lookupObject(&oid,arryfields[newbase],&countInvalidObj)) {
1630 //Ended in a null pointer...
1637 //Entire prefetch is local
1638 if (newbase==endindex&&checkoid(oid,isLastOffset)) {
1643 //Add to remote requests
1644 machinenum=lhashSearch(oid);
1645 insertPile(machinenum, oid, siteid,endindex-newbase, &arryfields[newbase], &head);
1650 /* handle dynamic prefetching */
1651 handleDynPrefetching(numLocal, ntuples, siteid);
1652 ptr=((char *)&arryfields[endoffsets[ntuples-1]])+sizeof(int);
1658 int checkoid(unsigned int oid, int isLastOffset) {
1659 objheader_t *header;
1660 if ((header=mhashSearch(oid))!=NULL) {
1663 } else if ((header=prehashSearch(oid))!=NULL) {
1664 //if the last offset then prefetch object
1665 if((STATUS(header) & DIRTY) && isLastOffset) {
1675 int lookupObject(unsigned int * oid, short offset, int *countInvalidObj) {
1676 objheader_t *header;
1677 if ((header=mhashSearch(*oid))!=NULL) {
1680 } else if ((header=prehashSearch(*oid))!=NULL) {
1682 if(STATUS(header) & DIRTY) {//Read an oid that is an old entry in the cache;
1683 //only once because later old entries may still cause unnecessary roundtrips during prefetching
1684 (*countInvalidObj)+=1;
1685 if(*countInvalidObj > 1) {
1693 if(TYPE(header) >= NUMCLASSES) {
1694 int elementsize = classsize[TYPE(header)];
1695 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1696 int length = ao->___length___;
1697 /* Check if array out of bounds */
1698 if(offset < 0 || offset >= length) {
1699 //if yes treat the object as found
1703 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1706 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1712 /* This function is called by the thread calling transPrefetch */
1713 void *transPrefetch(void *t) {
1715 /* read from prefetch queue */
1716 void *node=gettail();
1717 /* Check if the tuples are found locally, if yes then reduce them further*/
1718 /* and group requests by remote machine ids by calling the makePreGroups() */
1719 int count=numavailable();
1720 prefetchpile_t *pilehead = foundLocal(node, count, 0);
1722 if (pilehead!=NULL) {
1723 // Get sock from shared pool
1725 /* Send Prefetch Request */
1726 prefetchpile_t *ptr = pilehead;
1727 while(ptr != NULL) {
1729 int sd = getSock2(transPrefetchSockPool, ptr->mid);
1730 sendPrefetchReq(ptr, sd,globalid);
1734 /* Release socket */
1735 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1737 /* Deallocated pilehead */
1738 mcdealloc(pilehead);
1740 // Deallocate the prefetch queue pile node
1745 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1748 int size=sizeof(char)+sizeof(int);
1749 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1750 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1755 *buf=TRANS_PREFETCH;
1758 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1759 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1762 *((unsigned int *)buf)=tmp->oid;
1763 buf+=sizeof(unsigned int);
1764 *((unsigned int *)(buf)) = myIpAddr;
1765 buf+=sizeof(unsigned int);
1766 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1767 buf+=tmp->numoffset*sizeof(short);
1770 send_data(sd, buft, size);
1775 * parameters: mcpilenode -> pile node to traverse to assemble pref requests
1777 * gid -> global identifier for each prefetch request sent, starts with 0
1779 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd, int gid) {
1783 struct writestruct writebuffer;
1784 writebuffer.offset=0;
1787 /* Send TRANS_PREFETCH control message */
1790 /* Send Oids and offsets in pairs */
1791 tmp = mcpilenode->objpiles;
1792 while(tmp != NULL) {
1793 len = sizeof(int)+sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1794 char oidnoffset[len+5];
1795 char *buf=oidnoffset;
1797 *buf=TRANS_PREFETCH;
1801 *((int*)buf) = tmp->numoffset;
1803 *((unsigned int *)buf) = tmp->oid;
1804 LOGOIDTYPE("S",tmp->oid,tmp->numoffset,myrdtsc());
1808 buf+=sizeof(unsigned int);
1809 *((unsigned int *)buf) = myIpAddr;
1810 buf+= sizeof(unsigned int);
1813 memcpy(buf, tmp->offset, (tmp->numoffset)*sizeof(short));
1816 *((int *)(&oidnoffset[len]))=-1;
1820 send_buf(sd, &writebuffer, oidnoffset, len);
1822 forcesend_buf(sd, &writebuffer, oidnoffset, len);
1824 LOGOIDTYPE("SREQ",0,0,myrdtsc());
1826 LOGTIME('S',0,0,myrdtsc(),gid); //after sending
1830 int getPrefetchResponse(int sd, struct readstruct *readbuffer) {
1831 int gid,length = 0, size = 0;
1834 void *modptr, *oldptr;
1836 recv_data_buf(sd, readbuffer, &length, sizeof(int));
1837 size = length - sizeof(int);
1838 char recvbuffer[size];
1842 LOGTIME('K',0,0, myrdtsc(),0); //log time after first recv
1844 recv_data_buf(sd, readbuffer, recvbuffer, size);
1845 control = *((char *) recvbuffer);
1846 if(control == OBJECT_FOUND) {
1847 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1848 gid = *((int *) (recvbuffer+sizeof(char)+sizeof(unsigned int)));
1849 LOGTIME('G',oid,0, myrdtsc(),gid); //log time after first recv
1850 size = size - (sizeof(char) + sizeof(unsigned int) + sizeof(int));
1851 pthread_mutex_lock(&prefetchcache_mutex);
1852 if ((modptr = prefetchobjstrAlloc(size)) == NULL) {
1853 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1854 pthread_mutex_unlock(&prefetchcache_mutex);
1857 pthread_mutex_unlock(&prefetchcache_mutex);
1858 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int)+sizeof(int), size);
1862 /* Insert the oid and its address into the prefetch hash lookup table */
1863 /* Do a version comparison if the oid exists */
1864 if((oldptr = prehashSearch(oid)) != NULL) {
1865 /* If older version then update with new object ptr */
1866 if(((objheader_t *)oldptr)->version < ((objheader_t *)modptr)->version) {
1867 prehashInsert(oid, modptr);
1869 } else { /* Else add the object ptr to hash table*/
1870 prehashInsert(oid, modptr);
1872 LOGOIDTYPE("GR",oid, TYPE(modptr),myrdtsc());
1873 LOGTIME('Z',oid, TYPE(modptr), myrdtsc(),gid); //log time after copying it into the prefetch cache
1874 } else if(control == OBJECT_NOT_FOUND) {
1875 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1876 gid = *((int *) (recvbuffer+sizeof(char)+sizeof(unsigned int)));
1877 LOGOIDTYPE("NF",oid,0,myrdtsc());
1878 LOGTIME('F',oid, 0, myrdtsc(),gid); //log time after copying it into the prefetch cache
1879 /* TODO: For each object not found query DHT for new location and retrieve the object */
1880 /* Throw an error */
1881 //printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1884 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1890 unsigned short getObjType(unsigned int oid) {
1891 objheader_t *objheader;
1892 unsigned short numoffset[] ={0};
1893 short fieldoffset[] ={};
1895 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1897 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1899 unsigned int mid = lhashSearch(oid);
1900 int sd = getSock2(transReadSockPool, mid);
1901 char remotereadrequest[sizeof(char)+sizeof(unsigned int)];
1902 remotereadrequest[0] = READ_REQUEST;
1903 *((unsigned int *)(&remotereadrequest[1])) = oid;
1904 send_data(sd, remotereadrequest, sizeof(remotereadrequest));
1906 /* Read response from the Participant */
1908 recv_data(sd, &control, sizeof(char));
1910 if (control==OBJECT_NOT_FOUND) {
1911 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1915 /* Read object if found into local cache */
1917 recv_data(sd, &size, sizeof(int));
1919 pthread_mutex_lock(&prefetchcache_mutex);
1920 if ((objheader = prefetchobjstrAlloc(size)) == NULL) {
1921 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1924 pthread_mutex_unlock(&prefetchcache_mutex);
1925 recv_data(sd, objheader, size);
1926 prehashInsert(oid, objheader);
1927 return TYPE(objheader);
1930 if((buffer = calloc(1, size)) == NULL) {
1931 printf("%s() Calloc Error %s at line %d\n", __func__, __FILE__, __LINE__);
1935 recv_data(sd, buffer, size);
1936 objheader = (objheader_t *)buffer;
1937 unsigned short type = TYPE(objheader);
1946 return TYPE(objheader);
1949 int startRemoteThread(unsigned int oid, unsigned int mid) {
1951 struct sockaddr_in remoteAddr;
1952 char msg[1 + sizeof(unsigned int)];
1956 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1957 perror("startRemoteThread():socket()");
1961 bzero(&remoteAddr, sizeof(remoteAddr));
1962 remoteAddr.sin_family = AF_INET;
1963 remoteAddr.sin_port = htons(LISTEN_PORT);
1964 remoteAddr.sin_addr.s_addr = htonl(mid);
1966 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1967 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1968 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1972 msg[0] = START_REMOTE_THREAD;
1973 *((unsigned int *) &msg[1]) = oid;
1974 send_data(sock, msg, 1 + sizeof(unsigned int));
1981 //TODO: when reusing oids, make sure they are not already in use!
1982 static unsigned int id = 0xFFFFFFFF;
1983 unsigned int getNewOID(void) {
1985 if (id > oidMax || id < oidMin) {
1991 int processConfigFile() {
1993 const int maxLineLength = 200;
1994 char lineBuffer[maxLineLength];
1996 const char *delimiters = " \t\n";
2000 configFile = fopen(CONFIG_FILENAME, "r");
2001 if (configFile == NULL) {
2002 printf("error opening %s:\n", CONFIG_FILENAME);
2007 numHostsInSystem = 0;
2008 sizeOfHostArray = 8;
2009 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
2011 while(fgets(lineBuffer, maxLineLength, configFile) != NULL) {
2012 commentBegin = strchr(lineBuffer, '#');
2013 if (commentBegin != NULL)
2014 *commentBegin = '\0';
2015 token = strtok(lineBuffer, delimiters);
2016 while (token != NULL) {
2017 tmpAddr = inet_addr(token);
2018 if ((int)tmpAddr == -1) {
2019 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
2023 addHost(htonl(tmpAddr));
2024 token = strtok(NULL, delimiters);
2030 if (numHostsInSystem < 1) {
2031 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
2035 myIpAddr = getMyIpAddr("en1");
2037 myIpAddr = getMyIpAddr("eth0");
2039 myIndexInHostArray = findHost(myIpAddr);
2040 if (myIndexInHostArray == -1) {
2041 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
2044 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
2045 oidMin = oidsPerBlock * myIndexInHostArray;
2046 if (myIndexInHostArray == numHostsInSystem - 1)
2047 oidMax = 0xFFFFFFFF;
2049 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
2054 void addHost(unsigned int hostIp) {
2055 unsigned int *tmpArray;
2057 if (findHost(hostIp) != -1)
2060 if (numHostsInSystem == sizeOfHostArray) {
2061 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
2062 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
2064 hostIpAddrs = tmpArray;
2067 hostIpAddrs[numHostsInSystem++] = hostIp;
2072 int findHost(unsigned int hostIp) {
2074 for (i = 0; i < numHostsInSystem; i++)
2075 if (hostIpAddrs[i] == hostIp)
2082 /* This function sends notification request per thread waiting on object(s) whose version
2084 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
2086 objheader_t *objheader;
2087 struct sockaddr_in remoteAddr;
2088 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
2092 unsigned short version;
2093 unsigned int oid,mid;
2094 static unsigned int threadid = 0;
2095 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
2096 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
2097 notifydata_t *ndata;
2100 if((mid = lhashSearch(oid)) == 0) {
2101 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
2105 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2106 perror("reqNotify():socket()");
2110 bzero(&remoteAddr, sizeof(remoteAddr));
2111 remoteAddr.sin_family = AF_INET;
2112 remoteAddr.sin_port = htons(LISTEN_PORT);
2113 remoteAddr.sin_addr.s_addr = htonl(mid);
2115 /* Generate unique threadid */
2118 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
2119 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
2120 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
2123 ndata->numoid = numoid;
2124 ndata->threadid = threadid;
2125 ndata->oidarry = oidarry;
2126 ndata->versionarry = versionarry;
2127 ndata->threadcond = threadcond;
2128 ndata->threadnotify = threadnotify;
2129 if((status = notifyhashInsert(threadid, ndata)) != 0) {
2130 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
2135 /* Send number of oids, oidarry, version array, machine id and threadid */
2136 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
2137 printf("reqNotify():error %d connecting to %s:%d\n", errno,
2138 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
2142 msg[0] = THREAD_NOTIFY_REQUEST;
2143 *((unsigned int *)(&msg[1])) = numoid;
2144 /* Send array of oids */
2145 size = sizeof(unsigned int);
2147 for(i = 0;i < numoid; i++) {
2149 *((unsigned int *)(&msg[1] + size)) = oid;
2150 size += sizeof(unsigned int);
2153 /* Send array of version */
2154 for(i = 0;i < numoid; i++) {
2155 version = versionarry[i];
2156 *((unsigned short *)(&msg[1] + size)) = version;
2157 size += sizeof(unsigned short);
2160 *((unsigned int *)(&msg[1] + size)) = myIpAddr; size += sizeof(unsigned int);
2161 *((unsigned int *)(&msg[1] + size)) = threadid;
2162 pthread_mutex_lock(&(ndata->threadnotify));
2163 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
2164 send_data(sock, msg, size);
2165 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
2166 pthread_mutex_unlock(&(ndata->threadnotify));
2169 pthread_cond_destroy(&threadcond);
2170 pthread_mutex_destroy(&threadnotify);
2176 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
2177 notifydata_t *ndata;
2178 int i, objIsFound = 0, index;
2181 //Look up the tid and call the corresponding pthread_cond_signal
2182 if((ndata = notifyhashSearch(tid)) == NULL) {
2183 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
2186 for(i = 0; i < ndata->numoid; i++) {
2187 if(ndata->oidarry[i] == oid) {
2192 if(objIsFound == 0) {
2193 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
2196 if(version <= ndata->versionarry[index]) {
2197 printf("threadNotify(): New version %d has not changed since last version for oid = %d, %s, %d\n", version, oid, __FILE__, __LINE__);
2201 /* Clear from prefetch cache and free thread related data structure */
2202 if((ptr = prehashSearch(oid)) != NULL) {
2206 pthread_mutex_lock(&(ndata->threadnotify));
2207 pthread_cond_signal(&(ndata->threadcond));
2208 pthread_mutex_unlock(&(ndata->threadnotify));
2215 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
2218 struct sockaddr_in remoteAddr;
2219 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
2220 int sock, status, size, bytesSent;
2222 while(*head != NULL) {
2225 //create a socket connection to that machine
2226 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2227 perror("notifyAll():socket()");
2231 bzero(&remoteAddr, sizeof(remoteAddr));
2232 remoteAddr.sin_family = AF_INET;
2233 remoteAddr.sin_port = htons(LISTEN_PORT);
2234 remoteAddr.sin_addr.s_addr = htonl(mid);
2235 //send Thread Notify response and threadid to that machine
2236 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
2237 printf("notifyAll():error %d connecting to %s:%d\n", errno,
2238 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
2242 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
2243 msg[0] = THREAD_NOTIFY_RESPONSE;
2244 *((unsigned int *)&msg[1]) = oid;
2245 size = sizeof(unsigned int);
2246 *((unsigned short *)(&msg[1]+ size)) = version;
2247 size+= sizeof(unsigned short);
2248 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
2250 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
2251 send_data(sock, msg, size);
2264 removetransactionhash();
2266 objstrDelete(t_cache);
2270 /* This function inserts necessary information into
2271 * a machine pile data structure */
2272 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
2273 plistnode_t *ptr, *tmp;
2274 int found = 0, offset = 0;
2277 //Add oid into a machine that is already present in the pile linked list structure
2278 while(tmp != NULL) {
2279 if (tmp->mid == mid) {
2282 if (STATUS(headeraddr) & NEW) {
2283 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
2285 GETSIZE(tmpsize, headeraddr);
2286 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
2287 } else if (STATUS(headeraddr) & DIRTY) {
2288 tmp->oidmod[tmp->nummod] = OID(headeraddr);
2290 GETSIZE(tmpsize, headeraddr);
2291 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
2293 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
2294 *((unsigned int *)(((char *)tmp->objread) + offset))=OID(headeraddr);
2295 offset += sizeof(unsigned int);
2296 *((short *)(((char *)tmp->objread) + offset)) = headeraddr->version;
2304 //Add oid for any new machine
2307 if((ptr = pCreate(num_objs)) == NULL) {
2311 if (STATUS(headeraddr) & NEW) {
2312 ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
2314 GETSIZE(tmpsize, headeraddr);
2315 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
2316 } else if (STATUS(headeraddr) & DIRTY) {
2317 ptr->oidmod[ptr->nummod] = OID(headeraddr);
2319 GETSIZE(tmpsize, headeraddr);
2320 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
2322 *((unsigned int *)ptr->objread)=OID(headeraddr);
2323 offset = sizeof(unsigned int);
2324 *((short *)(((char *)ptr->objread) + offset)) = headeraddr->version;
2332 STATUS(headeraddr) =0;
2338 plistnode_t *sortPiles(plistnode_t *pileptr) {
2339 plistnode_t *head, *ptr, *tail;
2342 /* Get tail pointer */
2348 plistnode_t *prev = pileptr;
2349 /* Arrange local machine processing at the end of the pile list */
2350 while(ptr != NULL) {
2352 if(ptr->mid == myIpAddr && (prev != pileptr)) {
2353 prev->next = ptr->next;
2358 if((ptr->mid == myIpAddr) && (prev == pileptr)) {