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 srcObj = mhashSearch(oid);
131 h = (objheader_t *) srcObj;
132 size = sizeof(objheader_t) + sizeof(classsize[TYPE(h)]);
134 ctrl = OBJECT_NOT_FOUND;
135 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
136 perror("Error sending control msg to coordinator\n");
141 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
142 *((int *)&msg[1])=size;
143 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
144 perror("Error sending size of object to coordinator\n");
147 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
148 perror("Error in sending object\n");
154 case READ_MULT_REQUEST:
155 printf("DEBUG-> READ_MULT_REQUEST\n");
159 printf("DEBUG -> MOVE_REQUEST\n");
162 case MOVE_MULT_REQUEST:
163 printf("DEBUG -> MOVE_MULT_REQUEST\n");
167 /* Read transaction request */
168 printf("DEBUG -> Recv TRANS_REQUEST\n");
169 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
170 printf("Error in readClientReq\n");
175 printf("DEBUG -> Recv TRANS_PREFETCH\n");
176 if((val = prefetchReq((int)acceptfd)) != 0) {
177 printf("Error in readClientReq\n");
181 case START_REMOTE_THREAD:
182 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
184 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
185 else if (retval != sizeof(unsigned int))
186 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
189 { //TODO: execute run method on this global thread object
190 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
191 objType = getObjType(oid);
192 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
193 startDSMthread(oid, objType);
200 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
203 /* Close connection */
204 if (close((int)acceptfd) == -1)
210 /* This function reads the information available in a transaction request
211 * and makes a function call to process the request */
212 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
215 unsigned int *oidmod, oid;
217 objheader_t *headaddr;
218 int sum = 0, i, N, n, val;
220 /* Read fixed_data_t data structure */
221 N = sizeof(fixed) - 1;
222 ptr = (char *)&fixed;;
223 fixed.control = TRANS_REQUEST;
225 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
227 } while(sum < N && n != 0);
229 /* Read list of mids */
230 int mcount = fixed.mcount;
231 N = mcount * sizeof(unsigned int);
232 unsigned int listmid[mcount];
233 ptr = (char *) listmid;
236 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
238 } while(sum < N && n != 0);
240 /* Read oid and version tuples for those objects that are not modified in the transaction */
241 int numread = fixed.numread;
242 N = numread * (sizeof(unsigned int) + sizeof(short));
244 if(numread != 0) { //If pile contains more than one object to be read,
245 // keep reading all objects
248 n = recv((int)acceptfd, (void *) objread, N, 0);
250 } while(sum < N && n != 0);
253 /* Read modified objects */
254 if(fixed.nummod != 0) { // If pile contains more than one modified object,
255 // allocate new object store and recv all modified objects
256 // TODO deallocate this space
257 pthread_mutex_lock(&mainobjstore_mutex);
258 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
259 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
260 pthread_mutex_unlock(&mainobjstore_mutex);
263 pthread_mutex_unlock(&mainobjstore_mutex);
265 do { // Recv the objs that are modified by the Coordinator
266 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
268 } while (sum < fixed.sum_bytes && n != 0);
271 /* Create an array of oids for modified objects */
272 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
273 ptr = (char *) modptr;
274 for(i = 0 ; i < fixed.nummod; i++) {
275 headaddr = (objheader_t *) ptr;
278 ptr += sizeof(objheader_t) + classsize[TYPE(headaddr)];
281 /*Process the information read */
282 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
283 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
294 /* This function processes the Coordinator's transaction request using "handleTransReq"
295 * function and sends a reply to the co-ordinator.
296 * Following this it also receives a new control message from the co-ordinator and processes this message*/
297 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
298 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
299 char *ptr, control, sendctrl;
300 objheader_t *tmp_header;
302 int i = 0, val, retval;
304 /* Send reply to the Coordinator */
305 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
306 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
310 /* Read new control message from Coordiator */
311 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
312 perror("Error in receiving control message\n");
316 /* Process the new control message */
319 /* Set all ref counts as 1 and do garbage collection */
321 for(i = 0; i< fixed->nummod; i++) {
322 tmp_header = (objheader_t *)ptr;
323 tmp_header->rcount = 0;
324 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
326 /* Unlock objects that was locked due to this transaction */
327 for(i = 0; i< transinfo->numlocked; i++) {
328 header = mhashSearch(transinfo->objlocked[i]);// find the header address
329 STATUS(((objheader_t *)header)) &= ~(LOCK);
332 /* Send ack to Coordinator */
333 printf("DEBUG -> Recv TRANS_ABORT\n");
334 sendctrl = TRANS_SUCESSFUL;
335 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
336 perror("Error sending ACK to coordinator\n");
343 /* Invoke the transCommit process() */
344 printf("DEBUG -> Recv TRANS_COMMIT \n");
345 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
346 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
351 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
352 //TODO expect another transrequest from client
353 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
356 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
357 //TODO Use fixed.trans_id TID since Client may have died
361 printf("DEBUG -> Freeing...\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;
375 /* This function increments counters while running a voting decision on all objects involved
376 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
377 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
380 char control = 0, *ptr;
382 unsigned int *oidnotfound, *oidlocked, *oidmod;
384 objheader_t *headptr;
386 /* Counters and arrays to formulate decision on control message to be sent */
387 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
388 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
389 int objnotfound = 0, objlocked = 0;
390 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
392 /* modptr points to the beginning of the object store
393 * created at the Pariticipant.
394 * Object store holds the modified objects involved in the transaction request */
395 ptr = (char *) modptr;
397 /* Process each oid in the machine pile/ group per thread */
398 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
399 if (i < fixed->numread) {//Objs only read and not modified
400 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
402 oid = *((unsigned int *)(objread + incr));
403 incr += sizeof(unsigned int);
404 version = *((short *)(objread + incr));
405 } else {//Objs modified
406 headptr = (objheader_t *) ptr;
408 version = headptr->version;
409 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
412 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
414 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
415 /* Save the oids not found and number of oids not found for later use */
416 //oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
417 oidnotfound[objnotfound] = oid;
419 } else { /* If Obj found in machine (i.e. has not moved) */
420 /* Check if Obj is locked by any previous transaction */
421 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
422 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
424 } else {/* If versions don't match ...HARD ABORT */
426 /* Send TRANS_DISAGREE to Coordinator */
427 control = TRANS_DISAGREE;
428 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
429 perror("Error in sending control to the Coordinator\n");
432 printf("DEBUG -> Sending TRANS_DISAGREE\n");
435 } else {/* If Obj is not locked then lock object */
436 STATUS(((objheader_t *)mobj)) |= LOCK;
438 /*TESTING Add random wait to make transactions run for a long time such that
439 * we can test for soft abort case */
443 /* Save all object oids that are locked on this machine during this transaction request call */
444 oidlocked[objlocked] = OID(((objheader_t *)mobj));
446 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
448 } else { /* If versions don't match ...HARD ABORT */
450 control = TRANS_DISAGREE;
451 /* Send TRANS_DISAGREE to Coordinator */
452 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
453 perror("Error in sending control to the Coordinator\n");
456 printf("DEBUG -> Sending TRANS_DISAGREE\n");
463 /* Decide what control message to send to Coordinator */
464 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
465 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
466 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
473 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
474 * to send to Coordinator based on the votes of oids involved in the transaction */
475 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
476 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
477 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
480 /* Condition to send TRANS_AGREE */
481 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
482 control = TRANS_AGREE;
483 /* Send control message */
484 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
485 perror("Error in sending control to Coordinator\n");
488 printf("DEBUG -> Sending TRANS_AGREE\n");
490 /* Condition to send TRANS_SOFT_ABORT */
491 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
492 control = TRANS_SOFT_ABORT;
493 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
494 *((int*)&msg[1])= *(objnotfound);
496 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
497 /* Send control message */
498 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
499 perror("Error in sending no of objects that are not found\n");
502 /* Send number of oids not found and the missing oids if objects are missing in the machine */
503 if(*(objnotfound) != 0) {
504 int size = sizeof(unsigned int)* *(objnotfound);
505 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
506 perror("Error in sending objects that are not found\n");
512 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
513 * if Participant receives a TRANS_COMMIT */
514 transinfo->objlocked = oidlocked;
515 transinfo->objnotfound = oidnotfound;
516 transinfo->modptr = modptr;
517 transinfo->numlocked = *(objlocked);
518 transinfo->numnotfound = *(objnotfound);
523 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
524 * addresses in lookup table and also changes version number
525 * Sends an ACK back to Coordinator */
526 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
528 int i = 0, offset = 0;
531 /* Process each modified object saved in the mainobject store */
532 for(i = 0; i < nummod; i++) {
533 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
534 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
537 /* Change reference count of older address and free space in objstr ?? */
540 /* Change ptr address in mhash table */
541 mhashRemove(oidmod[i]);
542 mhashInsert(oidmod[i], (((char *)modptr) + offset));
543 offset += sizeof(objheader_t) + classsize[TYPE(header)];
545 /* Update object version number */
546 header = (objheader_t *) mhashSearch(oidmod[i]);
547 header->version += 1;
549 /* Unlock locked objects */
550 for(i = 0; i < numlocked; i++) {
551 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
552 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
555 STATUS(header) &= ~(LOCK);
558 //TODO Update location lookup table
560 /* Send ack to coordinator */
561 control = TRANS_SUCESSFUL;
562 printf("DEBUG-> TRANS_SUCESSFUL\n");
563 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
564 perror("Error sending ACK to coordinator\n");
570 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
571 * Looks for the objects to be prefetched in the main object store.
572 * If objects are not found then record those and if objects are found
573 * then use offset values to prefetch references to other objects */
575 int prefetchReq(int acceptfd) {
576 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
577 unsigned int oid, index = 0;
578 char *ptr, buffer[PRE_BUF_SIZE];
581 char *header, control;
585 /* Repeatedly recv the oid and offset pairs sent for prefetch */
586 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
591 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
592 // first 4 bytes are saved to send the
593 // size of the buffer (that is computed at the end of the loop)
596 bytesRecvd += recv((int)acceptfd, (char *)&oid +bytesRecvd,
597 sizeof(unsigned int) - bytesRecvd, 0);
598 } while (bytesRecvd < sizeof(unsigned int));
599 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
600 N = numoffset * sizeof(short);
601 short offset[numoffset];
602 ptr = (char *)&offset;
603 /* Recv the offset values per oid */
605 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
607 } while(sum < N && n != 0);
609 /* Process each oid */
610 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
611 /* Save the oids not found in buffer for later use */
612 *(buffer + index) = OBJECT_NOT_FOUND;
613 index += sizeof(char);
614 memcpy(buffer+index, &oid, sizeof(unsigned int));
615 index += sizeof(unsigned int);
616 } else { /* If Obj found in machine (i.e. has not moved) */
617 /* send the oid, it's size, it's header and data */
618 header = (char *) mobj;
619 head = (objheader_t *) header;
620 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
621 *(buffer + index) = OBJECT_FOUND;
622 index += sizeof(char);
623 memcpy(buffer+index, &oid, sizeof(unsigned int));
624 index += sizeof(unsigned int);
625 memcpy(buffer+index, &size, sizeof(int));
626 index += sizeof(int);
627 memcpy(buffer + index, header, size);
629 /* Calculate the oid corresponding to the offset value */
630 for(i = 0 ; i< numoffset ; i++) {
631 objoid = *((int *)(header + sizeof(objheader_t) + offset[i]));
632 if((header = (char *) mhashSearch(objoid)) == NULL) {
633 /* Obj not found, send oid */
634 *(buffer + index) = OBJECT_NOT_FOUND;
635 index += sizeof(char);
636 memcpy(buffer+index, &oid, sizeof(unsigned int));
637 index += sizeof(unsigned int);
639 } else {/* Obj Found */
640 /* send the oid, it's size, it's header and data */
641 head = (objheader_t *) header;
642 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
643 *(buffer + index) = OBJECT_FOUND;
644 index += sizeof(char);
645 memcpy(buffer+index, &oid, sizeof(unsigned int));
646 index += sizeof(unsigned int);
647 memcpy(buffer+index, &size, sizeof(int));
648 index += sizeof(int);
649 memcpy(buffer + index, header, size);
655 /* Check for overflow in the buffer */
656 if (index >= PRE_BUF_SIZE) {
657 printf("Char buffer is overflowing\n");
660 /* Send Prefetch response control message only once*/
662 control = TRANS_PREFETCH_RESPONSE;
663 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
664 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
669 /* Add the buffer size into buffer as a parameter */
670 *((unsigned int *)buffer)=index;
671 /* Send the entire buffer with its size and oids found and not found */
672 if(send((int)acceptfd, &buffer, index, MSG_NOSIGNAL) < sizeof(index -1)) {
673 perror("Error sending oids found\n");