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 */
14 #define LISTEN_PORT 2156
15 #define BACKLOG 10 //max pending connections
16 #define RECEIVE_BUFFER_SIZE 2048
17 #define PRE_BUF_SIZE 2048
19 extern int classsize[];
21 objstr_t *mainobjstore;
23 /* This function initializes the main objects store and creates the
24 * global machine and location lookup table */
28 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
29 if (mhashCreate(HASH_SIZE, LOADFACTOR))
32 if (lhashCreate(HASH_SIZE, LOADFACTOR))
38 /* This function starts the thread to listen on a socket
39 * for tranaction calls */
42 int listenfd, acceptfd;
43 struct sockaddr_in my_addr;
44 struct sockaddr_in client_addr;
45 socklen_t addrlength = sizeof(struct sockaddr);
46 pthread_t thread_dstm_accept;
50 listenfd = socket(AF_INET, SOCK_STREAM, 0);
57 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
62 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
68 my_addr.sin_family = AF_INET;
69 my_addr.sin_port = htons(LISTEN_PORT);
70 my_addr.sin_addr.s_addr = INADDR_ANY;
71 memset(&(my_addr.sin_zero), '\0', 8);
73 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
79 if (listen(listenfd, BACKLOG) == -1)
85 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
88 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
89 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
93 /* This function accepts a new connection request, decodes the control message in the connection
94 * and accordingly calls other functions to process new requests */
95 void *dstmAccept(void *acceptfd)
97 int numbytes,i, val, retval;
99 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
103 trans_commit_data_t transinfo;
104 unsigned short objType;
106 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
108 /* Receive control messages from other machines */
109 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
111 return; // Testing connection
113 perror("Error in receiving control from coordinator\n");
119 /* Read oid requested and search if available */
120 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
121 perror("Error receiving object from cooridnator\n");
124 srcObj = mhashSearch(oid);
125 h = (objheader_t *) srcObj;
126 size = sizeof(objheader_t) + sizeof(classsize[TYPE(h)]);
128 ctrl = OBJECT_NOT_FOUND;
129 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
130 perror("Error sending control msg to coordinator\n");
135 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
136 *((int *)&msg[1])=size;
137 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
138 perror("Error sending size of object to coordinator\n");
141 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
142 perror("Error in sending object\n");
148 case READ_MULT_REQUEST:
149 printf("DEBUG-> READ_MULT_REQUEST\n");
153 printf("DEBUG -> MOVE_REQUEST\n");
156 case MOVE_MULT_REQUEST:
157 printf("DEBUG -> MOVE_MULT_REQUEST\n");
161 /* Read transaction request */
162 printf("DEBUG -> Recv TRANS_REQUEST\n");
163 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
164 printf("Error in readClientReq\n");
169 printf("DEBUG -> Recv TRANS_PREFETCH\n");
170 if((val = prefetchReq((int)acceptfd)) != 0) {
171 printf("Error in readClientReq\n");
175 case START_REMOTE_THREAD:
176 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
178 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
179 else if (retval != sizeof(unsigned int))
180 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
183 { //TODO: execute run method on this global thread object
184 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
185 objType = getObjType(oid);
186 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
191 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
194 /* Close connection */
195 if (close((int)acceptfd) == -1)
201 /* This function reads the information available in a transaction request
202 * and makes a function call to process the request */
203 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
206 unsigned int *oidmod, oid;
208 objheader_t *headaddr;
209 int sum = 0, i, N, n, val;
211 /* Read fixed_data_t data structure */
212 N = sizeof(fixed) - 1;
213 ptr = (char *)&fixed;;
214 fixed.control = TRANS_REQUEST;
216 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
218 } while(sum < N && n != 0);
220 /* Read list of mids */
221 int mcount = fixed.mcount;
222 N = mcount * sizeof(unsigned int);
223 unsigned int listmid[mcount];
224 ptr = (char *) listmid;
227 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
229 } while(sum < N && n != 0);
231 /* Read oid and version tuples for those objects that are not modified in the transaction */
232 int numread = fixed.numread;
233 N = numread * (sizeof(unsigned int) + sizeof(short));
235 if(numread != 0) { //If pile contains more than one object to be read,
236 // keep reading all objects
239 n = recv((int)acceptfd, (void *) objread, N, 0);
241 } while(sum < N && n != 0);
244 /* Read modified objects */
245 if(fixed.nummod != 0) { // If pile contains more than one modified object,
246 // allocate new object store and recv all modified objects
247 // TODO deallocate this space
248 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
249 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
253 do { // Recv the objs that are modified by the Coordinator
254 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
256 } while (sum < fixed.sum_bytes && n != 0);
259 /* Create an array of oids for modified objects */
260 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
261 ptr = (char *) modptr;
262 for(i = 0 ; i < fixed.nummod; i++) {
263 headaddr = (objheader_t *) ptr;
266 ptr += sizeof(objheader_t) + classsize[TYPE(headaddr)];
269 /*Process the information read */
270 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
271 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
282 /* This function processes the Coordinator's transaction request using "handleTransReq"
283 * function and sends a reply to the co-ordinator.
284 * Following this it also receives a new control message from the co-ordinator and processes this message*/
285 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
286 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
287 char *ptr, control, sendctrl;
288 objheader_t *tmp_header;
290 int i = 0, val, retval;
292 /* Send reply to the Coordinator */
293 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
294 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
298 /* Read new control message from Coordiator */
299 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
300 perror("Error in receiving control message\n");
304 /* Process the new control message */
307 /* Set all ref counts as 1 and do garbage collection */
309 for(i = 0; i< fixed->nummod; i++) {
310 tmp_header = (objheader_t *)ptr;
311 tmp_header->rcount = 0;
312 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
314 /* Unlock objects that was locked due to this transaction */
315 for(i = 0; i< transinfo->numlocked; i++) {
316 header = mhashSearch(transinfo->objlocked[i]);// find the header address
317 STATUS(((objheader_t *)header)) &= ~(LOCK);
320 /* Send ack to Coordinator */
321 printf("DEBUG -> Recv TRANS_ABORT\n");
322 sendctrl = TRANS_SUCESSFUL;
323 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
324 perror("Error sending ACK to coordinator\n");
331 /* Invoke the transCommit process() */
332 printf("DEBUG -> Recv TRANS_COMMIT \n");
333 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
334 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
339 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
340 //TODO expect another transrequest from client
341 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
344 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
345 //TODO Use fixed.trans_id TID since Client may have died
349 printf("DEBUG -> Freeing...\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;
363 /* This function increments counters while running a voting decision on all objects involved
364 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
365 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
368 char control = 0, *ptr;
370 unsigned int *oidnotfound, *oidlocked, *oidmod;
372 objheader_t *headptr;
374 /* Counters and arrays to formulate decision on control message to be sent */
375 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
376 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
377 int objnotfound = 0, objlocked = 0;
378 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
380 /* modptr points to the beginning of the object store
381 * created at the Pariticipant.
382 * Object store holds the modified objects involved in the transaction request */
383 ptr = (char *) modptr;
385 /* Process each oid in the machine pile/ group per thread */
386 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
387 if (i < fixed->numread) {//Objs only read and not modified
388 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
390 oid = *((unsigned int *)(objread + incr));
391 incr += sizeof(unsigned int);
392 version = *((short *)(objread + incr));
393 } else {//Objs modified
394 headptr = (objheader_t *) ptr;
396 version = headptr->version;
397 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
400 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
402 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
403 /* Save the oids not found and number of oids not found for later use */
404 //oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
405 oidnotfound[objnotfound] = oid;
407 } else { /* If Obj found in machine (i.e. has not moved) */
408 /* Check if Obj is locked by any previous transaction */
409 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
410 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
412 } else {/* If versions don't match ...HARD ABORT */
414 /* Send TRANS_DISAGREE to Coordinator */
415 control = TRANS_DISAGREE;
416 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
417 perror("Error in sending control to the Coordinator\n");
420 printf("DEBUG -> Sending TRANS_DISAGREE\n");
423 } else {/* If Obj is not locked then lock object */
424 STATUS(((objheader_t *)mobj)) |= LOCK;
426 /*TESTING Add random wait to make transactions run for a long time such that
427 * we can test for soft abort case */
431 /* Save all object oids that are locked on this machine during this transaction request call */
432 oidlocked[objlocked] = OID(((objheader_t *)mobj));
434 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
436 } else { /* If versions don't match ...HARD ABORT */
438 control = TRANS_DISAGREE;
439 /* Send TRANS_DISAGREE to Coordinator */
440 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
441 perror("Error in sending control to the Coordinator\n");
444 printf("DEBUG -> Sending TRANS_DISAGREE\n");
451 /* Decide what control message to send to Coordinator */
452 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
453 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
454 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
461 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
462 * to send to Coordinator based on the votes of oids involved in the transaction */
463 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
464 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
465 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
468 /* Condition to send TRANS_AGREE */
469 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
470 control = TRANS_AGREE;
471 /* Send control message */
472 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
473 perror("Error in sending control to Coordinator\n");
476 printf("DEBUG -> Sending TRANS_AGREE\n");
478 /* Condition to send TRANS_SOFT_ABORT */
479 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
480 control = TRANS_SOFT_ABORT;
481 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
482 *((int*)&msg[1])= *(objnotfound);
484 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
485 /* Send control message */
486 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
487 perror("Error in sending no of objects that are not found\n");
490 /* Send number of oids not found and the missing oids if objects are missing in the machine */
491 if(*(objnotfound) != 0) {
492 int size = sizeof(unsigned int)* *(objnotfound);
493 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
494 perror("Error in sending objects that are not found\n");
500 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
501 * if Participant receives a TRANS_COMMIT */
502 transinfo->objlocked = oidlocked;
503 transinfo->objnotfound = oidnotfound;
504 transinfo->modptr = modptr;
505 transinfo->numlocked = *(objlocked);
506 transinfo->numnotfound = *(objnotfound);
511 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
512 * addresses in lookup table and also changes version number
513 * Sends an ACK back to Coordinator */
514 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
516 int i = 0, offset = 0;
519 /* Process each modified object saved in the mainobject store */
520 for(i = 0; i < nummod; i++) {
521 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
522 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
525 /* Change reference count of older address and free space in objstr ?? */
528 /* Change ptr address in mhash table */
529 mhashRemove(oidmod[i]);
530 mhashInsert(oidmod[i], (((char *)modptr) + offset));
531 offset += sizeof(objheader_t) + classsize[TYPE(header)];
533 /* Update object version number */
534 header = (objheader_t *) mhashSearch(oidmod[i]);
535 header->version += 1;
537 /* Unlock locked objects */
538 for(i = 0; i < numlocked; i++) {
539 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
540 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
543 STATUS(header) &= ~(LOCK);
546 //TODO Update location lookup table
548 /* Send ack to coordinator */
549 control = TRANS_SUCESSFUL;
550 printf("DEBUG-> TRANS_SUCESSFUL\n");
551 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
552 perror("Error sending ACK to coordinator\n");
558 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
559 * Looks for the objects to be prefetched in the main object store.
560 * If objects are not found then record those and if objects are found
561 * then use offset values to prefetch references to other objects */
563 int prefetchReq(int acceptfd) {
564 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
565 unsigned int oid, index = 0;
566 char *ptr, buffer[PRE_BUF_SIZE];
569 char *header, control;
572 /* Repeatedly recv the oid and offset pairs sent for prefetch */
573 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
578 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
579 // first 4 bytes are saved to send the
580 // size of the buffer (that is computed at the end of the loop)
581 oid = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
582 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
583 N = numoffset * sizeof(short);
584 short offset[numoffset];
585 ptr = (char *)&offset;
586 /* Recv the offset values per oid */
588 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
590 } while(sum < N && n != 0);
592 /* Process each oid */
593 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
594 /* Save the oids not found in buffer for later use */
595 *(buffer + index) = OBJECT_NOT_FOUND;
596 index += sizeof(char);
597 memcpy(buffer+index, &oid, sizeof(unsigned int));
598 index += sizeof(unsigned int);
599 } else { /* If Obj found in machine (i.e. has not moved) */
600 /* send the oid, it's size, it's header and data */
601 header = (char *) mobj;
602 head = (objheader_t *) header;
603 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
604 *(buffer + index) = OBJECT_FOUND;
605 index += sizeof(char);
606 memcpy(buffer+index, &oid, sizeof(unsigned int));
607 index += sizeof(unsigned int);
608 memcpy(buffer+index, &size, sizeof(int));
609 index += sizeof(int);
610 memcpy(buffer + index, header, size);
612 /* Calculate the oid corresponding to the offset value */
613 for(i = 0 ; i< numoffset ; i++) {
614 objoid = *((int *)(header + sizeof(objheader_t) + offset[i]));
615 if((header = (char *) mhashSearch(objoid)) == NULL) {
616 /* Obj not found, send oid */
617 *(buffer + index) = OBJECT_NOT_FOUND;
618 index += sizeof(char);
619 memcpy(buffer+index, &oid, sizeof(unsigned int));
620 index += sizeof(unsigned int);
622 } else {/* Obj Found */
623 /* send the oid, it's size, it's header and data */
624 head = (objheader_t *) header;
625 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
626 *(buffer + index) = OBJECT_FOUND;
627 index += sizeof(char);
628 memcpy(buffer+index, &oid, sizeof(unsigned int));
629 index += sizeof(unsigned int);
630 memcpy(buffer+index, &size, sizeof(int));
631 index += sizeof(int);
632 memcpy(buffer + index, header, size);
638 /* Check for overflow in the buffer */
639 if (index >= PRE_BUF_SIZE) {
640 printf("Char buffer is overflowing\n");
643 /* Send Prefetch response control message only once*/
645 control = TRANS_PREFETCH_RESPONSE;
646 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
647 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
652 /* Add the buffer size into buffer as a parameter */
653 memcpy(buffer, &index, sizeof(unsigned int));
654 /* Send the entire buffer with its size and oids found and not found */
655 if(send((int)acceptfd, &buffer, sizeof(index - 1), MSG_NOSIGNAL) < sizeof(index -1)) {
656 perror("Error sending oids found\n");