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;
25 /* Initialize main object store */
26 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
27 /* Create machine lookup table and location lookup table */
28 if (mhashCreate(HASH_SIZE, LOADFACTOR))
31 if (lhashCreate(HASH_SIZE, LOADFACTOR))
39 int listenfd, acceptfd;
40 struct sockaddr_in my_addr;
41 struct sockaddr_in client_addr;
42 socklen_t addrlength = sizeof(struct sockaddr);
43 pthread_t thread_dstm_accept;
47 listenfd = socket(AF_INET, SOCK_STREAM, 0);
54 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
59 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
65 my_addr.sin_family = AF_INET;
66 my_addr.sin_port = htons(LISTEN_PORT);
67 my_addr.sin_addr.s_addr = INADDR_ANY;
68 memset(&(my_addr.sin_zero), '\0', 8);
70 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
76 if (listen(listenfd, BACKLOG) == -1)
82 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
85 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
86 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
90 /* This function accepts a new connection request, decodes the control message in the connection
91 * and accordingly calls other functions to process new requests */
92 void *dstmAccept(void *acceptfd)
94 int numbytes,i, val, retval;
96 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
100 trans_commit_data_t transinfo;
101 unsigned short objType;
103 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
105 printf("Recieved connection: fd = %d\n", (int)acceptfd);
106 /* Receive control messages from other machines */
107 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0) {
109 return; // Testing connection
111 perror("Error in receiving control from coordinator\n");
117 /* Read oid requested and search if available */
118 if((retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0)) <= 0) {
119 perror("Error receiving object from cooridnator\n");
122 srcObj = mhashSearch(oid);
123 h = (objheader_t *) srcObj;
124 size = sizeof(objheader_t) + sizeof(classsize[TYPE(h)]);
126 ctrl = OBJECT_NOT_FOUND;
127 if(send((int)acceptfd, &ctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
128 perror("Error sending control msg to coordinator\n");
133 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
134 *((int *)&msg[1])=size;
135 if(send((int)acceptfd, &msg, sizeof(msg), MSG_NOSIGNAL) < sizeof(msg)) {
136 perror("Error sending size of object to coordinator\n");
139 if(send((int)acceptfd, h, size, MSG_NOSIGNAL) < size) {
140 perror("Error in sending object\n");
146 case READ_MULT_REQUEST:
147 printf("DEBUG-> READ_MULT_REQUEST\n");
151 printf("DEBUG -> MOVE_REQUEST\n");
154 case MOVE_MULT_REQUEST:
155 printf("DEBUG -> MOVE_MULT_REQUEST\n");
159 /* Read transaction request */
160 printf("DEBUG -> Recv TRANS_REQUEST\n");
161 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
162 printf("Error in readClientReq\n");
167 printf("DEBUG -> Recv TRANS_PREFETCH\n");
168 if((val = prefetchReq((int)acceptfd)) != 0) {
169 printf("Error in readClientReq\n");
173 case START_REMOTE_THREAD:
174 retval = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
176 perror("dstmAccept(): error receiving START_REMOTE_THREAD msg");
177 else if (retval != sizeof(unsigned int))
178 printf("dstmAccept(): incorrect msg size %d for START_REMOTE_THREAD\n",
181 { //TODO: execute run method on this global thread object
182 printf("dstmAccept(): received START_REMOTE_THREAD msg, oid=%d\n", oid);
183 objType = getObjType(oid);
184 printf("dstmAccept(): type of object %d is %d\n", oid, objType);
189 printf("DEBUG -> dstmAccept: Error Unknown opcode %d\n", control);
192 /* Close connection */
193 if (close((int)acceptfd) == -1)
196 printf("Closed connection: fd = %d\n", (int)acceptfd);
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) {
207 int sum = 0, i, N, n, val;
209 /* Read fixed_data_t data structure */
210 N = sizeof(fixed) - 1;
211 ptr = (char *)&fixed;;
212 fixed.control = TRANS_REQUEST;
214 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
216 } while(sum < N && n != 0);
218 /* Read list of mids */
219 int mcount = fixed.mcount;
220 N = mcount * sizeof(unsigned int);
221 unsigned int listmid[mcount];
222 ptr = (char *) listmid;
225 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
227 } while(sum < N && n != 0);
229 /* Read oid and version tuples for those objects that are not modified in the transaction */
230 int numread = fixed.numread;
231 N = numread * (sizeof(unsigned int) + sizeof(short));
233 if(numread != 0) { //If pile contains more than one object to be read,
234 // keep reading all objects
237 n = recv((int)acceptfd, (void *) objread, N, 0);
239 } while(sum < N && n != 0);
242 /* Read modified objects */
243 if(fixed.nummod != 0) { // If pile contains more than one modified object,
244 // allocate new object store and recv all modified objects
245 // TODO deallocate this space
246 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
247 printf("objstrAlloc error for modified objects %s, %d\n", __FILE__, __LINE__);
251 do { // Recv the objs that are modified by the Coordinator
252 n = recv((int)acceptfd, modptr+sum, fixed.sum_bytes-sum, 0);
254 } while (sum < fixed.sum_bytes && n != 0);
257 /*Process the information read */
258 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, acceptfd)) != 0) {
259 printf("Error in processClientReq %s, %d\n", __FILE__, __LINE__);
266 /* This function processes the Coordinator's transaction request using "handleTransReq"
267 * function and sends a reply to the co-ordinator.
268 * Following this it also receives a new control message from the co-ordinator and processes this message*/
269 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
270 unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
271 char *ptr, control, sendctrl;
272 objheader_t *tmp_header;
274 int i = 0, val, retval;
276 /* Send reply to the Coordinator */
277 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
278 printf("Handle Trans Req error %s, %d\n", __FILE__, __LINE__);
282 /* Read new control message from Coordiator */
283 if((retval = recv((int)acceptfd, &control, sizeof(char), 0)) <= 0 ) {
284 perror("Error in receiving control message\n");
288 /* Process the new control message */
291 /* Set all ref counts as 1 and do garbage collection */
293 for(i = 0; i< fixed->nummod; i++) {
294 tmp_header = (objheader_t *)ptr;
295 tmp_header->rcount = 1;
296 ptr += sizeof(objheader_t) + classsize[TYPE(tmp_header)];
298 /* Unlock objects that was locked due to this transaction */
299 for(i = 0; i< transinfo->numlocked; i++) {
300 header = mhashSearch(transinfo->objlocked[i]);// find the header address
301 STATUS(((objheader_t *)header)) &= ~(LOCK);
304 /* Send ack to Coordinator */
305 printf("DEBUG -> Recv TRANS_ABORT\n");
306 sendctrl = TRANS_SUCESSFUL;
307 if(send((int)acceptfd, &sendctrl, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
308 perror("Error sending ACK to coordinator\n");
315 /* Invoke the transCommit process() */
316 printf("DEBUG -> Recv TRANS_COMMIT \n");
317 if((val = transCommitProcess(transinfo, (int)acceptfd)) != 0) {
318 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
323 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
324 //TODO expect another transrequest from client
325 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING\n");
328 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
329 //TODO Use fixed.trans_id TID since Client may have died
333 printf("DEBUG -> Freeing...\n");
335 if (transinfo->objmod != NULL) {
336 free(transinfo->objmod);
337 transinfo->objmod = NULL;
339 if (transinfo->objlocked != NULL) {
340 free(transinfo->objlocked);
341 transinfo->objlocked = NULL;
343 if (transinfo->objnotfound != NULL) {
344 free(transinfo->objnotfound);
345 transinfo->objnotfound = NULL;
350 /* This function increments counters while running a voting decision on all objects involved
351 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
352 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
355 char control = 0, *ptr;
357 unsigned int *oidnotfound, *oidlocked, *oidmod;
359 objheader_t *headptr;
361 /* Counters and arrays to formulate decision on control message to be sent */
362 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
363 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
364 oidmod = (unsigned int *) calloc(fixed->nummod, sizeof(unsigned int));
365 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
366 int objmodnotfound = 0, nummodfound = 0;
368 /* modptr points to the beginning of the object store
369 * created at the Pariticipant.
370 * Object store holds the modified objects involved in the transaction request */
373 /* Process each oid in the machine pile/ group per thread */
374 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
375 if (i < fixed->numread) {//Objs only read and not modified
376 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
378 oid = *((unsigned int *)(objread + incr));
379 incr += sizeof(unsigned int);
380 version = *((short *)(objread + incr));
381 } else {//Objs modified
382 headptr = (objheader_t *) ptr;
384 oidmod[objmod] = oid;//Array containing modified oids
386 version = headptr->version;
387 ptr += sizeof(objheader_t) + classsize[TYPE(headptr)];
390 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
392 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
393 /* Save the oids not found and number of oids not found for later use */
394 //oidnotfound[objnotfound] = OID(((objheader_t *)mobj));
395 oidnotfound[objnotfound] = oid;
397 } else { /* If Obj found in machine (i.e. has not moved) */
398 /* Check if Obj is locked by any previous transaction */
399 if ((STATUS(((objheader_t *)mobj)) & LOCK) == LOCK) {
400 if (version == ((objheader_t *)mobj)->version) { /* If not locked then match versions */
402 } else {/* If versions don't match ...HARD ABORT */
404 /* Send TRANS_DISAGREE to Coordinator */
405 control = TRANS_DISAGREE;
406 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
407 perror("Error in sending control to the Coordinator\n");
410 printf("DEBUG -> Sending TRANS_DISAGREE\n");
413 } else {/* If Obj is not locked then lock object */
414 STATUS(((objheader_t *)mobj)) |= LOCK;
416 /*TESTING Add random wait to make transactions run for a long time such that
417 * we can test for soft abort case */
421 /* Save all object oids that are locked on this machine during this transaction request call */
422 oidlocked[objlocked] = OID(((objheader_t *)mobj));
424 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
426 } else { /* If versions don't match ...HARD ABORT */
428 control = TRANS_DISAGREE;
429 /* Send TRANS_DISAGREE to Coordinator */
430 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
431 perror("Error in sending control to the Coordinator\n");
434 printf("DEBUG -> Sending TRANS_DISAGREE\n");
441 /* Decide what control message to send to Coordinator */
442 if ((val = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
443 modptr, oidnotfound, oidlocked, oidmod, acceptfd)) == 0) {
444 printf("Error in decideCtrlMessage %s, %d\n", __FILE__, __LINE__);
451 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
452 * to send to Coordinator based on the votes of oids involved in the transaction */
453 int decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
454 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
455 unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidmod,
459 /* Condition to send TRANS_AGREE */
460 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
461 control = TRANS_AGREE;
462 /* Send control message */
463 if((val = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
464 perror("Error in sending control to Coordinator\n");
467 printf("DEBUG -> Sending TRANS_AGREE\n");
469 /* Condition to send TRANS_SOFT_ABORT */
470 if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
471 control = TRANS_SOFT_ABORT;
472 char msg[]={TRANS_SOFT_ABORT, 0,0,0,0};
473 *((int*)&msg[1])= *(objnotfound);
475 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
476 /* Send control message */
477 if((val = send(acceptfd, &msg, sizeof(msg),MSG_NOSIGNAL)) < sizeof(msg)) {
478 perror("Error in sending no of objects that are not found\n");
481 /* Send number of oids not found and the missing oids if objects are missing in the machine */
482 if(*(objnotfound) != 0) {
483 int size = sizeof(unsigned int)* *(objnotfound);
484 if((val = send(acceptfd, oidnotfound, size ,MSG_NOSIGNAL)) < size) {
485 perror("Error in sending objects that are not found\n");
491 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
492 * if Participant receives a TRANS_COMMIT */
493 transinfo->objmod = oidmod;
494 transinfo->objlocked = oidlocked;
495 transinfo->objnotfound = oidnotfound;
496 transinfo->modptr = modptr;
497 transinfo->nummod = fixed->nummod;
498 transinfo->numlocked = *(objlocked);
499 transinfo->numnotfound = *(objnotfound);
504 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
505 * addresses in lookup table and also changes version number
506 * Sends an ACK back to Coordinator */
507 int transCommitProcess(trans_commit_data_t *transinfo, int acceptfd) {
509 int i = 0, offset = 0;
511 /* Process each modified object saved in the mainobject store */
512 for(i=0; i<transinfo->nummod; i++) {
513 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
514 printf("mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
516 /* Change reference count of older address and free space in objstr ?? */
517 header->rcount = 1; //Not sure what would be the val
519 /* Change ptr address in mhash table */
520 printf("DEBUG -> removing object oid = %d\n", transinfo->objmod[i]);
521 mhashRemove(transinfo->objmod[i]);
522 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
523 offset += sizeof(objheader_t) + classsize[TYPE(header)];
525 /* Update object version number */
526 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
527 header->version += 1;
529 /* Unlock locked objects */
530 for(i=0; i<transinfo->numlocked; i++) {
531 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
532 STATUS(header) &= ~(LOCK);
535 //TODO Update location lookup table
537 /* Send ack to coordinator */
538 control = TRANS_SUCESSFUL;
539 printf("DEBUG-> TRANS_SUCESSFUL\n");
540 if(send((int)acceptfd, &control, sizeof(char), MSG_NOSIGNAL) < sizeof(char)) {
541 perror("Error sending ACK to coordinator\n");
547 int prefetchReq(int acceptfd) {
548 int i, length, sum, n, numbytes, numoffset, N, objnotfound = 0, size, count = 0;
549 unsigned int oid, index = 0;
550 char *ptr, buffer[PRE_BUF_SIZE];
553 char *header, control;
556 /* Repeatedly recv the oid and offset pairs sent for prefetch */
557 while(numbytes = recv((int)acceptfd, &length, sizeof(int), 0) != 0) {
562 index = sizeof(unsigned int); // Index starts with sizeof unsigned int because the
563 // first 4 bytes are saved to send the
564 // size of the buffer (that is computed at the end of the loop)
565 oid = recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
566 numoffset = (length - (sizeof(int) + sizeof(unsigned int)))/ sizeof(short);
567 N = numoffset * sizeof(short);
568 short offset[numoffset];
569 ptr = (char *)&offset;
570 /* Recv the offset values per oid */
572 n = recv((int)acceptfd, (void *)ptr+sum, N-sum, 0);
574 } while(sum < N && n != 0);
576 /* Process each oid */
577 /* Check if object is still present in the machine since the beginning of TRANS_PREFETCH */
578 if ((mobj = mhashSearch(oid)) == NULL) {/* Obj not found */
579 /* Save the oids not found in buffer for later use */
580 *(buffer + index) = OBJECT_NOT_FOUND;
581 index += sizeof(char);
582 memcpy(buffer+index, &oid, sizeof(unsigned int));
583 index += sizeof(unsigned int);
584 } else { /* If Obj found in machine (i.e. has not moved) */
585 /* send the oid, it's size, it's header and data */
586 header = (char *) mobj;
587 head = (objheader_t *) header;
588 size = sizeof(objheader_t) + sizeof(classsize[TYPE(head)]);
589 *(buffer + index) = OBJECT_FOUND;
590 index += sizeof(char);
591 memcpy(buffer+index, &oid, sizeof(unsigned int));
592 index += sizeof(unsigned int);
593 memcpy(buffer+index, &size, sizeof(int));
594 index += sizeof(int);
595 memcpy(buffer + index, header, size);
597 /* Calculate the oid corresponding to the offset value */
598 for(i = 0 ; i< numoffset ; i++) {
599 objoid = *((int *)(header + sizeof(objheader_t) + offset[i]));
600 if((header = (char *) mhashSearch(objoid)) == NULL) {
601 /* Obj not found, send oid */
602 *(buffer + index) = OBJECT_NOT_FOUND;
603 index += sizeof(char);
604 memcpy(buffer+index, &oid, sizeof(unsigned int));
605 index += sizeof(unsigned int);
607 } else {/* Obj Found */
608 /* send the oid, it's size, it's header and data */
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);
623 /* Check for overflow in the buffer */
624 if (index >= PRE_BUF_SIZE) {
625 printf("Char buffer is overflowing\n");
628 /* Send Prefetch response control message only once*/
630 control = TRANS_PREFETCH_RESPONSE;
631 if((numbytes = send(acceptfd, &control, sizeof(char), MSG_NOSIGNAL)) < sizeof(char)) {
632 perror("Error in sending PREFETCH RESPONSE to Coordinator\n");
637 /* Add the buffer size into buffer as a parameter */
638 memcpy(buffer, &index, sizeof(unsigned int));
639 /* Send the entire buffer with its size and oids found and not found */
640 if(send((int)acceptfd, &buffer, sizeof(index - 1), MSG_NOSIGNAL) < sizeof(index -1)) {
641 perror("Error sending oids found\n");