8 /***********************************************************
10 **********************************************************/
11 #define GET_SITEID(x) ((int *)(x))
12 #define GET_NTUPLES(x) ((int *)(x + sizeof(int)))
13 #define GET_PTR_OID(x) ((unsigned int *)(x + 2*sizeof(int)))
14 #define GET_PTR_EOFF(x,n) ((short *)(x + 2*sizeof(int) + (n*sizeof(unsigned int))))
15 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + 2*sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
16 #define ENDEBUG(s) { printf("Inside %s()\n", s); fflush(stdout);}
17 #define EXDEBUG(s) {printf("Outside %s()\n", s); fflush(stdout);}
18 /*****************************************
19 * Coordinator Messages
20 ***************************************/
21 #define READ_REQUEST 1
22 #define READ_MULT_REQUEST 2
23 #define MOVE_REQUEST 3
24 #define MOVE_MULT_REQUEST 4
25 #define TRANS_REQUEST 5
27 #define TRANS_COMMIT 7
28 #define TRANS_PREFETCH 8
29 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING 9
31 /*********************************
32 * Participant Messages
33 *******************************/
34 #define OBJECT_FOUND 10
35 #define OBJECT_NOT_FOUND 11
36 #define OBJECTS_FOUND 12
37 #define OBJECTS_NOT_FOUND 13
38 #define TRANS_AGREE 17
39 #define TRANS_DISAGREE 18
40 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
41 #define TRANS_SOFT_ABORT 20
42 #define TRANS_SUCESSFUL 21
43 #define TRANS_PREFETCH_RESPONSE 22
44 #define START_REMOTE_THREAD 23
45 #define THREAD_NOTIFY_REQUEST 24
46 #define THREAD_NOTIFY_RESPONSE 25
47 #define TRANS_UNSUCESSFUL 26
48 #define CLOSE_CONNECTION 27
50 //Max number of objects
51 #define MAX_OBJECTS 20
52 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
53 //Transaction id per machine
55 #define LISTEN_PORT 2156
57 //Prefetch tuning paramters
58 #define RETRYINTERVAL 20 //N
59 #define SHUTDOWNINTERVAL 3 //M
68 #include "threadnotify.h"
69 #include <arpa/inet.h>
70 #include <sys/types.h>
71 #include <sys/socket.h>
72 #include <sys/ioctl.h>
75 #include <netinet/in.h>
76 #include <sys/types.h>
85 //bit designations for status field of objheader
91 /*******Global statistics *********/
92 extern int numprefetchsites;
96 #include "structdefs.h"
98 typedef struct objheader {
99 threadlist_t *notifylist;
100 unsigned short version;
101 unsigned short rcount;
105 (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
108 ((void *x)!=NULL)?(*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___)):0
111 *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
113 #define STATUSPTR(x) \
114 ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
117 ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
119 #define GETSIZE(size, x) { \
121 if (type<NUMCLASSES) { \
122 size=classsize[type]; \
124 size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject); \
130 typedef struct objheader {
131 threadlist_t *notifylist;
134 unsigned short version;
135 unsigned short rcount;
139 #define OID(x) x->oid
140 #define TYPE(x) x->type
141 #define STATUS(x) x->status
142 #define STATUSPTR(x) &x->status
143 #define GETSIZE(size, x) size=classsize[TYPE(x)]
146 typedef struct objstr {
147 unsigned int size; //this many bytes are allocated after this header
152 typedef struct oidmidpair {
157 typedef struct transrecord {
159 chashtable_t *lookupTable;
161 struct ___Object___ * revertlist;
165 // Structure is a shared structure that keeps track of responses from the participants
166 typedef struct thread_response {
170 // Structure that holds fixed data to be sent along with TRANS_REQUEST
171 typedef struct fixed_data {
172 char control; /* control message */
173 char trans_id[TID_LEN]; /* transaction id */
174 int mcount; /* participant count */
175 unsigned int numread; /* no of objects read */
176 unsigned int nummod; /* no of objects modified */
177 unsigned int numcreated; /* no of objects created */
178 int sum_bytes; /* total bytes of modified objects in a transaction */
181 /* Structure that holds trans request information for each participant */
182 typedef struct trans_req_data {
183 fixed_data_t f; /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
184 unsigned int *listmid; /* Pointer to array holding list of participants */
185 char *objread; /* Pointer to array holding oid and version number of objects that are only read */
186 unsigned int *oidmod; /* Pointer to array holding oids of objects that are modified */
187 unsigned int *oidcreated; /* Pointer to array holding oids of objects that are newly created */
190 /* Structure that holds information of objects that are not found in the participant
191 * and objs locked within a transaction during commit process */
192 typedef struct trans_commit_data {
193 unsigned int *objlocked; /* Pointer to array holding oids of objects locked inside a transaction */
194 unsigned int *objnotfound; /* Pointer to array holding oids of objects not found on the participant machine */
195 unsigned int *objvernotmatch; /* Pointer to array holding oids whose version doesn't match on the participant machine */
196 void *modptr; /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
197 int numlocked; /* no of objects locked */
198 int numnotfound; /* no of objects not found */
199 int numvernotmatch; /* no of objects whose version doesn't match */
200 } trans_commit_data_t;
203 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
204 /* Structure for passing multiple arguments to a thread
205 * spawned to process each transaction on a machine */
206 typedef struct thread_data_array {
209 trans_req_data_t *buffer; /* Holds trans request information sent to a participant, based on threadid */
210 thread_response_t *recvmsg; /* Shared datastructure to keep track of the participants response to a trans request */
211 pthread_cond_t *threshold; /* Condition var to waking up a thread */
212 pthread_mutex_t *lock; /* Lock for counting participants response */
213 int *count; /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
214 char *replyctrl; /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
215 char *replyretry; /* Shared variable that keep track if coordinator needs retry */
216 transrecord_t *rec; /* Shared variable transaction record send to all thread data */
217 } thread_data_array_t;
220 //Structure for passing arguments to the local m/c thread
221 typedef struct local_thread_data_array {
222 thread_data_array_t *tdata; /* Holds all the arguments send to a thread that is spawned when transaction commits */
223 trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */
224 } local_thread_data_array_t;
226 //Structure to store mid and socketid information
227 typedef struct midSocketInfo {
228 unsigned int mid; /* To communicate with mid use sockid in this data structure */
232 /* Initialize main object store and lookup tables, start server thread. */
234 void send_data(int fd, void *buf, int buflen);
235 void recv_data(int fd, void *buf, int buflen);
236 int recv_data_errorcode(int fd, void *buf, int buflen);
238 /* Prototypes for object header */
239 unsigned int getNewOID(void);
240 /* end object header */
242 /* Prototypes for object store */
243 objstr_t *objstrCreate(unsigned int size); //size in bytes
244 void objstrDelete(objstr_t *store); //traverse and free entire list
245 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
246 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
247 /* end object store */
249 /* Prototypes for server portion */
250 void *dstmListen(void *);
251 int startlistening();
252 void *dstmAccept(void *);
253 int readClientReq(trans_commit_data_t *, int);
254 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
255 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
256 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
257 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
258 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
259 /* end server portion */
261 /* Prototypes for transactions */
262 /* Function called at beginning. Passes in the first parameter. */
263 /* Returns 1 if this thread should run the main process */
265 int dstmStartup(const char *);
267 int processConfigFile();
268 void addHost(unsigned int);
269 void mapObjMethod(unsigned short);
272 transrecord_t *transStart();
273 objheader_t *transRead(transrecord_t *, unsigned int);
274 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
275 int transCommit(transrecord_t *record); //return 0 if successful
276 void *transRequest(void *); //the C routine that the thread will execute when TRANS_REQUEST begins
277 void decideResponse(thread_data_array_t *); // Coordinator decides what response to send to the participant
278 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
279 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int); // returns object header from main object store after object is copied into it from remote machine
280 void *handleLocalReq(void *); //handles Local requests
281 int transComProcess(local_thread_data_array_t *);
282 int transAbortProcess(local_thread_data_array_t *);
283 void transAbort(transrecord_t *trans);
284 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
285 void prefetch(int, int, unsigned int *, unsigned short *, short*);
286 void *transPrefetch(void *);
287 void *mcqProcess(void *);
288 prefetchpile_t *foundLocal(char *); // returns node with prefetch elements(oids, offsets)
289 int lookupObject(unsigned int * oid, short offset);
290 int transPrefetchProcess(transrecord_t *, int **, short);
291 void sendPrefetchReq(prefetchpile_t*, int);
292 void sendPrefetchReqnew(prefetchpile_t*, int);
293 int getPrefetchResponse(int);
294 unsigned short getObjType(unsigned int oid);
295 int startRemoteThread(unsigned int oid, unsigned int mid);
296 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs);
298 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
299 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
300 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
301 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
303 /* end transactions */