4 #include "machinepile.h"
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <sys/types.h>
22 #define LISTEN_PORT 2156
23 #define RECEIVE_BUFFER_SIZE 2048
24 #define NUM_THREADS 10
25 #define PREFETCH_CACHE_SIZE 1048576 //1MB
27 /* Global Variables */
28 extern int classsize[];
29 extern primarypfq_t pqueue; // shared prefetch queue
30 extern mcpileq_t mcqueue; //Shared queue containing prefetch requests sorted by remote machineids
31 objstr_t *prefetchcache; //Global Prefetch cache
32 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
33 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
35 extern objstr_t *mainobjstore;
37 plistnode_t *createPiles(transrecord_t *);
38 inline int arrayLength(int *array) {
40 for(i=0 ;array[i] != -1; i++)
44 inline int findmax(int *array, int arraylength) {
47 for(i = 0; i < arraylength; i++){
54 /* This function is a prefetch call generated by the compiler that
55 * populates the shared primary prefetch queue*/
56 void prefetch(int ntuples, unsigned int *oids, short *endoffsets, short *arrayfields) {
60 /* Allocate for the queue node*/
62 qnodesize = sizeof(prefetchqelem_t) + sizeof(int) + ntuples * (sizeof(short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
63 if((node = calloc(1, qnodesize)) == NULL) {
64 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
67 /* Set queue node values */
68 len = sizeof(prefetchqelem_t);
69 memcpy(node + len, &ntuples, sizeof(int));
71 memcpy(node + len, oids, ntuples*sizeof(unsigned int));
72 len += ntuples * sizeof(unsigned int);
73 memcpy(node + len, endoffsets, ntuples*sizeof(short));
74 len += ntuples * sizeof(short);
75 memcpy(node + len, arrayfields, endoffsets[ntuples-1]*sizeof(short));
76 /* Lock and insert into primary prefetch queue */
77 pthread_mutex_lock(&pqueue.qlock);
78 pre_enqueue((prefetchqelem_t *)node);
79 pthread_cond_signal(&pqueue.qcond);
80 pthread_mutex_unlock(&pqueue.qlock);
84 /* This function starts up the transaction runtime. */
85 int dstmStartup(const char * option) {
86 pthread_t thread_Listen;
88 int master=strcmp(option, "master")==0;
94 pthread_attr_init(&attr);
95 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
96 pthread_create(&thread_Listen, &attr, dstmListen, NULL);
106 /* This function initiates the prefetch thread
107 * A queue is shared between the main thread of execution
108 * and the prefetch thread to process the prefetch call
109 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
110 * processes the prefetch requests */
113 //Create and initialize prefetch cache structure
114 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
115 //Create prefetch cache lookup table
116 if(prehashCreate(HASH_SIZE, LOADFACTOR))
118 //Initialize primary shared queue
120 //Initialize machine pile w/prefetch oids and offsets shared queue
122 //Create the primary prefetch thread
123 pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
124 //Create and Initialize a pool of threads
125 /* Threads are active for the entire period runtime is running */
126 for(t = 0; t< NUM_THREADS; t++) {
127 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
129 printf("Thread create error %s, %d\n", __FILE__, __LINE__);
135 /* This function stops the threads spawned */
138 pthread_cancel(tPrefetch);
139 for(t = 0; t < NUM_THREADS; t++)
140 pthread_cancel(wthreads[t]);
145 /* This functions inserts randowm wait delays in the order of msec
146 * Mostly used when transaction commits retry*/
147 void randomdelay(void)
149 struct timespec req, rem;
154 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
155 nanosleep(&req, &rem);
159 /* This function initializes things required in the transaction start*/
160 transrecord_t *transStart()
162 transrecord_t *tmp = malloc(sizeof(transrecord_t));
163 tmp->cache = objstrCreate(1048576);
164 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
166 tmp->revertlist=NULL;
171 /* This function finds the location of the objects involved in a transaction
172 * and returns the pointer to the object if found in a remote location */
173 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
174 printf("Inside transaction read call\n");
175 unsigned int machinenumber;
176 objheader_t *tmp, *objheader;
178 int size, rc, found = 0;
183 rc = gettimeofday(&tp, NULL);
185 /* Convert from timeval to timespec */
186 ts.tv_nsec = tp.tv_usec * 1000;
188 /* Search local transaction cache */
189 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
190 printf("Inside transaction cache \n");
192 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
193 /* Look up in machine lookup table and copy into cache*/
194 printf("Inside mainobject store \n");
195 tmp = mhashSearch(oid);
196 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
197 objcopy = objstrAlloc(record->cache, size);
198 memcpy(objcopy, (void *)objheader, size);
199 /* Insert into cache's lookup table */
200 chashInsert(record->lookupTable, OID(objheader), objcopy);
202 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
203 printf("Inside prefetch cache \n");
205 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
206 objcopy = objstrAlloc(record->cache, size);
207 memcpy(objcopy, (void *)tmp, size);
208 /* Insert into cache's lookup table */
209 chashInsert(record->lookupTable, OID(tmp), objcopy);
211 } else { /* If not found anywhere, then block until object appears in prefetch cache */
212 printf("Inside remote machine\n");
213 pthread_mutex_lock(&pflookup.lock);
215 rc = pthread_cond_timedwait(&pflookup.cond, &pflookup.lock, &ts);
216 if(rc == ETIMEDOUT) {
217 printf("Wait timed out\n");
218 /* Check Prefetch cache again */
219 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
221 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
222 objcopy = objstrAlloc(record->cache, size);
223 memcpy(objcopy, (void *)tmp, size);
224 /* Insert into cache's lookup table */
225 chashInsert(record->lookupTable, OID(tmp), objcopy);
228 pthread_mutex_unlock(&pflookup.lock);
231 pthread_mutex_unlock(&pflookup.lock);
234 /* Get the object from the remote location */
235 machinenumber = lhashSearch(oid);
236 objcopy = getRemoteObj(record, machinenumber, oid);
237 if(objcopy == NULL) {
238 //If object is not found in Remote location
239 //printf("Object oid = %d not found in Machine %d\n", oid, machinenumber);
243 //printf("Object oid = %d found in Machine %d\n", oid, machinenumber);
249 /* This function creates objects in the transaction record */
250 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
252 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
253 OID(tmp) = getNewOID();
256 tmp->rcount = 0; //? not sure how to handle this yet
258 chashInsert(record->lookupTable, OID(tmp), tmp);
262 /* This function creates machine piles based on all machines involved in a
263 * transaction commit request */
264 plistnode_t *createPiles(transrecord_t *record) {
266 unsigned int size;/* Represents number of bins in the chash table */
267 chashlistnode_t *curr, *ptr, *next;
268 plistnode_t *pile = NULL;
269 unsigned int machinenum;
270 void *localmachinenum;
271 objheader_t *headeraddr;
273 ptr = record->lookupTable->table;
274 size = record->lookupTable->size;
276 for(i = 0; i < size ; i++) {
278 /* Inner loop to traverse the linked list of the cache lookupTable */
279 while(curr != NULL) {
280 //if the first bin in hash table is empty
285 //Get machine location for object id
287 if ((machinenum = lhashSearch(curr->key)) == 0) {
288 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
292 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
293 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
296 //Make machine groups
297 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
298 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
302 /* Check if local or not */
303 if((localmachinenum = mhashSearch(curr->key)) != NULL) {
304 /* Set the pile->local flag*/
305 pile->local = 1; //True i.e. local
314 /* This function initiates the transaction commit process
315 * Spawns threads for each of the new connections with Participants
316 * and creates new piles by calling the createPiles(),
317 * Fills the piles with necesaary information and
318 * Sends a transrequest() to each pile*/
319 int transCommit(transrecord_t *record) {
320 unsigned int tot_bytes_mod, *listmid;
321 plistnode_t *pile, *pile_ptr;
323 int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
324 char buffer[RECEIVE_BUFFER_SIZE],control;
325 char transid[TID_LEN];
326 trans_req_data_t *tosend;
327 trans_commit_data_t transinfo;
328 static int newtid = 0;
329 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
333 /* Look through all the objects in the transaction record and make piles
334 * for each machine involved in the transaction*/
335 pile_ptr = pile = createPiles(record);
337 /* Create the packet to be sent in TRANS_REQUEST */
339 /* Count the number of participants */
340 pilecount = pCount(pile);
342 /* Create a list of machine ids(Participants) involved in transaction */
343 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
344 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
347 pListMid(pile, listmid);
350 /* Initialize thread variables,
351 * Spawn a thread for each Participant involved in a transaction */
352 pthread_t thread[pilecount];
354 pthread_cond_t tcond;
355 pthread_mutex_t tlock;
356 pthread_mutex_t tlshrd;
358 thread_data_array_t *thread_data_array;
359 thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
360 local_thread_data_array_t *ltdata;
361 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
362 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
366 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
368 /* Initialize and set thread detach attribute */
369 pthread_attr_init(&attr);
370 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
371 pthread_mutex_init(&tlock, NULL);
372 pthread_cond_init(&tcond, NULL);
374 /* Process each machine pile */
375 while(pile != NULL) {
376 //Create transaction id
378 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
379 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
382 tosend->f.control = TRANS_REQUEST;
383 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
384 tosend->f.mcount = pilecount;
385 tosend->f.numread = pile->numread;
386 tosend->f.nummod = pile->nummod;
387 tosend->f.sum_bytes = pile->sum_bytes;
388 tosend->listmid = listmid;
389 tosend->objread = pile->objread;
390 tosend->oidmod = pile->oidmod;
391 thread_data_array[threadnum].thread_id = threadnum;
392 thread_data_array[threadnum].mid = pile->mid;
393 thread_data_array[threadnum].pilecount = pilecount;
394 thread_data_array[threadnum].buffer = tosend;
395 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
396 thread_data_array[threadnum].threshold = &tcond;
397 thread_data_array[threadnum].lock = &tlock;
398 thread_data_array[threadnum].count = &trecvcount;
399 thread_data_array[threadnum].replyctrl = &treplyctrl;
400 thread_data_array[threadnum].replyretry = &treplyretry;
401 thread_data_array[threadnum].rec = record;
402 /* If local do not create any extra connection */
403 if(pile->local != 1) { /* Not local */
404 rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);
406 perror("Error in pthread create\n");
410 /*Unset the pile->local flag*/
412 /*Set flag to identify that Local machine is involved*/
413 ltdata->tdata = &thread_data_array[threadnum];
414 ltdata->transinfo = &transinfo;
415 val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
417 perror("Error in pthread create\n");
425 /* Free attribute and wait for the other threads */
426 pthread_attr_destroy(&attr);
427 for (i = 0 ;i < pilecount ; i++) {
428 rc = pthread_join(thread[i], NULL);
431 printf("ERROR return code from pthread_join() is %d\n", rc);
434 free(thread_data_array[i].buffer);
438 pthread_cond_destroy(&tcond);
439 pthread_mutex_destroy(&tlock);
442 free(thread_data_array);
445 /* Retry trans commit procedure if not sucessful in the first try */
446 if(treplyretry == 1) {
447 /* wait a random amount of time */
449 /* Retry the commiting transaction again */
456 /* This function sends information involved in the transaction request and
457 * accepts a response from particpants.
458 * It calls decideresponse() to decide on what control message
459 * to send next and sends the message using sendResponse()*/
460 void *transRequest(void *threadarg) {
462 struct sockaddr_in serv_addr;
463 struct hostent *server;
464 thread_data_array_t *tdata;
465 objheader_t *headeraddr;
466 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
467 char machineip[16], retval;
469 tdata = (thread_data_array_t *) threadarg;
471 /* Send Trans Request */
472 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
473 perror("Error in socket for TRANS_REQUEST\n");
476 bzero((char*) &serv_addr, sizeof(serv_addr));
477 serv_addr.sin_family = AF_INET;
478 serv_addr.sin_port = htons(LISTEN_PORT);
479 midtoIP(tdata->mid,machineip);
480 machineip[15] = '\0';
481 serv_addr.sin_addr.s_addr = inet_addr(machineip);
482 /* Open Connection */
483 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
484 perror("Error in connect for TRANS_REQUEST\n");
488 printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
489 /* Send bytes of data with TRANS_REQUEST control message */
490 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
491 perror("Error sending fixed bytes for thread\n");
494 /* Send list of machines involved in the transaction */
496 int size=sizeof(unsigned int)*tdata->pilecount;
497 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
498 perror("Error sending list of machines for thread\n");
502 /* Send oids and version number tuples for objects that are read */
504 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
505 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
506 perror("Error sending tuples for thread\n");
510 /* Send objects that are modified */
511 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
513 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
514 size=sizeof(objheader_t)+classsize[TYPE(headeraddr)];
515 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
516 perror("Error sending obj modified for thread\n");
521 /* Read control message from Participant */
522 if((n = read(sd, &control, sizeof(char))) <= 0) {
523 perror("Error in reading control message from Participant\n");
526 recvcontrol = control;
528 /* Update common data structure and increment count */
529 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
531 /* Lock and update count */
532 //Thread sleeps until all messages from pariticipants are received by coordinator
533 pthread_mutex_lock(tdata->lock);
535 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
537 /* Wake up the threads and invoke decideResponse (once) */
538 if(*(tdata->count) == tdata->pilecount) {
539 if (decideResponse(tdata) != 0) {
540 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
541 pthread_mutex_unlock(tdata->lock);
545 pthread_cond_broadcast(tdata->threshold);
547 pthread_cond_wait(tdata->threshold, tdata->lock);
549 pthread_mutex_unlock(tdata->lock);
551 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
552 * to all participants in their respective socket */
553 if (sendResponse(tdata, sd) == 0) {
554 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
555 pthread_mutex_unlock(tdata->lock);
560 /* Close connection */
565 /* This function decides the reponse that needs to be sent to
566 * all Participant machines involved in the transaction commit */
567 int decideResponse(thread_data_array_t *tdata) {
569 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
572 //Check common data structure
573 for (i = 0 ; i < tdata->pilecount ; i++) {
574 /*Switch on response from Participant */
575 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
576 written onto the shared array */
579 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
584 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
588 case TRANS_SOFT_ABORT:
589 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
593 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
598 /* Decide what control message to send to Participant */
599 if(transdisagree > 0) {
601 *(tdata->replyctrl) = TRANS_ABORT;
602 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
604 objstrDelete(tdata->rec->cache);
605 chashDelete(tdata->rec->lookupTable);
607 } else if(transagree == tdata->pilecount){
609 *(tdata->replyctrl) = TRANS_COMMIT;
610 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
612 objstrDelete(tdata->rec->cache);
613 chashDelete(tdata->rec->lookupTable);
615 } else if(transsoftabort > 0 && transdisagree == 0) {
616 /* Send Abort in soft abort case followed by retry commiting transaction again*/
617 *(tdata->replyctrl) = TRANS_ABORT;
618 *(tdata->replyretry) = 1;
619 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
621 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
627 /* This function sends the final response to remote machines per thread in their respective socket id */
628 char sendResponse(thread_data_array_t *tdata, int sd) {
629 int n, N, sum, oidcount = 0;
630 char *ptr, retval = 0;
631 unsigned int *oidnotfound;
633 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
634 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
635 /* Read list of objects missing */
636 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
637 N = oidcount * sizeof(unsigned int);
638 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
639 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
641 ptr = (char *) oidnotfound;
643 n = read(sd, ptr+sum, N-sum);
645 } while(sum < N && n !=0);
647 retval = TRANS_SOFT_ABORT;
649 /* If the decided response is TRANS_ABORT */
650 if(*(tdata->replyctrl) == TRANS_ABORT) {
651 retval = TRANS_ABORT;
652 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
653 retval = TRANS_COMMIT;
655 /* Send response to the Participant */
656 if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
657 perror("Error sending ctrl message for participant\n");
663 /* This function opens a connection, places an object read request to the
664 * remote machine, reads the control message and object if available and
665 * copies the object and its header to the local cache.
666 * TODO replace mnum and midtoIP() with MACHINE_IP address later */
668 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
670 struct sockaddr_in serv_addr;
671 struct hostent *server;
677 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
678 perror("Error in socket\n");
681 bzero((char*) &serv_addr, sizeof(serv_addr));
682 serv_addr.sin_family = AF_INET;
683 serv_addr.sin_port = htons(LISTEN_PORT);
684 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
685 midtoIP(mnum,machineip);
686 machineip[15] = '\0';
687 serv_addr.sin_addr.s_addr = inet_addr(machineip);
688 /* Open connection */
689 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
690 perror("Error in connect\n");
693 char readrequest[sizeof(char)+sizeof(unsigned int)];
694 readrequest[0] = READ_REQUEST;
695 *((unsigned int *)(&readrequest[1])) = oid;
696 if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
697 perror("Error sending message\n");
702 printf("DEBUG -> ready to rcv ...\n");
704 /* Read response from the Participant */
705 if((val = read(sd, &control, sizeof(char))) <= 0) {
706 perror("No control response for getRemoteObj sent\n");
710 case OBJECT_NOT_FOUND:
711 printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
714 /* Read object if found into local cache */
715 if((val = read(sd, &size, sizeof(int))) <= 0) {
716 perror("No size is read from the participant\n");
719 objcopy = objstrAlloc(record->cache, size);
720 if((val = read(sd, objcopy, size)) <= 0) {
721 perror("No objects are read from the remote participant\n");
724 /* Insert into cache's lookup table */
725 chashInsert(record->lookupTable, oid, objcopy);
728 printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
731 /* Close connection */
736 /*This function handles the local trans requests involved in a transaction commiting process
737 * makes a decision if the local machine sends AGREE or DISAGREE or SOFT_ABORT
738 * Activates the other nonlocal threads that are waiting for the decision and the
739 * based on common decision by all groups involved in the transaction it
740 * either commits or aborts the transaction.
741 * It also frees the calloced memory resources
744 void *handleLocalReq(void *threadarg) {
747 char control = 0, *ptr;
749 unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
751 objheader_t *headptr;
752 local_thread_data_array_t *localtdata;
754 localtdata = (local_thread_data_array_t *) threadarg;
756 /* Counters and arrays to formulate decision on control message to be sent */
757 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
758 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
759 oidmod = (unsigned int *) calloc(localtdata->tdata->buffer->f.nummod, sizeof(unsigned int));
760 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
761 int objmodnotfound = 0, nummodfound = 0;
763 /* modptr points to the beginning of the object store
764 * created at the Pariticipant */
765 if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
766 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
772 /* Process each oid in the machine pile/ group per thread */
773 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
774 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
775 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
777 oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
778 incr += sizeof(unsigned int);
779 version = *((short *)(localtdata->tdata->buffer->objread + incr));
780 } else {//Objs modified
781 headptr = (objheader_t *) ptr;
783 oidmod[objmod] = oid;//Array containing modified oids
785 version = headptr->version;
786 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
789 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
791 /* Save the oids not found and number of oids not found for later use */
792 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
793 /* Save the oids not found and number of oids not found for later use */
795 oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
797 } else { /* If Obj found in machine (i.e. has not moved) */
798 /* Check if Obj is locked by any previous transaction */
799 if (STATUS(((objheader_t *)mobj)) & LOCK) {
800 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
802 } else {/* If versions don't match ...HARD ABORT */
804 /* Send TRANS_DISAGREE to Coordinator */
805 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
806 printf("DEBUG -> Sending TRANS_DISAGREE\n");
807 //return tdata->recvmsg[tdata->thread_id].rcv_status;
809 } else {/* If Obj is not locked then lock object */
810 STATUS(((objheader_t *)mobj)) |= LOCK;
811 //TODO Remove this for Testing
814 /* Save all object oids that are locked on this machine during this transaction request call */
815 oidlocked[objlocked] = OID(((objheader_t *)mobj));
817 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
819 } else { /* If versions don't match ...HARD ABORT */
821 /* Send TRANS_DISAGREE to Coordinator */
822 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
823 printf("DEBUG -> Sending TRANS_DISAGREE\n");
824 // return tdata->recvmsg[tdata->thread_id].rcv_status;
830 /*Decide the response to be sent to the Coordinator( the local machine in this case)*/
832 /* Condition to send TRANS_AGREE */
833 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
834 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
835 printf("DEBUG -> Sending TRANS_AGREE\n");
837 /* Condition to send TRANS_SOFT_ABORT */
838 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
839 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
840 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
841 //TODO currently the only soft abort case that is supported is when object locked by previous
842 //transaction => v_matchlock > 0
843 //The other case for SOFT ABORT i.e. when object is not found but versions match is not supported
844 /* Send number of oids not found and the missing oids if objects are missing in the machine */
845 /* TODO Remember to store the oidnotfound for later use
846 if(objnotfound != 0) {
847 int size = sizeof(unsigned int)* objnotfound;
852 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
853 * if Participant receives a TRANS_COMMIT */
854 localtdata->transinfo->objmod = oidmod;
855 localtdata->transinfo->objlocked = oidlocked;
856 localtdata->transinfo->objnotfound = oidnotfound;
857 localtdata->transinfo->modptr = modptr;
858 localtdata->transinfo->nummod = localtdata->tdata->buffer->f.nummod;
859 localtdata->transinfo->numlocked = objlocked;
860 localtdata->transinfo->numnotfound = objnotfound;
862 /*Set flag to show that common data structure for this individual thread has been written to */
863 //*(tdata->localstatus) |= LM_UPDATED;
865 /* Lock and update count */
866 //Thread sleeps until all messages from pariticipants are received by coordinator
867 pthread_mutex_lock(localtdata->tdata->lock);
868 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
870 /* Wake up the threads and invoke decideResponse (once) */
871 if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
872 if (decideResponse(localtdata->tdata) != 0) {
873 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
874 pthread_mutex_unlock(localtdata->tdata->lock);
877 pthread_cond_broadcast(localtdata->tdata->threshold);
879 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
881 pthread_mutex_unlock(localtdata->tdata->lock);
883 /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
884 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
885 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->transinfo->nummod, localtdata->tdata->buffer->f.numread) != 0) {
886 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
889 }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
890 if(transComProcess(localtdata->transinfo) != 0) {
891 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
897 printf("DEBUG -> Freeing...\n");
899 if (localtdata->transinfo->objmod != NULL) {
900 free(localtdata->transinfo->objmod);
901 localtdata->transinfo->objmod = NULL;
903 if (localtdata->transinfo->objlocked != NULL) {
904 free(localtdata->transinfo->objlocked);
905 localtdata->transinfo->objlocked = NULL;
907 if (localtdata->transinfo->objnotfound != NULL) {
908 free(localtdata->transinfo->objnotfound);
909 localtdata->transinfo->objnotfound = NULL;
914 /* This function completes the ABORT process if the transaction is aborting
916 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod, int numread) {
919 objheader_t *tmp_header;
922 printf("DEBUG -> Recv TRANS_ABORT\n");
923 /* Set all ref counts as 1 and do garbage collection */
925 for(i = 0; i< nummod; i++) {
926 tmp_header = (objheader_t *)ptr;
927 tmp_header->rcount = 1;
928 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
930 /* Unlock objects that was locked due to this transaction */
931 for(i = 0; i< numlocked; i++) {
932 header = mhashSearch(objlocked[i]);// find the header address
933 STATUS(((objheader_t *)header)) &= ~(LOCK);
936 /* Send ack to Coordinator */
937 printf("DEBUG-> TRANS_SUCCESSFUL\n");
939 /*Free the pointer */
944 /*This function completes the COMMIT process is the transaction is commiting
946 int transComProcess(trans_commit_data_t *transinfo) {
948 int i = 0, offset = 0;
951 printf("DEBUG -> Recv TRANS_COMMIT\n");
952 /* Process each modified object saved in the mainobject store */
953 for(i=0; i<transinfo->nummod; i++) {
954 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
955 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
957 /* Change reference count of older address and free space in objstr ?? */
958 header->rcount = 1; //TODO Not sure what would be the val
960 /* Change ptr address in mhash table */
961 mhashRemove(transinfo->objmod[i]);
962 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
963 offset += sizeof(objheader_t) + classsize[TYPE(header)];
965 /* Update object version number */
966 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
967 header->version += 1;
970 /* Unlock locked objects */
971 for(i=0; i<transinfo->numlocked; i++) {
972 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
973 STATUS(header) &= ~(LOCK);
976 //TODO Update location lookup table
978 /* Send ack to Coordinator */
979 printf("DEBUG-> TRANS_SUCESSFUL\n");
983 /* This function checks if the prefetch oids are same and have same offsets
984 * for case x.a.b and y.a.b where x and y have same oid's
985 * or if a.b.c is a subset of x.b.c.d*/
986 /* check for case where the generated request a.y.z or x.y.z.g then
987 * prefetch needs to be generated for x.y.z.g if oid of a and x are same*/
988 void checkPrefetchTuples(prefetchqelem_t *node) {
989 int i,j, count,k, sindex, index;
991 int ntuples, slength;
993 short *endoffsets, *arryfields;
995 /* Check for the case x.y.z and a.b.c are same oids */
997 ntuples = *(GET_NTUPLES(ptr));
998 oid = GET_PTR_OID(ptr);
999 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1000 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1001 /* Find offset length for each tuple */
1002 int numoffset[ntuples];
1003 numoffset[0] = endoffsets[0];
1004 for(i = 1; i<ntuples; i++) {
1005 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1007 /* Check for redundant tuples by comparing oids of each tuple */
1008 for(i = 0; i < ntuples; i++) {
1011 for(j = i+1 ; j < ntuples; j++) {
1014 /*If oids of tuples match */
1015 if (oid[i] == oid[j]) {
1016 /* Find the smallest offset length of two tuples*/
1017 if(numoffset[i] > numoffset[j]){
1018 slength = numoffset[j];
1022 slength = numoffset[i];
1026 /* Compare the offset values based on the current indices
1027 * break if they do not match
1028 * if all offset values match then pick the largest tuple*/
1032 index = endoffsets[j -1];
1033 for(count = 0; count < slength; count ++) {
1034 if (arryfields[k] != arryfields[index]) {
1041 k = endoffsets[i-1];
1042 index = endoffsets[j-1];
1043 printf("Value of slength = %d\n", slength);
1044 for(count = 0; count < slength; count++) {
1045 if(arryfields[k] != arryfields[index]) {
1053 if(slength == count) {
1061 void checkPreCache(prefetchqelem_t *node, int *numoffset, int counter, int loopcount, unsigned int objoid, int index, int iter, int oidnfound) {
1063 int ntuples, i, k, flag;
1065 short *endoffsets, *arryfields;
1066 objheader_t *header;
1068 ptr = (char *) node;
1069 ntuples = *(GET_NTUPLES(ptr));
1070 oid = GET_PTR_OID(ptr);
1071 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1072 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1074 if(oidnfound == 1) {
1075 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1077 } else { //Found in Prefetch Cache
1078 //TODO Decide if object is too old, if old remove from cache
1079 tmp = (char *) header;
1080 /* Check if any of the offset oid is available in the Prefetch cache */
1081 for(i = counter; i < loopcount; i++) {
1082 objoid = *(tmp + sizeof(objheader_t) + arryfields[counter]);
1083 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1092 for(i = counter; i<loopcount; i++) {
1093 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1094 tmp = (char *) header;
1095 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1105 /* If oid not found locally or in prefetch cache then
1106 * assign the latest oid found as the new oid
1107 * and copy left over offsets into the arrayoffsetfieldarray*/
1109 numoffset[iter] = numoffset[iter] - (i+1);
1110 for(k = 0; k < numoffset[iter] ; k++) {
1111 arryfields[endoffsets[counter]+k] = arryfields[endoffsets[counter]+k+1];
1116 numoffset[iter] = 0;
1120 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1121 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1123 int ntuples, slength, i, machinenum;
1126 short *endoffsets, *arryfields, *offset;
1127 prefetchpile_t *head = NULL;
1129 /* Check for the case x.y.z and a.b.c are same oids */
1130 ptr = (char *) node;
1131 ntuples = *(GET_NTUPLES(ptr));
1132 oid = GET_PTR_OID(ptr);
1133 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1134 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1136 /* Check for redundant tuples by comparing oids of each tuple */
1137 for(i = 0; i < ntuples; i++) {
1140 /* For each tuple make piles */
1141 if ((machinenum = lhashSearch(oid[i])) == 0) {
1142 printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1145 /* Insert into machine pile */
1146 offset = &arryfields[endoffsets[i-1]];
1147 insertPile(machinenum, oid[i], numoffset[i], offset, head);
1154 /* This function checks if the oids within the prefetch tuples are available locally.
1155 * If yes then makes the tuple invalid. If no then rearranges oid and offset values in
1156 * the prefetchqelem_t node to represent a new prefetch tuple */
1157 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1158 int ntuples,i, j, k, oidnfound = 0, index, flag;
1160 unsigned int objoid;
1162 objheader_t *objheader;
1163 short *endoffsets, *arryfields;
1164 prefetchpile_t *head = NULL;
1166 ptr = (char *) node;
1167 ntuples = *(GET_NTUPLES(ptr));
1168 oid = GET_PTR_OID(ptr);
1169 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1170 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1171 /* Find offset length for each tuple */
1172 int numoffset[ntuples];//Number of offsets for each tuple
1173 numoffset[0] = endoffsets[0];
1174 for(i = 1; i<ntuples; i++) {
1175 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1177 for(i = 0; i < ntuples; i++) {
1180 /* If object found locally */
1181 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1183 tmp = (char *) objheader;
1184 /* Find the oid of its offset value */
1188 index = endoffsets[i - 1];
1189 for(j = 0 ; j < numoffset[i] ; j++) {
1190 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1191 /*If oid found locally then
1192 *assign the latest oid found as the new oid
1193 *and copy left over offsets into the arrayoffsetfieldarray*/
1195 numoffset[i] = numoffset[i] - (j+1);
1196 for(k = 0; k < numoffset[i]; k++)
1197 arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1199 /*New offset oid not found */
1200 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1202 checkPreCache(node, numoffset, j, numoffset[i], objoid, index, i, oidnfound);
1208 /*If all offset oids are found locally,make the prefetch tuple invalid */
1215 /* Look in Prefetch cache */
1216 checkPreCache(node, numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound);
1220 /* Make machine groups */
1221 head = makePreGroups(node, numoffset);
1225 /* This function is called by the thread calling transPrefetch */
1226 void *transPrefetch(void *t) {
1227 //int *offstarray = NULL;
1228 prefetchqelem_t *qnode;
1229 prefetchpile_t *pilehead = NULL;
1232 /* lock mutex of primary prefetch queue */
1233 pthread_mutex_lock(&pqueue.qlock);
1234 /* while primary queue is empty, then wait */
1235 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1236 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1239 /* dequeue node to create a machine piles and finally unlock mutex */
1240 if((qnode = pre_dequeue()) == NULL) {
1241 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1244 pthread_mutex_unlock(&pqueue.qlock);
1245 /* Reduce redundant prefetch requests */
1246 checkPrefetchTuples(qnode);
1247 /* Check if the tuples are found locally, if yes then reduce them further*/
1248 /* and group requests by remote machine ids by calling the makePreGroups() */
1249 pilehead = foundLocal(qnode);
1251 /* Lock mutex of pool queue */
1252 pthread_mutex_lock(&mcqueue.qlock);
1253 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1254 mcpileenqueue(pilehead);
1255 /* Broadcast signal on machine pile queue */
1256 pthread_cond_broadcast(&mcqueue.qcond);
1257 /* Unlock mutex of machine pile queue */
1258 pthread_mutex_unlock(&mcqueue.qlock);
1259 /* Deallocate the prefetch queue pile node */
1265 /* Each thread in the pool of threads calls this function to establish connection with
1266 * remote machines, send the prefetch requests and process the reponses from
1267 * the remote machines .
1268 * The thread is active throughout the period of runtime */
1270 void *mcqProcess(void *threadid) {
1272 prefetchpile_t *mcpilenode;
1274 tid = (int) threadid;
1276 /* Lock mutex of mc pile queue */
1277 pthread_mutex_lock(&mcqueue.qlock);
1278 /* When mc pile queue is empty, wait */
1279 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1280 pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1282 /* Dequeue node to send remote machine connections*/
1283 if((mcpilenode = mcpiledequeue()) == NULL) {
1284 printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1288 pthread_mutex_unlock(&mcqueue.qlock);
1290 /*Initiate connection to remote host and send request */
1291 /* Process Request */
1292 sendPrefetchReq(mcpilenode, tid);
1294 /* Deallocate the machine queue pile node */
1295 mcdealloc(mcpilenode);
1299 void sendPrefetchReq(prefetchpile_t *mcpilenode, int threadid) {
1300 int sd, i, offset, off, len, endpair, numoffsets, count = 0;
1301 struct sockaddr_in serv_addr;
1302 struct hostent *server;
1303 char machineip[16], control;
1307 /* Send Trans Prefetch Request */
1308 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1309 perror("Error in socket for TRANS_REQUEST\n");
1312 bzero((char*) &serv_addr, sizeof(serv_addr));
1313 serv_addr.sin_family = AF_INET;
1314 serv_addr.sin_port = htons(LISTEN_PORT);
1315 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
1316 midtoIP(mcpilenode->mid ,machineip);
1317 machineip[15] = '\0';
1318 serv_addr.sin_addr.s_addr = inet_addr(machineip);
1320 /* Open Connection */
1321 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1322 perror("Error in connect for TRANS_REQUEST\n");
1326 /* Send TRANS_PREFETCH control message */
1327 control = TRANS_PREFETCH;
1328 if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1329 perror("Error in sending prefetch control\n");
1333 /* Send Oids and offsets in pairs */
1334 tmp = mcpilenode->objpiles;
1335 while(tmp != NULL) {
1337 count++; // Keeps track of the number of oid and offset tuples sent per remote machine
1338 len = sizeof(int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1339 char oidnoffset[len];
1340 memcpy(oidnoffset, &len, sizeof(int));
1342 memcpy(oidnoffset + off, &tmp->oid, sizeof(unsigned int));
1343 off += sizeof(unsigned int);
1344 for(i = 0; i < numoffsets; i++) {
1345 offset = off + (i * sizeof(short));
1346 memcpy(oidnoffset + offset, tmp->offset, sizeof(short));
1348 if (send(sd, &oidnoffset, sizeof(oidnoffset),MSG_NOSIGNAL) < sizeof(oidnoffset)) {
1349 perror("Error sending fixed bytes for thread\n");
1355 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1357 if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1358 perror("Error sending fixed bytes for thread\n");
1362 /* Get Response from the remote machine */
1363 getPrefetchResponse(count,sd);
1368 void getPrefetchResponse(int count, int sd) {
1369 int i = 0, val, n, N, sum, index, objsize;
1370 unsigned int bufsize,oid;
1371 char buffer[RECEIVE_BUFFER_SIZE], control;
1373 void *modptr, *oldptr;
1375 /* Read prefetch response from the Remote machine */
1376 if((val = read(sd, &control, sizeof(char))) <= 0) {
1377 perror("No control response for Prefetch request sent\n");
1381 if(control == TRANS_PREFETCH_RESPONSE) {
1382 /*For each oid and offset tuple sent as prefetch request to remote machine*/
1384 /* Clear contents of buffer */
1385 memset(buffer, 0, RECEIVE_BUFFER_SIZE);
1388 /* Read the size of buffer to be received */
1389 if((N = read(sd, buffer, sizeof(unsigned int))) <= 0) {
1390 perror("Size of buffer not recv\n");
1393 memcpy(&bufsize, buffer, sizeof(unsigned int));
1394 ptr = buffer + sizeof(unsigned int);
1395 /* Keep receiving the buffer containing oid info */
1397 n = recv((int)sd, (void *)ptr+sum, bufsize-sum, 0);
1399 } while(sum < bufsize && n != 0);
1400 /* Decode the contents of the buffer */
1401 index = sizeof(unsigned int);
1402 while(index < (bufsize - sizeof(unsigned int))) {
1403 if(buffer[index] == OBJECT_FOUND) {
1404 /* Increment it to get the object */
1405 index += sizeof(char);
1406 memcpy(&oid, buffer + index, sizeof(unsigned int));
1407 index += sizeof(unsigned int);
1408 /* Lock the Prefetch Cache look up table*/
1409 pthread_mutex_lock(&pflookup.lock);
1410 /* For each object found add to Prefetch Cache */
1411 memcpy(&objsize, buffer + index, sizeof(int));
1412 if ((modptr = objstrAlloc(prefetchcache, objsize)) == NULL) {
1413 printf("objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1416 memcpy(modptr, buffer+index, objsize);
1417 index += sizeof(int);
1418 /* Insert the oid and its address into the prefetch hash lookup table */
1419 /* Do a version comparison if the oid exists */
1420 if((oldptr = prehashSearch(oid)) != NULL) {
1421 /* If older version then update with new object ptr */
1422 if(((objheader_t *)oldptr)->version < ((objheader_t *)modptr)->version) {
1424 prehashInsert(oid, modptr);
1425 } else if(((objheader_t *)oldptr)->version == ((objheader_t *)modptr)->version) {
1426 /* Add the new object ptr to hash table */
1427 prehashInsert(oid, modptr);
1428 } else { /* Do nothing */
1431 } else {/*If doesn't no match found in hashtable, add the object ptr to hash table*/
1432 prehashInsert(oid, modptr);
1434 /* Broadcast signal on prefetch cache condition variable */
1435 pthread_cond_broadcast(&pflookup.cond);
1436 /* Unlock the Prefetch Cache look up table*/
1437 pthread_mutex_unlock(&pflookup.lock);
1438 } else if(buffer[index] == OBJECT_NOT_FOUND) {
1439 /* Increment it to get the object */
1440 /* TODO: For each object not found query DHT for new location and retrieve the object */
1441 index += sizeof(char);
1442 memcpy(&oid, buffer + index, sizeof(unsigned int));
1443 index += sizeof(unsigned int);
1444 /* Throw an error */
1445 printf("OBJECT NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n");
1448 printf("Error in decoding the index value %s, %d\n",__FILE__, __LINE__);
1454 printf("Error in receving response for prefetch request %s, %d\n",__FILE__, __LINE__);