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;
27 /* This function initializes the main objects store and creates the
28 * global machine and location lookup table */
32 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
33 if (mhashCreate(HASH_SIZE, LOADFACTOR))
36 if (lhashCreate(HASH_SIZE, LOADFACTOR))
42 /* This function starts the thread to listen on a socket
43 * for tranaction calls */
46 int listenfd, acceptfd;
47 struct sockaddr_in my_addr;
48 struct sockaddr_in client_addr;
49 socklen_t addrlength = sizeof(struct sockaddr);
50 pthread_t thread_dstm_accept;
54 listenfd = socket(AF_INET, SOCK_STREAM, 0);
61 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
66 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
72 my_addr.sin_family = AF_INET;
73 my_addr.sin_port = htons(LISTEN_PORT);
74 my_addr.sin_addr.s_addr = INADDR_ANY;
75 memset(&(my_addr.sin_zero), '\0', 8);
77 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
83 if (listen(listenfd, BACKLOG) == -1)
89 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
92 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
93 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
97 /* This function accepts a new connection request, decodes the control message in the connection
98 * and accordingly calls other functions to process new requests */
99 void *dstmAccept(void *acceptfd)
101 int numbytes,i, val, retval;
103 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
107 trans_commit_data_t transinfo;
108 unsigned short objType;
110 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
112 /* Receive control messages from other machines */
113 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
115 return; // Testing connection
117 perror("Error in receiving control from coordinator\n");
123 /* Read oid requested and search if available */
124 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
125 perror("Error receiving object from cooridnator\n");
128 srcObj = mhashSearch(oid);
129 h = (objheader_t *) srcObj;
130 size = sizeof(objheader_t) + sizeof(classsize[TYPE(h)]);
132 ctrl = OBJECT_NOT_FOUND;
133 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
134 perror("Error sending control msg to coordinator\n");
139 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
140 *((int *)&msg[1])=size;
141 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
142 perror("Error sending size of object to coordinator\n");
145 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
146 perror("Error in sending object\n");
152 case READ_MULT_REQUEST:
153 printf("DEBUG-> READ_MULT_REQUEST\n");
157 printf("DEBUG -> MOVE_REQUEST\n");
160 case MOVE_MULT_REQUEST:
161 printf("DEBUG -> MOVE_MULT_REQUEST\n");
165 /* Read transaction request */
166 printf("DEBUG -> Recv TRANS_REQUEST\n");
167 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
168 printf("Error in readClientReq\n");
173 printf("DEBUG -> Recv TRANS_PREFETCH\n");
174 if((val = prefetchReq((int)acceptfd)) != 0) {
175 printf("Error in readClientReq\n");
179 case START_REMOTE_THREAD:
180 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
182 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
183 else if (retval != sizeof(unsigned int))
184 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
187 { //TODO: execute run method on this global thread object
188 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=0x%x\n", oid);
189 objType = getObjType(oid);
190 printf("dstmAccept(): type of object 0x%x is %d\n", oid, objType);
191 startDSMthread(oid, objType);
198 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
201 /* Close connection */
202 if (close((int)acceptfd) == -1)
208 /* This function reads the information available in a transaction request
209 * and makes a function call to process the request */
210 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
213 unsigned int *oidmod, oid;
215 objheader_t *headaddr;
216 int sum = 0, i, N, n, val;
218 /* Read fixed_data_t data structure */
219 N = sizeof(fixed) - 1;
220 ptr = (char *)&fixed;;
221 fixed.control = TRANS_REQUEST;
223 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
225 } while(sum < N && n != 0);
227 /* Read list of mids */
228 int mcount = fixed.mcount;
229 N = mcount * sizeof(unsigned int);
230 unsigned int listmid[mcount];
231 ptr = (char *) listmid;
234 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
236 } while(sum < N && n != 0);
238 /* Read oid and version tuples for those objects that are not modified in the transaction */
239 int numread = fixed.numread;
240 N = numread * (sizeof(unsigned int) + sizeof(short));
242 if(numread != 0) { //If pile contains more than one object to be read,
243 // keep reading all objects
246 n = recv((int)acceptfd, (void *) objread, N, 0);
248 } while(sum < N && n != 0);
251 /* Read modified objects */
252 if(fixed.nummod != 0) { // If pile contains more than one modified object,
253 // allocate new object store and recv all modified objects
254 // TODO deallocate this space
255 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
256 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
260 do { // Recv the objs that are modified by the Coordinator
261 n = recv((int)acceptfd, (char *) modptr+sum, fixed.sum_bytes-sum, 0);
263 } while (sum < fixed.sum_bytes && n != 0);
266 /* Create an array of oids for modified objects */
267 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
268 ptr = (char *) modptr;
269 for(i = 0 ; i < fixed.nummod; i++) {
270 headaddr = (objheader_t *) ptr;
273 ptr += sizeof(objheader_t) + classsize[TYPE(headaddr)];
276 /*Process the information read */
277 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
278 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
289 /* This function processes the Coordinator's transaction request using "handleTransReq"
290 * function and sends a reply to the co-ordinator.
291 * Following this it also receives a new control message from the co-ordinator and processes this message*/
292 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
293 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
294 char *ptr, control, sendctrl;
295 objheader_t *tmp_header;
297 int i = 0, val, retval;
299 /* Send reply to the Coordinator */
300 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
301 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
305 /* Read new control message from Coordiator */
306 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
307 perror("Error in receiving control message\n");
311 /* Process the new control message */
314 /* Set all ref counts as 1 and do garbage collection */
316 for(i = 0; i< fixed->nummod; i++) {
317 tmp_header = (objheader_t *)ptr;
318 tmp_header->rcount = 0;
319 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
321 /* Unlock objects that was locked due to this transaction */
322 for(i = 0; i< transinfo->numlocked; i++) {
323 header = mhashSearch(transinfo->objlocked[i]);// find the header address
324 STATUS(((objheader_t *)header)) &= ~(LOCK);
327 /* Send ack to Coordinator */
328 printf("DEBUG -> Recv TRANS_ABORT\n");
329 sendctrl = TRANS_SUCESSFUL;
330 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
331 perror("Error sending ACK to coordinator\n");
338 /* Invoke the transCommit process() */
339 printf("DEBUG -> Recv TRANS_COMMIT \n");
340 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
341 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
346 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
347 //TODO expect another transrequest from client
348 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
351 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
352 //TODO Use fixed.trans_id TID since Client may have died
356 printf("DEBUG -> Freeing...\n");
359 if (transinfo->objlocked != NULL) {
360 free(transinfo->objlocked);
361 transinfo->objlocked = NULL;
363 if (transinfo->objnotfound != NULL) {
364 free(transinfo->objnotfound);
365 transinfo->objnotfound = NULL;
370 /* This function increments counters while running a voting decision on all objects involved
371 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
372 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
375 char control = 0, *ptr;
377 unsigned int *oidnotfound, *oidlocked, *oidmod;
379 objheader_t *headptr;
381 /* Counters and arrays to formulate decision on control message to be sent */
382 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
383 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
384 int objnotfound = 0, objlocked = 0;
385 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
387 /* modptr points to the beginning of the object store
388 * created at the Pariticipant.
389 * Object store holds the modified objects involved in the transaction request */
390 ptr = (char *) modptr;
392 /* Process each oid in the machine pile/ group per thread */
393 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
394 if (i < fixed->numread) {//Objs only read and not modified
395 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
397 oid = *((unsigned int *)(objread + incr));
398 incr += sizeof(unsigned int);
399 version = *((short *)(objread + incr));
400 } else {//Objs modified
401 headptr = (objheader_t *) ptr;
403 version = headptr->version;
404 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
407 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
409 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
410 /* Save the oids not found and number of oids not found for later use */
411 //oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
412 oidnotfound[objnotfound] = oid;
414 } else { /* If Obj found in machine (i.e. has not moved) */
415 /* Check if Obj is locked by any previous transaction */
416 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
417 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
419 } else {/* If versions don't match ...HARD ABORT */
421 /* Send TRANS_DISAGREE to Coordinator */
422 control = TRANS_DISAGREE;
423 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
424 perror("Error in sending control to the Coordinator\n");
427 printf("DEBUG -> Sending TRANS_DISAGREE\n");
430 } else {/* If Obj is not locked then lock object */
431 STATUS(((objheader_t *)mobj)) |= LOCK;
433 /*TESTING Add random wait to make transactions run for a long time such that
434 * we can test for soft abort case */
438 /* Save all object oids that are locked on this machine during this transaction request call */
439 oidlocked[objlocked] = OID(((objheader_t *)mobj));
441 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
443 } else { /* If versions don't match ...HARD ABORT */
445 control = TRANS_DISAGREE;
446 /* Send TRANS_DISAGREE to Coordinator */
447 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
448 perror("Error in sending control to the Coordinator\n");
451 printf("DEBUG -> Sending TRANS_DISAGREE\n");
458 /* Decide what control message to send to Coordinator */
459 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
460 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
461 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
468 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
469 * to send to Coordinator based on the votes of oids involved in the transaction */
470 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
471 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
472 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
475 /* Condition to send TRANS_AGREE */
476 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
477 control = TRANS_AGREE;
478 /* Send control message */
479 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
480 perror("Error in sending control to Coordinator\n");
483 printf("DEBUG -> Sending TRANS_AGREE\n");
485 /* Condition to send TRANS_SOFT_ABORT */
486 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
487 control = TRANS_SOFT_ABORT;
488 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
489 *((int*)&msg[1])= *(objnotfound);
491 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
492 /* Send control message */
493 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
494 perror("Error in sending no of objects that are not found\n");
497 /* Send number of oids not found and the missing oids if objects are missing in the machine */
498 if(*(objnotfound) != 0) {
499 int size = sizeof(unsigned int)* *(objnotfound);
500 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
501 perror("Error in sending objects that are not found\n");
507 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
508 * if Participant receives a TRANS_COMMIT */
509 transinfo->objlocked = oidlocked;
510 transinfo->objnotfound = oidnotfound;
511 transinfo->modptr = modptr;
512 transinfo->numlocked = *(objlocked);
513 transinfo->numnotfound = *(objnotfound);
518 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
519 * addresses in lookup table and also changes version number
520 * Sends an ACK back to Coordinator */
521 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
523 int i = 0, offset = 0;
526 /* Process each modified object saved in the mainobject store */
527 for(i = 0; i < nummod; i++) {
528 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
529 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
532 /* Change reference count of older address and free space in objstr ?? */
535 /* Change ptr address in mhash table */
536 mhashRemove(oidmod[i]);
537 mhashInsert(oidmod[i], (((char *)modptr) + offset));
538 offset += sizeof(objheader_t) + classsize[TYPE(header)];
540 /* Update object version number */
541 header = (objheader_t *) mhashSearch(oidmod[i]);
542 header->version += 1;
544 /* Unlock locked objects */
545 for(i = 0; i < numlocked; i++) {
546 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
547 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
550 STATUS(header) &= ~(LOCK);
553 //TODO Update location lookup table
555 /* Send ack to coordinator */
556 control = TRANS_SUCESSFUL;
557 printf("DEBUG-> TRANS_SUCESSFUL\n");
558 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
559 perror("Error sending ACK to coordinator\n");
565 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
566 * Looks for the objects to be prefetched in the main object store.
567 * If objects are not found then record those and if objects are found
568 * then use offset values to prefetch references to other objects */
570 int prefetchReq(int acceptfd) {
571 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
572 unsigned int oid, index = 0;
573 char *ptr, buffer[PRE_BUF_SIZE];
576 char *header, control;
579 /* Repeatedly recv the oid and offset pairs sent for prefetch */
580 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
585 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
586 // first 4 bytes are saved to send the
587 // size of the buffer (that is computed at the end of the loop)
588 oid = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
589 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
590 N = numoffset * sizeof(short);
591 short offset[numoffset];
592 ptr = (char *)&offset;
593 /* Recv the offset values per oid */
595 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
597 } while(sum < N && n != 0);
599 /* Process each oid */
600 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
601 /* Save the oids not found in buffer for later use */
602 *(buffer + index) = OBJECT_NOT_FOUND;
603 index += sizeof(char);
604 memcpy(buffer+index, &oid, sizeof(unsigned int));
605 index += sizeof(unsigned int);
606 } else { /* If Obj found in machine (i.e. has not moved) */
607 /* send the oid, it's size, it's header and data */
608 header = (char *) mobj;
609 head = (objheader_t *) header;
610 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
611 *(buffer + index) = OBJECT_FOUND;
612 index += sizeof(char);
613 memcpy(buffer+index, &oid, sizeof(unsigned int));
614 index += sizeof(unsigned int);
615 memcpy(buffer+index, &size, sizeof(int));
616 index += sizeof(int);
617 memcpy(buffer + index, header, size);
619 /* Calculate the oid corresponding to the offset value */
620 for(i = 0 ; i< numoffset ; i++) {
621 objoid = *((int *)(header + sizeof(objheader_t) + offset[i]));
622 if((header = (char *) mhashSearch(objoid)) == NULL) {
623 /* Obj not found, send oid */
624 *(buffer + index) = OBJECT_NOT_FOUND;
625 index += sizeof(char);
626 memcpy(buffer+index, &oid, sizeof(unsigned int));
627 index += sizeof(unsigned int);
629 } else {/* Obj Found */
630 /* send the oid, it's size, it's header and data */
631 head = (objheader_t *) header;
632 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
633 *(buffer + index) = OBJECT_FOUND;
634 index += sizeof(char);
635 memcpy(buffer+index, &oid, sizeof(unsigned int));
636 index += sizeof(unsigned int);
637 memcpy(buffer+index, &size, sizeof(int));
638 index += sizeof(int);
639 memcpy(buffer + index, header, size);
645 /* Check for overflow in the buffer */
646 if (index >= PRE_BUF_SIZE) {
647 printf("Char buffer is overflowing\n");
650 /* Send Prefetch response control message only once*/
652 control = TRANS_PREFETCH_RESPONSE;
653 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
654 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
659 /* Add the buffer size into buffer as a parameter */
660 memcpy(buffer, &index, sizeof(unsigned int));
661 /* Send the entire buffer with its size and oids found and not found */
662 if(send((int)acceptfd, &buffer, sizeof(index - 1), MSG_NOSIGNAL) < sizeof(index -1)) {
663 perror("Error sending oids found\n");