update README and man page
[beanstalkd.git] / dat.h
blob3c5c2d040a9afa334f9596fefbbd9fc27a6edc11
1 typedef unsigned char uchar;
2 typedef uchar byte;
3 typedef unsigned int uint;
4 typedef int32_t int32;
5 typedef uint32_t uint32;
6 typedef int64_t int64;
7 typedef uint64_t uint64;
9 #define int8_t do_not_use_int8_t
10 #define uint8_t do_not_use_uint8_t
11 #define int32_t do_not_use_int32_t
12 #define uint32_t do_not_use_uint32_t
13 #define int64_t do_not_use_int64_t
14 #define uint64_t do_not_use_uint64_t
16 typedef struct ms *ms;
17 typedef struct job *job;
18 typedef struct tube *tube;
19 typedef struct Conn Conn;
20 typedef struct Heap Heap;
21 typedef struct Jobrec Jobrec;
22 typedef struct File File;
23 typedef struct Socket Socket;
24 typedef struct Server Server;
25 typedef struct Wal Wal;
27 typedef void(*evh)(int, short, void *);
28 typedef void(*ms_event_fn)(ms a, void *item, size_t i);
29 typedef void(*Handle)(void*, int rw); // rw can also be 'h' for hangup
30 typedef int(*Less)(void*, void*);
31 typedef void(*Record)(void*, int);
32 typedef int(FAlloc)(int, int);
34 #if _LP64
35 #define NUM_PRIMES 48
36 #else
37 #define NUM_PRIMES 19
38 #endif
40 #define MAX_TUBE_NAME_LEN 201
42 /* A command can be at most LINE_BUF_SIZE chars, including "\r\n". This value
43 * MUST be enough to hold the longest possible command or reply line, which is
44 * currently "USING a{200}\r\n". */
45 #define LINE_BUF_SIZE 208
47 /* CONN_TYPE_* are bit masks */
48 #define CONN_TYPE_PRODUCER 1
49 #define CONN_TYPE_WORKER 2
50 #define CONN_TYPE_WAITING 4
52 #define min(a,b) ((a)<(b)?(a):(b))
54 #define twarn(fmt, args...) warn("%s:%d in %s: " fmt, \
55 __FILE__, __LINE__, __func__, ##args)
56 #define twarnx(fmt, args...) warnx("%s:%d in %s: " fmt, \
57 __FILE__, __LINE__, __func__, ##args)
59 #define URGENT_THRESHOLD 1024
60 #define JOB_DATA_SIZE_LIMIT_DEFAULT ((1 << 16) - 1)
62 extern const char version[];
63 extern int verbose;
64 extern struct Server srv;
66 // Replaced by tests to simulate failures.
67 extern FAlloc *falloc;
69 struct stats {
70 uint urgent_ct;
71 uint waiting_ct;
72 uint buried_ct;
73 uint reserved_ct;
74 uint pause_ct;
75 uint64 total_delete_ct;
76 uint64 total_jobs_ct;
80 struct Heap {
81 int cap;
82 int len;
83 void **data;
84 Less less;
85 Record rec;
87 int heapinsert(Heap *h, void *x);
88 void* heapremove(Heap *h, int k);
91 struct Socket {
92 int fd;
93 Handle f;
94 void *x;
95 int added;
98 void sockinit(Handle tick, void *x, int64 ns);
99 int sockwant(Socket *s, int rw);
100 void sockmain(void); // does not return
102 struct ms {
103 size_t used, cap, last;
104 void **items;
105 ms_event_fn oninsert, onremove;
108 enum
110 Walver = 7
113 enum // Jobrec.state
115 Invalid,
116 Ready,
117 Reserved,
118 Buried,
119 Delayed,
120 Copy
123 // if you modify this struct, you must increment Walver above
124 struct Jobrec {
125 uint64 id;
126 uint32 pri;
127 int64 delay;
128 int64 ttr;
129 int32 body_size;
130 int64 created_at;
131 int64 deadline_at;
132 uint32 reserve_ct;
133 uint32 timeout_ct;
134 uint32 release_ct;
135 uint32 bury_ct;
136 uint32 kick_ct;
137 byte state;
140 struct job {
141 Jobrec r; // persistent fields; these get written to the wal
143 /* bookeeping fields; these are in-memory only */
144 char pad[6];
145 tube tube;
146 job prev, next; /* linked list of jobs */
147 job ht_next; /* Next job in a hash table list */
148 size_t heap_index; /* where is this job in its current heap */
149 File *file;
150 job fnext;
151 job fprev;
152 void *reserver;
153 int walresv;
154 int walused;
156 char body[]; // written separately to the wal
159 struct tube {
160 uint refs;
161 char name[MAX_TUBE_NAME_LEN];
162 Heap ready;
163 Heap delay;
164 struct ms waiting; /* set of conns */
165 struct stats stat;
166 uint using_ct;
167 uint watching_ct;
168 int64 pause;
169 int64 deadline_at;
170 struct job buried;
174 void v(void);
176 void warn(const char *fmt, ...);
177 void warnx(const char *fmt, ...);
178 char* fmtalloc(char *fmt, ...);
179 void* zalloc(int n);
180 #define new(T) zalloc(sizeof(T))
181 void optparse(Server*, char**);
183 extern const char *progname;
185 int64 nanoseconds(void);
186 int rawfalloc(int fd, int len);
189 void ms_init(ms a, ms_event_fn oninsert, ms_event_fn onremove);
190 void ms_clear(ms a);
191 int ms_append(ms a, void *item);
192 int ms_remove(ms a, void *item);
193 int ms_contains(ms a, void *item);
194 void *ms_take(ms a);
197 #define make_job(pri,delay,ttr,body_size,tube) make_job_with_id(pri,delay,ttr,body_size,tube,0)
199 job allocate_job(int body_size);
200 job make_job_with_id(uint pri, int64 delay, int64 ttr,
201 int body_size, tube tube, uint64 id);
202 void job_free(job j);
204 /* Lookup a job by job ID */
205 job job_find(uint64 job_id);
207 /* the void* parameters are really job pointers */
208 void job_setheappos(void*, int);
209 int job_pri_less(void*, void*);
210 int job_delay_less(void*, void*);
212 job job_copy(job j);
214 const char * job_state(job j);
216 int job_list_any_p(job head);
217 job job_remove(job j);
218 void job_insert(job head, job j);
220 uint64 total_jobs(void);
222 /* for unit tests */
223 size_t get_all_jobs_used(void);
226 extern struct ms tubes;
228 tube make_tube(const char *name);
229 void tube_dref(tube t);
230 void tube_iref(tube t);
231 tube tube_find(const char *name);
232 tube tube_find_or_make(const char *name);
233 #define TUBE_ASSIGN(a,b) (tube_dref(a), (a) = (b), tube_iref(a))
236 Conn *make_conn(int fd, char start_state, tube use, tube watch);
238 int count_cur_conns(void);
239 uint count_tot_conns(void);
240 int count_cur_producers(void);
241 int count_cur_workers(void);
244 extern size_t primes[];
247 extern size_t job_data_size_limit;
249 void prot_init(void);
250 void prottick(Server *s);
252 Conn *remove_waiting_conn(Conn *c);
254 void enqueue_reserved_jobs(Conn *c);
256 void enter_drain_mode(int sig);
257 void h_accept(const int fd, const short which, Server* srv);
258 void prot_remove_tube(tube t);
259 void prot_replay(Server *s, job list);
262 int make_server_socket(char *host_addr, char *port);
265 struct Conn {
266 Server *srv;
267 Socket sock;
268 char state;
269 char type;
270 Conn *next;
271 tube use;
272 int64 tickat; // time at which to do more work
273 int tickpos; // position in srv->conns
274 job soonest_job; // memoization of the soonest job
275 int rw; // currently want: 'r', 'w', or 'h'
276 int pending_timeout;
278 struct ms watch;
279 struct job reserved_jobs; // linked list header
281 char cmd[LINE_BUF_SIZE]; // this string is NOT NUL-terminated
282 int cmd_len;
283 int cmd_read;
285 char *reply;
286 int reply_len;
287 int reply_sent;
288 char reply_buf[LINE_BUF_SIZE]; // this string IS NUL-terminated
290 // How many bytes of in_job->body have been read so far. If in_job is NULL
291 // while in_job_read is nonzero, we are in bit bucket mode and
292 // in_job_read's meaning is inverted -- then it counts the bytes that
293 // remain to be thrown away.
294 int in_job_read;
295 job in_job; // a job to be read from the client
297 job out_job;
298 int out_job_sent;
300 int connless(Conn *a, Conn *b);
301 void connrec(Conn *c, int i);
302 void connwant(Conn *c, int rw);
303 void connsched(Conn *c);
304 void connclose(Conn *c);
305 void connsetproducer(Conn *c);
306 void connsetworker(Conn *c);
307 job connsoonestjob(Conn *c);
308 int conndeadlinesoon(Conn *c);
309 int conn_ready(Conn *c);
310 #define conn_waiting(c) ((c)->type & CONN_TYPE_WAITING)
315 enum
317 Filesizedef = (10 << 20)
320 struct Wal {
321 int filesize;
322 int use;
323 char *dir;
324 File *head;
325 File *cur;
326 File *tail;
327 int nfile;
328 int next;
329 int resv; // bytes reserved
330 int alive; // bytes in use
331 int64 nmig; // migrations
332 int64 nrec; // records written ever
333 int wantsync;
334 int64 syncrate;
335 int64 lastsync;
336 int nocomp; // disable binlog compaction?
338 int waldirlock(Wal*);
339 void walinit(Wal*, job list);
340 int walwrite(Wal*, job);
341 void walmaint(Wal*);
342 int walresvput(Wal*, job);
343 int walresvupdate(Wal*, job);
344 void walgc(Wal*);
347 struct File {
348 File *next;
349 uint refs;
350 int seq;
351 int iswopen; // is open for writing
352 int fd;
353 int free;
354 int resv;
355 char *path;
356 Wal *w;
358 struct job jlist; // jobs written in this file
360 int fileinit(File*, Wal*, int);
361 Wal* fileadd(File*, Wal*);
362 void fileincref(File*);
363 void filedecref(File*);
364 void fileaddjob(File*, job);
365 void filermjob(File*, job);
366 int fileread(File*, job list);
367 void filewopen(File*);
368 void filewclose(File*);
369 int filewrjobshort(File*, job);
370 int filewrjobfull(File*, job);
373 #define Portdef "11300"
375 struct Server {
376 char *port;
377 char *addr;
378 char *user;
380 Wal wal;
381 Socket sock;
382 Heap conns;
384 void srvserve(Server *srv);
385 void srvaccept(Server *s, int ev);
386 void srvschedconn(Server *srv, Conn *c);
387 void srvtick(Server *s, int ev);