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 */
4 #include <netinet/tcp.h>
9 #include "threadnotify.h"
23 #define BACKLOG 10 //max pending connections
24 #define RECEIVE_BUFFER_SIZE 2048
26 extern int classsize[];
27 extern int numHostsInSystem;
28 extern pthread_mutex_t notifymutex;
30 extern unsigned int myIpAddr;
31 extern unsigned int *hostIpAddrs;
34 extern unsigned int *locateObjHosts;
35 extern int *liveHosts;
36 extern int liveHostsValid;
37 extern int numLiveHostsInSystem;
38 int clearNotifyListFlag;
41 objstr_t *mainobjstore;
42 pthread_mutex_t mainobjstore_mutex;
43 pthread_mutex_t lockObjHeader;
44 pthread_mutex_t clearNotifyList_mutex;
45 pthread_mutexattr_t mainobjstore_mutex_attr; /* Attribute for lock to make it a recursive lock */
47 sockPoolHashTable_t *transPResponseSocketPool;
48 extern sockPoolHashTable_t *transRequestSockPool;
49 extern sockPoolHashTable_t *transReadSockPool;
51 int failFlag = 0; //debug
54 /******************************
55 * Global variables for Paxos
56 ******************************/
58 extern unsigned int v_a;
62 extern int paxosRound;
63 /* This function initializes the main objects store and creates the
64 * global machine and location lookup table */
68 mainobjstore = objstrCreate(DEFAULT_OBJ_STORE_SIZE);
69 /* Initialize attribute for mutex */
70 pthread_mutexattr_init(&mainobjstore_mutex_attr);
71 pthread_mutexattr_settype(&mainobjstore_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
72 pthread_mutex_init(&mainobjstore_mutex, &mainobjstore_mutex_attr);
73 pthread_mutex_init(&lockObjHeader,NULL);
76 pthread_mutex_init(&liveHosts_mutex, NULL);
77 pthread_mutex_init(&leaderFixing_mutex, NULL);
78 pthread_mutex_init(&clearNotifyList_mutex,NULL);
81 if (mhashCreate(MHASH_SIZE, MLOADFACTOR))
84 if (lhashCreate(HASH_SIZE, LOADFACTOR))
88 if (thashCreate(THASH_SIZE, LOADFACTOR))
92 if (notifyhashCreate(N_HASH_SIZE, N_LOADFACTOR))
95 //Initialize socket pool
96 if((transPResponseSocketPool = createSockPool(transPResponseSocketPool, DEFAULTSOCKPOOLSIZE)) == NULL) {
97 printf("Error in creating new socket pool at %s line %d\n", __FILE__, __LINE__);
104 int startlistening() {
106 struct sockaddr_in my_addr;
107 socklen_t addrlength = sizeof(struct sockaddr);
110 listenfd = socket(AF_INET, SOCK_STREAM, 0);
111 if (listenfd == -1) {
116 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &setsockflag, sizeof (setsockflag)) < 0) {
121 if (setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &setsockflag, sizeof (setsockflag)) < 0) {
127 my_addr.sin_family = AF_INET;
128 my_addr.sin_port = htons(LISTEN_PORT);
129 my_addr.sin_addr.s_addr = INADDR_ANY;
130 memset(&(my_addr.sin_zero), '\0', 8);
132 if (bind(listenfd, (struct sockaddr *)&my_addr, addrlength) == -1) {
137 if (listen(listenfd, BACKLOG) == -1) {
144 /* This function starts the thread to listen on a socket
145 * for tranaction calls */
146 void *dstmListen(void *lfd) {
147 int listenfd=(int)lfd;
149 struct sockaddr_in client_addr;
150 socklen_t addrlength = sizeof(struct sockaddr);
151 pthread_t thread_dstm_accept;
155 pthread_t thread_dstm_asking;
158 printf("Listening on port %d, fd = %d\n", LISTEN_PORT, listenfd);
163 acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlength);
168 retval = pthread_create(&thread_dstm_asking, NULL, startAsking, NULL);
171 pthread_detach(thread_dstm_asking);
175 printf("%s -> fd accepted\n",__func__);
178 setsockopt(acceptfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
180 retval=pthread_create(&thread_dstm_accept, NULL, dstmAccept, (void *)acceptfd);
182 pthread_detach(thread_dstm_accept);
189 unsigned int deadMachineIndex = -1;
195 printf("%s -> Entering\n",__func__);
198 socklist = (int*) calloc(numHostsInSystem,sizeof(int));
200 for(i = 0; i< numHostsInSystem;i++) { // for 1
201 if((sd = getSockWithLock(transPResponseSocketPool,hostIpAddrs[i])) < 0) {
202 printf("%s -> Cannot create socket connection to [%s]\n",__func__,midtoIPString(hostIpAddrs[i]));
212 deadMachineIndex = checkIfAnyMachineDead(socklist);
214 // free socket of dead machine
215 if(deadMachineIndex >= 0) { // if 2
217 printf("%s -> Dead Machine : %s\n",__func__, midtoIPString(hostIpAddrs[deadMachineIndex]));
219 restoreDuplicationState(hostIpAddrs[deadMachineIndex]);
220 freeSockWithLock(transPResponseSocketPool, hostIpAddrs[deadMachineIndex], socklist[deadMachineIndex]);
221 socklist[deadMachineIndex] = -1;
225 printf("%s -> Exiting\n",__func__);
230 unsigned int checkIfAnyMachineDead(int* socklist)
234 char control = RESPOND_LIVE;
237 printf("%s -> Entering\n",__func__);
241 for(i = 0; i< numHostsInSystem;i++) {
243 printf("%s -> socklist[%d] = %d\n",__func__,i,socklist[i]);
245 if(socklist[i] > 0) {
246 send_data(socklist[i], &control,sizeof(char));
248 if(recv_data(socklist[i], &response, sizeof(char)) < 0) {
249 // if machine is dead, returns index of socket
251 printf("%s -> Machine dead detecteed\n",__func__);
257 if(response != LIVE) {
259 printf("%s -> Machine dead detected\n",__func__);
264 }// end if(socklist[i]
267 clearDeadThreadsNotification();
269 sleep(numLiveHostsInSystem); // wait for seconds for next checking
275 /* This function accepts a new connection request, decodes the control message in the connection
276 * and accordingly calls other functions to process new requests */
277 void *dstmAccept(void *acceptfd) {
278 int val, retval, size, sum, sockid, sd = 0;
281 char control,ctrl, response;
287 trans_commit_data_t transinfo;
288 unsigned short objType, *versionarry, version;
289 unsigned int *oidarry, numoid, mid, threadid;
291 unsigned int transIDreceived;
293 struct sockaddr_in remoteAddr;
296 printf("%s-> Entering dstmAccept\n", __func__); fflush(stdout);
298 /* Receive control messages from other machines */
300 int ret=recv_data_errorcode((int)acceptfd, &control, sizeof(char));
307 printf("DEBUG -> RECV Error!.. retrying\n");
313 printf("%s-> dstmAccept control = %d\n", __func__, (int)control);
318 printf("control -> READ_REQUEST\n");
320 /* Read oid requested and search if available */
321 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
322 while((srcObj = mhashSearch(oid)) == NULL) {
324 if((ret = sched_yield()) != 0) {
325 printf("%s(): error no %d in thread yield\n", __func__, errno);
328 h = (objheader_t *) srcObj;
330 size += sizeof(objheader_t);
331 sockid = (int) acceptfd;
333 ctrl = OBJECT_NOT_FOUND;
334 send_data(sockid, &ctrl, sizeof(char));
337 char msg[]={OBJECT_FOUND, 0, 0, 0, 0};
338 *((int *)&msg[1])=size;
339 send_data(sockid, &msg, sizeof(msg));
340 send_data(sockid, h, size);
344 case READ_MULT_REQUEST:
350 case MOVE_MULT_REQUEST:
355 printf("control -> TRANS_REQUEST\n");
357 /* Read transaction request */
358 transinfo.objlocked = NULL;
359 transinfo.objnotfound = NULL;
360 transinfo.modptr = NULL;
361 transinfo.numlocked = 0;
362 transinfo.numnotfound = 0;
363 if((val = readClientReq(&transinfo, (int)acceptfd)) != 0) {
364 printf("Error: In readClientReq() %s, %d\n", __FILE__, __LINE__);
371 recv_data((int)acceptfd, &transIDreceived, sizeof(unsigned int));
373 decision = checkDecision(transIDreceived);
375 send_data((int)acceptfd,&decision,sizeof(char));
381 printf("control -> TRANS_PREFETCH\n");
384 if((val = rangePrefetchReq((int)acceptfd)) != 0) {
385 printf("Error: In rangePrefetchReq() %s, %d\n", __FILE__, __LINE__);
389 if((val = prefetchReq((int)acceptfd)) != 0) {
390 printf("Error: In prefetchReq() %s, %d\n", __FILE__, __LINE__);
396 case TRANS_PREFETCH_RESPONSE:
398 printf("control -> TRANS_PREFETCH_RESPONSE\n");
401 if((val = getRangePrefetchResponse((int)acceptfd)) != 0) {
402 printf("Error: In getRangePrefetchRespose() %s, %d\n", __FILE__, __LINE__);
406 if((val = getPrefetchResponse((int) acceptfd)) != 0) {
407 printf("Error: In getPrefetchResponse() %s, %d\n", __FILE__, __LINE__);
413 case START_REMOTE_THREAD:
415 printf("control -> START_REMOTE_THREAD\n");
417 recv_data((int)acceptfd, &oid, sizeof(unsigned int));
418 objType = getObjType(oid);
419 startDSMthread(oid, objType);
422 case THREAD_NOTIFY_REQUEST:
424 printf("control -> THREAD_NOTIFY_REQUEST FD : %d\n",acceptfd);
427 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
429 size = (sizeof(unsigned int) + sizeof(unsigned short)) * numoid + 2 * sizeof(unsigned int);
430 if((buffer = calloc(1,size)) == NULL) {
431 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
435 recv_data((int)acceptfd, buffer, size);
437 oidarry = calloc(numoid, sizeof(unsigned int));
438 memcpy(oidarry, buffer, sizeof(unsigned int) * numoid);
439 size = sizeof(unsigned int) * numoid;
440 versionarry = calloc(numoid, sizeof(unsigned short));
441 memcpy(versionarry, buffer+size, sizeof(unsigned short) * numoid);
442 size += sizeof(unsigned short) * numoid;
443 mid = *((unsigned int *)(buffer+size));
444 size += sizeof(unsigned int);
445 threadid = *((unsigned int *)(buffer+size));
446 processReqNotify(numoid, oidarry, versionarry, mid, threadid);
451 case THREAD_NOTIFY_RESPONSE:
453 printf("control -> THREAD_NOTIFY_RESPONSE\n");
455 size = sizeof(unsigned short) + 2 * sizeof(unsigned int);
456 if((buffer = calloc(1,size)) == NULL) {
457 printf("%s() Calloc error at %s, %d\n", __func__, __FILE__, __LINE__);
461 recv_data((int)acceptfd, buffer, size);
464 oid = *((unsigned int *)buffer);
465 size = sizeof(unsigned int);
466 version = *((unsigned short *)(buffer+size));
467 size += sizeof(unsigned short);
468 threadid = *((unsigned int *)(buffer+size));
469 threadNotify(oid,version,threadid);
473 case CLEAR_NOTIFY_LIST:
475 printf("control -> CLEAR_NOTIFY_LIST\n");
477 size = sizeof(unsigned int);
478 if((buffer = calloc(1,size)) == NULL) {
479 printf("%s() Caclloc error at CLEAR_NOTIFY_LIST\n");
483 recv_data((int)acceptfd,buffer, size);
485 oid = *((unsigned int *)buffer);
487 pthread_mutex_lock(&clearNotifyList_mutex);
488 if(clearNotifyListFlag == 0) {
489 clearNotifyListFlag = 1;
490 pthread_mutex_unlock(&clearNotifyList_mutex);
491 clearNotifyList(oid);
494 pthread_mutex_unlock(&clearNotifyList_mutex);
499 case CLOSE_CONNECTION:
501 printf("control -> CLOSE_CONNECTION\n");
503 goto closeconnection;
508 printf("control -> RESPOND_LIVE\n");
512 send_data((int)acceptfd, &ctrl, sizeof(ctrl));
514 printf("%s (RESPOND_LIVE)-> Sending LIVE!\n", __func__);
519 case REMOTE_RESTORE_DUPLICATED_STATE:
521 printf("control -> REMOTE_RESTORE_DUPLICATED_STATE\n");
523 recv_data((int)acceptfd, &mid, sizeof(unsigned int));
524 if(!liveHosts[findHost(mid)]) {
526 printf("%s (REMOTE_RESTORE_DUPLICATED_STATE) -> already fixed\n",__func__);
530 pthread_mutex_lock(&leaderFixing_mutex);
533 pthread_mutex_unlock(&leaderFixing_mutex);
536 duplicateLostObjects(mid);
537 if(updateLiveHostsCommit() != 0) {
538 printf("error updateLiveHostsCommit()\n");
543 pthread_mutex_lock(&leaderFixing_mutex);
545 pthread_mutex_unlock(&leaderFixing_mutex);
548 pthread_mutex_unlock(&leaderFixing_mutex);
550 printf("%s (REMOTE_RESTORE_DUPLICATED_STATE -> LEADER is already fixing\n",__func__);
557 case UPDATE_LIVE_HOSTS:
559 printf("control -> UPDATE_LIVE_HOSTS\n");
562 pthread_mutex_lock(&liveHosts_mutex);
563 recv_data((int)acceptfd, liveHosts, sizeof(int)*numHostsInSystem);
564 recv_data((int)acceptfd, locateObjHosts, sizeof(unsigned int)*numHostsInSystem*2);
565 pthread_mutex_unlock(&liveHosts_mutex);
567 numLiveHostsInSystem = getNumLiveHostsInSystem();
570 printf("%s (UPDATE_LIVE_HOSTS)-> Finished\n", __func__);
577 case DUPLICATE_ORIGINAL:
579 printf("control -> DUPLICATE_ORIGINAL\n");
580 printf("%s (DUPLICATE_ORIGINAL)-> Attempt to duplicate original objects\n", __func__);
582 //object store stuffffff
583 recv_data((int)acceptfd, &mid, sizeof(unsigned int));
584 tempsize = mhashGetDuplicate(&dupeptr, 0);
586 //send control and dupes after
587 ctrl = RECEIVE_DUPES;
589 if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
590 perror("ORIGINAL : ");
594 bzero(&remoteAddr, sizeof(remoteAddr));
595 remoteAddr.sin_family = AF_INET;
596 remoteAddr.sin_port = htons(LISTEN_PORT);
597 remoteAddr.sin_addr.s_addr = htonl(mid);
599 if(connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr))<0) {
600 printf("ORIGINAL ERROR : %s\n",strerror(errno));
604 send_data(sd, &ctrl, sizeof(char));
605 send_data(sd, dupeptr, tempsize);
607 recv_data(sd, &response, sizeof(char));
609 printf("%s ->response : %d - %d\n",__func__,response,DUPLICATION_COMPLETE);
611 if(response != DUPLICATION_COMPLETE) {
613 printf("%s(DUPLICATION_ORIGINAL) -> DUPLICATION FAIL\n",__func__);
623 ctrl = DUPLICATION_COMPLETE;
624 send_data((int)acceptfd, &ctrl, sizeof(char));
626 printf("%s (DUPLICATE_ORIGINAL)-> Finished\n", __func__);
630 case DUPLICATE_BACKUP:
632 printf("control -> DUPLICATE_BACKUP\n");
633 printf("%s (DUPLICATE_BACKUP)-> Attempt to duplicate backup objects\n", __func__);
635 //object store stuffffff
636 recv_data((int)acceptfd, &mid, sizeof(unsigned int));
639 tempsize = mhashGetDuplicate(&dupeptr, 1);
641 //send control and dupes after
642 ctrl = RECEIVE_DUPES;
644 if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
649 bzero(&remoteAddr, sizeof(remoteAddr));
650 remoteAddr.sin_family = AF_INET;
651 remoteAddr.sin_port = htons(LISTEN_PORT);
652 remoteAddr.sin_addr.s_addr = htonl(mid);
654 if(connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr))<0) {
655 printf("BACKUP ERROR : %s\n",strerror(errno));
659 send_data(sd, &ctrl, sizeof(char));
660 send_data(sd, dupeptr, tempsize);
662 recv_data(sd, &response, sizeof(char));
664 printf("%s ->response : %d - %d\n",__func__,response,DUPLICATION_COMPLETE);
666 if(response != DUPLICATION_COMPLETE) {
668 printf("%s(DUPLICATION_BACKUP) -> DUPLICATION FAIL\n",__func__);
678 ctrl = DUPLICATION_COMPLETE;
679 send_data((int)acceptfd, &ctrl, sizeof(char));
681 printf("%s (DUPLICATE_BACKUP)-> Finished\n", __func__);
688 printf("control -> RECEIVE_DUPES sd : %d\n",(int)acceptfd);
690 if((readDuplicateObjs((int)acceptfd)) != 0) {
691 printf("Error: In readDuplicateObjs() %s, %d\n", __FILE__, __LINE__);
695 ctrl = DUPLICATION_COMPLETE;
696 send_data((int)acceptfd, &ctrl, sizeof(char));
698 printf("%s (RECEIVE_DUPES) -> Finished\n",__func__);
705 printf("control -> PAXOS_PREPARE\n");
707 recv_data((int)acceptfd, &val, sizeof(int));
709 control = PAXOS_PREPARE_REJECT;
710 send_data((int)acceptfd, &control, sizeof(char));
714 control = PAXOS_PREPARE_OK;
716 send_data((int)acceptfd, &control, sizeof(char));
717 send_data((int)acceptfd, &n_a, sizeof(int));
718 send_data((int)acceptfd, &v_a, sizeof(int));
724 printf("control -> PAXOS_ACCEPT\n");
726 recv_data((int)acceptfd, &n, sizeof(int));
727 recv_data((int)acceptfd, &v, sizeof(int));
729 control = PAXOS_ACCEPT_REJECT;
730 send_data((int)acceptfd, &control, sizeof(char));
736 control = PAXOS_ACCEPT_OK;
737 send_data((int)acceptfd, &control, sizeof(char));
743 printf("control -> PAXOS_LEARN\n");
745 recv_data((int)acceptfd, &v, sizeof(int));
749 printf("%s (PAXOS_LEARN)-> This is my leader!: [%s]\n", __func__, midtoIPString(leader));
755 printf("control -> DELETE_LEADER\n");
761 printf("Error: dstmAccept() Unknown opcode %d at %s, %d\n", control, __FILE__, __LINE__);
765 printf("%s-> Exiting\n", __func__); fflush(stdout);
768 /* Close connection */
769 if (close((int)acceptfd) == -1)
774 int readDuplicateObjs(int acceptfd) {
775 int numoid, i, size, tmpsize;
777 void *dupeptr, *ptrcreate, *ptr;
781 printf("%s-> Start\n", __func__);
783 recv_data((int)acceptfd, &numoid, sizeof(unsigned int));
784 recv_data((int)acceptfd, &size, sizeof(int));
785 // do i need array of oids?
786 // answer: no! now get to work
788 if ((dupeptr = calloc(1, size)) == NULL) {
789 printf("calloc error for duplicated objects %s, %d\n", __FILE__, __LINE__);
793 recv_data((int)acceptfd, dupeptr, size);
795 for(i = 0; i < numoid; i++) {
796 header = (objheader_t *)ptr;
798 GETSIZE(tmpsize, header);
799 tmpsize += sizeof(objheader_t);
802 printf("%s-> oid being received/backed:%u, version:%d, type:%d\n", __func__, oid, header->version, TYPE(header));
803 printf("STATUSPTR(header):%u, STATUS:%d\n", STATUSPTR(header), STATUS(header));
806 if(mhashSearch(oid) != NULL) {
808 printf("%s -> oid : %d is already in there\n",__func__,oid);
811 if(header->notifylist != NULL) {
812 unsigned int *listSize = (ptr + tmpsize);
813 tmpsize += sizeof(unsigned int);
814 tmpsize += sizeof(threadlist_t) * (*listSize);
818 pthread_mutex_lock(&mainobjstore_mutex);
819 if ((ptrcreate = objstrAlloc(&mainobjstore, tmpsize)) == NULL) {
820 printf("Error: readDuplicateObjs() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
821 pthread_mutex_unlock(&mainobjstore_mutex);
824 pthread_mutex_unlock(&mainobjstore_mutex);
825 memcpy(ptrcreate, header, tmpsize);
827 objheader_t* oPtr = (objheader_t*)ptrcreate;
829 if(oPtr->notifylist != NULL) {
830 oPtr->notifylist = NULL; // reset for new list
831 threadlist_t *listNode;
832 unsigned int* listSize = (ptr + tmpsize); // get number of notifylist
835 tmpsize += sizeof(unsigned int); // skip number of notifylist
836 listNode = (threadlist_t*)(ptr + tmpsize); // get first element of address
837 for(j = 0; j< *listSize; j++) { // retreive all threadlist
838 oPtr->notifylist = insNode(oPtr->notifylist,listNode[j].threadid,listNode[j].mid);
841 tmpsize += sizeof(threadlist_t) * (*listSize);
844 mhashInsert(oid, ptrcreate);
849 printf("%s-> End\n", __func__);
856 printf("%s-> No objects duplicated\n", __func__);
862 /* This function reads the information available in a transaction request
863 * and makes a function call to process the request */
864 int readClientReq(trans_commit_data_t *transinfo, int acceptfd) {
867 unsigned int *oidmod, oid;
869 objheader_t *headaddr;
870 int sum, i, size, n, val;
875 printf("%s-> Entering\n", __func__);
878 /* Read fixed_data_t data structure */
879 size = sizeof(fixed) - 1;
880 ptr = (char *)&fixed;
881 fixed.control = TRANS_REQUEST;
882 timeout = recv_data((int)acceptfd, ptr+1, size);
884 /* Read list of mids */
885 int mcount = fixed.mcount;
886 size = mcount * sizeof(unsigned int);
887 unsigned int listmid[mcount];
888 ptr = (char *) listmid;
889 timeout = recv_data((int)acceptfd, ptr, size);
891 if(timeout < 0) // coordinator failed
894 /* Read oid and version tuples for those objects that are not modified in the transaction */
895 int numread = fixed.numread;
896 size = numread * (sizeof(unsigned int) + sizeof(unsigned short));
898 if(numread != 0) { //If pile contains more than one object to be read,
899 // keep reading all objects
900 timeout = recv_data((int)acceptfd, objread, size);
903 /* Read modified objects */
904 if(fixed.nummod != 0) {
905 if ((modptr = calloc(1, fixed.sum_bytes)) == NULL) {
906 printf("calloc error for modified objects %s, %d\n", __FILE__, __LINE__);
909 size = fixed.sum_bytes;
910 timeout = recv_data((int)acceptfd, modptr, size);
913 if(timeout < 0) // coordinator failed
916 /* Create an array of oids for modified objects */
917 oidmod = (unsigned int *) calloc(fixed.nummod, sizeof(unsigned int));
918 if (oidmod == NULL) {
919 printf("calloc error %s, %d\n", __FILE__, __LINE__);
922 ptr = (char *) modptr;
923 for(i = 0 ; i < fixed.nummod; i++) {
925 headaddr = (objheader_t *) ptr;
928 GETSIZE(tmpsize, headaddr);
929 ptr += sizeof(objheader_t) + tmpsize;
932 printf("%s-> num oid read = %d, oids modified = %d, size = %d\n", __func__, fixed.numread, fixed.nummod, size);
934 /*Process the information read */
935 if((val = processClientReq(&fixed, transinfo, listmid, objread, modptr, oidmod, acceptfd)) != 0) {
936 printf("Error: In processClientReq() %s, %d\n", __FILE__, __LINE__);
949 printf("%s-> Exiting\n", __func__);
955 /* This function processes the Coordinator's transaction request using "handleTransReq"
956 * function and sends a reply to the co-ordinator.
957 * Following this it also receives a new control message from the co-ordinator and processes this message*/
958 int processClientReq(fixed_data_t *fixed, trans_commit_data_t *transinfo,
959 unsigned int *listmid, char *objread, void *modptr, unsigned int *oidmod, int acceptfd) {
961 char control, sendctrl, retval;
963 objheader_t *tmp_header;
966 unsigned int transID;
968 printf("%s-> Entering\n", __func__);
971 /* receives transaction id */
972 recv_data((int)acceptfd, &transID, sizeof(unsigned int));
974 /* Send reply to the Coordinator */
975 if((retval = handleTransReq(fixed, transinfo, listmid, objread, modptr,acceptfd)) == 0 ) {
976 printf("Error: In handleTransReq() %s, %d\n", __FILE__, __LINE__);
977 printf("DEBUG-> Exiting processClientReq, line = %d\n", __LINE__);
981 int timeout = recv_data((int)acceptfd, &control, sizeof(char));
982 /* Process the new control message */
984 printf("%s -> timeout = %d control = %d\n",__func__,timeout,control);
988 if(timeout < 0) { // timeout. failed to receiving data from coordinator
990 printf("%s -> timeout!! assumes coordinator is dead\n",__func__);
992 control = receiveDecisionFromBackup(transID,fixed->mcount,listmid);
994 printf("%s -> received Decision %d\n",__func__,control);
998 /* insert received control into thash for another transaction*/
999 thashInsert(transID, control);
1003 if (fixed->nummod > 0)
1005 /* Unlock objects that was locked due to this transaction */
1006 int useWriteUnlock = 0;
1007 for(i = 0; i< transinfo->numlocked; i++) {
1008 if(transinfo->objlocked[i] == -1) {
1012 if((header = mhashSearch(transinfo->objlocked[i])) == NULL) {
1013 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__); // find the header address
1014 printf("%s-> Exiting, line:%d\n", __func__, __LINE__);
1017 if(useWriteUnlock) {
1018 write_unlock(STATUSPTR(header));
1020 read_unlock(STATUSPTR(header));
1026 /* Invoke the transCommit process() */
1027 if((val = transCommitProcess(modptr, oidmod, transinfo->objlocked, fixed->nummod, transinfo->numlocked, (int)acceptfd)) != 0) {
1028 printf("Error: In transCommitProcess() %s, %d\n", __FILE__, __LINE__);
1030 if (transinfo->objlocked != NULL) {
1031 free(transinfo->objlocked);
1033 if (transinfo->objnotfound != NULL) {
1034 free(transinfo->objnotfound);
1036 printf("%s-> Exiting, line:%d\n", __func__, __LINE__);
1043 case TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING:
1047 printf("Error: No response to TRANS_AGREE OR DISAGREE protocol %s, %d\n", __FILE__, __LINE__);
1048 //TODO Use fixed.trans_id TID since Client may have died
1053 if (transinfo->objlocked != NULL) {
1054 free(transinfo->objlocked);
1056 if (transinfo->objnotfound != NULL) {
1057 free(transinfo->objnotfound);
1060 printf("%s-> Exiting, line:%d\n", __func__, __LINE__);
1067 char checkDecision(unsigned int transID)
1070 printf("%s -> transID : %u\n",__func__,transID);
1073 char response = thashSearch(transID);
1082 /* This function increments counters while running a voting decision on all objects involved
1083 * in TRANS_REQUEST and If a TRANS_DISAGREE sends the response immediately back to the coordinator */
1084 char handleTransReq(fixed_data_t *fixed, trans_commit_data_t *transinfo, unsigned int *listmid, char *objread, void *modptr, int acceptfd) {
1086 unsigned short version;
1087 char control = 0, *ptr;
1089 unsigned int *oidnotfound, *oidlocked, *oidvernotmatch;
1090 objheader_t *headptr;
1092 /* Counters and arrays to formulate decision on control message to be sent */
1093 oidnotfound = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
1094 oidlocked = (unsigned int *) calloc(fixed->numread + fixed->nummod + 1, sizeof(unsigned int));
1095 oidvernotmatch = (unsigned int *) calloc(fixed->numread + fixed->nummod, sizeof(unsigned int));
1096 int objnotfound = 0, objlocked = 0, objvernotmatch = 0;
1097 int v_nomatch = 0, v_matchlock = 0, v_matchnolock = 0;
1099 /* modptr points to the beginning of the object store
1100 * created at the Pariticipant.
1101 * Object store holds the modified objects involved in the transaction request */
1102 ptr = (char *) modptr;
1104 /* Process each oid in the machine pile/ group per thread */
1105 for (i = 0; i < fixed->numread + fixed->nummod; i++) {
1106 if (i < fixed->numread) { //Objs only read and not modified
1107 int incr = sizeof(unsigned int) + sizeof(unsigned short); // Offset that points to next position in the objread array
1109 oid = *((unsigned int *)(objread + incr));
1110 incr += sizeof(unsigned int);
1111 version = *((unsigned short *)(objread + incr));
1113 printf("%s -> oid : %u version : %d\n",__func__,oid,version);
1115 getCommitCountForObjRead(oidnotfound, oidlocked, oidvernotmatch, &objnotfound, &objlocked, &objvernotmatch,
1116 &v_matchnolock, &v_matchlock, &v_nomatch, &numBytes, &control, oid, version);
1117 } else { //Objs modified
1118 if(i == fixed->numread) {
1119 oidlocked[objlocked++] = -1;
1122 headptr = (objheader_t *) ptr;
1124 version = headptr->version;
1125 GETSIZE(tmpsize, headptr);
1126 ptr += sizeof(objheader_t) + tmpsize;
1128 getCommitCountForObjMod(oidnotfound, oidlocked, oidvernotmatch, &objnotfound,
1129 &objlocked, &objvernotmatch, &v_matchnolock, &v_matchlock, &v_nomatch,
1130 &numBytes, &control, oid, version);
1134 /* send TRANS_DISAGREE and objs*/
1137 char *objs = calloc(1, numBytes);
1139 for(j = 0; j<objvernotmatch; j++) {
1140 objheader_t *header = mhashSearch(oidvernotmatch[j]);
1142 GETSIZE(size, header);
1143 size += sizeof(objheader_t);
1144 memcpy(objs+offset, header, size);
1148 if (objlocked > 0) {
1149 int useWriteUnlock = 0;
1150 for(j = 0; j < objlocked; j++) {
1151 if(oidlocked[j] == -1) {
1155 if((headptr = mhashSearch(oidlocked[j])) == NULL) {
1156 printf("mhashSearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1159 if(useWriteUnlock) {
1160 write_unlock(STATUSPTR(headptr));
1162 read_unlock(STATUSPTR(headptr));
1170 printf("%s -> control = %d, file = %s, line = %d\n", __func__,(int)control, __FILE__, __LINE__);
1173 send_data(acceptfd, &control, sizeof(char));
1175 send_data(acceptfd, &numBytes, sizeof(int));
1176 send_data(acceptfd, objs, numBytes);
1178 transinfo->objvernotmatch = oidvernotmatch;
1179 transinfo->numvernotmatch = objvernotmatch;
1181 free(transinfo->objvernotmatch);
1186 /* Decide what control message to send to Coordinator */
1187 if ((control = decideCtrlMessage(fixed, transinfo, &v_matchnolock, &v_matchlock, &v_nomatch, &objnotfound, &objlocked,
1188 modptr, oidnotfound, oidlocked, acceptfd)) == 0) {
1189 printf("Error: In decideCtrlMessage() %s, %d\n", __FILE__, __LINE__);
1195 /* Update Commit info for objects that are modified */
1196 void getCommitCountForObjMod(unsigned int *oidnotfound, unsigned int *oidlocked,
1197 unsigned int *oidvernotmatch, int *objnotfound, int *objlocked, int *objvernotmatch,
1198 int *v_matchnolock, int *v_matchlock, int *v_nomatch, int *numBytes,
1199 char *control, unsigned int oid, unsigned short version) {
1201 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1202 //printf("version number: %d\n", version);
1207 printf("%s -> *backup object* oid:%u\n", __func__,oid);
1213 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1215 printf("Obj not found: %s() oid = %d, type = %d\t\n", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1218 /* Save the oids not found and number of oids not found for later use */
1219 oidnotfound[*objnotfound] = oid;
1221 } else { /* If Obj found in machine (i.e. has not moved) */
1222 /* Check if Obj is locked by any previous transaction */
1223 if (write_trylock(STATUSPTR(mobj))) { // Can acquire write lock
1225 printf("****%s->Trying to acquire 'remote' writelock for oid:%d, version:%d\n", __func__, oid, version);
1226 printf("this version: %d, mlookup version: %d\n", version, ((objheader_t *)mobj)->version);
1228 if (version == ((objheader_t *)mobj)->version) { /* match versions */
1230 } else { /* If versions don't match ...HARD ABORT */
1232 oidvernotmatch[*objvernotmatch] = oid;
1233 (*objvernotmatch)++;
1235 GETSIZE(size, mobj);
1236 size += sizeof(objheader_t);
1238 /* Send TRANS_DISAGREE to Coordinator */
1239 *control = TRANS_DISAGREE;
1240 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1242 //Keep track of oid locked
1243 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
1244 } else { //we are locked
1245 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1247 } else { /* If versions don't match ...HARD ABORT */
1249 oidvernotmatch[*objvernotmatch] = oid;
1250 (*objvernotmatch)++;
1252 GETSIZE(size, mobj);
1253 size += sizeof(objheader_t);
1255 *control = TRANS_DISAGREE;
1256 //printf("%s() oid = %d, type = %d\t", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1261 printf("%s -> oid: %u, v_matchnolock: %d, v_matchlock: %d, v_nomatch: %d\n",__func__,oid, *v_matchnolock, *v_matchlock, *v_nomatch);
1265 /* Update Commit info for objects that are read */
1266 void getCommitCountForObjRead(unsigned int *oidnotfound, unsigned int *oidlocked, unsigned int *oidvernotmatch,
1267 int *objnotfound, int *objlocked, int * objvernotmatch, int *v_matchnolock, int *v_matchlock,
1268 int *v_nomatch, int *numBytes, char *control, unsigned int oid, unsigned short version) {
1270 /* Check if object is still present in the machine since the beginning of TRANS_REQUEST */
1271 //printf("version number: %d\n", version);
1273 printf("%s -> Entering\n",__func__);
1278 printf("*backup object* oid:%u\n", oid);
1283 if ((mobj = mhashSearch(oid)) == NULL) { /* Obj not found */
1285 printf("%s -> Obj not found!\n",__func__);
1286 printf("%s -> Obj not found: oid = %d, type = %d\t\n", __func__,OID(mobj), TYPE((objheader_t *)mobj));
1289 /* Save the oids not found and number of oids not found for later use */
1290 oidnotfound[*objnotfound] = oid;
1292 } else { /* If Obj found in machine (i.e. has not moved) */
1294 printf("%s -> Obj found!!\n",__func__);
1295 printf("%s -> Obj found: oid = %d, type = %d\t\n", __func__, OID(mobj), TYPE((objheader_t *)mobj));
1299 /* Check if Obj is locked by any previous transaction */
1300 if (read_trylock(STATUSPTR(mobj))) { //Can further acquire read locks
1301 if (version == ((objheader_t *)mobj)->version) { /* match versions */
1303 } else { /* If versions don't match ...HARD ABORT */
1305 oidvernotmatch[(*objvernotmatch)++] = oid;
1307 GETSIZE(size, mobj);
1308 size += sizeof(objheader_t);
1311 /* Send TRANS_DISAGREE to Coordinator */
1312 *control = TRANS_DISAGREE;
1315 //Keep track of oid locked
1316 oidlocked[(*objlocked)++] = OID(((objheader_t *)mobj));
1317 } else { /* Some other transaction has aquired a write lock on this object */
1318 if (version == ((objheader_t *)mobj)->version) { /* Check if versions match */
1320 } else { /* If versions don't match ...HARD ABORT */
1322 oidvernotmatch[*objvernotmatch] = oid;
1323 (*objvernotmatch)++;
1325 GETSIZE(size, mobj);
1326 size += sizeof(objheader_t);
1328 *control = TRANS_DISAGREE;
1333 printf("%s -> oid: %u, v_matchnolock: %d, v_matchlock: %d, v_nomatch: %d\n",__func__, oid, *v_matchnolock, *v_matchlock, *v_nomatch);
1337 /* This function decides what control message such as TRANS_AGREE, TRANS_DISAGREE or TRANS_SOFT_ABORT
1338 * to send to Coordinator based on the votes of oids involved in the transaction */
1339 char decideCtrlMessage(fixed_data_t *fixed, trans_commit_data_t *transinfo, int *v_matchnolock, int *v_matchlock,
1340 int *v_nomatch, int *objnotfound, int *objlocked, void *modptr,
1341 unsigned int *oidnotfound, unsigned int *oidlocked, int acceptfd) {
1345 /* Condition to send TRANS_AGREE */
1346 if(*(v_matchnolock) == fixed->numread + fixed->nummod) {
1347 control = TRANS_AGREE;
1348 /* Send control message */
1350 printf("%s -> control = %s\n", __func__,"TRANS_AGREE");
1352 send_data(acceptfd, &control, sizeof(char));
1355 printf("%s -> finished sending control\n",__func__);
1358 /* Condition to send TRANS_SOFT_ABORT */
1359 else if((*(v_matchlock) > 0 && *(v_nomatch) == 0) || (*(objnotfound) > 0 && *(v_nomatch) == 0)) {
1360 control = TRANS_SOFT_ABORT;
1362 printf("%s -> control = %s\n", __func__,"TRANS_SOFT_ABORT");
1364 /* Send control message */
1365 send_data(acceptfd, &control, sizeof(char));
1367 /* FIXME how to send objs Send number of oids not found and the missing oids if objects are missing in the machine */
1368 if(*(objnotfound) != 0) {
1370 msg[0] = *(objnotfound);
1371 send_data(acceptfd, &msg, sizeof(int));
1372 int size = sizeof(unsigned int)* *(objnotfound);
1373 send_data(acceptfd, oidnotfound, size);
1377 /* Fill out the trans_commit_data_t data structure. This is required for a trans commit process
1378 * if Participant receives a TRANS_COMMIT */
1379 transinfo->objlocked = oidlocked;
1380 transinfo->objnotfound = oidnotfound;
1381 transinfo->modptr = modptr;
1382 transinfo->numlocked = *(objlocked);
1383 transinfo->numnotfound = *(objnotfound);
1387 /* This function processes all modified objects involved in a TRANS_COMMIT and updates pointer
1388 * addresses in lookup table and also changes version number
1389 * Sends an ACK back to Coordinator */
1390 int transCommitProcess(void *modptr, unsigned int *oidmod, unsigned int *oidlocked, int nummod, int numlocked, int acceptfd) {
1391 objheader_t *header;
1392 objheader_t *newheader;
1393 int i = 0, offset = 0;
1398 printf("DEBUG-> Entering transCommitProcess, dstmserver.c\n");
1399 printf("nummod: %d, numlocked: %d\n", nummod, numlocked);
1402 /* Process each modified object saved in the mainobject store */
1403 for(i = 0; i < nummod; i++) {
1404 if((header = (objheader_t *) mhashSearch(oidmod[i])) == NULL) {
1406 printf("Error: mhashsearch returns NULL at dstmserver.c %d\n", __LINE__);
1410 printf("DEBUG->*backup* i:%d, nummod:%d\n", i, nummod);
1412 header = (objheader_t *)(modptr+offset);
1413 header->version += 1;
1414 header->isBackup = 1;
1416 printf("oid: %u, new header version: %d\n", oidmod[i], header->version);
1418 GETSIZE(tmpsize, header);
1419 tmpsize += sizeof(objheader_t);
1420 pthread_mutex_lock(&mainobjstore_mutex);
1421 if ((ptrcreate = objstrAlloc(&mainobjstore, tmpsize)) == NULL) {
1422 printf("Error: transComProcess() failed objstrAlloc %s, %d\n", __FILE__, __LINE__);
1423 pthread_mutex_unlock(&mainobjstore_mutex);
1426 pthread_mutex_unlock(&mainobjstore_mutex);
1427 /* Initialize read and write locks */
1428 initdsmlocks(STATUSPTR(header));
1429 memcpy(ptrcreate, header, tmpsize);
1430 mhashInsert(oidmod[i], ptrcreate);
1437 GETSIZE(tmpsize,header);
1440 struct ___Object___ *dst=(struct ___Object___*)((char*)header+sizeof(objheader_t));
1441 struct ___Object___ *src=(struct ___Object___*)((char*)modptr+sizeof(objheader_t)+offset);
1442 dst->type=src->type;
1443 dst->___cachedCode___=src->___cachedCode___;
1444 dst->___cachedHash___=src->___cachedHash___;
1445 memcpy(&dst[1], &src[1], tmpsize-sizeof(struct ___Object___));
1447 header->version += 1;
1449 printf("oid: %u, new header version: %d\n", oidmod[i], header->version);
1451 /* If threads are waiting on this object to be updated, notify them */
1452 if(header->notifylist != NULL) {
1454 printf("%s -> type : %d notifylist : %d\n",__func__,TYPE(header),header->notifylist);
1458 if(header->isBackup != 0)
1459 notifyAll(&header->notifylist, OID(header), header->version);
1461 clearNotifyList(OID(header));
1463 notifyAll(&header->notifylist, OID(header), header->version);
1467 offset += sizeof(objheader_t) + tmpsize;
1473 /* Unlock locked objects */
1474 int useWriteUnlock = 0;
1475 for(i = 0; i < numlocked; i++) {
1476 if(oidlocked[i] == -1) {
1480 if((header = (objheader_t *) mhashSearch(oidlocked[i])) == NULL) {
1481 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1486 printf("header oid:%d, version:%d, useWriteUnlock:%d\n", OID(header), header->version, useWriteUnlock);
1488 if(useWriteUnlock) {
1489 write_unlock(STATUSPTR(header));
1491 read_unlock(STATUSPTR(header));
1494 //TODO Update location lookup table
1498 /* This function recevies the oid and offset tuples from the Coordinator's prefetch call.
1499 * Looks for the objects to be prefetched in the main object store.
1500 * If objects are not found then record those and if objects are found
1501 * then use offset values to prefetch references to other objects */
1503 int prefetchReq(int acceptfd) {
1504 int i, size, objsize, numoffset = 0;
1506 char *recvbuffer, control;
1507 unsigned int oid, mid=-1;
1508 objheader_t *header;
1509 oidmidpair_t oidmid;
1513 recv_data((int)acceptfd, &numoffset, sizeof(int));
1516 recv_data((int)acceptfd, &oidmid, 2*sizeof(unsigned int));
1518 if (mid != oidmid.mid) {
1520 freeSockWithLock(transPResponseSocketPool, mid, sd);
1523 sd = getSockWithLock(transPResponseSocketPool, mid);
1525 short offsetarry[numoffset];
1526 recv_data((int) acceptfd, offsetarry, numoffset*sizeof(short));
1528 /*Process each oid */
1529 if ((header = mhashSearch(oid)) == NULL) { /* Obj not found */
1530 /* Save the oids not found in buffer for later use */
1531 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
1532 char sendbuffer[size];
1533 *((int *) sendbuffer) = size;
1534 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
1535 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
1536 control = TRANS_PREFETCH_RESPONSE;
1537 sendPrefetchResponse(sd, &control, sendbuffer, &size);
1538 } else { /* Object Found */
1540 GETSIZE(objsize, header);
1541 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
1542 char sendbuffer[size];
1543 *((int *)(sendbuffer + incr)) = size;
1544 incr += sizeof(int);
1545 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
1546 incr += sizeof(char);
1547 *((unsigned int *)(sendbuffer+incr)) = oid;
1548 incr += sizeof(unsigned int);
1549 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
1551 control = TRANS_PREFETCH_RESPONSE;
1552 sendPrefetchResponse(sd, &control, sendbuffer, &size);
1554 /* Calculate the oid corresponding to the offset value */
1555 for(i = 0 ; i< numoffset ; i++) {
1556 /* Check for arrays */
1557 if(TYPE(header) >= NUMCLASSES) {
1558 int elementsize = classsize[TYPE(header)];
1559 struct ArrayObject *ao = (struct ArrayObject *) (((char *)header) + sizeof(objheader_t));
1560 unsigned short length = ao->___length___;
1561 /* Check if array out of bounds */
1562 if(offsetarry[i]< 0 || offsetarry[i] >= length) {
1565 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + sizeof(struct ArrayObject) + (elementsize*offsetarry[i])));
1567 oid = *((unsigned int *)(((char *)header) + sizeof(objheader_t) + offsetarry[i]));
1570 /* Don't continue if we hit a NULL pointer */
1574 if((header = mhashSearch(oid)) == NULL) {
1575 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) ;
1576 char sendbuffer[size];
1577 *((int *) sendbuffer) = size;
1578 *((char *)(sendbuffer + sizeof(int))) = OBJECT_NOT_FOUND;
1579 *((unsigned int *)(sendbuffer + sizeof(int) + sizeof(char))) = oid;
1581 control = TRANS_PREFETCH_RESPONSE;
1582 sendPrefetchResponse(sd, &control, sendbuffer, &size);
1584 } else { /* Obj Found */
1586 GETSIZE(objsize, header);
1587 size = sizeof(int) + sizeof(char) + sizeof(unsigned int) + sizeof(objheader_t) + objsize;
1588 char sendbuffer[size];
1589 *((int *)(sendbuffer + incr)) = size;
1590 incr += sizeof(int);
1591 *((char *)(sendbuffer + incr)) = OBJECT_FOUND;
1592 incr += sizeof(char);
1593 *((unsigned int *)(sendbuffer+incr)) = oid;
1594 incr += sizeof(unsigned int);
1595 memcpy(sendbuffer + incr, header, objsize + sizeof(objheader_t));
1597 control = TRANS_PREFETCH_RESPONSE;
1598 sendPrefetchResponse(sd, &control, sendbuffer, &size);
1605 freeSockWithLock(transPResponseSocketPool, mid, sd);
1610 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size) {
1612 printf("control = %d, file = %s, line = %d\n", (int)control, __FILE__, __LINE__);
1614 send_data(sd, control, sizeof(char));
1615 /* Send the buffer with its size */
1616 int length = *(size);
1617 send_data(sd, sendbuffer, length);
1620 void processReqNotify(unsigned int numoid, unsigned int *oidarry, unsigned short *versionarry, unsigned int mid, unsigned int threadid) {
1621 objheader_t *header;
1623 unsigned short newversion;
1624 char msg[1+ 2 * sizeof(unsigned int) + sizeof(unsigned short)];
1626 struct sockaddr_in remoteAddr;
1632 oid = *(oidarry + i);
1633 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
1634 printf("Error: mhashsearch returns NULL at %s, %d\n", __FILE__, __LINE__);
1637 /* Check to see if versions are same */
1639 if (write_trylock(STATUSPTR(header))) { // Can acquire write lock
1640 newversion = header->version;
1642 if(newversion == *(versionarry + i)) {
1643 //Add to the notify list
1644 if((header->notifylist = insNode(header->notifylist, threadid, mid)) == NULL) {
1645 printf("Error: Obj notify list points to NULL %s, %d\n", __FILE__, __LINE__);
1648 write_unlock(STATUSPTR(header));
1651 write_unlock(STATUSPTR(header));
1652 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1653 perror("processReqNotify():socket()");
1656 bzero(&remoteAddr, sizeof(remoteAddr));
1657 remoteAddr.sin_family = AF_INET;
1658 remoteAddr.sin_port = htons(LISTEN_PORT);
1659 remoteAddr.sin_addr.s_addr = htonl(mid);
1661 if(connect(sd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0) {
1662 printf("Error: processReqNotify():error %d connecting to %s:%d\n", errno,
1663 inet_ntoa(remoteAddr.sin_addr), LISTEN_PORT);
1667 //Send Update notification
1668 msg[0] = THREAD_NOTIFY_RESPONSE;
1669 *((unsigned int *)&msg[1]) = oid;
1670 size = sizeof(unsigned int);
1671 *((unsigned short *)(&msg[1]+size)) = newversion;
1672 size += sizeof(unsigned short);
1673 *((unsigned int *)(&msg[1]+size)) = threadid;
1674 size = 1+ 2*sizeof(unsigned int) + sizeof(unsigned short);
1675 send_data(sd, msg, size);
1691 /* go through oid's notifylist and clear them */
1692 void clearNotifyList(unsigned int oid)
1695 printf("%s -> Entering\n",__func__);
1698 objheader_t* header;
1702 if((header = (objheader_t *) mhashSearch(oid)) == NULL) {
1703 printf("%s -> mhashSearch returned NULL!!\n",__func__);
1706 if(header->notifylist != NULL) {
1707 t = header->notifylist;
1715 header->notifylist = NULL;
1718 pthread_mutex_lock(&clearNotifyList_mutex);
1719 clearNotifyListFlag = 0;
1720 pthread_mutex_unlock(&clearNotifyList_mutex);
1722 printf("%s -> finished\n",__func__);