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);
83 /* This function allocates an object on the local machine */
86 void * dstmalloc(transrecord_t *trans, int size) {
87 objheader_t * newobj=(objheader_t *)objstrAlloc(trans->cache, size+sizeof(objheader_t));
93 /* This function starts up the transaction runtime. */
94 int dstmStartup(const char * option) {
95 pthread_t thread_Listen;
97 int master=strcmp(option, "master")==0;
103 pthread_attr_init(&attr);
104 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
105 pthread_create(&thread_Listen, &attr, dstmListen, NULL);
115 /* This function initiates the prefetch thread
116 * A queue is shared between the main thread of execution
117 * and the prefetch thread to process the prefetch call
118 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
119 * processes the prefetch requests */
122 //Create and initialize prefetch cache structure
123 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
124 //Create prefetch cache lookup table
125 if(prehashCreate(HASH_SIZE, LOADFACTOR))
127 //Initialize primary shared queue
129 //Initialize machine pile w/prefetch oids and offsets shared queue
131 //Create the primary prefetch thread
132 pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
133 //Create and Initialize a pool of threads
134 /* Threads are active for the entire period runtime is running */
135 for(t = 0; t< NUM_THREADS; t++) {
136 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
138 printf("Thread create error %s, %d\n", __FILE__, __LINE__);
144 /* This function stops the threads spawned */
147 pthread_cancel(tPrefetch);
148 for(t = 0; t < NUM_THREADS; t++)
149 pthread_cancel(wthreads[t]);
154 /* This functions inserts randowm wait delays in the order of msec
155 * Mostly used when transaction commits retry*/
156 void randomdelay(void)
158 struct timespec req, rem;
163 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
164 nanosleep(&req, &rem);
168 /* This function initializes things required in the transaction start*/
169 transrecord_t *transStart()
171 transrecord_t *tmp = malloc(sizeof(transrecord_t));
172 tmp->cache = objstrCreate(1048576);
173 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
175 tmp->revertlist=NULL;
180 /* This function finds the location of the objects involved in a transaction
181 * and returns the pointer to the object if found in a remote location */
182 objheader_t *transRead(transrecord_t *record, unsigned int oid) {
183 printf("Inside transaction read call\n");
184 unsigned int machinenumber;
185 objheader_t *tmp, *objheader;
187 int size, rc, found = 0;
192 rc = gettimeofday(&tp, NULL);
194 /* Convert from timeval to timespec */
195 ts.tv_nsec = tp.tv_usec * 1000;
197 /* Search local transaction cache */
198 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
199 printf("Inside transaction cache \n");
201 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
202 /* Look up in machine lookup table and copy into cache*/
203 printf("Inside mainobject store \n");
204 tmp = mhashSearch(oid);
205 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
206 objcopy = objstrAlloc(record->cache, size);
207 memcpy(objcopy, (void *)objheader, size);
208 /* Insert into cache's lookup table */
209 chashInsert(record->lookupTable, OID(objheader), objcopy);
211 } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
212 printf("Inside prefetch cache \n");
214 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
215 objcopy = objstrAlloc(record->cache, size);
216 memcpy(objcopy, (void *)tmp, size);
217 /* Insert into cache's lookup table */
218 chashInsert(record->lookupTable, OID(tmp), objcopy);
220 } else { /* If not found anywhere, then block until object appears in prefetch cache */
222 printf("Inside remote machine\n");
223 pthread_mutex_lock(&pflookup.lock);
225 rc = pthread_cond_timedwait(&pflookup.cond, &pflookup.lock, &ts);
226 if(rc == ETIMEDOUT) {
227 printf("Wait timed out\n");
228 /* Check Prefetch cache again */
229 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
231 size = sizeof(objheader_t)+classsize[TYPE(tmp)];
232 objcopy = objstrAlloc(record->cache, size);
233 memcpy(objcopy, (void *)tmp, size);
234 /* Insert into cache's lookup table */
235 chashInsert(record->lookupTable, OID(tmp), objcopy);
238 pthread_mutex_unlock(&pflookup.lock);
241 pthread_mutex_unlock(&pflookup.lock);
245 /* Get the object from the remote location */
246 machinenumber = lhashSearch(oid);
247 objcopy = getRemoteObj(record, machinenumber, oid);
248 if(objcopy == NULL) {
249 //If object is not found in Remote location
258 /* This function creates objects in the transaction record */
259 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
261 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
262 OID(tmp) = getNewOID();
265 tmp->rcount = 0; //? not sure how to handle this yet
267 chashInsert(record->lookupTable, OID(tmp), tmp);
271 /* This function creates machine piles based on all machines involved in a
272 * transaction commit request */
273 plistnode_t *createPiles(transrecord_t *record) {
275 unsigned int size;/* Represents number of bins in the chash table */
276 chashlistnode_t *curr, *ptr, *next;
277 plistnode_t *pile = NULL;
278 unsigned int machinenum;
279 void *localmachinenum;
280 objheader_t *headeraddr;
282 ptr = record->lookupTable->table;
283 size = record->lookupTable->size;
285 for(i = 0; i < size ; i++) {
287 /* Inner loop to traverse the linked list of the cache lookupTable */
288 while(curr != NULL) {
289 //if the first bin in hash table is empty
294 //Get machine location for object id
295 //TODO Check is the object is newly created ...if not then lookup the location table
297 if ((machinenum = lhashSearch(curr->key)) == 0) {
298 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
302 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
303 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
306 //Make machine groups
307 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
308 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
312 /* Check if local or not */
313 if((localmachinenum = mhashSearch(curr->key)) != NULL) {
314 /* Set the pile->local flag*/
315 pile->local = 1; //True i.e. local
324 /* This function initiates the transaction commit process
325 * Spawns threads for each of the new connections with Participants
326 * and creates new piles by calling the createPiles(),
327 * Sends a transrequest() to each remote machines for objects found remotely
328 * and calls handleLocalReq() to process objects found locally */
329 int transCommit(transrecord_t *record) {
330 unsigned int tot_bytes_mod, *listmid;
331 plistnode_t *pile, *pile_ptr;
333 int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
334 char buffer[RECEIVE_BUFFER_SIZE],control;
335 char transid[TID_LEN];
336 trans_req_data_t *tosend;
337 trans_commit_data_t transinfo;
338 static int newtid = 0;
339 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
343 /* Look through all the objects in the transaction record and make piles
344 * for each machine involved in the transaction*/
345 pile_ptr = pile = createPiles(record);
347 /* Create the packet to be sent in TRANS_REQUEST */
349 /* Count the number of participants */
350 pilecount = pCount(pile);
352 /* Create a list of machine ids(Participants) involved in transaction */
353 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
354 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
357 pListMid(pile, listmid);
360 /* Initialize thread variables,
361 * Spawn a thread for each Participant involved in a transaction */
362 pthread_t thread[pilecount];
364 pthread_cond_t tcond;
365 pthread_mutex_t tlock;
366 pthread_mutex_t tlshrd;
368 thread_data_array_t *thread_data_array;
369 thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
370 local_thread_data_array_t *ltdata;
371 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
372 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
376 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
378 /* Initialize and set thread detach attribute */
379 pthread_attr_init(&attr);
380 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
381 pthread_mutex_init(&tlock, NULL);
382 pthread_cond_init(&tcond, NULL);
384 /* Process each machine pile */
385 while(pile != NULL) {
386 //Create transaction id
388 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
389 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
392 tosend->f.control = TRANS_REQUEST;
393 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
394 tosend->f.mcount = pilecount;
395 tosend->f.numread = pile->numread;
396 tosend->f.nummod = pile->nummod;
397 tosend->f.sum_bytes = pile->sum_bytes;
398 tosend->listmid = listmid;
399 tosend->objread = pile->objread;
400 tosend->oidmod = pile->oidmod;
401 thread_data_array[threadnum].thread_id = threadnum;
402 thread_data_array[threadnum].mid = pile->mid;
403 thread_data_array[threadnum].pilecount = pilecount;
404 thread_data_array[threadnum].buffer = tosend;
405 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
406 thread_data_array[threadnum].threshold = &tcond;
407 thread_data_array[threadnum].lock = &tlock;
408 thread_data_array[threadnum].count = &trecvcount;
409 thread_data_array[threadnum].replyctrl = &treplyctrl;
410 thread_data_array[threadnum].replyretry = &treplyretry;
411 thread_data_array[threadnum].rec = record;
412 /* If local do not create any extra connection */
413 if(pile->local != 1) { /* Not local */
414 rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);
416 perror("Error in pthread create\n");
420 /*Unset the pile->local flag*/
422 /*Set flag to identify that Local machine is involved*/
423 ltdata->tdata = &thread_data_array[threadnum];
424 ltdata->transinfo = &transinfo;
425 val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
427 perror("Error in pthread create\n");
435 /* Free attribute and wait for the other threads */
436 pthread_attr_destroy(&attr);
437 for (i = 0 ;i < pilecount ; i++) {
438 rc = pthread_join(thread[i], NULL);
441 printf("ERROR return code from pthread_join() is %d\n", rc);
444 free(thread_data_array[i].buffer);
448 pthread_cond_destroy(&tcond);
449 pthread_mutex_destroy(&tlock);
452 free(thread_data_array);
455 /* Retry trans commit procedure if not sucessful in the first try */
456 if(treplyretry == 1) {
457 /* wait a random amount of time */
459 /* Retry the commiting transaction again */
466 /* This function sends information involved in the transaction request
467 * to participants and accepts a response from particpants.
468 * It calls decideresponse() to decide on what control message
469 * to send next to participants and sends the message using sendResponse()*/
470 void *transRequest(void *threadarg) {
472 struct sockaddr_in serv_addr;
473 struct hostent *server;
474 thread_data_array_t *tdata;
475 objheader_t *headeraddr;
476 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
477 char machineip[16], retval;
479 tdata = (thread_data_array_t *) threadarg;
481 /* Send Trans Request */
482 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
483 perror("Error in socket for TRANS_REQUEST\n");
486 bzero((char*) &serv_addr, sizeof(serv_addr));
487 serv_addr.sin_family = AF_INET;
488 serv_addr.sin_port = htons(LISTEN_PORT);
489 midtoIP(tdata->mid,machineip);
490 machineip[15] = '\0';
491 serv_addr.sin_addr.s_addr = inet_addr(machineip);
492 /* Open Connection */
493 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
494 perror("Error in connect for TRANS_REQUEST\n");
498 printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
499 /* Send bytes of data with TRANS_REQUEST control message */
500 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
501 perror("Error sending fixed bytes for thread\n");
504 /* Send list of machines involved in the transaction */
506 int size=sizeof(unsigned int)*tdata->pilecount;
507 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
508 perror("Error sending list of machines for thread\n");
512 /* Send oids and version number tuples for objects that are read */
514 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
515 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
516 perror("Error sending tuples for thread\n");
520 /* Send objects that are modified */
521 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
523 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
524 size=sizeof(objheader_t)+classsize[TYPE(headeraddr)];
525 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
526 perror("Error sending obj modified for thread\n");
531 /* Read control message from Participant */
532 if((n = read(sd, &control, sizeof(char))) <= 0) {
533 perror("Error in reading control message from Participant\n");
536 recvcontrol = control;
538 /* Update common data structure and increment count */
539 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
541 /* Lock and update count */
542 /* Thread sleeps until all messages from pariticipants are received by coordinator */
543 pthread_mutex_lock(tdata->lock);
545 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
547 /* Wake up the threads and invoke decideResponse (once) */
548 if(*(tdata->count) == tdata->pilecount) {
549 if (decideResponse(tdata) != 0) {
550 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
551 pthread_mutex_unlock(tdata->lock);
555 pthread_cond_broadcast(tdata->threshold);
557 pthread_cond_wait(tdata->threshold, tdata->lock);
559 pthread_mutex_unlock(tdata->lock);
561 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
562 * to all participants in their respective socket */
563 if (sendResponse(tdata, sd) == 0) {
564 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
565 pthread_mutex_unlock(tdata->lock);
570 /* Close connection */
575 /* This function decides the reponse that needs to be sent to
576 * all Participant machines after the TRANS_REQUEST protocol */
577 int decideResponse(thread_data_array_t *tdata) {
579 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
582 for (i = 0 ; i < tdata->pilecount ; i++) {
583 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
584 written onto the shared array */
587 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
592 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
596 case TRANS_SOFT_ABORT:
597 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
601 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
607 if(transdisagree > 0) {
608 *(tdata->replyctrl) = TRANS_ABORT;
609 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
611 objstrDelete(tdata->rec->cache);
612 chashDelete(tdata->rec->lookupTable);
614 } else if(transagree == tdata->pilecount){
616 *(tdata->replyctrl) = TRANS_COMMIT;
617 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
619 objstrDelete(tdata->rec->cache);
620 chashDelete(tdata->rec->lookupTable);
622 } else if(transsoftabort > 0 && transdisagree == 0) {
623 /* Send Abort in soft abort case followed by retry commiting transaction again*/
624 *(tdata->replyctrl) = TRANS_ABORT;
625 *(tdata->replyretry) = 1;
626 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
628 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
634 /* This function sends the final response to remote machines per thread in their respective socket id */
635 char sendResponse(thread_data_array_t *tdata, int sd) {
636 int n, N, sum, oidcount = 0;
637 char *ptr, retval = 0;
638 unsigned int *oidnotfound;
640 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
641 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
642 /* Read list of objects missing */
643 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
644 N = oidcount * sizeof(unsigned int);
645 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
646 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
649 ptr = (char *) oidnotfound;
651 n = read(sd, ptr+sum, N-sum);
653 } while(sum < N && n !=0);
655 retval = TRANS_SOFT_ABORT;
657 /* If the decided response is TRANS_ABORT */
658 if(*(tdata->replyctrl) == TRANS_ABORT) {
659 retval = TRANS_ABORT;
660 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
661 retval = TRANS_COMMIT;
664 if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
665 perror("Error sending ctrl message for participant\n");
671 /* This function opens a connection, places an object read request to the
672 * remote machine, reads the control message and object if available and
673 * copies the object and its header to the local cache.
674 * TODO replace mnum and midtoIP() with MACHINE_IP address later */
676 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
678 struct sockaddr_in serv_addr;
679 struct hostent *server;
685 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
686 perror("Error in socket\n");
689 bzero((char*) &serv_addr, sizeof(serv_addr));
690 serv_addr.sin_family = AF_INET;
691 serv_addr.sin_port = htons(LISTEN_PORT);
692 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
693 midtoIP(mnum,machineip);
694 machineip[15] = '\0';
695 serv_addr.sin_addr.s_addr = inet_addr(machineip);
696 /* Open connection */
697 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
698 perror("Error in connect\n");
701 char readrequest[sizeof(char)+sizeof(unsigned int)];
702 readrequest[0] = READ_REQUEST;
703 *((unsigned int *)(&readrequest[1])) = oid;
704 if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
705 perror("Error sending message\n");
710 printf("DEBUG -> ready to rcv ...\n");
712 /* Read response from the Participant */
713 if((val = read(sd, &control, sizeof(char))) <= 0) {
714 perror("No control response for getRemoteObj sent\n");
718 case OBJECT_NOT_FOUND:
719 printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
722 /* Read object if found into local cache */
723 if((val = read(sd, &size, sizeof(int))) <= 0) {
724 perror("No size is read from the participant\n");
727 objcopy = objstrAlloc(record->cache, size);
728 if((val = read(sd, objcopy, size)) <= 0) {
729 perror("No objects are read from the remote participant\n");
732 /* Insert into cache's lookup table */
733 chashInsert(record->lookupTable, oid, objcopy);
736 printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
739 /* Close connection */
744 /* This function handles the local objects involved in a transaction commiting process.
745 * It also makes a decision if this local machine sends AGREE or DISAGREE or SOFT_ABORT to coordinator.
746 * Note Coordinator = local machine
747 * It wakes up the other threads from remote participants that are waiting for the coordinator's decision and
748 * based on common agreement it either commits or aborts the transaction.
749 * It also frees the memory resources */
750 void *handleLocalReq(void *threadarg) {
751 int val, i = 0, size, offset = 0;
753 char control = 0, *ptr;
755 unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
757 objheader_t *headptr, *headeraddr;
758 local_thread_data_array_t *localtdata;
760 localtdata = (local_thread_data_array_t *) threadarg;
762 /* Counters and arrays to formulate decision on control message to be sent */
763 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
764 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
765 int objnotfound = 0, objlocked = 0;
766 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
768 /* modptr points to the beginning of the object store
769 * created at the Pariticipant */
770 if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
771 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
774 /* Write modified objects into the mainobject store */
775 for(i = 0; i< localtdata->tdata->buffer->f.nummod; i++) {
776 headeraddr = chashSearch(localtdata->tdata->rec->lookupTable, localtdata->tdata->buffer->oidmod[i]);
777 size = sizeof(objheader_t) + classsize[TYPE(headeraddr)];
778 memcpy(modptr+offset, headeraddr, size);
785 /* Process each oid in the machine pile/ group per thread */
786 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
787 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
788 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
790 oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
791 incr += sizeof(unsigned int);
792 version = *((short *)(localtdata->tdata->buffer->objread + incr));
793 } else {//Objs modified
794 headptr = (objheader_t *)ptr;
796 version = headptr->version;
797 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
800 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
802 /* Save the oids not found and number of oids not found for later use */
803 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
804 /* Save the oids not found and number of oids not found for later use */
805 oidnotfound[objnotfound] = oid;
807 } else { /* If Obj found in machine (i.e. has not moved) */
808 /* Check if Obj is locked by any previous transaction */
809 if (STATUS(((objheader_t *)mobj)) & LOCK) {
810 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
812 } else {/* If versions don't match ...HARD ABORT */
814 /* Send TRANS_DISAGREE to Coordinator */
815 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
816 printf("DEBUG -> Sending TRANS_DISAGREE\n");
818 } else {/* If Obj is not locked then lock object */
819 STATUS(((objheader_t *)mobj)) |= LOCK;
820 //TODO Remove this for Testing
823 /* Save all object oids that are locked on this machine during this transaction request call */
824 oidlocked[objlocked] = OID(((objheader_t *)mobj));
826 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
828 } else { /* If versions don't match ...HARD ABORT */
830 /* Send TRANS_DISAGREE to Coordinator */
831 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
832 printf("DEBUG -> Sending TRANS_DISAGREE\n");
838 /* Condition to send TRANS_AGREE */
839 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
840 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
841 printf("DEBUG -> Sending TRANS_AGREE\n");
843 /* Condition to send TRANS_SOFT_ABORT */
844 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
845 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
846 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
847 //TODO currently the only soft abort case that is supported is when object locked by previous
848 //transaction => v_matchlock > 0
849 //The other case for SOFT ABORT i.e. when object is not found but versions match is not supported
850 /* Send number of oids not found and the missing oids if objects are missing in the machine */
851 /* TODO Remember to store the oidnotfound for later use
852 if(objnotfound != 0) {
853 int size = sizeof(unsigned int)* objnotfound;
858 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
859 * if Participant receives a TRANS_COMMIT */
860 localtdata->transinfo->objlocked = oidlocked;
861 localtdata->transinfo->objnotfound = oidnotfound;
862 localtdata->transinfo->modptr = modptr;
863 localtdata->transinfo->numlocked = objlocked;
864 localtdata->transinfo->numnotfound = objnotfound;
866 /* Lock and update count */
867 //Thread sleeps until all messages from pariticipants are received by coordinator
868 pthread_mutex_lock(localtdata->tdata->lock);
869 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
871 /* Wake up the threads and invoke decideResponse (once) */
872 if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
873 if (decideResponse(localtdata->tdata) != 0) {
874 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
875 pthread_mutex_unlock(localtdata->tdata->lock);
878 pthread_cond_broadcast(localtdata->tdata->threshold);
880 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
882 pthread_mutex_unlock(localtdata->tdata->lock);
884 /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
885 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
886 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->tdata->buffer->f.nummod) != 0) {
887 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
890 }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
891 if(transComProcess(modptr, localtdata->tdata->buffer->oidmod, oidlocked, localtdata->tdata->buffer->f.nummod, localtdata->transinfo->numlocked) != 0) {
892 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
898 printf("DEBUG -> Freeing...\n");
901 if (localtdata->transinfo->objlocked != NULL) {
902 free(localtdata->transinfo->objlocked);
903 localtdata->transinfo->objlocked = NULL;
905 if (localtdata->transinfo->objnotfound != NULL) {
906 free(localtdata->transinfo->objnotfound);
907 localtdata->transinfo->objnotfound = NULL;
912 /* This function completes the ABORT process if the transaction is aborting
914 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod) {
917 objheader_t *tmp_header;
920 printf("DEBUG -> Recv TRANS_ABORT\n");
921 /* Set all ref counts as 1 and do garbage collection */
922 ptr = (char *)modptr;
923 for(i = 0; i< nummod; i++) {
924 tmp_header = (objheader_t *)ptr;
925 tmp_header->rcount = 1;
926 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
928 /* Unlock objects that was locked due to this transaction */
929 for(i = 0; i< numlocked; i++) {
930 if((header = mhashSearch(objlocked[i])) == NULL) {
931 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
934 STATUS(((objheader_t *)header)) &= ~(LOCK);
937 /* Send ack to Coordinator */
938 printf("DEBUG-> TRANS_SUCCESSFUL\n");
940 /*Free the pointer */
945 /*This function completes the COMMIT process is the transaction is commiting
947 int transComProcess(void *modptr, unsigned int *oidmod, unsigned int *objlocked, int nummod, int numlocked) {
949 int i = 0, offset = 0;
952 /* Process each modified object saved in the mainobject store */
953 for(i = 0; i < nummod; i++) {
954 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
955 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
958 /* Change reference count of older address and free space in objstr ?? */
959 header->rcount = 1; //TODO Not sure what would be the val
961 /* Change ptr address in mhash table */
962 mhashRemove(oidmod[i]);
963 mhashInsert(oidmod[i], (((char *)modptr) + offset));
964 offset += sizeof(objheader_t) + classsize[TYPE(header)];
966 /* Update object version number */
967 header = (objheader_t *) mhashSearch(oidmod[i]);
968 header->version += 1;
971 /* Unlock locked objects */
972 for(i = 0; i < numlocked; i++) {
973 if((header = (objheader_t *) mhashSearch(objlocked[i])) == NULL) {
974 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
977 STATUS(header) &= ~(LOCK);
980 //TODO Update location lookup table
982 /* Send ack to Coordinator */
983 printf("DEBUG-> TRANS_SUCESSFUL\n");
987 /* This function checks if the prefetch oids are same and have same offsets
988 * for case x.a.b and y.a.b where x and y have same oid's
989 * or if a.b.c is a subset of x.b.c.d*/
990 /* check for case where the generated request a.y.z or x.y.z.g then
991 * prefetch needs to be generated for x.y.z.g if oid of a and x are same*/
992 void checkPrefetchTuples(prefetchqelem_t *node) {
993 int i,j, count,k, sindex, index;
995 int ntuples, slength;
997 short *endoffsets, *arryfields;
999 /* Check for the case x.y.z and a.b.c are same oids */
1000 ptr = (char *) node;
1001 ntuples = *(GET_NTUPLES(ptr));
1002 oid = GET_PTR_OID(ptr);
1003 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1004 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1005 /* Find offset length for each tuple */
1006 int numoffset[ntuples];
1007 numoffset[0] = endoffsets[0];
1008 for(i = 1; i<ntuples; i++) {
1009 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1011 /* Check for redundant tuples by comparing oids of each tuple */
1012 for(i = 0; i < ntuples; i++) {
1015 for(j = i+1 ; j < ntuples; j++) {
1018 /*If oids of tuples match */
1019 if (oid[i] == oid[j]) {
1020 /* Find the smallest offset length of two tuples*/
1021 if(numoffset[i] > numoffset[j]){
1022 slength = numoffset[j];
1026 slength = numoffset[i];
1030 /* Compare the offset values based on the current indices
1031 * break if they do not match
1032 * if all offset values match then pick the largest tuple*/
1036 index = endoffsets[j -1];
1037 for(count = 0; count < slength; count ++) {
1038 if (arryfields[k] != arryfields[index]) {
1045 k = endoffsets[i-1];
1046 index = endoffsets[j-1];
1047 printf("Value of slength = %d\n", slength);
1048 for(count = 0; count < slength; count++) {
1049 if(arryfields[k] != arryfields[index]) {
1057 if(slength == count) {
1065 void checkPreCache(prefetchqelem_t *node, int *numoffset, int counter, int loopcount, unsigned int objoid, int index, int iter, int oidnfound) {
1067 int ntuples, i, k, flag;
1069 short *endoffsets, *arryfields;
1070 objheader_t *header;
1072 ptr = (char *) node;
1073 ntuples = *(GET_NTUPLES(ptr));
1074 oid = GET_PTR_OID(ptr);
1075 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1076 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1078 if(oidnfound == 1) {
1079 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1081 } else { //Found in Prefetch Cache
1082 //TODO Decide if object is too old, if old remove from cache
1083 tmp = (char *) header;
1084 /* Check if any of the offset oid is available in the Prefetch cache */
1085 for(i = counter; i < loopcount; i++) {
1086 objoid = *(tmp + sizeof(objheader_t) + arryfields[counter]);
1087 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1096 for(i = counter; i<loopcount; i++) {
1097 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1098 tmp = (char *) header;
1099 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1109 /* If oid not found locally or in prefetch cache then
1110 * assign the latest oid found as the new oid
1111 * and copy left over offsets into the arrayoffsetfieldarray*/
1113 numoffset[iter] = numoffset[iter] - (i+1);
1114 for(k = 0; k < numoffset[iter] ; k++) {
1115 arryfields[endoffsets[counter]+k] = arryfields[endoffsets[counter]+k+1];
1120 numoffset[iter] = 0;
1124 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1125 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1127 int ntuples, slength, i, machinenum;
1130 short *endoffsets, *arryfields, *offset;
1131 prefetchpile_t *head = NULL;
1133 /* Check for the case x.y.z and a.b.c are same oids */
1134 ptr = (char *) node;
1135 ntuples = *(GET_NTUPLES(ptr));
1136 oid = GET_PTR_OID(ptr);
1137 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1138 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1140 /* Check for redundant tuples by comparing oids of each tuple */
1141 for(i = 0; i < ntuples; i++) {
1144 /* For each tuple make piles */
1145 if ((machinenum = lhashSearch(oid[i])) == 0) {
1146 printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1149 /* Insert into machine pile */
1150 offset = &arryfields[endoffsets[i-1]];
1151 insertPile(machinenum, oid[i], numoffset[i], offset, head);
1158 /* This function checks if the oids within the prefetch tuples are available locally.
1159 * If yes then makes the tuple invalid. If no then rearranges oid and offset values in
1160 * the prefetchqelem_t node to represent a new prefetch tuple */
1161 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1162 int ntuples,i, j, k, oidnfound = 0, index, flag;
1164 unsigned int objoid;
1166 objheader_t *objheader;
1167 short *endoffsets, *arryfields;
1168 prefetchpile_t *head = NULL;
1170 ptr = (char *) node;
1171 ntuples = *(GET_NTUPLES(ptr));
1172 oid = GET_PTR_OID(ptr);
1173 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1174 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1175 /* Find offset length for each tuple */
1176 int numoffset[ntuples];//Number of offsets for each tuple
1177 numoffset[0] = endoffsets[0];
1178 for(i = 1; i<ntuples; i++) {
1179 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1181 for(i = 0; i < ntuples; i++) {
1184 /* If object found locally */
1185 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1187 tmp = (char *) objheader;
1188 /* Find the oid of its offset value */
1192 index = endoffsets[i - 1];
1193 for(j = 0 ; j < numoffset[i] ; j++) {
1194 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1195 /*If oid found locally then
1196 *assign the latest oid found as the new oid
1197 *and copy left over offsets into the arrayoffsetfieldarray*/
1199 numoffset[i] = numoffset[i] - (j+1);
1200 for(k = 0; k < numoffset[i]; k++)
1201 arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1203 /*New offset oid not found */
1204 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1206 checkPreCache(node, numoffset, j, numoffset[i], objoid, index, i, oidnfound);
1212 /*If all offset oids are found locally,make the prefetch tuple invalid */
1219 /* Look in Prefetch cache */
1220 checkPreCache(node, numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound);
1224 /* Make machine groups */
1225 head = makePreGroups(node, numoffset);
1229 /* This function is called by the thread calling transPrefetch */
1230 void *transPrefetch(void *t) {
1231 prefetchqelem_t *qnode;
1232 prefetchpile_t *pilehead = NULL;
1235 /* lock mutex of primary prefetch queue */
1236 pthread_mutex_lock(&pqueue.qlock);
1237 /* while primary queue is empty, then wait */
1238 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1239 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1242 /* dequeue node to create a machine piles and finally unlock mutex */
1243 if((qnode = pre_dequeue()) == NULL) {
1244 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1247 pthread_mutex_unlock(&pqueue.qlock);
1248 /* Reduce redundant prefetch requests */
1249 checkPrefetchTuples(qnode);
1250 /* Check if the tuples are found locally, if yes then reduce them further*/
1251 /* and group requests by remote machine ids by calling the makePreGroups() */
1252 pilehead = foundLocal(qnode);
1254 /* Lock mutex of pool queue */
1255 pthread_mutex_lock(&mcqueue.qlock);
1256 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1257 mcpileenqueue(pilehead);
1258 /* Broadcast signal on machine pile queue */
1259 pthread_cond_broadcast(&mcqueue.qcond);
1260 /* Unlock mutex of machine pile queue */
1261 pthread_mutex_unlock(&mcqueue.qlock);
1262 /* Deallocate the prefetch queue pile node */
1268 /* Each thread in the pool of threads calls this function to establish connection with
1269 * remote machines, send the prefetch requests and process the reponses from
1270 * the remote machines .
1271 * The thread is active throughout the period of runtime */
1273 void *mcqProcess(void *threadid) {
1275 prefetchpile_t *mcpilenode;
1277 tid = (int) threadid;
1279 /* Lock mutex of mc pile queue */
1280 pthread_mutex_lock(&mcqueue.qlock);
1281 /* When mc pile queue is empty, wait */
1282 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1283 pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1285 /* Dequeue node to send remote machine connections*/
1286 if((mcpilenode = mcpiledequeue()) == NULL) {
1287 printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1291 pthread_mutex_unlock(&mcqueue.qlock);
1293 /*Initiate connection to remote host and send request */
1294 /* Process Request */
1295 sendPrefetchReq(mcpilenode, tid);
1297 /* Deallocate the machine queue pile node */
1298 mcdealloc(mcpilenode);
1302 void sendPrefetchReq(prefetchpile_t *mcpilenode, int threadid) {
1303 int sd, i, offset, off, len, endpair, numoffsets, count = 0;
1304 struct sockaddr_in serv_addr;
1305 struct hostent *server;
1306 char machineip[16], control;
1310 /* Send Trans Prefetch Request */
1311 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1312 perror("Error in socket for TRANS_REQUEST\n");
1315 bzero((char*) &serv_addr, sizeof(serv_addr));
1316 serv_addr.sin_family = AF_INET;
1317 serv_addr.sin_port = htons(LISTEN_PORT);
1318 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
1319 midtoIP(mcpilenode->mid ,machineip);
1320 machineip[15] = '\0';
1321 serv_addr.sin_addr.s_addr = inet_addr(machineip);
1323 /* Open Connection */
1324 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1325 perror("Error in connect for TRANS_REQUEST\n");
1329 /* Send TRANS_PREFETCH control message */
1330 control = TRANS_PREFETCH;
1331 if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1332 perror("Error in sending prefetch control\n");
1336 /* Send Oids and offsets in pairs */
1337 tmp = mcpilenode->objpiles;
1338 while(tmp != NULL) {
1340 count++; /* Keeps track of the number of oid and offset tuples sent per remote machine */
1341 len = sizeof(int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1342 char oidnoffset[len];
1343 memcpy(oidnoffset, &len, sizeof(int));
1345 memcpy(oidnoffset + off, &tmp->oid, sizeof(unsigned int));
1346 off += sizeof(unsigned int);
1347 for(i = 0; i < numoffsets; i++) {
1348 offset = off + (i * sizeof(short));
1349 memcpy(oidnoffset + offset, tmp->offset, sizeof(short));
1351 if (send(sd, &oidnoffset, sizeof(oidnoffset),MSG_NOSIGNAL) < sizeof(oidnoffset)) {
1352 perror("Error sending fixed bytes for thread\n");
1358 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1360 if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1361 perror("Error sending fixed bytes for thread\n");
1365 /* Get Response from the remote machine */
1366 getPrefetchResponse(count,sd);
1371 void getPrefetchResponse(int count, int sd) {
1372 int i = 0, val, n, N, sum, index, objsize;
1373 unsigned int bufsize,oid;
1374 char buffer[RECEIVE_BUFFER_SIZE], control;
1376 void *modptr, *oldptr;
1378 /* Read prefetch response from the Remote machine */
1379 if((val = read(sd, &control, sizeof(char))) <= 0) {
1380 perror("No control response for Prefetch request sent\n");
1384 if(control == TRANS_PREFETCH_RESPONSE) {
1385 /*For each oid and offset tuple sent as prefetch request to remote machine*/
1387 /* Clear contents of buffer */
1388 memset(buffer, 0, RECEIVE_BUFFER_SIZE);
1391 /* Read the size of buffer to be received */
1392 if((N = read(sd, buffer, sizeof(unsigned int))) <= 0) {
1393 perror("Size of buffer not recv\n");
1396 memcpy(&bufsize, buffer, sizeof(unsigned int));
1397 ptr = buffer + sizeof(unsigned int);
1398 /* Keep receiving the buffer containing oid info */
1400 n = recv((int)sd, (void *)ptr+sum, bufsize-sum, 0);
1402 } while(sum < bufsize && n != 0);
1403 /* Decode the contents of the buffer */
1404 index = sizeof(unsigned int);
1405 while(index < (bufsize - sizeof(unsigned int))) {
1406 if(buffer[index] == OBJECT_FOUND) {
1407 /* Increment it to get the object */
1408 index += sizeof(char);
1409 memcpy(&oid, buffer + index, sizeof(unsigned int));
1410 index += sizeof(unsigned int);
1411 /* Lock the Prefetch Cache look up table*/
1412 pthread_mutex_lock(&pflookup.lock);
1413 /* For each object found add to Prefetch Cache */
1414 memcpy(&objsize, buffer + index, sizeof(int));
1415 if ((modptr = objstrAlloc(prefetchcache, objsize)) == NULL) {
1416 printf("objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1419 memcpy(modptr, buffer+index, objsize);
1420 index += sizeof(int);
1421 /* Insert the oid and its address into the prefetch hash lookup table */
1422 /* Do a version comparison if the oid exists */
1423 if((oldptr = prehashSearch(oid)) != NULL) {
1424 /* If older version then update with new object ptr */
1425 if(((objheader_t *)oldptr)->version < ((objheader_t *)modptr)->version) {
1427 prehashInsert(oid, modptr);
1428 } else if(((objheader_t *)oldptr)->version == ((objheader_t *)modptr)->version) {
1429 /* Add the new object ptr to hash table */
1430 prehashInsert(oid, modptr);
1431 } else { /* Do nothing */
1434 } else {/*If doesn't no match found in hashtable, add the object ptr to hash table*/
1435 prehashInsert(oid, modptr);
1437 /* Broadcast signal on prefetch cache condition variable */
1438 pthread_cond_broadcast(&pflookup.cond);
1439 /* Unlock the Prefetch Cache look up table*/
1440 pthread_mutex_unlock(&pflookup.lock);
1441 } else if(buffer[index] == OBJECT_NOT_FOUND) {
1442 /* Increment it to get the object */
1443 /* TODO: For each object not found query DHT for new location and retrieve the object */
1444 index += sizeof(char);
1445 memcpy(&oid, buffer + index, sizeof(unsigned int));
1446 index += sizeof(unsigned int);
1447 /* Throw an error */
1448 printf("OBJECT NOT FOUND.... THIS SHOULD NOT HAPPEN...TERMINATE PROGRAM\n");
1451 printf("Error in decoding the index value %s, %d\n",__FILE__, __LINE__);
1457 printf("Error in receving response for prefetch request %s, %d\n",__FILE__, __LINE__);
1461 unsigned short getObjType(unsigned int oid)
1463 objheader_t *objheader;
1464 unsigned short numoffsets = 0;
1466 if ((objheader = (objheader_t *) mhashSearch(oid)) == NULL)
1468 if ((objheader = (objheader_t *) prehashSearch(oid)) == NULL)
1470 prefetch(1, &oid, &numoffsets, NULL);
1471 pthread_mutex_lock(&pflookup.lock);
1472 while ((objheader = (objheader_t *) prehashSearch(oid)) == NULL)
1473 pthread_cond_wait(&pflookup.cond, &pflookup.lock);
1474 pthread_mutex_unlock(&pflookup.lock);
1478 return TYPE(objheader);
1481 int startRemoteThread(unsigned int oid, unsigned int mid)
1484 struct sockaddr_in remoteAddr;
1485 char msg[1 + sizeof(unsigned int)];
1489 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1491 perror("startRemoteThread():socket()");
1495 bzero(&remoteAddr, sizeof(remoteAddr));
1496 remoteAddr.sin_family = AF_INET;
1497 remoteAddr.sin_port = htons(LISTEN_PORT);
1498 remoteAddr.sin_addr.s_addr = htonl(mid);
1500 if (connect(sock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
1502 printf("startRemoteThread():error %d connecting to %s:%d\n", errno,
1503 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1508 msg[0] = START_REMOTE_THREAD;
1509 memcpy(&msg[1], &oid, sizeof(unsigned int));
1511 bytesSent = send(sock, msg, 1 + sizeof(unsigned int), 0);
1514 perror("startRemoteThread():send()");
1517 else if (bytesSent != 1 + sizeof(unsigned int))
1519 printf("startRemoteThread(): error, sent %d bytes\n", bytesSent);