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