8 /***********************************************************
10 **********************************************************/
11 #define GET_NTUPLES(x) ((int *)(x + sizeof(prefetchqelem_t)))
12 #define GET_PTR_OID(x) ((unsigned int *)(x + sizeof(prefetchqelem_t) + sizeof(int)))
13 #define GET_PTR_EOFF(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int))))
14 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
15 /*****************************************
16 * Coordinator Messages
17 ***************************************/
18 #define READ_REQUEST 1
19 #define READ_MULT_REQUEST 2
20 #define MOVE_REQUEST 3
21 #define MOVE_MULT_REQUEST 4
22 #define TRANS_REQUEST 5
24 #define TRANS_COMMIT 7
25 #define TRANS_PREFETCH 8
26 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING 9
28 /*********************************
29 * Participant Messages
30 *******************************/
31 #define OBJECT_FOUND 10
32 #define OBJECT_NOT_FOUND 11
33 #define OBJECTS_FOUND 12
34 #define OBJECTS_NOT_FOUND 13
35 #define TRANS_AGREE 17
36 #define TRANS_DISAGREE 18
37 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
38 #define TRANS_SOFT_ABORT 20
39 #define TRANS_SUCESSFUL 21
40 #define TRANS_PREFETCH_RESPONSE 22
41 #define START_REMOTE_THREAD 23
42 #define THREAD_NOTIFY_REQUEST 24
43 #define THREAD_NOTIFY_RESPONSE 25
44 #define TRANS_UNSUCESSFUL 26
45 #define CLOSE_CONNECTION 27
47 //Max number of objects
48 #define MAX_OBJECTS 20
49 //Max remote-machine connections
50 #define NUM_MACHINES 2
51 #define LOADFACTOR 0.5
52 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
53 //Transaction id per machine
55 #define LISTEN_PORT 2156
65 #include "threadnotify.h"
66 #include <sys/types.h>
67 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <sys/types.h>
75 #include "prelookup.h"
77 //bit designations for status field of objheader
85 #include "structdefs.h"
87 typedef struct objheader {
88 threadlist_t *notifylist;
89 unsigned short version;
90 unsigned short rcount;
94 (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
97 (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
100 *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
103 ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
105 #define GETSIZE(size, x) {\
107 if (type<NUMCLASSES) {\
108 size=classsize[type];\
110 size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
116 typedef struct objheader {
117 threadlist_t *notifylist;
120 unsigned short version;
121 unsigned short rcount;
125 #define OID(x) x->oid
126 #define TYPE(x) x->type
127 #define STATUS(x) x->status
128 #define GETSIZE(size, x) size=classsize[TYPE(x)]
132 typedef struct objstr {
133 unsigned int size; //this many bytes are allocated after this header
138 typedef struct oidmidpair {
143 typedef struct transrecord {
145 chashtable_t *lookupTable;
147 struct ___Object___ * revertlist;
151 // Structure is a shared structure that keeps track of responses from the participants
152 typedef struct thread_response {
156 // Structure that holds fixed data to be sent along with TRANS_REQUEST
157 typedef struct fixed_data {
158 char control; /* control message */
159 char trans_id[TID_LEN]; /* transaction id */
160 int mcount; /* participant count */
161 short numread; /* no of objects read */
162 short nummod; /* no of objects modified */
163 short numcreated; /* no of objects created */
164 int sum_bytes; /* total bytes of modified objects in a transaction */
167 /* Structure that holds trans request information for each participant */
168 typedef struct trans_req_data {
169 fixed_data_t f; /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
170 unsigned int *listmid; /* Pointer to array holding list of participants */
171 char *objread; /* Pointer to array holding oid and version number of objects that are only read */
172 unsigned int *oidmod; /* Pointer to array holding oids of objects that are modified */
173 unsigned int *oidcreated; /* Pointer to array holding oids of objects that are newly created */
176 /* Structure that holds information of objects that are not found in the participant
177 * and objs locked within a transaction during commit process */
178 typedef struct trans_commit_data{
179 unsigned int *objlocked; /* Pointer to array holding oids of objects locked inside a transaction */
180 unsigned int *objnotfound; /* Pointer to array holding oids of objects not found on the participant machine */
181 void *modptr; /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
182 int numlocked; /* no of objects locked */
183 int numnotfound; /* no of objects not found */
184 } trans_commit_data_t;
187 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
188 /* Structure for passing multiple arguments to a thread
189 * spawned to process each transaction on a machine */
190 typedef struct thread_data_array {
193 trans_req_data_t *buffer; /* Holds trans request information sent to participants */
194 thread_response_t *recvmsg; /* Shared datastructure to keep track of the participants response to a trans request */
195 pthread_cond_t *threshold; /* Condition var to waking up a thread */
196 pthread_mutex_t *lock; /* Lock for counting participants response */
197 int *count; /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
198 char *replyctrl; /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
199 char *replyretry; /* Shared variable that keep track if coordinator needs retry */
200 transrecord_t *rec; /* To send modified objects */
201 } thread_data_array_t;
204 //Structure for passing arguments to the local m/c thread
205 typedef struct local_thread_data_array {
206 thread_data_array_t *tdata; /* Holds all the arguments send to a thread that is spawned when transaction commits */
207 trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */
208 } local_thread_data_array_t;
210 //Structure to store mid and socketid information
211 typedef struct midSocketInfo {
212 unsigned int mid; /* To communicate with mid use sockid in this data structure*/
216 /* Initialize main object store and lookup tables, start server thread. */
218 void send_data(int fd, void *buf, int buflen);
219 void recv_data(int fd, void *buf, int buflen);
220 int recv_data_errorcode(int fd, void *buf, int buflen);
222 /* Prototypes for object header */
223 unsigned int getNewOID(void);
224 /* end object header */
226 /* Prototypes for object store */
227 objstr_t *objstrCreate(unsigned int size); //size in bytes
228 void objstrDelete(objstr_t *store); //traverse and free entire list
229 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
230 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
231 /* end object store */
233 /* Prototypes for server portion */
235 void *dstmAccept(void *);
236 int readClientReq(trans_commit_data_t *, int);
237 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
238 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
239 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
240 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
241 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
242 /* end server portion */
244 /* Prototypes for transactions */
245 /* Function called at beginning. Passes in the first parameter. */
246 /* Returns 1 if this thread should run the main process */
248 int dstmStartup(const char *);
250 int processConfigFile();
251 void addHost(unsigned int);
252 void mapObjMethod(unsigned short);
255 transrecord_t *transStart();
256 objheader_t *transRead(transrecord_t *, unsigned int);
257 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
258 int transCommit(transrecord_t *record); //return 0 if successful
259 void *transRequest(void *); //the C routine that the thread will execute when TRANS_REQUEST begins
260 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
261 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
262 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);// returns object header from main object store after object is copied into it from remote machine
263 void *handleLocalReq(void *);//handles Local requests
264 int transComProcess(local_thread_data_array_t *);
265 int transAbortProcess(local_thread_data_array_t *);
266 void transAbort(transrecord_t *trans);
267 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
268 void prefetch(int, unsigned int *, unsigned short *, short*);
269 void *transPrefetch(void *);
270 void *mcqProcess(void *);
271 prefetchpile_t *foundLocal(prefetchqelem_t *);// returns node with prefetch elements(oids, offsets)
272 int lookupObject(unsigned int * oid, short offset);
273 int transPrefetchProcess(transrecord_t *, int **, short);
274 void sendPrefetchReq(prefetchpile_t*, int);
275 int getPrefetchResponse(int);
276 unsigned short getObjType(unsigned int oid);
277 int startRemoteThread(unsigned int oid, unsigned int mid);
278 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
279 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
280 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
281 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
283 /* end transactions */