040cd0c2e47d263cc6006a6f8b13a929469f89a2
[rusty/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 <tdb.h>
27
28 /* All *_send() functions are guaranteed to be non-blocking and fully
29  * asynchronous.  The non-_send variants are synchronous. */
30
31 /*
32  * Connect to ctdb using the specified domain socket.
33  * Returns a ctdb context if successful or NULL.
34  *
35  * Use ctdb_free() to release the returned ctdb_connection when finished.
36  */
37 struct ctdb_connection *ctdb_connect(const char *addr);
38
39 int ctdb_get_fd(struct ctdb_connection *ctdb);
40
41 int ctdb_which_events(struct ctdb_connection *ctdb);
42
43 int ctdb_service(struct ctdb_connection *ctdb, int revents);
44
45 struct ctdb_request;
46
47 void ctdb_request_free(struct ctdb_request *req);
48
49 /*
50  * Callback for completed requests: it would normally unpack the request
51  * using ctdb_*_recv().  You must free the request using ctdb_request_free().
52  *
53  * Note that due to macro magic, your callback doesn't have to take void *,
54  * it can take a type which matches the actual private parameter.
55  */
56 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
57                                 struct ctdb_request *req, void *private);
58
59 /*
60  * Special node addresses :
61  */
62 /* used on the domain socket, send a pdu to the local daemon */
63 #define CTDB_CURRENT_NODE     0xF0000001
64 /* send a broadcast to all nodes in the cluster, active or not */
65 #define CTDB_BROADCAST_ALL    0xF0000002
66 /* send a broadcast to all nodes in the current vnn map */
67 #define CTDB_BROADCAST_VNNMAP 0xF0000003
68 /* send a broadcast to all connected nodes */
69 #define CTDB_BROADCAST_CONNECTED 0xF0000004
70
71
72 /*
73  * functions to attach to a database
74  * if the database does not exist it will be created.
75  *
76  * You have to free the handle with ctdb_detach_db() when finished with it.
77  */
78 struct ctdb_db;
79
80 struct ctdb_request *
81 ctdb_attachdb_send(struct ctdb_connection *ctdb,
82                    const char *name, int persistent, uint32_t tdb_flags,
83                    ctdb_callback_t callback, void *private_data);
84
85 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_request *req);
86
87 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
88                               const char *name, int persistent,
89                               uint32_t tdb_flags);
90
91 struct ctdb_lock;
92
93 /*
94  * functions to read a record from the database
95  * when the callback is invoked, the client will hold an exclusive lock
96  * on the record, the client MUST NOT block during holding this lock and MUST
97  * release it quickly by performing ctdb_release_lock(lock).
98  *
99  * When the lock is released, data is freed too, so make sure to copy the data
100  * before that.
101  *
102  * This returns true on success, and req will be non-NULL if a request was
103  * actually sent, otherwise callback will have already been called.
104  */
105 bool
106 ctdb_readrecordlock_send(struct ctdb_db *ctdb_db, TDB_DATA key,
107                          struct ctdb_request **req,
108                          ctdb_callback_t callback, void *private_data);
109 struct ctdb_lock *ctdb_readrecordlock_recv(struct ctdb_db *ctdb_db,
110                                            struct ctdb_request *handle,
111                                            TDB_DATA *data);
112
113 /* Returns null on failure. */
114 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_db *ctdb_db, TDB_DATA key,
115                                       TDB_DATA *data);
116
117 /*
118  * Function to write data to a record
119  * This function may ONLY be called while holding a lock to the record
120  * created by ctdb_readrecordlock*
121  * Either from the callback provided to ctdb_readrecordlock_send()
122  * or after calling ctdb_readrecordlock_recv() but before calling
123  * ctdb_release_lock() to release the lock.
124  */
125 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
126
127
128 void ctdb_release_lock(struct ctdb_lock *lock);
129
130 /*
131  * messaging functions
132  * these functions provide a messaging layer for applications to communicate
133  * with eachother across
134  */
135 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *, uint64_t srvid, TDB_DATA data, void *);
136
137 struct ctdb_request *
138 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
139                               ctdb_message_fn_t handler,
140                               ctdb_callback_t callback,
141                               void *private_data);
142
143 int ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
144                                   struct ctdb_request *handle);
145
146 int ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
147                              ctdb_message_fn_t handler, void *private_data);
148
149
150
151 /*
152  * unregister a message handler and stop listening on teh specified port
153  */
154 struct ctdb_request *
155 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
156                                  ctdb_callback_t callback,
157                                  void *private_data);
158
159 int ctdb_remove_message_handler_recv(struct ctdb_request *handle);
160
161 int ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
162
163
164
165 /*
166  * send a message to a specific node/port
167  * this function is non-blocking
168  */
169 int ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
170
171
172
173 /*
174  * functions to read the pnn number of the local node
175  */
176 struct ctdb_request *
177 ctdb_getpnn_send(struct ctdb_connection *ctdb,
178                  uint32_t destnode,
179                  ctdb_callback_t callback,
180                  void *private_data);
181 int ctdb_getpnn_recv(struct ctdb_request *req, uint32_t *pnn);
182
183 int ctdb_getpnn(struct ctdb_connection *ctdb,
184                 uint32_t destnode,
185                 uint32_t *pnn);
186
187
188
189
190 /*
191  * functions to read the recovery master of a node
192  */
193 struct ctdb_request *
194 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
195                         uint32_t destnode,
196                         ctdb_callback_t callback,
197                         void *private_data);
198 int ctdb_getrecmaster_recv(struct ctdb_request *handle,
199                            uint32_t *recmaster);
200 int ctdb_getrecmaster(struct ctdb_connection *ctdb,
201                         uint32_t destnode,
202                         uint32_t *recmaster);
203
204
205
206
207 /*
208  * cancel a request
209  */
210 int ctdb_cancel(struct ctdb_request *);
211
212
213 /* These ugly macro wrappers make the callbacks typesafe. */
214 #include <ctdb_typesafe_cb.h>
215 #define ctdb_sendcb(cb, cbdata)                                         \
216          typesafe_cb_preargs(void, (cb), (cbdata),                      \
217                              struct ctdb_connection *, struct ctdb_request *)
218
219 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
220         ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags),   \
221                            ctdb_sendcb((cb), (cbdata)), (cbdata))
222
223 #define ctdb_readrecordlock_send(ctdb_db, key, reqp, cb, cbdata)        \
224         ctdb_readrecordlock_send((ctdb_db), (key), (reqp),              \
225                                  ctdb_sendcb((cb), (cbdata)), (cbdata))
226
227 #define ctdb_set_message_handler_send(ctdb, srvid, handler, cb, cbdata) \
228         ctdb_set_message_handler_send((ctdb), (srvid), (handler),       \
229               ctdb_sendcb((cb), (cbdata)), (cbdata))
230
231 #define ctdb_remove_message_handler_send(ctdb, srvid, cb, cbdata)       \
232         ctdb_remove_message_handler_send((ctdb), (srvid),               \
233               ctdb_sendcb((cb), (cbdata)), (cbdata))
234
235 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata)                    \
236         ctdb_getpnn_send((ctdb), (destnode),                            \
237                          ctdb_sendcb((cb), (cbdata)), (cbdata))
238
239 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata)              \
240         ctdb_getrecmaster_send((ctdb), (destnode),                      \
241                                ctdb_sendcb((cb), (cbdata)), (cbdata))
242 #endif