4 #include "machinepile.h"
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <sys/types.h>
21 #define LISTEN_PORT 2156
22 #define RECEIVE_BUFFER_SIZE 2048
23 #define NUM_THREADS 10
24 #define PREFETCH_CACHE_SIZE 1048576 //1MB
26 /* Global Variables */
27 extern int classsize[];
28 extern primarypfq_t pqueue; // shared prefetch queue
29 extern mcpileq_t mcqueue; //Shared queue containing prefetch requests sorted by remote machineids
30 objstr_t *prefetchcache; //Global Prefetch cache
31 extern prehashtable_t pflookup; //Global Prefetch cache's lookup table
32 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
34 extern objstr_t *mainobjstore;
36 plistnode_t *createPiles(transrecord_t *);
37 inline int arrayLength(int *array) {
39 for(i=0 ;array[i] != -1; i++)
43 inline int findmax(int *array, int arraylength) {
46 for(i = 0; i < arraylength; i++){
53 /* This function is a prefetch call generated by the compiler that
54 * populates the shared primary prefetch queue*/
55 void prefetch(int ntuples, unsigned int *oids, short *endoffsets, short *arrayfields) {
59 /* Allocate for the queue node*/
61 qnodesize = sizeof(prefetchqelem_t) + sizeof(int) + ntuples * (sizeof(short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
62 if((node = calloc(1,qnodesize)) == NULL) {
63 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
66 /* Set queue node values */
67 len = sizeof(prefetchqelem_t);
68 memcpy(node + len, &ntuples, sizeof(int));
70 memcpy(node + len, oids, ntuples*sizeof(unsigned int));
71 len += ntuples * sizeof(unsigned int);
72 memcpy(node + len, endoffsets, ntuples*sizeof(short));
73 len += ntuples * sizeof(short);
74 memcpy(node + len, arrayfields, endoffsets[ntuples-1]*sizeof(short));
75 /* Lock and insert into primary prefetch queue */
76 pthread_mutex_lock(&pqueue.qlock);
77 enqueue((prefetchqelem_t *)node);
78 pthread_cond_signal(&pqueue.qcond);
79 pthread_mutex_unlock(&pqueue.qlock);
82 /* This function initiates the prefetch thread
83 * A queue is shared between the main thread of execution
84 * and the prefetch thread to process the prefetch call
85 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
86 * processes the prefetch requests */
89 //Create and initialize prefetch cache structure
90 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
91 //Create prefetch cache lookup table
92 if(prehashCreate(HASH_SIZE, LOADFACTOR))
94 //Initialize primary shared queue
96 //Initialize machine pile w/prefetch oids and offsets shared queue
98 //Create the primary prefetch thread
99 pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
100 //Create and Initialize a pool of threads
101 for(t = 0; t< NUM_THREADS; t++) {
102 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
104 printf("Thread create error %s, %d\n", __FILE__, __LINE__);
110 /* This function stops the threads spawned */
113 pthread_cancel(tPrefetch);
114 for(t = 0; t < NUM_THREADS; t++)
115 pthread_cancel(wthreads[t]);
120 /* This functions inserts randowm wait delays in the order of msec
121 * Mostly used when transaction commits retry*/
122 void randomdelay(void)
124 struct timespec req, rem;
129 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
130 nanosleep(&req, &rem);
134 /* This function initializes things required in the transaction start*/
135 transrecord_t *transStart()
137 transrecord_t *tmp = malloc(sizeof(transrecord_t));
138 tmp->cache = objstrCreate(1048576);
139 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
144 /* This function finds the location of the objects involved in a transaction
145 * and returns the pointer to the object if found in a remote location */
146 objheader_t *transRead(transrecord_t *record, unsigned int oid)
148 unsigned int machinenumber;
149 objheader_t *tmp, *objheader;
153 /* Search local cache */
154 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
156 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
157 /* Look up in machine lookup table and copy into cache*/
158 // tmp = mhashSearch(oid);
159 size = sizeof(objheader_t)+classsize[tmp->type];
160 objcopy = objstrAlloc(record->cache, size);
161 memcpy(objcopy, (void *)objheader, size);
162 /* Insert into cache's lookup table */
163 chashInsert(record->lookupTable, objheader->oid, objcopy);
165 } else { /* If not found in machine look up */
166 /* Get the object from the remote location */
167 machinenumber = lhashSearch(oid);
168 objcopy = getRemoteObj(record, machinenumber, oid);
169 if(objcopy == NULL) {
170 //If object is not found in Remote location
171 //printf("Object oid = %d not found in Machine %d\n", oid, machinenumber);
175 //printf("Object oid = %d found in Machine %d\n", oid, machinenumber);
181 /* This function creates objects in the transaction record */
182 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
184 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
185 tmp->oid = getNewOID();
188 tmp->rcount = 0; //? not sure how to handle this yet
191 chashInsert(record->lookupTable, tmp->oid, tmp);
195 /* This function creates machine piles based on all machines involved in a
196 * transaction commit request */
197 plistnode_t *createPiles(transrecord_t *record) {
199 unsigned int size;/* Represents number of bins in the chash table */
200 chashlistnode_t *curr, *ptr, *next;
201 plistnode_t *pile = NULL;
202 unsigned int machinenum;
203 void *localmachinenum;
204 objheader_t *headeraddr;
206 ptr = record->lookupTable->table;
207 size = record->lookupTable->size;
209 for(i = 0; i < size ; i++) {
211 /* Inner loop to traverse the linked list of the cache lookupTable */
212 while(curr != NULL) {
213 //if the first bin in hash table is empty
218 //Get machine location for object id
220 if ((machinenum = lhashSearch(curr->key)) == 0) {
221 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
225 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
226 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
229 //Make machine groups
230 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
231 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
235 /* Check if local or not */
236 if((localmachinenum = mhashSearch(curr->key)) != NULL) {
237 pile->local = 1; //True i.e. local
246 /* This function initiates the transaction commit process
247 * Spawns threads for each of the new connections with Participants
248 * and creates new piles by calling the createPiles(),
249 * Fills the piles with necesaary information and
250 * Sends a transrequest() to each pile*/
251 int transCommit(transrecord_t *record) {
252 unsigned int tot_bytes_mod, *listmid;
255 int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
256 char buffer[RECEIVE_BUFFER_SIZE],control;
257 char transid[TID_LEN];
258 trans_req_data_t *tosend;
259 trans_commit_data_t transinfo;
260 static int newtid = 0;
261 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
264 /* Look through all the objects in the transaction record and make piles
265 * for each machine involved in the transaction*/
266 pile = createPiles(record);
268 /* Create the packet to be sent in TRANS_REQUEST */
270 /* Count the number of participants */
271 pilecount = pCount(pile);
273 /* Create a list of machine ids(Participants) involved in transaction */
274 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
275 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
278 pListMid(pile, listmid);
281 /* Initialize thread variables,
282 * Spawn a thread for each Participant involved in a transaction */
283 pthread_t thread[pilecount];
285 pthread_cond_t tcond;
286 pthread_mutex_t tlock;
287 pthread_mutex_t tlshrd;
289 thread_data_array_t *thread_data_array;
290 thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
291 local_thread_data_array_t *ltdata;
292 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
293 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
297 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
299 /* Initialize and set thread detach attribute */
300 pthread_attr_init(&attr);
301 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
302 pthread_mutex_init(&tlock, NULL);
303 pthread_cond_init(&tcond, NULL);
305 /* Process each machine pile */
306 while(pile != NULL) {
307 //Create transaction id
309 //trans_req_data_t *tosend;
310 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
311 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
314 tosend->f.control = TRANS_REQUEST;
315 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
316 tosend->f.mcount = pilecount;
317 tosend->f.numread = pile->numread;
318 tosend->f.nummod = pile->nummod;
319 tosend->f.sum_bytes = pile->sum_bytes;
320 tosend->listmid = listmid;
321 tosend->objread = pile->objread;
322 tosend->oidmod = pile->oidmod;
323 thread_data_array[threadnum].thread_id = threadnum;
324 thread_data_array[threadnum].mid = pile->mid;
325 thread_data_array[threadnum].pilecount = pilecount;
326 thread_data_array[threadnum].buffer = tosend;
327 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
328 thread_data_array[threadnum].threshold = &tcond;
329 thread_data_array[threadnum].lock = &tlock;
330 thread_data_array[threadnum].count = &trecvcount;
331 thread_data_array[threadnum].replyctrl = &treplyctrl;
332 thread_data_array[threadnum].replyretry = &treplyretry;
333 thread_data_array[threadnum].rec = record;
334 /* If local do not create any extra connection */
335 if(pile->local != 1) { /* Not local */
336 rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);
338 perror("Error in pthread create\n");
342 /*Unset the pile->local flag*/
344 /*Set flag to identify that Local machine is involved*/
345 ltdata->tdata = &thread_data_array[threadnum];
346 ltdata->transinfo = &transinfo;
347 val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
349 perror("Error in pthread create\n");
357 /* Free attribute and wait for the other threads */
358 pthread_attr_destroy(&attr);
359 for (i = 0 ;i < pilecount ; i++) {
360 rc = pthread_join(thread[i], NULL);
363 printf("ERROR return code from pthread_join() is %d\n", rc);
369 pthread_cond_destroy(&tcond);
370 pthread_mutex_destroy(&tlock);
374 free(thread_data_array);
377 /* Retry trans commit procedure if not sucessful in the first try */
378 if(treplyretry == 1) {
379 /* wait a random amount of time */
382 /* Retry the commiting transaction again */
389 /* This function sends information involved in the transaction request and
390 * accepts a response from particpants.
391 * It calls decideresponse() to decide on what control message
392 * to send next and sends the message using sendResponse()*/
393 void *transRequest(void *threadarg) {
395 struct sockaddr_in serv_addr;
396 struct hostent *server;
397 thread_data_array_t *tdata;
398 objheader_t *headeraddr;
399 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
400 char machineip[16], retval;
402 tdata = (thread_data_array_t *) threadarg;
404 /* Send Trans Request */
405 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
406 perror("Error in socket for TRANS_REQUEST\n");
409 bzero((char*) &serv_addr, sizeof(serv_addr));
410 serv_addr.sin_family = AF_INET;
411 serv_addr.sin_port = htons(LISTEN_PORT);
412 midtoIP(tdata->mid,machineip);
413 machineip[15] = '\0';
414 serv_addr.sin_addr.s_addr = inet_addr(machineip);
415 /* Open Connection */
416 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
417 perror("Error in connect for TRANS_REQUEST\n");
421 printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
422 /* Send bytes of data with TRANS_REQUEST control message */
423 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
424 perror("Error sending fixed bytes for thread\n");
427 /* Send list of machines involved in the transaction */
429 int size=sizeof(unsigned int)*tdata->pilecount;
430 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
431 perror("Error sending list of machines for thread\n");
435 /* Send oids and version number tuples for objects that are read */
437 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
438 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
439 perror("Error sending tuples for thread\n");
443 /* Send objects that are modified */
444 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
446 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
447 size=sizeof(objheader_t)+classsize[headeraddr->type];
448 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
449 perror("Error sending obj modified for thread\n");
454 /* Read control message from Participant */
455 if((n = read(sd, &control, sizeof(char))) <= 0) {
456 perror("Error in reading control message from Participant\n");
459 recvcontrol = control;
461 /* Update common data structure and increment count */
462 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
464 /* Lock and update count */
465 //Thread sleeps until all messages from pariticipants are received by coordinator
466 pthread_mutex_lock(tdata->lock);
468 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
470 /* Wake up the threads and invoke decideResponse (once) */
471 if(*(tdata->count) == tdata->pilecount) {
472 if (decideResponse(tdata) != 0) {
473 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
474 pthread_mutex_unlock(tdata->lock);
478 pthread_cond_broadcast(tdata->threshold);
480 pthread_cond_wait(tdata->threshold, tdata->lock);
482 pthread_mutex_unlock(tdata->lock);
484 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
485 * to all participants in their respective socket */
486 if (sendResponse(tdata, sd) == 0) {
487 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
488 pthread_mutex_unlock(tdata->lock);
493 /* Close connection */
498 /* This function decides the reponse that needs to be sent to
499 * all Participant machines involved in the transaction commit */
500 int decideResponse(thread_data_array_t *tdata) {
502 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
505 //Check common data structure
506 for (i = 0 ; i < tdata->pilecount ; i++) {
507 /*Switch on response from Participant */
508 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
509 written onto the shared array */
512 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
517 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
521 case TRANS_SOFT_ABORT:
522 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
526 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
531 /* Decide what control message to send to Participant */
532 if(transdisagree > 0) {
534 *(tdata->replyctrl) = TRANS_ABORT;
535 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
536 objstrDelete(tdata->rec->cache);
537 chashDelete(tdata->rec->lookupTable);
539 } else if(transagree == tdata->pilecount){
541 *(tdata->replyctrl) = TRANS_COMMIT;
542 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
543 objstrDelete(tdata->rec->cache);
544 chashDelete(tdata->rec->lookupTable);
546 } else if(transsoftabort > 0 && transdisagree == 0) {
547 /* Send Abort in soft abort case followed by retry commiting transaction again*/
548 *(tdata->replyctrl) = TRANS_ABORT;
549 *(tdata->replyretry) = 1;
550 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
552 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
558 /* This function sends the final response to all threads in their respective socket id */
559 char sendResponse(thread_data_array_t *tdata, int sd) {
560 int n, N, sum, oidcount = 0;
561 char *ptr, retval = 0;
562 unsigned int *oidnotfound;
564 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
565 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
566 /* Read list of objects missing */
567 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
568 N = oidcount * sizeof(unsigned int);
569 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
570 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
572 ptr = (char *) oidnotfound;
574 n = read(sd, ptr+sum, N-sum);
576 } while(sum < N && n !=0);
578 retval = TRANS_SOFT_ABORT;
580 /* If the decided response is TRANS_ABORT */
581 if(*(tdata->replyctrl) == TRANS_ABORT) {
582 retval = TRANS_ABORT;
583 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
584 retval = TRANS_COMMIT;
586 /* Send response to the Participant */
587 if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
588 perror("Error sending ctrl message for participant\n");
594 /* This function opens a connection, places an object read request to the
595 * remote machine, reads the control message and object if available and
596 * copies the object and its header to the local cache.
597 * TODO replace mnum and midtoIP() with MACHINE_IP address later */
599 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
601 struct sockaddr_in serv_addr;
602 struct hostent *server;
608 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
609 perror("Error in socket\n");
612 bzero((char*) &serv_addr, sizeof(serv_addr));
613 serv_addr.sin_family = AF_INET;
614 serv_addr.sin_port = htons(LISTEN_PORT);
615 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
616 midtoIP(mnum,machineip);
617 machineip[15] = '\0';
618 serv_addr.sin_addr.s_addr = inet_addr(machineip);
619 /* Open connection */
620 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
621 perror("Error in connect\n");
624 char readrequest[sizeof(char)+sizeof(unsigned int)];
625 readrequest[0] = READ_REQUEST;
626 *((unsigned int *)(&readrequest[1])) = oid;
627 if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
628 perror("Error sending message\n");
633 printf("DEBUG -> ready to rcv ...\n");
635 /* Read response from the Participant */
636 if((val = read(sd, &control, sizeof(char))) <= 0) {
637 perror("No control response for getRemoteObj sent\n");
641 case OBJECT_NOT_FOUND:
642 printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
645 /* Read object if found into local cache */
646 if((val = read(sd, &size, sizeof(int))) <= 0) {
647 perror("No size is read from the participant\n");
650 objcopy = objstrAlloc(record->cache, size);
651 if((val = read(sd, objcopy, size)) <= 0) {
652 perror("No objects are read from the remote participant\n");
655 /* Insert into cache's lookup table */
656 chashInsert(record->lookupTable, oid, objcopy);
659 printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
662 /* Close connection */
667 /*This function handles the local trans requests involved in a transaction commiting process
668 * makes a decision if the local machine sends AGREE or DISAGREE or SOFT_ABORT
669 * Activates the other nonlocal threads that are waiting for the decision and the
670 * based on common decision by all groups involved in the transaction it
671 * either commits or aborts the transaction.
672 * It also frees the calloced memory resources
675 void *handleLocalReq(void *threadarg) {
678 char control = 0, *ptr;
680 unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
682 objheader_t *headptr;
683 local_thread_data_array_t *localtdata;
685 localtdata = (local_thread_data_array_t *) threadarg;
687 /* Counters and arrays to formulate decision on control message to be sent */
688 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
689 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
690 oidmod = (unsigned int *) calloc(localtdata->tdata->buffer->f.nummod, sizeof(unsigned int));
691 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
692 int objmodnotfound = 0, nummodfound = 0;
694 /* modptr points to the beginning of the object store
695 * created at the Pariticipant */
696 if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
697 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
703 /* Process each oid in the machine pile/ group per thread */
704 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
705 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
706 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
708 oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
709 incr += sizeof(unsigned int);
710 version = *((short *)(localtdata->tdata->buffer->objread + incr));
711 } else {//Objs modified
712 headptr = (objheader_t *) ptr;
714 oidmod[objmod] = oid;//Array containing modified oids
716 version = headptr->version;
717 ptr += sizeof(objheader_t) + classsize[headptr->type];
720 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
722 /* Save the oids not found and number of oids not found for later use */
723 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
724 /* Save the oids not found and number of oids not found for later use */
726 oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
728 } else { /* If Obj found in machine (i.e. has not moved) */
729 /* Check if Obj is locked by any previous transaction */
730 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
731 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
733 } else {/* If versions don't match ...HARD ABORT */
735 /* Send TRANS_DISAGREE to Coordinator */
736 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
737 printf("DEBUG -> Sending TRANS_DISAGREE\n");
738 //return tdata->recvmsg[tdata->thread_id].rcv_status;
740 } else {/* If Obj is not locked then lock object */
741 ((objheader_t *)mobj)->status |= LOCK;
742 //TODO Remove this for Testing
745 /* Save all object oids that are locked on this machine during this transaction request call */
746 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
748 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
750 } else { /* If versions don't match ...HARD ABORT */
752 /* Send TRANS_DISAGREE to Coordinator */
753 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
754 printf("DEBUG -> Sending TRANS_DISAGREE\n");
755 // return tdata->recvmsg[tdata->thread_id].rcv_status;
761 /*Decide the response to be sent to the Coordinator( the local machine in this case)*/
763 /* Condition to send TRANS_AGREE */
764 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
765 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
766 printf("DEBUG -> Sending TRANS_AGREE\n");
768 /* Condition to send TRANS_SOFT_ABORT */
769 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
770 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
771 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
772 /* Send number of oids not found and the missing oids if objects are missing in the machine */
773 /* TODO Remember to store the oidnotfound for later use
774 if(objnotfound != 0) {
775 int size = sizeof(unsigned int)* objnotfound;
780 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
781 * if Participant receives a TRANS_COMMIT */
782 localtdata->transinfo->objmod = oidmod;
783 localtdata->transinfo->objlocked = oidlocked;
784 localtdata->transinfo->objnotfound = oidnotfound;
785 localtdata->transinfo->modptr = modptr;
786 localtdata->transinfo->nummod = localtdata->tdata->buffer->f.nummod;
787 localtdata->transinfo->numlocked = objlocked;
788 localtdata->transinfo->numnotfound = objnotfound;
790 /*Set flag to show that common data structure for this individual thread has been written to */
791 //*(tdata->localstatus) |= LM_UPDATED;
793 /* Lock and update count */
794 //Thread sleeps until all messages from pariticipants are received by coordinator
795 pthread_mutex_lock(localtdata->tdata->lock);
796 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
798 /* Wake up the threads and invoke decideResponse (once) */
799 if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
800 if (decideResponse(localtdata->tdata) != 0) {
801 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
802 pthread_mutex_unlock(localtdata->tdata->lock);
805 pthread_cond_broadcast(localtdata->tdata->threshold);
807 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
809 pthread_mutex_unlock(localtdata->tdata->lock);
811 /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
812 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
813 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->transinfo->nummod, localtdata->tdata->buffer->f.numread) != 0) {
814 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
817 }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
818 if(transComProcess(localtdata->transinfo) != 0) {
819 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
825 printf("DEBUG -> Freeing...\n");
827 if (localtdata->transinfo->objmod != NULL) {
828 free(localtdata->transinfo->objmod);
829 localtdata->transinfo->objmod = NULL;
831 if (localtdata->transinfo->objlocked != NULL) {
832 free(localtdata->transinfo->objlocked);
833 localtdata->transinfo->objlocked = NULL;
835 if (localtdata->transinfo->objnotfound != NULL) {
836 free(localtdata->transinfo->objnotfound);
837 localtdata->transinfo->objnotfound = NULL;
842 /* This function completes the ABORT process if the transaction is aborting
844 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod, int numread) {
847 objheader_t *tmp_header;
850 printf("DEBUG -> Recv TRANS_ABORT\n");
851 /* Set all ref counts as 1 and do garbage collection */
853 for(i = 0; i< nummod; i++) {
854 tmp_header = (objheader_t *)ptr;
855 tmp_header->rcount = 1;
856 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
858 /* Unlock objects that was locked due to this transaction */
859 for(i = 0; i< numlocked; i++) {
860 header = mhashSearch(objlocked[i]);// find the header address
861 ((objheader_t *)header)->status &= ~(LOCK);
863 //TODO/* Unset the bit for local objects */
865 /* Send ack to Coordinator */
866 printf("DEBUG-> TRANS_SUCCESSFUL\n");
868 /*Free the pointer */
873 /*This function completes the COMMIT process is the transaction is commiting
875 int transComProcess(trans_commit_data_t *transinfo) {
877 int i = 0, offset = 0;
880 printf("DEBUG -> Recv TRANS_COMMIT\n");
881 /* Process each modified object saved in the mainobject store */
882 for(i=0; i<transinfo->nummod; i++) {
883 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
884 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
886 /* Change reference count of older address and free space in objstr ?? */
887 header->rcount = 1; //TODO Not sure what would be the val
889 /* Change ptr address in mhash table */
890 mhashRemove(transinfo->objmod[i]);
891 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
892 offset += sizeof(objheader_t) + classsize[header->type];
894 /* Update object version number */
895 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
896 header->version += 1;
899 /* Unlock locked objects */
900 for(i=0; i<transinfo->numlocked; i++) {
901 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
902 header->status &= ~(LOCK);
905 //TODO Update location lookup table
906 //TODO/* Unset the bit for local objects */
908 /* Send ack to Coordinator */
909 printf("DEBUG-> TRANS_SUCESSFUL\n");
913 /* This function checks if the prefetch oids are same and have same offsets
914 * for case x.a.b and y.a.b where x and y have same oid's
915 * or if a.b.c is a subset of x.b.c.d*/
916 /* check for case where the generated request a.y.z or x.y.z.g then
917 * prefetch needs to be generated for x.y.z.g if oid of a and x are same*/
918 void checkPrefetchTuples(prefetchqelem_t *node) {
919 int i,j, count,k, sindex, index;
921 int ntuples, slength;
923 short *endoffsets, *arryfields;
925 /* Check for the case x.y.z and a.b.c are same oids */
927 ntuples = *(GET_NTUPLES(ptr));
928 oid = GET_PTR_OID(ptr);
929 endoffsets = GET_PTR_EOFF(ptr, ntuples);
930 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
931 /* Find offset length for each tuple */
932 int numoffset[ntuples];
933 numoffset[0] = endoffsets[0];
934 for(i = 1; i<ntuples; i++) {
935 numoffset[i] = endoffsets[i] - endoffsets[i-1];
937 /* Check for redundant tuples by comparing oids of each tuple */
938 for(i = 0; i < ntuples; i++) {
941 for(j = i+1 ; j < ntuples; j++) {
944 /*If oids of tuples match */
945 if (oid[i] == oid[j]) {
946 /* Find the smallest offset length of two tuples*/
947 if(numoffset[i] > numoffset[j]){
948 slength = numoffset[j];
952 slength = numoffset[i];
956 /* Compare the offset values based on the current indices
957 * break if they do not match
958 * if all offset values match then pick the largest tuple*/
962 index = endoffsets[j -1];
963 for(count = 0; count < slength; count ++) {
964 if (arryfields[k] != arryfields[index]) {
972 index = endoffsets[j-1];
973 printf("Value of slength = %d\n", slength);
974 for(count = 0; count < slength; count++) {
975 if(arryfields[k] != arryfields[index]) {
983 if(slength == count) {
991 void checkPreCache(prefetchqelem_t *node, int *numoffset, int counter, int loopcount, unsigned int objoid, int index, int iter, int oidnfound) {
993 int ntuples, i, k, flag;
995 short *endoffsets, *arryfields;
999 ntuples = *(GET_NTUPLES(ptr));
1000 oid = GET_PTR_OID(ptr);
1001 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1002 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1004 if(oidnfound == 1) {
1005 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1007 } else { //Found in Prefetch Cache
1008 //TODO Decide if object is too old, if old remove from cache
1009 tmp = (char *) header;
1010 /* Check if any of the offset oid is available in the Prefetch cache */
1011 for(i = counter; i < loopcount; i++) {
1012 objoid = *(tmp + sizeof(objheader_t) + arryfields[counter]);
1013 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1022 for(i = counter; i<loopcount; i++) {
1023 if((header = (objheader_t *)prehashSearch(objoid)) != NULL) {
1024 tmp = (char *) header;
1025 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1035 /* If oid not found locally or in prefetch cache then
1036 * assign the latest oid found as the new oid
1037 * and copy left over offsets into the arrayoffsetfieldarray*/
1039 numoffset[iter] = numoffset[iter] - (i+1);
1040 for(k = 0; k < numoffset[iter] ; k++) {
1041 arryfields[endoffsets[counter]+k] = arryfields[endoffsets[counter]+k+1];
1046 numoffset[iter] = 0;
1050 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1051 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1053 int ntuples, slength, i, machinenum;
1056 short *endoffsets, *arryfields, *offset;
1057 prefetchpile_t *head = NULL;
1059 /* Check for the case x.y.z and a.b.c are same oids */
1060 ptr = (char *) node;
1061 ntuples = *(GET_NTUPLES(ptr));
1062 oid = GET_PTR_OID(ptr);
1063 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1064 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1066 /* Check for redundant tuples by comparing oids of each tuple */
1067 for(i = 0; i < ntuples; i++) {
1070 /* For each tuple make piles */
1071 if ((machinenum = lhashSearch(oid[i])) == 0) {
1072 printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1075 /* Insert into machine pile */
1076 offset = &arryfields[endoffsets[i-1]];
1077 insertPile(machinenum, oid[i], numoffset[i], offset, head);
1084 /* This function checks if the oids within the prefetch tuples are available locally.
1085 * If yes then makes the tuple invalid. If no then rearranges oid and offset values in
1086 * the prefetchqelem_t node to represent a new prefetch tuple */
1087 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1088 int ntuples,i, j, k, oidnfound = 0, index, flag;
1090 unsigned int objoid;
1092 objheader_t *objheader;
1093 short *endoffsets, *arryfields;
1094 prefetchpile_t *head = NULL;
1096 ptr = (char *) node;
1097 ntuples = *(GET_NTUPLES(ptr));
1098 oid = GET_PTR_OID(ptr);
1099 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1100 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1101 /* Find offset length for each tuple */
1102 int numoffset[ntuples];//Number of offsets for each tuple
1103 numoffset[0] = endoffsets[0];
1104 for(i = 1; i<ntuples; i++) {
1105 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1107 for(i = 0; i < ntuples; i++) {
1110 /* If object found locally */
1111 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1113 tmp = (char *) objheader;
1114 /* Find the oid of its offset value */
1118 index = endoffsets[i - 1];
1119 for(j = 0 ; j < numoffset[i] ; j++) {
1120 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1121 /*If oid found locally then
1122 *assign the latest oid found as the new oid
1123 *and copy left over offsets into the arrayoffsetfieldarray*/
1125 numoffset[i] = numoffset[i] - (j+1);
1126 for(k = 0; k < numoffset[i]; k++)
1127 arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1129 /*New offset oid not found */
1130 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1132 checkPreCache(node, numoffset, j, numoffset[i], objoid, index, i, oidnfound);
1138 /*If all offset oids are found locally,make the prefetch tuple invalid */
1145 /* Look in Prefetch cache */
1146 checkPreCache(node, numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound);
1150 /* Make machine groups */
1151 head = makePreGroups(node, numoffset);
1155 /* This function is called by the thread calling transPrefetch */
1156 void *transPrefetch(void *t) {
1157 //int *offstarray = NULL;
1158 prefetchqelem_t *qnode;
1159 prefetchpile_t *pilehead = NULL;
1162 /* lock mutex of primary prefetch queue */
1163 pthread_mutex_lock(&pqueue.qlock);
1164 /* while primary queue is empty, then wait */
1165 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1166 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1169 /* dequeue node to create a machine piles and finally unlock mutex */
1170 if((qnode = dequeue()) == NULL) {
1171 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1174 pthread_mutex_unlock(&pqueue.qlock);
1175 /* Reduce redundant prefetch requests */
1176 checkPrefetchTuples(qnode);
1177 /* Check if the tuples are found locally, if yes then reduce them further*/
1178 /* and group requests by remote machine ids by calling the makePreGroups() */
1179 pilehead = foundLocal(qnode);
1181 /* Lock mutex of pool queue */
1182 pthread_mutex_lock(&mcqueue.qlock);
1183 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1184 mcpileenqueue(pilehead);
1185 /* Broadcast signal on machine pile queue */
1186 pthread_cond_broadcast(&mcqueue.qcond);
1187 /* Unlock mutex of mcahine pile queue */
1188 pthread_mutex_unlock(&mcqueue.qlock);
1192 /*The pool of threads work on this function to establish connection with
1193 * remote machines */
1195 void *mcqProcess(void *threadid) {
1197 prefetchpile_t *mcpilenode;
1199 tid = (int) threadid;
1201 /* Lock mutex of mc pile queue */
1202 pthread_mutex_lock(&mcqueue.qlock);
1203 /* while mc pile queue is empty, then wait */
1204 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1205 pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1207 /* dequeue node to send remote machine connections*/
1208 if((mcpilenode = mcpiledequeue()) == NULL) {
1209 printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1213 pthread_mutex_unlock(&mcqueue.qlock);
1215 /*Initiate connection to remote host and send request */
1216 /* Process Request */
1217 sendPrefetchReq(mcpilenode, tid);
1218 /* TODO: For each object not found query DHT for new location and retrieve the object */
1220 /* Deallocate the dequeued node */
1224 /*This function is called by the thread that processes the
1225 * prefetch request makes piles to prefetch records and prefetches the oids from remote machines */
1226 int transPrefetchProcess(transrecord_t *record, int *arrayofoffset[], short numoids){
1228 int arraylength[numoids];
1229 unsigned int machinenumber;
1230 objheader_t *tmp, *objheader;
1233 pthread_attr_t attr;
1235 /* Given tuple find length of tuple*/
1236 for(i = 0; i < numoids ; i++) {
1237 arraylength[i] = arrayLength(arrayofoffset[i]);
1240 /* Initialize and set thread attributes
1241 * Spawn a thread for each prefetch request sent*/
1242 pthread_t thread[numoids];
1243 pthread_attr_init(&attr);
1244 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1246 /* Create Machine Piles to send prefetch requests use threads*/
1247 for( i = 0 ; i< numoids ; i++) {
1248 if(arrayofoffset[i][0] == -1)
1251 /* For each Pile in the machine send TRANS_PREFETCH */
1252 //makePiles(arrayofoffset, numoids);
1253 /* Fill thread data structure */
1254 //rc = pthread_create(&thread[i] , &attr, sendPrefetchReq, (void *) arrayofoffset[i]);
1256 perror("Error in pthread create at transPrefetchProcess()\n");
1263 /* Free attribute and wait to join other threads */
1264 for (i = 0 ;i < numoids ; i++) {
1265 rc = pthread_join(thread[i], NULL);
1267 perror("Error pthread_join() in transPrefetchProcess()\n");
1271 pthread_attr_destroy(&attr);
1277 void sendPrefetchReq(prefetchpile_t *mcpilenode, int threadid) {
1278 int sd, i, offset, off, len, endpair, numoffsets, count = 0;
1279 struct sockaddr_in serv_addr;
1280 struct hostent *server;
1281 char machineip[16], control;
1285 /* Send Trans Prefetch Request */
1286 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1287 perror("Error in socket for TRANS_REQUEST\n");
1290 bzero((char*) &serv_addr, sizeof(serv_addr));
1291 serv_addr.sin_family = AF_INET;
1292 serv_addr.sin_port = htons(LISTEN_PORT);
1293 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
1294 midtoIP(mcpilenode->mid ,machineip);
1295 machineip[15] = '\0';
1296 serv_addr.sin_addr.s_addr = inet_addr(machineip);
1298 /* Open Connection */
1299 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1300 perror("Error in connect for TRANS_REQUEST\n");
1304 /* Send TRANS_PREFETCH control message */
1305 control = TRANS_PREFETCH;
1306 if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1307 perror("Error in sending prefetch control\n");
1311 /* Send Oids and offsets in pairs */
1312 tmp = mcpilenode->objpiles;
1313 while(tmp != NULL) {
1315 count++; // Keeps track of the number of oid and offset tuples sent per remote machine
1316 len = sizeof(int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1317 char oidnoffset[len];
1318 memcpy(oidnoffset, &len, sizeof(int));
1320 memcpy(oidnoffset + off, &tmp->oid, sizeof(unsigned int));
1321 off += sizeof(unsigned int);
1322 for(i = 0; i < numoffsets; i++) {
1323 offset = off + (i * sizeof(short));
1324 memcpy(oidnoffset + offset, tmp->offset, sizeof(short));
1326 if (send(sd, &oidnoffset, sizeof(oidnoffset),MSG_NOSIGNAL) < sizeof(oidnoffset)) {
1327 perror("Error sending fixed bytes for thread\n");
1333 /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1335 if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1336 perror("Error sending fixed bytes for thread\n");
1340 /* Get Response from the remote machine */
1341 getPrefetchResponse(count,sd);
1345 void getPrefetchResponse(int count, int sd) {
1346 int i = 0, val, n, N, sum, index, objsize;
1347 unsigned int bufsize,oid;
1348 char buffer[RECEIVE_BUFFER_SIZE], control;
1352 /* Read prefetch response from the Remote machine */
1353 if((val = read(sd, &control, sizeof(char))) <= 0) {
1354 perror("No control response for Prefetch request sent\n");
1357 if(control == TRANS_PREFETCH_RESPONSE) {
1358 /*For each oid and offset tuple sent as prefetch request to remote machine*/
1360 /* Clear contents of buffer */
1361 memset(buffer, 0, RECEIVE_BUFFER_SIZE);
1364 /* Read the size of buffer to be received */
1365 if((N = read(sd, buffer, sizeof(unsigned int))) <= 0) {
1366 perror("Size of buffer not recv\n");
1369 memcpy(&bufsize, buffer, sizeof(unsigned int));
1370 ptr = buffer + sizeof(unsigned int);
1371 /* Keep receiving the buffer containing oid info */
1373 n = recv((int)sd, (void *)ptr+sum, bufsize-sum, 0);
1375 } while(sum < bufsize && n != 0);
1376 /* Decode the contents of the buffer */
1377 index = sizeof(unsigned int);
1378 while(index < (bufsize - sizeof(unsigned int))) {
1379 if(buffer[index] == OBJECT_FOUND) {
1380 /* Increment it to get the object */
1381 index += sizeof(char);
1382 memcpy(&oid, buffer + index, sizeof(unsigned int));
1383 index += sizeof(unsigned int);
1384 /* Lock the Prefetch Cache look up table*/
1385 pthread_mutex_lock(&pflookup.lock);
1386 /* For each object found add to Prefetch Cache */
1387 memcpy(&objsize, buffer + index, sizeof(int));
1388 if ((modptr = objstrAlloc(prefetchcache, objsize)) == NULL) {
1389 printf("objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1392 memcpy(modptr, buffer+index, objsize);
1393 index += sizeof(int);
1394 /* Add pointer and oid to hash table */
1395 //TODO Do we need a version comparison herei ??
1396 prehashInsert(oid, modptr);
1397 /* Broadcast signal on prefetch cache condition variable */
1398 pthread_cond_broadcast(&pflookup.cond);
1399 /* Unlock the Prefetch Cache look up table*/
1400 pthread_mutex_unlock(&pflookup.lock);
1401 } else if(buffer[index] == OBJECT_NOT_FOUND) {
1402 /* Increment it to get the object */
1403 // TODO If object not found, local machine takes inventory
1404 index += sizeof(char);
1405 memcpy(&oid, buffer + index, sizeof(unsigned int));
1406 index += sizeof(unsigned int);
1408 printf("Error in decoding the index value %s, %d\n",__FILE__, __LINE__);
1414 printf("Error in receving response for prefetch request %s, %d\n",__FILE__, __LINE__);