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",
207 objType = getObjType(oid);
208 startDSMthread(oid, objType);
213 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
216 /* Close connection */
217 if (close((int)acceptfd) == -1)
223 /* This function reads the information available in a transaction request
224 * and makes a function call to process the request */
225 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
228 unsigned int *oidmod, oid;
230 objheader_t *headaddr;
231 int sum = 0, i, N, n, val;
235 /* Read fixed_data_t data structure */
236 N = sizeof(fixed) - 1;
237 ptr = (char *)&fixed;;
238 fixed.control = TRANS_REQUEST;
240 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
242 } while(sum < N && n != 0);
244 /* Read list of mids */
245 int mcount = fixed.mcount;
246 N = mcount * sizeof(unsigned int);
247 unsigned int listmid[mcount];
248 ptr = (char *) listmid;
251 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
253 } while(sum < N && n != 0);
255 /* Read oid and version tuples for those objects that are not modified in the transaction */
256 int numread = fixed.numread;
257 N = numread * (sizeof(unsigned int) + sizeof(short));
259 if(numread != 0) { //If pile contains more than one object to be read,
260 // keep reading all objects
263 n = recv((int)acceptfd, (void *) objread, N, 0);
265 } while(sum < N && n != 0);
268 /* Read modified objects */
269 if(fixed.nummod != 0) {
270 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
271 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
275 do { // Recv the objs that are modified by the Coordinator
276 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
278 } while (sum < fixed.sum_bytes && n != 0);
281 /* Create an array of oids for modified objects */
282 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
285 printf("calloc error %s, %d\n", __FILE__, __LINE__);
288 ptr = (char *) modptr;
289 for(i = 0 ; i < fixed.nummod; i++) {
291 headaddr = (objheader_t *) ptr;
294 GETSIZE(tmpsize, headaddr);
295 ptr += sizeof(objheader_t) + tmpsize;
298 /*Process the information read */
299 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
300 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
316 /* This function processes the Coordinator's transaction request using "handleTransReq"
317 * function and sends a reply to the co-ordinator.
318 * Following this it also receives a new control message from the co-ordinator and processes this message*/
319 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
320 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
321 char *ptr, control, sendctrl;
322 objheader_t *tmp_header;
324 int i = 0, val, retval;
326 /* Send reply to the Coordinator */
327 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
328 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
332 /* Read new control message from Coordiator */
333 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
334 perror("Error in receiving control message\n");
338 /* Process the new control message */
341 if (fixed->nummod > 0)
343 /* Unlock objects that was locked due to this transaction */
344 for(i = 0; i< transinfo->numlocked; i++) {
345 header = mhashSearch(transinfo->objlocked[i]);// find the header address
346 STATUS(((objheader_t *)header)) &= ~(LOCK);
349 /* Send ack to Coordinator */
350 sendctrl = TRANS_SUCESSFUL;
351 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
352 perror("Error sending ACK to coordinator\n");
353 if (transinfo->objlocked != NULL) {
354 free(transinfo->objlocked);
356 if (transinfo->objnotfound != NULL) {
357 free(transinfo->objnotfound);
366 /* Invoke the transCommit process() */
367 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
368 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
370 if (transinfo->objlocked != NULL) {
371 free(transinfo->objlocked);
373 if (transinfo->objnotfound != NULL) {
374 free(transinfo->objnotfound);
380 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
381 //TODO expect another transrequest from client
382 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
385 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
386 //TODO Use fixed.trans_id TID since Client may have died
391 if (transinfo->objlocked != NULL) {
392 free(transinfo->objlocked);
394 if (transinfo->objnotfound != NULL) {
395 free(transinfo->objnotfound);
401 /* This function increments counters while running a voting decision on all objects involved
402 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
403 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
405 unsigned short version;
406 char control = 0, *ptr;
408 unsigned int *oidnotfound, *oidlocked;
410 objheader_t *headptr;
412 /* Counters and arrays to formulate decision on control message to be sent */
413 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
414 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
415 int objnotfound = 0, objlocked = 0;
416 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
418 /* modptr points to the beginning of the object store
419 * created at the Pariticipant.
420 * Object store holds the modified objects involved in the transaction request */
421 ptr = (char *) modptr;
423 /* Process each oid in the machine pile/ group per thread */
424 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
425 if (i < fixed->numread) {//Objs only read and not modified
426 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
428 oid = *((unsigned int *)(objread + incr));
429 incr += sizeof(unsigned int);
430 version = *((unsigned short *)(objread + incr));
431 } else {//Objs modified
433 headptr = (objheader_t *) ptr;
435 version = headptr->version;
436 GETSIZE(tmpsize, headptr);
437 ptr += sizeof(objheader_t) + tmpsize;
440 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
442 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
443 /* Save the oids not found and number of oids not found for later use */
444 oidnotfound[objnotfound] = oid;
446 } else { /* If Obj found in machine (i.e. has not moved) */
447 /* Check if Obj is locked by any previous transaction */
448 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
449 if (version == ((objheader_t *)mobj)->version) { /* If locked then match versions */
451 } else {/* If versions don't match ...HARD ABORT */
453 /* Send TRANS_DISAGREE to Coordinator */
454 control = TRANS_DISAGREE;
455 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
456 perror("Error in sending control to the Coordinator\n");
461 } else {/* If Obj is not locked then lock object */
462 STATUS(((objheader_t *)mobj)) |= LOCK;
463 /* Save all object oids that are locked on this machine during this transaction request call */
464 oidlocked[objlocked] = OID(((objheader_t *)mobj));
466 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
468 } else { /* If versions don't match ...HARD ABORT */
470 control = TRANS_DISAGREE;
471 /* Send TRANS_DISAGREE to Coordinator */
472 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
473 perror("Error in sending control to the Coordinator\n");
477 STATUS(((objheader_t *)mobj)) &= ~(LOCK);
486 /* Decide what control message to send to Coordinator */
487 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
488 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
489 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
496 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
497 * to send to Coordinator based on the votes of oids involved in the transaction */
498 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
499 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
500 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
503 /* Condition to send TRANS_AGREE */
504 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
505 control = TRANS_AGREE;
506 /* Send control message */
507 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
508 perror("Error in sending control to Coordinator\n");
512 /* Condition to send TRANS_SOFT_ABORT */
513 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
514 control = TRANS_SOFT_ABORT;
515 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
516 *((int*)&msg[1])= *(objnotfound);
518 /* Send control message */
519 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
520 perror("Error in sending no of objects that are not found\n");
523 /* Send number of oids not found and the missing oids if objects are missing in the machine */
524 if(*(objnotfound) != 0) {
525 int size = sizeof(unsigned int)* *(objnotfound);
526 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
527 perror("Error in sending objects that are not found\n");
533 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
534 * if Participant receives a TRANS_COMMIT */
535 transinfo->objlocked = oidlocked;
536 transinfo->objnotfound = oidnotfound;
537 transinfo->modptr = modptr;
538 transinfo->numlocked = *(objlocked);
539 transinfo->numnotfound = *(objnotfound);
544 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
545 * addresses in lookup table and also changes version number
546 * Sends an ACK back to Coordinator */
547 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
549 objheader_t *newheader;
550 int i = 0, offset = 0;
554 /* Process each modified object saved in the mainobject store */
555 for(i = 0; i < nummod; i++) {
556 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
557 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
560 GETSIZE(tmpsize,header);
561 pthread_mutex_lock(&mainobjstore_mutex);
562 memcpy(header, (char *)modptr + offset, tmpsize + sizeof(objheader_t));
563 header->version += 1;
564 pthread_mutex_unlock(&mainobjstore_mutex);
565 offset += sizeof(objheader_t) + tmpsize;
571 /* Unlock locked objects */
572 for(i = 0; i < numlocked; i++) {
573 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
574 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
577 STATUS(header) &= ~(LOCK);
579 //TODO Update location lookup table
581 /* Send ack to coordinator */
582 control = TRANS_SUCESSFUL;
583 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
584 perror("Error sending ACK to coordinator\n");
590 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
591 * Looks for the objects to be prefetched in the main object store.
592 * If objects are not found then record those and if objects are found
593 * then use offset values to prefetch references to other objects */
595 int prefetchReq(int acceptfd) {
596 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
598 unsigned int oid, index = 0;
599 char *ptr, buffer[PRE_BUF_SIZE];
603 objheader_t * header;
606 /* Repeatedly recv the oid and offset pairs sent for prefetch */
607 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
612 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
613 // first 4 bytes are saved to send the
614 // size of the buffer (that is computed at the end of the loop)
617 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
618 sizeof(unsigned int) - bytesRecvd, 0);
619 } while (bytesRecvd < sizeof(unsigned int));
620 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
621 N = numoffset * sizeof(short);
622 short offset[numoffset];
623 ptr = (char *)&offset;
624 /* Recv the offset values per oid */
626 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
628 } while(sum < N && n != 0);
630 /* Process each oid */
631 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
632 /* Save the oids not found in buffer for later use */
633 *(buffer + index) = OBJECT_NOT_FOUND;
634 index += sizeof(char);
635 memcpy(buffer+index, &oid, sizeof(unsigned int));
636 index += sizeof(unsigned int);
637 } else { /* If Obj found in machine (i.e. has not moved) */
638 /* send the oid, it's size, it's header and data */
640 GETSIZE(size, header);
641 size += sizeof(objheader_t);
642 *(buffer + index) = OBJECT_FOUND;
643 index += sizeof(char);
644 memcpy(buffer+index, &oid, sizeof(unsigned int));
645 index += sizeof(unsigned int);
646 memcpy(buffer+index, &size, sizeof(int));
647 index += sizeof(int);
648 memcpy(buffer + index, header, size);
650 /* Calculate the oid corresponding to the offset value */
651 for(i = 0 ; i< numoffset ; i++) {
652 /* Check for arrays */
653 if(TYPE(header) > NUMCLASSES) {
657 int elementsize = classsize[TYPE(header)];
658 objoid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offset[i])));
660 objoid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offset[i]));
662 if((header = mhashSearch(objoid)) == NULL) {
663 /* Obj not found, send oid */
664 *(buffer + index) = OBJECT_NOT_FOUND;
665 index += sizeof(char);
666 memcpy(buffer+index, &oid, sizeof(unsigned int));
667 index += sizeof(unsigned int);
669 } else {/* Obj Found */
670 /* 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);
686 /* Check for overflow in the buffer */
687 if (index >= PRE_BUF_SIZE) {
688 printf("Char buffer is overflowing\n");
691 /* Send Prefetch response control message only once*/
693 control = TRANS_PREFETCH_RESPONSE;
694 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
695 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
700 /* Add the buffer size into buffer as a parameter */
701 *((unsigned int *)buffer)=index;
702 /* Send the entire buffer with its size and oids found and not found */
703 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
704 perror("Error sending oids found\n");