3 #include "machinepile.h"
8 #include "threadnotify.h"
10 #include "addUdpEnhance.h"
11 #include "addPrefetchEnhance.h"
18 #define PREFETCH_CACHE_SIZE 1048576 //1MB
19 #define CONFIG_FILENAME "dstm.conf"
21 /* Global Variables */
22 extern int classsize[];
23 pfcstats_t *evalPrefetch;
24 objstr_t *prefetchcache; //Global Prefetch cache
25 pthread_mutex_t prefetchcache_mutex;// Mutex to lock Prefetch Cache
26 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
27 extern pthread_mutex_t mainobjstore_mutex;// Mutex to lock main Object store
28 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
29 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
30 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
31 extern objstr_t *mainobjstore;
32 unsigned int myIpAddr;
33 unsigned int *hostIpAddrs;
36 int myIndexInHostArray;
37 unsigned int oidsPerBlock;
41 sockPoolHashTable_t *transReadSockPool;
42 sockPoolHashTable_t *transPrefetchSockPool;
43 sockPoolHashTable_t *transRequestSockPool;
44 pthread_mutex_t notifymutex;
45 pthread_mutex_t atomicObjLock;
47 /***********************************
48 * Global Variables for statistics
49 **********************************/
50 int numTransCommit = 0;
51 int numTransAbort = 0;
54 int nprehashSearch = 0;
57 void printhex(unsigned char *, int);
58 plistnode_t *createPiles(transrecord_t *);
60 /*******************************
61 * Send and Recv function calls
62 *******************************/
63 void send_data(int fd , void *buf, int buflen) {
64 char *buffer = (char *)(buf);
68 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
78 void recv_data(int fd , void *buf, int buflen) {
79 char *buffer = (char *)(buf);
83 numbytes = recv(fd, buffer, size, 0);
93 int recv_data_errorcode(int fd , void *buf, int buflen) {
94 char *buffer = (char *)(buf);
98 numbytes = recv(fd, buffer, size, 0);
101 if (numbytes == -1) {
111 void printhex(unsigned char *ptr, int numBytes) {
113 for (i = 0; i < numBytes; i++) {
115 printf("0%x ", ptr[i]);
117 printf("%x ", ptr[i]);
123 inline int arrayLength(int *array) {
125 for(i=0 ;array[i] != -1; i++)
130 inline int findmax(int *array, int arraylength) {
133 for(i = 0; i < arraylength; i++){
141 /* This function is a prefetch call generated by the compiler that
142 * populates the shared primary prefetch queue*/
143 void prefetch(int siteid, int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
144 /* Allocate for the queue node*/
145 int qnodesize = 2*sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
147 char * node= getmemory(qnodesize);
148 int top=endoffsets[ntuples-1];
152 /* Set queue node values */
154 /* TODO: Remove this after testing */
155 evalPrefetch[siteid].callcount++;
157 *((int *)(node))=siteid;
158 *((int *)(node + sizeof(int))) = ntuples;
160 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
161 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
162 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
164 /* Lock and insert into primary prefetch queue */
168 /* This function starts up the transaction runtime. */
169 int dstmStartup(const char * option) {
170 pthread_t thread_Listen, udp_thread_Listen;
172 int master=option!=NULL && strcmp(option, "master")==0;
176 if (processConfigFile() != 0)
177 return 0; //TODO: return error value, cause main program to exit
184 printf("Trans stats is on\n");
188 //Initialize socket pool
189 transReadSockPool = createSockPool(transReadSockPool, 2*numHostsInSystem+1);
190 transPrefetchSockPool = createSockPool(transPrefetchSockPool, 2*numHostsInSystem+1);
191 transRequestSockPool = createSockPool(transRequestSockPool, 2*numHostsInSystem+1);
198 pthread_attr_init(&attr);
199 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
200 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
202 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
205 dstmListen((void *)fd);
210 //TODO Use this later
211 void *pCacheAlloc(objstr_t *store, unsigned int size) {
217 while(ptr->next != NULL) {
218 /* check if store is empty */
219 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
234 /* This function initiates the prefetch thread A queue is shared
235 * between the main thread of execution and the prefetch thread to
236 * process the prefetch call Call from compiler populates the shared
237 * queue with prefetch requests while prefetch thread processes the
238 * prefetch requests */
243 //Create and initialize prefetch cache structure
244 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
246 if((evalPrefetch = initPrefetchStats()) == NULL) {
247 printf("%s() Error allocating memory at %s, %d\n", __func__, __FILE__, __LINE__);
251 /* Initialize attributes for mutex */
252 pthread_mutexattr_init(&prefetchcache_mutex_attr);
253 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
255 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
257 pthread_mutex_init(¬ifymutex, NULL);
258 pthread_mutex_init(&atomicObjLock, NULL);
259 //Create prefetch cache lookup table
260 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
265 //Initialize primary shared queue
267 //Initialize machine pile w/prefetch oids and offsets shared queue
270 //Create the primary prefetch thread
272 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
274 pthread_detach(tPrefetch);
277 /* This function stops the threads spawned */
280 pthread_cancel(tPrefetch);
281 for(t = 0; t < NUM_THREADS; t++)
282 pthread_cancel(wthreads[t]);
287 /* This functions inserts randowm wait delays in the order of msec
288 * Mostly used when transaction commits retry*/
295 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
296 nanosleep(&req, NULL);
300 /* This function initializes things required in the transaction start*/
301 transrecord_t *transStart() {
303 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL){
304 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
307 tmp->cache = objstrCreate(1048576);
308 tmp->lookupTable = chashCreate(CHASH_SIZE, CLOADFACTOR);
310 tmp->revertlist=NULL;
315 /* This function finds the location of the objects involved in a transaction
316 * and returns the pointer to the object if found in a remote location */
317 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
318 unsigned int machinenumber;
319 objheader_t *tmp, *objheader;
320 objheader_t *objcopy;
328 if((objheader = chashSearch(record->lookupTable, oid)) != NULL){
332 /* Search local transaction cache */
334 return &objheader[1];
338 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
342 /* Look up in machine lookup table and copy into cache*/
343 GETSIZE(size, objheader);
344 size += sizeof(objheader_t);
345 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
346 memcpy(objcopy, objheader, size);
347 /* Insert into cache's lookup table */
349 chashInsert(record->lookupTable, OID(objheader), objcopy);
355 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
359 /* Look up in prefetch cache */
361 size+=sizeof(objheader_t);
362 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
363 memcpy(objcopy, tmp, size);
364 /* Insert into cache's lookup table */
365 chashInsert(record->lookupTable, OID(tmp), objcopy);
372 /* Get the object from the remote location */
373 if((machinenumber = lhashSearch(oid)) == 0) {
374 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
377 objcopy = getRemoteObj(record, machinenumber, oid);
379 if(objcopy == NULL) {
380 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
396 /* This function creates objects in the transaction record */
397 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
398 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
399 OID(tmp) = getNewOID();
403 chashInsert(record->lookupTable, OID(tmp), tmp);
406 return &tmp[1]; //want space after object header
412 /* This function creates machine piles based on all machines involved in a
413 * transaction commit request */
414 plistnode_t *createPiles(transrecord_t *record) {
416 plistnode_t *pile = NULL;
417 unsigned int machinenum;
418 objheader_t *headeraddr;
419 chashlistnode_t * ptr = record->lookupTable->table;
420 /* Represents number of bins in the chash table */
421 unsigned int size = record->lookupTable->size;
423 for(i = 0; i < size ; i++) {
424 chashlistnode_t * curr = &ptr[i];
425 /* Inner loop to traverse the linked list of the cache lookupTable */
426 while(curr != NULL) {
427 //if the first bin in hash table is empty
431 if ((headeraddr = (objheader_t *) chashSearch(record->lookupTable, curr->key)) == NULL) {
432 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
436 //Get machine location for object id (and whether local or not)
437 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
438 machinenum = myIpAddr;
439 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
440 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
444 //Make machine groups
445 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
452 /* This function initiates the transaction commit process
453 * Spawns threads for each of the new connections with Participants
454 * and creates new piles by calling the createPiles(),
455 * Sends a transrequest() to each remote machines for objects found remotely
456 * and calls handleLocalReq() to process objects found locally */
457 int transCommit(transrecord_t *record) {
458 unsigned int tot_bytes_mod, *listmid;
459 plistnode_t *pile, *pile_ptr;
461 int pilecount, offset, threadnum, trecvcount;
463 char transid[TID_LEN];
464 trans_req_data_t *tosend;
465 trans_commit_data_t transinfo;
466 static int newtid = 0;
467 char treplyctrl, treplyretry; /* keeps track of the common response that needs to be sent */
468 thread_data_array_t *thread_data_array;
469 local_thread_data_array_t *ltdata;
477 thread_data_array = NULL;
480 /* Look through all the objects in the transaction record and make piles
481 * for each machine involved in the transaction*/
483 pile_ptr = pile = createPiles(record);
488 /* Create the packet to be sent in TRANS_REQUEST */
490 /* Count the number of participants */
491 pilecount = pCount(pile);
493 /* Create a list of machine ids(Participants) involved in transaction */
494 listmid = calloc(pilecount, sizeof(unsigned int));
495 pListMid(pile, listmid);
498 /* Initialize thread variables,
499 * Spawn a thread for each Participant involved in a transaction */
500 pthread_t thread[pilecount];
502 pthread_cond_t tcond;
503 pthread_mutex_t tlock;
504 pthread_mutex_t tlshrd;
506 thread_data_array = (thread_data_array_t *) calloc(pilecount, sizeof(thread_data_array_t));
507 ltdata = calloc(1, sizeof(local_thread_data_array_t));
509 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
511 /* Initialize and set thread detach attribute */
512 pthread_attr_init(&attr);
513 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
514 pthread_mutex_init(&tlock, NULL);
515 pthread_cond_init(&tcond, NULL);
517 /* Process each machine pile */
518 while(pile != NULL) {
519 //Create transaction id
521 tosend = calloc(1, sizeof(trans_req_data_t));
522 tosend->f.control = TRANS_REQUEST;
523 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
524 tosend->f.mcount = pilecount;
525 tosend->f.numread = pile->numread;
526 tosend->f.nummod = pile->nummod;
527 tosend->f.numcreated = pile->numcreated;
528 tosend->f.sum_bytes = pile->sum_bytes;
529 tosend->listmid = listmid;
530 tosend->objread = pile->objread;
531 tosend->oidmod = pile->oidmod;
532 tosend->oidcreated = pile->oidcreated;
533 thread_data_array[threadnum].thread_id = threadnum;
534 thread_data_array[threadnum].mid = pile->mid;
535 thread_data_array[threadnum].buffer = tosend;
536 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
537 thread_data_array[threadnum].threshold = &tcond;
538 thread_data_array[threadnum].lock = &tlock;
539 thread_data_array[threadnum].count = &trecvcount;
540 thread_data_array[threadnum].replyctrl = &treplyctrl;
541 thread_data_array[threadnum].replyretry = &treplyretry;
542 thread_data_array[threadnum].rec = record;
543 /* If local do not create any extra connection */
544 if(pile->mid != myIpAddr) { /* Not local */
546 rc = pthread_create(&thread[threadnum], &attr, transRequest, (void *) &thread_data_array[threadnum]);
549 perror("Error in pthread create\n");
550 pthread_cond_destroy(&tcond);
551 pthread_mutex_destroy(&tlock);
554 for (i = 0; i < threadnum; i++)
555 free(thread_data_array[i].buffer);
556 free(thread_data_array);
561 ltdata->tdata = &thread_data_array[threadnum];
562 ltdata->transinfo = &transinfo;
564 val = pthread_create(&thread[threadnum], &attr, handleLocalReq, (void *) ltdata);
567 perror("Error in pthread create\n");
568 pthread_cond_destroy(&tcond);
569 pthread_mutex_destroy(&tlock);
572 for (i = 0; i < threadnum; i++)
573 free(thread_data_array[i].buffer);
574 free(thread_data_array);
583 /* Free attribute and wait for the other threads */
584 pthread_attr_destroy(&attr);
586 for (i = 0; i < threadnum; i++) {
587 rc = pthread_join(thread[i], NULL);
590 printf("Error: return code from pthread_join() is %d\n", rc);
591 pthread_cond_destroy(&tcond);
592 pthread_mutex_destroy(&tlock);
595 for (j = i; j < threadnum; j++) {
596 free(thread_data_array[j].buffer);
600 free(thread_data_array[i].buffer);
604 pthread_cond_destroy(&tcond);
605 pthread_mutex_destroy(&tlock);
611 /* wait a random amount of time before retrying to commit transaction*/
613 free(thread_data_array);
618 /* Retry trans commit procedure during soft_abort case */
619 } while (treplyretry);
621 if(treplyctrl == TRANS_ABORT) {
626 char a[] = "Aborting";
630 objstrDelete(record->cache);
631 chashDelete(record->lookupTable);
633 free(thread_data_array);
636 } else if(treplyctrl == TRANS_COMMIT) {
641 char a[] = "Commiting";
645 objstrDelete(record->cache);
646 chashDelete(record->lookupTable);
648 free(thread_data_array);
652 //TODO Add other cases
653 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
659 /* This function sends information involved in the transaction request
660 * to participants and accepts a response from particpants.
661 * It calls decideresponse() to decide on what control message
662 * to send next to participants and sends the message using sendResponse()*/
663 void *transRequest(void *threadarg) {
665 struct sockaddr_in serv_addr;
666 thread_data_array_t *tdata;
667 objheader_t *headeraddr;
668 char control, recvcontrol;
669 char machineip[16], retval;
671 tdata = (thread_data_array_t *) threadarg;
673 if((sd = getSock2WithLock(transRequestSockPool, tdata->mid)) < 0) {
674 printf("transRequest(): socket create error\n");
678 /* Send bytes of data with TRANS_REQUEST control message */
679 send_data(sd, &(tdata->buffer->f), sizeof(fixed_data_t));
681 /* Send list of machines involved in the transaction */
683 int size=sizeof(unsigned int)*tdata->buffer->f.mcount;
684 send_data(sd, tdata->buffer->listmid, size);
687 /* Send oids and version number tuples for objects that are read */
689 int size=(sizeof(unsigned int)+sizeof(unsigned short))*tdata->buffer->f.numread;
690 send_data(sd, tdata->buffer->objread, size);
693 /* Send objects that are modified */
694 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
696 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
697 GETSIZE(size,headeraddr);
698 size+=sizeof(objheader_t);
699 send_data(sd, headeraddr, size);
702 /* Read control message from Participant */
703 recv_data(sd, &control, sizeof(char));
704 /* Recv Objects if participant sends TRANS_DISAGREE */
705 if(control == TRANS_DISAGREE) {
707 recv_data(sd, &length, sizeof(int));
709 pthread_mutex_lock(&prefetchcache_mutex);
710 if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) {
711 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
714 pthread_mutex_unlock(&prefetchcache_mutex);
715 recv_data(sd, newAddr, length);
718 unsigned int oidToPrefetch;
719 objheader_t * header;
720 header = (objheader_t *) (((char *)newAddr) + offset);
721 oidToPrefetch = OID(header);
723 char a[] = "object type";
724 TABORT8(__func__, a, TYPE(header));
727 GETSIZE(size, header);
728 size += sizeof(objheader_t);
729 //make an entry in prefetch hash table
731 if((oldptr = prehashSearch(oidToPrefetch)) != NULL) {
732 prehashRemove(oidToPrefetch);
733 prehashInsert(oidToPrefetch, header);
735 prehashInsert(oidToPrefetch, header);
737 length = length - size;
742 recvcontrol = control;
746 TABORT5(__func__, a, c, tdata->mid, control);
748 /* Update common data structure and increment count */
749 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
751 /* Lock and update count */
752 /* Thread sleeps until all messages from pariticipants are received by coordinator */
753 pthread_mutex_lock(tdata->lock);
755 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
757 /* Wake up the threads and invoke decideResponse (once) */
758 if(*(tdata->count) == tdata->buffer->f.mcount) {
759 decideResponse(tdata);
760 pthread_cond_broadcast(tdata->threshold);
762 pthread_cond_wait(tdata->threshold, tdata->lock);
764 pthread_mutex_unlock(tdata->lock);
766 if(*(tdata->replyctrl) == TRANS_COMMIT) {
768 /* Update prefetch cache */
769 if((retval = updatePrefetchCache(tdata)) != 0) {
770 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
774 /* Invalidate objects in other machine cache */
775 if(tdata->buffer->f.nummod > 0) {
776 if((retval = invalidateObj(tdata)) != 0) {
777 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
783 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT
784 * to all participants in their respective socket */
785 if (sendResponse(tdata, sd) == 0) {
786 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
790 recv_data((int)sd, &control, sizeof(char));
792 if(control == TRANS_UNSUCESSFUL) {
793 //printf("DEBUG-> TRANS_ABORTED\n");
794 } else if(control == TRANS_SUCESSFUL) {
795 //printf("DEBUG-> TRANS_SUCCESSFUL\n");
797 //printf("DEBUG-> Error: Incorrect Transaction End Message %d\n", control);
802 /* This function decides the reponse that needs to be sent to
803 * all Participant machines after the TRANS_REQUEST protocol */
804 void decideResponse(thread_data_array_t *tdata) {
806 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
809 for (i = 0 ; i < tdata->buffer->f.mcount; i++) {
810 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
811 written onto the shared array */
814 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
815 /* treat as disagree, pass thru */
824 case TRANS_SOFT_ABORT:
830 if(transdisagree > 0) {
832 *(tdata->replyctrl) = TRANS_ABORT;
833 *(tdata->replyretry) = 0;
834 /* clear objects from prefetch cache */
836 } else if(transagree == tdata->buffer->f.mcount){
838 *(tdata->replyctrl) = TRANS_COMMIT;
839 *(tdata->replyretry) = 0;
841 /* Send Abort in soft abort case followed by retry commiting transaction again*/
842 *(tdata->replyctrl) = TRANS_ABORT;
843 *(tdata->replyretry) = 1;
848 /* This function sends the final response to remote machines per
849 * thread in their respective socket id It returns a char that is only
850 * needed to check the correctness of execution of this function
851 * inside transRequest()*/
853 char sendResponse(thread_data_array_t *tdata, int sd) {
854 int n, size, sum, oidcount = 0, control;
855 char *ptr, retval = 0;
856 unsigned int *oidnotfound;
858 control = *(tdata->replyctrl);
859 send_data(sd, &control, sizeof(char));
861 //TODO read missing objects during object migration
862 /* If response is a soft abort due to missing objects at the
863 Participant's side */
865 /* If the decided response is TRANS_ABORT */
866 if(*(tdata->replyctrl) == TRANS_ABORT) {
867 retval = TRANS_ABORT;
868 } else if(*(tdata->replyctrl) == TRANS_COMMIT) {
869 /* If the decided response is TRANS_COMMIT */
870 retval = TRANS_COMMIT;
876 /* This function opens a connection, places an object read request to
877 * the remote machine, reads the control message and object if
878 * available and copies the object and its header to the local
881 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
883 struct sockaddr_in serv_addr;
887 void *objcopy = NULL;
889 int sd = getSock2(transReadSockPool, mnum);
890 char readrequest[sizeof(char)+sizeof(unsigned int)];
891 readrequest[0] = READ_REQUEST;
892 *((unsigned int *)(&readrequest[1])) = oid;
893 send_data(sd, readrequest, sizeof(readrequest));
895 /* Read response from the Participant */
896 recv_data(sd, &control, sizeof(char));
898 if (control==OBJECT_NOT_FOUND) {
901 /* Read object if found into local cache */
902 recv_data(sd, &size, sizeof(int));
903 objcopy = objstrAlloc(record->cache, size);
904 recv_data(sd, objcopy, size);
905 /* Insert into cache's lookup table */
906 chashInsert(record->lookupTable, oid, objcopy);
912 /* This function handles the local objects involved in a transaction
913 * commiting process. It also makes a decision if this local machine
914 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator. Note
915 * Coordinator = local machine It wakes up the other threads from
916 * remote participants that are waiting for the coordinator's decision
917 * and based on common agreement it either commits or aborts the
918 * transaction. It also frees the memory resources */
920 void *handleLocalReq(void *threadarg) {
921 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
922 local_thread_data_array_t *localtdata;
923 int numoidnotfound = 0, numoidlocked = 0;
924 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
927 unsigned short version;
929 objheader_t *headptr;
931 localtdata = (local_thread_data_array_t *) threadarg;
933 /* Counters and arrays to formulate decision on control message to be sent */
934 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
935 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
937 numread = localtdata->tdata->buffer->f.numread;
938 /* Process each oid in the machine pile/ group per thread */
939 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
940 if (i < localtdata->tdata->buffer->f.numread) {
941 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
943 oid = *((unsigned int *)(((char *)localtdata->tdata->buffer->objread) + incr));
944 version = *((unsigned short *)(((char *)localtdata->tdata->buffer->objread) + incr + sizeof(unsigned int)));
945 } else { // Objects Modified
947 headptr = (objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i-numread]);
948 if (headptr == NULL) {
949 printf("Error: handleLocalReq() returning NULL %s, %d\n", __FILE__, __LINE__);
953 version = headptr->version;
955 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
957 /* Save the oids not found and number of oids not found for later use */
958 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
959 /* Save the oids not found and number of oids not found for later use */
960 oidnotfound[numoidnotfound] = oid;
962 } else { /* If Obj found in machine (i.e. has not moved) */
963 /* Check if Obj is locked by any previous transaction */
964 if (test_and_set(STATUSPTR(mobj))) {
965 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
967 } else {/* If versions don't match ...HARD ABORT */
969 /* Send TRANS_DISAGREE to Coordinator */
970 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
973 char b[] = "version mismatch";
974 char c[] = "object type";
975 TABORT7(__func__, b, a, c, localtdata->tdata->mid, TYPE(mobj));
981 /* Save all object oids that are locked on this machine during this transaction request call */
982 oidlocked[numoidlocked] = OID(((objheader_t *)mobj));
984 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
986 } else { /* If versions don't match ...HARD ABORT */
988 /* Send TRANS_DISAGREE to Coordinator */
989 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
992 char b[] = "version mismatch";
993 char c[] = "object type";
994 TABORT7(__func__, b, a, c, localtdata->tdata->mid, TYPE(mobj));
1001 /* Condition to send TRANS_AGREE */
1002 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
1003 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
1005 /* Condition to send TRANS_SOFT_ABORT */
1006 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
1009 char b[] = "version mismatch";
1010 char c[] = "object type";
1011 TABORT7(__func__, b, a, c, localtdata->tdata->mid, TYPE(mobj));
1013 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
1016 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
1017 * if Participant receives a TRANS_COMMIT */
1018 localtdata->transinfo->objlocked = oidlocked;
1019 localtdata->transinfo->objnotfound = oidnotfound;
1020 localtdata->transinfo->modptr = NULL;
1021 localtdata->transinfo->numlocked = numoidlocked;
1022 localtdata->transinfo->numnotfound = numoidnotfound;
1023 /* Lock and update count */
1024 //Thread sleeps until all messages from pariticipants are received by coordinator
1025 pthread_mutex_lock(localtdata->tdata->lock);
1026 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
1028 /* Wake up the threads and invoke decideResponse (once) */
1029 if(*(localtdata->tdata->count) == localtdata->tdata->buffer->f.mcount) {
1030 decideResponse(localtdata->tdata);
1031 pthread_cond_broadcast(localtdata->tdata->threshold);
1033 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
1035 pthread_mutex_unlock(localtdata->tdata->lock);
1036 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
1037 if(transAbortProcess(localtdata) != 0) {
1038 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
1042 } else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT) {
1043 if(transComProcess(localtdata) != 0) {
1044 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1050 if (localtdata->transinfo->objlocked != NULL) {
1051 free(localtdata->transinfo->objlocked);
1053 if (localtdata->transinfo->objnotfound != NULL) {
1054 free(localtdata->transinfo->objnotfound);
1059 /* This function completes the ABORT process if the transaction is aborting */
1060 int transAbortProcess(local_thread_data_array_t *localtdata) {
1062 unsigned int *objlocked;
1065 numlocked = localtdata->transinfo->numlocked;
1066 objlocked = localtdata->transinfo->objlocked;
1068 for (i = 0; i < numlocked; i++) {
1069 if((header = mhashSearch(objlocked[i])) == NULL) {
1070 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1073 UnLock(STATUSPTR(header));
1079 /*This function completes the COMMIT process if the transaction is commiting*/
1080 int transComProcess(local_thread_data_array_t *localtdata) {
1081 objheader_t *header, *tcptr;
1082 int i, nummod, tmpsize, numcreated, numlocked;
1083 unsigned int *oidmod, *oidcreated, *oidlocked;
1086 nummod = localtdata->tdata->buffer->f.nummod;
1087 oidmod = localtdata->tdata->buffer->oidmod;
1088 numcreated = localtdata->tdata->buffer->f.numcreated;
1089 oidcreated = localtdata->tdata->buffer->oidcreated;
1090 numlocked = localtdata->transinfo->numlocked;
1091 oidlocked = localtdata->transinfo->objlocked;
1093 for (i = 0; i < nummod; i++) {
1094 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1095 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1098 /* Copy from transaction cache -> main object store */
1099 if ((tcptr = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidmod[i]))) == NULL) {
1100 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1103 GETSIZE(tmpsize, header);
1104 char *tmptcptr = (char *) tcptr;
1105 memcpy((char*)header+sizeof(objheader_t), (char *)tmptcptr+ sizeof(objheader_t), tmpsize);
1106 header->version += 1;
1107 if(header->notifylist != NULL) {
1108 notifyAll(&header->notifylist, OID(header), header->version);
1111 /* If object is newly created inside transaction then commit it */
1112 for (i = 0; i < numcreated; i++) {
1113 if ((header = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidcreated[i]))) == NULL) {
1114 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1117 GETSIZE(tmpsize, header);
1118 tmpsize += sizeof(objheader_t);
1119 pthread_mutex_lock(&mainobjstore_mutex);
1120 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1121 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1122 pthread_mutex_unlock(&mainobjstore_mutex);
1125 pthread_mutex_unlock(&mainobjstore_mutex);
1126 memcpy(ptrcreate, header, tmpsize);
1127 mhashInsert(oidcreated[i], ptrcreate);
1128 lhashInsert(oidcreated[i], myIpAddr);
1130 /* Unlock locked objects */
1131 for(i = 0; i < numlocked; i++) {
1132 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1133 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1136 UnLock(STATUSPTR(header));
1142 prefetchpile_t *foundLocal(char *ptr) {
1143 int siteid = *(GET_SITEID(ptr));
1144 int ntuples = *(GET_NTUPLES(ptr));
1145 unsigned int * oidarray = GET_PTR_OID(ptr);
1146 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1147 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1148 prefetchpile_t * head=NULL;
1152 for(i=0;i<ntuples; i++) {
1153 unsigned short baseindex=(i==0)?0:endoffsets[i-1];
1154 unsigned short endindex=endoffsets[i];
1155 unsigned int oid=oidarray[i];
1160 //Look up fields locally
1161 for(newbase=baseindex;newbase<endindex;newbase++) {
1162 if (!lookupObject(&oid, arryfields[newbase]))
1164 //Ended in a null pointer...
1168 //Entire prefetch is local
1169 if (newbase==endindex&&checkoid(oid)){
1173 //Add to remote requests
1174 machinenum=lhashSearch(oid);
1175 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1180 /* handle dynamic prefetching */
1181 handleDynPrefetching(numLocal, ntuples, siteid);
1185 int checkoid(unsigned int oid) {
1186 objheader_t *header;
1187 if ((header=mhashSearch(oid))!=NULL) {
1190 } else if ((header=prehashSearch(oid))!=NULL) {
1198 int lookupObject(unsigned int * oid, short offset) {
1199 objheader_t *header;
1200 if ((header=mhashSearch(*oid))!=NULL) {
1203 } else if ((header=prehashSearch(*oid))!=NULL) {
1210 if(TYPE(header) > NUMCLASSES) {
1211 int elementsize = classsize[TYPE(header)];
1212 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1213 int length = ao->___length___;
1214 /* Check if array out of bounds */
1215 if(offset < 0 || offset >= length) {
1216 //if yes treat the object as found
1220 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1223 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1229 /* This function is called by the thread calling transPrefetch */
1230 void *transPrefetch(void *t) {
1232 /* lock mutex of primary prefetch queue */
1233 void *node=gettail();
1234 /* Check if the tuples are found locally, if yes then reduce them further*/
1235 /* and group requests by remote machine ids by calling the makePreGroups() */
1236 prefetchpile_t *pilehead = foundLocal(node);
1238 if (pilehead!=NULL) {
1239 // Get sock from shared pool
1240 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1242 /* Send Prefetch Request */
1243 prefetchpile_t *ptr = pilehead;
1244 while(ptr != NULL) {
1245 sendPrefetchReq(ptr, sd);
1249 /* Release socket */
1250 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1252 /* Deallocated pilehead */
1253 mcdealloc(pilehead);
1255 // Deallocate the prefetch queue pile node
1260 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1263 int size=sizeof(char)+sizeof(int);
1264 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1265 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1270 *buf=TRANS_PREFETCH;
1273 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1274 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1277 *((unsigned int *)buf)=tmp->oid;
1278 buf+=sizeof(unsigned int);
1279 *((unsigned int *)(buf)) = myIpAddr;
1280 buf+=sizeof(unsigned int);
1281 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1282 buf+=tmp->numoffset*sizeof(short);
1285 send_data(sd, buft, size);
1289 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1294 /* Send TRANS_PREFETCH control message */
1295 control = TRANS_PREFETCH;
1296 send_data(sd, &control, sizeof(char));
1298 /* Send Oids and offsets in pairs */
1299 tmp = mcpilenode->objpiles;
1300 while(tmp != NULL) {
1301 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1302 char oidnoffset[len];
1303 char *buf=oidnoffset;
1304 *((int*)buf) = tmp->numoffset;
1306 *((unsigned int *)buf) = tmp->oid;
1307 buf+=sizeof(unsigned int);
1308 *((unsigned int *)buf) = myIpAddr;
1309 buf += sizeof(unsigned int);
1310 memcpy(buf, tmp->offset, (tmp->numoffset)*sizeof(short));
1311 send_data(sd, oidnoffset, len);
1315 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1317 send_data(sd, &endpair, sizeof(int));
1322 int getPrefetchResponse(int sd) {
1323 int length = 0, size = 0;
1326 void *modptr, *oldptr;
1328 recv_data((int)sd, &length, sizeof(int));
1329 size = length - sizeof(int);
1330 char recvbuffer[size];
1332 recv_data((int)sd, recvbuffer, size);
1333 control = *((char *) recvbuffer);
1334 if(control == OBJECT_FOUND) {
1335 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1336 size = size - (sizeof(char) + sizeof(unsigned int));
1337 pthread_mutex_lock(&prefetchcache_mutex);
1338 if ((modptr = prefetchobjstrAlloc(size)) == NULL) {
1339 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1340 pthread_mutex_unlock(&prefetchcache_mutex);
1343 pthread_mutex_unlock(&prefetchcache_mutex);
1344 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1347 /* Insert the oid and its address into the prefetch hash lookup table */
1348 /* Do a version comparison if the oid exists */
1349 if((oldptr = prehashSearch(oid)) != NULL) {
1350 /* If older version then update with new object ptr */
1351 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1353 prehashInsert(oid, modptr);
1355 } else {/* Else add the object ptr to hash table*/
1356 prehashInsert(oid, modptr);
1358 /* Lock the Prefetch Cache look up table*/
1359 pthread_mutex_lock(&pflookup.lock);
1360 /* Broadcast signal on prefetch cache condition variable */
1361 pthread_cond_broadcast(&pflookup.cond);
1362 /* Unlock the Prefetch Cache look up table*/
1363 pthread_mutex_unlock(&pflookup.lock);
1364 } else if(control == OBJECT_NOT_FOUND) {
1365 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1366 /* TODO: For each object not found query DHT for new location and retrieve the object */
1367 /* Throw an error */
1368 //printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1371 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1377 unsigned short getObjType(unsigned int oid) {
1378 objheader_t *objheader;
1379 unsigned short numoffset[] ={0};
1380 short fieldoffset[] ={};
1382 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1383 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1384 prefetch(0, 1, &oid, numoffset, fieldoffset);
1385 pthread_mutex_lock(&pflookup.lock);
1386 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1387 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1389 pthread_mutex_unlock(&pflookup.lock);
1393 return TYPE(objheader);
1396 int startRemoteThread(unsigned int oid, unsigned int mid)
1399 struct sockaddr_in remoteAddr;
1400 char msg[1 + sizeof(unsigned int)];
1404 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1406 perror("startRemoteThread():socket()");
1410 bzero(&remoteAddr, sizeof(remoteAddr));
1411 remoteAddr.sin_family = AF_INET;
1412 remoteAddr.sin_port = htons(LISTEN_PORT);
1413 remoteAddr.sin_addr.s_addr = htonl(mid);
1415 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1417 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1418 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1423 msg[0] = START_REMOTE_THREAD;
1424 *((unsigned int *) &msg[1]) = oid;
1425 send_data(sock, msg, 1 + sizeof(unsigned int));
1432 //TODO: when reusing oids, make sure they are not already in use!
1433 static unsigned int id = 0xFFFFFFFF;
1434 unsigned int getNewOID(void) {
1436 if (id > oidMax || id < oidMin)
1443 int processConfigFile()
1446 const int maxLineLength = 200;
1447 char lineBuffer[maxLineLength];
1449 const char *delimiters = " \t\n";
1453 configFile = fopen(CONFIG_FILENAME, "r");
1454 if (configFile == NULL)
1456 printf("error opening %s:\n", CONFIG_FILENAME);
1461 numHostsInSystem = 0;
1462 sizeOfHostArray = 8;
1463 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1465 while(fgets(lineBuffer, maxLineLength, configFile) != NULL)
1467 commentBegin = strchr(lineBuffer, '#');
1468 if (commentBegin != NULL)
1469 *commentBegin = '\0';
1470 token = strtok(lineBuffer, delimiters);
1471 while (token != NULL)
1473 tmpAddr = inet_addr(token);
1474 if ((int)tmpAddr == -1)
1476 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1481 addHost(htonl(tmpAddr));
1482 token = strtok(NULL, delimiters);
1488 if (numHostsInSystem < 1)
1490 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1494 myIpAddr = getMyIpAddr("en1");
1496 myIpAddr = getMyIpAddr("eth0");
1500 printf("My ip address = %x", myIpAddr);
1502 myIndexInHostArray = findHost(myIpAddr);
1503 if (myIndexInHostArray == -1)
1505 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1508 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1509 oidMin = oidsPerBlock * myIndexInHostArray;
1510 if (myIndexInHostArray == numHostsInSystem - 1)
1511 oidMax = 0xFFFFFFFF;
1513 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1518 void addHost(unsigned int hostIp)
1520 unsigned int *tmpArray;
1522 if (findHost(hostIp) != -1)
1525 if (numHostsInSystem == sizeOfHostArray)
1527 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1528 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1530 hostIpAddrs = tmpArray;
1533 hostIpAddrs[numHostsInSystem++] = hostIp;
1538 int findHost(unsigned int hostIp)
1541 for (i = 0; i < numHostsInSystem; i++)
1542 if (hostIpAddrs[i] == hostIp)
1549 /* This function sends notification request per thread waiting on object(s) whose version
1551 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1553 objheader_t *objheader;
1554 struct sockaddr_in remoteAddr;
1555 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1559 unsigned short version;
1560 unsigned int oid,mid;
1561 static unsigned int threadid = 0;
1562 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1563 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1564 notifydata_t *ndata;
1567 if((mid = lhashSearch(oid)) == 0) {
1568 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1572 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1573 perror("reqNotify():socket()");
1577 bzero(&remoteAddr, sizeof(remoteAddr));
1578 remoteAddr.sin_family = AF_INET;
1579 remoteAddr.sin_port = htons(LISTEN_PORT);
1580 remoteAddr.sin_addr.s_addr = htonl(mid);
1582 /* Generate unique threadid */
1585 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1586 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1587 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1590 ndata->numoid = numoid;
1591 ndata->threadid = threadid;
1592 ndata->oidarry = oidarry;
1593 ndata->versionarry = versionarry;
1594 ndata->threadcond = threadcond;
1595 ndata->threadnotify = threadnotify;
1596 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1597 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1602 /* Send number of oids, oidarry, version array, machine id and threadid */
1603 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1604 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1605 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1609 msg[0] = THREAD_NOTIFY_REQUEST;
1610 *((unsigned int *)(&msg[1])) = numoid;
1611 /* Send array of oids */
1612 size = sizeof(unsigned int);
1617 *((unsigned int *)(&msg[1] + size)) = oid;
1618 size += sizeof(unsigned int);
1623 /* Send array of version */
1627 version = versionarry[i];
1628 *((unsigned short *)(&msg[1] + size)) = version;
1629 size += sizeof(unsigned short);
1634 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1635 size += sizeof(unsigned int);
1636 *((unsigned int *)(&msg[1] + size)) = threadid;
1637 pthread_mutex_lock(&(ndata->threadnotify));
1638 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1639 send_data(sock, msg, size);
1640 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1641 pthread_mutex_unlock(&(ndata->threadnotify));
1644 pthread_cond_destroy(&threadcond);
1645 pthread_mutex_destroy(&threadnotify);
1651 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1652 notifydata_t *ndata;
1653 int i, objIsFound = 0, index;
1656 //Look up the tid and call the corresponding pthread_cond_signal
1657 if((ndata = notifyhashSearch(tid)) == NULL) {
1658 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1661 for(i = 0; i < ndata->numoid; i++) {
1662 if(ndata->oidarry[i] == oid){
1667 if(objIsFound == 0){
1668 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1671 if(version <= ndata->versionarry[index]){
1672 printf("threadNotify(): New version %d has not changed since last version %s, %d\n", version, __FILE__, __LINE__);
1675 /* Clear from prefetch cache and free thread related data structure */
1676 if((ptr = prehashSearch(oid)) != NULL) {
1679 pthread_cond_signal(&(ndata->threadcond));
1686 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1689 struct sockaddr_in remoteAddr;
1690 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1691 int sock, status, size, bytesSent;
1693 while(*head != NULL) {
1696 //create a socket connection to that machine
1697 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1698 perror("notifyAll():socket()");
1702 bzero(&remoteAddr, sizeof(remoteAddr));
1703 remoteAddr.sin_family = AF_INET;
1704 remoteAddr.sin_port = htons(LISTEN_PORT);
1705 remoteAddr.sin_addr.s_addr = htonl(mid);
1706 //send Thread Notify response and threadid to that machine
1707 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1708 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1709 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1713 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1714 msg[0] = THREAD_NOTIFY_RESPONSE;
1715 *((unsigned int *)&msg[1]) = oid;
1716 size = sizeof(unsigned int);
1717 *((unsigned short *)(&msg[1]+ size)) = version;
1718 size+= sizeof(unsigned short);
1719 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1721 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1722 send_data(sock, msg, size);
1733 void transAbort(transrecord_t *trans) {
1734 objstrDelete(trans->cache);
1735 chashDelete(trans->lookupTable);
1739 /* This function inserts necessary information into
1740 * a machine pile data structure */
1741 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
1742 plistnode_t *ptr, *tmp;
1743 int found = 0, offset = 0;
1746 //Add oid into a machine that is already present in the pile linked list structure
1747 while(tmp != NULL) {
1748 if (tmp->mid == mid) {
1751 if (STATUS(headeraddr) & NEW) {
1752 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
1754 GETSIZE(tmpsize, headeraddr);
1755 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1756 }else if (STATUS(headeraddr) & DIRTY) {
1757 tmp->oidmod[tmp->nummod] = OID(headeraddr);
1759 GETSIZE(tmpsize, headeraddr);
1760 tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
1762 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
1763 *((unsigned int *)(((char *)tmp->objread) + offset))=OID(headeraddr);
1764 offset += sizeof(unsigned int);
1765 *((short *)(((char *)tmp->objread) + offset)) = headeraddr->version;
1773 //Add oid for any new machine
1776 if((ptr = pCreate(num_objs)) == NULL) {
1780 if (STATUS(headeraddr) & NEW) {
1781 ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
1783 GETSIZE(tmpsize, headeraddr);
1784 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1785 } else if (STATUS(headeraddr) & DIRTY) {
1786 ptr->oidmod[ptr->nummod] = OID(headeraddr);
1788 GETSIZE(tmpsize, headeraddr);
1789 ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
1791 *((unsigned int *)ptr->objread)=OID(headeraddr);
1792 offset = sizeof(unsigned int);
1793 *((short *)(((char *)ptr->objread) + offset)) = headeraddr->version;
1801 STATUS(headeraddr) =0;