4 #include "machinepile.h"
9 #include "threadnotify.h"
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <sys/types.h>
25 #define LISTEN_PORT 2156
27 #define PREFETCH_CACHE_SIZE 1048576 //1MB
28 #define NUM_MACHINES 2
29 #define CONFIG_FILENAME "dstm.conf"
31 /* Global Variables */
32 extern int classsize[];
33 extern primarypfq_t pqueue; //Shared prefetch queue
34 extern mcpileq_t mcqueue; //Shared queue containing prefetch requests sorted by remote machineids
35 objstr_t *prefetchcache; //Global Prefetch cache
36 pthread_mutex_t prefetchcache_mutex;// Mutex to lock Prefetch Cache
37 pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */
38 extern pthread_mutex_t mainobjstore_mutex;// Mutex to lock main Object store
39 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
40 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
41 pthread_t tPrefetch; /* Primary Prefetch thread that processes the prefetch queue */
42 extern objstr_t *mainobjstore;
43 unsigned int myIpAddr;
44 unsigned int *hostIpAddrs;
47 int myIndexInHostArray;
48 unsigned int oidsPerBlock;
52 /* Global variables to track mapping of socketid and remote mid */
53 midSocketInfo_t midSocketArray[NUM_MACHINES];
57 void printhex(unsigned char *, int);
58 plistnode_t *createPiles(transrecord_t *);
60 void printhex(unsigned char *ptr, int numBytes)
63 for (i = 0; i < numBytes; i++)
66 printf("0%x ", ptr[i]);
68 printf("%x ", ptr[i]);
74 inline int arrayLength(int *array) {
76 for(i=0 ;array[i] != -1; i++)
80 inline int findmax(int *array, int arraylength) {
83 for(i = 0; i < arraylength; i++){
90 /* This function is a prefetch call generated by the compiler that
91 * populates the shared primary prefetch queue*/
92 void prefetch(int ntuples, unsigned int *oids, unsigned short *endoffsets, short *arrayfields) {
97 /* Allocate for the queue node*/
100 qnodesize = sizeof(prefetchqelem_t) + sizeof(int) + ntuples * (sizeof(unsigned short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
101 if((node = calloc(1, qnodesize)) == NULL) {
102 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
105 /* Set queue node values */
106 len = sizeof(prefetchqelem_t);
107 memcpy(node + len, &ntuples, sizeof(int));
109 memcpy(node + len, oids, ntuples*sizeof(unsigned int));
110 len += ntuples * sizeof(unsigned int);
111 memcpy(node + len, endoffsets, ntuples*sizeof(unsigned short));
112 len += ntuples * sizeof(unsigned short);
113 memcpy(node + len, arrayfields, endoffsets[ntuples-1]*sizeof(short));
114 /* Lock and insert into primary prefetch queue */
115 pthread_mutex_lock(&pqueue.qlock);
116 pre_enqueue((prefetchqelem_t *)node);
117 pthread_cond_signal(&pqueue.qcond);
118 pthread_mutex_unlock(&pqueue.qlock);
122 /* This function starts up the transaction runtime. */
123 int dstmStartup(const char * option) {
124 pthread_t thread_Listen;
126 int master=option!=NULL && strcmp(option, "master")==0;
128 if (processConfigFile() != 0)
129 return 0; //TODO: return error value, cause main program to exit
141 pthread_attr_init(&attr);
142 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
143 pthread_create(&thread_Listen, &attr, dstmListen, NULL);
152 //TODO Use this later
153 void *pCacheAlloc(objstr_t *store, unsigned int size) {
159 while(ptr->next != NULL) {
160 /* check if store is empty */
161 if(((unsigned int)ptr->top - (unsigned int)ptr - sizeof(objstr_t) + size) <= ptr->size) {
176 /* This function initiates the prefetch thread
177 * A queue is shared between the main thread of execution
178 * and the prefetch thread to process the prefetch call
179 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
180 * processes the prefetch requests */
184 //Create and initialize prefetch cache structure
185 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
186 //prefetchcache->next = objstrCreate(PREFETCH_CACHE_SIZE);
187 //prefetchcache->next->next = objstrCreate(PREFETCH_CACHE_SIZE);
189 /* Initialize attributes for mutex */
190 pthread_mutexattr_init(&prefetchcache_mutex_attr);
191 pthread_mutexattr_settype(&prefetchcache_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
193 pthread_mutex_init(&prefetchcache_mutex, &prefetchcache_mutex_attr);
195 //Create prefetch cache lookup table
196 if(prehashCreate(HASH_SIZE, LOADFACTOR))
199 //Initialize primary shared queue
201 //Initialize machine pile w/prefetch oids and offsets shared queue
204 //Create the primary prefetch thread
206 retval=pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
208 pthread_detach(tPrefetch);
210 //Initialize mid to socketid mapping array
211 for(t = 0; t < NUM_MACHINES; t++) {
212 midSocketArray[t].mid = 0;
213 midSocketArray[t].sockid = 0;
216 //Create and Initialize a pool of threads
217 /* Threads are active for the entire period runtime is running */
218 for(t = 0; t< NUM_THREADS; t++) {
220 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
222 pthread_detach(wthreads[t]);
226 /* This function stops the threads spawned */
229 pthread_cancel(tPrefetch);
230 for(t = 0; t < NUM_THREADS; t++)
231 pthread_cancel(wthreads[t]);
236 /* This functions inserts randowm wait delays in the order of msec
237 * Mostly used when transaction commits retry*/
245 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
246 nanosleep(&req, NULL);
250 /* This function initializes things required in the transaction start*/
251 transrecord_t *transStart()
253 transrecord_t *tmp = calloc(1, sizeof(transrecord_t));
254 tmp->cache = objstrCreate(1048576);
255 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
257 tmp->revertlist=NULL;
262 /* This function finds the location of the objects involved in a transaction
263 * and returns the pointer to the object if found in a remote location */
264 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
265 unsigned int machinenumber;
266 objheader_t *tmp, *objheader;
267 objheader_t *objcopy;
268 int size, rc, found = 0;
274 printf("Error: %s, %d oid is NULL \n", __FILE__, __LINE__);
278 rc = gettimeofday(&tp, NULL);
282 if (tp.tv_usec >= 1000000)
284 tp.tv_usec -= 1000000;
287 /* Convert from timeval to timespec */
288 ts.tv_sec = tp.tv_sec;
289 ts.tv_nsec = tp.tv_usec * 1000;
291 /* Search local transaction cache */
292 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
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 */
306 chashInsert(record->lookupTable, OID(objheader), objcopy);
312 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* 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 /*If object not found in prefetch cache then block until object appears in the prefetch cache */
327 pthread_mutex_lock(&pflookup.lock);
329 rc = pthread_cond_timedwait(&pflookup.cond, &pflookup.lock, &ts);
330 // Check Prefetch cache again
331 if((tmp =(objheader_t *) prehashSearch(oid)) != NULL) {
334 size+=sizeof(objheader_t);
335 objcopy = (objheader_t *) objstrAlloc(record->cache, size);
336 memcpy(objcopy, tmp, size);
337 chashInsert(record->lookupTable, OID(tmp), objcopy);
338 pthread_mutex_unlock(&pflookup.lock);
344 } else if (rc == ETIMEDOUT) {
345 pthread_mutex_unlock(&pflookup.lock);
351 /* Get the object from the remote location */
352 if((machinenumber = lhashSearch(oid)) == 0) {
353 printf("Error: %s() No machine found for oid =% %s,%dx\n",__func__, machinenumber, __FILE__, __LINE__);
356 objcopy = getRemoteObj(record, machinenumber, oid);
358 if(objcopy == NULL) {
359 printf("Error: Object not found in Remote location %s, %d\n", __FILE__, __LINE__);
372 /* This function creates objects in the transaction record */
373 objheader_t *transCreateObj(transrecord_t *record, unsigned int size)
375 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + size));
376 tmp->notifylist = NULL;
377 OID(tmp) = getNewOID();
381 chashInsert(record->lookupTable, OID(tmp), tmp);
384 return &tmp[1]; //want space after object header
390 /* This function creates machine piles based on all machines involved in a
391 * transaction commit request */
392 plistnode_t *createPiles(transrecord_t *record) {
394 unsigned int size;/* Represents number of bins in the chash table */
395 chashlistnode_t *curr, *ptr, *next;
396 plistnode_t *pile = NULL;
397 unsigned int machinenum;
398 void *localmachinenum;
399 objheader_t *headeraddr;
401 ptr = record->lookupTable->table;
402 size = record->lookupTable->size;
404 for(i = 0; i < size ; i++) {
406 /* Inner loop to traverse the linked list of the cache lookupTable */
407 while(curr != NULL) {
408 //if the first bin in hash table is empty
414 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
415 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
419 //Get machine location for object id (and whether local or not)
420 if (STATUS(headeraddr) & NEW || (mhashSearch(curr->key) != NULL)) {
421 machinenum = myIpAddr;
422 } else if ((machinenum = lhashSearch(curr->key)) == 0) {
423 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
427 //Make machine groups
428 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
429 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
439 /* This function initiates the transaction commit process
440 * Spawns threads for each of the new connections with Participants
441 * and creates new piles by calling the createPiles(),
442 * Sends a transrequest() to each remote machines for objects found remotely
443 * and calls handleLocalReq() to process objects found locally */
444 int transCommit(transrecord_t *record) {
445 unsigned int tot_bytes_mod, *listmid;
446 plistnode_t *pile, *pile_ptr;
448 int pilecount, offset, threadnum = 0, trecvcount = 0;
450 char transid[TID_LEN];
451 trans_req_data_t *tosend;
452 trans_commit_data_t transinfo;
453 static int newtid = 0;
454 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
456 thread_data_array_t *thread_data_array;
457 local_thread_data_array_t *ltdata;
463 thread_data_array = NULL;
466 /* Look through all the objects in the transaction record and make piles
467 * for each machine involved in the transaction*/
468 pile_ptr = pile = createPiles(record);
470 /* Create the packet to be sent in TRANS_REQUEST */
472 /* Count the number of participants */
473 pilecount = pCount(pile);
475 /* Create a list of machine ids(Participants) involved in transaction */
476 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
477 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
480 pListMid(pile, listmid);
483 /* Initialize thread variables,
484 * Spawn a thread for each Participant involved in a transaction */
485 pthread_t thread[pilecount];
487 pthread_cond_t tcond;
488 pthread_mutex_t tlock;
489 pthread_mutex_t tlshrd;
491 if((thread_data_array = (thread_data_array_t *) calloc(pilecount, sizeof(thread_data_array_t))) == NULL) {
492 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
493 pthread_cond_destroy(&tcond);
494 pthread_mutex_destroy(&tlock);
500 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
501 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
502 pthread_cond_destroy(&tcond);
503 pthread_mutex_destroy(&tlock);
506 free(thread_data_array);
510 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
512 /* Initialize and set thread detach attribute */
513 pthread_attr_init(&attr);
514 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
515 pthread_mutex_init(&tlock, NULL);
516 pthread_cond_init(&tcond, NULL);
518 /* Process each machine pile */
519 while(pile != NULL) {
520 //Create transaction id
522 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
523 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
524 pthread_cond_destroy(&tcond);
525 pthread_mutex_destroy(&tlock);
528 free(thread_data_array);
532 tosend->f.control = TRANS_REQUEST;
533 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
534 tosend->f.mcount = pilecount;
535 tosend->f.numread = pile->numread;
536 tosend->f.nummod = pile->nummod;
537 tosend->f.numcreated = pile->numcreated;
538 tosend->f.sum_bytes = pile->sum_bytes;
539 tosend->listmid = listmid;
540 tosend->objread = pile->objread;
541 tosend->oidmod = pile->oidmod;
542 tosend->oidcreated = pile->oidcreated;
543 thread_data_array[threadnum].thread_id = threadnum;
544 thread_data_array[threadnum].mid = pile->mid;
545 thread_data_array[threadnum].buffer = tosend;
546 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
547 thread_data_array[threadnum].threshold = &tcond;
548 thread_data_array[threadnum].lock = &tlock;
549 thread_data_array[threadnum].count = &trecvcount;
550 thread_data_array[threadnum].replyctrl = &treplyctrl;
551 thread_data_array[threadnum].replyretry = &treplyretry;
552 thread_data_array[threadnum].rec = record;
553 /* If local do not create any extra connection */
554 if(pile->mid != myIpAddr) { /* Not local */
556 rc = pthread_create(&thread[threadnum], &attr, transRequest, (void *) &thread_data_array[threadnum]);
559 perror("Error in pthread create\n");
560 pthread_cond_destroy(&tcond);
561 pthread_mutex_destroy(&tlock);
564 for (i = 0; i < threadnum; i++)
565 free(thread_data_array[i].buffer);
566 free(thread_data_array);
571 ltdata->tdata = &thread_data_array[threadnum];
572 ltdata->transinfo = &transinfo;
574 val = pthread_create(&thread[threadnum], &attr, handleLocalReq, (void *) ltdata);
577 perror("Error in pthread create\n");
578 pthread_cond_destroy(&tcond);
579 pthread_mutex_destroy(&tlock);
582 for (i = 0; i < threadnum; i++)
583 free(thread_data_array[i].buffer);
584 free(thread_data_array);
593 /* Free attribute and wait for the other threads */
594 pthread_attr_destroy(&attr);
596 for (i = 0; i < threadnum; i++) {
597 rc = pthread_join(thread[i], NULL);
600 printf("Error: return code from pthread_join() is %d\n", rc);
601 pthread_cond_destroy(&tcond);
602 pthread_mutex_destroy(&tlock);
605 for (j = i; j < threadnum; j++) {
606 free(thread_data_array[j].buffer);
610 free(thread_data_array[i].buffer);
614 pthread_cond_destroy(&tcond);
615 pthread_mutex_destroy(&tlock);
619 /* wait a random amount of time before retrying to commit transaction*/
620 if(treplyretry == 1) {
621 free(thread_data_array);
626 /* Retry trans commit procedure during soft_abort case */
627 } while (treplyretry == 1);
630 if(treplyctrl == TRANS_ABORT) {
632 objstrDelete(record->cache);
633 chashDelete(record->lookupTable);
635 free(thread_data_array);
638 } else if(treplyctrl == TRANS_COMMIT) {
640 objstrDelete(record->cache);
641 chashDelete(record->lookupTable);
643 free(thread_data_array);
647 //TODO Add other cases
648 printf("Error: in %s() THIS SHOULD NOT HAPPEN.....EXIT PROGRAM\n", __func__);
655 /* This function sends information involved in the transaction request
656 * to participants and accepts a response from particpants.
657 * It calls decideresponse() to decide on what control message
658 * to send next to participants and sends the message using sendResponse()*/
659 void *transRequest(void *threadarg) {
661 struct sockaddr_in serv_addr;
662 thread_data_array_t *tdata;
663 objheader_t *headeraddr;
664 char control, recvcontrol;
665 char machineip[16], retval;
667 tdata = (thread_data_array_t *) threadarg;
669 /* Send Trans Request */
670 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
671 perror("Error in socket for TRANS_REQUEST\n");
674 bzero((char*) &serv_addr, sizeof(serv_addr));
675 serv_addr.sin_family = AF_INET;
676 serv_addr.sin_port = htons(LISTEN_PORT);
677 midtoIP(tdata->mid,machineip);
678 machineip[15] = '\0';
679 serv_addr.sin_addr.s_addr = inet_addr(machineip);
680 /* Open Connection */
681 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
682 perror("Error in connect for TRANS_REQUEST\n");
687 /* Send bytes of data with TRANS_REQUEST control message */
688 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
689 perror("Error sending fixed bytes for thread\n");
694 /* Send list of machines involved in the transaction */
696 int size=sizeof(unsigned int)*tdata->buffer->f.mcount;
697 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
698 perror("Error sending list of machines for thread\n");
704 /* Send oids and version number tuples for objects that are read */
706 int size=(sizeof(unsigned int)+sizeof(unsigned short))*tdata->buffer->f.numread;
708 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
709 perror("Error sending tuples for thread\n");
715 /* Send objects that are modified */
716 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
718 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
719 GETSIZE(size,headeraddr);
720 size+=sizeof(objheader_t);
721 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
722 perror("Error sending obj modified for thread\n");
728 /* Read control message from Participant */
729 if((n = read(sd, &control, sizeof(char))) <= 0) {
730 perror("Error in reading control message from Participant\n");
735 recvcontrol = control;
737 /* Update common data structure and increment count */
738 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
740 /* Lock and update count */
741 /* Thread sleeps until all messages from pariticipants are received by coordinator */
742 pthread_mutex_lock(tdata->lock);
744 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
746 /* Wake up the threads and invoke decideResponse (once) */
747 if(*(tdata->count) == tdata->buffer->f.mcount) {
748 decideResponse(tdata);
749 pthread_cond_broadcast(tdata->threshold);
751 pthread_cond_wait(tdata->threshold, tdata->lock);
753 pthread_mutex_unlock(tdata->lock);
755 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
756 * to all participants in their respective socket */
757 if (sendResponse(tdata, sd) == 0) {
758 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
764 retval = recv((int)sd, &control, sizeof(char), 0);
765 } while (retval < sizeof(char));
767 if(control == TRANS_UNSUCESSFUL) {
768 //printf("DEBUG-> TRANS_ABORTED\n");
769 } else if(control == TRANS_SUCESSFUL) {
770 //printf("DEBUG-> TRANS_SUCCESSFUL\n");
772 //printf("DEBUG-> Error: Incorrect Transaction End Message %d\n", control);
775 /* Close connection */
780 /* This function decides the reponse that needs to be sent to
781 * all Participant machines after the TRANS_REQUEST protocol */
782 void decideResponse(thread_data_array_t *tdata) {
784 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
787 for (i = 0 ; i < tdata->buffer->f.mcount; i++) {
788 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
789 written onto the shared array */
792 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
793 /* treat as disagree, pass thru */
802 case TRANS_SOFT_ABORT:
808 if(transdisagree > 0) {
810 *(tdata->replyctrl) = TRANS_ABORT;
811 *(tdata->replyretry) = 0;
812 /* clear objects from prefetch cache */
813 for (i = 0; i < tdata->buffer->f.numread; i++)
814 prehashRemove(*((unsigned int *)(((char *)tdata->buffer->objread) + (sizeof(unsigned int) + sizeof(unsigned short))*i)));
815 for (i = 0; i < tdata->buffer->f.nummod; i++)
816 prehashRemove(tdata->buffer->oidmod[i]);
817 } else if(transagree == tdata->buffer->f.mcount){
819 *(tdata->replyctrl) = TRANS_COMMIT;
820 *(tdata->replyretry) = 0;
822 /* Send Abort in soft abort case followed by retry commiting transaction again*/
823 *(tdata->replyctrl) = TRANS_ABORT;
824 *(tdata->replyretry) = 1;
829 /* This function sends the final response to remote machines per thread in their respective socket id
830 * It returns a char that is only needed to check the correctness of execution of this function inside
832 char sendResponse(thread_data_array_t *tdata, int sd) {
833 int n, N, sum, oidcount = 0, control;
834 char *ptr, retval = 0;
835 unsigned int *oidnotfound;
837 control = *(tdata->replyctrl);
838 if (send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
839 perror("Error sending ctrl message for participant\n");
843 //FIXME read missing objects
844 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
846 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
847 // Read list of objects missing
848 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
849 N = oidcount * sizeof(unsigned int);
850 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
851 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
854 ptr = (char *) oidnotfound;
856 n = read(sd, ptr+sum, N-sum);
858 } while(sum < N && n !=0);
860 retval = TRANS_SOFT_ABORT;
864 /* If the decided response is TRANS_ABORT */
865 if(*(tdata->replyctrl) == TRANS_ABORT) {
866 retval = TRANS_ABORT;
867 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
868 retval = TRANS_COMMIT;
874 /* This function opens a connection, places an object read request to the
875 * remote machine, reads the control message and object if available and
876 * copies the object and its header to the local cache.
879 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
881 struct sockaddr_in serv_addr;
887 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
888 perror("Error in socket\n");
892 bzero((char*) &serv_addr, sizeof(serv_addr));
893 serv_addr.sin_family = AF_INET;
894 serv_addr.sin_port = htons(LISTEN_PORT);
895 midtoIP(mnum,machineip);
896 machineip[15] = '\0';
897 serv_addr.sin_addr.s_addr = inet_addr(machineip);
900 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
901 perror("getRemoteObj() Error in connect\n");
905 char readrequest[sizeof(char)+sizeof(unsigned int)];
906 readrequest[0] = READ_REQUEST;
907 *((unsigned int *)(&readrequest[1])) = oid;
908 if (send(sd, readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
909 perror("getRemoteObj(): error sending message\n");
913 /* Read response from the Participant */
914 if((val = read(sd, &control, sizeof(char))) <= 0) {
915 printf("getRemoteObj(): error no response, %d\n", val);
920 case OBJECT_NOT_FOUND:
923 /* Read object if found into local cache */
924 if((val = read(sd, &size, sizeof(int))) <= 0) {
925 perror("getRemoteObj(): error in reading size\n");
928 objcopy = objstrAlloc(record->cache, size);
931 sum += read(sd, (char *)objcopy+sum, size-sum);
933 /* Insert into cache's lookup table */
934 chashInsert(record->lookupTable, oid, objcopy);
937 printf("Error: in recv response from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
946 /* This function handles the local objects involved in a transaction commiting process.
947 * It also makes a decision if this local machine sends AGREE or DISAGREE or SOFT_ABORT to coordinator.
948 * Note Coordinator = local machine
949 * It wakes up the other threads from remote participants that are waiting for the coordinator's decision and
950 * based on common agreement it either commits or aborts the transaction.
951 * It also frees the memory resources */
952 void *handleLocalReq(void *threadarg) {
953 unsigned int *oidnotfound = NULL, *oidlocked = NULL;
954 local_thread_data_array_t *localtdata;
955 int objnotfound = 0, objlocked = 0;
956 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
959 unsigned short version;
961 objheader_t *headptr;
963 localtdata = (local_thread_data_array_t *) threadarg;
965 /* Counters and arrays to formulate decision on control message to be sent */
966 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
967 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
969 numread = localtdata->tdata->buffer->f.numread;
970 /* Process each oid in the machine pile/ group per thread */
971 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
972 if (i < localtdata->tdata->buffer->f.numread) {
973 int incr = sizeof(unsigned int) + sizeof(unsigned short);// Offset that points to next position in the objread array
975 oid = *((unsigned int *)(((char *)localtdata->tdata->buffer->objread) + incr));
976 version = *((unsigned short *)(((char *)localtdata->tdata->buffer->objread) + incr + sizeof(unsigned int)));
977 } else { // Objects Modified
979 headptr = (objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i-numread]);
980 if (headptr == NULL) {
981 printf("Error: handleLocalReq() returning NULL %s, %d\n", __FILE__, __LINE__);
985 version = headptr->version;
987 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
989 /* Save the oids not found and number of oids not found for later use */
990 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
991 /* Save the oids not found and number of oids not found for later use */
992 oidnotfound[objnotfound] = oid;
994 } else { /* If Obj found in machine (i.e. has not moved) */
995 /* Check if Obj is locked by any previous transaction */
996 if ((STATUS((objheader_t *)mobj) & LOCK) == LOCK) {
997 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
999 } else {/* If versions don't match ...HARD ABORT */
1001 /* Send TRANS_DISAGREE to Coordinator */
1002 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
1004 } else {/* If Obj is not locked then lock object */
1005 STATUS(((objheader_t *)mobj)) |= LOCK;
1006 /* Save all object oids that are locked on this machine during this transaction request call */
1007 oidlocked[objlocked] = OID(((objheader_t *)mobj));
1009 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1011 } else { /* If versions don't match ...HARD ABORT */
1013 /* Send TRANS_DISAGREE to Coordinator */
1014 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
1019 /* Condition to send TRANS_AGREE */
1020 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
1021 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
1023 /* Condition to send TRANS_SOFT_ABORT */
1024 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
1025 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
1028 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
1029 * if Participant receives a TRANS_COMMIT */
1030 localtdata->transinfo->objlocked = oidlocked;
1031 localtdata->transinfo->objnotfound = oidnotfound;
1032 localtdata->transinfo->modptr = NULL;
1033 localtdata->transinfo->numlocked = objlocked;
1034 localtdata->transinfo->numnotfound = objnotfound;
1035 /* Lock and update count */
1036 //Thread sleeps until all messages from pariticipants are received by coordinator
1037 pthread_mutex_lock(localtdata->tdata->lock);
1038 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
1040 /* Wake up the threads and invoke decideResponse (once) */
1041 if(*(localtdata->tdata->count) == localtdata->tdata->buffer->f.mcount) {
1042 decideResponse(localtdata->tdata);
1043 pthread_cond_broadcast(localtdata->tdata->threshold);
1045 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
1047 pthread_mutex_unlock(localtdata->tdata->lock);
1048 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
1049 if(transAbortProcess(localtdata) != 0) {
1050 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
1053 } else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT) {
1054 if(transComProcess(localtdata) != 0) {
1055 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
1060 if (localtdata->transinfo->objlocked != NULL) {
1061 free(localtdata->transinfo->objlocked);
1063 if (localtdata->transinfo->objnotfound != NULL) {
1064 free(localtdata->transinfo->objnotfound);
1070 /* This function completes the ABORT process if the transaction is aborting */
1071 int transAbortProcess(local_thread_data_array_t *localtdata) {
1073 unsigned int *objlocked;
1076 numlocked = localtdata->transinfo->numlocked;
1077 objlocked = localtdata->transinfo->objlocked;
1079 for (i = 0; i < numlocked; i++) {
1080 if((header = mhashSearch(objlocked[i])) == NULL) {
1081 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1084 STATUS(((objheader_t *)header)) &= ~(LOCK);
1090 /*This function completes the COMMIT process is the transaction is commiting*/
1091 int transComProcess(local_thread_data_array_t *localtdata) {
1092 objheader_t *header, *tcptr;
1093 int i, nummod, tmpsize, numcreated, numlocked;
1094 unsigned int *oidmod, *oidcreated, *oidlocked;
1097 nummod = localtdata->tdata->buffer->f.nummod;
1098 oidmod = localtdata->tdata->buffer->oidmod;
1099 numcreated = localtdata->tdata->buffer->f.numcreated;
1100 oidcreated = localtdata->tdata->buffer->oidcreated;
1101 numlocked = localtdata->transinfo->numlocked;
1102 oidlocked = localtdata->transinfo->objlocked;
1104 for (i = 0; i < nummod; i++) {
1105 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1106 printf("Error: transComProcess() mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1109 /* Copy from transaction cache -> main object store */
1110 if ((tcptr = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidmod[i]))) == NULL) {
1111 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1114 GETSIZE(tmpsize, header);
1115 pthread_mutex_lock(&mainobjstore_mutex);
1116 memcpy((char*)header+sizeof(objheader_t), (char *)tcptr+ sizeof(objheader_t), tmpsize);
1117 header->version += 1;
1118 if(header->notifylist != NULL) {
1119 notifyAll(&header->notifylist, OID(header), header->version);
1121 pthread_mutex_unlock(&mainobjstore_mutex);
1123 /* If object is newly created inside transaction then commit it */
1124 for (i = 0; i < numcreated; i++) {
1125 if ((header = ((objheader_t *) chashSearch(localtdata->tdata->rec->lookupTable, oidcreated[i]))) == NULL) {
1126 printf("Error: transComProcess() chashSearch returned NULL at %s, %d\n", __FILE__, __LINE__);
1129 GETSIZE(tmpsize, header);
1130 tmpsize += sizeof(objheader_t);
1131 pthread_mutex_lock(&mainobjstore_mutex);
1132 if ((ptrcreate = objstrAlloc(mainobjstore, tmpsize)) == NULL) {
1133 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1134 pthread_mutex_unlock(&mainobjstore_mutex);
1137 pthread_mutex_unlock(&mainobjstore_mutex);
1138 memcpy(ptrcreate, header, tmpsize);
1139 mhashInsert(oidcreated[i], ptrcreate);
1140 lhashInsert(oidcreated[i], myIpAddr);
1142 /* Unlock locked objects */
1143 for(i = 0; i < numlocked; i++) {
1144 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1145 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1148 STATUS(header) &= ~(LOCK);
1154 /* This function checks if the prefetch oids are same and have same offsets
1155 * for case x.a.b and y.a.b where x and y have same oid's
1156 * or if a.b.c is a subset of x.b.c.d*/
1157 /* check for case where the generated request a.y.z or x.y.z.g then
1158 * prefetch needs to be generated for x.y.z.g if oid of a and x are same*/
1159 void checkPrefetchTuples(prefetchqelem_t *node) {
1160 int i,j, count,k, sindex, index;
1162 int ntuples, slength;
1164 unsigned short *endoffsets;
1167 /* Check for the case x.y.z and a.b.c are same oids */
1168 ptr = (char *) node;
1169 ntuples = *(GET_NTUPLES(ptr));
1170 oid = GET_PTR_OID(ptr);
1171 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1172 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1174 /* Find offset length for each tuple */
1175 int numoffset[ntuples];
1176 numoffset[0] = endoffsets[0];
1177 for(i = 1; i<ntuples; i++) {
1178 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1180 /* Check for redundant tuples by comparing oids of each tuple */
1181 for(i = 0; i < ntuples; i++) {
1184 for(j = i+1 ; j < ntuples; j++) {
1187 /*If oids of tuples match */
1188 if (oid[i] == oid[j]) {
1189 /* Find the smallest offset length of two tuples*/
1190 if(numoffset[i] > numoffset[j]){
1191 slength = numoffset[j];
1195 slength = numoffset[i];
1199 /* Compare the offset values based on the current indices
1200 * break if they do not match
1201 * if all offset values match then pick the largest tuple*/
1206 k = endoffsets[i-1];
1208 index = endoffsets[j -1];
1209 for(count = 0; count < slength; count ++) {
1210 if (arryfields[k] != arryfields[index]) {
1216 if(slength == count) {
1223 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1224 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1226 int ntuples, i, machinenum, count=0;
1228 unsigned short *endoffsets;
1229 short *arryfields, *offset;
1230 prefetchpile_t *head = NULL, *tmp = NULL;
1232 /* Check for the case x.y.z and a.b.c are same oids */
1233 ptr = (char *) node;
1234 ntuples = *(GET_NTUPLES(ptr));
1235 oid = GET_PTR_OID(ptr);
1236 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1237 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1239 if((head = (prefetchpile_t *) calloc(1, sizeof(prefetchpile_t))) == NULL) {
1240 printf("Calloc error: %s %d\n", __FILE__, __LINE__);
1244 /* Check for redundant tuples by comparing oids of each tuple */
1245 for(i = 0; i < ntuples; i++) {
1247 if(head->next != NULL) {
1248 if((tmp = (prefetchpile_t *) calloc(1, sizeof(prefetchpile_t))) == NULL) {
1249 printf("Calloc error: %s %d\n", __FILE__, __LINE__);
1252 tmp->mid = myIpAddr;
1256 head->mid = myIpAddr;
1260 /* For each tuple make piles */
1261 if ((machinenum = lhashSearch(oid[i])) == 0) {
1262 printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1265 /* Insert into machine pile */
1267 offset = &arryfields[0];
1269 offset = &arryfields[endoffsets[i-1]];
1272 if((head = insertPile(machinenum, oid[i], numoffset[i], offset, head)) == NULL){
1273 printf("Error: Couldn't create a pile %s, %d\n", __FILE__, __LINE__);
1281 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1282 int ntuples,i, j, k, oidnfound = 0, arryfieldindex,nextarryfieldindex, flag = 0, val;
1286 objheader_t *objheader;
1287 unsigned short *endoffsets;
1289 prefetchpile_t *head = NULL;
1291 ptr = (char *) node;
1292 ntuples = *(GET_NTUPLES(ptr));
1293 oid = GET_PTR_OID(ptr);
1294 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1295 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1297 /* Find offset length for each tuple */
1298 int numoffset[ntuples];//Number of offsets for each tuple
1299 numoffset[0] = endoffsets[0];
1300 for(i = 1; i<ntuples; i++) {
1301 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1304 for(i = 0; i < ntuples; i++) {
1308 nextarryfieldindex = endoffsets[0];
1310 arryfieldindex = endoffsets[i-1];
1311 nextarryfieldindex = endoffsets[i];
1314 endoffsets[0] = val = numoffset[0];
1315 for(k = 1; k < ntuples; k++) {
1316 val = val + numoffset[k];
1317 endoffsets[k] = val;
1320 for(k = 0; k<endoffsets[ntuples-1]; k++) {
1321 arryfields[arryfieldindex+k] = arryfields[nextarryfieldindex+k];
1326 /* If object found locally */
1327 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1329 tmp = (char *) objheader;
1330 int orgnumoffset = numoffset[i];
1334 arryfieldindex = endoffsets[i-1];
1337 for(j = 0; j<orgnumoffset; j++) {
1338 unsigned int objoid = 0;
1339 /* Check for arrays */
1340 if(TYPE(objheader) > NUMCLASSES) {
1344 int elementsize = classsize[TYPE(objheader)];
1345 struct ArrayObject *ao = (struct ArrayObject *) (tmp + sizeof(objheader_t));
1346 unsigned short length = ao->___length___;
1347 /* Check if array out of bounds */
1348 if(arryfields[arryfieldindex] < 0 || arryfields[arryfieldindex]>= length) {
1351 objoid = *((unsigned int *)(tmp + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*arryfields[arryfieldindex])));
1353 objoid = *((unsigned int *)(tmp + sizeof(objheader_t) + arryfields[arryfieldindex]));
1355 //Update numoffset array
1356 numoffset[i] = numoffset[i] - 1;
1359 //Update endoffset array
1360 endoffsets[0] = val = numoffset[0];
1361 for(k = 1; k < ntuples; k++) {
1362 val = val + numoffset[k];
1363 endoffsets[k] = val;
1365 //Update arrayfields array
1366 for(k = 0; k < endoffsets[ntuples-1]; k++) {
1367 arryfields[arryfieldindex+k] = arryfields[arryfieldindex+k+1];
1369 if((objheader = (objheader_t*) mhashSearch(oid[i])) == NULL) {
1371 checkPreCache(node, numoffset, oid[i], i);
1374 tmp = (char *) objheader;
1377 /*If all offset oids are found locally,make the prefetch tuple invalid */
1382 /* Look in Prefetch cache */
1383 checkPreCache(node, numoffset, oid[i],i);
1387 /* Make machine groups */
1388 if((head = makePreGroups(node, numoffset)) == NULL) {
1389 printf("Error in makePreGroups() %s %d\n", __FILE__, __LINE__);
1396 void checkPreCache(prefetchqelem_t *node, int *numoffset, unsigned int objoid, int index) {
1398 int ntuples, i, k, flag=0, isArray =0, arryfieldindex, val;
1400 unsigned short *endoffsets;
1402 objheader_t *header;
1404 ptr = (char *) node;
1405 ntuples = *(GET_NTUPLES(ptr));
1406 oid = GET_PTR_OID(ptr);
1407 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1408 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1410 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1412 } else { //Found in Prefetch Cache
1413 //TODO Decide if object is too old, if old remove from cache
1414 tmp = (char *) header;
1415 int loopcount = numoffset[index];
1419 arryfieldindex = endoffsets[(index - 1)];
1420 // Check if any of the offset oid is available in the Prefetch cache
1421 for(i = 0; i < loopcount; i++) {
1422 /* Check for arrays */
1423 if(TYPE(header) > NUMCLASSES) {
1427 int elementsize = classsize[TYPE(header)];
1428 objoid = *((unsigned int *)(tmp + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*arryfields[arryfieldindex])));
1430 objoid = *((unsigned int *)(tmp + sizeof(objheader_t) + arryfields[arryfieldindex]));
1432 //Update numoffset array
1433 numoffset[index] = numoffset[index] - 1;
1435 oid[index] = objoid;
1436 //Update endoffset array
1437 endoffsets[0] = val = numoffset[0];
1438 for(k = 1; k < ntuples; k++) {
1439 val = val + numoffset[k];
1440 endoffsets[k] = val;
1442 //Update arrayfields array
1443 for(k = 0; k < endoffsets[ntuples-1]; k++) {
1444 arryfields[arryfieldindex+k] = arryfields[arryfieldindex+k+1];
1446 if((header = (objheader_t *)prehashSearch(oid[index])) != NULL) {
1447 tmp = (char *) header;
1455 //Found in the prefetch cache
1456 if(flag == 0 && (numoffset[index] == 0)) {
1463 /* This function is called by the thread calling transPrefetch */
1464 void *transPrefetch(void *t) {
1465 prefetchqelem_t *qnode;
1466 prefetchpile_t *pilehead = NULL;
1467 prefetchpile_t *ptr = NULL, *piletail = NULL;
1470 /* lock mutex of primary prefetch queue */
1471 pthread_mutex_lock(&pqueue.qlock);
1472 /* while primary queue is empty, then wait */
1473 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1474 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1477 /* dequeue node to create a machine piles and finally unlock mutex */
1478 if((qnode = pre_dequeue()) == NULL) {
1479 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1480 pthread_mutex_unlock(&pqueue.qlock);
1483 pthread_mutex_unlock(&pqueue.qlock);
1485 /* Reduce redundant prefetch requests */
1486 checkPrefetchTuples(qnode);
1487 /* Check if the tuples are found locally, if yes then reduce them further*/
1488 /* and group requests by remote machine ids by calling the makePreGroups() */
1489 if((pilehead = foundLocal(qnode)) == NULL) {
1490 printf("Error: No node created for serving prefetch request %s %d\n", __FILE__, __LINE__);
1496 while(ptr != NULL) {
1497 if(ptr->next == NULL) {
1503 /* Lock mutex of pool queue */
1504 pthread_mutex_lock(&mcqueue.qlock);
1505 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1506 mcpileenqueue(pilehead, piletail);
1507 /* Broadcast signal on machine pile queue */
1508 pthread_cond_broadcast(&mcqueue.qcond);
1509 /* Unlock mutex of machine pile queue */
1510 pthread_mutex_unlock(&mcqueue.qlock);
1511 /* Deallocate the prefetch queue pile node */
1516 /* Each thread in the pool of threads calls this function to establish connection with
1517 * remote machines, send the prefetch requests and process the reponses from
1518 * the remote machines .
1519 * The thread is active throughout the period of runtime */
1521 void *mcqProcess(void *threadid) {
1523 prefetchpile_t *mcpilenode;
1524 struct sockaddr_in remoteAddr;
1527 tid = (int) threadid;
1531 /* Lock mutex of mc pile queue */
1532 pthread_mutex_lock(&mcqueue.qlock);
1533 /* When mc pile queue is empty, wait */
1534 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1535 pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1537 /* Dequeue node to send remote machine connections*/
1538 if((mcpilenode = mcpiledequeue()) == NULL) {
1539 printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1540 pthread_mutex_unlock(&mcqueue.qlock);
1544 pthread_mutex_unlock(&mcqueue.qlock);
1546 /*Initiate connection to remote host and send prefetch request */
1547 if(mcpilenode->mid != myIpAddr) {
1548 /* Check to see if socket exists */
1549 for(i = 0; i < NUM_MACHINES; i++) {
1550 if(midSocketArray[i].mid == mcpilenode->mid) {
1551 sendPrefetchReq(mcpilenode, midSocketArray[i].sockid);
1557 if(sockIdFound == 0) {
1558 if(sockCount < NUM_MACHINES) {
1560 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1561 printf("%s() Error: In creating socket at %s, %d\n", __func__, __FILE__, __LINE__);
1565 bzero(&remoteAddr, sizeof(remoteAddr));
1566 remoteAddr.sin_family = AF_INET;
1567 remoteAddr.sin_port = htons(LISTEN_PORT);
1568 remoteAddr.sin_addr.s_addr = htonl(mcpilenode->mid);
1570 /* Open Connection */
1571 if (connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1572 printf("%s():error %d connecting to %s:%d\n", __func__, errno,
1573 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1578 midSocketArray[sockCount].mid = mcpilenode->mid;
1579 midSocketArray[sockCount].sockid = sd;
1580 sendPrefetchReq(mcpilenode, midSocketArray[sockCount].sockid);
1583 //TODO Fix for connecting to more than 2 machines && close socket
1584 printf("%s(): Error: Currently works for only 2 machines\n", __func__);
1590 /* Deallocate the machine queue pile node */
1591 mcdealloc(mcpilenode);
1595 void sendPrefetchReq(prefetchpile_t *mcpilenode, int sd) {
1596 int i, off, len, endpair, count = 0;
1597 char machineip[16], control;
1600 /* Send TRANS_PREFETCH control message */
1601 control = TRANS_PREFETCH;
1602 if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1603 perror("sendPrefetchReq() Sending TRANS_PREFETCH");
1607 /* Send Oids and offsets in pairs */
1608 tmp = mcpilenode->objpiles;
1609 while(tmp != NULL) {
1611 count++; /* Keeps track of the number of oid and offset tuples sent per remote machine */
1612 len = sizeof(int) + sizeof(unsigned int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1613 char oidnoffset[len];
1614 bzero(oidnoffset, len);
1615 *((int*)oidnoffset) = len;
1617 *((unsigned int *)(oidnoffset + off)) = tmp->oid;
1618 off += sizeof(unsigned int);
1619 *((unsigned int *)(oidnoffset + off)) = myIpAddr;
1620 off += sizeof(unsigned int);
1621 for(i = 0; i < tmp->numoffset; i++) {
1622 *((short*)(oidnoffset + off)) = tmp->offset[i];
1625 if (send(sd, oidnoffset, len , MSG_NOSIGNAL) < len) {
1626 perror("Sending oids and offsets");
1633 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1635 if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1636 perror("Error sending endpair\n");
1643 int getPrefetchResponse(int sd) {
1644 int numbytes = 0, length = 0, size = 0;
1645 char *recvbuffer, control;
1647 void *modptr, *oldptr;
1649 if((numbytes = recv((int)sd, &length, sizeof(int), 0)) <= 0) {
1650 printf("%s() Error: in receiving length at %s, %d\n", __func__, __FILE__, __LINE__);
1654 size = length - sizeof(int);
1655 if((recvbuffer = calloc(1, size)) == NULL) {
1656 printf("%s() Calloc error at %s,%d\n", __func__, __FILE__, __LINE__);
1659 while(numbytes < size) {
1660 numbytes += recv((int)sd, recvbuffer+numbytes, size-numbytes, 0);
1663 control = *((char *) recvbuffer);
1664 if(control == OBJECT_FOUND) {
1666 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1667 size = size - (sizeof(char) + sizeof(unsigned int));
1668 pthread_mutex_lock(&prefetchcache_mutex);
1669 if ((modptr = objstrAlloc(prefetchcache, size)) == NULL) {
1670 printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1671 pthread_mutex_unlock(&prefetchcache_mutex);
1675 pthread_mutex_unlock(&prefetchcache_mutex);
1676 memcpy(modptr, recvbuffer + sizeof(char) + sizeof(unsigned int), size);
1678 /* Insert the oid and its address into the prefetch hash lookup table */
1679 /* Do a version comparison if the oid exists */
1680 if((oldptr = prehashSearch(oid)) != NULL) {
1681 /* If older version then update with new object ptr */
1682 if(((objheader_t *)oldptr)->version <= ((objheader_t *)modptr)->version) {
1684 prehashInsert(oid, modptr);
1686 /* TODO modptr should be reference counted */
1688 } else {/* Else add the object ptr to hash table*/
1689 prehashInsert(oid, modptr);
1691 /* Lock the Prefetch Cache look up table*/
1692 pthread_mutex_lock(&pflookup.lock);
1693 /* Broadcast signal on prefetch cache condition variable */
1694 pthread_cond_broadcast(&pflookup.cond);
1695 /* Unlock the Prefetch Cache look up table*/
1696 pthread_mutex_unlock(&pflookup.lock);
1697 } else if(control == OBJECT_NOT_FOUND) {
1698 oid = *((unsigned int *)(recvbuffer + sizeof(char)));
1699 /* TODO: For each object not found query DHT for new location and retrieve the object */
1700 /* Throw an error */
1701 printf("OBJECT %x NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n", oid);
1705 printf("Error: in decoding the control value %d, %s, %d\n",control, __FILE__, __LINE__);
1713 unsigned short getObjType(unsigned int oid)
1715 objheader_t *objheader;
1716 unsigned short numoffset[] ={0};
1717 short fieldoffset[] ={};
1719 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL)
1721 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL)
1723 prefetch(1, &oid, numoffset, fieldoffset);
1724 pthread_mutex_lock(&pflookup.lock);
1725 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL)
1727 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1729 pthread_mutex_unlock(&pflookup.lock);
1733 return TYPE(objheader);
1736 int startRemoteThread(unsigned int oid, unsigned int mid)
1739 struct sockaddr_in remoteAddr;
1740 char msg[1 + sizeof(unsigned int)];
1744 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1746 perror("startRemoteThread():socket()");
1750 bzero(&remoteAddr, sizeof(remoteAddr));
1751 remoteAddr.sin_family = AF_INET;
1752 remoteAddr.sin_port = htons(LISTEN_PORT);
1753 remoteAddr.sin_addr.s_addr = htonl(mid);
1755 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1757 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1758 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1763 msg[0] = START_REMOTE_THREAD;
1764 memcpy(&msg[1], &oid, sizeof(unsigned int));
1766 bytesSent = send(sock, msg, 1 + sizeof(unsigned int), 0);
1769 perror("startRemoteThread():send()");
1772 else if (bytesSent != 1 + sizeof(unsigned int))
1774 printf("startRemoteThread(): error, sent %d bytes\n", bytesSent);
1787 //TODO: when reusing oids, make sure they are not already in use!
1788 unsigned int getNewOID(void) {
1789 static unsigned int id = 0xFFFFFFFF;
1792 if (id > oidMax || id < oidMin)
1799 int processConfigFile()
1802 const int maxLineLength = 200;
1803 char lineBuffer[maxLineLength];
1805 const char *delimiters = " \t\n";
1809 configFile = fopen(CONFIG_FILENAME, "r");
1810 if (configFile == NULL)
1812 printf("error opening %s:\n", CONFIG_FILENAME);
1817 numHostsInSystem = 0;
1818 sizeOfHostArray = 8;
1819 hostIpAddrs = calloc(sizeOfHostArray, sizeof(unsigned int));
1821 while(fgets(lineBuffer, maxLineLength, configFile) != NULL)
1823 commentBegin = strchr(lineBuffer, '#');
1824 if (commentBegin != NULL)
1825 *commentBegin = '\0';
1826 token = strtok(lineBuffer, delimiters);
1827 while (token != NULL)
1829 tmpAddr = inet_addr(token);
1830 if ((int)tmpAddr == -1)
1832 printf("error in %s: bad token:%s\n", CONFIG_FILENAME, token);
1837 addHost(htonl(tmpAddr));
1838 token = strtok(NULL, delimiters);
1844 if (numHostsInSystem < 1)
1846 printf("error in %s: no IP Adresses found\n", CONFIG_FILENAME);
1850 myIpAddr = getMyIpAddr("en1");
1852 myIpAddr = getMyIpAddr("eth0");
1854 myIndexInHostArray = findHost(myIpAddr);
1855 if (myIndexInHostArray == -1)
1857 printf("error in %s: IP Address of eth0 not found\n", CONFIG_FILENAME);
1860 oidsPerBlock = (0xFFFFFFFF / numHostsInSystem) + 1;
1861 oidMin = oidsPerBlock * myIndexInHostArray;
1862 if (myIndexInHostArray == numHostsInSystem - 1)
1863 oidMax = 0xFFFFFFFF;
1865 oidMax = oidsPerBlock * (myIndexInHostArray + 1) - 1;
1870 void addHost(unsigned int hostIp)
1872 unsigned int *tmpArray;
1874 if (findHost(hostIp) != -1)
1877 if (numHostsInSystem == sizeOfHostArray)
1879 tmpArray = calloc(sizeOfHostArray * 2, sizeof(unsigned int));
1880 memcpy(tmpArray, hostIpAddrs, sizeof(unsigned int) * numHostsInSystem);
1882 hostIpAddrs = tmpArray;
1885 hostIpAddrs[numHostsInSystem++] = hostIp;
1890 int findHost(unsigned int hostIp)
1893 for (i = 0; i < numHostsInSystem; i++)
1894 if (hostIpAddrs[i] == hostIp)
1901 /* This function sends notification request per thread waiting on object(s) whose version
1903 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid) {
1905 objheader_t *objheader;
1906 struct sockaddr_in remoteAddr;
1907 char msg[1 + numoid * (sizeof(unsigned short) + sizeof(unsigned int)) + 3 * sizeof(unsigned int)];
1911 unsigned short version;
1912 unsigned int oid,mid;
1913 static unsigned int threadid = 0;
1914 pthread_mutex_t threadnotify = PTHREAD_MUTEX_INITIALIZER; //Lock and condition var for threadjoin and notification
1915 pthread_cond_t threadcond = PTHREAD_COND_INITIALIZER;
1916 notifydata_t *ndata;
1918 //FIXME currently all oids belong to one machine
1920 if((mid = lhashSearch(oid)) == 0) {
1921 printf("Error: %s() No such machine found for oid =%x\n",__func__, oid);
1925 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
1926 perror("reqNotify():socket()");
1930 bzero(&remoteAddr, sizeof(remoteAddr));
1931 remoteAddr.sin_family = AF_INET;
1932 remoteAddr.sin_port = htons(LISTEN_PORT);
1933 remoteAddr.sin_addr.s_addr = htonl(mid);
1935 /* Generate unique threadid */
1938 /* Save threadid, numoid, oidarray, versionarray, pthread_cond_variable for later processing */
1939 if((ndata = calloc(1, sizeof(notifydata_t))) == NULL) {
1940 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
1943 ndata->numoid = numoid;
1944 ndata->threadid = threadid;
1945 ndata->oidarry = oidarry;
1946 ndata->versionarry = versionarry;
1947 ndata->threadcond = threadcond;
1948 ndata->threadnotify = threadnotify;
1949 if((status = notifyhashInsert(threadid, ndata)) != 0) {
1950 printf("reqNotify(): Insert into notify hash table not successful %s, %d\n", __FILE__, __LINE__);
1955 /* Send number of oids, oidarry, version array, machine id and threadid */
1956 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
1957 printf("reqNotify():error %d connecting to %s:%d\n", errno,
1958 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1962 msg[0] = THREAD_NOTIFY_REQUEST;
1963 *((unsigned int *)(&msg[1])) = numoid;
1964 /* Send array of oids */
1965 size = sizeof(unsigned int);
1970 *((unsigned int *)(&msg[1] + size)) = oid;
1971 size += sizeof(unsigned int);
1976 /* Send array of version */
1980 version = versionarry[i];
1981 *((unsigned short *)(&msg[1] + size)) = version;
1982 size += sizeof(unsigned short);
1987 *((unsigned int *)(&msg[1] + size)) = myIpAddr;
1988 size += sizeof(unsigned int);
1989 *((unsigned int *)(&msg[1] + size)) = threadid;
1991 pthread_mutex_lock(&(ndata->threadnotify));
1992 bytesSent = send(sock, msg, 1 + numoid * (sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int) , 0);
1994 perror("reqNotify():send()");
1996 } else if (bytesSent != 1 + numoid*(sizeof(unsigned int) + sizeof(unsigned short)) + 3 * sizeof(unsigned int)){
1997 printf("reNotify(): error, sent %d bytes %s, %d\n", bytesSent, __FILE__, __LINE__);
2003 pthread_cond_wait(&(ndata->threadcond), &(ndata->threadnotify));
2004 pthread_mutex_unlock(&(ndata->threadnotify));
2007 pthread_cond_destroy(&threadcond);
2008 pthread_mutex_destroy(&threadnotify);
2014 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid) {
2015 notifydata_t *ndata;
2016 int i, objIsFound = 0, index;
2019 //Look up the tid and call the corresponding pthread_cond_signal
2020 if((ndata = notifyhashSearch(tid)) == NULL) {
2021 printf("threadnotify(): No such threadid is present %s, %d\n", __FILE__, __LINE__);
2024 for(i = 0; i < ndata->numoid; i++) {
2025 if(ndata->oidarry[i] == oid){
2030 if(objIsFound == 0){
2031 printf("threadNotify(): Oid not found %s, %d\n", __FILE__, __LINE__);
2034 if(version <= ndata->versionarry[index]){
2035 printf("threadNotify(): New version %d has not changed since last version %s, %d\n", version, __FILE__, __LINE__);
2038 /* Clear from prefetch cache and free thread related data structure */
2039 if((ptr = prehashSearch(oid)) != NULL) {
2042 pthread_cond_signal(&(ndata->threadcond));
2049 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version) {
2052 struct sockaddr_in remoteAddr;
2053 char msg[1 + sizeof(unsigned short) + 2*sizeof(unsigned int)];
2054 int sock, status, size, bytesSent;
2056 while(*head != NULL) {
2059 //create a socket connection to that machine
2060 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
2061 perror("notifyAll():socket()");
2065 bzero(&remoteAddr, sizeof(remoteAddr));
2066 remoteAddr.sin_family = AF_INET;
2067 remoteAddr.sin_port = htons(LISTEN_PORT);
2068 remoteAddr.sin_addr.s_addr = htonl(mid);
2069 //send Thread Notify response and threadid to that machine
2070 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0){
2071 printf("notifyAll():error %d connecting to %s:%d\n", errno,
2072 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
2075 bzero(msg, (1+sizeof(unsigned short) + 2*sizeof(unsigned int)));
2076 msg[0] = THREAD_NOTIFY_RESPONSE;
2077 *((unsigned int *)&msg[1]) = oid;
2078 size = sizeof(unsigned int);
2079 *((unsigned short *)(&msg[1]+ size)) = version;
2080 size+= sizeof(unsigned short);
2081 *((unsigned int *)(&msg[1]+ size)) = ptr->threadid;
2083 bytesSent = send(sock, msg, (1 + 2*sizeof(unsigned int) + sizeof(unsigned short)), 0);
2085 perror("notifyAll():send()");
2087 } else if (bytesSent != 1 + 2*sizeof(unsigned int) + sizeof(unsigned short)){
2088 printf("notifyAll(): error, sent %d bytes %s, %d\n",
2089 bytesSent, __FILE__, __LINE__);
2104 void transAbort(transrecord_t *trans) {
2105 objstrDelete(trans->cache);
2106 chashDelete(trans->lookupTable);