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");
312 //This function runs a decision after all objects are weighed under one of the 4 possibilities
313 //and returns the appropriate control message to the Ccordinator
314 char handleTransReq(int acceptfd, fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr) {
316 char control = 0, ctrlmissoid, *ptr;
319 unsigned int *oidnotfound, *oidlocked, *oidmod;
321 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
322 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
323 oidmod = (unsigned int *) calloc(fixed->nummod, sizeof(unsigned int));
325 // Counters and arrays to formulate decision on control message to be sent
326 int objnotfound = 0, objlocked = 0, objmod =0, v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
327 int objmodnotfound = 0, nummodfound = 0;
329 objheader_t *headptr;
331 //Process each object present in the pile
333 //Process each oid in the machine pile/ group
334 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
335 if (i < fixed->numread) {//Object is read
336 int incr = sizeof(unsigned int) + sizeof(short);// Offset that points to next position in the objread array
338 oid = *((unsigned int *)(objread + incr));
339 incr += sizeof(unsigned int);
340 version = *((short *)(objread + incr));
341 } else {//Obj is modified
342 headptr = (objheader_t *) ptr;
344 oidmod[objmod] = oid;//Array containing modified oids
346 version = headptr->version;
347 ptr += sizeof(objheader_t) + classsize[headptr->type];
349 //Check if object is still present in the machine since the beginning of TRANS_REQUEST
350 if ((mobj = mhashSearch(oid)) == NULL) {// Obj not found
351 //Save the oids not found for later use
352 oidnotfound[objnotfound] = ((objheader_t *)mobj)->oid;
354 } else { // If obj found in machine (i.e. has not moved)
355 //Check if obj is locked
356 if ((((objheader_t *)mobj)->status & LOCK) == LOCK) {
357 if (version == ((objheader_t *)mobj)->version) { // If version match
359 } else {//If versions don't match ..HARD ABORT
361 //send TRANS_DISAGREE to Coordinator
362 control = TRANS_DISAGREE;
363 write(acceptfd, &control, sizeof(char));
364 printf("DEBUG -> Sending TRANS_DISAGREE\n");
367 } else {//Obj is not locked , so lock object
368 ((objheader_t *)mobj)->status |= LOCK;
369 //Save all object oids that are locked on this machine during this transaction request call
370 oidlocked[objlocked] = ((objheader_t *)mobj)->oid;
372 if (version == ((objheader_t *)mobj)->version) { //If versions match
374 } else { //If versions don't match
376 //send TRANS_DISAGREE to Coordinator
377 control = TRANS_DISAGREE;
378 write(acceptfd, &control, sizeof(char));
379 printf("DEBUG -> Sending TRANS_DISAGREE\n");
386 printf("No of objs locked = %d\n", objlocked);
387 printf("No of v_nomatch = %d\n", v_nomatch);
388 printf("No of objs v_match but are did not have locks before = %d\n", v_matchnolock);
389 printf("No of objs v_match but had locks before = %d\n", v_matchlock);
390 printf("No of objs not found = %d\n", objnotfound);
391 printf("No of objs modified but not found = %d\n", objmodnotfound);
393 //Decide what control message(s) to send
394 if(v_matchnolock == fixed->numread + fixed->nummod) {
395 //send TRANS_AGREE to Coordinator
396 control = TRANS_AGREE;
397 write(acceptfd, &control, sizeof(char));
398 printf("DEBUG -> Sending TRANS_AGREE\n");
401 if((v_matchlock > 0 && v_nomatch == 0) || (objnotfound > 0 && v_nomatch == 0)) {
402 //send TRANS_SOFT_ABORT to Coordinator
403 control = TRANS_SOFT_ABORT;
404 write(acceptfd, &control, sizeof(char));
405 printf("DEBUG -> Sending TRANS_SOFT_ABORT\n");
406 //send number of oids not found and the missing oids
407 write(acceptfd, &objnotfound, sizeof(int));
409 write(acceptfd, oidnotfound, (sizeof(unsigned int) * objnotfound));
412 //Do the following when TRANS_DISAGREE is sent
413 if(control == TRANS_DISAGREE) {
414 //Set the reference count of the object to 1 in mainstore for garbage collection
416 for(i = 0; i< fixed->nummod; i++) {
417 headptr = (objheader_t *) ptr;
419 ptr += sizeof(objheader_t) + classsize[headptr->type];
421 //Unlock objects that was locked in the trans
422 for(i = 0; i< objlocked ; i++) {
423 mobj = mhashSearch(oidlocked[i]);// find the header address
424 ((objheader_t *)mobj)->status &= ~(LOCK);
428 //Fill out the structure required for a trans commit process if pile receives a TRANS_COMMIT
429 transinfo->objmod = oidmod;
430 transinfo->objlocked = oidlocked;
431 transinfo->objnotfound = oidnotfound;
432 transinfo->modptr = modptr;
433 transinfo->nummod = fixed->nummod;
434 transinfo->numlocked = objlocked;
435 transinfo->numnotfound = objnotfound;
440 //Processes oids in the TRANS_COMMIT request at the participant end and sends an ack back
441 int transCommitProcess(trans_commit_data_t *transinfo, int acceptfd) {
443 int i = 0, offset = 0;
445 //Process each modified object saved in the mainobject store
446 for(i=0; i<transinfo->nummod; i++) {
447 if((header = (objheader_t *) mhashSearch(transinfo->objmod[i])) == NULL) {
448 printf("mhashserach returns NULL\n");
450 //change reference count of older address and free space in objstr ??
451 header->rcount = 1; //Not sure what would be th val
452 //change ptr address in mhash table
453 mhashRemove(transinfo->objmod[i]);
454 mhashInsert(transinfo->objmod[i], (transinfo->modptr + offset));
455 offset += sizeof(objheader_t) + classsize[header->type];
456 //update object version
457 header = (objheader_t *) mhashSearch(transinfo->objmod[i]);
458 header->version += 1;
460 for(i=0; i<transinfo->numlocked; i++) {
462 header = (objheader_t *) mhashSearch(transinfo->objlocked[i]);
463 header->status &= ~(LOCK);
466 //TODO Update location lookup table
468 //send ack to coordinator
469 control = TRANS_SUCESSFUL;
470 if(send((int)acceptfd, &control, sizeof(char), 0) < 0) {
471 perror("Error sending ACK to coordinator\n");