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