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 extern primarypfq_t pqueue; //Shared prefetch queue
22 objstr_t *prefetchcache; //Global Prefetch cache
23 pthread_mutex_t prefetchcache_mutex;// Mutex to lock Prefetch Cache
24 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
25 extern pthread_mutex_t mainobjstore_mutex;// Mutex to lock main Object store
26 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
27 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
28 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
29 extern objstr_t *mainobjstore;
30 unsigned int myIpAddr;
31 unsigned int *hostIpAddrs;
34 int myIndexInHostArray;
35 unsigned int oidsPerBlock;
39 sockPoolHashTable_t *transReadSockPool;
40 sockPoolHashTable_t *transPrefetchSockPool;
41 pthread_mutex_t notifymutex;
42 pthread_mutex_t atomicObjLock;
44 void printhex(unsigned char *, int);
45 plistnode_t *createPiles(transrecord_t *);
47 /*******************************
48 * Send and Recv function calls
49 *******************************/
50 void send_data(int fd , void *buf, int buflen) {
51 char *buffer = (char *)(buf);
55 numbytes = send(fd, buffer, size, MSG_NOSIGNAL);
65 void recv_data(int fd , void *buf, int buflen) {
66 char *buffer = (char *)(buf);
70 numbytes = recv(fd, buffer, size, 0);
80 int recv_data_errorcode(int fd , void *buf, int buflen) {
81 char *buffer = (char *)(buf);
85 numbytes = recv(fd, buffer, size, 0);
95 void printhex(unsigned char *ptr, int numBytes) {
97 for (i = 0; i < numBytes; i++) {
99 printf("0%x ", ptr[i]);
101 printf("%x ", ptr[i]);
107 inline int arrayLength(int *array) {
109 for(i=0 ;array[i] != -1; i++)
114 inline int findmax(int *array, int arraylength) {
117 for(i = 0; i < arraylength; i++){
125 /* This function is a prefetch call generated by the compiler that
126 * populates the shared primary prefetch queue*/
127 void prefetch(int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
128 /* Allocate for the queue node*/
129 int qnodesize = sizeof(prefetchqelem_t) + sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
130 char * node= malloc(qnodesize);
131 /* Set queue node values */
132 int len = sizeof(prefetchqelem_t);
133 int top=endoffsets[ntuples-1];
134 *((int *)(node+len))=ntuples;
136 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
137 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
138 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
140 /* Lock and insert into primary prefetch queue */
141 pthread_mutex_lock(&pqueue.qlock);
142 pre_enqueue((prefetchqelem_t *)node);
143 pthread_cond_signal(&pqueue.qcond);
144 pthread_mutex_unlock(&pqueue.qlock);
147 /* This function starts up the transaction runtime. */
148 int dstmStartup(const char * option) {
149 pthread_t thread_Listen;
151 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);
168 pthread_attr_init(&attr);
169 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
170 pthread_create(&thread_Listen, &attr, dstmListen, NULL);
178 //TODO Use this later
179 void *pCacheAlloc(objstr_t *store, unsigned int size) {
185 while(ptr->next != NULL) {
186 /* check if store is empty */
187 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
202 /* This function initiates the prefetch thread A queue is shared
203 * between the main thread of execution and the prefetch thread to
204 * process the prefetch call Call from compiler populates the shared
205 * queue with prefetch requests while prefetch thread processes the
206 * prefetch requests */
211 //Create and initialize prefetch cache structure
212 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
214 /* Initialize attributes for mutex */
215 pthread_mutexattr_init(&prefetchcache_mutex_attr);
216 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
218 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
220 pthread_mutex_init(¬ifymutex, NULL);
221 pthread_mutex_init(&atomicObjLock, NULL);
222 //Create prefetch cache lookup table
223 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
228 //Initialize primary shared queue
230 //Initialize machine pile w/prefetch oids and offsets shared queue
233 //Create the primary prefetch thread
235 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
237 pthread_detach(tPrefetch);
240 /* This function stops the threads spawned */
243 pthread_cancel(tPrefetch);
244 for(t = 0; t < NUM_THREADS; t++)
245 pthread_cancel(wthreads[t]);
250 /* This functions inserts randowm wait delays in the order of msec
251 * Mostly used when transaction commits retry*/
258 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
259 nanosleep(&req, NULL);
263 /* This function initializes things required in the transaction start*/
264 transrecord_t *transStart() {
266 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL){
267 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
270 tmp->cache = objstrCreate(1048576);
271 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
273 tmp->revertlist=NULL;
278 /* This function finds the location of the objects involved in a transaction
279 * and returns the pointer to the object if found in a remote location */
280 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
281 unsigned int machinenumber;
282 objheader_t *tmp, *objheader;
283 objheader_t *objcopy;
291 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
292 /* Search local transaction cache */
294 return &objheader[1];
298 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
299 /* Look up in machine lookup table and copy into cache*/
300 GETSIZE(size, objheader);
301 size += sizeof(objheader_t);
302 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
303 memcpy(objcopy, objheader, size);
304 /* Insert into cache's lookup table */
305 chashInsert(record->lookupTable, OID(objheader), objcopy);
311 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
312 /* Look up in prefetch cache */
314 size+=sizeof(objheader_t);
315 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
316 memcpy(objcopy, tmp, size);
317 /* Insert into cache's lookup table */
318 chashInsert(record->lookupTable, OID(tmp), objcopy);
325 /* Get the object from the remote location */
326 if((machinenumber = lhashSearch(oid)) == 0) {
327 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
330 objcopy = getRemoteObj(record, machinenumber, oid);
332 if(objcopy == NULL) {
333 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
346 /* This function creates objects in the transaction record */
347 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
348 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
349 tmp->notifylist = NULL;
350 OID(tmp) = getNewOID();
354 chashInsert(record->lookupTable, OID(tmp), tmp);
357 return &tmp[1]; //want space after object header
363 /* This function creates machine piles based on all machines involved in a
364 * transaction commit request */
365 plistnode_t *createPiles(transrecord_t *record) {
367 plistnode_t *pile = NULL;
368 unsigned int machinenum;
369 objheader_t *headeraddr;
370 chashlistnode_t * ptr = record->lookupTable->table;
371 /* Represents number of bins in the chash table */
372 unsigned int size = record->lookupTable->size;
374 for(i = 0; i < size ; i++) {
375 chashlistnode_t * curr = &ptr[i];
376 /* Inner loop to traverse the linked list of the cache lookupTable */
377 while(curr != NULL) {
378 //if the first bin in hash table is empty
382 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
383 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
387 //Get machine location for object id (and whether local or not)
388 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
389 machinenum = myIpAddr;
390 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
391 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
395 //Make machine groups
396 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
403 /* This function initiates the transaction commit process
404 * Spawns threads for each of the new connections with Participants
405 * and creates new piles by calling the createPiles(),
406 * Sends a transrequest() to each remote machines for objects found remotely
407 * and calls handleLocalReq() to process objects found locally */
408 int transCommit(transrecord_t *record) {
409 unsigned int tot_bytes_mod, *listmid;
410 plistnode_t *pile, *pile_ptr;
412 int pilecount, offset, threadnum = 0, trecvcount = 0;
414 char transid[TID_LEN];
415 trans_req_data_t *tosend;
416 trans_commit_data_t transinfo;
417 static int newtid = 0;
418 char treplyctrl = 0, treplyretry = 0; /* 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;
427 thread_data_array = NULL;
430 /* Look through all the objects in the transaction record and make piles
431 * for each machine involved in the transaction*/
432 pile_ptr = pile = createPiles(record);
434 /* Create the packet to be sent in TRANS_REQUEST */
436 /* Count the number of participants */
437 pilecount = pCount(pile);
439 /* Create a list of machine ids(Participants) involved in transaction */
440 listmid = calloc(pilecount, sizeof(unsigned int));
441 pListMid(pile, listmid);
444 /* Initialize thread variables,
445 * Spawn a thread for each Participant involved in a transaction */
446 pthread_t thread[pilecount];
448 pthread_cond_t tcond;
449 pthread_mutex_t tlock;
450 pthread_mutex_t tlshrd;
452 thread_data_array = (thread_data_array_t *) calloc(pilecount, sizeof(thread_data_array_t));
454 ltdata = calloc(1, sizeof(local_thread_data_array_t));
456 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
458 /* Initialize and set thread detach attribute */
459 pthread_attr_init(&attr);
460 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
461 pthread_mutex_init(&tlock, NULL);
462 pthread_cond_init(&tcond, NULL);
464 /* Process each machine pile */
465 while(pile != NULL) {
466 //Create transaction id
468 tosend = calloc(1, sizeof(trans_req_data_t));
469 tosend->f.control = TRANS_REQUEST;
470 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
471 tosend->f.mcount = pilecount;
472 tosend->f.numread = pile->numread;
473 tosend->f.nummod = pile->nummod;
474 tosend->f.numcreated = pile->numcreated;
475 tosend->f.sum_bytes = pile->sum_bytes;
476 tosend->listmid = listmid;
477 tosend->objread = pile->objread;
478 tosend->oidmod = pile->oidmod;
479 tosend->oidcreated = pile->oidcreated;
480 thread_data_array[threadnum].thread_id = threadnum;
481 thread_data_array[threadnum].mid = pile->mid;
482 thread_data_array[threadnum].buffer = tosend;
483 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
484 thread_data_array[threadnum].threshold = &tcond;
485 thread_data_array[threadnum].lock = &tlock;
486 thread_data_array[threadnum].count = &trecvcount;
487 thread_data_array[threadnum].replyctrl = &treplyctrl;
488 thread_data_array[threadnum].replyretry = &treplyretry;
489 thread_data_array[threadnum].rec = record;
490 /* If local do not create any extra connection */
491 if(pile->mid != myIpAddr) { /* Not local */
493 rc = pthread_create(&thread[threadnum], &attr, transRequest, (void *) &thread_data_array[threadnum]);
496 perror("Error in pthread create\n");
497 pthread_cond_destroy(&tcond);
498 pthread_mutex_destroy(&tlock);
501 for (i = 0; i < threadnum; i++)
502 free(thread_data_array[i].buffer);
503 free(thread_data_array);
508 ltdata->tdata = &thread_data_array[threadnum];
509 ltdata->transinfo = &transinfo;
511 val = pthread_create(&thread[threadnum], &attr, handleLocalReq, (void *) ltdata);
514 perror("Error in pthread create\n");
515 pthread_cond_destroy(&tcond);
516 pthread_mutex_destroy(&tlock);
519 for (i = 0; i < threadnum; i++)
520 free(thread_data_array[i].buffer);
521 free(thread_data_array);
530 /* Free attribute and wait for the other threads */
531 pthread_attr_destroy(&attr);
533 for (i = 0; i < threadnum; i++) {
534 rc = pthread_join(thread[i], NULL);
537 printf("Error: return code from pthread_join() is %d\n", rc);
538 pthread_cond_destroy(&tcond);
539 pthread_mutex_destroy(&tlock);
542 for (j = i; j < threadnum; j++) {
543 free(thread_data_array[j].buffer);
547 free(thread_data_array[i].buffer);
551 pthread_cond_destroy(&tcond);
552 pthread_mutex_destroy(&tlock);
556 /* wait a random amount of time before retrying to commit transaction*/
558 free(thread_data_array);
563 /* Retry trans commit procedure during soft_abort case */
564 } while (treplyretry);
566 if(treplyctrl == TRANS_ABORT) {
568 objstrDelete(record->cache);
569 chashDelete(record->lookupTable);
571 free(thread_data_array);
574 } else if(treplyctrl == TRANS_COMMIT) {
576 objstrDelete(record->cache);
577 chashDelete(record->lookupTable);
579 free(thread_data_array);
583 //TODO Add other cases
584 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
590 /* This function sends information involved in the transaction request
591 * to participants and accepts a response from particpants.
592 * It calls decideresponse() to decide on what control message
593 * to send next to participants and sends the message using sendResponse()*/
594 void *transRequest(void *threadarg) {
596 struct sockaddr_in serv_addr;
597 thread_data_array_t *tdata;
598 objheader_t *headeraddr;
599 char control, recvcontrol;
600 char machineip[16], retval;
602 tdata = (thread_data_array_t *) threadarg;
604 /* Send Trans Request */
605 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
606 perror("Error in socket for TRANS_REQUEST\n");
609 bzero((char*) &serv_addr, sizeof(serv_addr));
610 serv_addr.sin_family = AF_INET;
611 serv_addr.sin_port = htons(LISTEN_PORT);
612 midtoIP(tdata->mid,machineip);
613 machineip[15] = '\0';
614 serv_addr.sin_addr.s_addr = inet_addr(machineip);
615 /* Open Connection */
616 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
617 perror("Error in connect for TRANS_REQUEST\n");
622 /* Send bytes of data with TRANS_REQUEST control message */
623 send_data(sd, &(tdata->buffer->f), sizeof(fixed_data_t));
625 /* Send list of machines involved in the transaction */
627 int size=sizeof(unsigned int)*tdata->buffer->f.mcount;
628 send_data(sd, tdata->buffer->listmid, size);
631 /* Send oids and version number tuples for objects that are read */
633 int size=(sizeof(unsigned int)+sizeof(unsigned short))*tdata->buffer->f.numread;
634 send_data(sd, tdata->buffer->objread, size);
637 /* Send objects that are modified */
638 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
640 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
641 GETSIZE(size,headeraddr);
642 size+=sizeof(objheader_t);
643 send_data(sd, headeraddr, size);
646 /* Read control message from Participant */
647 recv_data(sd, &control, sizeof(char));
648 recvcontrol = control;
650 /* Update common data structure and increment count */
651 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
653 /* Lock and update count */
654 /* Thread sleeps until all messages from pariticipants are received by coordinator */
655 pthread_mutex_lock(tdata->lock);
657 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
659 /* Wake up the threads and invoke decideResponse (once) */
660 if(*(tdata->count) == tdata->buffer->f.mcount) {
661 decideResponse(tdata);
662 pthread_cond_broadcast(tdata->threshold);
664 pthread_cond_wait(tdata->threshold, tdata->lock);
666 pthread_mutex_unlock(tdata->lock);
668 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
669 * to all participants in their respective socket */
670 if (sendResponse(tdata, sd) == 0) {
671 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
676 recv_data((int)sd, &control, sizeof(char));
678 if(control == TRANS_UNSUCESSFUL) {
679 //printf("DEBUG-> TRANS_ABORTED\n");
680 } else if(control == TRANS_SUCESSFUL) {
681 //printf("DEBUG-> TRANS_SUCCESSFUL\n");
683 //printf("DEBUG-> Error: Incorrect Transaction End Message %d\n", control);
686 /* Close connection */
691 /* This function decides the reponse that needs to be sent to
692 * all Participant machines after the TRANS_REQUEST protocol */
693 void decideResponse(thread_data_array_t *tdata) {
695 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
698 for (i = 0 ; i < tdata->buffer->f.mcount; i++) {
699 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
700 written onto the shared array */
703 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
704 /* treat as disagree, pass thru */
713 case TRANS_SOFT_ABORT:
719 if(transdisagree > 0) {
721 *(tdata->replyctrl) = TRANS_ABORT;
722 *(tdata->replyretry) = 0;
723 /* clear objects from prefetch cache */
724 for (i = 0; i < tdata->buffer->f.numread; i++)
725 prehashRemove(*((unsigned int *)(((char *)tdata->buffer->objread) + (sizeof(unsigned int) + sizeof(unsigned short))*i)));
726 for (i = 0; i < tdata->buffer->f.nummod; i++)
727 prehashRemove(tdata->buffer->oidmod[i]);
728 } else if(transagree == tdata->buffer->f.mcount){
730 *(tdata->replyctrl) = TRANS_COMMIT;
731 *(tdata->replyretry) = 0;
733 /* Send Abort in soft abort case followed by retry commiting transaction again*/
734 *(tdata->replyctrl) = TRANS_ABORT;
735 *(tdata->replyretry) = 1;
741 /* This function sends the final response to remote machines per
742 * thread in their respective socket id It returns a char that is only
743 * needed to check the correctness of execution of this function
744 * inside transRequest()*/
746 char sendResponse(thread_data_array_t *tdata, int sd) {
747 int n, size, sum, oidcount = 0, control;
748 char *ptr, retval = 0;
749 unsigned int *oidnotfound;
751 control = *(tdata->replyctrl);
752 send_data(sd, &control, sizeof(char));
754 //TODO read missing objects during object migration
755 /* If response is a soft abort due to missing objects at the
756 Participant's side */
758 /* If the decided response is TRANS_ABORT */
759 if(*(tdata->replyctrl) == TRANS_ABORT) {
760 retval = TRANS_ABORT;
761 } else if(*(tdata->replyctrl) == TRANS_COMMIT) {
762 /* If the decided response is TRANS_COMMIT */
763 retval = TRANS_COMMIT;
769 /* This function opens a connection, places an object read request to
770 * the remote machine, reads the control message and object if
771 * available and copies the object and its header to the local
774 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
776 struct sockaddr_in serv_addr;
780 void *objcopy = NULL;
782 int sd = getSock2(transReadSockPool, mnum);
783 char readrequest[sizeof(char)+sizeof(unsigned int)];
784 readrequest[0] = READ_REQUEST;
785 *((unsigned int *)(&readrequest[1])) = oid;
786 send_data(sd, readrequest, sizeof(readrequest));
788 /* Read response from the Participant */
789 recv_data(sd, &control, sizeof(char));
791 if (control==OBJECT_NOT_FOUND) {
794 /* Read object if found into local cache */
795 recv_data(sd, &size, sizeof(int));
796 objcopy = objstrAlloc(record->cache, size);
797 recv_data(sd, objcopy, size);
799 /* Insert into cache's lookup table */
800 chashInsert(record->lookupTable, oid, objcopy);
806 /* This function handles the local objects involved in a transaction
807 * commiting process. It also makes a decision if this local machine
808 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator. Note
809 * Coordinator = local machine It wakes up the other threads from
810 * remote participants that are waiting for the coordinator's decision
811 * and based on common agreement it either commits or aborts the
812 * transaction. It also frees the memory resources */
814 void *handleLocalReq(void *threadarg) {
815 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
816 local_thread_data_array_t *localtdata;
817 int objnotfound = 0, objlocked = 0;
818 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
821 unsigned short version;
823 objheader_t *headptr;
825 localtdata = (local_thread_data_array_t *) threadarg;
827 /* Counters and arrays to formulate decision on control message to be sent */
828 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
829 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
831 numread = localtdata->tdata->buffer->f.numread;
832 /* Process each oid in the machine pile/ group per thread */
833 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
834 if (i < localtdata->tdata->buffer->f.numread) {
835 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
837 oid = *((unsigned int *)(((char *)localtdata->tdata->buffer->objread) + incr));
838 version = *((unsigned short *)(((char *)localtdata->tdata->buffer->objread) + incr + sizeof(unsigned int)));
839 } else { // Objects Modified
841 headptr = (objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i-numread]);
842 if (headptr == NULL) {
843 printf("Error: handleLocalReq() returning NULL %s, %d\n", __FILE__, __LINE__);
847 version = headptr->version;
849 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
851 /* Save the oids not found and number of oids not found for later use */
852 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
853 /* Save the oids not found and number of oids not found for later use */
854 oidnotfound[objnotfound] = oid;
856 } else { /* If Obj found in machine (i.e. has not moved) */
857 /* Check if Obj is locked by any previous transaction */
858 pthread_mutex_lock(&atomicObjLock);
859 if ((STATUS((objheader_t *)mobj) & LOCK) == LOCK) {
860 pthread_mutex_unlock(&atomicObjLock);
861 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
863 } else {/* If versions don't match ...HARD ABORT */
865 /* Send TRANS_DISAGREE to Coordinator */
866 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
868 } else {/* If Obj is not locked then lock object */
869 STATUS(((objheader_t *)mobj)) |= LOCK;
870 pthread_mutex_unlock(&atomicObjLock);
871 /* Save all object oids that are locked on this machine during this transaction request call */
872 oidlocked[objlocked] = OID(((objheader_t *)mobj));
874 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
876 } else { /* If versions don't match ...HARD ABORT */
878 /* Send TRANS_DISAGREE to Coordinator */
879 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
884 /* Condition to send TRANS_AGREE */
885 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
886 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
888 /* Condition to send TRANS_SOFT_ABORT */
889 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
890 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
893 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
894 * if Participant receives a TRANS_COMMIT */
895 localtdata->transinfo->objlocked = oidlocked;
896 localtdata->transinfo->objnotfound = oidnotfound;
897 localtdata->transinfo->modptr = NULL;
898 localtdata->transinfo->numlocked = objlocked;
899 localtdata->transinfo->numnotfound = objnotfound;
900 /* Lock and update count */
901 //Thread sleeps until all messages from pariticipants are received by coordinator
902 pthread_mutex_lock(localtdata->tdata->lock);
903 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
905 /* Wake up the threads and invoke decideResponse (once) */
906 if(*(localtdata->tdata->count) == localtdata->tdata->buffer->f.mcount) {
907 decideResponse(localtdata->tdata);
908 pthread_cond_broadcast(localtdata->tdata->threshold);
910 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
912 pthread_mutex_unlock(localtdata->tdata->lock);
913 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
914 if(transAbortProcess(localtdata) != 0) {
915 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
918 } else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT) {
919 if(transComProcess(localtdata) != 0) {
920 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
925 if (localtdata->transinfo->objlocked != NULL) {
926 free(localtdata->transinfo->objlocked);
928 if (localtdata->transinfo->objnotfound != NULL) {
929 free(localtdata->transinfo->objnotfound);
935 /* This function completes the ABORT process if the transaction is aborting */
936 int transAbortProcess(local_thread_data_array_t *localtdata) {
938 unsigned int *objlocked;
941 numlocked = localtdata->transinfo->numlocked;
942 objlocked = localtdata->transinfo->objlocked;
944 for (i = 0; i < numlocked; i++) {
945 if((header = mhashSearch(objlocked[i])) == NULL) {
946 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
949 STATUS(((objheader_t *)header)) &= ~(LOCK);
955 /*This function completes the COMMIT process is the transaction is commiting*/
956 int transComProcess(local_thread_data_array_t *localtdata) {
957 objheader_t *header, *tcptr;
958 int i, nummod, tmpsize, numcreated, numlocked;
959 unsigned int *oidmod, *oidcreated, *oidlocked;
962 nummod = localtdata->tdata->buffer->f.nummod;
963 oidmod = localtdata->tdata->buffer->oidmod;
964 numcreated = localtdata->tdata->buffer->f.numcreated;
965 oidcreated = localtdata->tdata->buffer->oidcreated;
966 numlocked = localtdata->transinfo->numlocked;
967 oidlocked = localtdata->transinfo->objlocked;
969 for (i = 0; i < nummod; i++) {
970 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
971 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
974 /* Copy from transaction cache -> main object store */
975 if ((tcptr = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidmod[i]))) == NULL) {
976 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
979 GETSIZE(tmpsize, header);
980 pthread_mutex_lock(&mainobjstore_mutex);
981 char *tmptcptr = (char *) tcptr;
982 memcpy((char*)header+sizeof(objheader_t), (char *)tmptcptr+ sizeof(objheader_t), tmpsize);
983 header->version += 1;
984 pthread_mutex_lock(¬ifymutex);
985 if(header->notifylist != NULL) {
986 notifyAll(&header->notifylist, OID(header), header->version);
988 pthread_mutex_unlock(¬ifymutex);
989 pthread_mutex_unlock(&mainobjstore_mutex);
991 /* If object is newly created inside transaction then commit it */
992 for (i = 0; i < numcreated; i++) {
993 if ((header = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidcreated[i]))) == NULL) {
994 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
997 GETSIZE(tmpsize, header);
998 tmpsize += sizeof(objheader_t);
999 pthread_mutex_lock(&mainobjstore_mutex);
1000 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1001 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1002 pthread_mutex_unlock(&mainobjstore_mutex);
1005 pthread_mutex_unlock(&mainobjstore_mutex);
1006 memcpy(ptrcreate, header, tmpsize);
1007 mhashInsert(oidcreated[i], ptrcreate);
1008 lhashInsert(oidcreated[i], myIpAddr);
1010 /* Unlock locked objects */
1011 for(i = 0; i < numlocked; i++) {
1012 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1013 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1016 STATUS(header) &= ~(LOCK);
1022 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1023 char * ptr = (char *) node;
1024 int ntuples = *(GET_NTUPLES(ptr));
1025 unsigned int * oidarray = GET_PTR_OID(ptr);
1026 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1027 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1028 prefetchpile_t * head=NULL;
1031 for(i=0;i<ntuples; i++) {
1032 unsigned short baseindex=(i==0)?0:endoffsets[i-1];
1033 unsigned short endindex=endoffsets[i];
1034 unsigned int oid=oidarray[i];
1039 //Look up fields locally
1040 for(newbase=baseindex;newbase<endindex;newbase++) {
1041 if (!lookupObject(&oid, arryfields[newbase]))
1043 //Ended in a null pointer...
1047 //Entire prefetch is local
1048 if (newbase==endindex&&checkoid(oid))
1050 //Add to remote requests
1051 machinenum=lhashSearch(oid);
1052 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1059 int checkoid(unsigned int oid) {
1060 objheader_t *header;
1061 if ((header=mhashSearch(oid))!=NULL) {
1064 } else if ((header=prehashSearch(oid))!=NULL) {
1072 int lookupObject(unsigned int * oid, short offset) {
1073 objheader_t *header;
1074 if ((header=mhashSearch(*oid))!=NULL) {
1077 } else if ((header=prehashSearch(*oid))!=NULL) {
1084 if(TYPE(header) > NUMCLASSES) {
1085 int elementsize = classsize[TYPE(header)];
1086 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1087 int length = ao->___length___;
1088 /* Check if array out of bounds */
1089 if(offset < 0 || offset >= length) {
1090 //if yes treat the object as found
1094 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1097 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1103 /* This function is called by the thread calling transPrefetch */
1104 void *transPrefetch(void *t) {
1106 /* lock mutex of primary prefetch queue */
1107 pthread_mutex_lock(&pqueue.qlock);
1108 /* while primary queue is empty, then wait */
1109 while(pqueue.front == NULL) {
1110 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1113 /* dequeue node to create a machine piles and finally unlock mutex */
1114 prefetchqelem_t *qnode = pre_dequeue();
1115 pthread_mutex_unlock(&pqueue.qlock);
1117 /* Check if the tuples are found locally, if yes then reduce them further*/
1118 /* and group requests by remote machine ids by calling the makePreGroups() */
1119 prefetchpile_t *pilehead = foundLocal(qnode);
1121 if (pilehead!=NULL) {
1122 // Get sock from shared pool
1123 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1125 /* Send Prefetch Request */
1126 prefetchpile_t *ptr = pilehead;
1127 while(ptr != NULL) {
1128 sendPrefetchReq(ptr, sd);
1132 /* Release socket */
1133 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1135 /* Deallocated pilehead */
1136 mcdealloc(pilehead);
1139 // Deallocate the prefetch queue pile node
1144 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1147 int size=sizeof(char)+sizeof(int);
1148 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1149 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1154 *buf=TRANS_PREFETCH;
1157 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1158 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1161 *((unsigned int *)buf)=tmp->oid;
1162 buf+=sizeof(unsigned int);
1163 *((unsigned int *)(buf)) = myIpAddr;
1164 buf+=sizeof(unsigned int);
1165 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1166 buf+=tmp->numoffset*sizeof(short);
1169 send_data(sd, buft, size);
1173 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1178 /* Send TRANS_PREFETCH control message */
1179 control = TRANS_PREFETCH;
1180 send_data(sd, &control, sizeof(char));
1182 /* Send Oids and offsets in pairs */
1183 tmp = mcpilenode->objpiles;
1184 while(tmp != NULL) {
1185 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1186 char oidnoffset[len];
1187 char *buf=oidnoffset;
1188 *((int*)buf) = tmp->numoffset;
1190 *((unsigned int *)buf) = tmp->oid;
1191 buf+=sizeof(unsigned int);
1192 *((unsigned int *)buf) = myIpAddr;
1193 buf += sizeof(unsigned int);
1194 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1195 send_data(sd, oidnoffset, len);
1199 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1201 send_data(sd, &endpair, sizeof(int));
1206 int getPrefetchResponse(int sd) {
1207 int length = 0, size = 0;
1210 void *modptr, *oldptr;
1212 recv_data((int)sd, &length, sizeof(int));
1213 size = length - sizeof(int);
1214 char recvbuffer[size];
1216 recv_data((int)sd, recvbuffer, size);
1217 control = *((char *) recvbuffer);
1218 if(control == OBJECT_FOUND) {
1219 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1220 size = size - (sizeof(char) + sizeof(unsigned int));
1221 pthread_mutex_lock(&prefetchcache_mutex);
1222 if ((modptr = objstrAlloc(prefetchcache, size)) == NULL) {
1223 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1224 pthread_mutex_unlock(&prefetchcache_mutex);
1227 pthread_mutex_unlock(&prefetchcache_mutex);
1228 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1230 /* Insert the oid and its address into the prefetch hash lookup table */
1231 /* Do a version comparison if the oid exists */
1232 if((oldptr = prehashSearch(oid)) != NULL) {
1233 /* If older version then update with new object ptr */
1234 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1236 prehashInsert(oid, modptr);
1238 } else {/* Else add the object ptr to hash table*/
1239 prehashInsert(oid, modptr);
1241 /* Lock the Prefetch Cache look up table*/
1242 pthread_mutex_lock(&pflookup.lock);
1243 /* Broadcast signal on prefetch cache condition variable */
1244 pthread_cond_broadcast(&pflookup.cond);
1245 /* Unlock the Prefetch Cache look up table*/
1246 pthread_mutex_unlock(&pflookup.lock);
1247 } else if(control == OBJECT_NOT_FOUND) {
1248 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1249 /* TODO: For each object not found query DHT for new location and retrieve the object */
1250 /* Throw an error */
1251 printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1254 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1260 unsigned short getObjType(unsigned int oid) {
1261 objheader_t *objheader;
1262 unsigned short numoffset[] ={0};
1263 short fieldoffset[] ={};
1265 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1266 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1267 prefetch(1, &oid, numoffset, fieldoffset);
1268 pthread_mutex_lock(&pflookup.lock);
1269 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1270 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1272 pthread_mutex_unlock(&pflookup.lock);
1276 return TYPE(objheader);
1279 int startRemoteThread(unsigned int oid, unsigned int mid)
1282 struct sockaddr_in remoteAddr;
1283 char msg[1 + sizeof(unsigned int)];
1287 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1289 perror("startRemoteThread():socket()");
1293 bzero(&remoteAddr, sizeof(remoteAddr));
1294 remoteAddr.sin_family = AF_INET;
1295 remoteAddr.sin_port = htons(LISTEN_PORT);
1296 remoteAddr.sin_addr.s_addr = htonl(mid);
1298 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1300 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1301 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1306 msg[0] = START_REMOTE_THREAD;
1307 *((unsigned int *) &msg[1]) = oid;
1308 send_data(sock, msg, 1 + sizeof(unsigned int));
1315 //TODO: when reusing oids, make sure they are not already in use!
1316 static unsigned int id = 0xFFFFFFFF;
1317 unsigned int getNewOID(void) {
1319 if (id > oidMax || id < oidMin)
1326 int processConfigFile()
1329 const int maxLineLength = 200;
1330 char lineBuffer[maxLineLength];
1332 const char *delimiters = " \t\n";
1336 configFile = fopen(CONFIG_FILENAME, "r");
1337 if (configFile == NULL)
1339 printf("error opening %s:\n", CONFIG_FILENAME);
1344 numHostsInSystem = 0;
1345 sizeOfHostArray = 8;
1346 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1348 while(fgets(lineBuffer, maxLineLength, configFile) != NULL)
1350 commentBegin = strchr(lineBuffer, '#');
1351 if (commentBegin != NULL)
1352 *commentBegin = '\0';
1353 token = strtok(lineBuffer, delimiters);
1354 while (token != NULL)
1356 tmpAddr = inet_addr(token);
1357 if ((int)tmpAddr == -1)
1359 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1364 addHost(htonl(tmpAddr));
1365 token = strtok(NULL, delimiters);
1371 if (numHostsInSystem < 1)
1373 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1377 myIpAddr = getMyIpAddr("en1");
1379 myIpAddr = getMyIpAddr("eth0");
1381 myIndexInHostArray = findHost(myIpAddr);
1382 if (myIndexInHostArray == -1)
1384 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1387 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1388 oidMin = oidsPerBlock * myIndexInHostArray;
1389 if (myIndexInHostArray == numHostsInSystem - 1)
1390 oidMax = 0xFFFFFFFF;
1392 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1397 void addHost(unsigned int hostIp)
1399 unsigned int *tmpArray;
1401 if (findHost(hostIp) != -1)
1404 if (numHostsInSystem == sizeOfHostArray)
1406 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1407 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1409 hostIpAddrs = tmpArray;
1412 hostIpAddrs[numHostsInSystem++] = hostIp;
1417 int findHost(unsigned int hostIp)
1420 for (i = 0; i < numHostsInSystem; i++)
1421 if (hostIpAddrs[i] == hostIp)
1428 /* This function sends notification request per thread waiting on object(s) whose version
1430 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1432 objheader_t *objheader;
1433 struct sockaddr_in remoteAddr;
1434 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1438 unsigned short version;
1439 unsigned int oid,mid;
1440 static unsigned int threadid = 0;
1441 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1442 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1443 notifydata_t *ndata;
1446 if((mid = lhashSearch(oid)) == 0) {
1447 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1451 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1452 perror("reqNotify():socket()");
1456 bzero(&remoteAddr, sizeof(remoteAddr));
1457 remoteAddr.sin_family = AF_INET;
1458 remoteAddr.sin_port = htons(LISTEN_PORT);
1459 remoteAddr.sin_addr.s_addr = htonl(mid);
1461 /* Generate unique threadid */
1464 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1465 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1466 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1469 ndata->numoid = numoid;
1470 ndata->threadid = threadid;
1471 ndata->oidarry = oidarry;
1472 ndata->versionarry = versionarry;
1473 ndata->threadcond = threadcond;
1474 ndata->threadnotify = threadnotify;
1475 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1476 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1481 /* Send number of oids, oidarry, version array, machine id and threadid */
1482 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1483 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1484 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1488 msg[0] = THREAD_NOTIFY_REQUEST;
1489 *((unsigned int *)(&msg[1])) = numoid;
1490 /* Send array of oids */
1491 size = sizeof(unsigned int);
1496 *((unsigned int *)(&msg[1] + size)) = oid;
1497 size += sizeof(unsigned int);
1502 /* Send array of version */
1506 version = versionarry[i];
1507 *((unsigned short *)(&msg[1] + size)) = version;
1508 size += sizeof(unsigned short);
1513 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1514 size += sizeof(unsigned int);
1515 *((unsigned int *)(&msg[1] + size)) = threadid;
1516 pthread_mutex_lock(&(ndata->threadnotify));
1517 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1518 send_data(sock, msg, size);
1519 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1520 pthread_mutex_unlock(&(ndata->threadnotify));
1523 pthread_cond_destroy(&threadcond);
1524 pthread_mutex_destroy(&threadnotify);
1530 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1531 notifydata_t *ndata;
1532 int i, objIsFound = 0, index;
1535 //Look up the tid and call the corresponding pthread_cond_signal
1536 if((ndata = notifyhashSearch(tid)) == NULL) {
1537 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1540 for(i = 0; i < ndata->numoid; i++) {
1541 if(ndata->oidarry[i] == oid){
1546 if(objIsFound == 0){
1547 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1550 if(version <= ndata->versionarry[index]){
1551 printf("threadNotify(): New version %d has not changed since last version %s, %d\n", version, __FILE__, __LINE__);
1554 /* Clear from prefetch cache and free thread related data structure */
1555 if((ptr = prehashSearch(oid)) != NULL) {
1558 pthread_cond_signal(&(ndata->threadcond));
1565 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1568 struct sockaddr_in remoteAddr;
1569 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1570 int sock, status, size, bytesSent;
1572 while(*head != NULL) {
1575 //create a socket connection to that machine
1576 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1577 perror("notifyAll():socket()");
1581 bzero(&remoteAddr, sizeof(remoteAddr));
1582 remoteAddr.sin_family = AF_INET;
1583 remoteAddr.sin_port = htons(LISTEN_PORT);
1584 remoteAddr.sin_addr.s_addr = htonl(mid);
1585 //send Thread Notify response and threadid to that machine
1586 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1587 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1588 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1592 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1593 msg[0] = THREAD_NOTIFY_RESPONSE;
1594 *((unsigned int *)&msg[1]) = oid;
1595 size = sizeof(unsigned int);
1596 *((unsigned short *)(&msg[1]+ size)) = version;
1597 size+= sizeof(unsigned short);
1598 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1600 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1601 send_data(sock, msg, size);
1612 void transAbort(transrecord_t *trans) {
1613 objstrDelete(trans->cache);
1614 chashDelete(trans->lookupTable);