4 #include "machinepile.h"
9 #include "threadnotify.h"
16 #define PREFETCH_CACHE_SIZE 1048576 //1MB
17 #define CONFIG_FILENAME "dstm.conf"
19 /* Global Variables */
20 extern int classsize[];
21 objstr_t *prefetchcache; //Global Prefetch cache
22 pthread_mutex_t prefetchcache_mutex;// Mutex to lock Prefetch Cache
23 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
24 extern pthread_mutex_t mainobjstore_mutex;// Mutex to lock main Object store
25 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
26 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
27 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
28 extern objstr_t *mainobjstore;
29 unsigned int myIpAddr;
30 unsigned int *hostIpAddrs;
33 int myIndexInHostArray;
34 unsigned int oidsPerBlock;
38 sockPoolHashTable_t *transReadSockPool;
39 sockPoolHashTable_t *transPrefetchSockPool;
40 pthread_mutex_t notifymutex;
41 pthread_mutex_t atomicObjLock;
43 void printhex(unsigned char *, int);
44 plistnode_t *createPiles(transrecord_t *);
46 /*******************************
47 * Send and Recv function calls
48 *******************************/
49 void send_data(int fd , void *buf, int buflen) {
50 char *buffer = (char *)(buf);
54 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
64 void recv_data(int fd , void *buf, int buflen) {
65 char *buffer = (char *)(buf);
69 numbytes = recv(fd, buffer, size, 0);
79 int recv_data_errorcode(int fd , void *buf, int buflen) {
80 char *buffer = (char *)(buf);
84 numbytes = recv(fd, buffer, size, 0);
96 void printhex(unsigned char *ptr, int numBytes) {
98 for (i = 0; i < numBytes; i++) {
100 printf("0%x ", ptr[i]);
102 printf("%x ", ptr[i]);
108 inline int arrayLength(int *array) {
110 for(i=0 ;array[i] != -1; i++)
115 inline int findmax(int *array, int arraylength) {
118 for(i = 0; i < arraylength; i++){
126 /* This function is a prefetch call generated by the compiler that
127 * populates the shared primary prefetch queue*/
128 void prefetch(int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
129 /* Allocate for the queue node*/
130 int qnodesize = sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
131 char * node= getmemory(qnodesize);
132 /* Set queue node values */
134 int top=endoffsets[ntuples-1];
136 *((int *)(node))=ntuples;
138 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
139 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
140 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
142 /* Lock and insert into primary prefetch queue */
146 /* This function starts up the transaction runtime. */
147 int dstmStartup(const char * option) {
148 pthread_t thread_Listen;
150 int master=option!=NULL && strcmp(option, "master")==0;
153 if (processConfigFile() != 0)
154 return 0; //TODO: return error value, cause main program to exit
160 //Initialize socket pool
161 transReadSockPool = createSockPool(transReadSockPool, 2*numHostsInSystem+1);
162 transPrefetchSockPool = createSockPool(transPrefetchSockPool, 2*numHostsInSystem+1);
169 pthread_attr_init(&attr);
170 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
171 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
174 dstmListen((void *)fd);
179 //TODO Use this later
180 void *pCacheAlloc(objstr_t *store, unsigned int size) {
186 while(ptr->next != NULL) {
187 /* check if store is empty */
188 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
203 /* This function initiates the prefetch thread A queue is shared
204 * between the main thread of execution and the prefetch thread to
205 * process the prefetch call Call from compiler populates the shared
206 * queue with prefetch requests while prefetch thread processes the
207 * prefetch requests */
212 //Create and initialize prefetch cache structure
213 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
215 /* Initialize attributes for mutex */
216 pthread_mutexattr_init(&prefetchcache_mutex_attr);
217 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
219 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
221 pthread_mutex_init(¬ifymutex, NULL);
222 pthread_mutex_init(&atomicObjLock, NULL);
223 //Create prefetch cache lookup table
224 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
229 //Initialize primary shared queue
231 //Initialize machine pile w/prefetch oids and offsets shared queue
234 //Create the primary prefetch thread
236 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
238 pthread_detach(tPrefetch);
241 /* This function stops the threads spawned */
244 pthread_cancel(tPrefetch);
245 for(t = 0; t < NUM_THREADS; t++)
246 pthread_cancel(wthreads[t]);
251 /* This functions inserts randowm wait delays in the order of msec
252 * Mostly used when transaction commits retry*/
259 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
260 nanosleep(&req, NULL);
264 /* This function initializes things required in the transaction start*/
265 transrecord_t *transStart() {
267 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL){
268 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
271 tmp->cache = objstrCreate(1048576);
272 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
274 tmp->revertlist=NULL;
279 /* This function finds the location of the objects involved in a transaction
280 * and returns the pointer to the object if found in a remote location */
281 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
282 unsigned int machinenumber;
283 objheader_t *tmp, *objheader;
284 objheader_t *objcopy;
292 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
293 /* Search local transaction cache */
295 return &objheader[1];
299 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
300 /* Look up in machine lookup table and copy into cache*/
301 GETSIZE(size, objheader);
302 size += sizeof(objheader_t);
303 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
304 memcpy(objcopy, objheader, size);
305 /* Insert into cache's lookup table */
307 chashInsert(record->lookupTable, OID(objheader), objcopy);
313 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
314 /* Look up in prefetch cache */
316 size+=sizeof(objheader_t);
317 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
318 memcpy(objcopy, tmp, size);
319 /* Insert into cache's lookup table */
320 chashInsert(record->lookupTable, OID(tmp), objcopy);
327 /* Get the object from the remote location */
328 if((machinenumber = lhashSearch(oid)) == 0) {
329 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
332 objcopy = getRemoteObj(record, machinenumber, oid);
334 if(objcopy == NULL) {
335 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
348 /* This function creates objects in the transaction record */
349 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
350 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
351 OID(tmp) = getNewOID();
355 chashInsert(record->lookupTable, OID(tmp), tmp);
358 return &tmp[1]; //want space after object header
364 /* This function creates machine piles based on all machines involved in a
365 * transaction commit request */
366 plistnode_t *createPiles(transrecord_t *record) {
368 plistnode_t *pile = NULL;
369 unsigned int machinenum;
370 objheader_t *headeraddr;
371 chashlistnode_t * ptr = record->lookupTable->table;
372 /* Represents number of bins in the chash table */
373 unsigned int size = record->lookupTable->size;
375 for(i = 0; i < size ; i++) {
376 chashlistnode_t * curr = &ptr[i];
377 /* Inner loop to traverse the linked list of the cache lookupTable */
378 while(curr != NULL) {
379 //if the first bin in hash table is empty
383 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
384 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
388 //Get machine location for object id (and whether local or not)
389 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
390 machinenum = myIpAddr;
391 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
392 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
396 //Make machine groups
397 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
404 /* This function initiates the transaction commit process
405 * Spawns threads for each of the new connections with Participants
406 * and creates new piles by calling the createPiles(),
407 * Sends a transrequest() to each remote machines for objects found remotely
408 * and calls handleLocalReq() to process objects found locally */
409 int transCommit(transrecord_t *record) {
410 unsigned int tot_bytes_mod, *listmid;
411 plistnode_t *pile, *pile_ptr;
413 int pilecount, offset, threadnum, trecvcount;
415 char transid[TID_LEN];
416 trans_req_data_t *tosend;
417 trans_commit_data_t transinfo;
418 static int newtid = 0;
419 char treplyctrl, treplyretry; /* keeps track of the common response that needs to be sent */
420 thread_data_array_t *thread_data_array;
421 local_thread_data_array_t *ltdata;
429 thread_data_array = NULL;
432 /* Look through all the objects in the transaction record and make piles
433 * for each machine involved in the transaction*/
435 pile_ptr = pile = createPiles(record);
440 /* Create the packet to be sent in TRANS_REQUEST */
442 /* Count the number of participants */
443 pilecount = pCount(pile);
445 /* Create a list of machine ids(Participants) involved in transaction */
446 listmid = calloc(pilecount, sizeof(unsigned int));
447 pListMid(pile, listmid);
450 /* Initialize thread variables,
451 * Spawn a thread for each Participant involved in a transaction */
452 pthread_t thread[pilecount];
454 pthread_cond_t tcond;
455 pthread_mutex_t tlock;
456 pthread_mutex_t tlshrd;
458 thread_data_array = (thread_data_array_t *) calloc(pilecount, sizeof(thread_data_array_t));
460 ltdata = calloc(1, sizeof(local_thread_data_array_t));
462 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
464 /* Initialize and set thread detach attribute */
465 pthread_attr_init(&attr);
466 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
467 pthread_mutex_init(&tlock, NULL);
468 pthread_cond_init(&tcond, NULL);
470 /* Process each machine pile */
471 while(pile != NULL) {
472 //Create transaction id
474 tosend = calloc(1, sizeof(trans_req_data_t));
475 tosend->f.control = TRANS_REQUEST;
476 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
477 tosend->f.mcount = pilecount;
478 tosend->f.numread = pile->numread;
479 tosend->f.nummod = pile->nummod;
480 tosend->f.numcreated = pile->numcreated;
481 tosend->f.sum_bytes = pile->sum_bytes;
482 tosend->listmid = listmid;
483 tosend->objread = pile->objread;
484 tosend->oidmod = pile->oidmod;
485 tosend->oidcreated = pile->oidcreated;
486 thread_data_array[threadnum].thread_id = threadnum;
487 thread_data_array[threadnum].mid = pile->mid;
488 thread_data_array[threadnum].buffer = tosend;
489 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
490 thread_data_array[threadnum].threshold = &tcond;
491 thread_data_array[threadnum].lock = &tlock;
492 thread_data_array[threadnum].count = &trecvcount;
493 thread_data_array[threadnum].replyctrl = &treplyctrl;
494 thread_data_array[threadnum].replyretry = &treplyretry;
495 thread_data_array[threadnum].rec = record;
496 /* If local do not create any extra connection */
497 if(pile->mid != myIpAddr) { /* Not local */
499 rc = pthread_create(&thread[threadnum], &attr, transRequest, (void *) &thread_data_array[threadnum]);
502 perror("Error in pthread create\n");
503 pthread_cond_destroy(&tcond);
504 pthread_mutex_destroy(&tlock);
507 for (i = 0; i < threadnum; i++)
508 free(thread_data_array[i].buffer);
509 free(thread_data_array);
514 ltdata->tdata = &thread_data_array[threadnum];
515 ltdata->transinfo = &transinfo;
517 val = pthread_create(&thread[threadnum], &attr, handleLocalReq, (void *) ltdata);
520 perror("Error in pthread create\n");
521 pthread_cond_destroy(&tcond);
522 pthread_mutex_destroy(&tlock);
525 for (i = 0; i < threadnum; i++)
526 free(thread_data_array[i].buffer);
527 free(thread_data_array);
536 /* Free attribute and wait for the other threads */
537 pthread_attr_destroy(&attr);
539 for (i = 0; i < threadnum; i++) {
540 rc = pthread_join(thread[i], NULL);
543 printf("Error: return code from pthread_join() is %d\n", rc);
544 pthread_cond_destroy(&tcond);
545 pthread_mutex_destroy(&tlock);
548 for (j = i; j < threadnum; j++) {
549 free(thread_data_array[j].buffer);
553 free(thread_data_array[i].buffer);
557 pthread_cond_destroy(&tcond);
558 pthread_mutex_destroy(&tlock);
564 /* wait a random amount of time before retrying to commit transaction*/
566 free(thread_data_array);
571 /* Retry trans commit procedure during soft_abort case */
572 } while (treplyretry);
574 if(treplyctrl == TRANS_ABORT) {
576 objstrDelete(record->cache);
577 chashDelete(record->lookupTable);
579 free(thread_data_array);
582 } else if(treplyctrl == TRANS_COMMIT) {
584 objstrDelete(record->cache);
585 chashDelete(record->lookupTable);
587 free(thread_data_array);
591 //TODO Add other cases
592 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
598 /* This function sends information involved in the transaction request
599 * to participants and accepts a response from particpants.
600 * It calls decideresponse() to decide on what control message
601 * to send next to participants and sends the message using sendResponse()*/
602 void *transRequest(void *threadarg) {
604 struct sockaddr_in serv_addr;
605 thread_data_array_t *tdata;
606 objheader_t *headeraddr;
607 char control, recvcontrol;
608 char machineip[16], retval;
610 tdata = (thread_data_array_t *) threadarg;
612 /* Send Trans Request */
613 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
614 perror("Error in socket for TRANS_REQUEST\n");
617 bzero((char*) &serv_addr, sizeof(serv_addr));
618 serv_addr.sin_family = AF_INET;
619 serv_addr.sin_port = htons(LISTEN_PORT);
620 midtoIP(tdata->mid,machineip);
621 machineip[15] = '\0';
622 serv_addr.sin_addr.s_addr = inet_addr(machineip);
623 /* Open Connection */
624 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
625 perror("Error in connect for TRANS_REQUEST\n");
630 /* Send bytes of data with TRANS_REQUEST control message */
631 send_data(sd, &(tdata->buffer->f), sizeof(fixed_data_t));
633 /* Send list of machines involved in the transaction */
635 int size=sizeof(unsigned int)*tdata->buffer->f.mcount;
636 send_data(sd, tdata->buffer->listmid, size);
639 /* Send oids and version number tuples for objects that are read */
641 int size=(sizeof(unsigned int)+sizeof(unsigned short))*tdata->buffer->f.numread;
642 send_data(sd, tdata->buffer->objread, size);
645 /* Send objects that are modified */
646 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
648 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
649 GETSIZE(size,headeraddr);
650 size+=sizeof(objheader_t);
651 send_data(sd, headeraddr, size);
654 /* Read control message from Participant */
655 recv_data(sd, &control, sizeof(char));
656 /* Recv Objects if participant sends TRANS_DISAGREE */
657 if(control == TRANS_DISAGREE) {
659 recv_data(sd, &length, sizeof(int));
661 pthread_mutex_lock(&prefetchcache_mutex);
662 if ((newAddr = objstrAlloc(prefetchcache, length)) == NULL) {
663 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
667 pthread_mutex_unlock(&prefetchcache_mutex);
668 recv_data(sd, newAddr, length);
671 unsigned int oidToPrefetch;
672 objheader_t * header;
673 header = (objheader_t *) (((char *)newAddr) + offset);
674 oidToPrefetch = OID(header);
676 GETSIZE(size, header);
677 size += sizeof(objheader_t);
678 //make an entry in prefetch hash table
679 prehashInsert(oidToPrefetch, header);
680 length = length - size;
684 recvcontrol = control;
685 /* Update common data structure and increment count */
686 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
688 /* Lock and update count */
689 /* Thread sleeps until all messages from pariticipants are received by coordinator */
690 pthread_mutex_lock(tdata->lock);
692 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
694 /* Wake up the threads and invoke decideResponse (once) */
695 if(*(tdata->count) == tdata->buffer->f.mcount) {
696 decideResponse(tdata);
697 pthread_cond_broadcast(tdata->threshold);
699 pthread_cond_wait(tdata->threshold, tdata->lock);
701 pthread_mutex_unlock(tdata->lock);
703 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT
704 * to all participants in their respective socket */
705 if (sendResponse(tdata, sd) == 0) {
706 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
711 recv_data((int)sd, &control, sizeof(char));
713 if(control == TRANS_UNSUCESSFUL) {
714 //printf("DEBUG-> TRANS_ABORTED\n");
715 } else if(control == TRANS_SUCESSFUL) {
716 //printf("DEBUG-> TRANS_SUCCESSFUL\n");
718 //printf("DEBUG-> Error: Incorrect Transaction End Message %d\n", control);
721 /* Close connection */
726 /* This function decides the reponse that needs to be sent to
727 * all Participant machines after the TRANS_REQUEST protocol */
728 void decideResponse(thread_data_array_t *tdata) {
730 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
733 for (i = 0 ; i < tdata->buffer->f.mcount; i++) {
734 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
735 written onto the shared array */
738 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
739 /* treat as disagree, pass thru */
748 case TRANS_SOFT_ABORT:
754 if(transdisagree > 0) {
756 *(tdata->replyctrl) = TRANS_ABORT;
757 *(tdata->replyretry) = 0;
758 /* clear objects from prefetch cache */
759 for (i = 0; i < tdata->buffer->f.numread; i++) {
760 prehashRemove(*((unsigned int *)(((char *)tdata->buffer->objread) + (sizeof(unsigned int) + sizeof(unsigned short))*i)));
762 for (i = 0; i < tdata->buffer->f.nummod; i++) {
763 prehashRemove(tdata->buffer->oidmod[i]);
765 } else if(transagree == tdata->buffer->f.mcount){
767 *(tdata->replyctrl) = TRANS_COMMIT;
768 *(tdata->replyretry) = 0;
769 /* update prefetch cache */
770 /* For objects read */
774 if((retval = updatePrefetchCache(tdata, tdata->buffer->f.numread, oidType)) != 0) {
775 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
779 if((retval = updatePrefetchCache(tdata, tdata->buffer->f.nummod, oidType)) != 0) {
780 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
784 /* Send Abort in soft abort case followed by retry commiting transaction again*/
785 *(tdata->replyctrl) = TRANS_ABORT;
786 *(tdata->replyretry) = 1;
791 /* This function updates the prefetch cache when commiting objects
792 * based on the type of oid i.e. if oid is read or oid is modified
793 * Return -1 on error else returns 0
795 int updatePrefetchCache(thread_data_array_t* tdata, int numoid, char oidType) {
797 for (i = 0; i < numoid; i++) {
798 //find address object
799 objheader_t *header, *newAddr;
803 oid = *((unsigned int *)(((char *)tdata->buffer->objread) + (sizeof(unsigned int) + sizeof(unsigned short))*i));
805 oid = tdata->buffer->oidmod[i];
807 header = (objheader_t *) chashSearch(tdata->rec->lookupTable, oid);
808 //copy object into prefetch cache
809 GETSIZE(size, header);
810 pthread_mutex_lock(&prefetchcache_mutex);
811 if ((newAddr = objstrAlloc(prefetchcache, (size + sizeof(objheader_t)))) == NULL) {
812 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
815 pthread_mutex_unlock(&prefetchcache_mutex);
816 memcpy(newAddr, header, (size + sizeof(objheader_t)));
817 //make an entry in prefetch hash table
818 prehashInsert(oid, newAddr);
823 /* This function sends the final response to remote machines per
824 * thread in their respective socket id It returns a char that is only
825 * needed to check the correctness of execution of this function
826 * inside transRequest()*/
828 char sendResponse(thread_data_array_t *tdata, int sd) {
829 int n, size, sum, oidcount = 0, control;
830 char *ptr, retval = 0;
831 unsigned int *oidnotfound;
833 control = *(tdata->replyctrl);
834 send_data(sd, &control, sizeof(char));
836 //TODO read missing objects during object migration
837 /* If response is a soft abort due to missing objects at the
838 Participant's side */
840 /* If the decided response is TRANS_ABORT */
841 if(*(tdata->replyctrl) == TRANS_ABORT) {
842 retval = TRANS_ABORT;
843 } else if(*(tdata->replyctrl) == TRANS_COMMIT) {
844 /* If the decided response is TRANS_COMMIT */
845 retval = TRANS_COMMIT;
851 /* This function opens a connection, places an object read request to
852 * the remote machine, reads the control message and object if
853 * available and copies the object and its header to the local
856 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
858 struct sockaddr_in serv_addr;
862 void *objcopy = NULL;
864 int sd = getSock2(transReadSockPool, mnum);
865 char readrequest[sizeof(char)+sizeof(unsigned int)];
866 readrequest[0] = READ_REQUEST;
867 *((unsigned int *)(&readrequest[1])) = oid;
868 send_data(sd, readrequest, sizeof(readrequest));
870 /* Read response from the Participant */
871 recv_data(sd, &control, sizeof(char));
873 if (control==OBJECT_NOT_FOUND) {
876 /* Read object if found into local cache */
877 recv_data(sd, &size, sizeof(int));
878 objcopy = objstrAlloc(record->cache, size);
879 recv_data(sd, objcopy, size);
880 /* Insert into cache's lookup table */
881 chashInsert(record->lookupTable, oid, objcopy);
887 /* This function handles the local objects involved in a transaction
888 * commiting process. It also makes a decision if this local machine
889 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator. Note
890 * Coordinator = local machine It wakes up the other threads from
891 * remote participants that are waiting for the coordinator's decision
892 * and based on common agreement it either commits or aborts the
893 * transaction. It also frees the memory resources */
895 void *handleLocalReq(void *threadarg) {
896 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
897 local_thread_data_array_t *localtdata;
898 int numoidnotfound = 0, numoidlocked = 0;
899 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
902 unsigned short version;
904 objheader_t *headptr;
906 localtdata = (local_thread_data_array_t *) threadarg;
908 /* Counters and arrays to formulate decision on control message to be sent */
909 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
910 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
912 numread = localtdata->tdata->buffer->f.numread;
913 /* Process each oid in the machine pile/ group per thread */
914 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
915 if (i < localtdata->tdata->buffer->f.numread) {
916 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
918 oid = *((unsigned int *)(((char *)localtdata->tdata->buffer->objread) + incr));
919 version = *((unsigned short *)(((char *)localtdata->tdata->buffer->objread) + incr + sizeof(unsigned int)));
920 } else { // Objects Modified
922 headptr = (objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i-numread]);
923 if (headptr == NULL) {
924 printf("Error: handleLocalReq() returning NULL %s, %d\n", __FILE__, __LINE__);
928 version = headptr->version;
930 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
932 /* Save the oids not found and number of oids not found for later use */
933 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
934 /* Save the oids not found and number of oids not found for later use */
935 oidnotfound[numoidnotfound] = oid;
937 } else { /* If Obj found in machine (i.e. has not moved) */
938 /* Check if Obj is locked by any previous transaction */
939 if (test_and_set(STATUSPTR(mobj))) {
940 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
942 } else {/* If versions don't match ...HARD ABORT */
944 /* Send TRANS_DISAGREE to Coordinator */
945 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
949 /* Save all object oids that are locked on this machine during this transaction request call */
950 oidlocked[numoidlocked] = OID(((objheader_t *)mobj));
952 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
954 } else { /* If versions don't match ...HARD ABORT */
956 /* Send TRANS_DISAGREE to Coordinator */
957 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
962 /* Condition to send TRANS_AGREE */
963 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
964 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
966 /* Condition to send TRANS_SOFT_ABORT */
967 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
968 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
971 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
972 * if Participant receives a TRANS_COMMIT */
973 localtdata->transinfo->objlocked = oidlocked;
974 localtdata->transinfo->objnotfound = oidnotfound;
975 localtdata->transinfo->modptr = NULL;
976 localtdata->transinfo->numlocked = numoidlocked;
977 localtdata->transinfo->numnotfound = numoidnotfound;
978 /* Lock and update count */
979 //Thread sleeps until all messages from pariticipants are received by coordinator
980 pthread_mutex_lock(localtdata->tdata->lock);
981 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
983 /* Wake up the threads and invoke decideResponse (once) */
984 if(*(localtdata->tdata->count) == localtdata->tdata->buffer->f.mcount) {
985 decideResponse(localtdata->tdata);
986 pthread_cond_broadcast(localtdata->tdata->threshold);
988 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
990 pthread_mutex_unlock(localtdata->tdata->lock);
991 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
992 if(transAbortProcess(localtdata) != 0) {
993 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
996 } else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT) {
997 if(transComProcess(localtdata) != 0) {
998 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1003 if (localtdata->transinfo->objlocked != NULL) {
1004 free(localtdata->transinfo->objlocked);
1006 if (localtdata->transinfo->objnotfound != NULL) {
1007 free(localtdata->transinfo->objnotfound);
1013 /* This function completes the ABORT process if the transaction is aborting */
1014 int transAbortProcess(local_thread_data_array_t *localtdata) {
1016 unsigned int *objlocked;
1019 numlocked = localtdata->transinfo->numlocked;
1020 objlocked = localtdata->transinfo->objlocked;
1022 for (i = 0; i < numlocked; i++) {
1023 if((header = mhashSearch(objlocked[i])) == NULL) {
1024 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1027 UnLock(STATUSPTR(header));
1033 /*This function completes the COMMIT process is the transaction is commiting*/
1034 int transComProcess(local_thread_data_array_t *localtdata) {
1035 objheader_t *header, *tcptr;
1036 int i, nummod, tmpsize, numcreated, numlocked;
1037 unsigned int *oidmod, *oidcreated, *oidlocked;
1040 nummod = localtdata->tdata->buffer->f.nummod;
1041 oidmod = localtdata->tdata->buffer->oidmod;
1042 numcreated = localtdata->tdata->buffer->f.numcreated;
1043 oidcreated = localtdata->tdata->buffer->oidcreated;
1044 numlocked = localtdata->transinfo->numlocked;
1045 oidlocked = localtdata->transinfo->objlocked;
1047 for (i = 0; i < nummod; i++) {
1048 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1049 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1052 /* Copy from transaction cache -> main object store */
1053 if ((tcptr = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidmod[i]))) == NULL) {
1054 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1057 GETSIZE(tmpsize, header);
1058 char *tmptcptr = (char *) tcptr;
1059 memcpy((char*)header+sizeof(objheader_t), (char *)tmptcptr+ sizeof(objheader_t), tmpsize);
1060 header->version += 1;
1061 if(header->notifylist != NULL) {
1062 notifyAll(&header->notifylist, OID(header), header->version);
1065 /* If object is newly created inside transaction then commit it */
1066 for (i = 0; i < numcreated; i++) {
1067 if ((header = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidcreated[i]))) == NULL) {
1068 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1071 GETSIZE(tmpsize, header);
1072 tmpsize += sizeof(objheader_t);
1073 pthread_mutex_lock(&mainobjstore_mutex);
1074 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1075 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1076 pthread_mutex_unlock(&mainobjstore_mutex);
1079 pthread_mutex_unlock(&mainobjstore_mutex);
1080 memcpy(ptrcreate, header, tmpsize);
1081 mhashInsert(oidcreated[i], ptrcreate);
1082 lhashInsert(oidcreated[i], myIpAddr);
1084 /* Unlock locked objects */
1085 for(i = 0; i < numlocked; i++) {
1086 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1087 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1090 UnLock(STATUSPTR(header));
1096 prefetchpile_t *foundLocal(char *ptr) {
1097 int ntuples = *(GET_NTUPLES(ptr));
1098 unsigned int * oidarray = GET_PTR_OID(ptr);
1099 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1100 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1101 prefetchpile_t * head=NULL;
1104 for(i=0;i<ntuples; i++) {
1105 unsigned short baseindex=(i==0)?0:endoffsets[i-1];
1106 unsigned short endindex=endoffsets[i];
1107 unsigned int oid=oidarray[i];
1112 //Look up fields locally
1113 for(newbase=baseindex;newbase<endindex;newbase++) {
1114 if (!lookupObject(&oid, arryfields[newbase]))
1116 //Ended in a null pointer...
1120 //Entire prefetch is local
1121 if (newbase==endindex&&checkoid(oid))
1123 //Add to remote requests
1124 machinenum=lhashSearch(oid);
1125 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1132 int checkoid(unsigned int oid) {
1133 objheader_t *header;
1134 if ((header=mhashSearch(oid))!=NULL) {
1137 } else if ((header=prehashSearch(oid))!=NULL) {
1145 int lookupObject(unsigned int * oid, short offset) {
1146 objheader_t *header;
1147 if ((header=mhashSearch(*oid))!=NULL) {
1150 } else if ((header=prehashSearch(*oid))!=NULL) {
1157 if(TYPE(header) > NUMCLASSES) {
1158 int elementsize = classsize[TYPE(header)];
1159 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1160 int length = ao->___length___;
1161 /* Check if array out of bounds */
1162 if(offset < 0 || offset >= length) {
1163 //if yes treat the object as found
1167 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1170 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1176 /* This function is called by the thread calling transPrefetch */
1177 void *transPrefetch(void *t) {
1179 /* lock mutex of primary prefetch queue */
1180 void *node=gettail();
1181 /* Check if the tuples are found locally, if yes then reduce them further*/
1182 /* and group requests by remote machine ids by calling the makePreGroups() */
1183 prefetchpile_t *pilehead = foundLocal(node);
1185 if (pilehead!=NULL) {
1186 // Get sock from shared pool
1187 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1189 /* Send Prefetch Request */
1190 prefetchpile_t *ptr = pilehead;
1191 while(ptr != NULL) {
1192 sendPrefetchReq(ptr, sd);
1196 /* Release socket */
1197 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1199 /* Deallocated pilehead */
1200 mcdealloc(pilehead);
1202 // Deallocate the prefetch queue pile node
1207 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1210 int size=sizeof(char)+sizeof(int);
1211 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1212 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1217 *buf=TRANS_PREFETCH;
1220 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1221 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1224 *((unsigned int *)buf)=tmp->oid;
1225 buf+=sizeof(unsigned int);
1226 *((unsigned int *)(buf)) = myIpAddr;
1227 buf+=sizeof(unsigned int);
1228 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1229 buf+=tmp->numoffset*sizeof(short);
1232 send_data(sd, buft, size);
1236 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1241 /* Send TRANS_PREFETCH control message */
1242 control = TRANS_PREFETCH;
1243 send_data(sd, &control, sizeof(char));
1245 /* Send Oids and offsets in pairs */
1246 tmp = mcpilenode->objpiles;
1247 while(tmp != NULL) {
1248 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1249 char oidnoffset[len];
1250 char *buf=oidnoffset;
1251 *((int*)buf) = tmp->numoffset;
1253 *((unsigned int *)buf) = tmp->oid;
1254 buf+=sizeof(unsigned int);
1255 *((unsigned int *)buf) = myIpAddr;
1256 buf += sizeof(unsigned int);
1257 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1258 send_data(sd, oidnoffset, len);
1262 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1264 send_data(sd, &endpair, sizeof(int));
1269 int getPrefetchResponse(int sd) {
1270 int length = 0, size = 0;
1273 void *modptr, *oldptr;
1275 recv_data((int)sd, &length, sizeof(int));
1276 size = length - sizeof(int);
1277 char recvbuffer[size];
1279 recv_data((int)sd, recvbuffer, size);
1280 control = *((char *) recvbuffer);
1281 if(control == OBJECT_FOUND) {
1282 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1283 //printf("oid %d found\n",oid);
1284 size = size - (sizeof(char) + sizeof(unsigned int));
1285 pthread_mutex_lock(&prefetchcache_mutex);
1286 if ((modptr = objstrAlloc(prefetchcache, size)) == NULL) {
1287 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1288 pthread_mutex_unlock(&prefetchcache_mutex);
1291 pthread_mutex_unlock(&prefetchcache_mutex);
1292 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1295 /* Insert the oid and its address into the prefetch hash lookup table */
1296 /* Do a version comparison if the oid exists */
1297 if((oldptr = prehashSearch(oid)) != NULL) {
1298 /* If older version then update with new object ptr */
1299 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1301 prehashInsert(oid, modptr);
1303 } else {/* Else add the object ptr to hash table*/
1304 prehashInsert(oid, modptr);
1306 /* Lock the Prefetch Cache look up table*/
1307 pthread_mutex_lock(&pflookup.lock);
1308 /* Broadcast signal on prefetch cache condition variable */
1309 pthread_cond_broadcast(&pflookup.cond);
1310 /* Unlock the Prefetch Cache look up table*/
1311 pthread_mutex_unlock(&pflookup.lock);
1312 } else if(control == OBJECT_NOT_FOUND) {
1313 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1314 /* TODO: For each object not found query DHT for new location and retrieve the object */
1315 /* Throw an error */
1316 printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1319 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1325 unsigned short getObjType(unsigned int oid) {
1326 objheader_t *objheader;
1327 unsigned short numoffset[] ={0};
1328 short fieldoffset[] ={};
1330 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1331 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1332 prefetch(1, &oid, numoffset, fieldoffset);
1333 pthread_mutex_lock(&pflookup.lock);
1334 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1335 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1337 pthread_mutex_unlock(&pflookup.lock);
1341 return TYPE(objheader);
1344 int startRemoteThread(unsigned int oid, unsigned int mid)
1347 struct sockaddr_in remoteAddr;
1348 char msg[1 + sizeof(unsigned int)];
1352 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1354 perror("startRemoteThread():socket()");
1358 bzero(&remoteAddr, sizeof(remoteAddr));
1359 remoteAddr.sin_family = AF_INET;
1360 remoteAddr.sin_port = htons(LISTEN_PORT);
1361 remoteAddr.sin_addr.s_addr = htonl(mid);
1363 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1365 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1366 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1371 msg[0] = START_REMOTE_THREAD;
1372 *((unsigned int *) &msg[1]) = oid;
1373 send_data(sock, msg, 1 + sizeof(unsigned int));
1380 //TODO: when reusing oids, make sure they are not already in use!
1381 static unsigned int id = 0xFFFFFFFF;
1382 unsigned int getNewOID(void) {
1384 if (id > oidMax || id < oidMin)
1391 int processConfigFile()
1394 const int maxLineLength = 200;
1395 char lineBuffer[maxLineLength];
1397 const char *delimiters = " \t\n";
1401 configFile = fopen(CONFIG_FILENAME, "r");
1402 if (configFile == NULL)
1404 printf("error opening %s:\n", CONFIG_FILENAME);
1409 numHostsInSystem = 0;
1410 sizeOfHostArray = 8;
1411 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1413 while(fgets(lineBuffer, maxLineLength, configFile) != NULL)
1415 commentBegin = strchr(lineBuffer, '#');
1416 if (commentBegin != NULL)
1417 *commentBegin = '\0';
1418 token = strtok(lineBuffer, delimiters);
1419 while (token != NULL)
1421 tmpAddr = inet_addr(token);
1422 if ((int)tmpAddr == -1)
1424 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1429 addHost(htonl(tmpAddr));
1430 token = strtok(NULL, delimiters);
1436 if (numHostsInSystem < 1)
1438 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1442 myIpAddr = getMyIpAddr("en1");
1444 myIpAddr = getMyIpAddr("eth0");
1446 myIndexInHostArray = findHost(myIpAddr);
1447 if (myIndexInHostArray == -1)
1449 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1452 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1453 oidMin = oidsPerBlock * myIndexInHostArray;
1454 if (myIndexInHostArray == numHostsInSystem - 1)
1455 oidMax = 0xFFFFFFFF;
1457 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1462 void addHost(unsigned int hostIp)
1464 unsigned int *tmpArray;
1466 if (findHost(hostIp) != -1)
1469 if (numHostsInSystem == sizeOfHostArray)
1471 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1472 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1474 hostIpAddrs = tmpArray;
1477 hostIpAddrs[numHostsInSystem++] = hostIp;
1482 int findHost(unsigned int hostIp)
1485 for (i = 0; i < numHostsInSystem; i++)
1486 if (hostIpAddrs[i] == hostIp)
1493 /* This function sends notification request per thread waiting on object(s) whose version
1495 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1497 objheader_t *objheader;
1498 struct sockaddr_in remoteAddr;
1499 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1503 unsigned short version;
1504 unsigned int oid,mid;
1505 static unsigned int threadid = 0;
1506 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1507 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1508 notifydata_t *ndata;
1511 if((mid = lhashSearch(oid)) == 0) {
1512 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1516 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1517 perror("reqNotify():socket()");
1521 bzero(&remoteAddr, sizeof(remoteAddr));
1522 remoteAddr.sin_family = AF_INET;
1523 remoteAddr.sin_port = htons(LISTEN_PORT);
1524 remoteAddr.sin_addr.s_addr = htonl(mid);
1526 /* Generate unique threadid */
1529 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1530 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1531 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1534 ndata->numoid = numoid;
1535 ndata->threadid = threadid;
1536 ndata->oidarry = oidarry;
1537 ndata->versionarry = versionarry;
1538 ndata->threadcond = threadcond;
1539 ndata->threadnotify = threadnotify;
1540 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1541 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1546 /* Send number of oids, oidarry, version array, machine id and threadid */
1547 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1548 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1549 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1553 msg[0] = THREAD_NOTIFY_REQUEST;
1554 *((unsigned int *)(&msg[1])) = numoid;
1555 /* Send array of oids */
1556 size = sizeof(unsigned int);
1561 *((unsigned int *)(&msg[1] + size)) = oid;
1562 size += sizeof(unsigned int);
1567 /* Send array of version */
1571 version = versionarry[i];
1572 *((unsigned short *)(&msg[1] + size)) = version;
1573 size += sizeof(unsigned short);
1578 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1579 size += sizeof(unsigned int);
1580 *((unsigned int *)(&msg[1] + size)) = threadid;
1581 pthread_mutex_lock(&(ndata->threadnotify));
1582 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1583 send_data(sock, msg, size);
1584 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1585 pthread_mutex_unlock(&(ndata->threadnotify));
1588 pthread_cond_destroy(&threadcond);
1589 pthread_mutex_destroy(&threadnotify);
1595 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1596 notifydata_t *ndata;
1597 int i, objIsFound = 0, index;
1600 //Look up the tid and call the corresponding pthread_cond_signal
1601 if((ndata = notifyhashSearch(tid)) == NULL) {
1602 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1605 for(i = 0; i < ndata->numoid; i++) {
1606 if(ndata->oidarry[i] == oid){
1611 if(objIsFound == 0){
1612 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1615 if(version <= ndata->versionarry[index]){
1616 printf("threadNotify(): New version %d has not changed since last version %s, %d\n", version, __FILE__, __LINE__);
1619 /* Clear from prefetch cache and free thread related data structure */
1620 if((ptr = prehashSearch(oid)) != NULL) {
1623 pthread_cond_signal(&(ndata->threadcond));
1630 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1633 struct sockaddr_in remoteAddr;
1634 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1635 int sock, status, size, bytesSent;
1637 while(*head != NULL) {
1640 //create a socket connection to that machine
1641 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1642 perror("notifyAll():socket()");
1646 bzero(&remoteAddr, sizeof(remoteAddr));
1647 remoteAddr.sin_family = AF_INET;
1648 remoteAddr.sin_port = htons(LISTEN_PORT);
1649 remoteAddr.sin_addr.s_addr = htonl(mid);
1650 //send Thread Notify response and threadid to that machine
1651 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1652 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1653 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1657 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1658 msg[0] = THREAD_NOTIFY_RESPONSE;
1659 *((unsigned int *)&msg[1]) = oid;
1660 size = sizeof(unsigned int);
1661 *((unsigned short *)(&msg[1]+ size)) = version;
1662 size+= sizeof(unsigned short);
1663 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1665 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1666 send_data(sock, msg, size);
1677 void transAbort(transrecord_t *trans) {
1678 objstrDelete(trans->cache);
1679 chashDelete(trans->lookupTable);