4 #include "machinepile.h"
10 #include <sys/types.h>
11 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <sys/types.h>
20 #define LISTEN_PORT 2156
21 #define RECEIVE_BUFFER_SIZE 2048
22 #define NUM_THREADS 10
23 #define PREFETCH_CACHE_SIZE 1048576 //1MB
25 #define GET_NTUPLES(x) ((int *)(x + sizeof(prefetchqelem_t)))
26 #define GET_PTR_OID(x) ((unsigned int *)(x + sizeof(prefetchqelem_t) + sizeof(int)))
27 #define GET_PTR_EOFF(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int))))
28 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
31 /* Global Variables */
32 extern int classsize[];
33 extern primarypfq_t pqueue; // shared prefetch queue
34 extern mcpileq_t mcqueue; //Shared queue containing prefetch requests sorted by remote machineids
35 pthread_t wthreads[NUM_THREADS]; //Worker threads for working on the prefetch queue
37 extern objstr_t *mainobjstore;
38 objstr_t *prefetchcache;
40 plistnode_t *createPiles(transrecord_t *);
41 inline int arrayLength(int *array) {
43 for(i=0 ;array[i] != -1; i++)
47 inline int findmax(int *array, int arraylength) {
50 for(i = 0; i < arraylength; i++){
57 /* This function is a prefetch call generated by the compiler that
58 * populates the shared primary prefetch queue*/
59 void prefetch(int ntuples, unsigned int *oids, short *endoffsets, short *arrayfields) {
63 /* Allocate for the queue node*/
65 qnodesize = sizeof(prefetchqelem_t) + sizeof(int) + ntuples * (sizeof(short) + sizeof(unsigned int)) + endoffsets[ntuples - 1] * sizeof(short);
66 if((node = calloc(1,qnodesize)) == NULL) {
67 printf("Calloc Error %s, %d\n", __FILE__, __LINE__);
70 /* Set queue node values */
71 len = sizeof(prefetchqelem_t);
72 memcpy(node + len, &ntuples, sizeof(int));
74 memcpy(node + len, oids, ntuples*sizeof(unsigned int));
75 len += ntuples * sizeof(unsigned int);
76 memcpy(node + len, endoffsets, ntuples*sizeof(short));
77 len += ntuples * sizeof(short);
78 memcpy(node + len, arrayfields, endoffsets[ntuples-1]*sizeof(short));
79 /* Lock and insert into primary prefetch queue */
80 pthread_mutex_lock(&pqueue.qlock);
81 enqueue((prefetchqelem_t *)node);
82 pthread_cond_signal(&pqueue.qcond);
83 pthread_mutex_unlock(&pqueue.qlock);
86 /* This function initiates the prefetch thread
87 * A queue is shared between the main thread of execution
88 * and the prefetch thread to process the prefetch call
89 * Call from compiler populates the shared queue with prefetch requests while prefetch thread
90 * processes the prefetch requests */
93 //Create and initialize prefetch cache structure
94 prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
95 //Create prefetch cache lookup table
96 if(prehashCreate(HASH_SIZE, LOADFACTOR))
98 //Initialize primary shared queue
100 //Initialize machine pile w/prefetch oids and offsets shared queue
102 //Create the primary prefetch thread
103 pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
104 //Create and Initialize a pool of threads
105 for(t = 0; t< NUM_THREADS; t++) {
106 //rc = pthread_create(&wthreads[t], NULL, pfqProcess, (void *)t);
108 printf("Thread create error %s, %d\n", __FILE__, __LINE__);
114 /* This function stops the threads spawned */
117 pthread_cancel(tPrefetch);
118 for(t = 0; t < NUM_THREADS; t++)
119 pthread_cancel(wthreads[t]);
124 /* This functions inserts randowm wait delays in the order of msec
125 * Mostly used when transaction commits retry*/
126 void randomdelay(void)
128 struct timespec req, rem;
133 req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
134 nanosleep(&req, &rem);
138 /* This function initializes things required in the transaction start*/
139 transrecord_t *transStart()
141 transrecord_t *tmp = malloc(sizeof(transrecord_t));
142 tmp->cache = objstrCreate(1048576);
143 tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
148 /* This function finds the location of the objects involved in a transaction
149 * and returns the pointer to the object if found in a remote location */
150 objheader_t *transRead(transrecord_t *record, unsigned int oid)
152 unsigned int machinenumber;
153 objheader_t *tmp, *objheader;
157 /* Search local cache */
158 if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
160 } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
161 /* Look up in machine lookup table and copy into cache*/
162 // tmp = mhashSearch(oid);
163 size = sizeof(objheader_t)+classsize[tmp->type];
164 objcopy = objstrAlloc(record->cache, size);
165 memcpy(objcopy, (void *)objheader, size);
166 /* Insert into cache's lookup table */
167 chashInsert(record->lookupTable, objheader->oid, objcopy);
169 } else { /* If not found in machine look up */
170 /* Get the object from the remote location */
171 machinenumber = lhashSearch(oid);
172 objcopy = getRemoteObj(record, machinenumber, oid);
173 if(objcopy == NULL) {
174 //If object is not found in Remote location
175 //printf("Object oid = %d not found in Machine %d\n", oid, machinenumber);
179 //printf("Object oid = %d found in Machine %d\n", oid, machinenumber);
185 /* This function creates objects in the transaction record */
186 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
188 objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
189 tmp->oid = getNewOID();
192 tmp->rcount = 0; //? not sure how to handle this yet
195 chashInsert(record->lookupTable, tmp->oid, tmp);
199 /* This function creates machine piles based on all machines involved in a
200 * transaction commit request */
201 plistnode_t *createPiles(transrecord_t *record) {
203 unsigned int size;/* Represents number of bins in the chash table */
204 chashlistnode_t *curr, *ptr, *next;
205 plistnode_t *pile = NULL;
206 unsigned int machinenum;
207 void *localmachinenum;
208 objheader_t *headeraddr;
210 ptr = record->lookupTable->table;
211 size = record->lookupTable->size;
213 for(i = 0; i < size ; i++) {
215 /* Inner loop to traverse the linked list of the cache lookupTable */
216 while(curr != NULL) {
217 //if the first bin in hash table is empty
222 //Get machine location for object id
224 if ((machinenum = lhashSearch(curr->key)) == 0) {
225 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
229 if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
230 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
233 //Make machine groups
234 if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
235 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
239 /* Check if local or not */
240 if((localmachinenum = mhashSearch(curr->key)) != NULL) {
241 pile->local = 1; //True i.e. local
250 /* This function initiates the transaction commit process
251 * Spawns threads for each of the new connections with Participants
252 * and creates new piles by calling the createPiles(),
253 * Fills the piles with necesaary information and
254 * Sends a transrequest() to each pile*/
255 int transCommit(transrecord_t *record) {
256 unsigned int tot_bytes_mod, *listmid;
259 int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
260 char buffer[RECEIVE_BUFFER_SIZE],control;
261 char transid[TID_LEN];
262 trans_req_data_t *tosend;
263 trans_commit_data_t transinfo;
264 static int newtid = 0;
265 char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
268 /* Look through all the objects in the transaction record and make piles
269 * for each machine involved in the transaction*/
270 pile = createPiles(record);
272 /* Create the packet to be sent in TRANS_REQUEST */
274 /* Count the number of participants */
275 pilecount = pCount(pile);
277 /* Create a list of machine ids(Participants) involved in transaction */
278 if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
279 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
282 pListMid(pile, listmid);
285 /* Initialize thread variables,
286 * Spawn a thread for each Participant involved in a transaction */
287 pthread_t thread[pilecount];
289 pthread_cond_t tcond;
290 pthread_mutex_t tlock;
291 pthread_mutex_t tlshrd;
293 thread_data_array_t *thread_data_array;
294 thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
295 local_thread_data_array_t *ltdata;
296 if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
297 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
301 thread_response_t rcvd_control_msg[pilecount]; /* Shared thread array that keeps track of responses of participants */
303 /* Initialize and set thread detach attribute */
304 pthread_attr_init(&attr);
305 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
306 pthread_mutex_init(&tlock, NULL);
307 pthread_cond_init(&tcond, NULL);
309 /* Process each machine pile */
310 while(pile != NULL) {
311 //Create transaction id
313 //trans_req_data_t *tosend;
314 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
315 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
318 tosend->f.control = TRANS_REQUEST;
319 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
320 tosend->f.mcount = pilecount;
321 tosend->f.numread = pile->numread;
322 tosend->f.nummod = pile->nummod;
323 tosend->f.sum_bytes = pile->sum_bytes;
324 tosend->listmid = listmid;
325 tosend->objread = pile->objread;
326 tosend->oidmod = pile->oidmod;
327 thread_data_array[threadnum].thread_id = threadnum;
328 thread_data_array[threadnum].mid = pile->mid;
329 thread_data_array[threadnum].pilecount = pilecount;
330 thread_data_array[threadnum].buffer = tosend;
331 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
332 thread_data_array[threadnum].threshold = &tcond;
333 thread_data_array[threadnum].lock = &tlock;
334 thread_data_array[threadnum].count = &trecvcount;
335 thread_data_array[threadnum].replyctrl = &treplyctrl;
336 thread_data_array[threadnum].replyretry = &treplyretry;
337 thread_data_array[threadnum].rec = record;
338 /* If local do not create any extra connection */
339 if(pile->local != 1) { /* Not local */
340 rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);
342 perror("Error in pthread create\n");
346 /*Unset the pile->local flag*/
348 /*Set flag to identify that Local machine is involved*/
349 ltdata->tdata = &thread_data_array[threadnum];
350 ltdata->transinfo = &transinfo;
351 val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
353 perror("Error in pthread create\n");
361 /* Free attribute and wait for the other threads */
362 pthread_attr_destroy(&attr);
363 for (i = 0 ;i < pilecount ; i++) {
364 rc = pthread_join(thread[i], NULL);
367 printf("ERROR return code from pthread_join() is %d\n", rc);
373 pthread_cond_destroy(&tcond);
374 pthread_mutex_destroy(&tlock);
378 free(thread_data_array);
381 /* Retry trans commit procedure if not sucessful in the first try */
382 if(treplyretry == 1) {
383 /* wait a random amount of time */
386 /* Retry the commiting transaction again */
393 /* This function sends information involved in the transaction request and
394 * accepts a response from particpants.
395 * It calls decideresponse() to decide on what control message
396 * to send next and sends the message using sendResponse()*/
397 void *transRequest(void *threadarg) {
399 struct sockaddr_in serv_addr;
400 struct hostent *server;
401 thread_data_array_t *tdata;
402 objheader_t *headeraddr;
403 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
404 char machineip[16], retval;
406 tdata = (thread_data_array_t *) threadarg;
408 /* Send Trans Request */
409 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
410 perror("Error in socket for TRANS_REQUEST\n");
413 bzero((char*) &serv_addr, sizeof(serv_addr));
414 serv_addr.sin_family = AF_INET;
415 serv_addr.sin_port = htons(LISTEN_PORT);
416 midtoIP(tdata->mid,machineip);
417 machineip[15] = '\0';
418 serv_addr.sin_addr.s_addr = inet_addr(machineip);
419 /* Open Connection */
420 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
421 perror("Error in connect for TRANS_REQUEST\n");
425 printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
426 /* Send bytes of data with TRANS_REQUEST control message */
427 if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
428 perror("Error sending fixed bytes for thread\n");
431 /* Send list of machines involved in the transaction */
433 int size=sizeof(unsigned int)*tdata->pilecount;
434 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
435 perror("Error sending list of machines for thread\n");
439 /* Send oids and version number tuples for objects that are read */
441 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
442 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
443 perror("Error sending tuples for thread\n");
447 /* Send objects that are modified */
448 for(i = 0; i < tdata->buffer->f.nummod ; i++) {
450 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
451 size=sizeof(objheader_t)+classsize[headeraddr->type];
452 if (send(sd, headeraddr, size, MSG_NOSIGNAL) < size) {
453 perror("Error sending obj modified for thread\n");
458 /* Read control message from Participant */
459 if((n = read(sd, &control, sizeof(char))) <= 0) {
460 perror("Error in reading control message from Participant\n");
463 recvcontrol = control;
465 /* Update common data structure and increment count */
466 tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
468 /* Lock and update count */
469 //Thread sleeps until all messages from pariticipants are received by coordinator
470 pthread_mutex_lock(tdata->lock);
472 (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
474 /* Wake up the threads and invoke decideResponse (once) */
475 if(*(tdata->count) == tdata->pilecount) {
476 if (decideResponse(tdata) != 0) {
477 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
478 pthread_mutex_unlock(tdata->lock);
482 pthread_cond_broadcast(tdata->threshold);
484 pthread_cond_wait(tdata->threshold, tdata->lock);
486 pthread_mutex_unlock(tdata->lock);
488 /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
489 * to all participants in their respective socket */
490 if (sendResponse(tdata, sd) == 0) {
491 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
492 pthread_mutex_unlock(tdata->lock);
497 /* Close connection */
502 /* This function decides the reponse that needs to be sent to
503 * all Participant machines involved in the transaction commit */
504 int decideResponse(thread_data_array_t *tdata) {
506 int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
509 //Check common data structure
510 for (i = 0 ; i < tdata->pilecount ; i++) {
511 /*Switch on response from Participant */
512 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
513 written onto the shared array */
516 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
521 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
525 case TRANS_SOFT_ABORT:
526 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
530 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
535 /* Decide what control message to send to Participant */
536 if(transdisagree > 0) {
538 *(tdata->replyctrl) = TRANS_ABORT;
539 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
540 objstrDelete(tdata->rec->cache);
541 chashDelete(tdata->rec->lookupTable);
543 } else if(transagree == tdata->pilecount){
545 *(tdata->replyctrl) = TRANS_COMMIT;
546 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
547 objstrDelete(tdata->rec->cache);
548 chashDelete(tdata->rec->lookupTable);
550 } else if(transsoftabort > 0 && transdisagree == 0) {
551 /* Send Abort in soft abort case followed by retry commiting transaction again*/
552 *(tdata->replyctrl) = TRANS_ABORT;
553 *(tdata->replyretry) = 1;
554 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
556 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
562 /* This function sends the final response to all threads in their respective socket id */
563 char sendResponse(thread_data_array_t *tdata, int sd) {
564 int n, N, sum, oidcount = 0;
565 char *ptr, retval = 0;
566 unsigned int *oidnotfound;
568 /* If the decided response is due to a soft abort and missing objects at the Participant's side */
569 if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
570 /* Read list of objects missing */
571 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
572 N = oidcount * sizeof(unsigned int);
573 if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
574 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
576 ptr = (char *) oidnotfound;
578 n = read(sd, ptr+sum, N-sum);
580 } while(sum < N && n !=0);
582 retval = TRANS_SOFT_ABORT;
584 /* If the decided response is TRANS_ABORT */
585 if(*(tdata->replyctrl) == TRANS_ABORT) {
586 retval = TRANS_ABORT;
587 } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
588 retval = TRANS_COMMIT;
590 /* Send response to the Participant */
591 if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
592 perror("Error sending ctrl message for participant\n");
598 /* This function opens a connection, places an object read request to the
599 * remote machine, reads the control message and object if available and
600 * copies the object and its header to the local cache.
601 * TODO replace mnum and midtoIP() with MACHINE_IP address later */
603 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
605 struct sockaddr_in serv_addr;
606 struct hostent *server;
612 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
613 perror("Error in socket\n");
616 bzero((char*) &serv_addr, sizeof(serv_addr));
617 serv_addr.sin_family = AF_INET;
618 serv_addr.sin_port = htons(LISTEN_PORT);
619 //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
620 midtoIP(mnum,machineip);
621 machineip[15] = '\0';
622 serv_addr.sin_addr.s_addr = inet_addr(machineip);
623 /* Open connection */
624 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
625 perror("Error in connect\n");
628 char readrequest[sizeof(char)+sizeof(unsigned int)];
629 readrequest[0] = READ_REQUEST;
630 *((unsigned int *)(&readrequest[1])) = oid;
631 if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
632 perror("Error sending message\n");
637 printf("DEBUG -> ready to rcv ...\n");
639 /* Read response from the Participant */
640 if((val = read(sd, &control, sizeof(char))) <= 0) {
641 perror("No control response for getRemoteObj sent\n");
645 case OBJECT_NOT_FOUND:
646 printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
649 /* Read object if found into local cache */
650 if((val = read(sd, &size, sizeof(int))) <= 0) {
651 perror("No size is read from the participant\n");
654 objcopy = objstrAlloc(record->cache, size);
655 if((val = read(sd, objcopy, size)) <= 0) {
656 perror("No objects are read from the remote participant\n");
659 /* Insert into cache's lookup table */
660 chashInsert(record->lookupTable, oid, objcopy);
663 printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
666 /* Close connection */
671 /*This function handles the local trans requests involved in a transaction commiting process
672 * makes a decision if the local machine sends AGREE or DISAGREE or SOFT_ABORT
673 * Activates the other nonlocal threads that are waiting for the decision and the
674 * based on common decision by all groups involved in the transaction it
675 * either commits or aborts the transaction.
676 * It also frees the calloced memory resources
679 void *handleLocalReq(void *threadarg) {
682 char control = 0, *ptr;
684 unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
686 objheader_t *headptr;
687 local_thread_data_array_t *localtdata;
689 localtdata = (local_thread_data_array_t *) threadarg;
691 /* Counters and arrays to formulate decision on control message to be sent */
692 oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
693 oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
694 oidmod = (unsigned int *) calloc(localtdata->tdata->buffer->f.nummod, sizeof(unsigned int));
695 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
696 int objmodnotfound = 0, nummodfound = 0;
698 /* modptr points to the beginning of the object store
699 * created at the Pariticipant */
700 if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
701 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
707 /* Process each oid in the machine pile/ group per thread */
708 for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
709 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
710 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
712 oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
713 incr += sizeof(unsigned int);
714 version = *((short *)(localtdata->tdata->buffer->objread + incr));
715 } else {//Objs modified
716 headptr = (objheader_t *) ptr;
718 oidmod[objmod] = oid;//Array containing modified oids
720 version = headptr->version;
721 ptr += sizeof(objheader_t) + classsize[headptr->type];
724 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
726 /* Save the oids not found and number of oids not found for later use */
727 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
728 /* Save the oids not found and number of oids not found for later use */
730 oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
732 } else { /* If Obj found in machine (i.e. has not moved) */
733 /* Check if Obj is locked by any previous transaction */
734 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
735 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
737 } else {/* If versions don't match ...HARD ABORT */
739 /* Send TRANS_DISAGREE to Coordinator */
740 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
741 printf("DEBUG -> Sending TRANS_DISAGREE\n");
742 //return tdata->recvmsg[tdata->thread_id].rcv_status;
744 } else {/* If Obj is not locked then lock object */
745 ((objheader_t *)mobj)->status |= LOCK;
746 //TODO Remove this for Testing
749 /* Save all object oids that are locked on this machine during this transaction request call */
750 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
752 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
754 } else { /* If versions don't match ...HARD ABORT */
756 /* Send TRANS_DISAGREE to Coordinator */
757 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
758 printf("DEBUG -> Sending TRANS_DISAGREE\n");
759 // return tdata->recvmsg[tdata->thread_id].rcv_status;
765 /*Decide the response to be sent to the Coordinator( the local machine in this case)*/
767 /* Condition to send TRANS_AGREE */
768 if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
769 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
770 printf("DEBUG -> Sending TRANS_AGREE\n");
772 /* Condition to send TRANS_SOFT_ABORT */
773 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
774 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
775 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
776 /* Send number of oids not found and the missing oids if objects are missing in the machine */
777 /* TODO Remember to store the oidnotfound for later use
778 if(objnotfound != 0) {
779 int size = sizeof(unsigned int)* objnotfound;
784 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
785 * if Participant receives a TRANS_COMMIT */
786 localtdata->transinfo->objmod = oidmod;
787 localtdata->transinfo->objlocked = oidlocked;
788 localtdata->transinfo->objnotfound = oidnotfound;
789 localtdata->transinfo->modptr = modptr;
790 localtdata->transinfo->nummod = localtdata->tdata->buffer->f.nummod;
791 localtdata->transinfo->numlocked = objlocked;
792 localtdata->transinfo->numnotfound = objnotfound;
794 /*Set flag to show that common data structure for this individual thread has been written to */
795 //*(tdata->localstatus) |= LM_UPDATED;
797 /* Lock and update count */
798 //Thread sleeps until all messages from pariticipants are received by coordinator
799 pthread_mutex_lock(localtdata->tdata->lock);
800 (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
802 /* Wake up the threads and invoke decideResponse (once) */
803 if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
804 if (decideResponse(localtdata->tdata) != 0) {
805 printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
806 pthread_mutex_unlock(localtdata->tdata->lock);
809 pthread_cond_broadcast(localtdata->tdata->threshold);
811 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
813 pthread_mutex_unlock(localtdata->tdata->lock);
815 /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
816 if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
817 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->transinfo->nummod, localtdata->tdata->buffer->f.numread) != 0) {
818 printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
821 }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
822 if(transComProcess(localtdata->transinfo) != 0) {
823 printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
829 printf("DEBUG -> Freeing...\n");
831 if (localtdata->transinfo->objmod != NULL) {
832 free(localtdata->transinfo->objmod);
833 localtdata->transinfo->objmod = NULL;
835 if (localtdata->transinfo->objlocked != NULL) {
836 free(localtdata->transinfo->objlocked);
837 localtdata->transinfo->objlocked = NULL;
839 if (localtdata->transinfo->objnotfound != NULL) {
840 free(localtdata->transinfo->objnotfound);
841 localtdata->transinfo->objnotfound = NULL;
846 /* This function completes the ABORT process if the transaction is aborting
848 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod, int numread) {
851 objheader_t *tmp_header;
854 printf("DEBUG -> Recv TRANS_ABORT\n");
855 /* Set all ref counts as 1 and do garbage collection */
857 for(i = 0; i< nummod; i++) {
858 tmp_header = (objheader_t *)ptr;
859 tmp_header->rcount = 1;
860 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
862 /* Unlock objects that was locked due to this transaction */
863 for(i = 0; i< numlocked; i++) {
864 header = mhashSearch(objlocked[i]);// find the header address
865 ((objheader_t *)header)->status &= ~(LOCK);
867 //TODO/* Unset the bit for local objects */
869 /* Send ack to Coordinator */
870 printf("DEBUG-> TRANS_SUCCESSFUL\n");
872 /*Free the pointer */
877 /*This function completes the COMMIT process is the transaction is commiting
879 int transComProcess(trans_commit_data_t *transinfo) {
881 int i = 0, offset = 0;
884 printf("DEBUG -> Recv TRANS_COMMIT\n");
885 /* Process each modified object saved in the mainobject store */
886 for(i=0; i<transinfo->nummod; i++) {
887 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
888 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
890 /* Change reference count of older address and free space in objstr ?? */
891 header->rcount = 1; //TODO Not sure what would be the val
893 /* Change ptr address in mhash table */
894 mhashRemove(transinfo->objmod[i]);
895 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
896 offset += sizeof(objheader_t) + classsize[header->type];
898 /* Update object version number */
899 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
900 header->version += 1;
903 /* Unlock locked objects */
904 for(i=0; i<transinfo->numlocked; i++) {
905 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
906 header->status &= ~(LOCK);
909 //TODO Update location lookup table
910 //TODO/* Unset the bit for local objects */
912 /* Send ack to Coordinator */
913 printf("DEBUG-> TRANS_SUCESSFUL\n");
917 /* This function is called by the thread calling transPrefetch */
918 void *transPrefetch(void *prefdata) {
919 int *offstarray = NULL;
920 prefetchqelem_t *qnode;
923 /* lock mutex of primary prefetch queue */
924 pthread_mutex_lock(&pqueue.qlock);
925 /* while primary queue is empty, then wait */
926 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
927 pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
930 /* dequeue node to create a machine piles and finally unlock mutex */
931 if((qnode = dequeue()) == NULL) {
932 printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
935 pthread_mutex_unlock(&pqueue.qlock);
936 /* Reduce redundant prefetch requests */
937 checkPrefetchTuples(qnode);
938 /* Check if the tuples are found locally, if yes then reduce them further*/
939 /* and group requests by remote machine ids by calling the makePreGroups() */
942 /* Lock mutex of pool queue */
943 pthread_mutex_lock(&mcqueue.qlock);
944 /* Update the pool queue with the new remote machine piles generated per prefetch call */
947 /* Broadcast signal on pool queue */
949 /* Unlock mutex of pool queue */
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 void 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 void 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;
1136 ptr = (char *) node;
1137 ntuples = *(GET_NTUPLES(ptr));
1138 oid = GET_PTR_OID(ptr);
1139 endoffsets = GET_PTR_EOFF(ptr, ntuples);
1140 arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1141 /* Find offset length for each tuple */
1142 int numoffset[ntuples];//Number of offsets for each tuple
1143 numoffset[0] = endoffsets[0];
1144 for(i = 1; i<ntuples; i++) {
1145 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1147 for(i = 0; i < ntuples; i++) {
1150 /* If object found locally */
1151 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) {
1153 tmp = (char *) objheader;
1154 /* Find the oid of its offset value */
1158 index = endoffsets[i - 1];
1159 for(j = 0 ; j < numoffset[i] ; j++) {
1160 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1161 /*If oid found locally then
1162 *assign the latest oid found as the new oid
1163 *and copy left over offsets into the arrayoffsetfieldarray*/
1165 numoffset[i] = numoffset[i] - (j+1);
1166 for(k = 0; k < numoffset[i]; k++)
1167 arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1169 /*New offset oid not found */
1170 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1172 checkPreCache(node, &numoffset, j, numoffset[i], objoid, index, i, oidnfound);
1178 /*If all offset oids are found locally,make the prefetch tuple invalid */
1185 /* Look in Prefetch cache */
1186 checkPreCache(node, &numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound);
1190 // Make machine groups
1191 makePreGroups(node, numoffset);
1195 /*This function is called by the thread that processes the
1196 * prefetch request makes piles to prefetch records and prefetches the oids from remote machines */
1197 int transPrefetchProcess(transrecord_t *record, int *arrayofoffset[], short numoids){
1199 int arraylength[numoids];
1200 unsigned int machinenumber;
1201 objheader_t *tmp, *objheader;
1204 pthread_attr_t attr;
1206 /* Given tuple find length of tuple*/
1207 for(i = 0; i < numoids ; i++) {
1208 arraylength[i] = arrayLength(arrayofoffset[i]);
1211 /* Initialize and set thread attributes
1212 * Spawn a thread for each prefetch request sent*/
1213 pthread_t thread[numoids];
1214 pthread_attr_init(&attr);
1215 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1217 /* Create Machine Piles to send prefetch requests use threads*/
1218 for( i = 0 ; i< numoids ; i++) {
1219 if(arrayofoffset[i][0] == -1)
1222 /* For each Pile in the machine send TRANS_PREFETCH */
1223 //makePiles(arrayofoffset, numoids);
1224 /* Fill thread data structure */
1225 rc = pthread_create(&thread[i] , &attr, sendPrefetchReq, (void *) arrayofoffset[i]);
1227 perror("Error in pthread create at transPrefetchProcess()\n");
1234 /* Free attribute and wait to join other threads */
1235 for (i = 0 ;i < numoids ; i++) {
1236 rc = pthread_join(thread[i], NULL);
1238 perror("Error pthread_join() in transPrefetchProcess()\n");
1242 pthread_attr_destroy(&attr);
1248 void *sendPrefetchReq(void *prefetchtuple) {
1250 struct sockaddr_in serv_addr;
1251 struct hostent *server;
1252 char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
1253 char machineip[16], retval;
1256 /* Send Trans Prefetch Request */
1257 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1258 perror("Error in socket for TRANS_REQUEST\n");
1261 bzero((char*) &serv_addr, sizeof(serv_addr));
1262 serv_addr.sin_family = AF_INET;
1263 serv_addr.sin_port = htons(LISTEN_PORT);
1264 //midtoIP(tdata->mid,machineip);
1265 // machineip[15] = '\0';
1266 // serv_addr.sin_addr.s_addr = inet_addr(machineip);
1268 /* Open Connection */
1269 if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1270 perror("Error in connect for TRANS_REQUEST\n");