1 /* Coordinator => Machine that initiates the transaction request call for commiting a transaction
2 * Participant => Machines that host the objects involved in a transaction commit */
14 #define LISTEN_PORT 2156
15 #define BACKLOG 10 //max pending connections
16 #define RECEIVE_BUFFER_SIZE 2048
17 #define PRE_BUF_SIZE 2048
19 extern int classsize[];
21 objstr_t *mainobjstore;
25 /* Initialize main object store */
26 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
27 /* Create machine lookup table and location lookup table */
28 if (mhashCreate(HASH_SIZE, LOADFACTOR))
31 if (lhashCreate(HASH_SIZE, LOADFACTOR))
39 int listenfd, acceptfd;
40 struct sockaddr_in my_addr;
41 struct sockaddr_in client_addr;
42 socklen_t addrlength = sizeof(struct sockaddr);
43 pthread_t thread_dstm_accept;
47 listenfd = socket(AF_INET, SOCK_STREAM, 0);
54 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
59 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
65 my_addr.sin_family = AF_INET;
66 my_addr.sin_port = htons(LISTEN_PORT);
67 my_addr.sin_addr.s_addr = INADDR_ANY;
68 memset(&(my_addr.sin_zero), '\0', 8);
70 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
76 if (listen(listenfd, BACKLOG) == -1)
82 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
85 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
86 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
90 /* This function accepts a new connection request, decodes the control message in the connection
91 * and accordingly calls other functions to process new requests */
92 void *dstmAccept(void *acceptfd)
94 int numbytes,i, val, retval;
96 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
100 trans_commit_data_t transinfo;
102 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
104 printf("Recieved connection: fd = %d\n", (int)acceptfd);
105 /* Receive control messages from other machines */
106 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
108 return; // Testing connection
110 perror("Error in receiving control from coordinator\n");
116 /* Read oid requested and search if available */
117 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
118 perror("Error receiving object from cooridnator\n");
121 srcObj = mhashSearch(oid);
122 h = (objheader_t *) srcObj;
123 size = sizeof(objheader_t) + sizeof(classsize[h->type]);
125 ctrl = OBJECT_NOT_FOUND;
126 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
127 perror("Error sending control msg to coordinator\n");
132 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
133 *((int *)&msg[1])=size;
134 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
135 perror("Error sending size of object to coordinator\n");
138 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
139 perror("Error in sending object\n");
145 case READ_MULT_REQUEST:
146 printf("DEBUG-> READ_MULT_REQUEST\n");
150 printf("DEBUG -> MOVE_REQUEST\n");
153 case MOVE_MULT_REQUEST:
154 printf("DEBUG -> MOVE_MULT_REQUEST\n");
158 /* Read transaction request */
159 printf("DEBUG -> Recv TRANS_REQUEST\n");
160 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
161 printf("Error in readClientReq\n");
166 printf("DEBUG -> Recv TRANS_PREFETCH\n");
167 if((val = prefetchReq((int)acceptfd)) != 0) {
168 printf("Error in readClientReq\n");
174 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
177 /* Close connection */
178 if (close((int)acceptfd) == -1)
181 printf("Closed connection: fd = %d\n", (int)acceptfd);
186 /* This function reads the information available in a transaction request
187 * and makes a function call to process the request */
188 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
192 int sum = 0, i, N, n, val;
194 /* Read fixed_data_t data structure */
195 N = sizeof(fixed) - 1;
196 ptr = (char *)&fixed;;
197 fixed.control = TRANS_REQUEST;
199 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
201 } while(sum < N && n != 0);
203 /* Read list of mids */
204 int mcount = fixed.mcount;
205 N = mcount * sizeof(unsigned int);
206 unsigned int listmid[mcount];
207 ptr = (char *) listmid;
210 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
212 } while(sum < N && n != 0);
214 /* Read oid and version tuples for those objects that are not modified in the transaction */
215 int numread = fixed.numread;
216 N = numread * (sizeof(unsigned int) + sizeof(short));
218 if(numread != 0) { //If pile contains more than one object to be read,
219 // keep reading all objects
222 n = recv((int)acceptfd, (void *) objread, N, 0);
224 } while(sum < N && n != 0);
227 /* Read modified objects */
228 if(fixed.nummod != 0) { // If pile contains more than one modified object,
229 // allocate new object store and recv all modified objects
230 // TODO deallocate this space
231 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
232 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
236 do { // Recv the objs that are modified by the Coordinator
237 n = recv((int)acceptfd, modptr+sum, fixed.sum_bytes-sum, 0);
239 } while (sum < fixed.sum_bytes && n != 0);
242 /*Process the information read */
243 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, acceptfd)) != 0) {
244 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
251 /* This function processes the Coordinator's transaction request using "handleTransReq"
252 * function and sends a reply to the co-ordinator.
253 * Following this it also receives a new control message from the co-ordinator and processes this message*/
254 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
255 unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
256 char *ptr, control, sendctrl;
257 objheader_t *tmp_header;
259 int i = 0, val, retval;
261 /* Send reply to the Coordinator */
262 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
263 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
266 /* Read new control message from Coordiator */
267 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
268 perror("Error in receiving control message\n");
272 /* Process the new control message */
275 /* Set all ref counts as 1 and do garbage collection */
277 for(i = 0; i< fixed->nummod; i++) {
278 tmp_header = (objheader_t *)ptr;
279 tmp_header->rcount = 1;
280 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
282 /* Unlock objects that was locked due to this transaction */
283 for(i = 0; i< transinfo->numlocked; i++) {
284 header = mhashSearch(transinfo->objlocked[i]);// find the header address
285 ((objheader_t *)header)->status &= ~(LOCK);
288 /* Send ack to Coordinator */
289 printf("DEBUG -> Recv TRANS_ABORT\n");
290 sendctrl = TRANS_SUCESSFUL;
291 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
292 perror("Error sending ACK to coordinator\n");
300 /* Invoke the transCommit process() */
301 printf("DEBUG -> Recv TRANS_COMMIT \n");
302 if((val = transCommitProcess(transinfo, (int)acceptfd)) != 0) {
303 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
308 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
309 //TODO expect another transrequest from client
310 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
313 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
314 //TODO Use fixed.trans_id TID since Client may have died
318 printf("DEBUG -> Freeing...\n");
320 if (transinfo->objmod != NULL) {
321 free(transinfo->objmod);
322 transinfo->objmod = NULL;
324 if (transinfo->objlocked != NULL) {
325 free(transinfo->objlocked);
326 transinfo->objlocked = NULL;
328 if (transinfo->objnotfound != NULL) {
329 free(transinfo->objnotfound);
330 transinfo->objnotfound = NULL;
335 /* This function increments counters while running a voting decision on all objects involved
336 * in TRANS_REQUEST */
338 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
341 char control = 0, *ptr;
343 unsigned int *oidnotfound, *oidlocked, *oidmod;
345 objheader_t *headptr;
347 /* Counters and arrays to formulate decision on control message to be sent */
348 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
349 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
350 oidmod = (unsigned int *) calloc(fixed->nummod, sizeof(unsigned int));
351 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
352 int objmodnotfound = 0, nummodfound = 0;
354 /* modptr points to the beginning of the object store
355 * created at the Pariticipant.
356 * Object store holds the modified objects involved in the transaction request */
359 /* Process each oid in the machine pile/ group per thread */
360 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
361 if (i < fixed->numread) {//Objs only read and not modified
362 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
364 oid = *((unsigned int *)(objread + incr));
365 incr += sizeof(unsigned int);
366 version = *((short *)(objread + incr));
367 } else {//Objs modified
368 headptr = (objheader_t *) ptr;
370 oidmod[objmod] = oid;//Array containing modified oids
372 version = headptr->version;
373 ptr += sizeof(objheader_t) + classsize[headptr->type];
376 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
378 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
379 /* Save the oids not found and number of oids not found for later use */
380 //oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
381 oidnotfound[objnotfound] = oid;
383 } else { /* If Obj found in machine (i.e. has not moved) */
384 /* Check if Obj is locked by any previous transaction */
385 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
386 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
388 } else {/* If versions don't match ...HARD ABORT */
390 /* Send TRANS_DISAGREE to Coordinator */
391 control = TRANS_DISAGREE;
392 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
393 perror("Error in sending control to the Coordinator\n");
396 printf("DEBUG -> Sending TRANS_DISAGREE\n");
399 } else {/* If Obj is not locked then lock object */
400 ((objheader_t *)mobj)->status |= LOCK;
402 /*TESTING Add random wait to make transactions run for a long time such that
403 * we can test for soft abort case */
407 /* Save all object oids that are locked on this machine during this transaction request call */
408 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
410 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
412 } else { /* If versions don't match ...HARD ABORT */
414 control = TRANS_DISAGREE;
415 /* Send TRANS_DISAGREE to Coordinator */
416 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
417 perror("Error in sending control to the Coordinator\n");
420 printf("DEBUG -> Sending TRANS_DISAGREE\n");
427 /* Decide what control message to send to Coordinator */
428 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
429 modptr, oidnotfound, oidlocked, oidmod, acceptfd)) == 0) {
430 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
437 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
438 * to send to Coordinator based on the votes of oids involved in the transaction */
439 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
440 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
441 unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidmod,
445 /* Condition to send TRANS_AGREE */
446 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
447 control = TRANS_AGREE;
448 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
449 perror("Error in sending control to Coordinator\n");
452 printf("DEBUG -> Sending TRANS_AGREE\n");
454 /* Condition to send TRANS_SOFT_ABORT */
455 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
456 control = TRANS_SOFT_ABORT;
457 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
458 *((int*)&msg[1])= *(objnotfound);
460 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
461 /* Send control message */
462 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
463 perror("Error in sending no of objects that are not found\n");
466 /* Send number of oids not found and the missing oids if objects are missing in the machine */
467 if(*(objnotfound) != 0) {
468 int size = sizeof(unsigned int)* *(objnotfound);
469 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
470 perror("Error in sending objects that are not found\n");
476 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
477 * if Participant receives a TRANS_COMMIT */
478 transinfo->objmod = oidmod;
479 transinfo->objlocked = oidlocked;
480 transinfo->objnotfound = oidnotfound;
481 transinfo->modptr = modptr;
482 transinfo->nummod = fixed->nummod;
483 transinfo->numlocked = *(objlocked);
484 transinfo->numnotfound = *(objnotfound);
489 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
490 * addresses in lookup table and also changes version number
491 * Sends an ACK back to Coordinator */
492 int transCommitProcess(trans_commit_data_t *transinfo, int acceptfd) {
494 int i = 0, offset = 0;
496 /* Process each modified object saved in the mainobject store */
497 for(i=0; i<transinfo->nummod; i++) {
498 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
499 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
501 /* Change reference count of older address and free space in objstr ?? */
502 header->rcount = 1; //Not sure what would be the val
504 /* Change ptr address in mhash table */
505 printf("DEBUG -> removing object oid = %d\n", transinfo->objmod[i]);
506 mhashRemove(transinfo->objmod[i]);
507 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
508 offset += sizeof(objheader_t) + classsize[header->type];
510 /* Update object version number */
511 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
512 header->version += 1;
514 /* Unlock locked objects */
515 for(i=0; i<transinfo->numlocked; i++) {
516 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
517 header->status &= ~(LOCK);
520 //TODO Update location lookup table
522 /* Send ack to coordinator */
523 control = TRANS_SUCESSFUL;
524 printf("DEBUG-> TRANS_SUCESSFUL\n");
525 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
526 perror("Error sending ACK to coordinator\n");
532 int prefetchReq(int acceptfd) {
533 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
534 unsigned int oid, index = 0;
535 char *ptr, buffer[PRE_BUF_SIZE];
538 char *header, control;
541 /* Repeatedly recv the oid and offset pairs sent for prefetch */
542 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
547 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
548 // first 4 bytes are saved to send the
549 // size of the buffer (that is computed at the end of the loop)
550 oid = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
551 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
552 N = numoffset * sizeof(short);
553 short offset[numoffset];
554 ptr = (char *)&offset;
555 /* Recv the offset values per oid */
557 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
559 } while(sum < N && n != 0);
561 /* Process each oid */
562 /* Check if object is still present in the machine since the beginning of TRANS_PREFETCH */
563 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
564 /* Save the oids not found in buffer for later use */
565 *(buffer + index) = OBJECT_NOT_FOUND;
566 index += sizeof(char);
567 memcpy(buffer+index, &oid, sizeof(unsigned int));
568 index += sizeof(unsigned int);
569 } else { /* If Obj found in machine (i.e. has not moved) */
570 /* send the oid, it's size, it's header and data */
571 header = (char *) mobj;
572 head = (objheader_t *) header;
573 size = sizeof(objheader_t) + sizeof(classsize[head->type]);
574 *(buffer + index) = OBJECT_FOUND;
575 index += sizeof(char);
576 memcpy(buffer+index, &oid, sizeof(unsigned int));
577 index += sizeof(unsigned int);
578 memcpy(buffer+index, &size, sizeof(int));
579 index += sizeof(int);
580 memcpy(buffer + index, header, size);
582 /* Calculate the oid corresponding to the offset value */
583 for(i = 0 ; i< numoffset ; i++) {
584 objoid = *((int *)(header + sizeof(objheader_t) + offset[i]));
585 if((header = (char *) mhashSearch(objoid)) == NULL) {
586 /* Obj not found, send oid */
587 *(buffer + index) = OBJECT_NOT_FOUND;
588 index += sizeof(char);
589 memcpy(buffer+index, &oid, sizeof(unsigned int));
590 index += sizeof(unsigned int);
592 } else {/* Obj Found */
593 /* send the oid, it's size, it's header and data */
594 head = (objheader_t *) header;
595 size = sizeof(objheader_t) + sizeof(classsize[head->type]);
596 *(buffer + index) = OBJECT_FOUND;
597 index += sizeof(char);
598 memcpy(buffer+index, &oid, sizeof(unsigned int));
599 index += sizeof(unsigned int);
600 memcpy(buffer+index, &size, sizeof(int));
601 index += sizeof(int);
602 memcpy(buffer + index, header, size);
608 /* Check for overflow in the buffer */
609 if (index >= PRE_BUF_SIZE) {
610 printf("Char buffer is overflowing\n");
613 /* Send Prefetch response control message only once*/
615 control = TRANS_PREFETCH_RESPONSE;
616 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
617 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
622 /* Send the buffer size */
623 memcpy(buffer, &index, sizeof(unsigned int));
624 /* Send the entire buffer with its size and oids found and not found */
625 if(send((int)acceptfd, &buffer, sizeof(index - 1), MSG_NOSIGNAL) < sizeof(index -1)) {
626 perror("Error sending oids found\n");