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>
30 /* The type of the first arg should match the arg given to ctdb_connect() */
31 typedef void (*ctdb_log_fn_t)(void *log_priv,
32 int severity, const char *format, va_list ap);
35 /* All *_send() functions are guaranteed to be non-blocking and fully
36 * asynchronous. The non-_send variants are synchronous.
40 * Connect to ctdb using the specified domain socket.
41 * Returns a ctdb context if successful or NULL.
44 struct ctdb_connection *ctdb_connect(const char *addr,
45 ctdb_log_fn_t log_fn, void *log_priv);
47 int ctdb_get_fd(struct ctdb_connection *ctdb);
49 int ctdb_which_events(struct ctdb_connection *ctdb);
51 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
55 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req);
58 * Callback for completed requests: it would normally unpack the request
59 * using ctdb_*_recv().
60 * You must free the request using ctdb_request_free().
62 * Note that due to macro magic, your callback doesn't have to take void *,
63 * it can take a type which matches the actual private parameter.
65 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
66 struct ctdb_request *req, void *private);
69 * Special node addresses :
71 /* used on the domain socket, send a pdu to the local daemon */
72 #define CTDB_CURRENT_NODE 0xF0000001
73 /* send a broadcast to all nodes in the cluster, active or not */
74 #define CTDB_BROADCAST_ALL 0xF0000002
75 /* send a broadcast to all nodes in the current vnn map */
76 #define CTDB_BROADCAST_VNNMAP 0xF0000003
77 /* send a broadcast to all connected nodes */
78 #define CTDB_BROADCAST_CONNECTED 0xF0000004
82 * functions to attach to a database
83 * if the database does not exist it will be created.
85 * You have to free the handle with ctdb_detach_db() when finished with it.
90 ctdb_attachdb_send(struct ctdb_connection *ctdb,
91 const char *name, int persistent, uint32_t tdb_flags,
92 ctdb_callback_t callback, void *private_data);
94 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
95 struct ctdb_request *req);
97 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
98 const char *name, int persistent,
104 * functions to read a record from the database
105 * when the callback is invoked, the client will hold an exclusive lock
106 * on the record, the client MUST NOT block during holding this lock and MUST
107 * release it quickly by performing ctdb_release_lock(lock).
109 * When the lock is released, data is freed too, so make sure to copy the data
112 * This returns true on success: the callback may have already been called,
113 * or it might be awaiting a response from ctdbd.
115 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
116 struct ctdb_lock *lock,
120 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
121 ctdb_rrl_callback_t callback, void *private_data);
123 /* Returns null on failure. */
124 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_db *ctdb_db, TDB_DATA key,
128 * Function to write data to a record
129 * This function may ONLY be called while holding a lock to the record
130 * created by ctdb_readrecordlock*, and before calling
131 * ctdb_release_lock() to release the lock.
133 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
136 void ctdb_release_lock(struct ctdb_lock *lock);
139 * messaging functions
140 * these functions provide a messaging layer for applications to communicate
141 * with eachother across
143 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *, uint64_t srvid, TDB_DATA data, void *);
145 struct ctdb_request *
146 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
147 ctdb_message_fn_t handler,
148 ctdb_callback_t callback,
151 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
152 struct ctdb_request *handle);
154 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
155 ctdb_message_fn_t handler, void *private_data);
160 * unregister a message handler and stop listening on teh specified port
162 struct ctdb_request *
163 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
164 ctdb_callback_t callback,
167 bool ctdb_remove_message_handler_recv(struct ctdb_request *handle);
169 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
174 * send a message to a specific node/port
175 * this function is non-blocking
177 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
182 * functions to read the pnn number of the local node
184 struct ctdb_request *
185 ctdb_getpnn_send(struct ctdb_connection *ctdb,
187 ctdb_callback_t callback,
189 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
190 struct ctdb_request *req, uint32_t *pnn);
192 bool ctdb_getpnn(struct ctdb_connection *ctdb,
200 * functions to read the recovery master of a node
202 struct ctdb_request *
203 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
205 ctdb_callback_t callback,
207 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
208 struct ctdb_request *handle,
209 uint32_t *recmaster);
210 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
212 uint32_t *recmaster);
220 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
224 * functions for logging errors
226 extern int ctdb_log_level; /* LOG_WARNING and above by default. */
227 void ctdb_log_file(FILE *, int severity, const char *format, va_list ap);
230 /* These ugly macro wrappers make the callbacks typesafe. */
231 #include <ctdb_typesafe_cb.h>
232 #define ctdb_sendcb(cb, cbdata) \
233 typesafe_cb_preargs(void, (cb), (cbdata), \
234 struct ctdb_connection *, struct ctdb_request *)
236 #define ctdb_connect(addr, log, logpriv) \
237 ctdb_connect((addr), \
238 typesafe_cb_postargs(void, (log), (logpriv), \
239 int, const char *, va_list), \
243 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
244 ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags), \
245 ctdb_sendcb((cb), (cbdata)), (cbdata))
247 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata) \
248 ctdb_readrecordlock_async((_ctdb_db), (key), \
249 typesafe_cb_preargs(void, (cb), (cbdata), \
250 struct ctdb_db *, struct ctdb_lock *, \
253 #define ctdb_set_message_handler_send(ctdb, srvid, handler, cb, cbdata) \
254 ctdb_set_message_handler_send((ctdb), (srvid), (handler), \
255 ctdb_sendcb((cb), (cbdata)), (cbdata))
257 #define ctdb_remove_message_handler_send(ctdb, srvid, cb, cbdata) \
258 ctdb_remove_message_handler_send((ctdb), (srvid), \
259 ctdb_sendcb((cb), (cbdata)), (cbdata))
261 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata) \
262 ctdb_getpnn_send((ctdb), (destnode), \
263 ctdb_sendcb((cb), (cbdata)), (cbdata))
265 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata) \
266 ctdb_getrecmaster_send((ctdb), (destnode), \
267 ctdb_sendcb((cb), (cbdata)), (cbdata))