r23795: more v2->v3 conversion
[gd/samba/.git] / source4 / cluster / ctdb / include / ctdb_private.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 3 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef _CTDB_PRIVATE_H
22 #define _CTDB_PRIVATE_H
23
24 #include "ctdb.h"
25
26 /* location of daemon socket */
27 #define CTDB_PATH       "/tmp/ctdb.socket"
28
29 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
30 #define CTDB_DS_ALIGNMENT 8
31
32
33 #define CTDB_NULL_FUNC 0xf0000001
34
35 /*
36   an installed ctdb remote call
37 */
38 struct ctdb_registered_call {
39         struct ctdb_registered_call *next, *prev;
40         uint32_t id;
41         ctdb_fn_t fn;
42 };
43
44 /*
45   this address structure might need to be generalised later for some
46   transports
47 */
48 struct ctdb_address {
49         const char *address;
50         int port;
51 };
52
53 /*
54   check a vnn is valid
55  */
56 #define ctdb_validate_vnn(ctdb, vnn) (((uint32_t)(vnn)) < (ctdb)->num_nodes)
57
58
59 /* called from the queue code when a packet comes in. Called with data==NULL
60    on error */
61 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
62                                    void *private_data);
63
64
65 /*
66   state associated with one node
67 */
68 struct ctdb_node {
69         struct ctdb_context *ctdb;
70         struct ctdb_address address;
71         const char *name; /* for debug messages */
72         void *private_data; /* private to transport */
73         uint32_t vnn;
74 };
75
76 /*
77   transport specific methods
78 */
79 struct ctdb_methods {
80         int (*start)(struct ctdb_context *); /* start protocol processing */    
81         int (*add_node)(struct ctdb_node *); /* setup a new node */     
82         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
83         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
84 };
85
86 /*
87   transport calls up to the ctdb layer
88 */
89 struct ctdb_upcalls {
90         /* recv_pkt is called when a packet comes in */
91         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
92
93         /* node_dead is called when an attempt to send to a node fails */
94         void (*node_dead)(struct ctdb_node *);
95
96         /* node_connected is called when a connection to a node is established */
97         void (*node_connected)(struct ctdb_node *);
98 };
99
100 /* list of message handlers - needs to be changed to a more efficient data
101    structure so we can find a message handler given a srvid quickly */
102 struct ctdb_message_list {
103         struct ctdb_context *ctdb;
104         struct ctdb_message_list *next, *prev;
105         uint32_t srvid;
106         ctdb_message_fn_t message_handler;
107         void *message_private;
108 };
109
110 /* additional data required for the daemon mode */
111 struct ctdb_daemon_data {
112         int sd;
113         char *name;
114         struct ctdb_queue *queue;
115 };
116
117 /*
118   ctdb status information
119  */
120 struct ctdb_status {
121         uint32_t client_packets_sent;
122         uint32_t client_packets_recv;
123         uint32_t node_packets_sent;
124         uint32_t node_packets_recv;
125         struct {
126                 uint32_t req_call;
127                 uint32_t reply_call;
128                 uint32_t reply_redirect;
129                 uint32_t req_dmaster;
130                 uint32_t reply_dmaster;
131                 uint32_t reply_error;
132                 uint32_t req_message;
133                 uint32_t req_finished;
134         } count;
135         struct {
136                 uint32_t req_call;
137                 uint32_t req_message;
138                 uint32_t req_finished;
139                 uint32_t req_register;
140                 uint32_t req_connect_wait;
141                 uint32_t req_shutdown;
142                 uint32_t req_status;
143         } client;
144         uint32_t total_calls;
145         uint32_t pending_calls;
146         uint32_t lockwait_calls;
147         uint32_t pending_lockwait_calls;
148         double max_call_latency;
149         double max_lockwait_latency;
150 };
151
152 /* main state of the ctdb daemon */
153 struct ctdb_context {
154         struct event_context *ev;
155         struct ctdb_address address;
156         const char *name;
157         const char *db_directory;
158         uint32_t vnn; /* our own vnn */
159         uint32_t num_nodes;
160         uint32_t num_connected;
161         uint32_t num_finished;
162         unsigned flags;
163         struct idr_context *idr;
164         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
165         char *err_msg;
166         const struct ctdb_methods *methods; /* transport methods */
167         const struct ctdb_upcalls *upcalls; /* transport upcalls */
168         void *private_data; /* private to transport */
169         unsigned max_lacount;
170         struct ctdb_db_context *db_list;
171         struct ctdb_message_list *message_list;
172         struct ctdb_daemon_data daemon;
173         struct ctdb_status status;
174 };
175
176 struct ctdb_db_context {
177         struct ctdb_db_context *next, *prev;
178         struct ctdb_context *ctdb;
179         uint32_t db_id;
180         const char *db_name;
181         struct tdb_wrap *ltdb;
182         struct ctdb_registered_call *calls; /* list of registered calls */
183 };
184
185
186 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
187           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
188           return -1; }} while (0)
189
190 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
191           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
192           return NULL; }} while (0)
193
194 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
195           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
196           }} while (0)
197
198 /* arbitrary maximum timeout for ctdb operations */
199 #define CTDB_REQ_TIMEOUT 10
200
201 /* max number of redirects before we ask the lmaster */
202 #define CTDB_MAX_REDIRECT 2
203
204 /* number of consecutive calls from the same node before we give them
205    the record */
206 #define CTDB_DEFAULT_MAX_LACOUNT 7
207
208 /*
209   the extended header for records in the ltdb
210 */
211 struct ctdb_ltdb_header {
212         uint64_t rsn;
213         uint32_t dmaster;
214         uint32_t laccessor;
215         uint32_t lacount;
216 };
217
218 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
219
220 /*
221   state of a in-progress ctdb call
222 */
223 struct ctdb_call_state {
224         enum call_state state;
225         struct ctdb_req_call *c;
226         struct ctdb_db_context *ctdb_db;
227         struct ctdb_node *node;
228         const char *errmsg;
229         struct ctdb_call call;
230         int redirect_count;
231         struct ctdb_ltdb_header header;
232         struct {
233                 void (*fn)(struct ctdb_call_state *);
234                 void *private_data;
235         } async;
236 };
237
238
239 /* used for fetch_lock */
240 struct ctdb_fetch_handle {
241         struct ctdb_db_context *ctdb_db;
242         TDB_DATA key;
243         TDB_DATA *data;
244         struct ctdb_ltdb_header header;
245 };
246
247 /*
248   operation IDs
249 */
250 enum ctdb_operation {
251         CTDB_REQ_CALL           = 0,
252         CTDB_REPLY_CALL         = 1,
253         CTDB_REPLY_REDIRECT     = 2,
254         CTDB_REQ_DMASTER        = 3,
255         CTDB_REPLY_DMASTER      = 4,
256         CTDB_REPLY_ERROR        = 5,
257         CTDB_REQ_MESSAGE        = 6,
258         CTDB_REQ_FINISHED       = 7,
259         
260         /* only used on the domain socket */
261         CTDB_REQ_REGISTER       = 1000,     
262         CTDB_REQ_CONNECT_WAIT   = 1001,
263         CTDB_REPLY_CONNECT_WAIT = 1002,
264         CTDB_REQ_SHUTDOWN       = 1003,
265         CTDB_REQ_STATUS         = 1004,
266         CTDB_REPLY_STATUS       = 1005
267 };
268
269 #define CTDB_MAGIC 0x43544442 /* CTDB */
270 #define CTDB_VERSION 1
271
272 /*
273   packet structures
274 */
275 struct ctdb_req_header {
276         uint32_t length;
277         uint32_t ctdb_magic;
278         uint32_t ctdb_version;
279         uint32_t operation;
280         uint32_t destnode;
281         uint32_t srcnode;
282         uint32_t reqid;
283 };
284
285 struct ctdb_req_call {
286         struct ctdb_req_header hdr;
287         uint32_t flags;
288         uint32_t db_id;
289         uint32_t callid;
290         uint32_t keylen;
291         uint32_t calldatalen;
292         uint8_t data[1]; /* key[] followed by calldata[] */
293 };
294
295 struct ctdb_reply_call {
296         struct ctdb_req_header hdr;
297         uint32_t status;
298         uint32_t datalen;
299         uint8_t  data[1];
300 };
301
302 struct ctdb_reply_error {
303         struct ctdb_req_header hdr;
304         uint32_t status;
305         uint32_t msglen;
306         uint8_t  msg[1];
307 };
308
309 struct ctdb_reply_redirect {
310         struct ctdb_req_header hdr;
311         uint32_t dmaster;
312 };
313
314 struct ctdb_req_dmaster {
315         struct ctdb_req_header hdr;
316         uint32_t db_id;
317         uint32_t dmaster;
318         uint32_t keylen;
319         uint32_t datalen;
320         uint8_t  data[1];
321 };
322
323 struct ctdb_reply_dmaster {
324         struct ctdb_req_header hdr;
325         uint32_t datalen;
326         uint8_t  data[1];
327 };
328
329 struct ctdb_req_register {
330         struct ctdb_req_header hdr;
331         uint32_t srvid;
332 };
333
334 struct ctdb_req_message {
335         struct ctdb_req_header hdr;
336         uint32_t srvid;
337         uint32_t datalen;
338         uint8_t data[1];
339 };
340
341 struct ctdb_req_finished {
342         struct ctdb_req_header hdr;
343 };
344
345 struct ctdb_req_shutdown {
346         struct ctdb_req_header hdr;
347 };
348
349 struct ctdb_req_connect_wait {
350         struct ctdb_req_header hdr;
351 };
352
353 struct ctdb_reply_connect_wait {
354         struct ctdb_req_header hdr;
355         uint32_t vnn;
356         uint32_t num_connected;
357 };
358
359 struct ctdb_req_status {
360         struct ctdb_req_header hdr;
361 };
362
363 struct ctdb_reply_status {
364         struct ctdb_req_header hdr;
365         struct ctdb_status status;
366 };
367
368 /* internal prototypes */
369 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
370 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
371 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
372 int ctdb_parse_address(struct ctdb_context *ctdb,
373                        TALLOC_CTX *mem_ctx, const char *str,
374                        struct ctdb_address *address);
375 uint32_t ctdb_hash(const TDB_DATA *key);
376 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
377 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
378 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
379 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
380 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
381 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
382 void ctdb_reply_redirect(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
383
384 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
385 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
386                     TDB_DATA key, struct ctdb_ltdb_header *header, 
387                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
388 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
389                     struct ctdb_ltdb_header *header, TDB_DATA data);
390 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
391 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
392                            TDB_DATA key, struct ctdb_req_header *hdr,
393                            void (*recv_pkt)(void *, uint8_t *, uint32_t ),
394                            void *recv_context);
395 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
396                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
397                                  struct ctdb_req_header *hdr, TDB_DATA *data,
398                                  void (*recv_pkt)(void *, uint8_t *, uint32_t ),
399                                  void *recv_context);
400 void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length);
401
402 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
403                                              struct ctdb_call *call,
404                                              struct ctdb_ltdb_header *header,
405                                              TDB_DATA *data);
406
407
408 int ctdbd_start(struct ctdb_context *ctdb);
409 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
410 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
411
412 /*
413   queue a packet for sending
414 */
415 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
416
417 /*
418   setup the fd used by the queue
419  */
420 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
421
422 /*
423   setup a packet queue on a socket
424  */
425 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
426                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
427                                     
428                                     ctdb_queue_cb_fn_t callback,
429                                     void *private_data);
430
431 /*
432   allocate a packet for use in client<->daemon communication
433  */
434 void *ctdbd_allocate_pkt(TALLOC_CTX *mem_ctx, size_t len);
435
436
437 /*
438   lock a record in the ltdb, given a key
439  */
440 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
441
442 /*
443   unlock a record in the ltdb, given a key
444  */
445 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
446
447
448 /*
449   make a ctdb call to the local daemon - async send. Called from client context.
450
451   This constructs a ctdb_call request and queues it for processing. 
452   This call never blocks.
453 */
454 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
455                                               struct ctdb_call *call);
456
457 /*
458   make a recv call to the local ctdb daemon - called from client context
459
460   This is called when the program wants to wait for a ctdb_call to complete and get the 
461   results. This call will block unless the call has already completed.
462 */
463 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
464
465 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
466                              ctdb_message_fn_t handler,
467                              void *private_data);
468
469 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
470                              uint32_t srvid, TDB_DATA data);
471
472 /*
473   send a ctdb message
474 */
475 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
476                              uint32_t srvid, TDB_DATA data);
477
478
479 /*
480   wait for all nodes to be connected
481 */
482 void ctdb_daemon_connect_wait(struct ctdb_context *ctdb);
483
484
485 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
486                                       TDB_DATA key,
487                                       void (*callback)(void *), void *private_data);
488
489 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
490                                               struct ctdb_call *call);
491
492 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
493
494 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
495                                                      struct ctdb_call *call, 
496                                                      struct ctdb_ltdb_header *header);
497
498 void ctdb_request_finished(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
499
500 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
501                     struct ctdb_ltdb_header *header, TDB_DATA *data,
502                     uint32_t caller);
503
504 void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location);
505 #define idr_find_type(idp, id, type) (type *)_idr_find_type(idp, id, #type, __location__)
506
507 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
508
509 int ctdb_socket_connect(struct ctdb_context *ctdb);
510
511 void ctdb_latency(double *latency, struct timeval t);
512
513 #endif