libctdb: documentation
[sahlberg/ctdb.git] / include / ctdb.h
1 /*
2    ctdb database library
3
4    Copyright (C) Ronnie sahlberg 2010
5    Copyright (C) Rusty Russell 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _CTDB_H
22 #define _CTDB_H
23 #include <sys/types.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <tdb.h>
29 #include <ctdb_protocol.h>
30
31 /**
32  * ctdb - a library for accessing tdbs controlled by ctdbd
33  *
34  * ctdbd (clustered tdb daemon) is a daemon designed to syncronize TDB
35  * databases across a cluster.  Using this library, you can communicate with
36  * the daemon to access the databases, pass messages across the cluster, and
37  * control the daemon itself.
38  *
39  * The general API is event-driven and asynchronous: you call the
40  * *_send functions, supplying callbacks, then when the ctdbd file
41  * descriptor is usable, call ctdb_service() to perform read from it
42  * and call your callbacks, which use the *_recv functions to unpack
43  * the replies from ctdbd.
44  *
45  * There is also a synchronous wrapper for each function for trivial
46  * programs; these can be found in the section marked "Synchronous API".
47  */
48
49 typedef void (*ctdb_log_fn_t)(void *log_priv,
50                               int severity, const char *format, va_list ap);
51
52 /**
53  * ctdb_connect - connect to ctdb using the specified domain socket.
54  * @addr: the socket address, or NULL for default
55  * @log: the logging function
56  * @log_priv: the private argument to the logging function.
57  *
58  * Returns a ctdb context if successful or NULL.  Use ctdb_free() to
59  * release the returned ctdb_connection when finished.
60  *
61  * The log() function can be typesafe: the log_priv arg and signature
62  * of log() should match.  The priority passed to log() os as per
63  * syslog(3).
64  *
65  * See Also:
66  *      ctdb_log_file()
67  */
68 struct ctdb_connection *ctdb_connect(const char *addr,
69                                      ctdb_log_fn_t log_fn, void *log_priv);
70
71 /**
72  * ctdb_log_file - example logging function
73  *
74  * Logs everything at priority LOG_WARNING or above to the file given (via
75  * the log_priv argument, usually stderr).
76  */
77 void ctdb_log_file(FILE *, int, const char *, va_list);
78
79 /**
80  * ctdb_log_level - level at which to call logging function
81  *
82  * This variable globally controls filtering on the logging function.
83  * It is initialized to LOG_WARNING, meaning that strange but nonfatal
84  * events, as well as errors and API misuses are reported.
85  *
86  * Set it to LOG_DEBUG to receive all messages.
87  */
88 extern int ctdb_log_level;
89
90 /***
91  *
92  *  Asynchronous API
93  *
94  ***/
95
96 /**
97  * ctdb_get_fd - get the filedescriptor to select/poll on
98  * @ctdb: the ctdb_connection from ctdb_connect.
99  *
100  * By using poll or select on this file descriptor, you will know when to call
101  * ctdb_service().
102  *
103  * See Also:
104  *      ctdb_which_events(), ctdb_service()
105  */
106 int ctdb_get_fd(struct ctdb_connection *ctdb);
107
108 /**
109  * ctdb_which_events - determine which events ctdb_service wants to see
110  * @ctdb: the ctdb_connection from ctdb_connect.
111  *
112  * This returns POLLIN, possibly or'd with POLLOUT if there are writes
113  * pending.  You can set this straight into poll.events.
114  *
115  * See Also:
116  *      ctdb_service()
117  */
118 int ctdb_which_events(struct ctdb_connection *ctdb);
119
120 /**
121  * ctdb_service - service any I/O and callbacks from ctdbd communication
122  * @ctdb: the ctdb_connection from ctdb_connect.
123  * @revents: which events are available.
124  *
125  * This is the core of the library: it read and writes to the ctdbd
126  * socket.  It may call callbacks registered with the various _send
127  * functions.
128  *
129  * revents is a bitset: POLLIN and/or POLLOUT may be set to indicate
130  * it is worth attempting to read/write the (nonblocking)
131  * filedescriptor respectively.
132  *
133  * Note that the synchronous functions call this internally.
134  * Returns false on catastrophic failure.
135  */
136 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
137
138 /**
139  * struct ctdb_request - handle for an outstanding request
140  *
141  * This opaque structure returned from various *_send functions gives
142  * you a handle by which you can cancel a request.  You can't do
143  * anything else with it until the request is completed and it is
144  * handed to your callback function.
145  */
146 struct ctdb_request;
147
148 /**
149  * ctdb_request_free - free a completed request
150  *
151  * This frees a request: you should only call it once it has been
152  * handed to your callback.  For incomplete requests, see ctdb_cancel().
153  */
154 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req);
155
156 /**
157  * ctdb_callback_t - callback for completed requests.
158  *
159  * This would normally unpack the request using ctdb_*_recv().  You
160  * must free the request using ctdb_request_free().
161  *
162  * Note that due to macro magic, actual your callback can be typesafe:
163  * instead of taking a void *, it can take a type which matches the
164  * actual private parameter.
165  */
166 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
167                                 struct ctdb_request *req, void *private);
168
169 /**
170  * struct ctdb_db - connection to a particular open TDB
171  *
172  * This represents a particular open database: you receive it from
173  * ctdb_attachdb or ctdb_attachdb_recv to manipulate a database.
174  *
175  * You have to free the handle with ctdb_detach_db() when finished with it.
176  */
177 struct ctdb_db;
178
179 /**
180  * ctdb_attachdb_send - open a clustered TDB
181  * @ctdb: the ctdb_connection from ctdb_connect.
182  * @name: the filename of the database (no /).
183  * @persistent: whether the database is persistent across ctdbd's life
184  * @tdb_flags: the flags to pass to tdb_open.
185  * @callback: the callback when we're attached or failed (typesafe)
186  * @cbdata: the argument to callback()
187  *
188  * This function connects to a TDB controlled by ctdbd.  It can create
189  * a new TDB if it does not exist, depending on tdb_flags.  Returns
190  * the pending request, or NULL on error.
191  */
192 struct ctdb_request *
193 ctdb_attachdb_send(struct ctdb_connection *ctdb,
194                    const char *name, int persistent, uint32_t tdb_flags,
195                    ctdb_callback_t callback, void *cbdata);
196
197 /**
198  * ctdb_attachdb_recv - read an ctdb_attach reply from ctdbd
199  * @ctdb: the ctdb_connection from ctdb_connect.
200  * @req: the completed request.
201  *
202  * This returns NULL if something went wrong, or otherwise the open database.
203  */
204 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
205                                    struct ctdb_request *req);
206
207
208 /**
209  * struct ctdb_lock - a record lock on a clustered TDB database
210  *
211  * This locks a subset of the database across the entire cluster; it
212  * is the fundamental sychronization element for ctdb.  You cannot have
213  * more than one lock at once.
214  *
215  * You MUST NOT block during holding this lock and MUST release it
216  * quickly by performing ctdb_release_lock(lock).
217  */
218 struct ctdb_lock;
219
220 /**
221  * ctdb_rrl_callback_t - callback for ctdb_readrecordlock_async
222  *
223  * This is not the standard ctdb_callback_t, because there is often no
224  * request required to access a database record (ie. if it is local already).
225  * So the callback is handed the lock directly: it might be NULL if there
226  * was an error obtaining the lock.
227  *
228  * See Also:
229  *      ctdb_readrecordlock_async(), ctdb_readrecordlock()
230  */
231 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
232                                     struct ctdb_lock *lock,
233                                     TDB_DATA data,
234                                     void *private);
235
236 /**
237  * ctdb_readrecordlock_async - read and lock a record
238  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
239  * @key: the key of the record to lock.
240  * @callback: the callback once the record is locked (typesafe).
241  * @cbdata: the argument to callback()
242  *
243  * This returns true on success.  Commonly, we can obtain the record
244  * immediately and so the callback will be invoked.  Otherwise a request
245  * will be queued to ctdbd for the record.
246  *
247  * If failure is immediate, false is returned.  Otherwise, the callback
248  * may receive a NULL lock arg to indicate asynchronous failure.
249  */
250 bool ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
251                                ctdb_rrl_callback_t callback, void *cbdata);
252
253 /**
254  * ctdb_writerecord - write a locked record in a TDB
255  * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_recv
256  * @data: the new data to place in the record.
257  */
258 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
259
260 /**
261  * ctdb_release_lock - release a record lock on a TDB
262  * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_async
263  */
264 void ctdb_release_lock(struct ctdb_lock *lock);
265
266 /**
267  * ctdb_message_fn_t - messaging callback for ctdb messages
268  *
269  * ctdbd provides a simple messaging API; you can register for a particular
270  * 64-bit id on which you want to send messages, and send to other ids.
271  *
272  * See Also:
273  *      ctdb_set_message_handler_send()
274  */
275 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *,
276                                   uint64_t srvid, TDB_DATA data, void *);
277
278 /**
279  * ctdb_set_message_handler_send - register for messages to a srvid
280  * @ctdb: the ctdb_connection from ctdb_connect.
281  * @srvid: the 64 bit identifier for our messages.
282  * @handler: the callback when we receive such a message (typesafe)
283  * @callback: the callback when ctdb replies to our message (typesafe)
284  * @cbdata: the argument to callback() and handler()
285  *
286  * Note: our callback will always be called before handler.
287  *
288  * See Also:
289  *      ctdb_set_message_handler_recv(), ctdb_remove_message_handler_send()
290  */
291 struct ctdb_request *
292 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
293                               ctdb_message_fn_t handler,
294                               ctdb_callback_t callback,
295                               void *cbdata);
296
297 /**
298  * ctdb_set_message_handler_recv - read a set_message_handler result
299  * @ctdb: the ctdb_connection from ctdb_connect.
300  * @req: the completed request
301  *
302  * If this returns true, the registered handler may be called from the next
303  * ctdb_service().  If this returns false, the registration failed.
304  */
305 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
306                                    struct ctdb_request *handle);
307
308 /**
309  * ctdb_remove_message_handler_send - unregister for messages to a srvid
310  * @ctdb: the ctdb_connection from ctdb_connect.
311  * @srvid: the 64 bit identifier for our messages.
312  * @callback: the callback when ctdb replies to our message (typesafe)
313  * @cbdata: the argument to callback()
314  *
315  * This undoes a successful ctdb_set_message_handler or
316  * ctdb_set_message_handler_recv.
317  */
318 struct ctdb_request *
319 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
320                                  ctdb_callback_t callback, void *cbdata);
321
322 /**
323  * ctdb_remove_message_handler_recv - read a remove_message_handler result
324  * @ctdb: the ctdb_connection from ctdb_connect.
325  * @req: the completed request
326  *
327  * After this returns true, the registered handler will no longer be called.
328  * If this returns false, the de-registration failed.
329  */
330 bool ctdb_remove_message_handler_recv(struct ctdb_request *handle);
331
332
333 /**
334  * ctdb_send_message - send a message via ctdbd
335  * @ctdb: the ctdb_connection from ctdb_connect.
336  * @pnn: the physical node number to send to
337  * @srvid: the 64 bit identifier for this message type.
338  * @data: the data to send
339  *
340  * This allows arbitrary messages to be sent across the cluster to those
341  * listening (via ctdb_set_message_handler et al).
342  *
343  * This queues a message to be sent: you will need to call
344  * ctdb_service() to actually send the message.  There is no callback
345  * because there is no acknowledgement.
346  *
347  * See Also:
348  *      ctdb_getpnn_send(), ctdb_getpnn()
349  */
350 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
351
352 /**
353  * ctdb_getpnn_send - read the pnn number of a node.
354  * @ctdb: the ctdb_connection from ctdb_connect.
355  * @destnode: the destination node (see below)
356  * @callback: the callback when ctdb replies to our message (typesafe)
357  * @cbdata: the argument to callback()
358  *
359  * There are several special values for destnode, detailed in
360  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
361  * local ctdbd.
362  */
363 struct ctdb_request *
364 ctdb_getpnn_send(struct ctdb_connection *ctdb,
365                  uint32_t destnode,
366                  ctdb_callback_t callback,
367                  void *cbdata);
368 /**
369  * ctdb_getpnn_recv - read an ctdb_getpnn reply from ctdbd
370  * @ctdb: the ctdb_connection from ctdb_connect.
371  * @req: the completed request.
372  * @pnn: a pointer to the pnn to fill in
373  *
374  * This returns false if something went wrong, or otherwise fills in pnn.
375  */
376 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
377                       struct ctdb_request *req, uint32_t *pnn);
378
379
380 /**
381  * ctdb_getrecmaster_send - read the recovery master of a node
382  * @ctdb: the ctdb_connection from ctdb_connect.
383  * @destnode: the destination node (see below)
384  * @callback: the callback when ctdb replies to our message (typesafe)
385  * @cbdata: the argument to callback()
386  *
387  * There are several special values for destnode, detailed in
388  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
389  * local ctdbd.
390  */
391 struct ctdb_request *
392 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
393                         uint32_t destnode,
394                             ctdb_callback_t callback, void *cbdata);
395
396 /**
397  * ctdb_getrecmaster_recv - read an ctdb_getrecmaster reply from ctdbd
398  * @ctdb: the ctdb_connection from ctdb_connect.
399  * @req: the completed request.
400  * @recmaster: a pointer to the recmaster to fill in
401  *
402  * This returns false if something went wrong, or otherwise fills in
403  * recmaster.
404  */
405 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
406                             struct ctdb_request *handle,
407                             uint32_t *recmaster);
408
409 /**
410  * ctdb_cancel - cancel an uncompleted request
411  * @ctdb: the ctdb_connection from ctdb_connect.
412  * @req: the uncompleted request.
413  *
414  * This cancels a request, returning true.  You may not cancel a
415  * request which has already been completed (ie. once its callback has
416  * been called); you should simply use ctdb_request_free() in that case.
417  */
418 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
419
420 /***
421  *
422  *  Synchronous API
423  *
424  ***/
425
426 /**
427  * ctdb_attachdb - open a clustered TDB (synchronous)
428  * @ctdb: the ctdb_connection from ctdb_connect.
429  * @name: the filename of the database (no /).
430  * @persistent: whether the database is persistent across ctdbd's life
431  * @tdb_flags: the flags to pass to tdb_open.
432  *
433  * Do a ctdb_attachdb_send and wait for it to complete.
434  * Returns NULL on failure.
435  */
436 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
437                               const char *name, int persistent,
438                               uint32_t tdb_flags);
439
440 /**
441  * ctdb_readrecordlock - read and lock a record (synchronous)
442  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
443  * @key: the key of the record to lock.
444  * @req: a pointer to the request, if one is needed.
445  *
446  * Do a ctdb_readrecordlock_send and wait for it to complete.
447  * Returns NULL on failure.
448  */
449 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_db *ctdb_db, TDB_DATA key,
450                                       TDB_DATA *data);
451
452
453 /**
454  * ctdb_set_message_handler - register for messages to a srvid (synchronous)
455  * @ctdb: the ctdb_connection from ctdb_connect.
456  * @srvid: the 64 bit identifier for our messages.
457  * @handler: the callback when we receive such a message (typesafe)
458  * @cbdata: the argument to handler()
459  *
460  * If this returns true, the message handler can be called from any
461  * ctdb_service() (which is also called indirectly by other
462  * synchronous functions).  If this returns false, the registration
463  * failed.
464  */
465 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
466                              ctdb_message_fn_t handler, void *cbdata);
467
468
469 /**
470  * ctdb_remove_message_handler - deregister for messages (synchronous)
471  * @ctdb: the ctdb_connection from ctdb_connect.
472  * @srvid: the 64 bit identifier for our messages.
473  *
474  * If this returns true, the message handler will no longer be called.
475  * If this returns false, the deregistration failed.
476  */
477 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
478
479 /**
480  * ctdb_getpnn - read the pnn number of a node (synchronous)
481  * @ctdb: the ctdb_connection from ctdb_connect.
482  * @destnode: the destination node (see below)
483  * @pnn: a pointer to the pnn to fill in
484  *
485  * There are several special values for destnode, detailed in
486  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
487  * local ctdbd.
488  *
489  * Returns true and fills in *pnn on success.
490  */
491 bool ctdb_getpnn(struct ctdb_connection *ctdb,
492                  uint32_t destnode,
493                  uint32_t *pnn);
494
495 /**
496  * ctdb_getrecmaster - read the recovery master of a node (synchronous)
497  * @ctdb: the ctdb_connection from ctdb_connect.
498  * @destnode: the destination node (see below)
499  * @recmaster: a pointer to the recmaster to fill in
500  *
501  * There are several special values for destnode, detailed in
502  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
503  * local ctdbd.
504  *
505  * Returns true and fills in *recmaster on success.
506  */
507 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
508                        uint32_t destnode,
509                        uint32_t *recmaster);
510
511 /* These ugly macro wrappers make the callbacks typesafe. */
512 #include <ctdb_typesafe_cb.h>
513 #define ctdb_sendcb(cb, cbdata)                                         \
514          typesafe_cb_preargs(void, (cb), (cbdata),                      \
515                              struct ctdb_connection *, struct ctdb_request *)
516
517 #define ctdb_connect(addr, log, logpriv)                                \
518         ctdb_connect((addr),                                            \
519                      typesafe_cb_postargs(void, (log), (logpriv),       \
520                                           int, const char *, va_list),  \
521                      (logpriv))
522
523
524 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
525         ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags),   \
526                            ctdb_sendcb((cb), (cbdata)), (cbdata))
527
528 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata)            \
529         ctdb_readrecordlock_async((_ctdb_db), (key),                    \
530                 typesafe_cb_preargs(void, (cb), (cbdata),               \
531                                     struct ctdb_db *, struct ctdb_lock *, \
532                                     TDB_DATA), (cbdata))
533
534 #define ctdb_set_message_handler_send(ctdb, srvid, handler, cb, cbdata) \
535         ctdb_set_message_handler_send((ctdb), (srvid), (handler),       \
536               ctdb_sendcb((cb), (cbdata)), (cbdata))
537
538 #define ctdb_remove_message_handler_send(ctdb, srvid, cb, cbdata)       \
539         ctdb_remove_message_handler_send((ctdb), (srvid),               \
540               ctdb_sendcb((cb), (cbdata)), (cbdata))
541
542 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata)                    \
543         ctdb_getpnn_send((ctdb), (destnode),                            \
544                          ctdb_sendcb((cb), (cbdata)), (cbdata))
545
546 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata)              \
547         ctdb_getrecmaster_send((ctdb), (destnode),                      \
548                                ctdb_sendcb((cb), (cbdata)), (cbdata))
549 #endif