3 #include "machinepile.h"
8 #include "threadnotify.h"
10 #include "addUdpEnhance.h"
11 #include "addPrefetchEnhance.h"
19 #include "abortreaders.h"
23 #define CONFIG_FILENAME "dstm.conf"
26 /* Global Variables */
27 extern int classsize[];
28 pfcstats_t *evalPrefetch;
29 extern int numprefetchsites; //Global variable containing number of prefetch sites
30 extern pthread_mutex_t mainobjstore_mutex; // Mutex to lock main Object store
31 pthread_mutex_t prefetchcache_mutex; // Mutex to lock Prefetch Cache
32 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
33 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
34 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
35 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
36 extern objstr_t *mainobjstore;
37 unsigned int myIpAddr;
38 unsigned int *hostIpAddrs;
41 int myIndexInHostArray;
42 unsigned int oidsPerBlock;
46 sockPoolHashTable_t *transReadSockPool;
47 sockPoolHashTable_t *transPrefetchSockPool;
48 sockPoolHashTable_t *transRequestSockPool;
49 pthread_mutex_t notifymutex;
50 pthread_mutex_t atomicObjLock;
52 /***********************************
53 * Global Variables for statistics
54 **********************************/
55 int numTransCommit = 0;
56 int numTransAbort = 0;
59 int nprehashSearch = 0;
65 void printhex(unsigned char *, int);
66 plistnode_t *createPiles(transrecord_t *);
67 plistnode_t *sortPiles(plistnode_t *pileptr);
69 /*******************************
70 * Send and Recv function calls
71 *******************************/
72 void send_data(int fd, void *buf, int buflen) {
73 char *buffer = (char *)(buf);
77 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
78 bytesSent = bytesSent + numbytes;
88 void recv_data(int fd, void *buf, int buflen) {
89 char *buffer = (char *)(buf);
93 numbytes = recv(fd, buffer, size, 0);
94 bytesRecv = bytesRecv + numbytes;
104 int recv_data_errorcode(int fd, void *buf, int buflen) {
105 char *buffer = (char *)(buf);
109 numbytes = recv(fd, buffer, size, 0);
112 if (numbytes == -1) {
122 void printhex(unsigned char *ptr, int numBytes) {
124 for (i = 0; i < numBytes; i++) {
126 printf("0%x ", ptr[i]);
128 printf("%x ", ptr[i]);
134 inline int arrayLength(int *array) {
136 for(i=0 ; array[i] != -1; i++)
141 inline int findmax(int *array, int arraylength) {
144 for(i = 0; i < arraylength; i++) {
152 /* This function is a prefetch call generated by the compiler that
153 * populates the shared primary prefetch queue*/
154 void prefetch(int siteid, int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
155 /* Allocate for the queue node*/
156 int qnodesize = 2*sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
158 char * node= getmemory(qnodesize);
159 int top=endoffsets[ntuples-1];
163 /* Set queue node values */
165 /* TODO: Remove this after testing */
166 evalPrefetch[siteid].callcount++;
168 *((int *)(node))=siteid;
169 *((int *)(node + sizeof(int))) = ntuples;
171 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
172 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
173 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
175 /* Lock and insert into primary prefetch queue */
179 /* This function starts up the transaction runtime. */
180 int dstmStartup(const char * option) {
181 pthread_t thread_Listen, udp_thread_Listen;
183 int master=option!=NULL && strcmp(option, "master")==0;
187 if (processConfigFile() != 0)
188 return 0; //TODO: return error value, cause main program to exit
195 printf("Trans stats is on\n");
202 //Initialize socket pool
203 transReadSockPool = createSockPool(transReadSockPool, DEFAULTSOCKPOOLSIZE);
204 transPrefetchSockPool = createSockPool(transPrefetchSockPool, DEFAULTSOCKPOOLSIZE);
205 transRequestSockPool = createSockPool(transRequestSockPool, DEFAULTSOCKPOOLSIZE);
211 pthread_attr_init(&attr);
212 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
215 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
218 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
221 dstmListen((void *)fd);
226 //TODO Use this later
227 void *pCacheAlloc(objstr_t *store, unsigned int size) {
233 while(ptr->next != NULL) {
234 /* check if store is empty */
235 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
250 /* This function initiates the prefetch thread A queue is shared
251 * between the main thread of execution and the prefetch thread to
252 * process the prefetch call Call from compiler populates the shared
253 * queue with prefetch requests while prefetch thread processes the
254 * prefetch requests */
257 //Create and initialize prefetch cache structure
260 if((evalPrefetch = initPrefetchStats()) == NULL) {
261 printf("%s() Error allocating memory at %s, %d\n", __func__, __FILE__, __LINE__);
266 /* Initialize attributes for mutex */
267 pthread_mutexattr_init(&prefetchcache_mutex_attr);
268 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
270 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
271 pthread_mutex_init(¬ifymutex, NULL);
272 pthread_mutex_init(&atomicObjLock, NULL);
274 //Create prefetch cache lookup table
275 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
280 //Initialize primary shared queue
282 //Initialize machine pile w/prefetch oids and offsets shared queue
285 //Create the primary prefetch thread
289 retval=pthread_create(&tPrefetch, NULL, transPrefetchNew, NULL);
293 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
296 pthread_detach(tPrefetch);
300 /* This function stops the threads spawned */
304 pthread_cancel(tPrefetch);
305 for(t = 0; t < NUM_THREADS; t++)
306 pthread_cancel(wthreads[t]);
312 /* This functions inserts randowm wait delays in the order of msec
313 * Mostly used when transaction commits retry*/
320 req.tv_nsec = (long)(1000 + (t%10000)); //1-11 microsec
321 nanosleep(&req, NULL);
325 /* This function initializes things required in the transaction start*/
326 __attribute__((malloc)) transrecord_t *transStart() {
328 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL) {
329 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
332 tmp->cache = objstrCreate(1048576);
333 tmp->lookupTable = chashCreate(CHASH_SIZE, CLOADFACTOR);
335 // tmp->revertlist=NULL; //Not necessary...already null
340 // Search for an address for a given oid
341 /*#define INLINE inline __attribute__((always_inline))
343 INLINE void * chashSearchI(chashtable_t *table, unsigned int key) {
344 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
345 chashlistnode_t *node = &table->table[(key & table->mask)>>1];
348 if(node->key == key) {
352 } while(node != NULL);
358 /* This function finds the location of the objects involved in a transaction
359 * and returns the pointer to the object if found in a remote location */
360 __attribute__((pure)) objheader_t *transRead(transrecord_t *record, unsigned int oid) {
361 unsigned int machinenumber;
362 objheader_t *tmp, *objheader;
363 objheader_t *objcopy;
366 chashlistnode_t *node;
367 chashtable_t *table=record->lookupTable;
373 node= &table->table[(oid & table->mask)>>1];
375 if(node->key == oid) {
380 return &((objheader_t*)node->val)[1];
386 } while(node != NULL);
390 if((objheader = chashSearchI(record->lookupTable, oid)) != NULL) {
395 return &objheader[1];
404 //abort this transaction
405 printf("ABORTING\n");
406 objstrDelete(record->cache);
407 chashDelete(record->lookupTable);
408 _longjmp(record->aborttrans,1);
410 addtransaction(oid,record);
413 if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
417 /* Look up in machine lookup table and copy into cache*/
418 GETSIZE(size, objheader);
419 size += sizeof(objheader_t);
420 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
421 memcpy(objcopy, objheader, size);
422 /* Insert into cache's lookup table */
424 chashInsert(record->lookupTable, OID(objheader), objcopy);
432 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
436 /* Look up in prefetch cache */
438 size+=sizeof(objheader_t);
439 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
440 memcpy(objcopy, tmp, size);
441 /* Insert into cache's lookup table */
442 chashInsert(record->lookupTable, OID(tmp), objcopy);
450 /* Get the object from the remote location */
451 if((machinenumber = lhashSearch(oid)) == 0) {
452 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
455 objcopy = getRemoteObj(record, machinenumber, oid);
457 if(objcopy == NULL) {
458 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
473 /* This function creates objects in the transaction record */
474 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
475 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
476 OID(tmp) = getNewOID();
480 chashInsert(record->lookupTable, OID(tmp), tmp);
483 return &tmp[1]; //want space after object header
490 /* This function creates machine piles based on all machines involved in a
491 * transaction commit request */
492 plistnode_t *createPiles(transrecord_t *record) {
494 plistnode_t *pile = NULL;
495 unsigned int machinenum;
496 objheader_t *headeraddr;
497 chashlistnode_t * ptr = record->lookupTable->table;
498 /* Represents number of bins in the chash table */
499 unsigned int size = record->lookupTable->size;
501 for(i = 0; i < size ; i++) {
502 chashlistnode_t * curr = &ptr[i];
503 /* Inner loop to traverse the linked list of the cache lookupTable */
504 while(curr != NULL) {
505 //if the first bin in hash table is empty
508 headeraddr=(objheader_t *) curr->val;
510 //Get machine location for object id (and whether local or not)
511 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
512 machinenum = myIpAddr;
513 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
514 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
518 //Make machine groups
519 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
526 /* This function creates machine piles based on all machines involved in a
527 * transaction commit request */
528 plistnode_t *createPiles(transrecord_t *record) {
530 plistnode_t *pile = NULL;
531 unsigned int machinenum;
532 objheader_t *headeraddr;
533 struct chashentry * ptr = record->lookupTable->table;
534 /* Represents number of bins in the chash table */
535 unsigned int size = record->lookupTable->size;
537 for(i = 0; i < size ; i++) {
538 struct chashentry * curr = & ptr[i];
539 /* Inner loop to traverse the linked list of the cache lookupTable */
540 //if the first bin in hash table is empty
543 headeraddr=(objheader_t *) curr->ptr;
545 //Get machine location for object id (and whether local or not)
546 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
547 machinenum = myIpAddr;
548 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
549 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
553 //Make machine groups
554 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
560 /* This function initiates the transaction commit process
561 * Spawns threads for each of the new connections with Participants
562 * and creates new piles by calling the createPiles(),
563 * Sends a transrequest() to each remote machines for objects found remotely
564 * and calls handleLocalReq() to process objects found locally */
565 int transCommit(transrecord_t *record) {
566 unsigned int tot_bytes_mod, *listmid;
567 plistnode_t *pile, *pile_ptr;
569 char treplyretry; /* keeps track of the common response that needs to be sent */
571 trans_commit_data_t transinfo; /* keeps track of objs locked during transaction */
576 //abort this transaction
577 printf("ABORTING TRANSACTION AT COMMIT\n");
578 objstrDelete(record->cache);
579 chashDelete(record->lookupTable);
590 /* Look through all the objects in the transaction record and make piles
591 * for each machine involved in the transaction*/
593 pile_ptr = pile = createPiles(record);
594 pile_ptr = pile = sortPiles(pile);
599 /* Create the packet to be sent in TRANS_REQUEST */
601 /* Count the number of participants */
603 pilecount = pCount(pile);
605 /* Create a list of machine ids(Participants) involved in transaction */
606 listmid = calloc(pilecount, sizeof(unsigned int));
607 pListMid(pile, listmid);
609 /* Create a socket and getReplyCtrl array, initialize */
610 int socklist[pilecount];
612 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
613 socklist[loopcount] = 0;
614 char getReplyCtrl[pilecount];
615 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
616 getReplyCtrl[loopcount] = 0;
618 /* Process each machine pile */
620 trans_req_data_t *tosend;
621 tosend = calloc(pilecount, sizeof(trans_req_data_t));
622 while(pile != NULL) {
623 tosend[sockindex].f.control = TRANS_REQUEST;
624 tosend[sockindex].f.mcount = pilecount;
625 tosend[sockindex].f.numread = pile->numread;
626 tosend[sockindex].f.nummod = pile->nummod;
627 tosend[sockindex].f.numcreated = pile->numcreated;
628 tosend[sockindex].f.sum_bytes = pile->sum_bytes;
629 tosend[sockindex].listmid = listmid;
630 tosend[sockindex].objread = pile->objread;
631 tosend[sockindex].oidmod = pile->oidmod;
632 tosend[sockindex].oidcreated = pile->oidcreated;
634 if(pile->mid != myIpAddr) {
635 if((sd = getSock2WithLock(transRequestSockPool, pile->mid)) < 0) {
636 printf("transRequest(): socket create error\n");
641 socklist[sockindex] = sd;
642 /* Send bytes of data with TRANS_REQUEST control message */
643 send_data(sd, &(tosend[sockindex].f), sizeof(fixed_data_t));
645 /* Send list of machines involved in the transaction */
647 int size=sizeof(unsigned int)*(tosend[sockindex].f.mcount);
648 send_data(sd, tosend[sockindex].listmid, size);
651 /* Send oids and version number tuples for objects that are read */
653 int size=(sizeof(unsigned int)+sizeof(unsigned short))*(tosend[sockindex].f.numread);
654 send_data(sd, tosend[sockindex].objread, size);
657 /* Send objects that are modified */
659 if((modptr = calloc(1, tosend[sockindex].f.sum_bytes)) == NULL) {
660 printf("Calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
667 for(i = 0; i < tosend[sockindex].f.nummod ; i++) {
669 objheader_t *headeraddr;
670 if((headeraddr = chashSearch(record->lookupTable, tosend[sockindex].oidmod[i])) == NULL) {
671 printf("%s() Error: No such oid %s, %d\n", __func__, __FILE__, __LINE__);
677 GETSIZE(size,headeraddr);
678 size+=sizeof(objheader_t);
679 memcpy(modptr+offset, headeraddr, size);
682 send_data(sd, modptr, tosend[sockindex].f.sum_bytes);
684 } else { //handle request locally
685 handleLocalReq(&tosend[sockindex], &transinfo, record, &getReplyCtrl[sockindex]);
689 } //end of pile processing
690 /* Recv Ctrl msgs from all machines */
692 for(i = 0; i < pilecount; i++) {
693 int sd = socklist[i];
696 recv_data(sd, &control, sizeof(char));
697 //Update common data structure with new ctrl msg
698 getReplyCtrl[i] = control;
699 /* Recv Objects if participant sends TRANS_DISAGREE */
701 if(control == TRANS_DISAGREE) {
703 recv_data(sd, &length, sizeof(int));
705 pthread_mutex_lock(&prefetchcache_mutex);
706 if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) {
707 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
710 pthread_mutex_unlock(&prefetchcache_mutex);
713 pthread_mutex_unlock(&prefetchcache_mutex);
714 recv_data(sd, newAddr, length);
717 unsigned int oidToPrefetch;
718 objheader_t * header;
719 header = (objheader_t *)(((char *)newAddr) + offset);
720 oidToPrefetch = OID(header);
723 GETSIZE(size, header);
724 size += sizeof(objheader_t);
725 //make an entry in prefetch hash table
727 if((oldptr = prehashSearch(oidToPrefetch)) != NULL) {
728 prehashRemove(oidToPrefetch);
729 prehashInsert(oidToPrefetch, header);
731 prehashInsert(oidToPrefetch, header);
733 length = length - size;
736 } //end of receiving objs
740 /* Decide the final response */
741 if((finalResponse = decideResponse(getReplyCtrl, &treplyretry, record, pilecount)) == 0) {
742 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
748 /* Send responses to all machines */
749 for(i = 0; i < pilecount; i++) {
750 int sd = socklist[i];
753 if(finalResponse == TRANS_COMMIT) {
755 /* Update prefetch cache */
756 if((retval = updatePrefetchCache(&(tosend[i]), record)) != 0) {
757 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
764 /* Invalidate objects in other machine cache */
765 if(tosend[i].f.nummod > 0) {
766 if((retval = invalidateObj(&(tosend[i]))) != 0) {
767 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
774 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
775 removethisreadtransaction(tosend[i].objread, tosend[i].f.numread, record);
779 else if (!treplyretry) {
780 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod,record);
781 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread,record);
785 send_data(sd, &finalResponse, sizeof(char));
787 /* Complete local processing */
788 doLocalProcess(finalResponse, &(tosend[i]), &transinfo, record);
790 if(finalResponse == TRANS_COMMIT) {
791 removetransaction(tosend[i].oidmod,tosend[i].f.nummod);
792 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread, record);
793 } else if (!treplyretry) {
794 removethistransaction(tosend[i].oidmod,tosend[i].f.nummod,record);
795 removethisreadtransaction(tosend[i].objread,tosend[i].f.numread,record);
806 /* wait a random amount of time before retrying to commit transaction*/
813 /* Retry trans commit procedure during soft_abort case */
814 } while (treplyretry);
816 if(finalResponse == TRANS_ABORT) {
817 //printf("Aborting trans\n");
822 objstrDelete(record->cache);
823 chashDelete(record->lookupTable);
826 } else if(finalResponse == TRANS_COMMIT) {
831 objstrDelete(record->cache);
832 chashDelete(record->lookupTable);
836 //TODO Add other cases
837 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
843 /* This function handles the local objects involved in a transaction
844 * commiting process. It also makes a decision if this local machine
845 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator */
846 void handleLocalReq(trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *rec, char *getReplyCtrl) {
847 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
848 int numoidnotfound = 0, numoidlocked = 0;
849 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
852 unsigned short version;
854 /* Counters and arrays to formulate decision on control message to be sent */
855 oidnotfound = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod), sizeof(unsigned int));
856 oidlocked = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod +1), sizeof(unsigned int)); // calloc additional 1 byte for
857 //setting a divider between read and write locks
858 numread = tdata->f.numread;
859 /* Process each oid in the machine pile/ group per thread */
860 for (i = 0; i < tdata->f.numread + tdata->f.nummod; i++) {
861 if (i < tdata->f.numread) {
862 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
864 oid = *((unsigned int *)(((char *)tdata->objread) + incr));
865 version = *((unsigned short *)(((char *)tdata->objread) + incr + sizeof(unsigned int)));
866 commitCountForObjRead(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
867 } else { // Objects Modified
868 if(i == tdata->f.numread) {
869 oidlocked[numoidlocked++] = -1;
872 objheader_t *headptr;
873 headptr = (objheader_t *) chashSearch(rec->lookupTable, tdata->oidmod[i-numread]);
874 if (headptr == NULL) {
875 printf("Error: handleLocalReq() returning NULL, no such oid %s, %d\n", __FILE__, __LINE__);
879 version = headptr->version;
880 commitCountForObjMod(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
884 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
885 * if Participant receives a TRANS_COMMIT */
886 transinfo->objlocked = oidlocked;
887 transinfo->objnotfound = oidnotfound;
888 transinfo->modptr = NULL;
889 transinfo->numlocked = numoidlocked;
890 transinfo->numnotfound = numoidnotfound;
892 /* Condition to send TRANS_AGREE */
893 if(v_matchnolock == tdata->f.numread + tdata->f.nummod) {
894 *getReplyCtrl = TRANS_AGREE;
896 /* Condition to send TRANS_SOFT_ABORT */
897 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
898 *getReplyCtrl = TRANS_SOFT_ABORT;
902 void doLocalProcess(char finalResponse, trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *record) {
903 if(finalResponse == TRANS_ABORT) {
904 if(transAbortProcess(transinfo) != 0) {
905 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
909 } else if(finalResponse == TRANS_COMMIT) {
911 /* Invalidate objects in other machine cache */
912 if(tdata->f.nummod > 0) {
914 if((retval = invalidateObj(tdata)) != 0) {
915 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
920 if(transComProcess(tdata, transinfo, record) != 0) {
921 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
926 printf("ERROR...No Decision\n");
930 if (transinfo->objlocked != NULL) {
931 free(transinfo->objlocked);
933 if (transinfo->objnotfound != NULL) {
934 free(transinfo->objnotfound);
938 /* This function decides the reponse that needs to be sent to
939 * all Participant machines after the TRANS_REQUEST protocol */
940 char decideResponse(char *getReplyCtrl, char *treplyretry, transrecord_t *record, int pilecount) {
941 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
943 for (i = 0 ; i < pilecount; i++) {
945 control = getReplyCtrl[i];
948 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
950 /* treat as disagree, pass thru */
959 case TRANS_SOFT_ABORT:
965 if(transdisagree > 0) {
970 /* clear objects from prefetch cache */
973 } else if(transagree == pilecount) {
978 /* Send Abort in soft abort case followed by retry commiting transaction again*/
985 /* This function opens a connection, places an object read request to
986 * the remote machine, reads the control message and object if
987 * available and copies the object and its header to the local
990 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
992 struct sockaddr_in serv_addr;
996 void *objcopy = NULL;
998 int sd = getSock2(transReadSockPool, mnum);
999 char readrequest[sizeof(char)+sizeof(unsigned int)];
1000 readrequest[0] = READ_REQUEST;
1001 *((unsigned int *)(&readrequest[1])) = oid;
1002 send_data(sd, readrequest, sizeof(readrequest));
1004 /* Read response from the Participant */
1005 recv_data(sd, &control, sizeof(char));
1007 if (control==OBJECT_NOT_FOUND) {
1010 /* Read object if found into local cache */
1011 recv_data(sd, &size, sizeof(int));
1012 objcopy = objstrAlloc(record->cache, size);
1013 recv_data(sd, objcopy, size);
1015 /* Insert into cache's lookup table */
1016 chashInsert(record->lookupTable, oid, objcopy);
1022 /* Commit info for objects modified */
1023 void commitCountForObjMod(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1024 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1026 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1027 /* Save the oids not found and number of oids not found for later use */
1028 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1029 /* Save the oids not found and number of oids not found for later use */
1030 oidnotfound[*numoidnotfound] = oid;
1031 (*numoidnotfound)++;
1032 } else { /* If Obj found in machine (i.e. has not moved) */
1033 /* Check if Obj is locked by any previous transaction */
1034 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
1035 if (version == ((objheader_t *)mobj)->version) { /* match versions */
1037 //Keep track of what is locked
1038 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1039 } else { /* If versions don't match ...HARD ABORT */
1041 /* Send TRANS_DISAGREE to Coordinator */
1042 *getReplyCtrl = TRANS_DISAGREE;
1044 //Keep track of what is locked
1045 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1046 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1049 } else { //A lock is acquired some place else
1050 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1052 } else { /* If versions don't match ...HARD ABORT */
1054 /* Send TRANS_DISAGREE to Coordinator */
1055 *getReplyCtrl = TRANS_DISAGREE;
1056 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1063 /* Commit info for objects modified */
1064 void commitCountForObjRead(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
1065 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
1067 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1068 /* Save the oids not found and number of oids not found for later use */
1069 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1070 /* Save the oids not found and number of oids not found for later use */
1071 oidnotfound[*numoidnotfound] = oid;
1072 (*numoidnotfound)++;
1073 } else { /* If Obj found in machine (i.e. has not moved) */
1074 /* Check if Obj is locked by any previous transaction */
1075 if (read_trylock(STATUSPTR(mobj))) { // Can further acquire read locks
1076 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
1078 //Keep track of what is locked
1079 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1080 } else { /* If versions don't match ...HARD ABORT */
1082 /* Send TRANS_DISAGREE to Coordinator */
1083 *getReplyCtrl = TRANS_DISAGREE;
1084 //Keep track of what is locked
1085 oidlocked[(*numoidlocked)++] = OID(((objheader_t *)mobj));
1086 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1089 } else { //Has reached max number of readers or some other transaction
1090 //has acquired a lock on this object
1091 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1093 } else { /* If versions don't match ...HARD ABORT */
1095 /* Send TRANS_DISAGREE to Coordinator */
1096 *getReplyCtrl = TRANS_DISAGREE;
1097 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1104 /* This function completes the ABORT process if the transaction is aborting */
1105 int transAbortProcess(trans_commit_data_t *transinfo) {
1107 unsigned int *objlocked;
1110 numlocked = transinfo->numlocked;
1111 objlocked = transinfo->objlocked;
1113 int useWriteUnlock = 0;
1114 for (i = 0; i < numlocked; i++) {
1115 if(objlocked[i] == -1) {
1119 if((header = mhashSearch(objlocked[i])) == NULL) {
1120 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1123 if(!useWriteUnlock) {
1124 read_unlock(STATUSPTR(header));
1126 write_unlock(STATUSPTR(header));
1133 /*This function completes the COMMIT process if the transaction is commiting*/
1134 int transComProcess(trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *rec) {
1135 objheader_t *header, *tcptr;
1136 int i, nummod, tmpsize, numcreated, numlocked;
1137 unsigned int *oidmod, *oidcreated, *oidlocked;
1140 nummod = tdata->f.nummod;
1141 oidmod = tdata->oidmod;
1142 numcreated = tdata->f.numcreated;
1143 oidcreated = tdata->oidcreated;
1144 numlocked = transinfo->numlocked;
1145 oidlocked = transinfo->objlocked;
1147 for (i = 0; i < nummod; i++) {
1148 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1149 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1152 /* Copy from transaction cache -> main object store */
1153 if ((tcptr = ((objheader_t *) chashSearch(rec->lookupTable, oidmod[i]))) == NULL) {
1154 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1157 GETSIZE(tmpsize, header);
1158 char *tmptcptr = (char *) tcptr;
1160 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
1161 struct ___Object___ *src=(struct ___Object___*)((char*)tmptcptr+sizeof(objheader_t));
1162 dst->___cachedCode___=src->___cachedCode___;
1163 dst->___cachedHash___=src->___cachedHash___;
1165 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
1168 header->version += 1;
1169 if(header->notifylist != NULL) {
1170 notifyAll(&header->notifylist, OID(header), header->version);
1173 /* If object is newly created inside transaction then commit it */
1174 for (i = 0; i < numcreated; i++) {
1175 if ((header = ((objheader_t *) chashSearch(rec->lookupTable, oidcreated[i]))) == NULL) {
1176 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1179 GETSIZE(tmpsize, header);
1180 tmpsize += sizeof(objheader_t);
1181 pthread_mutex_lock(&mainobjstore_mutex);
1182 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1183 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1184 pthread_mutex_unlock(&mainobjstore_mutex);
1187 pthread_mutex_unlock(&mainobjstore_mutex);
1188 /* Initialize read and write locks */
1189 initdsmlocks(STATUSPTR(header));
1190 memcpy(ptrcreate, header, tmpsize);
1191 mhashInsert(oidcreated[i], ptrcreate);
1192 lhashInsert(oidcreated[i], myIpAddr);
1194 /* Unlock locked objects */
1195 int useWriteUnlock = 0;
1196 for(i = 0; i < numlocked; i++) {
1197 if(oidlocked[i] == -1) {
1201 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1202 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1205 if(!useWriteUnlock) {
1206 read_unlock(STATUSPTR(header));
1208 write_unlock(STATUSPTR(header));
1214 prefetchpile_t *foundLocal(char *ptr) {
1215 int siteid = *(GET_SITEID(ptr));
1216 int ntuples = *(GET_NTUPLES(ptr));
1217 unsigned int * oidarray = GET_PTR_OID(ptr);
1218 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1219 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1220 prefetchpile_t * head=NULL;
1224 for(i=0; i<ntuples; i++) {
1225 unsigned short baseindex=(i==0) ? 0 : endoffsets[i-1];
1226 unsigned short endindex=endoffsets[i];
1227 unsigned int oid=oidarray[i];
1232 //Look up fields locally
1233 for(newbase=baseindex; newbase<endindex; newbase++) {
1234 if (!lookupObject(&oid, arryfields[newbase]))
1236 //Ended in a null pointer...
1240 //Entire prefetch is local
1241 if (newbase==endindex&&checkoid(oid)) {
1245 //Add to remote requests
1246 machinenum=lhashSearch(oid);
1247 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1252 /* handle dynamic prefetching */
1253 handleDynPrefetching(numLocal, ntuples, siteid);
1257 int checkoid(unsigned int oid) {
1258 objheader_t *header;
1259 if ((header=mhashSearch(oid))!=NULL) {
1262 } else if ((header=prehashSearch(oid))!=NULL) {
1270 int lookupObject(unsigned int * oid, short offset) {
1271 objheader_t *header;
1272 if ((header=mhashSearch(*oid))!=NULL) {
1275 } else if ((header=prehashSearch(*oid))!=NULL) {
1282 if(TYPE(header) > NUMCLASSES) {
1283 int elementsize = classsize[TYPE(header)];
1284 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1285 int length = ao->___length___;
1286 /* Check if array out of bounds */
1287 if(offset < 0 || offset >= length) {
1288 //if yes treat the object as found
1292 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1295 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1301 /* This function is called by the thread calling transPrefetch */
1302 void *transPrefetch(void *t) {
1304 /* read from prefetch queue */
1305 void *node=gettail();
1306 /* Check if the tuples are found locally, if yes then reduce them further*/
1307 /* and group requests by remote machine ids by calling the makePreGroups() */
1308 prefetchpile_t *pilehead = foundLocal(node);
1310 if (pilehead!=NULL) {
1311 // Get sock from shared pool
1312 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1314 /* Send Prefetch Request */
1315 prefetchpile_t *ptr = pilehead;
1316 while(ptr != NULL) {
1317 sendPrefetchReq(ptr, sd);
1321 /* Release socket */
1322 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1324 /* Deallocated pilehead */
1325 mcdealloc(pilehead);
1327 // Deallocate the prefetch queue pile node
1332 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1335 int size=sizeof(char)+sizeof(int);
1336 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1337 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1342 *buf=TRANS_PREFETCH;
1345 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1346 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1349 *((unsigned int *)buf)=tmp->oid;
1350 buf+=sizeof(unsigned int);
1351 *((unsigned int *)(buf)) = myIpAddr;
1352 buf+=sizeof(unsigned int);
1353 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1354 buf+=tmp->numoffset*sizeof(short);
1357 send_data(sd, buft, size);
1361 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1366 /* Send TRANS_PREFETCH control message */
1367 control = TRANS_PREFETCH;
1368 send_data(sd, &control, sizeof(char));
1370 /* Send Oids and offsets in pairs */
1371 tmp = mcpilenode->objpiles;
1372 while(tmp != NULL) {
1373 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1374 char oidnoffset[len];
1375 char *buf=oidnoffset;
1376 *((int*)buf) = tmp->numoffset;
1378 *((unsigned int *)buf) = tmp->oid;
1379 buf+=sizeof(unsigned int);
1380 *((unsigned int *)buf) = myIpAddr;
1381 buf += sizeof(unsigned int);
1382 memcpy(buf, tmp->offset, (tmp->numoffset)*sizeof(short));
1383 send_data(sd, oidnoffset, len);
1387 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1389 send_data(sd, &endpair, sizeof(int));
1394 int getPrefetchResponse(int sd) {
1395 int length = 0, size = 0;
1398 void *modptr, *oldptr;
1400 recv_data((int)sd, &length, sizeof(int));
1401 size = length - sizeof(int);
1402 char recvbuffer[size];
1404 recv_data((int)sd, recvbuffer, size);
1405 control = *((char *) recvbuffer);
1406 if(control == OBJECT_FOUND) {
1407 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1408 size = size - (sizeof(char) + sizeof(unsigned int));
1409 pthread_mutex_lock(&prefetchcache_mutex);
1410 if ((modptr = prefetchobjstrAlloc(size)) == NULL) {
1411 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1412 pthread_mutex_unlock(&prefetchcache_mutex);
1415 pthread_mutex_unlock(&prefetchcache_mutex);
1416 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1419 /* Insert the oid and its address into the prefetch hash lookup table */
1420 /* Do a version comparison if the oid exists */
1421 if((oldptr = prehashSearch(oid)) != NULL) {
1422 /* If older version then update with new object ptr */
1423 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1425 prehashInsert(oid, modptr);
1427 } else { /* Else add the object ptr to hash table*/
1428 prehashInsert(oid, modptr);
1430 /* Lock the Prefetch Cache look up table*/
1431 pthread_mutex_lock(&pflookup.lock);
1432 /* Broadcast signal on prefetch cache condition variable */
1433 pthread_cond_broadcast(&pflookup.cond);
1434 /* Unlock the Prefetch Cache look up table*/
1435 pthread_mutex_unlock(&pflookup.lock);
1436 } else if(control == OBJECT_NOT_FOUND) {
1437 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1438 /* TODO: For each object not found query DHT for new location and retrieve the object */
1439 /* Throw an error */
1440 //printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1443 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1449 unsigned short getObjType(unsigned int oid) {
1450 objheader_t *objheader;
1451 unsigned short numoffset[] ={0};
1452 short fieldoffset[] ={};
1454 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1456 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1458 unsigned int mid = lhashSearch(oid);
1459 int sd = getSock2(transReadSockPool, mid);
1460 char remotereadrequest[sizeof(char)+sizeof(unsigned int)];
1461 remotereadrequest[0] = READ_REQUEST;
1462 *((unsigned int *)(&remotereadrequest[1])) = oid;
1463 send_data(sd, remotereadrequest, sizeof(remotereadrequest));
1465 /* Read response from the Participant */
1467 recv_data(sd, &control, sizeof(char));
1469 if (control==OBJECT_NOT_FOUND) {
1470 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1474 /* Read object if found into local cache */
1476 recv_data(sd, &size, sizeof(int));
1478 pthread_mutex_lock(&prefetchcache_mutex);
1479 if ((objheader = prefetchobjstrAlloc(size)) == NULL) {
1480 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1483 pthread_mutex_unlock(&prefetchcache_mutex);
1484 recv_data(sd, objheader, size);
1485 prehashInsert(oid, objheader);
1486 return TYPE(objheader);
1489 if((buffer = calloc(1, size)) == NULL) {
1490 printf("%s() Calloc Error %s at line %d\n", __func__, __FILE__, __LINE__);
1494 recv_data(sd, buffer, size);
1495 objheader = (objheader_t *)buffer;
1496 unsigned short type = TYPE(objheader);
1505 return TYPE(objheader);
1508 int startRemoteThread(unsigned int oid, unsigned int mid) {
1510 struct sockaddr_in remoteAddr;
1511 char msg[1 + sizeof(unsigned int)];
1515 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1516 perror("startRemoteThread():socket()");
1520 bzero(&remoteAddr, sizeof(remoteAddr));
1521 remoteAddr.sin_family = AF_INET;
1522 remoteAddr.sin_port = htons(LISTEN_PORT);
1523 remoteAddr.sin_addr.s_addr = htonl(mid);
1525 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1526 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1527 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1531 msg[0] = START_REMOTE_THREAD;
1532 *((unsigned int *) &msg[1]) = oid;
1533 send_data(sock, msg, 1 + sizeof(unsigned int));
1540 //TODO: when reusing oids, make sure they are not already in use!
1541 static unsigned int id = 0xFFFFFFFF;
1542 unsigned int getNewOID(void) {
1544 if (id > oidMax || id < oidMin) {
1550 int processConfigFile() {
1552 const int maxLineLength = 200;
1553 char lineBuffer[maxLineLength];
1555 const char *delimiters = " \t\n";
1559 configFile = fopen(CONFIG_FILENAME, "r");
1560 if (configFile == NULL) {
1561 printf("error opening %s:\n", CONFIG_FILENAME);
1566 numHostsInSystem = 0;
1567 sizeOfHostArray = 8;
1568 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1570 while(fgets(lineBuffer, maxLineLength, configFile) != NULL) {
1571 commentBegin = strchr(lineBuffer, '#');
1572 if (commentBegin != NULL)
1573 *commentBegin = '\0';
1574 token = strtok(lineBuffer, delimiters);
1575 while (token != NULL) {
1576 tmpAddr = inet_addr(token);
1577 if ((int)tmpAddr == -1) {
1578 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1582 addHost(htonl(tmpAddr));
1583 token = strtok(NULL, delimiters);
1589 if (numHostsInSystem < 1) {
1590 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1594 myIpAddr = getMyIpAddr("en1");
1596 myIpAddr = getMyIpAddr("eth0");
1598 myIndexInHostArray = findHost(myIpAddr);
1599 if (myIndexInHostArray == -1) {
1600 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1603 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1604 oidMin = oidsPerBlock * myIndexInHostArray;
1605 if (myIndexInHostArray == numHostsInSystem - 1)
1606 oidMax = 0xFFFFFFFF;
1608 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1613 void addHost(unsigned int hostIp) {
1614 unsigned int *tmpArray;
1616 if (findHost(hostIp) != -1)
1619 if (numHostsInSystem == sizeOfHostArray) {
1620 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1621 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1623 hostIpAddrs = tmpArray;
1626 hostIpAddrs[numHostsInSystem++] = hostIp;
1631 int findHost(unsigned int hostIp) {
1633 for (i = 0; i < numHostsInSystem; i++)
1634 if (hostIpAddrs[i] == hostIp)
1641 /* This function sends notification request per thread waiting on object(s) whose version
1643 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1645 objheader_t *objheader;
1646 struct sockaddr_in remoteAddr;
1647 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1651 unsigned short version;
1652 unsigned int oid,mid;
1653 static unsigned int threadid = 0;
1654 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1655 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1656 notifydata_t *ndata;
1659 if((mid = lhashSearch(oid)) == 0) {
1660 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1664 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1665 perror("reqNotify():socket()");
1669 bzero(&remoteAddr, sizeof(remoteAddr));
1670 remoteAddr.sin_family = AF_INET;
1671 remoteAddr.sin_port = htons(LISTEN_PORT);
1672 remoteAddr.sin_addr.s_addr = htonl(mid);
1674 /* Generate unique threadid */
1677 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1678 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1679 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1682 ndata->numoid = numoid;
1683 ndata->threadid = threadid;
1684 ndata->oidarry = oidarry;
1685 ndata->versionarry = versionarry;
1686 ndata->threadcond = threadcond;
1687 ndata->threadnotify = threadnotify;
1688 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1689 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1694 /* Send number of oids, oidarry, version array, machine id and threadid */
1695 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1696 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1697 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1701 msg[0] = THREAD_NOTIFY_REQUEST;
1702 *((unsigned int *)(&msg[1])) = numoid;
1703 /* Send array of oids */
1704 size = sizeof(unsigned int);
1706 for(i = 0;i < numoid; i++) {
1708 *((unsigned int *)(&msg[1] + size)) = oid;
1709 size += sizeof(unsigned int);
1712 /* Send array of version */
1713 for(i = 0;i < numoid; i++) {
1714 version = versionarry[i];
1715 *((unsigned short *)(&msg[1] + size)) = version;
1716 size += sizeof(unsigned short);
1719 *((unsigned int *)(&msg[1] + size)) = myIpAddr; size += sizeof(unsigned int);
1720 *((unsigned int *)(&msg[1] + size)) = threadid;
1721 pthread_mutex_lock(&(ndata->threadnotify));
1722 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1723 send_data(sock, msg, size);
1724 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1725 pthread_mutex_unlock(&(ndata->threadnotify));
1728 pthread_cond_destroy(&threadcond);
1729 pthread_mutex_destroy(&threadnotify);
1735 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1736 notifydata_t *ndata;
1737 int i, objIsFound = 0, index;
1740 //Look up the tid and call the corresponding pthread_cond_signal
1741 if((ndata = notifyhashSearch(tid)) == NULL) {
1742 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1745 for(i = 0; i < ndata->numoid; i++) {
1746 if(ndata->oidarry[i] == oid) {
1751 if(objIsFound == 0) {
1752 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1755 if(version <= ndata->versionarry[index]) {
1756 printf("threadNotify(): New version %d has not changed since last version for oid = %d, %s, %d\n", version, oid, __FILE__, __LINE__);
1760 /* Clear from prefetch cache and free thread related data structure */
1761 if((ptr = prehashSearch(oid)) != NULL) {
1765 pthread_mutex_lock(&(ndata->threadnotify));
1766 pthread_cond_signal(&(ndata->threadcond));
1767 pthread_mutex_unlock(&(ndata->threadnotify));
1774 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1777 struct sockaddr_in remoteAddr;
1778 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1779 int sock, status, size, bytesSent;
1781 while(*head != NULL) {
1784 //create a socket connection to that machine
1785 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1786 perror("notifyAll():socket()");
1790 bzero(&remoteAddr, sizeof(remoteAddr));
1791 remoteAddr.sin_family = AF_INET;
1792 remoteAddr.sin_port = htons(LISTEN_PORT);
1793 remoteAddr.sin_addr.s_addr = htonl(mid);
1794 //send Thread Notify response and threadid to that machine
1795 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1796 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1797 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1801 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1802 msg[0] = THREAD_NOTIFY_RESPONSE;
1803 *((unsigned int *)&msg[1]) = oid;
1804 size = sizeof(unsigned int);
1805 *((unsigned short *)(&msg[1]+ size)) = version;
1806 size+= sizeof(unsigned short);
1807 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1809 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1810 send_data(sock, msg, size);
1821 void transAbort(transrecord_t *trans) {
1823 removetransactionhash(trans->lookupTable, trans);
1825 objstrDelete(trans->cache);
1826 chashDelete(trans->lookupTable);
1830 /* This function inserts necessary information into
1831 * a machine pile data structure */
1832 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
1833 plistnode_t *ptr, *tmp;
1834 int found = 0, offset = 0;
1837 //Add oid into a machine that is already present in the pile linked list structure
1838 while(tmp != NULL) {
1839 if (tmp->mid == mid) {
1842 if (STATUS(headeraddr) & NEW) {
1843 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
1845 GETSIZE(tmpsize, headeraddr);
1846 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1847 } else if (STATUS(headeraddr) & DIRTY) {
1848 tmp->oidmod[tmp->nummod] = OID(headeraddr);
1850 GETSIZE(tmpsize, headeraddr);
1851 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1853 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
1854 *((unsigned int *)(((char *)tmp->objread) + offset))=OID(headeraddr);
1855 offset += sizeof(unsigned int);
1856 *((short *)(((char *)tmp->objread) + offset)) = headeraddr->version;
1864 //Add oid for any new machine
1867 if((ptr = pCreate(num_objs)) == NULL) {
1871 if (STATUS(headeraddr) & NEW) {
1872 ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
1874 GETSIZE(tmpsize, headeraddr);
1875 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1876 } else if (STATUS(headeraddr) & DIRTY) {
1877 ptr->oidmod[ptr->nummod] = OID(headeraddr);
1879 GETSIZE(tmpsize, headeraddr);
1880 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1882 *((unsigned int *)ptr->objread)=OID(headeraddr);
1883 offset = sizeof(unsigned int);
1884 *((short *)(((char *)ptr->objread) + offset)) = headeraddr->version;
1892 STATUS(headeraddr) =0;
1898 plistnode_t *sortPiles(plistnode_t *pileptr) {
1899 plistnode_t *head, *ptr, *tail;
1902 /* Get tail pointer */
1908 plistnode_t *prev = pileptr;
1909 /* Arrange local machine processing at the end of the pile list */
1910 while(ptr != NULL) {
1912 if(ptr->mid == myIpAddr && (prev != pileptr)) {
1913 prev->next = ptr->next;
1918 if((ptr->mid == myIpAddr) && (prev == pileptr)) {