8 /***********************************************************
10 **********************************************************/
11 #define GET_NTUPLES(x) ((int *)(x))
12 #define GET_PTR_OID(x) ((unsigned int *)(x + sizeof(int)))
13 #define GET_PTR_EOFF(x,n) ((short *)(x + sizeof(int) + (n*sizeof(unsigned int))))
14 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + 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 <arpa/inet.h>
67 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <sys/ioctl.h>
72 #include <netinet/in.h>
73 #include <sys/types.h>
78 #include "prelookup.h"
80 //bit designations for status field of objheader
88 #include "structdefs.h"
90 typedef struct objheader {
91 threadlist_t *notifylist;
92 unsigned short version;
93 unsigned short rcount;
97 (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
100 (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
103 *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
105 #define STATUSPTR(x)\
106 ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
109 ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
111 #define GETSIZE(size, x) {\
113 if (type<NUMCLASSES) {\
114 size=classsize[type];\
116 size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
122 typedef struct objheader {
123 threadlist_t *notifylist;
126 unsigned short version;
127 unsigned short rcount;
131 #define OID(x) x->oid
132 #define TYPE(x) x->type
133 #define STATUS(x) x->status
134 #define STATUSPTR(x) &x->status
135 #define GETSIZE(size, x) size=classsize[TYPE(x)]
139 typedef struct objstr {
140 unsigned int size; //this many bytes are allocated after this header
145 typedef struct oidmidpair {
150 typedef struct transrecord {
152 chashtable_t *lookupTable;
154 struct ___Object___ * revertlist;
158 // Structure is a shared structure that keeps track of responses from the participants
159 typedef struct thread_response {
163 // Structure that holds fixed data to be sent along with TRANS_REQUEST
164 typedef struct fixed_data {
165 char control; /* control message */
166 char trans_id[TID_LEN]; /* transaction id */
167 int mcount; /* participant count */
168 unsigned int numread; /* no of objects read */
169 unsigned int nummod; /* no of objects modified */
170 unsigned int numcreated; /* no of objects created */
171 int sum_bytes; /* total bytes of modified objects in a transaction */
174 /* Structure that holds trans request information for each participant */
175 typedef struct trans_req_data {
176 fixed_data_t f; /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
177 unsigned int *listmid; /* Pointer to array holding list of participants */
178 char *objread; /* Pointer to array holding oid and version number of objects that are only read */
179 unsigned int *oidmod; /* Pointer to array holding oids of objects that are modified */
180 unsigned int *oidcreated; /* Pointer to array holding oids of objects that are newly created */
183 /* Structure that holds information of objects that are not found in the participant
184 * and objs locked within a transaction during commit process */
185 typedef struct trans_commit_data{
186 unsigned int *objlocked; /* Pointer to array holding oids of objects locked inside a transaction */
187 unsigned int *objnotfound; /* Pointer to array holding oids of objects not found on the participant machine */
188 unsigned int *objvernotmatch; /* Pointer to array holding oids whose version doesn't match on the participant machine */
189 void *modptr; /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
190 int numlocked; /* no of objects locked */
191 int numnotfound; /* no of objects not found */
192 int numvernotmatch; /* no of objects whose version doesn't match */
193 } trans_commit_data_t;
196 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
197 /* Structure for passing multiple arguments to a thread
198 * spawned to process each transaction on a machine */
199 typedef struct thread_data_array {
202 trans_req_data_t *buffer; /* Holds trans request information sent to participants */
203 thread_response_t *recvmsg; /* Shared datastructure to keep track of the participants response to a trans request */
204 pthread_cond_t *threshold; /* Condition var to waking up a thread */
205 pthread_mutex_t *lock; /* Lock for counting participants response */
206 int *count; /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
207 char *replyctrl; /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
208 char *replyretry; /* Shared variable that keep track if coordinator needs retry */
209 transrecord_t *rec; /* To send modified objects */
210 } thread_data_array_t;
213 //Structure for passing arguments to the local m/c thread
214 typedef struct local_thread_data_array {
215 thread_data_array_t *tdata; /* Holds all the arguments send to a thread that is spawned when transaction commits */
216 trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */
217 } local_thread_data_array_t;
219 //Structure to store mid and socketid information
220 typedef struct midSocketInfo {
221 unsigned int mid; /* To communicate with mid use sockid in this data structure */
225 /* Initialize main object store and lookup tables, start server thread. */
227 void send_data(int fd, void *buf, int buflen);
228 void recv_data(int fd, void *buf, int buflen);
229 int recv_data_errorcode(int fd, void *buf, int buflen);
231 /* Prototypes for object header */
232 unsigned int getNewOID(void);
233 /* end object header */
235 /* Prototypes for object store */
236 objstr_t *objstrCreate(unsigned int size); //size in bytes
237 void objstrDelete(objstr_t *store); //traverse and free entire list
238 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
239 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
240 /* end object store */
242 /* Prototypes for server portion */
243 void *dstmListen(void *);
244 int startlistening();
245 void *dstmAccept(void *);
246 int readClientReq(trans_commit_data_t *, int);
247 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
248 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
249 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
250 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
251 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
252 /* end server portion */
254 /* Prototypes for transactions */
255 /* Function called at beginning. Passes in the first parameter. */
256 /* Returns 1 if this thread should run the main process */
258 int dstmStartup(const char *);
260 int processConfigFile();
261 void addHost(unsigned int);
262 void mapObjMethod(unsigned short);
265 transrecord_t *transStart();
266 objheader_t *transRead(transrecord_t *, unsigned int);
267 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
268 int transCommit(transrecord_t *record); //return 0 if successful
269 void *transRequest(void *); //the C routine that the thread will execute when TRANS_REQUEST begins
270 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
271 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
272 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);// returns object header from main object store after object is copied into it from remote machine
273 void *handleLocalReq(void *);//handles Local requests
274 int transComProcess(local_thread_data_array_t *);
275 int transAbortProcess(local_thread_data_array_t *);
276 void transAbort(transrecord_t *trans);
277 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
278 void prefetch(int, unsigned int *, unsigned short *, short*);
279 void *transPrefetch(void *);
280 void *mcqProcess(void *);
281 prefetchpile_t *foundLocal(char *);// returns node with prefetch elements(oids, offsets)
282 int lookupObject(unsigned int * oid, short offset);
283 int transPrefetchProcess(transrecord_t *, int **, short);
284 void sendPrefetchReq(prefetchpile_t*, int);
285 int getPrefetchResponse(int);
286 unsigned short getObjType(unsigned int oid);
287 int startRemoteThread(unsigned int oid, unsigned int mid);
288 int updatePrefetchCache(thread_data_array_t *, int, char);
292 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
293 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
294 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
295 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
297 /* end transactions */