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