4 #include "machinepile.h"
9 #include "threadnotify.h"
11 #include "addUdpEnhance.h"
17 #define PREFETCH_CACHE_SIZE 1048576 //1MB
18 #define CONFIG_FILENAME "dstm.conf"
20 /* Global Variables */
21 extern int classsize[];
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);
97 void printhex(unsigned char *ptr, int numBytes) {
99 for (i = 0; i < numBytes; i++) {
101 printf("0%x ", ptr[i]);
103 printf("%x ", ptr[i]);
109 inline int arrayLength(int *array) {
111 for(i=0 ;array[i] != -1; i++)
116 inline int findmax(int *array, int arraylength) {
119 for(i = 0; i < arraylength; i++){
127 /* This function is a prefetch call generated by the compiler that
128 * populates the shared primary prefetch queue*/
129 void prefetch(int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
130 /* Allocate for the queue node*/
131 int qnodesize = sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
132 char * node= getmemory(qnodesize);
133 /* Set queue node values */
135 int top=endoffsets[ntuples-1];
137 *((int *)(node))=ntuples;
139 memcpy(node+len, oids, ntuples*sizeof(unsigned int));
140 memcpy(node+len+ntuples*sizeof(unsigned int), endoffsets, ntuples*sizeof(unsigned short));
141 memcpy(node+len+ntuples*(sizeof(unsigned int)+sizeof(short)), arrayfields, top*sizeof(short));
143 /* Lock and insert into primary prefetch queue */
147 /* This function starts up the transaction runtime. */
148 int dstmStartup(const char * option) {
149 pthread_t thread_Listen, udp_thread_Listen;
151 int master=option!=NULL && strcmp(option, "master")==0;
155 if (processConfigFile() != 0)
156 return 0; //TODO: return error value, cause main program to exit
162 //Initialize socket pool
163 transReadSockPool = createSockPool(transReadSockPool, 2*numHostsInSystem+1);
164 transPrefetchSockPool = createSockPool(transPrefetchSockPool, 2*numHostsInSystem+1);
171 pthread_create(&udp_thread_Listen, &attr, udpListenBroadcast, (void*)udpfd);
173 pthread_attr_init(&attr);
174 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
175 pthread_create(&thread_Listen, &attr, dstmListen, (void*)fd);
178 dstmListen((void *)fd);
183 //TODO Use this later
184 void *pCacheAlloc(objstr_t *store, unsigned int size) {
190 while(ptr->next != NULL) {
191 /* check if store is empty */
192 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
207 /* This function initiates the prefetch thread A queue is shared
208 * between the main thread of execution and the prefetch thread to
209 * process the prefetch call Call from compiler populates the shared
210 * queue with prefetch requests while prefetch thread processes the
211 * prefetch requests */
216 //Create and initialize prefetch cache structure
217 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
219 /* Initialize attributes for mutex */
220 pthread_mutexattr_init(&prefetchcache_mutex_attr);
221 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
223 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
225 pthread_mutex_init(¬ifymutex, NULL);
226 pthread_mutex_init(&atomicObjLock, NULL);
227 //Create prefetch cache lookup table
228 if(prehashCreate(HASH_SIZE, LOADFACTOR)) {
233 //Initialize primary shared queue
235 //Initialize machine pile w/prefetch oids and offsets shared queue
238 //Create the primary prefetch thread
240 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
242 pthread_detach(tPrefetch);
245 /* This function stops the threads spawned */
248 pthread_cancel(tPrefetch);
249 for(t = 0; t < NUM_THREADS; t++)
250 pthread_cancel(wthreads[t]);
255 /* This functions inserts randowm wait delays in the order of msec
256 * Mostly used when transaction commits retry*/
263 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
264 nanosleep(&req, NULL);
268 /* This function initializes things required in the transaction start*/
269 transrecord_t *transStart() {
271 if((tmp = calloc(1, sizeof(transrecord_t))) == NULL){
272 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
275 tmp->cache = objstrCreate(1048576);
276 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
278 tmp->revertlist=NULL;
283 /* This function finds the location of the objects involved in a transaction
284 * and returns the pointer to the object if found in a remote location */
285 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
286 unsigned int machinenumber;
287 objheader_t *tmp, *objheader;
288 objheader_t *objcopy;
296 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
297 /* Search local transaction cache */
299 return &objheader[1];
303 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
304 /* Look up in machine lookup table and copy into cache*/
305 GETSIZE(size, objheader);
306 size += sizeof(objheader_t);
307 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
308 memcpy(objcopy, objheader, size);
309 /* Insert into cache's lookup table */
311 chashInsert(record->lookupTable, OID(objheader), objcopy);
317 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) {
318 /* Look up in prefetch cache */
320 size+=sizeof(objheader_t);
321 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
322 memcpy(objcopy, tmp, size);
323 /* Insert into cache's lookup table */
324 chashInsert(record->lookupTable, OID(tmp), objcopy);
331 /* Get the object from the remote location */
332 if((machinenumber = lhashSearch(oid)) == 0) {
333 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
336 objcopy = getRemoteObj(record, machinenumber, oid);
338 if(objcopy == NULL) {
339 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
352 /* This function creates objects in the transaction record */
353 objheader_t *transCreateObj(transrecord_t *record, unsigned int size) {
354 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
355 OID(tmp) = getNewOID();
359 chashInsert(record->lookupTable, OID(tmp), tmp);
362 return &tmp[1]; //want space after object header
368 /* This function creates machine piles based on all machines involved in a
369 * transaction commit request */
370 plistnode_t *createPiles(transrecord_t *record) {
372 plistnode_t *pile = NULL;
373 unsigned int machinenum;
374 objheader_t *headeraddr;
375 chashlistnode_t * ptr = record->lookupTable->table;
376 /* Represents number of bins in the chash table */
377 unsigned int size = record->lookupTable->size;
379 for(i = 0; i < size ; i++) {
380 chashlistnode_t * curr = &ptr[i];
381 /* Inner loop to traverse the linked list of the cache lookupTable */
382 while(curr != NULL) {
383 //if the first bin in hash table is empty
387 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
388 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
392 //Get machine location for object id (and whether local or not)
393 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
394 machinenum = myIpAddr;
395 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
396 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
400 //Make machine groups
401 pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements);
408 /* This function initiates the transaction commit process
409 * Spawns threads for each of the new connections with Participants
410 * and creates new piles by calling the createPiles(),
411 * Sends a transrequest() to each remote machines for objects found remotely
412 * and calls handleLocalReq() to process objects found locally */
413 int transCommit(transrecord_t *record) {
414 unsigned int tot_bytes_mod, *listmid;
415 plistnode_t *pile, *pile_ptr;
417 int pilecount, offset, threadnum, trecvcount;
419 char transid[TID_LEN];
420 trans_req_data_t *tosend;
421 trans_commit_data_t transinfo;
422 static int newtid = 0;
423 char treplyctrl, treplyretry; /* keeps track of the common response that needs to be sent */
424 thread_data_array_t *thread_data_array;
425 local_thread_data_array_t *ltdata;
433 thread_data_array = NULL;
436 /* Look through all the objects in the transaction record and make piles
437 * for each machine involved in the transaction*/
439 pile_ptr = pile = createPiles(record);
444 /* Create the packet to be sent in TRANS_REQUEST */
446 /* Count the number of participants */
447 pilecount = pCount(pile);
449 /* Create a list of machine ids(Participants) involved in transaction */
450 listmid = calloc(pilecount, sizeof(unsigned int));
451 pListMid(pile, listmid);
454 /* Initialize thread variables,
455 * Spawn a thread for each Participant involved in a transaction */
456 pthread_t thread[pilecount];
458 pthread_cond_t tcond;
459 pthread_mutex_t tlock;
460 pthread_mutex_t tlshrd;
462 thread_data_array = (thread_data_array_t *) calloc(pilecount, sizeof(thread_data_array_t));
464 ltdata = calloc(1, sizeof(local_thread_data_array_t));
466 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
468 /* Initialize and set thread detach attribute */
469 pthread_attr_init(&attr);
470 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
471 pthread_mutex_init(&tlock, NULL);
472 pthread_cond_init(&tcond, NULL);
474 /* Process each machine pile */
475 while(pile != NULL) {
476 //Create transaction id
478 tosend = calloc(1, sizeof(trans_req_data_t));
479 tosend->f.control = TRANS_REQUEST;
480 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
481 tosend->f.mcount = pilecount;
482 tosend->f.numread = pile->numread;
483 tosend->f.nummod = pile->nummod;
484 tosend->f.numcreated = pile->numcreated;
485 tosend->f.sum_bytes = pile->sum_bytes;
486 tosend->listmid = listmid;
487 tosend->objread = pile->objread;
488 tosend->oidmod = pile->oidmod;
489 tosend->oidcreated = pile->oidcreated;
490 thread_data_array[threadnum].thread_id = threadnum;
491 thread_data_array[threadnum].mid = pile->mid;
492 thread_data_array[threadnum].buffer = tosend;
493 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
494 thread_data_array[threadnum].threshold = &tcond;
495 thread_data_array[threadnum].lock = &tlock;
496 thread_data_array[threadnum].count = &trecvcount;
497 thread_data_array[threadnum].replyctrl = &treplyctrl;
498 thread_data_array[threadnum].replyretry = &treplyretry;
499 thread_data_array[threadnum].rec = record;
500 /* If local do not create any extra connection */
501 if(pile->mid != myIpAddr) { /* Not local */
503 rc = pthread_create(&thread[threadnum], &attr, transRequest, (void *) &thread_data_array[threadnum]);
506 perror("Error in pthread create\n");
507 pthread_cond_destroy(&tcond);
508 pthread_mutex_destroy(&tlock);
511 for (i = 0; i < threadnum; i++)
512 free(thread_data_array[i].buffer);
513 free(thread_data_array);
518 ltdata->tdata = &thread_data_array[threadnum];
519 ltdata->transinfo = &transinfo;
521 val = pthread_create(&thread[threadnum], &attr, handleLocalReq, (void *) ltdata);
524 perror("Error in pthread create\n");
525 pthread_cond_destroy(&tcond);
526 pthread_mutex_destroy(&tlock);
529 for (i = 0; i < threadnum; i++)
530 free(thread_data_array[i].buffer);
531 free(thread_data_array);
540 /* Free attribute and wait for the other threads */
541 pthread_attr_destroy(&attr);
543 for (i = 0; i < threadnum; i++) {
544 rc = pthread_join(thread[i], NULL);
547 printf("Error: return code from pthread_join() is %d\n", rc);
548 pthread_cond_destroy(&tcond);
549 pthread_mutex_destroy(&tlock);
552 for (j = i; j < threadnum; j++) {
553 free(thread_data_array[j].buffer);
557 free(thread_data_array[i].buffer);
561 pthread_cond_destroy(&tcond);
562 pthread_mutex_destroy(&tlock);
568 /* wait a random amount of time before retrying to commit transaction*/
570 free(thread_data_array);
575 /* Retry trans commit procedure during soft_abort case */
576 } while (treplyretry);
578 if(treplyctrl == TRANS_ABORT) {
580 objstrDelete(record->cache);
581 chashDelete(record->lookupTable);
583 free(thread_data_array);
586 } else if(treplyctrl == TRANS_COMMIT) {
588 objstrDelete(record->cache);
589 chashDelete(record->lookupTable);
591 free(thread_data_array);
595 //TODO Add other cases
596 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
602 /* This function sends information involved in the transaction request
603 * to participants and accepts a response from particpants.
604 * It calls decideresponse() to decide on what control message
605 * to send next to participants and sends the message using sendResponse()*/
606 void *transRequest(void *threadarg) {
608 struct sockaddr_in serv_addr;
609 thread_data_array_t *tdata;
610 objheader_t *headeraddr;
611 char control, recvcontrol;
612 char machineip[16], retval;
614 tdata = (thread_data_array_t *) threadarg;
616 /* Send Trans Request */
617 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
618 perror("Error in socket for TRANS_REQUEST\n");
621 bzero((char*) &serv_addr, sizeof(serv_addr));
622 serv_addr.sin_family = AF_INET;
623 serv_addr.sin_port = htons(LISTEN_PORT);
624 serv_addr.sin_addr.s_addr = htonl(tdata->mid);
626 /* Open Connection */
627 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
628 perror("Error in connect for TRANS_REQUEST\n");
633 /* Send bytes of data with TRANS_REQUEST control message */
634 send_data(sd, &(tdata->buffer->f), sizeof(fixed_data_t));
636 /* Send list of machines involved in the transaction */
638 int size=sizeof(unsigned int)*tdata->buffer->f.mcount;
639 send_data(sd, tdata->buffer->listmid, size);
642 /* Send oids and version number tuples for objects that are read */
644 int size=(sizeof(unsigned int)+sizeof(unsigned short))*tdata->buffer->f.numread;
645 send_data(sd, tdata->buffer->objread, size);
648 /* Send objects that are modified */
649 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
651 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
652 GETSIZE(size,headeraddr);
653 size+=sizeof(objheader_t);
654 send_data(sd, headeraddr, size);
657 /* Read control message from Participant */
658 recv_data(sd, &control, sizeof(char));
659 /* Recv Objects if participant sends TRANS_DISAGREE */
660 if(control == TRANS_DISAGREE) {
662 recv_data(sd, &length, sizeof(int));
664 pthread_mutex_lock(&prefetchcache_mutex);
665 if ((newAddr = objstrAlloc(prefetchcache, length)) == NULL) {
666 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
670 pthread_mutex_unlock(&prefetchcache_mutex);
671 recv_data(sd, newAddr, length);
674 unsigned int oidToPrefetch;
675 objheader_t * header;
676 header = (objheader_t *) (((char *)newAddr) + offset);
677 oidToPrefetch = OID(header);
679 GETSIZE(size, header);
680 size += sizeof(objheader_t);
681 //make an entry in prefetch hash table
683 if((oldptr = prehashSearch(oidToPrefetch)) != NULL) {
684 prehashRemove(oidToPrefetch);
685 prehashInsert(oidToPrefetch, header);
687 prehashInsert(oidToPrefetch, header);
689 length = length - size;
693 recvcontrol = control;
694 /* Update common data structure and increment count */
695 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
697 /* Lock and update count */
698 /* Thread sleeps until all messages from pariticipants are received by coordinator */
699 pthread_mutex_lock(tdata->lock);
701 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
703 /* Wake up the threads and invoke decideResponse (once) */
704 if(*(tdata->count) == tdata->buffer->f.mcount) {
705 decideResponse(tdata);
706 pthread_cond_broadcast(tdata->threshold);
708 pthread_cond_wait(tdata->threshold, tdata->lock);
710 pthread_mutex_unlock(tdata->lock);
712 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT
713 * to all participants in their respective socket */
714 if (sendResponse(tdata, sd) == 0) {
715 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
720 recv_data((int)sd, &control, sizeof(char));
722 if(control == TRANS_UNSUCESSFUL) {
723 //printf("DEBUG-> TRANS_ABORTED\n");
724 } else if(control == TRANS_SUCESSFUL) {
725 //printf("DEBUG-> TRANS_SUCCESSFUL\n");
727 //printf("DEBUG-> Error: Incorrect Transaction End Message %d\n", control);
730 /* Close connection */
735 /* This function decides the reponse that needs to be sent to
736 * all Participant machines after the TRANS_REQUEST protocol */
737 void decideResponse(thread_data_array_t *tdata) {
739 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
742 for (i = 0 ; i < tdata->buffer->f.mcount; i++) {
743 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
744 written onto the shared array */
747 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
748 /* treat as disagree, pass thru */
757 case TRANS_SOFT_ABORT:
763 if(transdisagree > 0) {
765 *(tdata->replyctrl) = TRANS_ABORT;
766 *(tdata->replyretry) = 0;
767 /* clear objects from prefetch cache */
768 for (i = 0; i < tdata->buffer->f.numread; i++) {
769 prehashRemove(*((unsigned int *)(((char *)tdata->buffer->objread) + (sizeof(unsigned int) + sizeof(unsigned short))*i)));
771 for (i = 0; i < tdata->buffer->f.nummod; i++) {
772 prehashRemove(tdata->buffer->oidmod[i]);
774 } else if(transagree == tdata->buffer->f.mcount){
776 *(tdata->replyctrl) = TRANS_COMMIT;
777 *(tdata->replyretry) = 0;
778 /* update prefetch cache */
779 /* For objects read */
783 if((retval = updatePrefetchCache(tdata, tdata->buffer->f.numread, oidType)) != 0) {
784 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
788 if((retval = updatePrefetchCache(tdata, tdata->buffer->f.nummod, oidType)) != 0) {
789 printf("Error: %s() in updating prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
792 /* Invalidate objects in other machine cache */
793 if((retval = invalidateObj(tdata)) != 0) {
794 printf("Error: %s() in invalidating Objects %s, %d\n", __func__, __FILE__, __LINE__);
798 /* Send Abort in soft abort case followed by retry commiting transaction again*/
799 *(tdata->replyctrl) = TRANS_ABORT;
800 *(tdata->replyretry) = 1;
805 /* This function updates the prefetch cache when commiting objects
806 * based on the type of oid i.e. if oid is read or oid is modified
807 * Return -1 on error else returns 0
809 int updatePrefetchCache(thread_data_array_t* tdata, int numoid, char oidType) {
811 for (i = 0; i < numoid; i++) {
812 //find address object
813 objheader_t *header, *newAddr;
817 oid = *((unsigned int *)(tdata->buffer->objread + (sizeof(unsigned int) + sizeof(unsigned short))*i));
819 oid = tdata->buffer->oidmod[i];
821 header = (objheader_t *) chashSearch(tdata->rec->lookupTable, oid);
822 //copy object into prefetch cache
823 GETSIZE(size, header);
824 pthread_mutex_lock(&prefetchcache_mutex);
825 if ((newAddr = objstrAlloc(prefetchcache, (size + sizeof(objheader_t)))) == NULL) {
826 printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__);
829 pthread_mutex_unlock(&prefetchcache_mutex);
830 memcpy(newAddr, header, (size + sizeof(objheader_t)));
831 //make an entry in prefetch hash table
833 if((oldptr = prehashSearch(oid)) != NULL) {
835 prehashInsert(oid, newAddr);
837 prehashInsert(oid, newAddr);
844 /* This function sends the final response to remote machines per
845 * thread in their respective socket id It returns a char that is only
846 * needed to check the correctness of execution of this function
847 * inside transRequest()*/
849 char sendResponse(thread_data_array_t *tdata, int sd) {
850 int n, size, sum, oidcount = 0, control;
851 char *ptr, retval = 0;
852 unsigned int *oidnotfound;
854 control = *(tdata->replyctrl);
855 send_data(sd, &control, sizeof(char));
857 //TODO read missing objects during object migration
858 /* If response is a soft abort due to missing objects at the
859 Participant's side */
861 /* If the decided response is TRANS_ABORT */
862 if(*(tdata->replyctrl) == TRANS_ABORT) {
863 retval = TRANS_ABORT;
864 } else if(*(tdata->replyctrl) == TRANS_COMMIT) {
865 /* If the decided response is TRANS_COMMIT */
866 retval = TRANS_COMMIT;
872 /* This function opens a connection, places an object read request to
873 * the remote machine, reads the control message and object if
874 * available and copies the object and its header to the local
877 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
879 struct sockaddr_in serv_addr;
883 void *objcopy = NULL;
885 int sd = getSock2(transReadSockPool, mnum);
886 char readrequest[sizeof(char)+sizeof(unsigned int)];
887 readrequest[0] = READ_REQUEST;
888 *((unsigned int *)(&readrequest[1])) = oid;
889 send_data(sd, readrequest, sizeof(readrequest));
891 /* Read response from the Participant */
892 recv_data(sd, &control, sizeof(char));
894 if (control==OBJECT_NOT_FOUND) {
897 /* Read object if found into local cache */
898 recv_data(sd, &size, sizeof(int));
899 objcopy = objstrAlloc(record->cache, size);
900 recv_data(sd, objcopy, size);
901 /* Insert into cache's lookup table */
902 chashInsert(record->lookupTable, oid, objcopy);
908 /* This function handles the local objects involved in a transaction
909 * commiting process. It also makes a decision if this local machine
910 * sends AGREE or DISAGREE or SOFT_ABORT to coordinator. Note
911 * Coordinator = local machine It wakes up the other threads from
912 * remote participants that are waiting for the coordinator's decision
913 * and based on common agreement it either commits or aborts the
914 * transaction. It also frees the memory resources */
916 void *handleLocalReq(void *threadarg) {
917 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
918 local_thread_data_array_t *localtdata;
919 int numoidnotfound = 0, numoidlocked = 0;
920 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
923 unsigned short version;
925 objheader_t *headptr;
927 localtdata = (local_thread_data_array_t *) threadarg;
929 /* Counters and arrays to formulate decision on control message to be sent */
930 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
931 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
933 numread = localtdata->tdata->buffer->f.numread;
934 /* Process each oid in the machine pile/ group per thread */
935 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
936 if (i < localtdata->tdata->buffer->f.numread) {
937 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
939 oid = *((unsigned int *)(((char *)localtdata->tdata->buffer->objread) + incr));
940 version = *((unsigned short *)(((char *)localtdata->tdata->buffer->objread) + incr + sizeof(unsigned int)));
941 } else { // Objects Modified
943 headptr = (objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i-numread]);
944 if (headptr == NULL) {
945 printf("Error: handleLocalReq() returning NULL %s, %d\n", __FILE__, __LINE__);
949 version = headptr->version;
951 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
953 /* Save the oids not found and number of oids not found for later use */
954 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
955 /* Save the oids not found and number of oids not found for later use */
956 oidnotfound[numoidnotfound] = oid;
958 } else { /* If Obj found in machine (i.e. has not moved) */
959 /* Check if Obj is locked by any previous transaction */
960 if (test_and_set(STATUSPTR(mobj))) {
961 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
963 } else {/* If versions don't match ...HARD ABORT */
965 /* Send TRANS_DISAGREE to Coordinator */
966 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
970 /* Save all object oids that are locked on this machine during this transaction request call */
971 oidlocked[numoidlocked] = OID(((objheader_t *)mobj));
973 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
975 } else { /* If versions don't match ...HARD ABORT */
977 /* Send TRANS_DISAGREE to Coordinator */
978 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
983 /* Condition to send TRANS_AGREE */
984 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
985 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
987 /* Condition to send TRANS_SOFT_ABORT */
988 if((v_matchlock > 0 && v_nomatch == 0) || (numoidnotfound > 0 && v_nomatch == 0)) {
989 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
992 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
993 * if Participant receives a TRANS_COMMIT */
994 localtdata->transinfo->objlocked = oidlocked;
995 localtdata->transinfo->objnotfound = oidnotfound;
996 localtdata->transinfo->modptr = NULL;
997 localtdata->transinfo->numlocked = numoidlocked;
998 localtdata->transinfo->numnotfound = numoidnotfound;
999 /* Lock and update count */
1000 //Thread sleeps until all messages from pariticipants are received by coordinator
1001 pthread_mutex_lock(localtdata->tdata->lock);
1002 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
1004 /* Wake up the threads and invoke decideResponse (once) */
1005 if(*(localtdata->tdata->count) == localtdata->tdata->buffer->f.mcount) {
1006 decideResponse(localtdata->tdata);
1007 pthread_cond_broadcast(localtdata->tdata->threshold);
1009 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
1011 pthread_mutex_unlock(localtdata->tdata->lock);
1012 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
1013 if(transAbortProcess(localtdata) != 0) {
1014 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
1017 } else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT) {
1018 if(transComProcess(localtdata) != 0) {
1019 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1024 if (localtdata->transinfo->objlocked != NULL) {
1025 free(localtdata->transinfo->objlocked);
1027 if (localtdata->transinfo->objnotfound != NULL) {
1028 free(localtdata->transinfo->objnotfound);
1034 /* This function completes the ABORT process if the transaction is aborting */
1035 int transAbortProcess(local_thread_data_array_t *localtdata) {
1037 unsigned int *objlocked;
1040 numlocked = localtdata->transinfo->numlocked;
1041 objlocked = localtdata->transinfo->objlocked;
1043 for (i = 0; i < numlocked; i++) {
1044 if((header = mhashSearch(objlocked[i])) == NULL) {
1045 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1048 UnLock(STATUSPTR(header));
1054 /*This function completes the COMMIT process is the transaction is commiting*/
1055 int transComProcess(local_thread_data_array_t *localtdata) {
1056 objheader_t *header, *tcptr;
1057 int i, nummod, tmpsize, numcreated, numlocked;
1058 unsigned int *oidmod, *oidcreated, *oidlocked;
1061 nummod = localtdata->tdata->buffer->f.nummod;
1062 oidmod = localtdata->tdata->buffer->oidmod;
1063 numcreated = localtdata->tdata->buffer->f.numcreated;
1064 oidcreated = localtdata->tdata->buffer->oidcreated;
1065 numlocked = localtdata->transinfo->numlocked;
1066 oidlocked = localtdata->transinfo->objlocked;
1068 for (i = 0; i < nummod; i++) {
1069 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1070 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1073 /* Copy from transaction cache -> main object store */
1074 if ((tcptr = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidmod[i]))) == NULL) {
1075 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1078 GETSIZE(tmpsize, header);
1079 char *tmptcptr = (char *) tcptr;
1080 memcpy((char*)header+sizeof(objheader_t), (char *)tmptcptr+ sizeof(objheader_t), tmpsize);
1081 header->version += 1;
1082 if(header->notifylist != NULL) {
1083 notifyAll(&header->notifylist, OID(header), header->version);
1086 /* If object is newly created inside transaction then commit it */
1087 for (i = 0; i < numcreated; i++) {
1088 if ((header = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidcreated[i]))) == NULL) {
1089 printf("Error: transComProcess() chashSearch returned NULL for oid = %x at %s, %d\n", oidcreated[i], __FILE__, __LINE__);
1092 GETSIZE(tmpsize, header);
1093 tmpsize += sizeof(objheader_t);
1094 pthread_mutex_lock(&mainobjstore_mutex);
1095 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1096 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1097 pthread_mutex_unlock(&mainobjstore_mutex);
1100 pthread_mutex_unlock(&mainobjstore_mutex);
1101 memcpy(ptrcreate, header, tmpsize);
1102 mhashInsert(oidcreated[i], ptrcreate);
1103 lhashInsert(oidcreated[i], myIpAddr);
1105 /* Unlock locked objects */
1106 for(i = 0; i < numlocked; i++) {
1107 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1108 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1111 UnLock(STATUSPTR(header));
1117 prefetchpile_t *foundLocal(char *ptr) {
1118 int ntuples = *(GET_NTUPLES(ptr));
1119 unsigned int * oidarray = GET_PTR_OID(ptr);
1120 unsigned short * endoffsets = GET_PTR_EOFF(ptr, ntuples);
1121 short * arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1122 prefetchpile_t * head=NULL;
1125 for(i=0;i<ntuples; i++) {
1126 unsigned short baseindex=(i==0)?0:endoffsets[i-1];
1127 unsigned short endindex=endoffsets[i];
1128 unsigned int oid=oidarray[i];
1133 //Look up fields locally
1134 for(newbase=baseindex;newbase<endindex;newbase++) {
1135 if (!lookupObject(&oid, arryfields[newbase]))
1137 //Ended in a null pointer...
1141 //Entire prefetch is local
1142 if (newbase==endindex&&checkoid(oid))
1144 //Add to remote requests
1145 machinenum=lhashSearch(oid);
1146 insertPile(machinenum, oid, endindex-newbase, &arryfields[newbase], &head);
1153 int checkoid(unsigned int oid) {
1154 objheader_t *header;
1155 if ((header=mhashSearch(oid))!=NULL) {
1158 } else if ((header=prehashSearch(oid))!=NULL) {
1166 int lookupObject(unsigned int * oid, short offset) {
1167 objheader_t *header;
1168 if ((header=mhashSearch(*oid))!=NULL) {
1171 } else if ((header=prehashSearch(*oid))!=NULL) {
1178 if(TYPE(header) > NUMCLASSES) {
1179 int elementsize = classsize[TYPE(header)];
1180 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1181 int length = ao->___length___;
1182 /* Check if array out of bounds */
1183 if(offset < 0 || offset >= length) {
1184 //if yes treat the object as found
1188 (*oid) = *((unsigned int *)(((char *)ao) + sizeof(struct ArrayObject) + (elementsize*offset)));
1191 (*oid) = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset));
1197 /* This function is called by the thread calling transPrefetch */
1198 void *transPrefetch(void *t) {
1200 /* lock mutex of primary prefetch queue */
1201 void *node=gettail();
1202 /* Check if the tuples are found locally, if yes then reduce them further*/
1203 /* and group requests by remote machine ids by calling the makePreGroups() */
1204 prefetchpile_t *pilehead = foundLocal(node);
1206 if (pilehead!=NULL) {
1207 // Get sock from shared pool
1208 int sd = getSock2(transPrefetchSockPool, pilehead->mid);
1210 /* Send Prefetch Request */
1211 prefetchpile_t *ptr = pilehead;
1212 while(ptr != NULL) {
1213 sendPrefetchReq(ptr, sd);
1217 /* Release socket */
1218 // freeSock(transPrefetchSockPool, pilehead->mid, sd);
1220 /* Deallocated pilehead */
1221 mcdealloc(pilehead);
1223 // Deallocate the prefetch queue pile node
1228 void sendPrefetchReqnew(prefetchpile_t *mcpilenode, int sd) {
1231 int size=sizeof(char)+sizeof(int);
1232 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1233 size += sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1238 *buf=TRANS_PREFETCH;
1241 for(tmp=mcpilenode->objpiles;tmp!=NULL;tmp=tmp->next) {
1242 int len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1245 *((unsigned int *)buf)=tmp->oid;
1246 buf+=sizeof(unsigned int);
1247 *((unsigned int *)(buf)) = myIpAddr;
1248 buf+=sizeof(unsigned int);
1249 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1250 buf+=tmp->numoffset*sizeof(short);
1253 send_data(sd, buft, size);
1257 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1262 /* Send TRANS_PREFETCH control message */
1263 control = TRANS_PREFETCH;
1264 send_data(sd, &control, sizeof(char));
1266 /* Send Oids and offsets in pairs */
1267 tmp = mcpilenode->objpiles;
1268 while(tmp != NULL) {
1269 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1270 char oidnoffset[len];
1271 char *buf=oidnoffset;
1272 *((int*)buf) = tmp->numoffset;
1274 *((unsigned int *)buf) = tmp->oid;
1275 buf+=sizeof(unsigned int);
1276 *((unsigned int *)buf) = myIpAddr;
1277 buf += sizeof(unsigned int);
1278 memcpy(buf, tmp->offset, tmp->numoffset*sizeof(short));
1279 send_data(sd, oidnoffset, len);
1283 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1285 send_data(sd, &endpair, sizeof(int));
1290 int getPrefetchResponse(int sd) {
1291 int length = 0, size = 0;
1294 void *modptr, *oldptr;
1296 recv_data((int)sd, &length, sizeof(int));
1297 size = length - sizeof(int);
1298 char recvbuffer[size];
1300 recv_data((int)sd, recvbuffer, size);
1301 control = *((char *) recvbuffer);
1302 if(control == OBJECT_FOUND) {
1303 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1304 //printf("oid %d found\n",oid);
1305 size = size - (sizeof(char) + sizeof(unsigned int));
1306 pthread_mutex_lock(&prefetchcache_mutex);
1307 if ((modptr = objstrAlloc(prefetchcache, size)) == NULL) {
1308 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1309 pthread_mutex_unlock(&prefetchcache_mutex);
1312 pthread_mutex_unlock(&prefetchcache_mutex);
1313 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1316 /* Insert the oid and its address into the prefetch hash lookup table */
1317 /* Do a version comparison if the oid exists */
1318 if((oldptr = prehashSearch(oid)) != NULL) {
1319 /* If older version then update with new object ptr */
1320 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1322 prehashInsert(oid, modptr);
1324 } else {/* Else add the object ptr to hash table*/
1325 prehashInsert(oid, modptr);
1327 /* Lock the Prefetch Cache look up table*/
1328 pthread_mutex_lock(&pflookup.lock);
1329 /* Broadcast signal on prefetch cache condition variable */
1330 pthread_cond_broadcast(&pflookup.cond);
1331 /* Unlock the Prefetch Cache look up table*/
1332 pthread_mutex_unlock(&pflookup.lock);
1333 } else if(control == OBJECT_NOT_FOUND) {
1334 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1335 /* TODO: For each object not found query DHT for new location and retrieve the object */
1336 /* Throw an error */
1337 printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1340 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1346 unsigned short getObjType(unsigned int oid) {
1347 objheader_t *objheader;
1348 unsigned short numoffset[] ={0};
1349 short fieldoffset[] ={};
1351 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL) {
1352 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1353 prefetch(1, &oid, numoffset, fieldoffset);
1354 pthread_mutex_lock(&pflookup.lock);
1355 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL) {
1356 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1358 pthread_mutex_unlock(&pflookup.lock);
1362 return TYPE(objheader);
1365 int startRemoteThread(unsigned int oid, unsigned int mid)
1368 struct sockaddr_in remoteAddr;
1369 char msg[1 + sizeof(unsigned int)];
1373 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1375 perror("startRemoteThread():socket()");
1379 bzero(&remoteAddr, sizeof(remoteAddr));
1380 remoteAddr.sin_family = AF_INET;
1381 remoteAddr.sin_port = htons(LISTEN_PORT);
1382 remoteAddr.sin_addr.s_addr = htonl(mid);
1384 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1386 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1387 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1392 msg[0] = START_REMOTE_THREAD;
1393 *((unsigned int *) &msg[1]) = oid;
1394 send_data(sock, msg, 1 + sizeof(unsigned int));
1401 //TODO: when reusing oids, make sure they are not already in use!
1402 static unsigned int id = 0xFFFFFFFF;
1403 unsigned int getNewOID(void) {
1405 if (id > oidMax || id < oidMin)
1412 int processConfigFile()
1415 const int maxLineLength = 200;
1416 char lineBuffer[maxLineLength];
1418 const char *delimiters = " \t\n";
1422 configFile = fopen(CONFIG_FILENAME, "r");
1423 if (configFile == NULL)
1425 printf("error opening %s:\n", CONFIG_FILENAME);
1430 numHostsInSystem = 0;
1431 sizeOfHostArray = 8;
1432 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1434 while(fgets(lineBuffer, maxLineLength, configFile) != NULL)
1436 commentBegin = strchr(lineBuffer, '#');
1437 if (commentBegin != NULL)
1438 *commentBegin = '\0';
1439 token = strtok(lineBuffer, delimiters);
1440 while (token != NULL)
1442 tmpAddr = inet_addr(token);
1443 if ((int)tmpAddr == -1)
1445 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1450 addHost(htonl(tmpAddr));
1451 token = strtok(NULL, delimiters);
1457 if (numHostsInSystem < 1)
1459 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1463 myIpAddr = getMyIpAddr("en1");
1465 myIpAddr = getMyIpAddr("eth0");
1467 myIndexInHostArray = findHost(myIpAddr);
1468 if (myIndexInHostArray == -1)
1470 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1473 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1474 oidMin = oidsPerBlock * myIndexInHostArray;
1475 if (myIndexInHostArray == numHostsInSystem - 1)
1476 oidMax = 0xFFFFFFFF;
1478 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1483 void addHost(unsigned int hostIp)
1485 unsigned int *tmpArray;
1487 if (findHost(hostIp) != -1)
1490 if (numHostsInSystem == sizeOfHostArray)
1492 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1493 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1495 hostIpAddrs = tmpArray;
1498 hostIpAddrs[numHostsInSystem++] = hostIp;
1503 int findHost(unsigned int hostIp)
1506 for (i = 0; i < numHostsInSystem; i++)
1507 if (hostIpAddrs[i] == hostIp)
1514 /* This function sends notification request per thread waiting on object(s) whose version
1516 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1518 objheader_t *objheader;
1519 struct sockaddr_in remoteAddr;
1520 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1524 unsigned short version;
1525 unsigned int oid,mid;
1526 static unsigned int threadid = 0;
1527 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1528 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1529 notifydata_t *ndata;
1532 if((mid = lhashSearch(oid)) == 0) {
1533 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1537 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1538 perror("reqNotify():socket()");
1542 bzero(&remoteAddr, sizeof(remoteAddr));
1543 remoteAddr.sin_family = AF_INET;
1544 remoteAddr.sin_port = htons(LISTEN_PORT);
1545 remoteAddr.sin_addr.s_addr = htonl(mid);
1547 /* Generate unique threadid */
1550 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1551 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1552 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1555 ndata->numoid = numoid;
1556 ndata->threadid = threadid;
1557 ndata->oidarry = oidarry;
1558 ndata->versionarry = versionarry;
1559 ndata->threadcond = threadcond;
1560 ndata->threadnotify = threadnotify;
1561 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1562 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1567 /* Send number of oids, oidarry, version array, machine id and threadid */
1568 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1569 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1570 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1574 msg[0] = THREAD_NOTIFY_REQUEST;
1575 *((unsigned int *)(&msg[1])) = numoid;
1576 /* Send array of oids */
1577 size = sizeof(unsigned int);
1582 *((unsigned int *)(&msg[1] + size)) = oid;
1583 size += sizeof(unsigned int);
1588 /* Send array of version */
1592 version = versionarry[i];
1593 *((unsigned short *)(&msg[1] + size)) = version;
1594 size += sizeof(unsigned short);
1599 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1600 size += sizeof(unsigned int);
1601 *((unsigned int *)(&msg[1] + size)) = threadid;
1602 pthread_mutex_lock(&(ndata->threadnotify));
1603 size = 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int);
1604 send_data(sock, msg, size);
1605 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
1606 pthread_mutex_unlock(&(ndata->threadnotify));
1609 pthread_cond_destroy(&threadcond);
1610 pthread_mutex_destroy(&threadnotify);
1616 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
1617 notifydata_t *ndata;
1618 int i, objIsFound = 0, index;
1621 //Look up the tid and call the corresponding pthread_cond_signal
1622 if((ndata = notifyhashSearch(tid)) == NULL) {
1623 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
1626 for(i = 0; i < ndata->numoid; i++) {
1627 if(ndata->oidarry[i] == oid){
1632 if(objIsFound == 0){
1633 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
1636 if(version <= ndata->versionarry[index]){
1637 printf("threadNotify(): New version %d has not changed since last version %s, %d\n", version, __FILE__, __LINE__);
1640 /* Clear from prefetch cache and free thread related data structure */
1641 if((ptr = prehashSearch(oid)) != NULL) {
1644 pthread_cond_signal(&(ndata->threadcond));
1651 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
1654 struct sockaddr_in remoteAddr;
1655 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
1656 int sock, status, size, bytesSent;
1658 while(*head != NULL) {
1661 //create a socket connection to that machine
1662 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1663 perror("notifyAll():socket()");
1667 bzero(&remoteAddr, sizeof(remoteAddr));
1668 remoteAddr.sin_family = AF_INET;
1669 remoteAddr.sin_port = htons(LISTEN_PORT);
1670 remoteAddr.sin_addr.s_addr = htonl(mid);
1671 //send Thread Notify response and threadid to that machine
1672 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1673 printf("notifyAll():error %d connecting to %s:%d\n", errno,
1674 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1678 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
1679 msg[0] = THREAD_NOTIFY_RESPONSE;
1680 *((unsigned int *)&msg[1]) = oid;
1681 size = sizeof(unsigned int);
1682 *((unsigned short *)(&msg[1]+ size)) = version;
1683 size+= sizeof(unsigned short);
1684 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
1686 size = 1 + 2*sizeof(unsigned int) + sizeof(unsigned short);
1687 send_data(sock, msg, size);
1698 void transAbort(transrecord_t *trans) {
1699 objstrDelete(trans->cache);
1700 chashDelete(trans->lookupTable);