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 */
18 #define LISTEN_PORT 2156
19 #define BACKLOG 10 //max pending connections
20 #define RECEIVE_BUFFER_SIZE 2048
21 #define PRE_BUF_SIZE 2048
23 extern int classsize[];
25 objstr_t *mainobjstore;
26 pthread_mutex_t mainobjstore_mutex;
28 /* This function initializes the main objects store and creates the
29 * global machine and location lookup table */
33 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
34 pthread_mutex_init(&mainobjstore_mutex, NULL);
35 if (mhashCreate(HASH_SIZE, LOADFACTOR))
38 if (lhashCreate(HASH_SIZE, LOADFACTOR))
44 /* This function starts the thread to listen on a socket
45 * for tranaction calls */
48 int listenfd, acceptfd;
49 struct sockaddr_in my_addr;
50 struct sockaddr_in client_addr;
51 socklen_t addrlength = sizeof(struct sockaddr);
52 pthread_t thread_dstm_accept;
56 listenfd = socket(AF_INET, SOCK_STREAM, 0);
63 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
68 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
74 my_addr.sin_family = AF_INET;
75 my_addr.sin_port = htons(LISTEN_PORT);
76 my_addr.sin_addr.s_addr = INADDR_ANY;
77 memset(&(my_addr.sin_zero), '\0', 8);
79 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
85 if (listen(listenfd, BACKLOG) == -1)
91 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
94 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
95 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
99 /* This function accepts a new connection request, decodes the control message in the connection
100 * and accordingly calls other functions to process new requests */
101 void *dstmAccept(void *acceptfd)
103 int numbytes,i, val, retval;
105 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
109 trans_commit_data_t transinfo;
110 unsigned short objType;
112 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
114 /* Receive control messages from other machines */
115 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
117 return; // Testing connection
119 perror("Error in receiving control from coordinator\n");
125 /* Read oid requested and search if available */
126 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
127 perror("Error receiving object from cooridnator\n");
130 if((srcObj = mhashSearch(oid)) == NULL) {
131 printf("Object not found in Main Object Store %s %d\n", __FILE__, __LINE__);
133 h = (objheader_t *) srcObj;
135 size += sizeof(objheader_t);
138 ctrl = OBJECT_NOT_FOUND;
139 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
140 perror("Error sending control msg to coordinator\n");
145 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
146 *((int *)&msg[1])=size;
147 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
148 perror("Error sending size of object to coordinator\n");
151 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
152 perror("Error in sending object\n");
158 case READ_MULT_REQUEST:
159 printf("DEBUG-> READ_MULT_REQUEST\n");
163 printf("DEBUG -> MOVE_REQUEST\n");
166 case MOVE_MULT_REQUEST:
167 printf("DEBUG -> MOVE_MULT_REQUEST\n");
171 /* Read transaction request */
172 printf("DEBUG -> Recv TRANS_REQUEST\n");
173 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
174 printf("Error in readClientReq\n");
179 printf("DEBUG -> Recv TRANS_PREFETCH\n");
180 if((val = prefetchReq((int)acceptfd)) != 0) {
181 printf("Error in readClientReq\n");
185 case START_REMOTE_THREAD:
186 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
188 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
189 else if (retval != sizeof(unsigned int))
190 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
193 { //TODO: execute run method on this global thread object
194 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
195 objType = getObjType(oid);
196 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
197 startDSMthread(oid, objType);
204 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
207 /* Close connection */
208 if (close((int)acceptfd) == -1)
214 /* This function reads the information available in a transaction request
215 * and makes a function call to process the request */
216 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
219 unsigned int *oidmod, oid;
221 objheader_t *headaddr;
222 int sum = 0, i, N, n, val;
224 /* Read fixed_data_t data structure */
225 N = sizeof(fixed) - 1;
226 ptr = (char *)&fixed;;
227 fixed.control = TRANS_REQUEST;
229 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
231 } while(sum < N && n != 0);
233 /* Read list of mids */
234 int mcount = fixed.mcount;
235 N = mcount * sizeof(unsigned int);
236 unsigned int listmid[mcount];
237 ptr = (char *) listmid;
240 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
242 } while(sum < N && n != 0);
244 /* Read oid and version tuples for those objects that are not modified in the transaction */
245 int numread = fixed.numread;
246 N = numread * (sizeof(unsigned int) + sizeof(short));
248 if(numread != 0) { //If pile contains more than one object to be read,
249 // keep reading all objects
252 n = recv((int)acceptfd, (void *) objread, N, 0);
254 } while(sum < N && n != 0);
257 /* Read modified objects */
258 if(fixed.nummod != 0) { // If pile contains more than one modified object,
259 // allocate new object store and recv all modified objects
260 // TODO deallocate this space
261 pthread_mutex_lock(&mainobjstore_mutex);
262 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
263 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
264 pthread_mutex_unlock(&mainobjstore_mutex);
267 pthread_mutex_unlock(&mainobjstore_mutex);
269 do { // Recv the objs that are modified by the Coordinator
270 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
272 } while (sum < fixed.sum_bytes && n != 0);
275 /* Create an array of oids for modified objects */
276 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
277 ptr = (char *) modptr;
278 for(i = 0 ; i < fixed.nummod; i++) {
280 headaddr = (objheader_t *) ptr;
283 GETSIZE(tmpsize, headaddr);
284 ptr += sizeof(objheader_t) + tmpsize;
287 /*Process the information read */
288 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
289 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
307 /* This function processes the Coordinator's transaction request using "handleTransReq"
308 * function and sends a reply to the co-ordinator.
309 * Following this it also receives a new control message from the co-ordinator and processes this message*/
310 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
311 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
312 char *ptr, control, sendctrl;
313 objheader_t *tmp_header;
315 int i = 0, val, retval;
317 /* Send reply to the Coordinator */
318 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
319 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
323 /* Read new control message from Coordiator */
324 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
325 perror("Error in receiving control message\n");
329 /* Process the new control message */
332 /* Set all ref counts as 1 and do garbage collection */
334 for(i = 0; i< fixed->nummod; i++) {
336 tmp_header = (objheader_t *)ptr;
337 tmp_header->rcount = 0;
338 GETSIZE(tmpsize, tmp_header);
339 ptr += sizeof(objheader_t) + tmpsize;
341 /* Unlock objects that was locked due to this transaction */
342 for(i = 0; i< transinfo->numlocked; i++) {
343 header = mhashSearch(transinfo->objlocked[i]);// find the header address
344 STATUS(((objheader_t *)header)) &= ~(LOCK);
347 /* Send ack to Coordinator */
348 printf("DEBUG -> Recv TRANS_ABORT\n");
349 sendctrl = TRANS_SUCESSFUL;
350 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
351 perror("Error sending ACK to coordinator\n");
352 if (transinfo->objlocked != NULL) {
353 free(transinfo->objlocked);
354 transinfo->objlocked = NULL;
356 if (transinfo->objnotfound != NULL) {
357 free(transinfo->objnotfound);
358 transinfo->objnotfound = NULL;
367 /* Invoke the transCommit process() */
368 printf("DEBUG -> Recv TRANS_COMMIT \n");
369 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
370 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
372 printf("DEBUG -> Freeing...\n");
374 if (transinfo->objlocked != NULL) {
375 free(transinfo->objlocked);
376 transinfo->objlocked = NULL;
378 if (transinfo->objnotfound != NULL) {
379 free(transinfo->objnotfound);
380 transinfo->objnotfound = NULL;
386 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
387 //TODO expect another transrequest from client
388 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
391 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
392 //TODO Use fixed.trans_id TID since Client may have died
397 printf("DEBUG -> Freeing...\n");
400 if (transinfo->objlocked != NULL) {
401 free(transinfo->objlocked);
402 transinfo->objlocked = NULL;
404 if (transinfo->objnotfound != NULL) {
405 free(transinfo->objnotfound);
406 transinfo->objnotfound = NULL;
412 /* This function increments counters while running a voting decision on all objects involved
413 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
414 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
417 char control = 0, *ptr;
419 unsigned int *oidnotfound, *oidlocked, *oidmod;
421 objheader_t *headptr;
423 /* Counters and arrays to formulate decision on control message to be sent */
424 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
425 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
426 int objnotfound = 0, objlocked = 0;
427 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
429 /* modptr points to the beginning of the object store
430 * created at the Pariticipant.
431 * Object store holds the modified objects involved in the transaction request */
432 ptr = (char *) modptr;
434 /* Process each oid in the machine pile/ group per thread */
435 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
436 if (i < fixed->numread) {//Objs only read and not modified
437 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
439 oid = *((unsigned int *)(objread + incr));
440 incr += sizeof(unsigned int);
441 version = *((short *)(objread + incr));
442 } else {//Objs modified
444 headptr = (objheader_t *) ptr;
446 version = headptr->version;
447 GETSIZE(tmpsize, headptr);
448 ptr += sizeof(objheader_t) + tmpsize;
451 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
453 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
454 /* Save the oids not found and number of oids not found for later use */
455 //oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
456 oidnotfound[objnotfound] = oid;
458 } else { /* If Obj found in machine (i.e. has not moved) */
459 /* Check if Obj is locked by any previous transaction */
460 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
461 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
463 } else {/* If versions don't match ...HARD ABORT */
465 /* Send TRANS_DISAGREE to Coordinator */
466 control = TRANS_DISAGREE;
467 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
468 perror("Error in sending control to the Coordinator\n");
471 printf("DEBUG -> Sending TRANS_DISAGREE\n");
474 } else {/* If Obj is not locked then lock object */
475 STATUS(((objheader_t *)mobj)) |= LOCK;
477 /*TESTING Add random wait to make transactions run for a long time such that
478 * we can test for soft abort case */
482 /* Save all object oids that are locked on this machine during this transaction request call */
483 oidlocked[objlocked] = OID(((objheader_t *)mobj));
485 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
487 } else { /* If versions don't match ...HARD ABORT */
489 control = TRANS_DISAGREE;
490 /* Send TRANS_DISAGREE to Coordinator */
491 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
492 perror("Error in sending control to the Coordinator\n");
495 printf("DEBUG -> Sending TRANS_DISAGREE\n");
502 /* Decide what control message to send to Coordinator */
503 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
504 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
505 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
512 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
513 * to send to Coordinator based on the votes of oids involved in the transaction */
514 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
515 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
516 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
519 /* Condition to send TRANS_AGREE */
520 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
521 control = TRANS_AGREE;
522 /* Send control message */
523 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
524 perror("Error in sending control to Coordinator\n");
527 printf("DEBUG -> Sending TRANS_AGREE\n");
529 /* Condition to send TRANS_SOFT_ABORT */
530 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
531 control = TRANS_SOFT_ABORT;
532 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
533 *((int*)&msg[1])= *(objnotfound);
535 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
536 /* Send control message */
537 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
538 perror("Error in sending no of objects that are not found\n");
541 /* Send number of oids not found and the missing oids if objects are missing in the machine */
542 if(*(objnotfound) != 0) {
543 int size = sizeof(unsigned int)* *(objnotfound);
544 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
545 perror("Error in sending objects that are not found\n");
551 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
552 * if Participant receives a TRANS_COMMIT */
553 transinfo->objlocked = oidlocked;
554 transinfo->objnotfound = oidnotfound;
555 transinfo->modptr = modptr;
556 transinfo->numlocked = *(objlocked);
557 transinfo->numnotfound = *(objnotfound);
562 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
563 * addresses in lookup table and also changes version number
564 * Sends an ACK back to Coordinator */
565 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
567 int i = 0, offset = 0;
570 /* Process each modified object saved in the mainobject store */
571 for(i = 0; i < nummod; i++) {
572 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
573 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
576 /* Change reference count of older address and free space in objstr ?? */
579 /* Change ptr address in mhash table */
580 mhashRemove(oidmod[i]);
581 mhashInsert(oidmod[i], (((char *)modptr) + offset));
584 GETSIZE(tmpsize,header);
585 offset += sizeof(objheader_t) + tmpsize;
587 /* Update object version number */
588 header = (objheader_t *) mhashSearch(oidmod[i]);
589 header->version += 1;
592 /* Unlock locked objects */
593 for(i = 0; i < numlocked; i++) {
594 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
595 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
598 STATUS(header) &= ~(LOCK);
600 //TODO Update location lookup table
602 /* Send ack to coordinator */
603 control = TRANS_SUCESSFUL;
604 printf("DEBUG-> TRANS_SUCESSFUL\n");
605 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
606 perror("Error sending ACK to coordinator\n");
612 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
613 * Looks for the objects to be prefetched in the main object store.
614 * If objects are not found then record those and if objects are found
615 * then use offset values to prefetch references to other objects */
617 int prefetchReq(int acceptfd) {
618 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
619 unsigned int oid, index = 0;
620 char *ptr, buffer[PRE_BUF_SIZE];
624 objheader_t * header;
627 /* Repeatedly recv the oid and offset pairs sent for prefetch */
628 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
633 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
634 // first 4 bytes are saved to send the
635 // size of the buffer (that is computed at the end of the loop)
638 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
639 sizeof(unsigned int) - bytesRecvd, 0);
640 } while (bytesRecvd < sizeof(unsigned int));
641 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
642 N = numoffset * sizeof(short);
643 short offset[numoffset];
644 ptr = (char *)&offset;
645 /* Recv the offset values per oid */
647 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
649 } while(sum < N && n != 0);
651 /* Process each oid */
652 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
653 /* Save the oids not found in buffer for later use */
654 *(buffer + index) = OBJECT_NOT_FOUND;
655 index += sizeof(char);
656 memcpy(buffer+index, &oid, sizeof(unsigned int));
657 index += sizeof(unsigned int);
658 } else { /* If Obj found in machine (i.e. has not moved) */
659 /* send the oid, it's size, it's header and data */
661 GETSIZE(size, header);
662 size += sizeof(objheader_t);
663 *(buffer + index) = OBJECT_FOUND;
664 index += sizeof(char);
665 memcpy(buffer+index, &oid, sizeof(unsigned int));
666 index += sizeof(unsigned int);
667 memcpy(buffer+index, &size, sizeof(int));
668 index += sizeof(int);
669 memcpy(buffer + index, header, size);
671 /* Calculate the oid corresponding to the offset value */
672 for(i = 0 ; i< numoffset ; i++) {
673 objoid = *((int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
674 if((header = mhashSearch(objoid)) == NULL) {
675 /* Obj not found, send oid */
676 *(buffer + index) = OBJECT_NOT_FOUND;
677 index += sizeof(char);
678 memcpy(buffer+index, &oid, sizeof(unsigned int));
679 index += sizeof(unsigned int);
681 } else {/* Obj Found */
682 /* send the oid, it's size, it's header and data */
683 GETSIZE(size, header);
684 size+=sizeof(objheader_t);
685 *(buffer + index) = OBJECT_FOUND;
686 index += sizeof(char);
687 memcpy(buffer+index, &oid, sizeof(unsigned int));
688 index += sizeof(unsigned int);
689 memcpy(buffer+index, &size, sizeof(int));
690 index += sizeof(int);
691 memcpy(buffer + index, header, size);
697 /* Check for overflow in the buffer */
698 if (index >= PRE_BUF_SIZE) {
699 printf("Char buffer is overflowing\n");
702 /* Send Prefetch response control message only once*/
704 control = TRANS_PREFETCH_RESPONSE;
705 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
706 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
711 /* Add the buffer size into buffer as a parameter */
712 *((unsigned int *)buffer)=index;
713 /* Send the entire buffer with its size and oids found and not found */
714 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
715 perror("Error sending oids found\n");