4 Copyright (C) Ronnie sahlberg 2010
5 Copyright (C) Rusty Russell 2010
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.
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.
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/>.
23 #include <sys/types.h>
29 #include <ctdb_protocol.h>
32 * ctdb - a library for accessing tdbs controlled by ctdbd
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.
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.
45 * There is also a synchronous wrapper for each function for trivial
46 * programs; these can be found in the section marked "Synchronous API".
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.
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.
63 * The log() function can be typesafe: the @log_priv arg to
64 * ctdb_donnect and signature of log() should match.
66 typedef void (*ctdb_log_fn_t)(void *log_priv,
67 int severity, const char *format, va_list ap);
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.
75 * Returns a ctdb context if successful or NULL. Use ctdb_disconnect() to
76 * release the returned ctdb_connection when finished.
79 * ctdb_log_fn_t, ctdb_log_file()
81 struct ctdb_connection *ctdb_connect(const char *addr,
82 ctdb_log_fn_t log_fn, void *log_priv);
85 * ctdb_log_file - example logging function
87 * Logs everything at priority LOG_WARNING or above to the file given (via
88 * the log_priv argument, usually stderr).
90 void ctdb_log_file(FILE *, int, const char *, va_list);
93 * ctdb_log_level - level at which to call logging function
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.
99 * Set it to LOG_DEBUG to receive all messages.
101 extern int ctdb_log_level;
104 * ctdb_disconnect - close down a connection to ctdbd.
105 * @ctdb: the ctdb connectio returned from ctdb_connect.
107 * The @ctdb arg will be freed by this call, and must not be used again.
109 void ctdb_disconnect(struct ctdb_connection *ctdb);
118 * ctdb_get_fd - get the filedescriptor to select/poll on
119 * @ctdb: the ctdb_connection from ctdb_connect.
121 * By using poll or select on this file descriptor, you will know when to call
125 * ctdb_which_events(), ctdb_service()
127 int ctdb_get_fd(struct ctdb_connection *ctdb);
130 * ctdb_which_events - determine which events ctdb_service wants to see
131 * @ctdb: the ctdb_connection from ctdb_connect.
133 * This returns POLLIN, possibly or'd with POLLOUT if there are writes
134 * pending. You can set this straight into poll.events.
139 int ctdb_which_events(struct ctdb_connection *ctdb);
142 * ctdb_service - service any I/O and callbacks from ctdbd communication
143 * @ctdb: the ctdb_connection from ctdb_connect.
144 * @revents: which events are available.
146 * This is the core of the library: it read and writes to the ctdbd
147 * socket. It may call callbacks registered with the various _send
150 * revents is a bitset: POLLIN and/or POLLOUT may be set to indicate
151 * it is worth attempting to read/write the (nonblocking)
152 * filedescriptor respectively.
154 * Note that the synchronous functions call this internally.
155 * Returns false on catastrophic failure.
157 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
160 * struct ctdb_request - handle for an outstanding request
162 * This opaque structure returned from various *_send functions gives
163 * you a handle by which you can cancel a request. You can't do
164 * anything else with it until the request is completed and it is
165 * handed to your callback function.
170 * ctdb_request_free - free a completed request
172 * This frees a request: you should only call it once it has been
173 * handed to your callback. For incomplete requests, see ctdb_cancel().
175 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req);
178 * ctdb_callback_t - callback for completed requests.
180 * This would normally unpack the request using ctdb_*_recv(). You
181 * must free the request using ctdb_request_free().
183 * Note that due to macro magic, actual your callback can be typesafe:
184 * instead of taking a void *, it can take a type which matches the
185 * actual private parameter.
187 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
188 struct ctdb_request *req, void *private_data);
191 * struct ctdb_db - connection to a particular open TDB
193 * This represents a particular open database: you receive it from
194 * ctdb_attachdb or ctdb_attachdb_recv to manipulate a database.
196 * You have to free the handle with ctdb_detachdb() when finished with it.
201 * ctdb_attachdb_send - open a clustered TDB
202 * @ctdb: the ctdb_connection from ctdb_connect.
203 * @name: the filename of the database (no /).
204 * @persistent: whether the database is persistent across ctdbd's life
205 * @tdb_flags: the flags to pass to tdb_open.
206 * @callback: the callback when we're attached or failed (typesafe)
207 * @cbdata: the argument to callback()
209 * This function connects to a TDB controlled by ctdbd. It can create
210 * a new TDB if it does not exist, depending on tdb_flags. Returns
211 * the pending request, or NULL on error.
213 struct ctdb_request *
214 ctdb_attachdb_send(struct ctdb_connection *ctdb,
215 const char *name, bool persistent, uint32_t tdb_flags,
216 ctdb_callback_t callback, void *cbdata);
219 * ctdb_attachdb_recv - read an ctdb_attach reply from ctdbd
220 * @ctdb: the ctdb_connection from ctdb_connect.
221 * @req: the completed request.
223 * This returns NULL if something went wrong, or otherwise the open database.
225 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
226 struct ctdb_request *req);
230 * struct ctdb_lock - a record lock on a clustered TDB database
232 * This locks a subset of the database across the entire cluster; it
233 * is the fundamental sychronization element for ctdb. You cannot have
234 * more than one lock at once.
236 * You MUST NOT block during holding this lock and MUST release it
237 * quickly by performing ctdb_release_lock(lock).
238 * Do NOT make any system calls that may block while holding the lock.
240 * Try to release the lock as quickly as possible.
245 * ctdb_rrl_callback_t - callback for ctdb_readrecordlock_async
247 * This is not the standard ctdb_callback_t, because there is often no
248 * request required to access a database record (ie. if it is local already).
249 * So the callback is handed the lock directly: it might be NULL if there
250 * was an error obtaining the lock.
253 * ctdb_readrecordlock_async(), ctdb_readrecordlock()
255 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
256 struct ctdb_lock *lock,
261 * ctdb_readrecordlock_async - read and lock a record
262 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
263 * @key: the key of the record to lock.
264 * @callback: the callback once the record is locked (typesafe).
265 * @cbdata: the argument to callback()
267 * This returns true on success. Commonly, we can obtain the record
268 * immediately and so the callback will be invoked. Otherwise a request
269 * will be queued to ctdbd for the record.
271 * If failure is immediate, false is returned. Otherwise, the callback
272 * may receive a NULL lock arg to indicate asynchronous failure.
274 bool ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
275 ctdb_rrl_callback_t callback, void *cbdata);
278 * ctdb_writerecord - write a locked record in a TDB
279 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
280 * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_recv
281 * @data: the new data to place in the record.
283 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
284 struct ctdb_lock *lock, TDB_DATA data);
287 * ctdb_release_lock - release a record lock on a TDB
288 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
289 * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_async
291 void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock);
294 * ctdb_message_fn_t - messaging callback for ctdb messages
296 * ctdbd provides a simple messaging API; you can register for a particular
297 * 64-bit id on which you want to send messages, and send to other ids.
300 * ctdb_set_message_handler_send()
302 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *,
303 uint64_t srvid, TDB_DATA data, void *);
306 * ctdb_set_message_handler_send - register for messages to a srvid
307 * @ctdb: the ctdb_connection from ctdb_connect.
308 * @srvid: the 64 bit identifier for our messages.
309 * @handler: the callback when we receive such a message (typesafe)
310 * @handler_data: the argument to handler()
311 * @callback: the callback when ctdb replies to our message (typesafe)
312 * @cbdata: the argument to callback()
314 * Note: our callback will always be called before handler.
317 * ctdb_set_message_handler_recv(), ctdb_remove_message_handler_send()
319 struct ctdb_request *
320 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
321 ctdb_message_fn_t handler,
323 ctdb_callback_t callback,
327 * ctdb_set_message_handler_recv - read a set_message_handler result
328 * @ctdb: the ctdb_connection from ctdb_connect.
329 * @req: the completed request
331 * If this returns true, the registered handler may be called from the next
332 * ctdb_service(). If this returns false, the registration failed.
334 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
335 struct ctdb_request *handle);
338 * ctdb_remove_message_handler_send - unregister for messages to a srvid
339 * @ctdb: the ctdb_connection from ctdb_connect.
340 * @srvid: the 64 bit identifier for our messages.
341 * @handler: the callback when we receive such a message (typesafe)
342 * @handler_data: the argument to handler()
343 * @callback: the callback when ctdb replies to our message (typesafe)
344 * @cbdata: the argument to callback()
346 * This undoes a successful ctdb_set_message_handler or
347 * ctdb_set_message_handler_recv.
349 struct ctdb_request *
350 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
351 ctdb_message_fn_t handler, void *handler_data,
352 ctdb_callback_t callback, void *cbdata);
355 * ctdb_remove_message_handler_recv - read a remove_message_handler result
356 * @ctdb: the ctdb_connection from ctdb_connect.
357 * @req: the completed request
359 * After this returns true, the registered handler will no longer be called.
360 * If this returns false, the de-registration failed.
362 bool ctdb_remove_message_handler_recv(struct ctdb_connection *ctdb,
363 struct ctdb_request *req);
367 * ctdb_send_message - send a message via ctdbd
368 * @ctdb: the ctdb_connection from ctdb_connect.
369 * @pnn: the physical node number to send to
370 * @srvid: the 64 bit identifier for this message type.
371 * @data: the data to send
373 * This allows arbitrary messages to be sent across the cluster to those
374 * listening (via ctdb_set_message_handler et al).
376 * This queues a message to be sent: you will need to call
377 * ctdb_service() to actually send the message. There is no callback
378 * because there is no acknowledgement.
381 * ctdb_getpnn_send(), ctdb_getpnn()
383 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
386 * ctdb_getpnn_send - read the pnn number of a node.
387 * @ctdb: the ctdb_connection from ctdb_connect.
388 * @destnode: the destination node (see below)
389 * @callback: the callback when ctdb replies to our message (typesafe)
390 * @cbdata: the argument to callback()
392 * There are several special values for destnode, detailed in
393 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
396 struct ctdb_request *
397 ctdb_getpnn_send(struct ctdb_connection *ctdb,
399 ctdb_callback_t callback,
402 * ctdb_getpnn_recv - read an ctdb_getpnn reply from ctdbd
403 * @ctdb: the ctdb_connection from ctdb_connect.
404 * @req: the completed request.
405 * @pnn: a pointer to the pnn to fill in
407 * This returns false if something went wrong, or otherwise fills in pnn.
409 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
410 struct ctdb_request *req, uint32_t *pnn);
414 * ctdb_getrecmaster_send - read the recovery master of a node
415 * @ctdb: the ctdb_connection from ctdb_connect.
416 * @destnode: the destination node (see below)
417 * @callback: the callback when ctdb replies to our message (typesafe)
418 * @cbdata: the argument to callback()
420 * There are several special values for destnode, detailed in
421 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
424 struct ctdb_request *
425 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
427 ctdb_callback_t callback, void *cbdata);
430 * ctdb_getrecmaster_recv - read an ctdb_getrecmaster reply from ctdbd
431 * @ctdb: the ctdb_connection from ctdb_connect.
432 * @req: the completed request.
433 * @recmaster: a pointer to the recmaster to fill in
435 * This returns false if something went wrong, or otherwise fills in
438 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
439 struct ctdb_request *handle,
440 uint32_t *recmaster);
443 * ctdb_cancel - cancel an uncompleted request
444 * @ctdb: the ctdb_connection from ctdb_connect.
445 * @req: the uncompleted request.
447 * This cancels a request, returning true. You may not cancel a
448 * request which has already been completed (ie. once its callback has
449 * been called); you should simply use ctdb_request_free() in that case.
451 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
460 * ctdb_attachdb - open a clustered TDB (synchronous)
461 * @ctdb: the ctdb_connection from ctdb_connect.
462 * @name: the filename of the database (no /).
463 * @persistent: whether the database is persistent across ctdbd's life
464 * @tdb_flags: the flags to pass to tdb_open.
466 * Do a ctdb_attachdb_send and wait for it to complete.
467 * Returns NULL on failure.
469 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
470 const char *name, bool persistent,
474 * ctdb_detachdb - close a clustered TDB.
475 * @ctdb: the ctdb_connection from ctdb_connect.
476 * @db: the database from ctdb_attachdb/ctdb_attachdb_send
478 * Closes a clustered tdb.
480 void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db);
483 * ctdb_readrecordlock - read and lock a record (synchronous)
484 * @ctdb: the ctdb_connection from ctdb_connect.
485 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
486 * @key: the key of the record to lock.
487 * @req: a pointer to the request, if one is needed.
489 * Do a ctdb_readrecordlock_send and wait for it to complete.
490 * Returns NULL on failure.
492 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
493 struct ctdb_db *ctdb_db, TDB_DATA key,
498 * ctdb_set_message_handler - register for messages to a srvid (synchronous)
499 * @ctdb: the ctdb_connection from ctdb_connect.
500 * @srvid: the 64 bit identifier for our messages.
501 * @handler: the callback when we receive such a message (typesafe)
502 * @cbdata: the argument to handler()
504 * If this returns true, the message handler can be called from any
505 * ctdb_service() (which is also called indirectly by other
506 * synchronous functions). If this returns false, the registration
509 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
510 ctdb_message_fn_t handler, void *cbdata);
514 * ctdb_remove_message_handler - deregister for messages (synchronous)
515 * @ctdb: the ctdb_connection from ctdb_connect.
516 * @srvid: the 64 bit identifier for our messages.
517 * @handler: the callback when we receive such a message (typesafe)
518 * @handler_data: the argument to handler()
520 * If this returns true, the message handler will no longer be called.
521 * If this returns false, the deregistration failed.
523 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
524 ctdb_message_fn_t handler, void *handler_data);
527 * ctdb_getpnn - read the pnn number of a node (synchronous)
528 * @ctdb: the ctdb_connection from ctdb_connect.
529 * @destnode: the destination node (see below)
530 * @pnn: a pointer to the pnn to fill in
532 * There are several special values for destnode, detailed in
533 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
536 * Returns true and fills in *pnn on success.
538 bool ctdb_getpnn(struct ctdb_connection *ctdb,
543 * ctdb_getrecmaster - read the recovery master of a node (synchronous)
544 * @ctdb: the ctdb_connection from ctdb_connect.
545 * @destnode: the destination node (see below)
546 * @recmaster: a pointer to the recmaster to fill in
548 * There are several special values for destnode, detailed in
549 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
552 * Returns true and fills in *recmaster on success.
554 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
556 uint32_t *recmaster);
558 /* These ugly macro wrappers make the callbacks typesafe. */
559 #include <ctdb_typesafe_cb.h>
560 #define ctdb_sendcb(cb, cbdata) \
561 typesafe_cb_preargs(void, (cb), (cbdata), \
562 struct ctdb_connection *, struct ctdb_request *)
564 #define ctdb_msgcb(cb, cbdata) \
565 typesafe_cb_preargs(void, (cb), (cbdata), \
566 struct ctdb_connection *, uint64_t, TDB_DATA)
568 #define ctdb_connect(addr, log, logpriv) \
569 ctdb_connect((addr), \
570 typesafe_cb_postargs(void, (log), (logpriv), \
571 int, const char *, va_list), \
574 #define ctdb_set_message_handler(ctdb, srvid, handler, hdata) \
575 ctdb_set_message_handler((ctdb), (srvid), \
576 ctdb_msgcb((handler), (hdata)), (hdata))
578 #define ctdb_remove_message_handler(ctdb, srvid, handler, hdata) \
579 ctdb_remove_message_handler((ctdb), (srvid), \
580 ctdb_msgcb((handler), (hdata)), (hdata))
582 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
583 ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags), \
584 ctdb_sendcb((cb), (cbdata)), (cbdata))
586 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata) \
587 ctdb_readrecordlock_async((_ctdb_db), (key), \
588 typesafe_cb_preargs(void, (cb), (cbdata), \
589 struct ctdb_db *, struct ctdb_lock *, \
592 #define ctdb_set_message_handler_send(ctdb, srvid, handler, hdata, cb, cbdata) \
593 ctdb_set_message_handler_send((ctdb), (srvid), \
594 ctdb_msgcb((handler), (hdata)), (hdata), \
595 ctdb_sendcb((cb), (cbdata)), (cbdata))
597 #define ctdb_remove_message_handler_send(ctdb, srvid, handler, hdata, cb, cbdata) \
598 ctdb_remove_message_handler_send((ctdb), (srvid), \
599 ctdb_msgcb((handler), (hdata)), (hdata), \
600 ctdb_sendcb((cb), (cbdata)), (cbdata))
602 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata) \
603 ctdb_getpnn_send((ctdb), (destnode), \
604 ctdb_sendcb((cb), (cbdata)), (cbdata))
606 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata) \
607 ctdb_getrecmaster_send((ctdb), (destnode), \
608 ctdb_sendcb((cb), (cbdata)), (cbdata))