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".
49 typedef void (*ctdb_log_fn_t)(void *log_priv,
50 int severity, const char *format, va_list ap);
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.
58 * Returns a ctdb context if successful or NULL. Use ctdb_free() to
59 * release the returned ctdb_connection when finished.
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
68 struct ctdb_connection *ctdb_connect(const char *addr,
69 ctdb_log_fn_t log_fn, void *log_priv);
72 * ctdb_log_file - example logging function
74 * Logs everything at priority LOG_WARNING or above to the file given (via
75 * the log_priv argument, usually stderr).
77 void ctdb_log_file(FILE *, int, const char *, va_list);
80 * ctdb_log_level - level at which to call logging function
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.
86 * Set it to LOG_DEBUG to receive all messages.
88 extern int ctdb_log_level;
97 * ctdb_get_fd - get the filedescriptor to select/poll on
98 * @ctdb: the ctdb_connection from ctdb_connect.
100 * By using poll or select on this file descriptor, you will know when to call
104 * ctdb_which_events(), ctdb_service()
106 int ctdb_get_fd(struct ctdb_connection *ctdb);
109 * ctdb_which_events - determine which events ctdb_service wants to see
110 * @ctdb: the ctdb_connection from ctdb_connect.
112 * This returns POLLIN, possibly or'd with POLLOUT if there are writes
113 * pending. You can set this straight into poll.events.
118 int ctdb_which_events(struct ctdb_connection *ctdb);
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.
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
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.
133 * Note that the synchronous functions call this internally.
134 * Returns false on catastrophic failure.
136 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
139 * struct ctdb_request - handle for an outstanding request
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.
149 * ctdb_request_free - free a completed request
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().
154 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req);
157 * ctdb_callback_t - callback for completed requests.
159 * This would normally unpack the request using ctdb_*_recv(). You
160 * must free the request using ctdb_request_free().
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.
166 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
167 struct ctdb_request *req, void *private);
170 * struct ctdb_db - connection to a particular open TDB
172 * This represents a particular open database: you receive it from
173 * ctdb_attachdb or ctdb_attachdb_recv to manipulate a database.
175 * You have to free the handle with ctdb_detach_db() when finished with it.
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()
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.
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);
198 * ctdb_attachdb_recv - read an ctdb_attach reply from ctdbd
199 * @ctdb: the ctdb_connection from ctdb_connect.
200 * @req: the completed request.
202 * This returns NULL if something went wrong, or otherwise the open database.
204 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
205 struct ctdb_request *req);
209 * struct ctdb_lock - a record lock on a clustered TDB database
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.
215 * You MUST NOT block during holding this lock and MUST release it
216 * quickly by performing ctdb_release_lock(lock).
217 * Do NOT make any system calls that may block while holding the lock.
219 * Try to release the lock as quickly as possible.
224 * ctdb_rrl_callback_t - callback for ctdb_readrecordlock_async
226 * This is not the standard ctdb_callback_t, because there is often no
227 * request required to access a database record (ie. if it is local already).
228 * So the callback is handed the lock directly: it might be NULL if there
229 * was an error obtaining the lock.
232 * ctdb_readrecordlock_async(), ctdb_readrecordlock()
234 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
235 struct ctdb_lock *lock,
240 * ctdb_readrecordlock_async - read and lock a record
241 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
242 * @key: the key of the record to lock.
243 * @callback: the callback once the record is locked (typesafe).
244 * @cbdata: the argument to callback()
246 * This returns true on success. Commonly, we can obtain the record
247 * immediately and so the callback will be invoked. Otherwise a request
248 * will be queued to ctdbd for the record.
250 * If failure is immediate, false is returned. Otherwise, the callback
251 * may receive a NULL lock arg to indicate asynchronous failure.
253 bool ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
254 ctdb_rrl_callback_t callback, void *cbdata);
257 * ctdb_writerecord - write a locked record in a TDB
258 * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_recv
259 * @data: the new data to place in the record.
261 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
264 * ctdb_release_lock - release a record lock on a TDB
265 * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_async
267 void ctdb_release_lock(struct ctdb_lock *lock);
270 * ctdb_message_fn_t - messaging callback for ctdb messages
272 * ctdbd provides a simple messaging API; you can register for a particular
273 * 64-bit id on which you want to send messages, and send to other ids.
276 * ctdb_set_message_handler_send()
278 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *,
279 uint64_t srvid, TDB_DATA data, void *);
282 * ctdb_set_message_handler_send - register for messages to a srvid
283 * @ctdb: the ctdb_connection from ctdb_connect.
284 * @srvid: the 64 bit identifier for our messages.
285 * @handler: the callback when we receive such a message (typesafe)
286 * @callback: the callback when ctdb replies to our message (typesafe)
287 * @cbdata: the argument to callback() and handler()
289 * Note: our callback will always be called before handler.
292 * ctdb_set_message_handler_recv(), ctdb_remove_message_handler_send()
294 struct ctdb_request *
295 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
296 ctdb_message_fn_t handler,
297 ctdb_callback_t callback,
301 * ctdb_set_message_handler_recv - read a set_message_handler result
302 * @ctdb: the ctdb_connection from ctdb_connect.
303 * @req: the completed request
305 * If this returns true, the registered handler may be called from the next
306 * ctdb_service(). If this returns false, the registration failed.
308 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
309 struct ctdb_request *handle);
312 * ctdb_remove_message_handler_send - unregister for messages to a srvid
313 * @ctdb: the ctdb_connection from ctdb_connect.
314 * @srvid: the 64 bit identifier for our messages.
315 * @callback: the callback when ctdb replies to our message (typesafe)
316 * @cbdata: the argument to callback()
318 * This undoes a successful ctdb_set_message_handler or
319 * ctdb_set_message_handler_recv.
321 struct ctdb_request *
322 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
323 ctdb_callback_t callback, void *cbdata);
326 * ctdb_remove_message_handler_recv - read a remove_message_handler result
327 * @ctdb: the ctdb_connection from ctdb_connect.
328 * @req: the completed request
330 * After this returns true, the registered handler will no longer be called.
331 * If this returns false, the de-registration failed.
333 bool ctdb_remove_message_handler_recv(struct ctdb_request *handle);
337 * ctdb_send_message - send a message via ctdbd
338 * @ctdb: the ctdb_connection from ctdb_connect.
339 * @pnn: the physical node number to send to
340 * @srvid: the 64 bit identifier for this message type.
341 * @data: the data to send
343 * This allows arbitrary messages to be sent across the cluster to those
344 * listening (via ctdb_set_message_handler et al).
346 * This queues a message to be sent: you will need to call
347 * ctdb_service() to actually send the message. There is no callback
348 * because there is no acknowledgement.
351 * ctdb_getpnn_send(), ctdb_getpnn()
353 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
356 * ctdb_getpnn_send - read the pnn number of a node.
357 * @ctdb: the ctdb_connection from ctdb_connect.
358 * @destnode: the destination node (see below)
359 * @callback: the callback when ctdb replies to our message (typesafe)
360 * @cbdata: the argument to callback()
362 * There are several special values for destnode, detailed in
363 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
366 struct ctdb_request *
367 ctdb_getpnn_send(struct ctdb_connection *ctdb,
369 ctdb_callback_t callback,
372 * ctdb_getpnn_recv - read an ctdb_getpnn reply from ctdbd
373 * @ctdb: the ctdb_connection from ctdb_connect.
374 * @req: the completed request.
375 * @pnn: a pointer to the pnn to fill in
377 * This returns false if something went wrong, or otherwise fills in pnn.
379 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
380 struct ctdb_request *req, uint32_t *pnn);
384 * ctdb_getrecmaster_send - read the recovery master of a node
385 * @ctdb: the ctdb_connection from ctdb_connect.
386 * @destnode: the destination node (see below)
387 * @callback: the callback when ctdb replies to our message (typesafe)
388 * @cbdata: the argument to callback()
390 * There are several special values for destnode, detailed in
391 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
394 struct ctdb_request *
395 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
397 ctdb_callback_t callback, void *cbdata);
400 * ctdb_getrecmaster_recv - read an ctdb_getrecmaster reply from ctdbd
401 * @ctdb: the ctdb_connection from ctdb_connect.
402 * @req: the completed request.
403 * @recmaster: a pointer to the recmaster to fill in
405 * This returns false if something went wrong, or otherwise fills in
408 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
409 struct ctdb_request *handle,
410 uint32_t *recmaster);
413 * ctdb_cancel - cancel an uncompleted request
414 * @ctdb: the ctdb_connection from ctdb_connect.
415 * @req: the uncompleted request.
417 * This cancels a request, returning true. You may not cancel a
418 * request which has already been completed (ie. once its callback has
419 * been called); you should simply use ctdb_request_free() in that case.
421 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
430 * ctdb_attachdb - open a clustered TDB (synchronous)
431 * @ctdb: the ctdb_connection from ctdb_connect.
432 * @name: the filename of the database (no /).
433 * @persistent: whether the database is persistent across ctdbd's life
434 * @tdb_flags: the flags to pass to tdb_open.
436 * Do a ctdb_attachdb_send and wait for it to complete.
437 * Returns NULL on failure.
439 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
440 const char *name, int persistent,
444 * ctdb_readrecordlock - read and lock a record (synchronous)
445 * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
446 * @key: the key of the record to lock.
447 * @req: a pointer to the request, if one is needed.
449 * Do a ctdb_readrecordlock_send and wait for it to complete.
450 * Returns NULL on failure.
452 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_db *ctdb_db, TDB_DATA key,
457 * ctdb_set_message_handler - register for messages to a srvid (synchronous)
458 * @ctdb: the ctdb_connection from ctdb_connect.
459 * @srvid: the 64 bit identifier for our messages.
460 * @handler: the callback when we receive such a message (typesafe)
461 * @cbdata: the argument to handler()
463 * If this returns true, the message handler can be called from any
464 * ctdb_service() (which is also called indirectly by other
465 * synchronous functions). If this returns false, the registration
468 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
469 ctdb_message_fn_t handler, void *cbdata);
473 * ctdb_remove_message_handler - deregister for messages (synchronous)
474 * @ctdb: the ctdb_connection from ctdb_connect.
475 * @srvid: the 64 bit identifier for our messages.
477 * If this returns true, the message handler will no longer be called.
478 * If this returns false, the deregistration failed.
480 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
483 * ctdb_getpnn - read the pnn number of a node (synchronous)
484 * @ctdb: the ctdb_connection from ctdb_connect.
485 * @destnode: the destination node (see below)
486 * @pnn: a pointer to the pnn to fill in
488 * There are several special values for destnode, detailed in
489 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
492 * Returns true and fills in *pnn on success.
494 bool ctdb_getpnn(struct ctdb_connection *ctdb,
499 * ctdb_getrecmaster - read the recovery master of a node (synchronous)
500 * @ctdb: the ctdb_connection from ctdb_connect.
501 * @destnode: the destination node (see below)
502 * @recmaster: a pointer to the recmaster to fill in
504 * There are several special values for destnode, detailed in
505 * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
508 * Returns true and fills in *recmaster on success.
510 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
512 uint32_t *recmaster);
514 /* These ugly macro wrappers make the callbacks typesafe. */
515 #include <ctdb_typesafe_cb.h>
516 #define ctdb_sendcb(cb, cbdata) \
517 typesafe_cb_preargs(void, (cb), (cbdata), \
518 struct ctdb_connection *, struct ctdb_request *)
520 #define ctdb_connect(addr, log, logpriv) \
521 ctdb_connect((addr), \
522 typesafe_cb_postargs(void, (log), (logpriv), \
523 int, const char *, va_list), \
527 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
528 ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags), \
529 ctdb_sendcb((cb), (cbdata)), (cbdata))
531 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata) \
532 ctdb_readrecordlock_async((_ctdb_db), (key), \
533 typesafe_cb_preargs(void, (cb), (cbdata), \
534 struct ctdb_db *, struct ctdb_lock *, \
537 #define ctdb_set_message_handler_send(ctdb, srvid, handler, cb, cbdata) \
538 ctdb_set_message_handler_send((ctdb), (srvid), (handler), \
539 ctdb_sendcb((cb), (cbdata)), (cbdata))
541 #define ctdb_remove_message_handler_send(ctdb, srvid, cb, cbdata) \
542 ctdb_remove_message_handler_send((ctdb), (srvid), \
543 ctdb_sendcb((cb), (cbdata)), (cbdata))
545 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata) \
546 ctdb_getpnn_send((ctdb), (destnode), \
547 ctdb_sendcb((cb), (cbdata)), (cbdata))
549 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata) \
550 ctdb_getrecmaster_send((ctdb), (destnode), \
551 ctdb_sendcb((cb), (cbdata)), (cbdata))