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);
98 /* This function accepts a new connection request, decodes the control message in the connection
99 * and accordingly calls other functions to process new requests */
100 void *dstmAccept(void *acceptfd)
102 int numbytes,i, val, retval;
104 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
108 trans_commit_data_t transinfo;
109 unsigned short objType;
111 transinfo.objlocked = NULL;
112 transinfo.objnotfound = NULL;
113 transinfo.modptr = NULL;
114 transinfo.numlocked = 0;
115 transinfo.numnotfound = 0;
117 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
119 /* Receive control messages from other machines */
120 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
122 pthread_exit(NULL); // Testing connection
124 perror("Error in receiving control from coordinator\n");
130 /* Read oid requested and search if available */
131 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
132 perror("Error receiving object from cooridnator\n");
135 if((srcObj = mhashSearch(oid)) == NULL) {
136 printf("Object not found in Main Object Store %s %d\n", __FILE__, __LINE__);
138 h = (objheader_t *) srcObj;
140 size += sizeof(objheader_t);
143 ctrl = OBJECT_NOT_FOUND;
144 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
145 perror("Error sending control msg to coordinator\n");
150 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
151 *((int *)&msg[1])=size;
152 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
153 perror("Error sending size of object to coordinator\n");
156 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
157 perror("Error in sending object\n");
163 case READ_MULT_REQUEST:
164 printf("DEBUG-> READ_MULT_REQUEST\n");
168 printf("DEBUG -> MOVE_REQUEST\n");
171 case MOVE_MULT_REQUEST:
172 printf("DEBUG -> MOVE_MULT_REQUEST\n");
176 /* Read transaction request */
177 printf("DEBUG -> Recv TRANS_REQUEST\n");
178 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
179 printf("Error in readClientReq\n");
184 printf("DEBUG -> Recv TRANS_PREFETCH\n");
185 if((val = prefetchReq((int)acceptfd)) != 0) {
186 printf("Error in readClientReq\n");
190 case START_REMOTE_THREAD:
191 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
193 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
194 else if (retval != sizeof(unsigned int))
195 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
198 { //TODO: execute run method on this global thread object
199 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
200 objType = getObjType(oid);
201 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
202 startDSMthread(oid, objType);
209 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
212 /* Close connection */
213 if (close((int)acceptfd) == -1)
219 /* This function reads the information available in a transaction request
220 * and makes a function call to process the request */
221 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
224 unsigned int *oidmod, oid;
226 objheader_t *headaddr;
227 int sum = 0, i, N, n, val;
231 /* Read fixed_data_t data structure */
232 N = sizeof(fixed) - 1;
233 ptr = (char *)&fixed;;
234 fixed.control = TRANS_REQUEST;
236 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
238 } while(sum < N && n != 0);
240 /* Read list of mids */
241 int mcount = fixed.mcount;
242 N = mcount * sizeof(unsigned int);
243 unsigned int listmid[mcount];
244 ptr = (char *) listmid;
247 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
249 } while(sum < N && n != 0);
251 /* Read oid and version tuples for those objects that are not modified in the transaction */
252 int numread = fixed.numread;
253 N = numread * (sizeof(unsigned int) + sizeof(short));
255 if(numread != 0) { //If pile contains more than one object to be read,
256 // keep reading all objects
259 n = recv((int)acceptfd, (void *) objread, N, 0);
261 } while(sum < N && n != 0);
264 /* Read modified objects */
265 if(fixed.nummod != 0) { // If pile contains more than one modified object,
266 // allocate new object store and recv all modified objects
267 // TODO deallocate this space
268 pthread_mutex_lock(&mainobjstore_mutex);
269 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
270 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
271 pthread_mutex_unlock(&mainobjstore_mutex);
274 pthread_mutex_unlock(&mainobjstore_mutex);
276 do { // Recv the objs that are modified by the Coordinator
277 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
279 } while (sum < fixed.sum_bytes && n != 0);
282 /* Create an array of oids for modified objects */
283 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
286 printf("calloc error %s, %d\n", __FILE__, __LINE__);
289 ptr = (char *) modptr;
290 for(i = 0 ; i < fixed.nummod; i++) {
292 headaddr = (objheader_t *) ptr;
295 GETSIZE(tmpsize, headaddr);
296 ptr += sizeof(objheader_t) + tmpsize;
299 /*Process the information read */
300 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
301 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
319 /* This function processes the Coordinator's transaction request using "handleTransReq"
320 * function and sends a reply to the co-ordinator.
321 * Following this it also receives a new control message from the co-ordinator and processes this message*/
322 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
323 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
324 char *ptr, control, sendctrl;
325 objheader_t *tmp_header;
327 int i = 0, val, retval;
329 /* Send reply to the Coordinator */
330 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
331 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
335 /* Read new control message from Coordiator */
336 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
337 perror("Error in receiving control message\n");
341 /* Process the new control message */
344 /* Set all ref counts as 1 and do garbage collection */
346 for(i = 0; i< fixed->nummod; i++) {
348 tmp_header = (objheader_t *)ptr;
349 tmp_header->rcount = 0;
350 GETSIZE(tmpsize, tmp_header);
351 ptr += sizeof(objheader_t) + tmpsize;
353 /* Unlock objects that was locked due to this transaction */
354 for(i = 0; i< transinfo->numlocked; i++) {
355 header = mhashSearch(transinfo->objlocked[i]);// find the header address
356 STATUS(((objheader_t *)header)) &= ~(LOCK);
359 /* Send ack to Coordinator */
360 printf("DEBUG -> Recv TRANS_ABORT\n");
361 sendctrl = TRANS_SUCESSFUL;
362 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
363 perror("Error sending ACK to coordinator\n");
364 if (transinfo->objlocked != NULL) {
365 free(transinfo->objlocked);
366 transinfo->objlocked = NULL;
368 if (transinfo->objnotfound != NULL) {
369 free(transinfo->objnotfound);
370 transinfo->objnotfound = NULL;
379 /* Invoke the transCommit process() */
380 printf("DEBUG -> Recv TRANS_COMMIT \n");
381 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
382 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
384 printf("DEBUG -> Freeing...\n");
386 if (transinfo->objlocked != NULL) {
387 free(transinfo->objlocked);
388 transinfo->objlocked = NULL;
390 if (transinfo->objnotfound != NULL) {
391 free(transinfo->objnotfound);
392 transinfo->objnotfound = NULL;
398 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
399 //TODO expect another transrequest from client
400 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
403 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
404 //TODO Use fixed.trans_id TID since Client may have died
409 printf("DEBUG -> Freeing...\n");
412 if (transinfo->objlocked != NULL) {
413 free(transinfo->objlocked);
414 transinfo->objlocked = NULL;
416 if (transinfo->objnotfound != NULL) {
417 free(transinfo->objnotfound);
418 transinfo->objnotfound = NULL;
424 /* This function increments counters while running a voting decision on all objects involved
425 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
426 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
429 char control = 0, *ptr;
431 unsigned int *oidnotfound, *oidlocked;
433 objheader_t *headptr;
435 /* Counters and arrays to formulate decision on control message to be sent */
436 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
437 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
438 int objnotfound = 0, objlocked = 0;
439 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
441 /* modptr points to the beginning of the object store
442 * created at the Pariticipant.
443 * Object store holds the modified objects involved in the transaction request */
444 ptr = (char *) modptr;
446 /* Process each oid in the machine pile/ group per thread */
447 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
448 if (i < fixed->numread) {//Objs only read and not modified
449 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
451 oid = *((unsigned int *)(objread + incr));
452 incr += sizeof(unsigned int);
453 version = *((short *)(objread + incr));
454 } else {//Objs modified
456 headptr = (objheader_t *) ptr;
458 version = headptr->version;
459 GETSIZE(tmpsize, headptr);
460 ptr += sizeof(objheader_t) + tmpsize;
463 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
465 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
466 /* Save the oids not found and number of oids not found for later use */
467 oidnotfound[objnotfound] = oid;
469 } else { /* If Obj found in machine (i.e. has not moved) */
470 /* Check if Obj is locked by any previous transaction */
471 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
472 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
474 } else {/* If versions don't match ...HARD ABORT */
476 /* Send TRANS_DISAGREE to Coordinator */
477 control = TRANS_DISAGREE;
478 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
479 perror("Error in sending control to the Coordinator\n");
482 printf("DEBUG -> Sending TRANS_DISAGREE\n");
485 } else {/* If Obj is not locked then lock object */
486 STATUS(((objheader_t *)mobj)) |= LOCK;
488 /*TESTING Add random wait to make transactions run for a long time such that
489 * we can test for soft abort case */
493 /* Save all object oids that are locked on this machine during this transaction request call */
494 oidlocked[objlocked] = OID(((objheader_t *)mobj));
496 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
498 } else { /* If versions don't match ...HARD ABORT */
500 control = TRANS_DISAGREE;
501 /* Send TRANS_DISAGREE to Coordinator */
502 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
503 perror("Error in sending control to the Coordinator\n");
506 printf("DEBUG -> Sending TRANS_DISAGREE\n");
513 /* Decide what control message to send to Coordinator */
514 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
515 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
516 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
523 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
524 * to send to Coordinator based on the votes of oids involved in the transaction */
525 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
526 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
527 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
530 /* Condition to send TRANS_AGREE */
531 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
532 control = TRANS_AGREE;
533 /* Send control message */
534 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
535 perror("Error in sending control to Coordinator\n");
538 printf("DEBUG -> Sending TRANS_AGREE\n");
540 /* Condition to send TRANS_SOFT_ABORT */
541 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
542 control = TRANS_SOFT_ABORT;
543 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
544 *((int*)&msg[1])= *(objnotfound);
546 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
547 /* Send control message */
548 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
549 perror("Error in sending no of objects that are not found\n");
552 /* Send number of oids not found and the missing oids if objects are missing in the machine */
553 if(*(objnotfound) != 0) {
554 int size = sizeof(unsigned int)* *(objnotfound);
555 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
556 perror("Error in sending objects that are not found\n");
562 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
563 * if Participant receives a TRANS_COMMIT */
564 transinfo->objlocked = oidlocked;
565 transinfo->objnotfound = oidnotfound;
566 transinfo->modptr = modptr;
567 transinfo->numlocked = *(objlocked);
568 transinfo->numnotfound = *(objnotfound);
573 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
574 * addresses in lookup table and also changes version number
575 * Sends an ACK back to Coordinator */
576 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
578 int i = 0, offset = 0;
581 /* Process each modified object saved in the mainobject store */
582 for(i = 0; i < nummod; i++) {
583 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
584 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
587 /* Change reference count of older address and free space in objstr ?? */
590 /* Change ptr address in mhash table */
591 mhashRemove(oidmod[i]);
592 mhashInsert(oidmod[i], (((char *)modptr) + offset));
595 GETSIZE(tmpsize,header);
596 offset += sizeof(objheader_t) + tmpsize;
598 /* Update object version number */
599 header = (objheader_t *) mhashSearch(oidmod[i]);
600 header->version += 1;
603 /* Unlock locked objects */
604 for(i = 0; i < numlocked; i++) {
605 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
606 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
609 STATUS(header) &= ~(LOCK);
611 //TODO Update location lookup table
613 /* Send ack to coordinator */
614 control = TRANS_SUCESSFUL;
615 printf("DEBUG-> TRANS_SUCESSFUL\n");
616 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
617 perror("Error sending ACK to coordinator\n");
623 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
624 * Looks for the objects to be prefetched in the main object store.
625 * If objects are not found then record those and if objects are found
626 * then use offset values to prefetch references to other objects */
628 int prefetchReq(int acceptfd) {
629 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
630 unsigned int oid, index = 0;
631 char *ptr, buffer[PRE_BUF_SIZE];
635 objheader_t * header;
638 /* Repeatedly recv the oid and offset pairs sent for prefetch */
639 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
644 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
645 // first 4 bytes are saved to send the
646 // size of the buffer (that is computed at the end of the loop)
649 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
650 sizeof(unsigned int) - bytesRecvd, 0);
651 } while (bytesRecvd < sizeof(unsigned int));
652 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
653 N = numoffset * sizeof(short);
654 short offset[numoffset];
655 ptr = (char *)&offset;
656 /* Recv the offset values per oid */
658 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
660 } while(sum < N && n != 0);
662 /* Process each oid */
663 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
664 /* Save the oids not found in buffer for later use */
665 *(buffer + index) = OBJECT_NOT_FOUND;
666 index += sizeof(char);
667 memcpy(buffer+index, &oid, sizeof(unsigned int));
668 index += sizeof(unsigned int);
669 } else { /* If Obj found in machine (i.e. has not moved) */
670 /* send the oid, it's size, it's header and data */
672 GETSIZE(size, header);
673 size += sizeof(objheader_t);
674 *(buffer + index) = OBJECT_FOUND;
675 index += sizeof(char);
676 memcpy(buffer+index, &oid, sizeof(unsigned int));
677 index += sizeof(unsigned int);
678 memcpy(buffer+index, &size, sizeof(int));
679 index += sizeof(int);
680 memcpy(buffer + index, header, size);
682 /* Calculate the oid corresponding to the offset value */
683 for(i = 0 ; i< numoffset ; i++) {
684 objoid = *((int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
685 if((header = mhashSearch(objoid)) == NULL) {
686 /* Obj not found, send oid */
687 *(buffer + index) = OBJECT_NOT_FOUND;
688 index += sizeof(char);
689 memcpy(buffer+index, &oid, sizeof(unsigned int));
690 index += sizeof(unsigned int);
692 } else {/* Obj Found */
693 /* send the oid, it's size, it's header and data */
694 GETSIZE(size, header);
695 size+=sizeof(objheader_t);
696 *(buffer + index) = OBJECT_FOUND;
697 index += sizeof(char);
698 memcpy(buffer+index, &oid, sizeof(unsigned int));
699 index += sizeof(unsigned int);
700 memcpy(buffer+index, &size, sizeof(int));
701 index += sizeof(int);
702 memcpy(buffer + index, header, size);
708 /* Check for overflow in the buffer */
709 if (index >= PRE_BUF_SIZE) {
710 printf("Char buffer is overflowing\n");
713 /* Send Prefetch response control message only once*/
715 control = TRANS_PREFETCH_RESPONSE;
716 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
717 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
722 /* Add the buffer size into buffer as a parameter */
723 *((unsigned int *)buffer)=index;
724 /* Send the entire buffer with its size and oids found and not found */
725 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
726 perror("Error sending oids found\n");