binlog: better warning message when open fails
[beanstalkd.git] / dat.h
blob25c8eaeb01a42e87af86a62ad88e5f17751409ba
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 Srv Srv;
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);
33 #if _LP64
34 #define NUM_PRIMES 48
35 #else
36 #define NUM_PRIMES 19
37 #endif
39 #define MAX_TUBE_NAME_LEN 201
41 /* A command can be at most LINE_BUF_SIZE chars, including "\r\n". This value
42 * MUST be enough to hold the longest possible command or reply line, which is
43 * currently "USING a{200}\r\n". */
44 #define LINE_BUF_SIZE 208
46 /* CONN_TYPE_* are bit masks */
47 #define CONN_TYPE_PRODUCER 1
48 #define CONN_TYPE_WORKER 2
49 #define CONN_TYPE_WAITING 4
51 #define min(a,b) ((a)<(b)?(a):(b))
53 #define twarn(fmt, args...) warn("%s:%d in %s: " fmt, \
54 __FILE__, __LINE__, __func__, ##args)
55 #define twarnx(fmt, args...) warnx("%s:%d in %s: " fmt, \
56 __FILE__, __LINE__, __func__, ##args)
58 #define URGENT_THRESHOLD 1024
59 #define JOB_DATA_SIZE_LIMIT_DEFAULT ((1 << 16) - 1)
61 extern const char version[];
62 extern int verbose;
64 struct stats {
65 uint urgent_ct;
66 uint waiting_ct;
67 uint buried_ct;
68 uint reserved_ct;
69 uint pause_ct;
70 uint64 total_delete_ct;
71 uint64 total_jobs_ct;
75 struct Heap {
76 int cap;
77 int len;
78 void **data;
79 Less less;
80 Record rec;
82 int heapinsert(Heap *h, void *x);
83 void* heapremove(Heap *h, int k);
86 struct Socket {
87 int fd;
88 Handle f;
89 void *x;
90 int added;
93 void sockinit(Handle tick, void *x, int64 ns);
94 int sockwant(Socket *s, int rw);
95 void sockmain(void); // does not return
97 struct ms {
98 size_t used, cap, last;
99 void **items;
100 ms_event_fn oninsert, onremove;
103 enum
105 Walver = 7
108 enum // Jobrec.state
110 Invalid,
111 Ready,
112 Reserved,
113 Buried,
114 Delayed,
115 Copy
118 // if you modify this struct, you must increment Walver above
119 struct Jobrec {
120 uint64 id;
121 uint32 pri;
122 int64 delay;
123 int64 ttr;
124 int32 body_size;
125 int64 created_at;
126 int64 deadline_at;
127 uint32 reserve_ct;
128 uint32 timeout_ct;
129 uint32 release_ct;
130 uint32 bury_ct;
131 uint32 kick_ct;
132 byte state;
135 struct job {
136 Jobrec r; // persistent fields; these get written to the wal
138 /* bookeeping fields; these are in-memory only */
139 char pad[6];
140 tube tube;
141 job prev, next; /* linked list of jobs */
142 job ht_next; /* Next job in a hash table list */
143 size_t heap_index; /* where is this job in its current heap */
144 File *file;
145 job fnext;
146 job fprev;
147 void *reserver;
148 int walresv;
149 int walused;
151 char body[]; // written separately to the wal
154 struct tube {
155 uint refs;
156 char name[MAX_TUBE_NAME_LEN];
157 Heap ready;
158 Heap delay;
159 struct ms waiting; /* set of conns */
160 struct stats stat;
161 uint using_ct;
162 uint watching_ct;
163 int64 pause;
164 int64 deadline_at;
165 struct job buried;
168 struct conn {
169 conn prev, next; /* linked list of connections */
170 Srv *srv;
171 Socket sock;
172 char state;
173 char type;
174 int rw; // currently want: 'r' or 'w'
175 int pending_timeout;
176 int64 tickat; // time at which to do more work
177 int tickpos; // position in srv->conns
179 /* we cannot share this buffer with the reply line because we might read in
180 * command line data for a subsequent command, and we need to store it
181 * here. */
182 char cmd[LINE_BUF_SIZE]; /* this string is NOT NUL-terminated */
183 int cmd_len;
184 int cmd_read;
185 const char *reply;
186 int reply_len;
187 int reply_sent;
188 char reply_buf[LINE_BUF_SIZE]; /* this string IS NUL-terminated */
190 /* A job to be read from the client. */
191 job in_job;
193 /* Memoization of the soonest job */
194 job soonest_job;
196 /* How many bytes of in_job->body have been read so far. If in_job is NULL
197 * while in_job_read is nonzero, we are in bit bucket mode and
198 * in_job_read's meaning is inverted -- then it counts the bytes that
199 * remain to be thrown away. */
200 int in_job_read;
202 job out_job;
203 int out_job_sent;
204 tube use;
205 struct ms watch;
206 struct job reserved_jobs; /* doubly-linked list header */
210 void v(void);
212 void warn(const char *fmt, ...);
213 void warnx(const char *fmt, ...);
214 char* fmtalloc(char *fmt, ...);
215 void* zalloc(int n);
216 #define new(T) zalloc(sizeof(T))
218 extern const char *progname;
220 int64 nanoseconds(void);
221 int falloc(int fd, int len);
224 void ms_init(ms a, ms_event_fn oninsert, ms_event_fn onremove);
225 void ms_clear(ms a);
226 int ms_append(ms a, void *item);
227 int ms_remove(ms a, void *item);
228 int ms_contains(ms a, void *item);
229 void *ms_take(ms a);
232 #define make_job(pri,delay,ttr,body_size,tube) make_job_with_id(pri,delay,ttr,body_size,tube,0)
234 job allocate_job(int body_size);
235 job make_job_with_id(uint pri, int64 delay, int64 ttr,
236 int body_size, tube tube, uint64 id);
237 void job_free(job j);
239 /* Lookup a job by job ID */
240 job job_find(uint64 job_id);
242 /* the void* parameters are really job pointers */
243 void job_setheappos(void*, int);
244 int job_pri_less(void*, void*);
245 int job_delay_less(void*, void*);
247 job job_copy(job j);
249 const char * job_state(job j);
251 int job_list_any_p(job head);
252 job job_remove(job j);
253 void job_insert(job head, job j);
255 uint64 total_jobs(void);
257 /* for unit tests */
258 size_t get_all_jobs_used(void);
261 extern struct ms tubes;
263 tube make_tube(const char *name);
264 void tube_dref(tube t);
265 void tube_iref(tube t);
266 tube tube_find(const char *name);
267 tube tube_find_or_make(const char *name);
268 #define TUBE_ASSIGN(a,b) (tube_dref(a), (a) = (b), tube_iref(a))
271 conn make_conn(int fd, char start_state, tube use, tube watch);
273 int connless(conn a, conn b);
274 void connrec(conn c, int i);
275 void connwant(conn c, const int mask, conn list);
276 void connsched(conn c);
278 void conn_close(conn c);
280 conn conn_remove(conn c);
281 void conn_insert(conn head, conn c);
283 int count_cur_conns(void);
284 uint count_tot_conns(void);
285 int count_cur_producers(void);
286 int count_cur_workers(void);
288 void conn_set_producer(conn c);
289 void conn_set_worker(conn c);
291 job soonest_job(conn c);
292 int has_reserved_this_job(conn c, job j);
293 int conn_has_close_deadline(conn c);
294 int conn_ready(conn c);
296 #define conn_waiting(c) ((c)->type & CONN_TYPE_WAITING)
299 extern size_t primes[];
302 extern size_t job_data_size_limit;
304 void prot_init(void);
305 void prottick(Srv *s);
307 conn remove_waiting_conn(conn c);
309 void enqueue_reserved_jobs(conn c);
311 void enter_drain_mode(int sig);
312 void h_accept(const int fd, const short which, Srv* srv);
313 void prot_remove_tube(tube t);
314 void prot_replay(Srv *s, job list);
317 int make_server_socket(char *host_addr, char *port);
320 enum
322 Filesizedef = (10 << 20)
325 struct Wal {
326 int use;
327 char *dir;
328 File *head;
329 File *cur;
330 File *tail;
331 int nfile;
332 int next;
333 int filesz;
334 int resv; // bytes reserved
335 int alive; // bytes in use
336 int64 nmig; // migrations
337 int64 nrec; // records written ever
338 int wantsync;
339 int64 syncrate;
340 int64 lastsync;
341 int nocomp; // disable binlog compaction?
343 int waldirlock(Wal*);
344 void walinit(Wal*, job list);
345 int walwrite(Wal*, job);
346 void walmaint(Wal*);
347 int walresvput(Wal*, job);
348 int walresvupdate(Wal*, job);
349 void walgc(Wal*);
352 struct File {
353 File *next;
354 uint refs;
355 int seq;
356 int iswopen; // is open for writing
357 int fd;
358 int free;
359 int resv;
360 char *path;
361 Wal *w;
363 struct job jlist; // jobs written in this file
365 int fileinit(File*, Wal*, int);
366 Wal* fileadd(File*, Wal*);
367 void fileincref(File*);
368 void filedecref(File*);
369 void fileaddjob(File*, job);
370 void filermjob(File*, job);
371 int fileread(File*, job list);
372 void filewopen(File*);
373 void filewclose(File*);
374 int filewrjobshort(File*, job);
375 int filewrjobfull(File*, job);
378 struct Srv {
379 Socket sock;
380 Heap conns;
381 Wal wal;
383 void srv(Srv *srv);
384 void srvaccept(Srv *s, int ev);
385 void srvschedconn(Srv *srv, conn c);
386 void srvtick(Srv *s, int ev);