11 #define LISTEN_PORT 2156
12 #define BACKLOG 10 //max pending connections
13 #define RECEIVE_BUFFER_SIZE 2048
15 extern int classsize[];
17 objstr_t *mainobjstore;
21 //Initialize main object store
22 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
23 if (mhashCreate(HASH_SIZE, LOADFACTOR))
26 if (lhashCreate(HASH_SIZE, LOADFACTOR))
29 //pthread_t threadListen;
30 //pthread_create(&threadListen, NULL, dstmListen, NULL);
37 int listenfd, acceptfd;
38 struct sockaddr_in my_addr;
39 struct sockaddr_in client_addr;
40 socklen_t addrlength = sizeof(struct sockaddr);
41 pthread_t thread_dstm_accept;
44 listenfd = socket(AF_INET, SOCK_STREAM, 0);
51 my_addr.sin_family = AF_INET;
52 my_addr.sin_port = htons(LISTEN_PORT);
53 my_addr.sin_addr.s_addr = INADDR_ANY;
54 memset(&(my_addr.sin_zero), '\0', 8);
56 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1)
62 if (listen(listenfd, BACKLOG) == -1)
68 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
71 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
72 pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
77 void *dstmAccept(void *acceptfd)
81 char buffer[RECEIVE_BUFFER_SIZE], control,ctrl;
85 trans_commit_data_t transinfo;
87 int fd_flags = fcntl((int)acceptfd, F_GETFD), size;
89 printf("Recieved connection: fd = %d\n", (int)acceptfd);
90 recv((int)acceptfd, &control, sizeof(char), 0);
93 printf("DEBUG -> Recv READ_REQUEST from Coordinator\n");
94 recv((int)acceptfd, &oid, sizeof(unsigned int), 0);
95 srcObj = mhashSearch(oid);
96 h = (objheader_t *) srcObj;
97 size = sizeof(objheader_t) + sizeof(classsize[h->type]);
99 ctrl = OBJECT_NOT_FOUND;
100 if(send((int)acceptfd, &ctrl, sizeof(char), 0) < 0) {
101 perror("Error sending control msg to coordinator\n");
104 //char responsemessage[sizeof(char)+sizeof(int)];
107 if(send((int)acceptfd, &ctrl, sizeof(char), 0) < 0) {
108 perror("Error sending control msg to coordinator\n");
111 //responsemessage[0]=OBJECT_FOUND;
113 //*((int *)(&responsemessage[1])) = sizeof(objheader_t) + classsize[h->type];
114 //if(send((int)acceptfd, &responsemessage, sizeof(responsemessage), 0) < 0) {
115 // perror("Error sending control msg to coordinator\n");
119 if(send((int)acceptfd, &size, sizeof(int), 0) < 0) {
120 perror("Error sending size of object to coordinator\n");
122 if(send((int)acceptfd, h, size, 0) < 0) {
123 perror("Error in sending object\n");
128 case READ_MULT_REQUEST:
129 printf("DEBUG-> READ_MULT_REQUEST\n");
133 printf("DEBUG -> MOVE_REQUEST\n");
136 case MOVE_MULT_REQUEST:
137 printf("DEBUG -> MOVE_MULT_REQUEST\n");
141 printf("DEBUG -> Recv TRANS_REQUEST from Coordinator\n");
142 if((val = readClientReq((int)acceptfd, &transinfo)) != 0) {
143 printf("Error in readClientReq\n");
148 printf("Error receiving\n");
150 if (close((int)acceptfd) == -1)
155 printf("Closed connection: fd = %d\n", (int)acceptfd);
158 free(transinfo.objmod);
159 free(transinfo.objlocked);
160 free(transinfo.objnotfound);
164 int readClientReq(int acceptfd, trans_commit_data_t *transinfo) {
165 char *ptr, control, prevctrl, sendctrl, newctrl;
166 void *modptr, *header;
167 objheader_t *tmp_header;
169 int sum = 0, i, N, n, val;
171 //Reads to process the TRANS_REQUEST protocol further
173 N = sizeof(fixed) - 1;
174 ptr = (char *)&fixed;;
175 fixed.control = TRANS_REQUEST;
177 n = recv((int)acceptfd, (void *) ptr+1+sum, N-sum, 0);
178 // printf("DEBUG -> 1. Reading %d bytes \n", n);
180 } while(sum < N && n != 0);
182 //printf("Machine count = %d\tnumread = %d\tnummod = %d\tsum_bytes = %d\n", fixed.mcount, fixed.numread, fixed.nummod, fixed.sum_bytes);
184 int mcount = fixed.mcount;
185 N = mcount * sizeof(unsigned int);
186 unsigned int listmid[mcount];
187 ptr = (char *) listmid;
190 n = recv((int)acceptfd, (void *) ptr+sum, N-sum, 0);
191 // printf("DEBUG -> 2. Reading %d bytes cap = %d\n", n, N);
193 } while(sum < N && n != 0);
195 // Read oid and version tuples
196 int numread = fixed.numread;
197 N = numread * (sizeof(unsigned int) + sizeof(short));
201 n = recv((int)acceptfd, (void *) objread, N, 0);
202 // printf("DEBUG -> 3. Reading %d bytes cap = %d\n", n, N);
204 } while(sum < N && n != 0);
205 //printf("DEBUG -> %d %d %d %d\n", *objread, *(objread + 6), *(objread + 12), *(objread + 18));
207 // Read modified objects
208 if ((modptr = objstrAlloc(mainobjstore, fixed.sum_bytes)) == NULL) {
209 printf("objstrAlloc error for modified objects %s, %d", __FILE__, __LINE__);
214 n = recv((int)acceptfd, modptr+sum, fixed.sum_bytes-sum, 0);
215 //printf("DEBUG -> 4. Reading %d bytes cap = %d, oid = %d\n", n, fixed.sum_bytes, *((int *)modptr));
217 } while (sum < fixed.sum_bytes && n != 0);
219 //Send control message as per all votes from all oids in the machine
220 if((prevctrl = handleTransReq(acceptfd, &fixed, transinfo, listmid, objread, modptr)) == 0 ) {
221 printf("Handle req error\n");
224 //Read for new control message from Coordiator
225 recv((int)acceptfd, &control, sizeof(char), 0);
228 printf("DEBUG -> Recv TRANS_ABORT from Coordinator\n");
229 //send ack to coordinator
230 sendctrl = TRANS_SUCESSFUL;
231 if(send((int)acceptfd, &sendctrl, sizeof(char), 0) < 0) {
232 perror("Error sending ACK to coordinator\n");
234 //Mark all ref counts as 1 and do garbage collection
236 for(i = 0; i< fixed.nummod; i++) {
237 tmp_header = (objheader_t *)ptr;
238 tmp_header->rcount = 1;
239 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
241 //Unlock objects that was locked in this machine due to this transaction
242 for(i = 0; i< transinfo->numlocked; i++) {
243 header = mhashSearch(transinfo->objlocked[i]);// find the header address
244 ((objheader_t *)header)->status &= ~(LOCK);
250 printf("DEBUG -> Recv TRANS_COMMIT from Coordinator\n");
251 if((val = transCommitProcess(transinfo, (int)acceptfd)) != 0) {
252 printf("Error in transCommitProcess %s, %d\n", __FILE__, __LINE__);
255 case TRANS_ABORT_BUT_RETRY_COMMIT:
256 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT from Coordinator\n");
257 //Process again after waiting for sometime and on prev control message sent
260 sendctrl = TRANS_AGREE;
261 if(send((int)acceptfd, &sendctrl, sizeof(char), 0) < 0) {
262 perror("Error sending ACK to coordinator\n");
266 case TRANS_SOFT_ABORT:
267 if((newctrl = handleTransReq(acceptfd, &fixed, transinfo, listmid, objread, modptr)) == 0 ) {
268 printf("Handle req error\n");
270 if(newctrl == prevctrl){
272 newctrl = TRANS_DISAGREE;
273 if(send((int)acceptfd, &newctrl, sizeof(char), 0) < 0) {
274 perror("Error sending ACK to coordinator\n");
276 //Set the reference count of the object to 1 in mainstore for garbage collection
278 for(i = 0; i< fixed.nummod; i++) {
279 tmp_header = (objheader_t *) ptr;
280 tmp_header->rcount = 1;
281 ptr += sizeof(objheader_t) + classsize[tmp_header->type];
283 //Unlock objects that was locked in this machine due to this transaction
284 for(i = 0; i< transinfo->numlocked; i++) {
285 ptr = mhashSearch(transinfo->objlocked[i]);// find the header address
286 ((objheader_t *)ptr)->status &= ~(LOCK);
290 //Send new control message
291 if(send((int)acceptfd, &newctrl, sizeof(char), 0) < 0) {
292 perror("Error sending ACK to coordinator\n");
300 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
301 //TODO expect another transrequest from client
302 printf("DEBUG -> Recv TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING from Coordinator\n");
305 printf("No response to TRANS_AGREE OR DISAGREE protocol\n");
306 //TODO Use fixed.trans_id TID since Client may have died
313 //This function runs a decision after all objects are weighed under one of the 4 possibilities
314 //and returns the appropriate control message to the Ccordinator
315 char handleTransReq(int acceptfd, fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr) {
317 char control = 0, ctrlmissoid, *ptr;
320 unsigned int *oidnotfound, *oidlocked, *oidmod;
322 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
323 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
324 oidmod = (unsigned int *) calloc(fixed->nummod, sizeof(unsigned int));
326 // Counters and arrays to formulate decision on control message to be sent
327 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
328 int objmodnotfound = 0, nummodfound = 0;
330 objheader_t *headptr;
332 //Process each object present in the pile
334 printf("DEBUG -> Total objs involved in trans is %d\n",fixed->nummod + fixed->numread);
336 //Process each oid in the machine pile/ group
337 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
338 if (i < fixed->numread) {//Object is read
339 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
341 oid = *((unsigned int *)(objread + incr));
342 incr += sizeof(unsigned int);
343 version = *((short *)(objread + incr));
344 } else {//Obj is modified
345 headptr = (objheader_t *) ptr;
347 oidmod[objmod] = oid;//Array containing modified oids
349 version = headptr->version;
350 ptr += sizeof(objheader_t) + classsize[headptr->type];
352 //Check if object is still present in the machine since the beginning of TRANS_REQUEST
353 if ((mobj = mhashSearch(oid)) == NULL) {// Obj not found
354 //Save the oids not found for later use
355 oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
357 } else { // If obj found in machine (i.e. has not moved)
358 //Check if obj is locked
359 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
360 if (version == ((objheader_t *)mobj)->version) { // If version match
362 } else {//If versions don't match ..HARD ABORT
364 //send TRANS_DISAGREE to Coordinator
365 control = TRANS_DISAGREE;
366 write(acceptfd, &control, sizeof(char));
367 printf("DEBUG -> Sending TRANS_DISAGREE\n");
370 } else {//Obj is not locked , so lock object
371 ((objheader_t *)mobj)->status |= LOCK;
372 //Save all object oids that are locked on this machine during this transaction request call
373 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
374 printf("DEBUG-> Object to be locked is %d\n", ((objheader_t *)mobj)->oid);
376 if (version == ((objheader_t *)mobj)->version) { //If versions match
378 } else { //If versions don't match
380 //send TRANS_DISAGREE to Coordinator
381 control = TRANS_DISAGREE;
382 write(acceptfd, &control, sizeof(char));
383 printf("DEBUG -> Sending TRANS_DISAGREE\n");
390 printf("No of objs locked = %d\n", objlocked);
391 printf("No of v_nomatch = %d\n", v_nomatch);
392 printf("No of objs v_match but are did not have locks before = %d\n", v_matchnolock);
393 printf("No of objs v_match but had locks before = %d\n", v_matchlock);
394 printf("No of objs not found = %d\n", objnotfound);
395 printf("No of objs modified but not found = %d\n", objmodnotfound);
397 //Decide what control message(s) to send
398 if(v_matchnolock == fixed->numread + fixed->nummod) {
399 //send TRANS_AGREE to Coordinator
400 control = TRANS_AGREE;
401 write(acceptfd, &control, sizeof(char));
402 printf("DEBUG -> Sending TRANS_AGREE\n");
405 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
406 //send TRANS_SOFT_ABORT to Coordinator
407 control = TRANS_SOFT_ABORT;
408 write(acceptfd, &control, sizeof(char));
409 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
410 //send number of oids not found and the missing oids
411 write(acceptfd, &objnotfound, sizeof(int));
413 write(acceptfd, oidnotfound, (sizeof(unsigned int) * objnotfound));
416 //Do the following when TRANS_DISAGREE is sent
417 if(control == TRANS_DISAGREE) {
418 //Set the reference count of the object to 1 in mainstore for garbage collection
420 for(i = 0; i< fixed->nummod; i++) {
421 headptr = (objheader_t *) ptr;
423 ptr += sizeof(objheader_t) + classsize[headptr->type];
425 //Unlock objects that was locked in the trans
426 for(i = 0; i< objlocked ; i++) {
427 mobj = mhashSearch(oidlocked[i]);// find the header address
428 ((objheader_t *)mobj)->status &= ~(LOCK);
432 //Fill out the structure required for a trans commit process if pile receives a TRANS_COMMIT
433 transinfo->objmod = oidmod;
434 transinfo->objlocked = oidlocked;
435 transinfo->objnotfound = oidnotfound;
436 transinfo->modptr = modptr;
437 transinfo->nummod = fixed->nummod;
438 transinfo->numlocked = objlocked;
439 transinfo->numnotfound = objnotfound;
444 //Processes oids in the TRANS_COMMIT request at the participant end and sends an ack back
445 int transCommitProcess(trans_commit_data_t *transinfo, int acceptfd) {
447 int i = 0, offset = 0;
449 //Process each modified object saved in the mainobject store
450 for(i=0; i<transinfo->nummod; i++) {
451 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
452 printf("mhashserach returns NULL\n");
454 //change reference count of older address and free space in objstr ??
455 header->rcount = 1; //Not sure what would be th val
456 //change ptr address in mhash table
457 mhashRemove(transinfo->objmod[i]);
458 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
459 offset += sizeof(objheader_t) + classsize[header->type];
460 //update object version
461 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
462 header->version += 1;
464 for(i=0; i<transinfo->numlocked; i++) {
466 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
467 header->status &= ~(LOCK);
470 //TODO Update location lookup table
472 //send ack to coordinator
473 control = TRANS_SUCESSFUL;
474 if(send((int)acceptfd, &control, sizeof(char), 0) < 0) {
475 perror("Error sending ACK to coordinator\n");