3 #include "machinepile.h"
8 #include "threadnotify.h"
10 #include "addUdpEnhance.h"
11 #include "addPrefetchEnhance.h"
19 #define PREFETCH_CACHE_SIZE 1048576 //1MB
20 #define CONFIG_FILENAME "dstm.conf"
23 /* Global Variables */
24 extern int classsize[];
25 pfcstats_t *evalPrefetch;
26 extern int numprefetchsites; //Global variable containing number of prefetch sites
27 extern pthread_mutex_t mainobjstore_mutex; // Mutex to lock main Object store
28 objstr_t *prefetchcache; //Global Prefetch cache
29 pthread_mutex_t prefetchcache_mutex; // Mutex to lock Prefetch Cache
30 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
31 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
32 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
33 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
34 extern objstr_t *mainobjstore;
35 unsigned int myIpAddr;
36 unsigned int *hostIpAddrs;
39 int myIndexInHostArray;
40 unsigned int oidsPerBlock;
44 sockPoolHashTable_t *transReadSockPool;
45 sockPoolHashTable_t *transPrefetchSockPool;
46 sockPoolHashTable_t *transRequestSockPool;
47 pthread_mutex_t notifymutex;
48 pthread_mutex_t atomicObjLock;
50 /***********************************
51 * Global Variables for statistics
52 **********************************/
53 int numTransCommit = 0;
54 int numTransAbort = 0;
57 int nprehashSearch = 0;
61 void printhex(unsigned char *, int);
62 plistnode_t *createPiles(transrecord_t *);
63 plistnode_t *sortPiles(plistnode_t *pileptr);
65 /*******************************
66 * Send and Recv function calls
67 *******************************/
68 void send_data(int fd, void *buf, int buflen) {
69 char *buffer = (char *)(buf);
73 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
83 void recv_data(int fd, void *buf, int buflen) {
84 char *buffer = (char *)(buf);
88 numbytes = recv(fd, buffer, size, 0);
98 int recv_data_errorcode(int fd, void *buf, int buflen) {
99 char *buffer = (char *)(buf);
103 numbytes = recv(fd, buffer, size, 0);
106 if (numbytes == -1) {
116 void printhex(unsigned char *ptr, int numBytes) {
118 for (i = 0; i < numBytes; i++) {
120 printf("0%x ", ptr[i]);
122 printf("%x ", ptr[i]);
128 inline int arrayLength(int *array) {
130 for(i=0 ; array[i] != -1; i++)
135 inline int findmax(int *array, int arraylength) {
138 for(i = 0; i < arraylength; i++) {
146 /* This function is a prefetch call generated by the compiler that
147 * populates the shared primary prefetch queue*/
148 void prefetch(int siteid, int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
149 /* Allocate for the queue node*/
150 int qnodesize = 2*sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
152 char * node= getmemory(qnodesize);
153 int top=endoffsets[ntuples-1];
157 /* Set queue node values */
159 /* TODO: Remove this after testing */
160 evalPrefetch[siteid].callcount++;
162 *((int *)(node))=siteid;
163 *((int *)(node + sizeof(int))) = ntuples;
165 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
166 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
167 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
169 /* Lock and insert into primary prefetch queue */
173 /* This function starts up the transaction runtime. */
174 int dstmStartup(const char * option) {
175 pthread_t thread_Listen, udp_thread_Listen;
177 int master=option!=NULL && strcmp(option, "master")==0;
181 if (processConfigFile() != 0)
182 return 0; //TODO: return error value, cause main program to exit
189 printf("Trans stats is on\n");
193 //Initialize socket pool
194 transReadSockPool = createSockPool(transReadSockPool, DEFAULTSOCKPOOLSIZE);
195 transPrefetchSockPool = createSockPool(transPrefetchSockPool, DEFAULTSOCKPOOLSIZE);
196 transRequestSockPool = createSockPool(transRequestSockPool, DEFAULTSOCKPOOLSIZE);
202 pthread_attr_init(&attr);
203 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
206 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
209 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
212 dstmListen((void *)fd);
217 //TODO Use this later
218 void *pCacheAlloc(objstr_t *store, unsigned int size) {
224 while(ptr->next != NULL) {
225 /* check if store is empty */
226 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
241 /* This function initiates the prefetch thread A queue is shared
242 * between the main thread of execution and the prefetch thread to
243 * process the prefetch call Call from compiler populates the shared
244 * queue with prefetch requests while prefetch thread processes the
245 * prefetch requests */
248 //Create and initialize prefetch cache structure
250 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
252 if((evalPrefetch = initPrefetchStats()) == NULL) {
253 printf("%s() Error allocating memory at %s, %d\n", __func__, __FILE__, __LINE__);
258 /* Initialize attributes for mutex */
259 pthread_mutexattr_init(&prefetchcache_mutex_attr);
260 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
262 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
263 pthread_mutex_init(¬ifymutex, NULL);
264 pthread_mutex_init(&atomicObjLock, NULL);
266 //Create prefetch cache lookup table
267 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
272 //Initialize primary shared queue
274 //Initialize machine pile w/prefetch oids and offsets shared queue
277 //Create the primary prefetch thread
280 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
282 pthread_detach(tPrefetch);
286 /* This function stops the threads spawned */
290 pthread_cancel(tPrefetch);
291 for(t = 0; t < NUM_THREADS; t++)
292 pthread_cancel(wthreads[t]);
298 /* This functions inserts randowm wait delays in the order of msec
299 * Mostly used when transaction commits retry*/
306 req.tv_nsec = (long)(1000 + (t%10000)); //1-11 microsec
307 nanosleep(&req, NULL);
311 /* This function initializes things required in the transaction start*/
312 transrecord_t *transStart() {
314 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL) {
315 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
318 tmp->cache = objstrCreate(1048576);
319 tmp->lookupTable = chashCreate(CHASH_SIZE, CLOADFACTOR);
321 tmp->revertlist=NULL;
326 /* This function finds the location of the objects involved in a transaction
327 * and returns the pointer to the object if found in a remote location */
328 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
329 unsigned int machinenumber;
330 objheader_t *tmp, *objheader;
331 objheader_t *objcopy;
339 if((objheader = chashSearch(record->lookupTable, oid)) != NULL) {
343 /* Search local transaction cache */
345 return &objheader[1];
349 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
353 /* Look up in machine lookup table and copy into cache*/
354 GETSIZE(size, objheader);
355 size += sizeof(objheader_t);
356 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
357 memcpy(objcopy, objheader, size);
358 /* Insert into cache's lookup table */
360 chashInsert(record->lookupTable, OID(objheader), objcopy);
368 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
372 /* Look up in prefetch cache */
374 size+=sizeof(objheader_t);
375 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
376 memcpy(objcopy, tmp, size);
377 /* Insert into cache's lookup table */
378 chashInsert(record->lookupTable, OID(tmp), objcopy);
386 /* Get the object from the remote location */
387 if((machinenumber = lhashSearch(oid)) == 0) {
388 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
391 objcopy = getRemoteObj(record, machinenumber, oid);
393 if(objcopy == NULL) {
394 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
410 /* This function creates objects in the transaction record */
411 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
412 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
413 OID(tmp) = getNewOID();
417 chashInsert(record->lookupTable, OID(tmp), tmp);
420 return &tmp[1]; //want space after object header
426 /* This function creates machine piles based on all machines involved in a
427 * transaction commit request */
428 plistnode_t *createPiles(transrecord_t *record) {
430 plistnode_t *pile = NULL;
431 unsigned int machinenum;
432 objheader_t *headeraddr;
433 chashlistnode_t * ptr = record->lookupTable->table;
434 /* Represents number of bins in the chash table */
435 unsigned int size = record->lookupTable->size;
437 for(i = 0; i < size ; i++) {
438 chashlistnode_t * curr = &ptr[i];
439 /* Inner loop to traverse the linked list of the cache lookupTable */
440 while(curr != NULL) {
441 //if the first bin in hash table is empty
445 if ((headeraddr = (objheader_t *) chashSearch(record->lookupTable, curr->key)) == NULL) {
446 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
450 //Get machine location for object id (and whether local or not)
451 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
452 machinenum = myIpAddr;
453 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
454 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
458 //Make machine groups
459 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
466 /* This function initiates the transaction commit process
467 * Spawns threads for each of the new connections with Participants
468 * and creates new piles by calling the createPiles(),
469 * Sends a transrequest() to each remote machines for objects found remotely
470 * and calls handleLocalReq() to process objects found locally */
471 int transCommit(transrecord_t *record) {
472 unsigned int tot_bytes_mod, *listmid;
473 plistnode_t *pile, *pile_ptr;
475 char treplyretry; /* keeps track of the common response that needs to be sent */
477 trans_commit_data_t transinfo; /* keeps track of objs locked during transaction */
484 /* Look through all the objects in the transaction record and make piles
485 * for each machine involved in the transaction*/
487 pile_ptr = pile = createPiles(record);
488 pile_ptr = pile = sortPiles(pile);
493 /* Create the packet to be sent in TRANS_REQUEST */
495 /* Count the number of participants */
497 pilecount = pCount(pile);
499 /* Create a list of machine ids(Participants) involved in transaction */
500 listmid = calloc(pilecount, sizeof(unsigned int));
501 pListMid(pile, listmid);
503 /* Create a socket and getReplyCtrl array, initialize */
504 int socklist[pilecount];
506 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
507 socklist[loopcount] = 0;
508 char getReplyCtrl[pilecount];
509 for(loopcount = 0 ; loopcount < pilecount; loopcount++)
510 getReplyCtrl[loopcount] = 0;
512 /* Process each machine pile */
514 trans_req_data_t *tosend;
515 tosend = calloc(pilecount, sizeof(trans_req_data_t));
516 while(pile != NULL) {
517 tosend[sockindex].f.control = TRANS_REQUEST;
518 tosend[sockindex].f.mcount = pilecount;
519 tosend[sockindex].f.numread = pile->numread;
520 tosend[sockindex].f.nummod = pile->nummod;
521 tosend[sockindex].f.numcreated = pile->numcreated;
522 tosend[sockindex].f.sum_bytes = pile->sum_bytes;
523 tosend[sockindex].listmid = listmid;
524 tosend[sockindex].objread = pile->objread;
525 tosend[sockindex].oidmod = pile->oidmod;
526 tosend[sockindex].oidcreated = pile->oidcreated;
528 if(pile->mid != myIpAddr) {
529 if((sd = getSock2WithLock(transRequestSockPool, pile->mid)) < 0) {
530 printf("transRequest(): socket create error\n");
535 socklist[sockindex] = sd;
536 /* Send bytes of data with TRANS_REQUEST control message */
537 send_data(sd, &(tosend[sockindex].f), sizeof(fixed_data_t));
539 /* Send list of machines involved in the transaction */
541 int size=sizeof(unsigned int)*(tosend[sockindex].f.mcount);
542 send_data(sd, tosend[sockindex].listmid, size);
545 /* Send oids and version number tuples for objects that are read */
547 int size=(sizeof(unsigned int)+sizeof(unsigned short))*(tosend[sockindex].f.numread);
548 send_data(sd, tosend[sockindex].objread, size);
551 /* Send objects that are modified */
553 if((modptr = calloc(1, tosend[sockindex].f.sum_bytes)) == NULL) {
554 printf("Calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
561 for(i = 0; i < tosend[sockindex].f.nummod ; i++) {
563 objheader_t *headeraddr;
564 if((headeraddr = chashSearch(record->lookupTable, tosend[sockindex].oidmod[i])) == NULL) {
565 printf("%s() Error: No such oid %s, %d\n", __func__, __FILE__, __LINE__);
571 GETSIZE(size,headeraddr);
572 size+=sizeof(objheader_t);
573 memcpy(modptr+offset, headeraddr, size);
576 send_data(sd, modptr, tosend[sockindex].f.sum_bytes);
578 } else { //handle request locally
579 handleLocalReq(&tosend[sockindex], &transinfo, record, &getReplyCtrl[sockindex]);
583 } //end of pile processing
584 /* Recv Ctrl msgs from all machines */
586 for(i = 0; i < pilecount; i++) {
587 int sd = socklist[i];
590 recv_data(sd, &control, sizeof(char));
591 //Update common data structure with new ctrl msg
592 getReplyCtrl[i] = control;
593 /* Recv Objects if participant sends TRANS_DISAGREE */
595 if(control == TRANS_DISAGREE) {
597 recv_data(sd, &length, sizeof(int));
599 pthread_mutex_lock(&prefetchcache_mutex);
600 if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) {
601 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
604 pthread_mutex_unlock(&prefetchcache_mutex);
607 pthread_mutex_unlock(&prefetchcache_mutex);
608 recv_data(sd, newAddr, length);
611 unsigned int oidToPrefetch;
612 objheader_t * header;
613 header = (objheader_t *)(((char *)newAddr) + offset);
614 oidToPrefetch = OID(header);
616 GETSIZE(size, header);
617 size += sizeof(objheader_t);
618 //make an entry in prefetch hash table
620 if((oldptr = prehashSearch(oidToPrefetch)) != NULL) {
621 prehashRemove(oidToPrefetch);
622 prehashInsert(oidToPrefetch, header);
624 prehashInsert(oidToPrefetch, header);
626 length = length - size;
629 } //end of receiving objs
633 /* Decide the final response */
634 if((finalResponse = decideResponse(getReplyCtrl, &treplyretry, record, pilecount)) == 0) {
635 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
641 /* Send responses to all machines */
642 for(i = 0; i < pilecount; i++) {
643 int sd = socklist[i];
646 if(finalResponse == TRANS_COMMIT) {
648 /* Update prefetch cache */
649 if((retval = updatePrefetchCache(&(tosend[i]), record)) != 0) {
650 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
655 /* Invalidate objects in other machine cache */
656 if(tosend[i].f.nummod > 0) {
657 if((retval = invalidateObj(&(tosend[i]))) != 0) {
658 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
666 send_data(sd, &finalResponse, sizeof(char));
668 /* Complete local processing */
669 doLocalProcess(finalResponse, &(tosend[i]), &transinfo, record);
678 /* wait a random amount of time before retrying to commit transaction*/
685 /* Retry trans commit procedure during soft_abort case */
686 } while (treplyretry);
688 if(finalResponse == TRANS_ABORT) {
689 //printf("Aborting trans\n");
694 objstrDelete(record->cache);
695 chashDelete(record->lookupTable);
698 } else if(finalResponse == TRANS_COMMIT) {
703 objstrDelete(record->cache);
704 chashDelete(record->lookupTable);
708 //TODO Add other cases
709 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
715 /* This function handles the local objects involved in a transaction
716 * commiting process. It also makes a decision if this local machine
717 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator */
718 void handleLocalReq(trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *rec, char *getReplyCtrl) {
719 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
720 int numoidnotfound = 0, numoidlocked = 0;
721 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
724 unsigned short version;
726 /* Counters and arrays to formulate decision on control message to be sent */
727 oidnotfound = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod), sizeof(unsigned int));
728 oidlocked = (unsigned int *) calloc((tdata->f.numread + tdata->f.nummod +1), sizeof(unsigned int)); // calloc additional 1 byte for
729 //setting a divider between read and write locks
730 numread = tdata->f.numread;
731 /* Process each oid in the machine pile/ group per thread */
732 for (i = 0; i < tdata->f.numread + tdata->f.nummod; i++) {
733 if (i < tdata->f.numread) {
734 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
736 oid = *((unsigned int *)(((char *)tdata->objread) + incr));
737 version = *((unsigned short *)(((char *)tdata->objread) + incr + sizeof(unsigned int)));
738 commitCountForObjRead(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
739 } else { // Objects Modified
740 if(i == tdata->f.numread) {
741 oidlocked[numoidlocked] = -1;
745 objheader_t *headptr;
746 headptr = (objheader_t *) chashSearch(rec->lookupTable, tdata->oidmod[i-numread]);
747 if (headptr == NULL) {
748 printf("Error: handleLocalReq() returning NULL, no such oid %s, %d\n", __FILE__, __LINE__);
752 version = headptr->version;
753 commitCountForObjMod(getReplyCtrl, oidnotfound, oidlocked, &numoidnotfound, &numoidlocked, &v_nomatch, &v_matchlock, &v_matchnolock, oid, version);
757 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
758 * if Participant receives a TRANS_COMMIT */
759 transinfo->objlocked = oidlocked;
760 transinfo->objnotfound = oidnotfound;
761 transinfo->modptr = NULL;
762 transinfo->numlocked = numoidlocked;
763 transinfo->numnotfound = numoidnotfound;
765 /* Condition to send TRANS_AGREE */
766 if(v_matchnolock == tdata->f.numread + tdata->f.nummod) {
767 *getReplyCtrl = TRANS_AGREE;
769 /* Condition to send TRANS_SOFT_ABORT */
770 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
771 *getReplyCtrl = TRANS_SOFT_ABORT;
775 void doLocalProcess(char finalResponse, trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *record) {
776 if(finalResponse == TRANS_ABORT) {
777 if(transAbortProcess(transinfo) != 0) {
778 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
782 } else if(finalResponse == TRANS_COMMIT) {
784 /* Invalidate objects in other machine cache */
785 if(tdata->f.nummod > 0) {
787 if((retval = invalidateObj(tdata)) != 0) {
788 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
793 if(transComProcess(tdata, transinfo, record) != 0) {
794 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
801 if (transinfo->objlocked != NULL) {
802 free(transinfo->objlocked);
804 if (transinfo->objnotfound != NULL) {
805 free(transinfo->objnotfound);
809 /* This function decides the reponse that needs to be sent to
810 * all Participant machines after the TRANS_REQUEST protocol */
811 char decideResponse(char *getReplyCtrl, char *treplyretry, transrecord_t *record, int pilecount) {
812 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
814 for (i = 0 ; i < pilecount; i++) {
816 control = getReplyCtrl[i];
819 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
821 /* treat as disagree, pass thru */
830 case TRANS_SOFT_ABORT:
836 if(transdisagree > 0) {
841 /* clear objects from prefetch cache */
844 } else if(transagree == pilecount) {
849 /* Send Abort in soft abort case followed by retry commiting transaction again*/
856 /* This function opens a connection, places an object read request to
857 * the remote machine, reads the control message and object if
858 * available and copies the object and its header to the local
861 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
863 struct sockaddr_in serv_addr;
867 void *objcopy = NULL;
869 int sd = getSock2(transReadSockPool, mnum);
870 char readrequest[sizeof(char)+sizeof(unsigned int)];
871 readrequest[0] = READ_REQUEST;
872 *((unsigned int *)(&readrequest[1])) = oid;
873 send_data(sd, readrequest, sizeof(readrequest));
875 /* Read response from the Participant */
876 recv_data(sd, &control, sizeof(char));
878 if (control==OBJECT_NOT_FOUND) {
881 /* Read object if found into local cache */
882 recv_data(sd, &size, sizeof(int));
883 objcopy = objstrAlloc(record->cache, size);
884 recv_data(sd, objcopy, size);
885 /* Insert into cache's lookup table */
886 chashInsert(record->lookupTable, oid, objcopy);
892 /* Commit info for objects modified */
893 void commitCountForObjMod(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
894 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
896 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
897 /* Save the oids not found and number of oids not found for later use */
898 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
899 /* Save the oids not found and number of oids not found for later use */
900 oidnotfound[*numoidnotfound] = oid;
902 } else { /* If Obj found in machine (i.e. has not moved) */
903 /* Check if Obj is locked by any previous transaction */
904 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
905 if (version == ((objheader_t *)mobj)->version) { /* match versions */
907 //Keep track of what is locked
908 oidlocked[*numoidlocked] = OID(((objheader_t *)mobj));
910 } else { /* If versions don't match ...HARD ABORT */
912 /* Send TRANS_DISAGREE to Coordinator */
913 *getReplyCtrl = TRANS_DISAGREE;
915 //Keep track of what is locked
916 oidlocked[*numoidlocked] = OID(((objheader_t *)mobj));
918 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
921 } else { //A lock is acquired some place else
922 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
924 } else { /* If versions don't match ...HARD ABORT */
926 /* Send TRANS_DISAGREE to Coordinator */
927 *getReplyCtrl = TRANS_DISAGREE;
928 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
935 /* Commit info for objects modified */
936 void commitCountForObjRead(char *getReplyCtrl, unsigned int *oidnotfound, unsigned int *oidlocked, int *numoidnotfound,
937 int *numoidlocked, int *v_nomatch, int *v_matchlock, int *v_matchnolock, unsigned int oid, unsigned short version) {
939 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
940 /* Save the oids not found and number of oids not found for later use */
941 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
942 /* Save the oids not found and number of oids not found for later use */
943 oidnotfound[*numoidnotfound] = oid;
945 } else { /* If Obj found in machine (i.e. has not moved) */
946 /* Check if Obj is locked by any previous transaction */
947 if (read_trylock(STATUSPTR(mobj))) { // Can further acquire read locks
948 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
950 //Keep track of what is locked
951 oidlocked[*numoidlocked] = OID(((objheader_t *)mobj));
953 } else { /* If versions don't match ...HARD ABORT */
955 /* Send TRANS_DISAGREE to Coordinator */
956 *getReplyCtrl = TRANS_DISAGREE;
957 //Keep track of what is locked
958 oidlocked[*numoidlocked] = OID(((objheader_t *)mobj));
960 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
963 } else { //Has reached max number of readers or some other transaction
964 //has acquired a lock on this object
965 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
967 } else { /* If versions don't match ...HARD ABORT */
969 /* Send TRANS_DISAGREE to Coordinator */
970 *getReplyCtrl = TRANS_DISAGREE;
971 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
978 /* This function completes the ABORT process if the transaction is aborting */
979 int transAbortProcess(trans_commit_data_t *transinfo) {
981 unsigned int *objlocked;
984 numlocked = transinfo->numlocked;
985 objlocked = transinfo->objlocked;
987 int useWriteUnlock = 0;
988 for (i = 0; i < numlocked; i++) {
989 if(objlocked[i] == -1) {
993 if((header = mhashSearch(objlocked[i])) == NULL) {
994 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
997 if(!useWriteUnlock) {
998 read_unlock(STATUSPTR(header));
1000 write_unlock(STATUSPTR(header));
1007 /*This function completes the COMMIT process if the transaction is commiting*/
1008 int transComProcess(trans_req_data_t *tdata, trans_commit_data_t *transinfo, transrecord_t *rec) {
1009 objheader_t *header, *tcptr;
1010 int i, nummod, tmpsize, numcreated, numlocked;
1011 unsigned int *oidmod, *oidcreated, *oidlocked;
1014 nummod = tdata->f.nummod;
1015 oidmod = tdata->oidmod;
1016 numcreated = tdata->f.numcreated;
1017 oidcreated = tdata->oidcreated;
1018 numlocked = transinfo->numlocked;
1019 oidlocked = transinfo->objlocked;
1021 for (i = 0; i < nummod; i++) {
1022 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1023 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1026 /* Copy from transaction cache -> main object store */
1027 if ((tcptr = ((objheader_t *) chashSearch(rec->lookupTable, oidmod[i]))) == NULL) {
1028 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1031 GETSIZE(tmpsize, header);
1032 char *tmptcptr = (char *) tcptr;
1033 memcpy((char*)header+sizeof(objheader_t), (char *)tmptcptr+ sizeof(objheader_t), tmpsize);
1034 header->version += 1;
1035 if(header->notifylist != NULL) {
1036 notifyAll(&header->notifylist, OID(header), header->version);
1039 /* If object is newly created inside transaction then commit it */
1040 for (i = 0; i < numcreated; i++) {
1041 if ((header = ((objheader_t *) chashSearch(rec->lookupTable, oidcreated[i]))) == NULL) {
1042 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1045 GETSIZE(tmpsize, header);
1046 tmpsize += sizeof(objheader_t);
1047 pthread_mutex_lock(&mainobjstore_mutex);
1048 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1049 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1050 pthread_mutex_unlock(&mainobjstore_mutex);
1053 pthread_mutex_unlock(&mainobjstore_mutex);
1054 /* Initialize read and write locks */
1055 initdsmlocks(STATUSPTR(header));
1056 memcpy(ptrcreate, header, tmpsize);
1057 mhashInsert(oidcreated[i], ptrcreate);
1058 lhashInsert(oidcreated[i], myIpAddr);
1060 /* Unlock locked objects */
1061 int useWriteUnlock = 0;
1062 for(i = 0; i < numlocked; i++) {
1063 if(oidlocked[i] == -1) {
1067 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1068 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1071 if(!useWriteUnlock) {
1072 read_unlock(STATUSPTR(header));
1074 write_unlock(STATUSPTR(header));
1080 prefetchpile_t *foundLocal(char *ptr) {
1081 int siteid = *(GET_SITEID(ptr));
1082 int ntuples = *(GET_NTUPLES(ptr));
1083 unsigned int * oidarray = GET_PTR_OID(ptr);
1084 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1085 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1086 prefetchpile_t * head=NULL;
1090 for(i=0; i<ntuples; i++) {
1091 unsigned short baseindex=(i==0) ? 0 : endoffsets[i-1];
1092 unsigned short endindex=endoffsets[i];
1093 unsigned int oid=oidarray[i];
1098 //Look up fields locally
1099 for(newbase=baseindex; newbase<endindex; newbase++) {
1100 if (!lookupObject(&oid, arryfields[newbase]))
1102 //Ended in a null pointer...
1106 //Entire prefetch is local
1107 if (newbase==endindex&&checkoid(oid)) {
1111 //Add to remote requests
1112 machinenum=lhashSearch(oid);
1113 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1118 /* handle dynamic prefetching */
1119 handleDynPrefetching(numLocal, ntuples, siteid);
1123 int checkoid(unsigned int oid) {
1124 objheader_t *header;
1125 if ((header=mhashSearch(oid))!=NULL) {
1128 } else if ((header=prehashSearch(oid))!=NULL) {
1136 int lookupObject(unsigned int * oid, short offset) {
1137 objheader_t *header;
1138 if ((header=mhashSearch(*oid))!=NULL) {
1141 } else if ((header=prehashSearch(*oid))!=NULL) {
1148 if(TYPE(header) > NUMCLASSES) {
1149 int elementsize = classsize[TYPE(header)];
1150 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1151 int length = ao->___length___;
1152 /* Check if array out of bounds */
1153 if(offset < 0 || offset >= length) {
1154 //if yes treat the object as found
1158 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1161 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1167 /* This function is called by the thread calling transPrefetch */
1168 void *transPrefetch(void *t) {
1170 /* read from prefetch queue */
1171 void *node=gettail();
1172 /* Check if the tuples are found locally, if yes then reduce them further*/
1173 /* and group requests by remote machine ids by calling the makePreGroups() */
1174 prefetchpile_t *pilehead = foundLocal(node);
1176 if (pilehead!=NULL) {
1177 // Get sock from shared pool
1178 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1180 /* Send Prefetch Request */
1181 prefetchpile_t *ptr = pilehead;
1182 while(ptr != NULL) {
1183 sendPrefetchReq(ptr, sd);
1187 /* Release socket */
1188 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1190 /* Deallocated pilehead */
1191 mcdealloc(pilehead);
1193 // Deallocate the prefetch queue pile node
1198 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1201 int size=sizeof(char)+sizeof(int);
1202 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1203 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1208 *buf=TRANS_PREFETCH;
1211 for(tmp=mcpilenode->objpiles; tmp!=NULL; tmp=tmp->next) {
1212 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1215 *((unsigned int *)buf)=tmp->oid;
1216 buf+=sizeof(unsigned int);
1217 *((unsigned int *)(buf)) = myIpAddr;
1218 buf+=sizeof(unsigned int);
1219 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1220 buf+=tmp->numoffset*sizeof(short);
1223 send_data(sd, buft, size);
1227 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1232 /* Send TRANS_PREFETCH control message */
1233 control = TRANS_PREFETCH;
1234 send_data(sd, &control, sizeof(char));
1236 /* Send Oids and offsets in pairs */
1237 tmp = mcpilenode->objpiles;
1238 while(tmp != NULL) {
1239 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1240 char oidnoffset[len];
1241 char *buf=oidnoffset;
1242 *((int*)buf) = tmp->numoffset;
1244 *((unsigned int *)buf) = tmp->oid;
1245 buf+=sizeof(unsigned int);
1246 *((unsigned int *)buf) = myIpAddr;
1247 buf += sizeof(unsigned int);
1248 memcpy(buf, tmp->offset, (tmp->numoffset)*sizeof(short));
1249 send_data(sd, oidnoffset, len);
1253 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1255 send_data(sd, &endpair, sizeof(int));
1260 int getPrefetchResponse(int sd) {
1261 int length = 0, size = 0;
1264 void *modptr, *oldptr;
1266 recv_data((int)sd, &length, sizeof(int));
1267 size = length - sizeof(int);
1268 char recvbuffer[size];
1270 recv_data((int)sd, recvbuffer, size);
1271 control = *((char *) recvbuffer);
1272 if(control == OBJECT_FOUND) {
1273 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1274 size = size - (sizeof(char) + sizeof(unsigned int));
1275 pthread_mutex_lock(&prefetchcache_mutex);
1276 if ((modptr = prefetchobjstrAlloc(size)) == NULL) {
1277 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1278 pthread_mutex_unlock(&prefetchcache_mutex);
1281 pthread_mutex_unlock(&prefetchcache_mutex);
1282 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1285 /* Insert the oid and its address into the prefetch hash lookup table */
1286 /* Do a version comparison if the oid exists */
1287 if((oldptr = prehashSearch(oid)) != NULL) {
1288 /* If older version then update with new object ptr */
1289 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1291 prehashInsert(oid, modptr);
1293 } else { /* Else add the object ptr to hash table*/
1294 prehashInsert(oid, modptr);
1296 /* Lock the Prefetch Cache look up table*/
1297 pthread_mutex_lock(&pflookup.lock);
1298 /* Broadcast signal on prefetch cache condition variable */
1299 pthread_cond_broadcast(&pflookup.cond);
1300 /* Unlock the Prefetch Cache look up table*/
1301 pthread_mutex_unlock(&pflookup.lock);
1302 } else if(control == OBJECT_NOT_FOUND) {
1303 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1304 /* TODO: For each object not found query DHT for new location and retrieve the object */
1305 /* Throw an error */
1306 //printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1309 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1315 unsigned short getObjType(unsigned int oid) {
1316 objheader_t *objheader;
1317 unsigned short numoffset[] ={0};
1318 short fieldoffset[] ={};
1320 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1322 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1324 unsigned int mid = lhashSearch(oid);
1325 int sd = getSock2(transReadSockPool, mid);
1326 char remotereadrequest[sizeof(char)+sizeof(unsigned int)];
1327 remotereadrequest[0] = READ_REQUEST;
1328 *((unsigned int *)(&remotereadrequest[1])) = oid;
1329 send_data(sd, remotereadrequest, sizeof(remotereadrequest));
1331 /* Read response from the Participant */
1333 recv_data(sd, &control, sizeof(char));
1335 if (control==OBJECT_NOT_FOUND) {
1336 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
1340 /* Read object if found into local cache */
1342 recv_data(sd, &size, sizeof(int));
1344 pthread_mutex_lock(&prefetchcache_mutex);
1345 if ((objheader = prefetchobjstrAlloc(size)) == NULL) {
1346 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
1349 pthread_mutex_unlock(&prefetchcache_mutex);
1350 recv_data(sd, objheader, size);
1351 prehashInsert(oid, objheader);
1352 return TYPE(objheader);
1355 if((buffer = calloc(1, size)) == NULL) {
1356 printf("%s() Calloc Error %s at line %d\n", __func__, __FILE__, __LINE__);
1360 recv_data(sd, buffer, size);
1361 objheader = (objheader_t *)buffer;
1362 unsigned short type = TYPE(objheader);
1371 return TYPE(objheader);
1374 int startRemoteThread(unsigned int oid, unsigned int mid) {
1376 struct sockaddr_in remoteAddr;
1377 char msg[1 + sizeof(unsigned int)];
1381 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1382 perror("startRemoteThread():socket()");
1386 bzero(&remoteAddr, sizeof(remoteAddr));
1387 remoteAddr.sin_family = AF_INET;
1388 remoteAddr.sin_port = htons(LISTEN_PORT);
1389 remoteAddr.sin_addr.s_addr = htonl(mid);
1391 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1392 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1393 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1397 msg[0] = START_REMOTE_THREAD;
1398 *((unsigned int *) &msg[1]) = oid;
1399 send_data(sock, msg, 1 + sizeof(unsigned int));
1406 //TODO: when reusing oids, make sure they are not already in use!
1407 static unsigned int id = 0xFFFFFFFF;
1408 unsigned int getNewOID(void) {
1410 if (id > oidMax || id < oidMin) {
1416 int processConfigFile() {
1418 const int maxLineLength = 200;
1419 char lineBuffer[maxLineLength];
1421 const char *delimiters = " \t\n";
1425 configFile = fopen(CONFIG_FILENAME, "r");
1426 if (configFile == NULL) {
1427 printf("error opening %s:\n", CONFIG_FILENAME);
1432 numHostsInSystem = 0;
1433 sizeOfHostArray = 8;
1434 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1436 while(fgets(lineBuffer, maxLineLength, configFile) != NULL) {
1437 commentBegin = strchr(lineBuffer, '#');
1438 if (commentBegin != NULL)
1439 *commentBegin = '\0';
1440 token = strtok(lineBuffer, delimiters);
1441 while (token != NULL) {
1442 tmpAddr = inet_addr(token);
1443 if ((int)tmpAddr == -1) {
1444 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1448 addHost(htonl(tmpAddr));
1449 token = strtok(NULL, delimiters);
1455 if (numHostsInSystem < 1) {
1456 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1460 myIpAddr = getMyIpAddr("en1");
1462 myIpAddr = getMyIpAddr("eth0");
1464 myIndexInHostArray = findHost(myIpAddr);
1465 if (myIndexInHostArray == -1) {
1466 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1469 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1470 oidMin = oidsPerBlock * myIndexInHostArray;
1471 if (myIndexInHostArray == numHostsInSystem - 1)
1472 oidMax = 0xFFFFFFFF;
1474 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1479 void addHost(unsigned int hostIp) {
1480 unsigned int *tmpArray;
1482 if (findHost(hostIp) != -1)
1485 if (numHostsInSystem == sizeOfHostArray) {
1486 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1487 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1489 hostIpAddrs = tmpArray;
1492 hostIpAddrs[numHostsInSystem++] = hostIp;
1497 int findHost(unsigned int hostIp) {
1499 for (i = 0; i < numHostsInSystem; i++)
1500 if (hostIpAddrs[i] == hostIp)
1507 /* This function sends notification request per thread waiting on object(s) whose version
1509 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1511 objheader_t *objheader;
1512 struct sockaddr_in remoteAddr;
1513 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1517 unsigned short version;
1518 unsigned int oid,mid;
1519 static unsigned int threadid = 0;
1520 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1521 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1522 notifydata_t *ndata;
1525 if((mid = lhashSearch(oid)) == 0) {
1526 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1530 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1531 perror("reqNotify():socket()");
1535 bzero(&remoteAddr, sizeof(remoteAddr));
1536 remoteAddr.sin_family = AF_INET;
1537 remoteAddr.sin_port = htons(LISTEN_PORT);
1538 remoteAddr.sin_addr.s_addr = htonl(mid);
1540 /* Generate unique threadid */
1543 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1544 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1545 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1548 ndata->numoid = numoid;
1549 ndata->threadid = threadid;
1550 ndata->oidarry = oidarry;
1551 ndata->versionarry = versionarry;
1552 ndata->threadcond = threadcond;
1553 ndata->threadnotify = threadnotify;
1554 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1555 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1560 /* Send number of oids, oidarry, version array, machine id and threadid */
1561 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1562 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1563 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1567 msg[0] = THREAD_NOTIFY_REQUEST;
1568 *((unsigned int *)(&msg[1])) = numoid;
1569 /* Send array of oids */
1570 size = sizeof(unsigned int);
1575 *((unsigned int *)(&msg[1] + size)) = oid;
1576 size += sizeof(unsigned int);
1581 /* Send array of version */
1585 version = versionarry[i];
1586 *((unsigned short *)(&msg[1] + size)) = version;
1587 size += sizeof(unsigned short);
1592 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1593 size += sizeof(unsigned int);
1594 *((unsigned int *)(&msg[1] + size)) = threadid;
1595 pthread_mutex_lock(&(ndata->threadnotify));
1596 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1597 send_data(sock, msg, size);
1598 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1599 pthread_mutex_unlock(&(ndata->threadnotify));
1602 pthread_cond_destroy(&threadcond);
1603 pthread_mutex_destroy(&threadnotify);
1609 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1610 notifydata_t *ndata;
1611 int i, objIsFound = 0, index;
1614 //Look up the tid and call the corresponding pthread_cond_signal
1615 if((ndata = notifyhashSearch(tid)) == NULL) {
1616 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1619 for(i = 0; i < ndata->numoid; i++) {
1620 if(ndata->oidarry[i] == oid) {
1625 if(objIsFound == 0) {
1626 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1629 if(version <= ndata->versionarry[index]) {
1630 printf("threadNotify(): New version %d has not changed since last version for oid = %d, %s, %d\n", version, oid, __FILE__, __LINE__);
1634 /* Clear from prefetch cache and free thread related data structure */
1635 if((ptr = prehashSearch(oid)) != NULL) {
1639 pthread_cond_signal(&(ndata->threadcond));
1646 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1649 struct sockaddr_in remoteAddr;
1650 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1651 int sock, status, size, bytesSent;
1653 while(*head != NULL) {
1656 //create a socket connection to that machine
1657 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1658 perror("notifyAll():socket()");
1662 bzero(&remoteAddr, sizeof(remoteAddr));
1663 remoteAddr.sin_family = AF_INET;
1664 remoteAddr.sin_port = htons(LISTEN_PORT);
1665 remoteAddr.sin_addr.s_addr = htonl(mid);
1666 //send Thread Notify response and threadid to that machine
1667 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1668 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1669 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1673 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1674 msg[0] = THREAD_NOTIFY_RESPONSE;
1675 *((unsigned int *)&msg[1]) = oid;
1676 size = sizeof(unsigned int);
1677 *((unsigned short *)(&msg[1]+ size)) = version;
1678 size+= sizeof(unsigned short);
1679 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1681 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1682 send_data(sock, msg, size);
1693 void transAbort(transrecord_t *trans) {
1694 objstrDelete(trans->cache);
1695 chashDelete(trans->lookupTable);
1699 /* This function inserts necessary information into
1700 * a machine pile data structure */
1701 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
1702 plistnode_t *ptr, *tmp;
1703 int found = 0, offset = 0;
1706 //Add oid into a machine that is already present in the pile linked list structure
1707 while(tmp != NULL) {
1708 if (tmp->mid == mid) {
1711 if (STATUS(headeraddr) & NEW) {
1712 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
1714 GETSIZE(tmpsize, headeraddr);
1715 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1716 } else if (STATUS(headeraddr) & DIRTY) {
1717 tmp->oidmod[tmp->nummod] = OID(headeraddr);
1719 GETSIZE(tmpsize, headeraddr);
1720 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1722 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
1723 *((unsigned int *)(((char *)tmp->objread) + offset))=OID(headeraddr);
1724 offset += sizeof(unsigned int);
1725 *((short *)(((char *)tmp->objread) + offset)) = headeraddr->version;
1733 //Add oid for any new machine
1736 if((ptr = pCreate(num_objs)) == NULL) {
1740 if (STATUS(headeraddr) & NEW) {
1741 ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
1743 GETSIZE(tmpsize, headeraddr);
1744 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1745 } else if (STATUS(headeraddr) & DIRTY) {
1746 ptr->oidmod[ptr->nummod] = OID(headeraddr);
1748 GETSIZE(tmpsize, headeraddr);
1749 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1751 *((unsigned int *)ptr->objread)=OID(headeraddr);
1752 offset = sizeof(unsigned int);
1753 *((short *)(((char *)ptr->objread) + offset)) = headeraddr->version;
1761 STATUS(headeraddr) =0;
1767 plistnode_t *sortPiles(plistnode_t *pileptr) {
1768 plistnode_t *head, *ptr, *tail;
1771 /* Get tail pointer */
1777 plistnode_t *prev = pileptr;
1778 /* Arrange local machine processing at the end of the pile list */
1779 while(ptr != NULL) {
1781 if(ptr->mid == myIpAddr && (prev != pileptr)) {
1782 prev->next = ptr->next;
1787 if((ptr->mid == myIpAddr) && (prev == pileptr)) {