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;
27 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
29 /* This function initializes the main objects store and creates the
30 * global machine and location lookup table */
34 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
35 /* Initialize attribute for mutex */
36 pthread_mutexattr_init(&mainobjstore_mutex_attr);
37 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
38 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
39 if (mhashCreate(HASH_SIZE, LOADFACTOR))
42 if (lhashCreate(HASH_SIZE, LOADFACTOR))
48 /* This function starts the thread to listen on a socket
49 * for tranaction calls */
52 int listenfd, acceptfd;
53 struct sockaddr_in my_addr;
54 struct sockaddr_in client_addr;
55 socklen_t addrlength = sizeof(struct sockaddr);
56 pthread_t thread_dstm_accept;
60 listenfd = socket(AF_INET, SOCK_STREAM, 0);
67 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
72 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
78 my_addr.sin_family = AF_INET;
79 my_addr.sin_port = htons(LISTEN_PORT);
80 my_addr.sin_addr.s_addr = INADDR_ANY;
81 memset(&(my_addr.sin_zero), '\0', 8);
83 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
89 if (listen(listenfd, BACKLOG) == -1)
95 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
99 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
101 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
103 pthread_detach(thread_dstm_accept);
106 /* This function accepts a new connection request, decodes the control message in the connection
107 * and accordingly calls other functions to process new requests */
108 void *dstmAccept(void *acceptfd)
110 int numbytes,i, val, retval;
112 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
116 trans_commit_data_t transinfo;
117 unsigned short objType;
119 transinfo.objlocked = NULL;
120 transinfo.objnotfound = NULL;
121 transinfo.modptr = NULL;
122 transinfo.numlocked = 0;
123 transinfo.numnotfound = 0;
125 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
127 /* Receive control messages from other machines */
128 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
130 pthread_exit(NULL); // Testing connection
132 perror("Error in receiving control from coordinator\n");
138 /* Read oid requested and search if available */
139 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
140 perror("Error receiving object from cooridnator\n");
143 if((srcObj = mhashSearch(oid)) == NULL) {
144 printf("Object not found in Main Object Store %s %d\n", __FILE__, __LINE__);
146 h = (objheader_t *) srcObj;
148 size += sizeof(objheader_t);
151 ctrl = OBJECT_NOT_FOUND;
152 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
153 perror("Error sending control msg to coordinator\n");
158 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
159 *((int *)&msg[1])=size;
160 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
161 perror("Error sending size of object to coordinator\n");
164 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
165 perror("Error in sending object\n");
171 case READ_MULT_REQUEST:
172 printf("DEBUG-> READ_MULT_REQUEST\n");
176 printf("DEBUG -> MOVE_REQUEST\n");
179 case MOVE_MULT_REQUEST:
180 printf("DEBUG -> MOVE_MULT_REQUEST\n");
184 /* Read transaction request */
185 printf("DEBUG -> Recv TRANS_REQUEST\n");
186 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
187 printf("Error in readClientReq\n");
192 printf("DEBUG -> Recv TRANS_PREFETCH\n");
193 if((val = prefetchReq((int)acceptfd)) != 0) {
194 printf("Error in readClientReq\n");
198 case START_REMOTE_THREAD:
199 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
201 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
202 else if (retval != sizeof(unsigned int))
203 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
206 { //TODO: execute run method on this global thread object
207 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
208 objType = getObjType(oid);
209 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
210 startDSMthread(oid, objType);
217 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
220 /* Close connection */
221 if (close((int)acceptfd) == -1)
227 /* This function reads the information available in a transaction request
228 * and makes a function call to process the request */
229 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
232 unsigned int *oidmod, oid;
234 objheader_t *headaddr;
235 int sum = 0, i, N, n, val;
239 /* Read fixed_data_t data structure */
240 N = sizeof(fixed) - 1;
241 ptr = (char *)&fixed;;
242 fixed.control = TRANS_REQUEST;
244 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
246 } while(sum < N && n != 0);
248 /* Read list of mids */
249 int mcount = fixed.mcount;
250 N = mcount * sizeof(unsigned int);
251 unsigned int listmid[mcount];
252 ptr = (char *) listmid;
255 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
257 } while(sum < N && n != 0);
259 /* Read oid and version tuples for those objects that are not modified in the transaction */
260 int numread = fixed.numread;
261 N = numread * (sizeof(unsigned int) + sizeof(short));
263 if(numread != 0) { //If pile contains more than one object to be read,
264 // keep reading all objects
267 n = recv((int)acceptfd, (void *) objread, N, 0);
269 } while(sum < N && n != 0);
272 /* Read modified objects */
273 if(fixed.nummod != 0) { // If pile contains more than one modified object,
274 // allocate new object store and recv all modified objects
275 // TODO deallocate this space
276 pthread_mutex_lock(&mainobjstore_mutex);
277 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
278 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
279 pthread_mutex_unlock(&mainobjstore_mutex);
282 pthread_mutex_unlock(&mainobjstore_mutex);
284 do { // Recv the objs that are modified by the Coordinator
285 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
287 } while (sum < fixed.sum_bytes && n != 0);
290 /* Create an array of oids for modified objects */
291 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
294 printf("calloc error %s, %d\n", __FILE__, __LINE__);
297 ptr = (char *) modptr;
298 for(i = 0 ; i < fixed.nummod; i++) {
300 headaddr = (objheader_t *) ptr;
303 GETSIZE(tmpsize, headaddr);
304 ptr += sizeof(objheader_t) + tmpsize;
307 /*Process the information read */
308 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
309 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
327 /* This function processes the Coordinator's transaction request using "handleTransReq"
328 * function and sends a reply to the co-ordinator.
329 * Following this it also receives a new control message from the co-ordinator and processes this message*/
330 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
331 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
332 char *ptr, control, sendctrl;
333 objheader_t *tmp_header;
335 int i = 0, val, retval;
337 /* Send reply to the Coordinator */
338 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
339 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
343 /* Read new control message from Coordiator */
344 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
345 perror("Error in receiving control message\n");
349 /* Process the new control message */
352 /* Set all ref counts as 1 and do garbage collection */
354 for(i = 0; i< fixed->nummod; i++) {
356 tmp_header = (objheader_t *)ptr;
357 tmp_header->rcount = 0;
358 GETSIZE(tmpsize, tmp_header);
359 ptr += sizeof(objheader_t) + tmpsize;
361 /* Unlock objects that was locked due to this transaction */
362 for(i = 0; i< transinfo->numlocked; i++) {
363 header = mhashSearch(transinfo->objlocked[i]);// find the header address
364 STATUS(((objheader_t *)header)) &= ~(LOCK);
367 /* Send ack to Coordinator */
368 sendctrl = TRANS_SUCESSFUL;
369 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
370 perror("Error sending ACK to coordinator\n");
371 if (transinfo->objlocked != NULL) {
372 free(transinfo->objlocked);
374 if (transinfo->objnotfound != NULL) {
375 free(transinfo->objnotfound);
384 /* Invoke the transCommit process() */
385 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
386 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
388 printf("DEBUG -> Freeing...\n");
390 if (transinfo->objlocked != NULL) {
391 free(transinfo->objlocked);
393 if (transinfo->objnotfound != NULL) {
394 free(transinfo->objnotfound);
400 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
401 //TODO expect another transrequest from client
402 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
405 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
406 //TODO Use fixed.trans_id TID since Client may have died
411 printf("DEBUG -> Freeing...\n");
414 if (transinfo->objlocked != NULL) {
415 free(transinfo->objlocked);
417 if (transinfo->objnotfound != NULL) {
418 free(transinfo->objnotfound);
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 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");
484 } else {/* If Obj is not locked then lock object */
485 STATUS(((objheader_t *)mobj)) |= LOCK;
487 /*TESTING Add random wait to make transactions run for a long time such that
488 * we can test for soft abort case */
492 /* Save all object oids that are locked on this machine during this transaction request call */
493 oidlocked[objlocked] = OID(((objheader_t *)mobj));
495 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
497 } else { /* If versions don't match ...HARD ABORT */
499 control = TRANS_DISAGREE;
500 /* Send TRANS_DISAGREE to Coordinator */
501 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
502 perror("Error in sending control to the Coordinator\n");
506 STATUS(((objheader_t *)mobj)) &= ~(LOCK);
515 /* Decide what control message to send to Coordinator */
516 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
517 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
518 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
525 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
526 * to send to Coordinator based on the votes of oids involved in the transaction */
527 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
528 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
529 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
532 /* Condition to send TRANS_AGREE */
533 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
534 control = TRANS_AGREE;
535 /* Send control message */
536 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
537 perror("Error in sending control to Coordinator\n");
541 /* Condition to send TRANS_SOFT_ABORT */
542 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
543 control = TRANS_SOFT_ABORT;
544 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
545 *((int*)&msg[1])= *(objnotfound);
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 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
616 perror("Error sending ACK to coordinator\n");
622 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
623 * Looks for the objects to be prefetched in the main object store.
624 * If objects are not found then record those and if objects are found
625 * then use offset values to prefetch references to other objects */
627 int prefetchReq(int acceptfd) {
628 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
629 unsigned int oid, index = 0;
630 char *ptr, buffer[PRE_BUF_SIZE];
634 objheader_t * header;
637 /* Repeatedly recv the oid and offset pairs sent for prefetch */
638 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
643 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
644 // first 4 bytes are saved to send the
645 // size of the buffer (that is computed at the end of the loop)
648 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
649 sizeof(unsigned int) - bytesRecvd, 0);
650 } while (bytesRecvd < sizeof(unsigned int));
651 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
652 N = numoffset * sizeof(short);
653 short offset[numoffset];
654 ptr = (char *)&offset;
655 /* Recv the offset values per oid */
657 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
659 } while(sum < N && n != 0);
661 /* Process each oid */
662 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
663 /* Save the oids not found in buffer for later use */
664 *(buffer + index) = OBJECT_NOT_FOUND;
665 index += sizeof(char);
666 memcpy(buffer+index, &oid, sizeof(unsigned int));
667 index += sizeof(unsigned int);
668 } else { /* If Obj found in machine (i.e. has not moved) */
669 /* send the oid, it's size, it's header and data */
671 GETSIZE(size, header);
672 size += sizeof(objheader_t);
673 *(buffer + index) = OBJECT_FOUND;
674 index += sizeof(char);
675 memcpy(buffer+index, &oid, sizeof(unsigned int));
676 index += sizeof(unsigned int);
677 memcpy(buffer+index, &size, sizeof(int));
678 index += sizeof(int);
679 memcpy(buffer + index, header, size);
681 /* Calculate the oid corresponding to the offset value */
682 for(i = 0 ; i< numoffset ; i++) {
683 objoid = *((int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
684 if((header = mhashSearch(objoid)) == NULL) {
685 /* Obj not found, send oid */
686 *(buffer + index) = OBJECT_NOT_FOUND;
687 index += sizeof(char);
688 memcpy(buffer+index, &oid, sizeof(unsigned int));
689 index += sizeof(unsigned int);
691 } else {/* Obj Found */
692 /* send the oid, it's size, it's header and data */
693 GETSIZE(size, header);
694 size+=sizeof(objheader_t);
695 *(buffer + index) = OBJECT_FOUND;
696 index += sizeof(char);
697 memcpy(buffer+index, &oid, sizeof(unsigned int));
698 index += sizeof(unsigned int);
699 memcpy(buffer+index, &size, sizeof(int));
700 index += sizeof(int);
701 memcpy(buffer + index, header, size);
707 /* Check for overflow in the buffer */
708 if (index >= PRE_BUF_SIZE) {
709 printf("Char buffer is overflowing\n");
712 /* Send Prefetch response control message only once*/
714 control = TRANS_PREFETCH_RESPONSE;
715 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
716 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
721 /* Add the buffer size into buffer as a parameter */
722 *((unsigned int *)buffer)=index;
723 /* Send the entire buffer with its size and oids found and not found */
724 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
725 perror("Error sending oids found\n");