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 enqueue((prefetchqelem_t *)node);
79 pthread_cond_signal(&pqueue.qcond);
80 pthread_mutex_unlock(&pqueue.qlock);
83 /* This function initiates the prefetch thread
84 * A queue is shared between the main thread of execution
85 * and the prefetch thread to process the prefetch call
86 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
87 * processes the prefetch requests */
90 //Create and initialize prefetch cache structure
91 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
92 //Create prefetch cache lookup table
93 if(prehashCreate(HASH_SIZE, LOADFACTOR))
95 //Initialize primary shared queue
97 //Initialize machine pile w/prefetch oids and offsets shared queue
99 //Create the primary prefetch thread
100 pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
101 //Create and Initialize a pool of threads
102 for(t = 0; t< NUM_THREADS; t++) {
103 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
105 printf("Thread create error %s, %d\n", __FILE__, __LINE__);
109 //TODO when to deletethreads
112 /* This function stops the threads spawned */
115 pthread_cancel(tPrefetch);
116 for(t = 0; t < NUM_THREADS; t++)
117 pthread_cancel(wthreads[t]);
122 /* This functions inserts randowm wait delays in the order of msec
123 * Mostly used when transaction commits retry*/
124 void randomdelay(void)
126 struct timespec req, rem;
131 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
132 nanosleep(&req, &rem);
136 /* This function initializes things required in the transaction start*/
137 transrecord_t *transStart()
139 transrecord_t *tmp = malloc(sizeof(transrecord_t));
140 tmp->cache = objstrCreate(1048576);
141 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
146 /* This function finds the location of the objects involved in a transaction
147 * and returns the pointer to the object if found in a remote location */
148 objheader_t *transRead(transrecord_t *record, unsigned int oid)
150 unsigned int machinenumber;
151 objheader_t *tmp, *objheader;
153 int size, rc, found = 0;
158 rc = gettimeofday(&tp, NULL);
160 /* Convert from timeval to timespec */
161 ts.tv_nsec = tp.tv_usec * 1000;
163 /* Search local transaction cache */
164 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
166 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
167 /* Look up in machine lookup table and copy into cache*/
168 tmp = mhashSearch(oid);
169 size = sizeof(objheader_t)+classsize[tmp->type];
170 objcopy = objstrAlloc(record->cache, size);
171 memcpy(objcopy, (void *)objheader, size);
172 /* Insert into cache's lookup table */
173 chashInsert(record->lookupTable, objheader->oid, objcopy);
175 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
177 size = sizeof(objheader_t)+classsize[tmp->type];
178 objcopy = objstrAlloc(record->cache, size);
179 memcpy(objcopy, (void *)tmp, size);
180 /* Insert into cache's lookup table */
181 chashInsert(record->lookupTable, tmp->oid, objcopy);
183 } else { /* If not found anywhere, then block until object appears in prefetch cache */
184 pthread_mutex_lock(&pflookup.lock);
186 rc = pthread_cond_timedwait(&pflookup.cond, &pflookup.lock, &ts);
187 if(rc == ETIMEDOUT) {
188 printf("Wait timed out\n");
189 /* Check Prefetch cache again */
190 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
192 size = sizeof(objheader_t)+classsize[tmp->type];
193 objcopy = objstrAlloc(record->cache, size);
194 memcpy(objcopy, (void *)tmp, size);
195 /* Insert into cache's lookup table */
196 chashInsert(record->lookupTable, tmp->oid, objcopy);
199 pthread_mutex_unlock(&pflookup.lock);
202 pthread_mutex_unlock(&pflookup.lock);
205 /* Get the object from the remote location */
206 machinenumber = lhashSearch(oid);
207 objcopy = getRemoteObj(record, machinenumber, oid);
208 if(objcopy == NULL) {
209 //If object is not found in Remote location
210 //printf("Object oid = %d not found in Machine %d\n", oid, machinenumber);
214 //printf("Object oid = %d found in Machine %d\n", oid, machinenumber);
220 /* This function creates objects in the transaction record */
221 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
223 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
224 tmp->oid = getNewOID();
227 tmp->rcount = 0; //? not sure how to handle this yet
230 chashInsert(record->lookupTable, tmp->oid, tmp);
234 /* This function creates machine piles based on all machines involved in a
235 * transaction commit request */
236 plistnode_t *createPiles(transrecord_t *record) {
238 unsigned int size;/* Represents number of bins in the chash table */
239 chashlistnode_t *curr, *ptr, *next;
240 plistnode_t *pile = NULL;
241 unsigned int machinenum;
242 void *localmachinenum;
243 objheader_t *headeraddr;
245 ptr = record->lookupTable->table;
246 size = record->lookupTable->size;
248 for(i = 0; i < size ; i++) {
250 /* Inner loop to traverse the linked list of the cache lookupTable */
251 while(curr != NULL) {
252 //if the first bin in hash table is empty
257 //Get machine location for object id
259 if ((machinenum = lhashSearch(curr->key)) == 0) {
260 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
264 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
265 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
268 //Make machine groups
269 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
270 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
274 /* Check if local or not */
275 if((localmachinenum = mhashSearch(curr->key)) != NULL) {
276 /* Set the pile->local flag*/
277 pile->local = 1; //True i.e. local
286 /* This function initiates the transaction commit process
287 * Spawns threads for each of the new connections with Participants
288 * and creates new piles by calling the createPiles(),
289 * Fills the piles with necesaary information and
290 * Sends a transrequest() to each pile*/
291 int transCommit(transrecord_t *record) {
292 unsigned int tot_bytes_mod, *listmid;
295 int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
296 char buffer[RECEIVE_BUFFER_SIZE],control;
297 char transid[TID_LEN];
298 trans_req_data_t *tosend;
299 trans_commit_data_t transinfo;
300 static int newtid = 0;
301 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
304 /* Look through all the objects in the transaction record and make piles
305 * for each machine involved in the transaction*/
306 pile = createPiles(record);
308 /* Create the packet to be sent in TRANS_REQUEST */
310 /* Count the number of participants */
311 pilecount = pCount(pile);
313 /* Create a list of machine ids(Participants) involved in transaction */
314 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
315 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
318 pListMid(pile, listmid);
321 /* Initialize thread variables,
322 * Spawn a thread for each Participant involved in a transaction */
323 pthread_t thread[pilecount];
325 pthread_cond_t tcond;
326 pthread_mutex_t tlock;
327 pthread_mutex_t tlshrd;
329 thread_data_array_t *thread_data_array;
330 thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
331 local_thread_data_array_t *ltdata;
332 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
333 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
337 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
339 /* Initialize and set thread detach attribute */
340 pthread_attr_init(&attr);
341 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
342 pthread_mutex_init(&tlock, NULL);
343 pthread_cond_init(&tcond, NULL);
345 /* Process each machine pile */
346 while(pile != NULL) {
347 //Create transaction id
349 //trans_req_data_t *tosend;
350 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
351 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
354 tosend->f.control = TRANS_REQUEST;
355 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
356 tosend->f.mcount = pilecount;
357 tosend->f.numread = pile->numread;
358 tosend->f.nummod = pile->nummod;
359 tosend->f.sum_bytes = pile->sum_bytes;
360 tosend->listmid = listmid;
361 tosend->objread = pile->objread;
362 tosend->oidmod = pile->oidmod;
363 thread_data_array[threadnum].thread_id = threadnum;
364 thread_data_array[threadnum].mid = pile->mid;
365 thread_data_array[threadnum].pilecount = pilecount;
366 thread_data_array[threadnum].buffer = tosend;
367 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
368 thread_data_array[threadnum].threshold = &tcond;
369 thread_data_array[threadnum].lock = &tlock;
370 thread_data_array[threadnum].count = &trecvcount;
371 thread_data_array[threadnum].replyctrl = &treplyctrl;
372 thread_data_array[threadnum].replyretry = &treplyretry;
373 thread_data_array[threadnum].rec = record;
374 /* If local do not create any extra connection */
375 if(pile->local != 1) { /* Not local */
376 rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);
378 perror("Error in pthread create\n");
382 /*Unset the pile->local flag*/
384 /*Set flag to identify that Local machine is involved*/
385 ltdata->tdata = &thread_data_array[threadnum];
386 ltdata->transinfo = &transinfo;
387 val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
389 perror("Error in pthread create\n");
397 /* Free attribute and wait for the other threads */
398 pthread_attr_destroy(&attr);
399 for (i = 0 ;i < pilecount ; i++) {
400 rc = pthread_join(thread[i], NULL);
403 printf("ERROR return code from pthread_join() is %d\n", rc);
409 pthread_cond_destroy(&tcond);
410 pthread_mutex_destroy(&tlock);
414 free(thread_data_array);
417 /* Retry trans commit procedure if not sucessful in the first try */
418 if(treplyretry == 1) {
419 /* wait a random amount of time */
422 /* Retry the commiting transaction again */
429 /* This function sends information involved in the transaction request and
430 * accepts a response from particpants.
431 * It calls decideresponse() to decide on what control message
432 * to send next and sends the message using sendResponse()*/
433 void *transRequest(void *threadarg) {
435 struct sockaddr_in serv_addr;
436 struct hostent *server;
437 thread_data_array_t *tdata;
438 objheader_t *headeraddr;
439 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
440 char machineip[16], retval;
442 tdata = (thread_data_array_t *) threadarg;
444 /* Send Trans Request */
445 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
446 perror("Error in socket for TRANS_REQUEST\n");
449 bzero((char*) &serv_addr, sizeof(serv_addr));
450 serv_addr.sin_family = AF_INET;
451 serv_addr.sin_port = htons(LISTEN_PORT);
452 midtoIP(tdata->mid,machineip);
453 machineip[15] = '\0';
454 serv_addr.sin_addr.s_addr = inet_addr(machineip);
455 /* Open Connection */
456 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
457 perror("Error in connect for TRANS_REQUEST\n");
461 printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
462 /* Send bytes of data with TRANS_REQUEST control message */
463 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
464 perror("Error sending fixed bytes for thread\n");
467 /* Send list of machines involved in the transaction */
469 int size=sizeof(unsigned int)*tdata->pilecount;
470 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
471 perror("Error sending list of machines for thread\n");
475 /* Send oids and version number tuples for objects that are read */
477 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
478 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
479 perror("Error sending tuples for thread\n");
483 /* Send objects that are modified */
484 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
486 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
487 size=sizeof(objheader_t)+classsize[headeraddr->type];
488 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
489 perror("Error sending obj modified for thread\n");
494 /* Read control message from Participant */
495 if((n = read(sd, &control, sizeof(char))) <= 0) {
496 perror("Error in reading control message from Participant\n");
499 recvcontrol = control;
501 /* Update common data structure and increment count */
502 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
504 /* Lock and update count */
505 //Thread sleeps until all messages from pariticipants are received by coordinator
506 pthread_mutex_lock(tdata->lock);
508 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
510 /* Wake up the threads and invoke decideResponse (once) */
511 if(*(tdata->count) == tdata->pilecount) {
512 if (decideResponse(tdata) != 0) {
513 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
514 pthread_mutex_unlock(tdata->lock);
518 pthread_cond_broadcast(tdata->threshold);
520 pthread_cond_wait(tdata->threshold, tdata->lock);
522 pthread_mutex_unlock(tdata->lock);
524 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
525 * to all participants in their respective socket */
526 if (sendResponse(tdata, sd) == 0) {
527 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
528 pthread_mutex_unlock(tdata->lock);
533 /* Close connection */
538 /* This function decides the reponse that needs to be sent to
539 * all Participant machines involved in the transaction commit */
540 int decideResponse(thread_data_array_t *tdata) {
542 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
545 //Check common data structure
546 for (i = 0 ; i < tdata->pilecount ; i++) {
547 /*Switch on response from Participant */
548 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
549 written onto the shared array */
552 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
557 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
561 case TRANS_SOFT_ABORT:
562 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
566 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
571 /* Decide what control message to send to Participant */
572 if(transdisagree > 0) {
574 *(tdata->replyctrl) = TRANS_ABORT;
575 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
576 objstrDelete(tdata->rec->cache);
577 chashDelete(tdata->rec->lookupTable);
579 } else if(transagree == tdata->pilecount){
581 *(tdata->replyctrl) = TRANS_COMMIT;
582 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
583 objstrDelete(tdata->rec->cache);
584 chashDelete(tdata->rec->lookupTable);
586 } else if(transsoftabort > 0 && transdisagree == 0) {
587 /* Send Abort in soft abort case followed by retry commiting transaction again*/
588 *(tdata->replyctrl) = TRANS_ABORT;
589 *(tdata->replyretry) = 1;
590 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
592 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
598 /* This function sends the final response to all threads in their respective socket id */
599 char sendResponse(thread_data_array_t *tdata, int sd) {
600 int n, N, sum, oidcount = 0;
601 char *ptr, retval = 0;
602 unsigned int *oidnotfound;
604 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
605 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
606 /* Read list of objects missing */
607 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
608 N = oidcount * sizeof(unsigned int);
609 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
610 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
612 ptr = (char *) oidnotfound;
614 n = read(sd, ptr+sum, N-sum);
616 } while(sum < N && n !=0);
618 retval = TRANS_SOFT_ABORT;
620 /* If the decided response is TRANS_ABORT */
621 if(*(tdata->replyctrl) == TRANS_ABORT) {
622 retval = TRANS_ABORT;
623 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
624 retval = TRANS_COMMIT;
626 /* Send response to the Participant */
627 if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
628 perror("Error sending ctrl message for participant\n");
634 /* This function opens a connection, places an object read request to the
635 * remote machine, reads the control message and object if available and
636 * copies the object and its header to the local cache.
637 * TODO replace mnum and midtoIP() with MACHINE_IP address later */
639 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
641 struct sockaddr_in serv_addr;
642 struct hostent *server;
648 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
649 perror("Error in socket\n");
652 bzero((char*) &serv_addr, sizeof(serv_addr));
653 serv_addr.sin_family = AF_INET;
654 serv_addr.sin_port = htons(LISTEN_PORT);
655 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
656 midtoIP(mnum,machineip);
657 machineip[15] = '\0';
658 serv_addr.sin_addr.s_addr = inet_addr(machineip);
659 /* Open connection */
660 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
661 perror("Error in connect\n");
664 char readrequest[sizeof(char)+sizeof(unsigned int)];
665 readrequest[0] = READ_REQUEST;
666 *((unsigned int *)(&readrequest[1])) = oid;
667 if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
668 perror("Error sending message\n");
673 printf("DEBUG -> ready to rcv ...\n");
675 /* Read response from the Participant */
676 if((val = read(sd, &control, sizeof(char))) <= 0) {
677 perror("No control response for getRemoteObj sent\n");
681 case OBJECT_NOT_FOUND:
682 printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
685 /* Read object if found into local cache */
686 if((val = read(sd, &size, sizeof(int))) <= 0) {
687 perror("No size is read from the participant\n");
690 objcopy = objstrAlloc(record->cache, size);
691 if((val = read(sd, objcopy, size)) <= 0) {
692 perror("No objects are read from the remote participant\n");
695 /* Insert into cache's lookup table */
696 chashInsert(record->lookupTable, oid, objcopy);
699 printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
702 /* Close connection */
707 /*This function handles the local trans requests involved in a transaction commiting process
708 * makes a decision if the local machine sends AGREE or DISAGREE or SOFT_ABORT
709 * Activates the other nonlocal threads that are waiting for the decision and the
710 * based on common decision by all groups involved in the transaction it
711 * either commits or aborts the transaction.
712 * It also frees the calloced memory resources
715 void *handleLocalReq(void *threadarg) {
718 char control = 0, *ptr;
720 unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
722 objheader_t *headptr;
723 local_thread_data_array_t *localtdata;
725 localtdata = (local_thread_data_array_t *) threadarg;
727 /* Counters and arrays to formulate decision on control message to be sent */
728 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
729 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
730 oidmod = (unsigned int *) calloc(localtdata->tdata->buffer->f.nummod, sizeof(unsigned int));
731 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
732 int objmodnotfound = 0, nummodfound = 0;
734 /* modptr points to the beginning of the object store
735 * created at the Pariticipant */
736 if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
737 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
743 /* Process each oid in the machine pile/ group per thread */
744 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
745 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
746 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
748 oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
749 incr += sizeof(unsigned int);
750 version = *((short *)(localtdata->tdata->buffer->objread + incr));
751 } else {//Objs modified
752 headptr = (objheader_t *) ptr;
754 oidmod[objmod] = oid;//Array containing modified oids
756 version = headptr->version;
757 ptr += sizeof(objheader_t) + classsize[headptr->type];
760 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
762 /* Save the oids not found and number of oids not found for later use */
763 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
764 /* Save the oids not found and number of oids not found for later use */
766 oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
768 } else { /* If Obj found in machine (i.e. has not moved) */
769 /* Check if Obj is locked by any previous transaction */
770 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
771 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
773 } else {/* If versions don't match ...HARD ABORT */
775 /* Send TRANS_DISAGREE to Coordinator */
776 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
777 printf("DEBUG -> Sending TRANS_DISAGREE\n");
778 //return tdata->recvmsg[tdata->thread_id].rcv_status;
780 } else {/* If Obj is not locked then lock object */
781 ((objheader_t *)mobj)->status |= LOCK;
782 //TODO Remove this for Testing
785 /* Save all object oids that are locked on this machine during this transaction request call */
786 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
788 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
790 } else { /* If versions don't match ...HARD ABORT */
792 /* Send TRANS_DISAGREE to Coordinator */
793 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
794 printf("DEBUG -> Sending TRANS_DISAGREE\n");
795 // return tdata->recvmsg[tdata->thread_id].rcv_status;
801 /*Decide the response to be sent to the Coordinator( the local machine in this case)*/
803 /* Condition to send TRANS_AGREE */
804 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
805 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
806 printf("DEBUG -> Sending TRANS_AGREE\n");
808 /* Condition to send TRANS_SOFT_ABORT */
809 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
810 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
811 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
812 //TODO currently the only soft abort case that is supported is when object locked by previous
813 //transaction => v_matchlock > 0
814 //The other case for SOFT ABORT i.e. when object is not found but versions match is not supported
815 /* Send number of oids not found and the missing oids if objects are missing in the machine */
816 /* TODO Remember to store the oidnotfound for later use
817 if(objnotfound != 0) {
818 int size = sizeof(unsigned int)* objnotfound;
823 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
824 * if Participant receives a TRANS_COMMIT */
825 localtdata->transinfo->objmod = oidmod;
826 localtdata->transinfo->objlocked = oidlocked;
827 localtdata->transinfo->objnotfound = oidnotfound;
828 localtdata->transinfo->modptr = modptr;
829 localtdata->transinfo->nummod = localtdata->tdata->buffer->f.nummod;
830 localtdata->transinfo->numlocked = objlocked;
831 localtdata->transinfo->numnotfound = objnotfound;
833 /*Set flag to show that common data structure for this individual thread has been written to */
834 //*(tdata->localstatus) |= LM_UPDATED;
836 /* Lock and update count */
837 //Thread sleeps until all messages from pariticipants are received by coordinator
838 pthread_mutex_lock(localtdata->tdata->lock);
839 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
841 /* Wake up the threads and invoke decideResponse (once) */
842 if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
843 if (decideResponse(localtdata->tdata) != 0) {
844 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
845 pthread_mutex_unlock(localtdata->tdata->lock);
848 pthread_cond_broadcast(localtdata->tdata->threshold);
850 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
852 pthread_mutex_unlock(localtdata->tdata->lock);
854 /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
855 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
856 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->transinfo->nummod, localtdata->tdata->buffer->f.numread) != 0) {
857 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
860 }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
861 if(transComProcess(localtdata->transinfo) != 0) {
862 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
868 printf("DEBUG -> Freeing...\n");
870 if (localtdata->transinfo->objmod != NULL) {
871 free(localtdata->transinfo->objmod);
872 localtdata->transinfo->objmod = NULL;
874 if (localtdata->transinfo->objlocked != NULL) {
875 free(localtdata->transinfo->objlocked);
876 localtdata->transinfo->objlocked = NULL;
878 if (localtdata->transinfo->objnotfound != NULL) {
879 free(localtdata->transinfo->objnotfound);
880 localtdata->transinfo->objnotfound = NULL;
885 /* This function completes the ABORT process if the transaction is aborting
887 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod, int numread) {
890 objheader_t *tmp_header;
893 printf("DEBUG -> Recv TRANS_ABORT\n");
894 /* Set all ref counts as 1 and do garbage collection */
896 for(i = 0; i< nummod; i++) {
897 tmp_header = (objheader_t *)ptr;
898 tmp_header->rcount = 1;
899 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
901 /* Unlock objects that was locked due to this transaction */
902 for(i = 0; i< numlocked; i++) {
903 header = mhashSearch(objlocked[i]);// find the header address
904 ((objheader_t *)header)->status &= ~(LOCK);
907 /* Send ack to Coordinator */
908 printf("DEBUG-> TRANS_SUCCESSFUL\n");
910 /*Free the pointer */
915 /*This function completes the COMMIT process is the transaction is commiting
917 int transComProcess(trans_commit_data_t *transinfo) {
919 int i = 0, offset = 0;
922 printf("DEBUG -> Recv TRANS_COMMIT\n");
923 /* Process each modified object saved in the mainobject store */
924 for(i=0; i<transinfo->nummod; i++) {
925 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
926 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
928 /* Change reference count of older address and free space in objstr ?? */
929 header->rcount = 1; //TODO Not sure what would be the val
931 /* Change ptr address in mhash table */
932 mhashRemove(transinfo->objmod[i]);
933 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
934 offset += sizeof(objheader_t) + classsize[header->type];
936 /* Update object version number */
937 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
938 header->version += 1;
941 /* Unlock locked objects */
942 for(i=0; i<transinfo->numlocked; i++) {
943 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
944 header->status &= ~(LOCK);
947 //TODO Update location lookup table
949 /* Send ack to Coordinator */
950 printf("DEBUG-> TRANS_SUCESSFUL\n");
954 /* This function checks if the prefetch oids are same and have same offsets
955 * for case x.a.b and y.a.b where x and y have same oid's
956 * or if a.b.c is a subset of x.b.c.d*/
957 /* check for case where the generated request a.y.z or x.y.z.g then
958 * prefetch needs to be generated for x.y.z.g if oid of a and x are same*/
959 void checkPrefetchTuples(prefetchqelem_t *node) {
960 int i,j, count,k, sindex, index;
962 int ntuples, slength;
964 short *endoffsets, *arryfields;
966 /* Check for the case x.y.z and a.b.c are same oids */
968 ntuples = *(GET_NTUPLES(ptr));
969 oid = GET_PTR_OID(ptr);
970 endoffsets = GET_PTR_EOFF(ptr, ntuples);
971 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
972 /* Find offset length for each tuple */
973 int numoffset[ntuples];
974 numoffset[0] = endoffsets[0];
975 for(i = 1; i<ntuples; i++) {
976 numoffset[i] = endoffsets[i] - endoffsets[i-1];
978 /* Check for redundant tuples by comparing oids of each tuple */
979 for(i = 0; i < ntuples; i++) {
982 for(j = i+1 ; j < ntuples; j++) {
985 /*If oids of tuples match */
986 if (oid[i] == oid[j]) {
987 /* Find the smallest offset length of two tuples*/
988 if(numoffset[i] > numoffset[j]){
989 slength = numoffset[j];
993 slength = numoffset[i];
997 /* Compare the offset values based on the current indices
998 * break if they do not match
999 * if all offset values match then pick the largest tuple*/
1003 index = endoffsets[j -1];
1004 for(count = 0; count < slength; count ++) {
1005 if (arryfields[k] != arryfields[index]) {
1012 k = endoffsets[i-1];
1013 index = endoffsets[j-1];
1014 printf("Value of slength = %d\n", slength);
1015 for(count = 0; count < slength; count++) {
1016 if(arryfields[k] != arryfields[index]) {
1024 if(slength == count) {
1032 void checkPreCache(prefetchqelem_t *node, int *numoffset, int counter, int loopcount, unsigned int objoid, int index, int iter, int oidnfound) {
1034 int ntuples, i, k, flag;
1036 short *endoffsets, *arryfields;
1037 objheader_t *header;
1039 ptr = (char *) node;
1040 ntuples = *(GET_NTUPLES(ptr));
1041 oid = GET_PTR_OID(ptr);
1042 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1043 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1045 if(oidnfound == 1) {
1046 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1048 } else { //Found in Prefetch Cache
1049 //TODO Decide if object is too old, if old remove from cache
1050 tmp = (char *) header;
1051 /* Check if any of the offset oid is available in the Prefetch cache */
1052 for(i = counter; i < loopcount; i++) {
1053 objoid = *(tmp + sizeof(objheader_t) + arryfields[counter]);
1054 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1063 for(i = counter; i<loopcount; i++) {
1064 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1065 tmp = (char *) header;
1066 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1076 /* If oid not found locally or in prefetch cache then
1077 * assign the latest oid found as the new oid
1078 * and copy left over offsets into the arrayoffsetfieldarray*/
1080 numoffset[iter] = numoffset[iter] - (i+1);
1081 for(k = 0; k < numoffset[iter] ; k++) {
1082 arryfields[endoffsets[counter]+k] = arryfields[endoffsets[counter]+k+1];
1087 numoffset[iter] = 0;
1091 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1092 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1094 int ntuples, slength, i, machinenum;
1097 short *endoffsets, *arryfields, *offset;
1098 prefetchpile_t *head = NULL;
1100 /* Check for the case x.y.z and a.b.c are same oids */
1101 ptr = (char *) node;
1102 ntuples = *(GET_NTUPLES(ptr));
1103 oid = GET_PTR_OID(ptr);
1104 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1105 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1107 /* Check for redundant tuples by comparing oids of each tuple */
1108 for(i = 0; i < ntuples; i++) {
1111 /* For each tuple make piles */
1112 if ((machinenum = lhashSearch(oid[i])) == 0) {
1113 printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1116 /* Insert into machine pile */
1117 offset = &arryfields[endoffsets[i-1]];
1118 insertPile(machinenum, oid[i], numoffset[i], offset, head);
1125 /* This function checks if the oids within the prefetch tuples are available locally.
1126 * If yes then makes the tuple invalid. If no then rearranges oid and offset values in
1127 * the prefetchqelem_t node to represent a new prefetch tuple */
1128 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1129 int ntuples,i, j, k, oidnfound = 0, index, flag;
1131 unsigned int objoid;
1133 objheader_t *objheader;
1134 short *endoffsets, *arryfields;
1135 prefetchpile_t *head = NULL;
1137 ptr = (char *) node;
1138 ntuples = *(GET_NTUPLES(ptr));
1139 oid = GET_PTR_OID(ptr);
1140 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1141 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1142 /* Find offset length for each tuple */
1143 int numoffset[ntuples];//Number of offsets for each tuple
1144 numoffset[0] = endoffsets[0];
1145 for(i = 1; i<ntuples; i++) {
1146 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1148 for(i = 0; i < ntuples; i++) {
1151 /* If object found locally */
1152 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1154 tmp = (char *) objheader;
1155 /* Find the oid of its offset value */
1159 index = endoffsets[i - 1];
1160 for(j = 0 ; j < numoffset[i] ; j++) {
1161 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1162 /*If oid found locally then
1163 *assign the latest oid found as the new oid
1164 *and copy left over offsets into the arrayoffsetfieldarray*/
1166 numoffset[i] = numoffset[i] - (j+1);
1167 for(k = 0; k < numoffset[i]; k++)
1168 arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1170 /*New offset oid not found */
1171 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1173 checkPreCache(node, numoffset, j, numoffset[i], objoid, index, i, oidnfound);
1179 /*If all offset oids are found locally,make the prefetch tuple invalid */
1186 /* Look in Prefetch cache */
1187 checkPreCache(node, numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound);
1191 /* Make machine groups */
1192 head = makePreGroups(node, numoffset);
1196 /* This function is called by the thread calling transPrefetch */
1197 void *transPrefetch(void *t) {
1198 //int *offstarray = NULL;
1199 prefetchqelem_t *qnode;
1200 prefetchpile_t *pilehead = NULL;
1203 /* lock mutex of primary prefetch queue */
1204 pthread_mutex_lock(&pqueue.qlock);
1205 /* while primary queue is empty, then wait */
1206 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1207 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1210 /* dequeue node to create a machine piles and finally unlock mutex */
1211 if((qnode = dequeue()) == NULL) {
1212 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1215 pthread_mutex_unlock(&pqueue.qlock);
1216 /* Reduce redundant prefetch requests */
1217 checkPrefetchTuples(qnode);
1218 /* Check if the tuples are found locally, if yes then reduce them further*/
1219 /* and group requests by remote machine ids by calling the makePreGroups() */
1220 pilehead = foundLocal(qnode);
1222 /* Lock mutex of pool queue */
1223 pthread_mutex_lock(&mcqueue.qlock);
1224 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1225 mcpileenqueue(pilehead);
1226 /* Broadcast signal on machine pile queue */
1227 pthread_cond_broadcast(&mcqueue.qcond);
1228 /* Unlock mutex of machine pile queue */
1229 pthread_mutex_unlock(&mcqueue.qlock);
1230 /* Deallocate the prefetch queue pile node */
1236 /*The pool of threads work on this function to establish connection with
1237 * remote machines */
1239 void *mcqProcess(void *threadid) {
1241 prefetchpile_t *mcpilenode;
1243 tid = (int) threadid;
1245 /* Lock mutex of mc pile queue */
1246 pthread_mutex_lock(&mcqueue.qlock);
1247 /* When mc pile queue is empty, wait */
1248 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1249 pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1251 /* Dequeue node to send remote machine connections*/
1252 if((mcpilenode = mcpiledequeue()) == NULL) {
1253 printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1257 pthread_mutex_unlock(&mcqueue.qlock);
1259 /*Initiate connection to remote host and send request */
1260 /* Process Request */
1261 sendPrefetchReq(mcpilenode, tid);
1262 /* TODO: For each object not found query DHT for new location and retrieve the object */
1264 /* Deallocate the machine queue pile node */
1265 mcdealloc(mcpilenode);
1269 /*This function is called by the thread that processes the
1270 * prefetch request makes piles to prefetch records and prefetches the oids from remote machines */
1271 int transPrefetchProcess(transrecord_t *record, int *arrayofoffset[], short numoids){
1273 int arraylength[numoids];
1274 unsigned int machinenumber;
1275 objheader_t *tmp, *objheader;
1278 pthread_attr_t attr;
1280 /* Given tuple find length of tuple*/
1281 for(i = 0; i < numoids ; i++) {
1282 arraylength[i] = arrayLength(arrayofoffset[i]);
1285 /* Initialize and set thread attributes
1286 * Spawn a thread for each prefetch request sent*/
1287 pthread_t thread[numoids];
1288 pthread_attr_init(&attr);
1289 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1291 /* Create Machine Piles to send prefetch requests use threads*/
1292 for( i = 0 ; i< numoids ; i++) {
1293 if(arrayofoffset[i][0] == -1)
1296 /* For each Pile in the machine send TRANS_PREFETCH */
1297 //makePiles(arrayofoffset, numoids);
1298 /* Fill thread data structure */
1299 //rc = pthread_create(&thread[i] , &attr, sendPrefetchReq, (void *) arrayofoffset[i]);
1301 perror("Error in pthread create at transPrefetchProcess()\n");
1308 /* Free attribute and wait to join other threads */
1309 for (i = 0 ;i < numoids ; i++) {
1310 rc = pthread_join(thread[i], NULL);
1312 perror("Error pthread_join() in transPrefetchProcess()\n");
1316 pthread_attr_destroy(&attr);
1322 void sendPrefetchReq(prefetchpile_t *mcpilenode, int threadid) {
1323 int sd, i, offset, off, len, endpair, numoffsets, count = 0;
1324 struct sockaddr_in serv_addr;
1325 struct hostent *server;
1326 char machineip[16], control;
1330 /* Send Trans Prefetch Request */
1331 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1332 perror("Error in socket for TRANS_REQUEST\n");
1335 bzero((char*) &serv_addr, sizeof(serv_addr));
1336 serv_addr.sin_family = AF_INET;
1337 serv_addr.sin_port = htons(LISTEN_PORT);
1338 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
1339 midtoIP(mcpilenode->mid ,machineip);
1340 machineip[15] = '\0';
1341 serv_addr.sin_addr.s_addr = inet_addr(machineip);
1343 /* Open Connection */
1344 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1345 perror("Error in connect for TRANS_REQUEST\n");
1349 /* Send TRANS_PREFETCH control message */
1350 control = TRANS_PREFETCH;
1351 if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1352 perror("Error in sending prefetch control\n");
1356 /* Send Oids and offsets in pairs */
1357 tmp = mcpilenode->objpiles;
1358 while(tmp != NULL) {
1360 count++; // Keeps track of the number of oid and offset tuples sent per remote machine
1361 len = sizeof(int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1362 char oidnoffset[len];
1363 memcpy(oidnoffset, &len, sizeof(int));
1365 memcpy(oidnoffset + off, &tmp->oid, sizeof(unsigned int));
1366 off += sizeof(unsigned int);
1367 for(i = 0; i < numoffsets; i++) {
1368 offset = off + (i * sizeof(short));
1369 memcpy(oidnoffset + offset, tmp->offset, sizeof(short));
1371 if (send(sd, &oidnoffset, sizeof(oidnoffset),MSG_NOSIGNAL) < sizeof(oidnoffset)) {
1372 perror("Error sending fixed bytes for thread\n");
1378 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1380 if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1381 perror("Error sending fixed bytes for thread\n");
1385 /* Get Response from the remote machine */
1386 getPrefetchResponse(count,sd);
1390 void getPrefetchResponse(int count, int sd) {
1391 int i = 0, val, n, N, sum, index, objsize;
1392 unsigned int bufsize,oid;
1393 char buffer[RECEIVE_BUFFER_SIZE], control;
1397 /* Read prefetch response from the Remote machine */
1398 if((val = read(sd, &control, sizeof(char))) <= 0) {
1399 perror("No control response for Prefetch request sent\n");
1402 if(control == TRANS_PREFETCH_RESPONSE) {
1403 /*For each oid and offset tuple sent as prefetch request to remote machine*/
1405 /* Clear contents of buffer */
1406 memset(buffer, 0, RECEIVE_BUFFER_SIZE);
1409 /* Read the size of buffer to be received */
1410 if((N = read(sd, buffer, sizeof(unsigned int))) <= 0) {
1411 perror("Size of buffer not recv\n");
1414 memcpy(&bufsize, buffer, sizeof(unsigned int));
1415 ptr = buffer + sizeof(unsigned int);
1416 /* Keep receiving the buffer containing oid info */
1418 n = recv((int)sd, (void *)ptr+sum, bufsize-sum, 0);
1420 } while(sum < bufsize && n != 0);
1421 /* Decode the contents of the buffer */
1422 index = sizeof(unsigned int);
1423 while(index < (bufsize - sizeof(unsigned int))) {
1424 if(buffer[index] == OBJECT_FOUND) {
1425 /* Increment it to get the object */
1426 index += sizeof(char);
1427 memcpy(&oid, buffer + index, sizeof(unsigned int));
1428 index += sizeof(unsigned int);
1429 /* Lock the Prefetch Cache look up table*/
1430 pthread_mutex_lock(&pflookup.lock);
1431 /* For each object found add to Prefetch Cache */
1432 memcpy(&objsize, buffer + index, sizeof(int));
1433 if ((modptr = objstrAlloc(prefetchcache, objsize)) == NULL) {
1434 printf("objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1437 memcpy(modptr, buffer+index, objsize);
1438 index += sizeof(int);
1439 /* Add pointer and oid to hash table */
1440 //TODO Do we need a version comparison here??
1441 prehashInsert(oid, modptr);
1442 /* Broadcast signal on prefetch cache condition variable */
1443 pthread_cond_broadcast(&pflookup.cond);
1444 /* Unlock the Prefetch Cache look up table*/
1445 pthread_mutex_unlock(&pflookup.lock);
1446 } else if(buffer[index] == OBJECT_NOT_FOUND) {
1447 /* Increment it to get the object */
1448 // TODO If object not found, local machine takes inventory
1449 index += sizeof(char);
1450 memcpy(&oid, buffer + index, sizeof(unsigned int));
1451 index += sizeof(unsigned int);
1453 printf("Error in decoding the index value %s, %d\n",__FILE__, __LINE__);
1459 printf("Error in receving response for prefetch request %s, %d\n",__FILE__, __LINE__);