more changes
[IRC.git] / Robust / src / Runtime / DSTM / interface / trans.c
1 #include "dstm.h"
2 #include "ip.h"
3 #include "clookup.h"
4 #include "machinepile.h"
5 #include "mlookup.h"
6 #include "llookup.h"
7 #include "plookup.h"
8 #include "prelookup.h"
9 #include "queue.h"
10 #include <pthread.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netdb.h>
14 #include <netinet/in.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <time.h>
19 #include <string.h>
20 #include <pthread.h>
21
22 #define LISTEN_PORT 2156
23 #define RECEIVE_BUFFER_SIZE 2048
24 #define NUM_THREADS 10
25 #define PREFETCH_CACHE_SIZE 1048576 //1MB
26
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
34 pthread_t tPrefetch;
35 extern objstr_t *mainobjstore;
36
37 plistnode_t *createPiles(transrecord_t *);
38 inline int arrayLength(int *array) {
39         int i;
40         for(i=0 ;array[i] != -1; i++)
41                 ;
42         return i;
43 }
44 inline int findmax(int *array, int arraylength) {
45         int max, i;
46         max = array[0];
47         for(i = 0; i < arraylength; i++){
48                 if(array[i] > max) {
49                         max = array[i];
50                 }
51         }
52         return max;
53 }
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) {
57         int qnodesize;
58         int len = 0;
59
60         /* Allocate for the queue node*/
61         char *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__);
65                 return;
66         }
67         /* Set queue node values */
68         len = sizeof(prefetchqelem_t);
69         memcpy(node + len, &ntuples, sizeof(int));
70         len += sizeof(int);
71         memcpy(node + len, oids, ntuples*sizeof(unsigned int));
72         len += ntuples * sizeof(unsigned int);
73         memcpy(node + len, endoffsets, ntuples*sizeof(short));
74         len += ntuples * sizeof(short);
75         memcpy(node + len, arrayfields, endoffsets[ntuples-1]*sizeof(short));
76         /* Lock and insert into primary prefetch queue */
77         pthread_mutex_lock(&pqueue.qlock);
78         enqueue((prefetchqelem_t *)node);
79         pthread_cond_signal(&pqueue.qcond);
80         pthread_mutex_unlock(&pqueue.qlock);
81 }
82
83 /* This function initiates the prefetch thread
84  * A queue is shared between the main thread of execution
85  * and the prefetch thread to process the prefetch call
86  * Call from compiler populates the shared queue with prefetch requests while prefetch thread
87  * processes the prefetch requests */
88 void transInit() {
89         int t, rc;
90         //Create and initialize prefetch cache structure
91         prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE);
92         //Create prefetch cache lookup table
93         if(prehashCreate(HASH_SIZE, LOADFACTOR))
94                 return; //Failure
95         //Initialize primary shared queue
96         queueInit();
97         //Initialize machine pile w/prefetch oids and offsets shared queue
98         mcpileqInit();
99         //Create the primary prefetch thread 
100         pthread_create(&tPrefetch, NULL, transPrefetch, NULL);
101         //Create and Initialize a pool of threads 
102         for(t = 0; t< NUM_THREADS; t++) {
103                 rc = pthread_create(&wthreads[t], NULL, mcqProcess, (void *)t);
104                 if (rc) {
105                         printf("Thread create error %s, %d\n", __FILE__, __LINE__);
106                         return;
107                 }
108         }
109         //TODO when to deletethreads
110 }
111
112 /* This function stops the threads spawned */
113 void transExit() {
114         int t;
115         pthread_cancel(tPrefetch);
116         for(t = 0; t < NUM_THREADS; t++)
117                 pthread_cancel(wthreads[t]);
118
119         return;
120 }
121
122 /* This functions inserts randowm wait delays in the order of msec
123  * Mostly used when transaction commits retry*/
124 void randomdelay(void)
125 {
126         struct timespec req, rem;
127         time_t t;
128
129         t = time(NULL);
130         req.tv_sec = 0;
131         req.tv_nsec = (long)(1000000 + (t%10000000)); //1-11 msec
132         nanosleep(&req, &rem);
133         return;
134 }
135
136 /* This function initializes things required in the transaction start*/
137 transrecord_t *transStart()
138 {
139         transrecord_t *tmp = malloc(sizeof(transrecord_t));
140         tmp->cache = objstrCreate(1048576);
141         tmp->lookupTable = chashCreate(HASH_SIZE, LOADFACTOR);
142
143         return tmp;
144 }
145
146 /* This function finds the location of the objects involved in a transaction
147  * and returns the pointer to the object if found in a remote location */
148 objheader_t *transRead(transrecord_t *record, unsigned int oid)
149 {       
150         unsigned int machinenumber;
151         objheader_t *tmp, *objheader;
152         void *objcopy;
153         int size, rc, found = 0;
154         void *buf;
155         struct timespec ts;
156         struct timeval tp;
157         
158         rc = gettimeofday(&tp, NULL);
159
160         /* Convert from timeval to timespec */
161         ts.tv_nsec = tp.tv_usec * 1000;
162
163         /* Search local transaction cache */
164         if((objheader = (objheader_t *)chashSearch(record->lookupTable, oid)) != NULL){
165                 return(objheader);
166         } else if ((objheader = (objheader_t *) mhashSearch(oid)) != NULL) {
167                 /* Look up in machine lookup table  and copy  into cache*/
168                 tmp = mhashSearch(oid);
169                 size = sizeof(objheader_t)+classsize[tmp->type];
170                 objcopy = objstrAlloc(record->cache, size);
171                 memcpy(objcopy, (void *)objheader, size);
172                 /* Insert into cache's lookup table */
173                 chashInsert(record->lookupTable, objheader->oid, objcopy); 
174                 return(objcopy);
175         } else if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
176                 found = 1;
177                 size = sizeof(objheader_t)+classsize[tmp->type];
178                 objcopy = objstrAlloc(record->cache, size);
179                 memcpy(objcopy, (void *)tmp, size);
180                 /* Insert into cache's lookup table */
181                 chashInsert(record->lookupTable, tmp->oid, objcopy); 
182                 return(objcopy);
183         } else { /* If not found anywhere, then block until object appears in prefetch cache */
184                 pthread_mutex_lock(&pflookup.lock);
185                 while(!found) {
186                         rc = pthread_cond_timedwait(&pflookup.cond, &pflookup.lock, &ts);
187                         if(rc == ETIMEDOUT) {
188                                 printf("Wait timed out\n");
189                                 /* Check Prefetch cache again */
190                                 if((tmp = (objheader_t *) prehashSearch(oid)) != NULL) { /* Look up in prefetch cache */
191                                         found = 1;
192                                         size = sizeof(objheader_t)+classsize[tmp->type];
193                                         objcopy = objstrAlloc(record->cache, size);
194                                         memcpy(objcopy, (void *)tmp, size);
195                                         /* Insert into cache's lookup table */
196                                         chashInsert(record->lookupTable, tmp->oid, objcopy); 
197                                         return(objcopy);
198                                 } else {
199                                         pthread_mutex_unlock(&pflookup.lock);
200                                         break;
201                                 }
202                                 pthread_mutex_unlock(&pflookup.lock);
203                         }
204                 }
205                 /* Get the object from the remote location */
206                 machinenumber = lhashSearch(oid);
207                 objcopy = getRemoteObj(record, machinenumber, oid);
208                 if(objcopy == NULL) {
209                         //If object is not found in Remote location
210                         //printf("Object oid = %d not found in Machine %d\n", oid, machinenumber);
211                         return NULL;
212                 }
213                 else {
214                         //printf("Object oid = %d found in Machine %d\n", oid, machinenumber);
215                         return(objcopy);
216                 }
217         } 
218 }
219
220 /* This function creates objects in the transaction record */
221 objheader_t *transCreateObj(transrecord_t *record, unsigned short type)
222 {
223         objheader_t *tmp = (objheader_t *) objstrAlloc(record->cache, (sizeof(objheader_t) + classsize[type]));
224         tmp->oid = getNewOID();
225         tmp->type = type;
226         tmp->version = 1;
227         tmp->rcount = 0; //? not sure how to handle this yet
228         tmp->status = 0;
229         tmp->status |= NEW;
230         chashInsert(record->lookupTable, tmp->oid, tmp);
231         return tmp;
232 }
233
234 /* This function creates machine piles based on all machines involved in a
235  * transaction commit request */
236 plistnode_t *createPiles(transrecord_t *record) {
237         int i = 0;
238         unsigned int size;/* Represents number of bins in the chash table */
239         chashlistnode_t *curr, *ptr, *next;
240         plistnode_t *pile = NULL;
241         unsigned int machinenum;
242         void *localmachinenum;
243         objheader_t *headeraddr;
244
245         ptr = record->lookupTable->table;
246         size = record->lookupTable->size;
247
248         for(i = 0; i < size ; i++) {
249                 curr = &ptr[i];
250                 /* Inner loop to traverse the linked list of the cache lookupTable */
251                 while(curr != NULL) {
252                         //if the first bin in hash table is empty
253                         if(curr->key == 0) {
254                                 break;
255                         }
256                         next = curr->next;
257                         //Get machine location for object id
258
259                         if ((machinenum = lhashSearch(curr->key)) == 0) {
260                                 printf("Error: No such machine %s, %d\n", __FILE__, __LINE__);
261                                 return NULL;
262                         }
263
264                         if ((headeraddr = chashSearch(record->lookupTable, curr->key)) == NULL) {
265                                 printf("Error: No such oid %s, %d\n", __FILE__, __LINE__);
266                                 return NULL;
267                         }
268                         //Make machine groups
269                         if ((pile = pInsert(pile, headeraddr, machinenum, record->lookupTable->numelements)) == NULL) {
270                                 printf("pInsert error %s, %d\n", __FILE__, __LINE__);
271                                 return NULL;
272                         }
273
274                         /* Check if local or not */
275                         if((localmachinenum = mhashSearch(curr->key)) != NULL) { 
276                                 /* Set the pile->local flag*/
277                                 pile->local = 1; //True i.e. local
278                         }
279
280                         curr = next;
281                 }
282         }
283         return pile; 
284 }
285
286 /* This function initiates the transaction commit process
287  * Spawns threads for each of the new connections with Participants 
288  * and creates new piles by calling the createPiles(),
289  * Fills the piles with necesaary information and 
290  * Sends a transrequest() to each pile*/
291 int transCommit(transrecord_t *record) {        
292         unsigned int tot_bytes_mod, *listmid;
293         plistnode_t *pile;
294         int i, rc, val;
295         int pilecount = 0, offset, threadnum = 0, trecvcount = 0, tmachcount = 0;
296         char buffer[RECEIVE_BUFFER_SIZE],control;
297         char transid[TID_LEN];
298         trans_req_data_t *tosend;
299         trans_commit_data_t transinfo;
300         static int newtid = 0;
301         char treplyctrl = 0, treplyretry = 0; /* keeps track of the common response that needs to be sent */
302         char localstat = 0;
303
304         /* Look through all the objects in the transaction record and make piles 
305          * for each machine involved in the transaction*/
306         pile = createPiles(record);
307
308         /* Create the packet to be sent in TRANS_REQUEST */
309
310         /* Count the number of participants */
311         pilecount = pCount(pile);
312
313         /* Create a list of machine ids(Participants) involved in transaction   */
314         if((listmid = calloc(pilecount, sizeof(unsigned int))) == NULL) {
315                 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
316                 return 1;
317         }               
318         pListMid(pile, listmid);
319
320
321         /* Initialize thread variables,
322          * Spawn a thread for each Participant involved in a transaction */
323         pthread_t thread[pilecount];
324         pthread_attr_t attr;                    
325         pthread_cond_t tcond;
326         pthread_mutex_t tlock;
327         pthread_mutex_t tlshrd;
328
329         thread_data_array_t *thread_data_array;
330         thread_data_array = (thread_data_array_t *) malloc(sizeof(thread_data_array_t)*pilecount);
331         local_thread_data_array_t *ltdata;
332         if((ltdata = calloc(1, sizeof(local_thread_data_array_t))) == NULL) {
333                 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
334                 return 1;
335         }
336
337         thread_response_t rcvd_control_msg[pilecount];  /* Shared thread array that keeps track of responses of participants */
338
339         /* Initialize and set thread detach attribute */
340         pthread_attr_init(&attr);
341         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
342         pthread_mutex_init(&tlock, NULL);
343         pthread_cond_init(&tcond, NULL);
344
345         /* Process each machine pile */
346         while(pile != NULL) {
347                 //Create transaction id
348                 newtid++;
349                 //trans_req_data_t *tosend;
350                 if ((tosend = calloc(1, sizeof(trans_req_data_t))) == NULL) {
351                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
352                         return 1;
353                 }
354                 tosend->f.control = TRANS_REQUEST;
355                 sprintf(tosend->f.trans_id, "%x_%d", pile->mid, newtid);
356                 tosend->f.mcount = pilecount;
357                 tosend->f.numread = pile->numread;
358                 tosend->f.nummod = pile->nummod;
359                 tosend->f.sum_bytes = pile->sum_bytes;
360                 tosend->listmid = listmid;
361                 tosend->objread = pile->objread;
362                 tosend->oidmod = pile->oidmod;
363                 thread_data_array[threadnum].thread_id = threadnum;
364                 thread_data_array[threadnum].mid = pile->mid;
365                 thread_data_array[threadnum].pilecount = pilecount;
366                 thread_data_array[threadnum].buffer = tosend;
367                 thread_data_array[threadnum].recvmsg = rcvd_control_msg;
368                 thread_data_array[threadnum].threshold = &tcond;
369                 thread_data_array[threadnum].lock = &tlock;
370                 thread_data_array[threadnum].count = &trecvcount;
371                 thread_data_array[threadnum].replyctrl = &treplyctrl;
372                 thread_data_array[threadnum].replyretry = &treplyretry;
373                 thread_data_array[threadnum].rec = record;
374                 /* If local do not create any extra connection */
375                 if(pile->local != 1) { /* Not local */
376                         rc = pthread_create(&thread[threadnum], NULL, transRequest, (void *) &thread_data_array[threadnum]);  
377                         if (rc) {
378                                 perror("Error in pthread create\n");
379                                 return 1;
380                         }
381                 } else { /*Local*/
382                         /*Unset the pile->local flag*/
383                         pile->local = 0;
384                         /*Set flag to identify that Local machine is involved*/
385                         ltdata->tdata = &thread_data_array[threadnum];
386                         ltdata->transinfo = &transinfo;
387                         val = pthread_create(&thread[threadnum], NULL, handleLocalReq, (void *) ltdata);
388                         if (val) {
389                                 perror("Error in pthread create\n");
390                                 return 1;
391                         }
392                 }
393                 threadnum++;            
394                 pile = pile->next;
395         }
396
397         /* Free attribute and wait for the other threads */
398         pthread_attr_destroy(&attr);
399         for (i = 0 ;i < pilecount ; i++) {
400                 rc = pthread_join(thread[i], NULL);
401                 if (rc)
402                 {
403                         printf("ERROR return code from pthread_join() is %d\n", rc);
404                         return 1;
405                 }
406         }
407
408         /* Free resources */    
409         pthread_cond_destroy(&tcond);
410         pthread_mutex_destroy(&tlock);
411         free(tosend);
412         free(listmid);
413         pDelete(pile);
414         free(thread_data_array);
415         free(ltdata);
416
417         /* Retry trans commit procedure if not sucessful in the first try */
418         if(treplyretry == 1) {
419                 /* wait a random amount of time */
420                 randomdelay();
421                 //sleep(1);
422                 /* Retry the commiting transaction again */
423                 transCommit(record);
424         }
425
426         return 0;
427 }
428
429 /* This function sends information involved in the transaction request and 
430  * accepts a response from particpants.
431  * It calls decideresponse() to decide on what control message 
432  * to send next and sends the message using sendResponse()*/
433 void *transRequest(void *threadarg) {
434         int sd, i, n;
435         struct sockaddr_in serv_addr;
436         struct hostent *server;
437         thread_data_array_t *tdata;
438         objheader_t *headeraddr;
439         char buffer[RECEIVE_BUFFER_SIZE], control, recvcontrol;
440         char machineip[16], retval;
441
442         tdata = (thread_data_array_t *) threadarg;
443
444         /* Send Trans Request */
445         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
446                 perror("Error in socket for TRANS_REQUEST\n");
447                 return NULL;
448         }
449         bzero((char*) &serv_addr, sizeof(serv_addr));
450         serv_addr.sin_family = AF_INET;
451         serv_addr.sin_port = htons(LISTEN_PORT);
452         midtoIP(tdata->mid,machineip);
453         machineip[15] = '\0';
454         serv_addr.sin_addr.s_addr = inet_addr(machineip);
455         /* Open Connection */
456         if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
457                 perror("Error in connect for TRANS_REQUEST\n");
458                 return NULL;
459         }
460
461         printf("DEBUG-> trans.c Sending TRANS_REQUEST to mid %s\n", machineip);
462         /* Send bytes of data with TRANS_REQUEST control message */
463         if (send(sd, &(tdata->buffer->f), sizeof(fixed_data_t),MSG_NOSIGNAL) < sizeof(fixed_data_t)) {
464                 perror("Error sending fixed bytes for thread\n");
465                 return NULL;
466         }
467         /* Send list of machines involved in the transaction */
468         {
469                 int size=sizeof(unsigned int)*tdata->pilecount;
470                 if (send(sd, tdata->buffer->listmid, size, MSG_NOSIGNAL) < size) {
471                         perror("Error sending list of machines for thread\n");
472                         return NULL;
473                 }
474         }
475         /* Send oids and version number tuples for objects that are read */
476         {
477                 int size=(sizeof(unsigned int)+sizeof(short))*tdata->buffer->f.numread;
478                 if (send(sd, tdata->buffer->objread, size, MSG_NOSIGNAL) < size) {
479                         perror("Error sending tuples for thread\n");
480                         return NULL;
481                 }
482         }
483         /* Send objects that are modified */
484         for(i = 0; i < tdata->buffer->f.nummod ; i++) {
485                 int size;
486                 headeraddr = chashSearch(tdata->rec->lookupTable, tdata->buffer->oidmod[i]);
487                 size=sizeof(objheader_t)+classsize[headeraddr->type];
488                 if (send(sd, headeraddr, size, MSG_NOSIGNAL)  < size) {
489                         perror("Error sending obj modified for thread\n");
490                         return NULL;
491                 }
492         }
493
494         /* Read control message from Participant */
495         if((n = read(sd, &control, sizeof(char))) <= 0) {
496                 perror("Error in reading control message from Participant\n");
497                 return NULL;
498         }
499         recvcontrol = control;
500
501         /* Update common data structure and increment count */
502         tdata->recvmsg[tdata->thread_id].rcv_status = recvcontrol;
503
504         /* Lock and update count */
505         //Thread sleeps until all messages from pariticipants are received by coordinator
506         pthread_mutex_lock(tdata->lock);
507
508         (*(tdata->count))++; /* keeps track of no of messages received by the coordinator */
509
510         /* Wake up the threads and invoke decideResponse (once) */
511         if(*(tdata->count) == tdata->pilecount) {
512                 if (decideResponse(tdata) != 0) { 
513                         printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
514                         pthread_mutex_unlock(tdata->lock);
515                         close(sd);
516                         return NULL;
517                 }
518                 pthread_cond_broadcast(tdata->threshold);
519         } else {
520                 pthread_cond_wait(tdata->threshold, tdata->lock);
521         }
522         pthread_mutex_unlock(tdata->lock);
523
524         /* Send the final response such as TRANS_COMMIT or TRANS_ABORT t
525          * to all participants in their respective socket */
526         if (sendResponse(tdata, sd) == 0) { 
527                 printf("sendResponse returned error %s,%d\n", __FILE__, __LINE__);
528                 pthread_mutex_unlock(tdata->lock);
529                 close(sd);
530                 return NULL;
531         }
532
533         /* Close connection */
534         close(sd);
535         pthread_exit(NULL);
536 }
537
538 /* This function decides the reponse that needs to be sent to 
539  * all Participant machines involved in the transaction commit */
540 int decideResponse(thread_data_array_t *tdata) {
541         char control;
542         int i, transagree = 0, transdisagree = 0, transsoftabort = 0; /* Counters to formulate decision of what
543                                                                          message to send */
544
545         //Check common data structure 
546         for (i = 0 ; i < tdata->pilecount ; i++) {
547                 /*Switch on response from Participant */
548                 control = tdata->recvmsg[i].rcv_status; /* tdata: keeps track of all participant responses
549                                                            written onto the shared array */
550                 switch(control) {
551                         case TRANS_DISAGREE:
552                                 printf("DEBUG-> trans.c Recv TRANS_DISAGREE\n");
553                                 transdisagree++;
554                                 break;
555
556                         case TRANS_AGREE:
557                                 printf("DEBUG-> trans.c Recv TRANS_AGREE\n");
558                                 transagree++;
559                                 break;
560
561                         case TRANS_SOFT_ABORT:
562                                 printf("DEBUG-> trans.c Recv TRANS_SOFT_ABORT\n");
563                                 transsoftabort++;
564                                 break;
565                         default:
566                                 printf("Participant sent unknown message in %s, %d\n", __FILE__, __LINE__);
567                                 return -1;
568                 }
569         }
570
571         /* Decide what control message to send to Participant */        
572         if(transdisagree > 0) {
573                 /* Send Abort */
574                 *(tdata->replyctrl) = TRANS_ABORT;
575                 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
576                 objstrDelete(tdata->rec->cache);
577                 chashDelete(tdata->rec->lookupTable);
578                 free(tdata->rec);
579         } else if(transagree == tdata->pilecount){
580                 /* Send Commit */
581                 *(tdata->replyctrl) = TRANS_COMMIT;
582                 printf("DEBUG-> trans.c Sending TRANS_COMMIT\n");
583                 objstrDelete(tdata->rec->cache);
584                 chashDelete(tdata->rec->lookupTable);
585                 free(tdata->rec);
586         } else if(transsoftabort > 0 && transdisagree == 0) {
587                 /* Send Abort in soft abort case followed by retry commiting transaction again*/
588                 *(tdata->replyctrl) = TRANS_ABORT;
589                 *(tdata->replyretry) = 1;
590                 printf("DEBUG-> trans.c Sending TRANS_ABORT\n");
591         } else {
592                 printf("DEBUG -> %s, %d: Error: undecided response\n", __FILE__, __LINE__);
593                 return -1;
594         }
595
596         return 0;
597 }
598 /* This function sends the final response to all threads in their respective socket id */
599 char sendResponse(thread_data_array_t *tdata, int sd) {
600         int n, N, sum, oidcount = 0;
601         char *ptr, retval = 0;
602         unsigned int *oidnotfound;
603
604         /* If the decided response is due to a soft abort and missing objects at the Participant's side */
605         if(tdata->recvmsg[tdata->thread_id].rcv_status == TRANS_SOFT_ABORT) {
606                 /* Read list of objects missing */
607                 if((read(sd, &oidcount, sizeof(int)) != 0) && (oidcount != 0)) {
608                         N = oidcount * sizeof(unsigned int);
609                         if((oidnotfound = calloc(oidcount, sizeof(unsigned int))) == NULL) {
610                                 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
611                         }
612                         ptr = (char *) oidnotfound;
613                         do {
614                                 n = read(sd, ptr+sum, N-sum);
615                                 sum += n;
616                         } while(sum < N && n !=0);
617                 }
618                 retval =  TRANS_SOFT_ABORT;
619         }
620         /* If the decided response is TRANS_ABORT */
621         if(*(tdata->replyctrl) == TRANS_ABORT) {
622                 retval = TRANS_ABORT;
623         } else if(*(tdata->replyctrl) == TRANS_COMMIT) { /* If the decided response is TRANS_COMMIT */
624                 retval = TRANS_COMMIT;
625         }
626         /* Send response to the Participant */
627         if (send(sd, tdata->replyctrl, sizeof(char),MSG_NOSIGNAL) < sizeof(char)) {
628                 perror("Error sending ctrl message for participant\n");
629         }
630
631         return retval;
632 }
633
634 /* This function opens a connection, places an object read request to the 
635  * remote machine, reads the control message and object if available  and 
636  * copies the object and its header to the local cache.
637  * TODO replace mnum and midtoIP() with MACHINE_IP address later */ 
638
639 void *getRemoteObj(transrecord_t *record, unsigned int mnum, unsigned int oid) {
640         int sd, size, val;
641         struct sockaddr_in serv_addr;
642         struct hostent *server;
643         char control;
644         char machineip[16];
645         objheader_t *h;
646         void *objcopy;
647
648         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
649                 perror("Error in socket\n");
650                 return NULL;
651         }
652         bzero((char*) &serv_addr, sizeof(serv_addr));
653         serv_addr.sin_family = AF_INET;
654         serv_addr.sin_port = htons(LISTEN_PORT);
655         //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
656         midtoIP(mnum,machineip);
657         machineip[15] = '\0';
658         serv_addr.sin_addr.s_addr = inet_addr(machineip);
659         /* Open connection */
660         if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
661                 perror("Error in connect\n");
662                 return NULL;
663         }
664         char readrequest[sizeof(char)+sizeof(unsigned int)];
665         readrequest[0] = READ_REQUEST;
666         *((unsigned int *)(&readrequest[1])) = oid;
667         if (send(sd, &readrequest, sizeof(readrequest), MSG_NOSIGNAL) < sizeof(readrequest)) {
668                 perror("Error sending message\n");
669                 return NULL;
670         }
671
672 #ifdef DEBUG1
673         printf("DEBUG -> ready to rcv ...\n");
674 #endif
675         /* Read response from the Participant */
676         if((val = read(sd, &control, sizeof(char))) <= 0) {
677                 perror("No control response for getRemoteObj sent\n");
678                 return NULL;
679         }
680         switch(control) {
681                 case OBJECT_NOT_FOUND:
682                         printf("DEBUG -> Control OBJECT_NOT_FOUND received\n");
683                         return NULL;
684                 case OBJECT_FOUND:
685                         /* Read object if found into local cache */
686                         if((val = read(sd, &size, sizeof(int))) <= 0) {
687                                 perror("No size is read from the participant\n");
688                                 return NULL;
689                         }
690                         objcopy = objstrAlloc(record->cache, size);
691                         if((val = read(sd, objcopy, size)) <= 0) {
692                                 perror("No objects are read from the remote participant\n");
693                                 return NULL;
694                         }
695                         /* Insert into cache's lookup table */
696                         chashInsert(record->lookupTable, oid, objcopy); 
697                         break;
698                 default:
699                         printf("Error in recv request from participant on a READ_REQUEST %s, %d\n",__FILE__, __LINE__);
700                         return NULL;
701         }
702         /* Close connection */
703         close(sd);
704         return objcopy;
705 }
706
707 /*This function handles the local trans requests involved in a transaction commiting process
708  * makes a decision if the local machine sends AGREE or DISAGREE or SOFT_ABORT
709  * Activates the other nonlocal threads that are waiting for the decision and the
710  * based on common decision by all groups involved in the transaction it 
711  * either commits or aborts the transaction.
712  * It also frees the calloced memory resources
713  */
714
715 void *handleLocalReq(void *threadarg) {
716         int val, i = 0;
717         short version;
718         char control = 0, *ptr;
719         unsigned int oid;
720         unsigned int *oidnotfound = NULL, *oidlocked = NULL, *oidmod = NULL;
721         void *mobj, *modptr;
722         objheader_t *headptr;
723         local_thread_data_array_t *localtdata;
724
725         localtdata = (local_thread_data_array_t *) threadarg;
726
727         /* Counters and arrays to formulate decision on control message to be sent */
728         oidnotfound = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
729         oidlocked = (unsigned int *) calloc((localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod), sizeof(unsigned int));
730         oidmod = (unsigned int *) calloc(localtdata->tdata->buffer->f.nummod, sizeof(unsigned int));
731         int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
732         int objmodnotfound = 0, nummodfound = 0;
733
734         /* modptr points to the beginning of the object store 
735          * created at the Pariticipant */ 
736         if ((modptr = objstrAlloc(mainobjstore, localtdata->tdata->buffer->f.sum_bytes)) == NULL) {
737                 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
738                 return NULL;
739         }
740
741         ptr = modptr;
742
743         /* Process each oid in the machine pile/ group per thread */
744         for (i = 0; i < localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod; i++) {
745                 if (i < localtdata->tdata->buffer->f.numread) {//Objs only read and not modified
746                         int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
747                         incr *= i;
748                         oid = *((unsigned int *)(localtdata->tdata->buffer->objread + incr));
749                         incr += sizeof(unsigned int);
750                         version = *((short *)(localtdata->tdata->buffer->objread + incr));
751                 } else {//Objs modified
752                         headptr = (objheader_t *) ptr;
753                         oid = headptr->oid;
754                         oidmod[objmod] = oid;//Array containing modified oids
755                         objmod++;
756                         version = headptr->version;
757                         ptr += sizeof(objheader_t) + classsize[headptr->type];
758                 }
759
760                 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
761
762                 /* Save the oids not found and number of oids not found for later use */
763                 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
764                         /* Save the oids not found and number of oids not found for later use */
765
766                         oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
767                         objnotfound++;
768                 } else { /* If Obj found in machine (i.e. has not moved) */
769                         /* Check if Obj is locked by any previous transaction */
770                         if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
771                                 if (version == ((objheader_t *)mobj)->version) {      /* If not locked then match versions */ 
772                                         v_matchlock++;
773                                 } else {/* If versions don't match ...HARD ABORT */
774                                         v_nomatch++;
775                                         /* Send TRANS_DISAGREE to Coordinator */
776                                         localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
777                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
778                                         //return tdata->recvmsg[tdata->thread_id].rcv_status;  
779                                 }
780                         } else {/* If Obj is not locked then lock object */
781                                 ((objheader_t *)mobj)->status |= LOCK;
782                                 //TODO Remove this for Testing
783                                 randomdelay();
784
785                                 /* Save all object oids that are locked on this machine during this transaction request call */
786                                 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
787                                 objlocked++;
788                                 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
789                                         v_matchnolock++;
790                                 } else { /* If versions don't match ...HARD ABORT */
791                                         v_nomatch++;
792                                         /* Send TRANS_DISAGREE to Coordinator */
793                                         localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_DISAGREE;
794                                         printf("DEBUG -> Sending TRANS_DISAGREE\n");
795                                         //      return tdata->recvmsg[tdata->thread_id].rcv_status;  
796                                 }
797                         }
798                 }
799         }
800
801         /*Decide the response to be sent to the Coordinator( the local machine in this case)*/
802
803         /* Condition to send TRANS_AGREE */
804         if(v_matchnolock == localtdata->tdata->buffer->f.numread + localtdata->tdata->buffer->f.nummod) {
805                 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_AGREE;
806                 printf("DEBUG -> Sending TRANS_AGREE\n");
807         }
808         /* Condition to send TRANS_SOFT_ABORT */
809         if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
810                 localtdata->tdata->recvmsg[localtdata->tdata->thread_id].rcv_status = TRANS_SOFT_ABORT;
811                 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
812                 //TODO  currently the only soft abort case that is supported is when object locked by previous
813                 //transaction => v_matchlock > 0 
814                 //The other case for SOFT ABORT i.e. when object is not found but versions match is not supported 
815                 /* Send number of oids not found and the missing oids if objects are missing in the machine */
816                 /* TODO Remember to store the oidnotfound for later use
817                    if(objnotfound != 0) {
818                    int size = sizeof(unsigned int)* objnotfound;
819                    }
820                    */
821         }
822
823         /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
824          * if Participant receives a TRANS_COMMIT */
825         localtdata->transinfo->objmod = oidmod;
826         localtdata->transinfo->objlocked = oidlocked;
827         localtdata->transinfo->objnotfound = oidnotfound;
828         localtdata->transinfo->modptr = modptr;
829         localtdata->transinfo->nummod = localtdata->tdata->buffer->f.nummod;
830         localtdata->transinfo->numlocked = objlocked;
831         localtdata->transinfo->numnotfound = objnotfound;
832
833         /*Set flag to show that common data structure for this individual thread has been written to */
834         //*(tdata->localstatus) |= LM_UPDATED;
835
836         /* Lock and update count */
837         //Thread sleeps until all messages from pariticipants are received by coordinator
838         pthread_mutex_lock(localtdata->tdata->lock);
839         (*(localtdata->tdata->count))++; /* keeps track of no of messages received by the coordinator */
840
841         /* Wake up the threads and invoke decideResponse (once) */
842         if(*(localtdata->tdata->count) == localtdata->tdata->pilecount) {
843                 if (decideResponse(localtdata->tdata) != 0) { 
844                         printf("decideResponse returned error %s,%d\n", __FILE__, __LINE__);
845                         pthread_mutex_unlock(localtdata->tdata->lock);
846                         return NULL;
847                 }
848                 pthread_cond_broadcast(localtdata->tdata->threshold);
849         } else {
850                 pthread_cond_wait(localtdata->tdata->threshold, localtdata->tdata->lock);
851         }
852         pthread_mutex_unlock(localtdata->tdata->lock);
853
854         /*Based on DecideResponse(), Either COMMIT or ABORT the operation*/
855         if(*(localtdata->tdata->replyctrl) == TRANS_ABORT){
856                 if(transAbortProcess(modptr,oidlocked, localtdata->transinfo->numlocked, localtdata->transinfo->nummod, localtdata->tdata->buffer->f.numread) != 0) {
857                         printf("Error in transAbortProcess() %s,%d\n", __FILE__, __LINE__);
858                         return NULL;
859                 }
860         }else if(*(localtdata->tdata->replyctrl) == TRANS_COMMIT){
861                 if(transComProcess(localtdata->transinfo) != 0) {
862                         printf("Error in transComProcess() %s,%d\n", __FILE__, __LINE__);
863                         return NULL;
864                 }
865         }
866
867         /* Free memory */
868         printf("DEBUG -> Freeing...\n");
869         fflush(stdout);
870         if (localtdata->transinfo->objmod != NULL) {
871                 free(localtdata->transinfo->objmod);
872                 localtdata->transinfo->objmod = NULL;
873         }
874         if (localtdata->transinfo->objlocked != NULL) {
875                 free(localtdata->transinfo->objlocked);
876                 localtdata->transinfo->objlocked = NULL;
877         }
878         if (localtdata->transinfo->objnotfound != NULL) {
879                 free(localtdata->transinfo->objnotfound);
880                 localtdata->transinfo->objnotfound = NULL;
881         }
882
883         pthread_exit(NULL);
884 }
885 /* This function completes the ABORT process if the transaction is aborting 
886 */
887 int transAbortProcess(void *modptr, unsigned int *objlocked, int numlocked, int nummod, int numread) {
888         char *ptr;
889         int i;
890         objheader_t *tmp_header;
891         void *header;
892
893         printf("DEBUG -> Recv TRANS_ABORT\n");
894         /* Set all ref counts as 1 and do garbage collection */
895         ptr = modptr;
896         for(i = 0; i< nummod; i++) {
897                 tmp_header = (objheader_t *)ptr;
898                 tmp_header->rcount = 1;
899                 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
900         }
901         /* Unlock objects that was locked due to this transaction */
902         for(i = 0; i< numlocked; i++) {
903                 header = mhashSearch(objlocked[i]);// find the header address
904                 ((objheader_t *)header)->status &= ~(LOCK);
905         }
906
907         /* Send ack to Coordinator */
908         printf("DEBUG-> TRANS_SUCCESSFUL\n");
909
910         /*Free the pointer */
911         ptr = NULL;
912         return 0;
913 }
914
915 /*This function completes the COMMIT process is the transaction is commiting
916 */
917 int transComProcess(trans_commit_data_t *transinfo) {
918         objheader_t *header;
919         int i = 0, offset = 0;
920         char control;
921
922         printf("DEBUG -> Recv TRANS_COMMIT\n");
923         /* Process each modified object saved in the mainobject store */
924         for(i=0; i<transinfo->nummod; i++) {
925                 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
926                         printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
927                 }
928                 /* Change reference count of older address and free space in objstr ?? */
929                 header->rcount = 1; //TODO Not sure what would be the val
930
931                 /* Change ptr address in mhash table */
932                 mhashRemove(transinfo->objmod[i]);
933                 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
934                 offset += sizeof(objheader_t) + classsize[header->type];
935
936                 /* Update object version number */
937                 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
938                 header->version += 1;
939         }
940
941         /* Unlock locked objects */
942         for(i=0; i<transinfo->numlocked; i++) {
943                 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
944                 header->status &= ~(LOCK);
945         }
946
947         //TODO Update location lookup table
948
949         /* Send ack to Coordinator */
950         printf("DEBUG-> TRANS_SUCESSFUL\n");
951         return 0;
952 }
953
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;
961         char *ptr, *tmp;
962         int ntuples, slength;
963         unsigned int *oid;
964         short *endoffsets, *arryfields; 
965
966         /* Check for the case x.y.z and a.b.c are same oids */ 
967         ptr = (char *) node;
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];
977         }
978         /* Check for redundant tuples by comparing oids of each tuple */
979         for(i = 0; i < ntuples; i++) {
980                 if(oid[i] == -1)
981                         continue;
982                 for(j = i+1 ; j < ntuples; j++) {
983                         if(oid[j] == -1)
984                                 continue;
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];
990                                         sindex = j;
991                                 }
992                                 else {
993                                         slength = numoffset[i];
994                                         sindex = i;
995                                 }
996
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*/
1000
1001                                 if(i == 0) {
1002                                         k = 0;
1003                                         index = endoffsets[j -1];
1004                                         for(count = 0; count < slength; count ++) {
1005                                                 if (arryfields[k] != arryfields[index]) { 
1006                                                         break;
1007                                                 }
1008                                                 index++;
1009                                                 k++;
1010                                         }       
1011                                 } else {
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]) {
1017                                                         break;
1018                                                 }
1019                                                 index++;
1020                                                 k++;
1021                                         }
1022                                 }
1023
1024                                 if(slength == count) {
1025                                         oid[sindex] = -1;
1026                                 }
1027                         }
1028                 }
1029         }
1030 }
1031
1032 void checkPreCache(prefetchqelem_t *node, int *numoffset, int counter, int loopcount, unsigned int objoid, int index, int iter, int oidnfound) {
1033         char *ptr, *tmp;
1034         int ntuples, i, k, flag;
1035         unsigned int * oid;
1036         short *endoffsets, *arryfields;
1037         objheader_t *header;
1038
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);
1044
1045         if(oidnfound == 1) {
1046                 if((header = (objheader_t *) prehashSearch(objoid)) == NULL) {
1047                         return;
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) {
1055                                         flag = 0;
1056                                 } else {
1057                                         flag = 1;
1058                                         break;
1059                                 }
1060                         }
1061                 }
1062         } else {
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]);
1067                                 flag = 0;
1068                                 index++;
1069                         } else {
1070                                 flag = 1;
1071                                 break;
1072                         }
1073                 }
1074         }
1075
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*/
1079         oid[iter] = objoid;
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];
1083         }
1084
1085         if(flag == 0) {
1086                 oid[iter] = -1;
1087                 numoffset[iter] = 0;
1088         }
1089 }
1090
1091 /* This function makes machine piles to be added into the machine pile queue for each prefetch call */
1092 prefetchpile_t *makePreGroups(prefetchqelem_t *node, int *numoffset) {
1093         char *ptr, *tmp;
1094         int ntuples, slength, i, machinenum;
1095         int maxoffset;
1096         unsigned int *oid;
1097         short *endoffsets, *arryfields, *offset; 
1098         prefetchpile_t *head = NULL;
1099
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);
1106
1107         /* Check for redundant tuples by comparing oids of each tuple */
1108         for(i = 0; i < ntuples; i++) {
1109                 if(oid[i] == -1)
1110                         continue;
1111                 /* For each tuple make piles */
1112                 if ((machinenum = lhashSearch(oid[i])) == 0) {
1113                         printf("Error: No such Machine %s, %d\n", __FILE__, __LINE__);
1114                         return NULL;
1115                 }
1116                 /* Insert into machine pile */
1117                 offset = &arryfields[endoffsets[i-1]];
1118                 insertPile(machinenum, oid[i], numoffset[i], offset, head);
1119         }
1120
1121         return head;
1122 }
1123
1124
1125 /* This function checks if the oids within the prefetch tuples are available locally.
1126  * If yes then makes the tuple invalid. If no then rearranges oid and offset values in 
1127  * the prefetchqelem_t node to represent a new prefetch tuple */
1128 prefetchpile_t *foundLocal(prefetchqelem_t *node) {
1129         int ntuples,i, j, k, oidnfound = 0, index, flag;
1130         unsigned int *oid;
1131         unsigned int  objoid;
1132         char *ptr, *tmp;
1133         objheader_t *objheader;
1134         short *endoffsets, *arryfields; 
1135         prefetchpile_t *head = NULL;
1136
1137         ptr = (char *) node;
1138         ntuples = *(GET_NTUPLES(ptr));
1139         oid = GET_PTR_OID(ptr);
1140         endoffsets = GET_PTR_EOFF(ptr, ntuples); 
1141         arryfields = GET_PTR_ARRYFLD(ptr, ntuples);
1142         /* Find offset length for each tuple */
1143         int numoffset[ntuples];//Number of offsets for each tuple
1144         numoffset[0] = endoffsets[0];
1145         for(i = 1; i<ntuples; i++) {
1146                 numoffset[i] = endoffsets[i] - endoffsets[i-1];
1147         }
1148         for(i = 0; i < ntuples; i++) { 
1149                 if(oid[i] == -1)
1150                         continue;
1151                 /* If object found locally */
1152                 if((objheader = (objheader_t*) mhashSearch(oid[i])) != NULL) { 
1153                         oidnfound = 0;
1154                         tmp = (char *) objheader;
1155                         /* Find the oid of its offset value */
1156                         if(i == 0) 
1157                                 index = 0;
1158                         else 
1159                                 index = endoffsets[i - 1];
1160                         for(j = 0 ; j < numoffset[i] ; j++) {
1161                                 objoid = *(tmp + sizeof(objheader_t) + arryfields[index]);
1162                                 /*If oid found locally then 
1163                                  *assign the latest oid found as the new oid 
1164                                  *and copy left over offsets into the arrayoffsetfieldarray*/
1165                                 oid[i] = objoid;
1166                                 numoffset[i] = numoffset[i] - (j+1);
1167                                 for(k = 0; k < numoffset[i]; k++)
1168                                         arryfields[endoffsets[j]+ k] = arryfields[endoffsets[j]+k+1];
1169                                 index++;
1170                                 /*New offset oid not found */
1171                                 if((objheader = (objheader_t*) mhashSearch(objoid)) == NULL) {
1172                                         flag = 1;
1173                                         checkPreCache(node, numoffset, j, numoffset[i], objoid, index, i, oidnfound); 
1174                                         break;
1175                                 } else 
1176                                         flag = 0;
1177                         }
1178
1179                         /*If all offset oids are found locally,make the prefetch tuple invalid */
1180                         if(flag == 0) {
1181                                 oid[i] = -1;
1182                                 numoffset[i] = 0;
1183                         }
1184                 } else {
1185                         oidnfound = 1;
1186                         /* Look in Prefetch cache */
1187                         checkPreCache(node, numoffset, 0, numoffset[i], oid[i], 0, i, oidnfound); 
1188                 }
1189
1190         }
1191         /* Make machine groups */
1192         head = makePreGroups(node, numoffset);
1193         return head;
1194 }
1195
1196 /* This function is called by the thread calling transPrefetch */
1197 void *transPrefetch(void *t) {
1198         //int *offstarray = NULL;
1199         prefetchqelem_t *qnode;
1200         prefetchpile_t *pilehead = NULL;
1201
1202         while(1) {
1203                 /* lock mutex of primary prefetch queue */
1204                 pthread_mutex_lock(&pqueue.qlock);
1205                 /* while primary queue is empty, then wait */
1206                 while((pqueue.front == NULL) && (pqueue.rear == NULL)) {
1207                         pthread_cond_wait(&pqueue.qcond, &pqueue.qlock);
1208                 }
1209
1210                 /* dequeue node to create a machine piles and  finally unlock mutex */
1211                 if((qnode = dequeue()) == NULL) {
1212                         printf("Error: No node returned %s, %d\n", __FILE__, __LINE__);
1213                         return NULL;
1214                 }
1215                 pthread_mutex_unlock(&pqueue.qlock);
1216                 /* Reduce redundant prefetch requests */
1217                 checkPrefetchTuples(qnode);
1218                 /* Check if the tuples are found locally, if yes then reduce them further*/ 
1219                 /* and group requests by remote machine ids by calling the makePreGroups() */
1220                 pilehead = foundLocal(qnode);
1221
1222                 /* Lock mutex of pool queue */
1223                 pthread_mutex_lock(&mcqueue.qlock);
1224                 /* Update the pool queue with the new remote machine piles generated per prefetch call */
1225                 mcpileenqueue(pilehead);
1226                 /* Broadcast signal on machine pile queue */
1227                 pthread_cond_broadcast(&mcqueue.qcond);
1228                 /* Unlock mutex of  machine pile queue */
1229                 pthread_mutex_unlock(&mcqueue.qlock);
1230                 /* Deallocate the prefetch queue pile node */
1231                 predealloc(qnode);
1232
1233         }
1234 }
1235
1236 /*The pool of threads work on this function to establish connection with
1237  * remote machines */
1238
1239 void *mcqProcess(void *threadid) {
1240         int tid;
1241         prefetchpile_t *mcpilenode;
1242
1243         tid = (int) threadid;
1244         while(1) {
1245                 /* Lock mutex of mc pile queue */
1246                 pthread_mutex_lock(&mcqueue.qlock);
1247                 /* When mc pile queue is empty, wait */
1248                 while((mcqueue.front == NULL) && (mcqueue.rear == NULL)) {
1249                         pthread_cond_wait(&mcqueue.qcond, &mcqueue.qlock);
1250                 }
1251                 /* Dequeue node to send remote machine connections*/
1252                 if((mcpilenode = mcpiledequeue()) == NULL) {
1253                         printf("Dequeue Error: No node returned %s %d\n", __FILE__, __LINE__);
1254                         return NULL;
1255                 }
1256                 /* Unlock mutex */
1257                 pthread_mutex_unlock(&mcqueue.qlock);
1258
1259                 /*Initiate connection to remote host and send request */ 
1260                 /* Process Request */
1261                 sendPrefetchReq(mcpilenode, tid);
1262                 /* TODO: For each object not found query DHT for new location and retrieve the object */
1263
1264                 /* Deallocate the machine queue pile node */
1265                 mcdealloc(mcpilenode);
1266         }
1267 }
1268
1269 /*This function is called by the thread that processes the 
1270  * prefetch request makes piles to prefetch records and prefetches the oids from remote machines */
1271 int transPrefetchProcess(transrecord_t *record, int *arrayofoffset[], short numoids){
1272         int i, k = 0, rc;
1273         int arraylength[numoids];
1274         unsigned int machinenumber;
1275         objheader_t *tmp, *objheader;
1276         void *objcopy;
1277         int size;
1278         pthread_attr_t attr;
1279
1280         /* Given tuple find length of tuple*/
1281         for(i = 0; i < numoids ; i++) {
1282                 arraylength[i] = arrayLength(arrayofoffset[i]);
1283         }
1284
1285         /* Initialize and set thread attributes 
1286          * Spawn a thread for each prefetch request sent*/
1287         pthread_t thread[numoids];
1288         pthread_attr_init(&attr);
1289         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1290
1291         /* Create  Machine Piles to send prefetch requests use threads*/
1292         for( i = 0 ; i< numoids ; i++) {
1293                 if(arrayofoffset[i][0] == -1) 
1294                         continue;
1295                 else{
1296                         /* For each Pile in the machine send TRANS_PREFETCH */
1297                         //makePiles(arrayofoffset, numoids);
1298                         /* Fill thread data structure */
1299                         //rc = pthread_create(&thread[i] , &attr, sendPrefetchReq, (void *) arrayofoffset[i]);
1300                         if (rc) {
1301                                 perror("Error in pthread create at transPrefetchProcess()\n");
1302                                 return 1;
1303                         }
1304
1305                 }
1306         }
1307
1308         /* Free attribute and wait to join other threads */
1309         for (i = 0 ;i < numoids ; i++) {
1310                 rc = pthread_join(thread[i], NULL);
1311                 if (rc) {
1312                         perror("Error pthread_join() in transPrefetchProcess()\n");
1313                         return 1;
1314                 }
1315         }
1316         pthread_attr_destroy(&attr);
1317
1318         return 0;
1319
1320 }
1321
1322 void sendPrefetchReq(prefetchpile_t *mcpilenode, int threadid) {
1323         int sd, i, offset, off, len, endpair, numoffsets, count = 0;
1324         struct sockaddr_in serv_addr;
1325         struct hostent *server;
1326         char machineip[16], control;
1327         objpile_t *tmp;
1328
1329
1330         /* Send Trans Prefetch Request */
1331         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1332                 perror("Error in socket for TRANS_REQUEST\n");
1333                 return;
1334         }
1335         bzero((char*) &serv_addr, sizeof(serv_addr));
1336         serv_addr.sin_family = AF_INET;
1337         serv_addr.sin_port = htons(LISTEN_PORT);
1338         //serv_addr.sin_addr.s_addr = inet_addr(MACHINE_IP);
1339         midtoIP(mcpilenode->mid ,machineip);
1340         machineip[15] = '\0';
1341         serv_addr.sin_addr.s_addr = inet_addr(machineip);
1342
1343         /* Open Connection */
1344         if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
1345                 perror("Error in connect for TRANS_REQUEST\n");
1346                 return;
1347         }
1348
1349         /* Send TRANS_PREFETCH control message */
1350         control = TRANS_PREFETCH;
1351         if(send(sd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
1352                 perror("Error in sending prefetch control\n");
1353                 return;
1354         }
1355
1356         /* Send Oids and offsets in pairs */
1357         tmp = mcpilenode->objpiles;
1358         while(tmp != NULL) {
1359                 off = offset = 0;
1360                 count++;  // Keeps track of the number of oid and offset tuples sent per remote machine
1361                 len = sizeof(int) + sizeof(unsigned int) + ((tmp->numoffset) * sizeof(short));
1362                 char oidnoffset[len];
1363                 memcpy(oidnoffset, &len, sizeof(int));
1364                 off = sizeof(int);
1365                 memcpy(oidnoffset + off, &tmp->oid, sizeof(unsigned int));
1366                 off += sizeof(unsigned int);
1367                 for(i = 0; i < numoffsets; i++) {
1368                         offset = off +  (i * sizeof(short));
1369                         memcpy(oidnoffset + offset, tmp->offset, sizeof(short));
1370                 }
1371                 if (send(sd, &oidnoffset, sizeof(oidnoffset),MSG_NOSIGNAL) < sizeof(oidnoffset)) {
1372                         perror("Error sending fixed bytes for thread\n");
1373                         return;
1374                 }
1375                 tmp = tmp->next;
1376         }
1377
1378         /* Send a special char -1 to represent the end of sending oids + offset pair to remote machine */
1379         endpair = -1;
1380         if (send(sd, &endpair, sizeof(int), MSG_NOSIGNAL) < sizeof(int)) {
1381                 perror("Error sending fixed bytes for thread\n");
1382                 return;
1383         }
1384
1385         /* Get Response from the remote machine */
1386         getPrefetchResponse(count,sd);
1387         close(sd);
1388 }
1389
1390 void getPrefetchResponse(int count, int sd) {
1391         int i = 0, val, n, N, sum, index, objsize;
1392         unsigned int bufsize,oid;
1393         char buffer[RECEIVE_BUFFER_SIZE], control;
1394         char *ptr;
1395         void *modptr;
1396
1397         /* Read  prefetch response from the Remote machine */
1398         if((val = read(sd, &control, sizeof(char))) <= 0) {
1399                 perror("No control response for Prefetch request sent\n");
1400                 return;
1401         }
1402         if(control == TRANS_PREFETCH_RESPONSE) {
1403                 /*For each oid and offset tuple sent as prefetch request to remote machine*/
1404                 while(i < count) {
1405                         /* Clear contents of buffer */
1406                         memset(buffer, 0, RECEIVE_BUFFER_SIZE);
1407                         sum = 0;
1408                         index = 0;
1409                         /* Read the size of buffer to be received */
1410                         if((N = read(sd, buffer, sizeof(unsigned int))) <= 0) {
1411                                 perror("Size of buffer not recv\n");
1412                                 return;
1413                         }
1414                         memcpy(&bufsize, buffer, sizeof(unsigned int));
1415                         ptr = buffer + sizeof(unsigned int);
1416                         /* Keep receiving the buffer containing oid info */ 
1417                         do {
1418                                 n = recv((int)sd, (void *)ptr+sum, bufsize-sum, 0);
1419                                 sum +=n;
1420                         } while(sum < bufsize && n != 0);
1421                         /* Decode the contents of the buffer */
1422                         index = sizeof(unsigned int);
1423                         while(index < (bufsize - sizeof(unsigned int))) {
1424                                 if(buffer[index] == OBJECT_FOUND) {
1425                                         /* Increment it to get the object */
1426                                         index += sizeof(char);
1427                                         memcpy(&oid, buffer + index, sizeof(unsigned int));
1428                                         index += sizeof(unsigned int);
1429                                         /* Lock the Prefetch Cache look up table*/
1430                                         pthread_mutex_lock(&pflookup.lock);
1431                                         /* For each object found add to Prefetch Cache */
1432                                         memcpy(&objsize, buffer + index, sizeof(int));
1433                                         if ((modptr = objstrAlloc(prefetchcache, objsize)) == NULL) {
1434                                                 printf("objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__);
1435                                                 return;
1436                                         }
1437                                         memcpy(modptr, buffer+index, objsize);
1438                                         index += sizeof(int);
1439                                         /* Add pointer and oid to hash table */
1440                                         //TODO Do we need a version comparison here??
1441                                         prehashInsert(oid, modptr);
1442                                         /* Broadcast signal on prefetch cache condition variable */ 
1443                                         pthread_cond_broadcast(&pflookup.cond);
1444                                         /* Unlock the Prefetch Cache look up table*/
1445                                         pthread_mutex_unlock(&pflookup.lock);
1446                                 } else if(buffer[index] == OBJECT_NOT_FOUND) {
1447                                         /* Increment it to get the object */
1448                                         // TODO If object not found, local machine takes inventory
1449                                         index += sizeof(char);
1450                                         memcpy(&oid, buffer + index, sizeof(unsigned int));
1451                                         index += sizeof(unsigned int);
1452                                 } else 
1453                                         printf("Error in decoding the index value %s, %d\n",__FILE__, __LINE__);
1454                         }
1455
1456                         i++;
1457                 }
1458         } else
1459                 printf("Error in receving response for prefetch request %s, %d\n",__FILE__, __LINE__);
1460 }