libctdb: API changes from Ronnie's version
[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 <tdb.h>
26
27 /* All *_send() functions are guaranteed to be non-blocking and fully
28  * asynchronous.  The non-_send variants are synchronous. */
29
30 /*
31  * Connect to ctdb using the specified domain socket.
32  * Returns a ctdb context if successful or NULL.
33  *
34  * Use ctdb_free() to release the returned ctdb_connection when finished.
35  */
36 struct ctdb_connection *ctdb_connect(const char *addr);
37
38 int ctdb_get_fd(struct ctdb_connection *ctdb);
39
40 int ctdb_which_events(struct ctdb_connection *ctdb);
41
42 int ctdb_service(struct ctdb_connection *ctdb, int revents);
43
44
45 /*
46  * Special node addresses :
47  */
48 /* used on the domain socket, send a pdu to the local daemon */
49 #define CTDB_CURRENT_NODE     0xF0000001
50 /* send a broadcast to all nodes in the cluster, active or not */
51 #define CTDB_BROADCAST_ALL    0xF0000002
52 /* send a broadcast to all nodes in the current vnn map */
53 #define CTDB_BROADCAST_VNNMAP 0xF0000003
54 /* send a broadcast to all connected nodes */
55 #define CTDB_BROADCAST_CONNECTED 0xF0000004
56
57
58 struct ctdb_request;
59
60 /*
61  * functions to attach to a database
62  * if the database does not exist it will be created.
63  *
64  * You have to free the handle with ctdb_detach_db() when finished with it.
65  */
66 struct ctdb_db;
67
68 typedef void (*ctdb_attachdb_cb)(int status, struct ctdb_db *ctdb_db, void *private_data);
69
70 struct ctdb_request *
71 ctdb_attachdb_send(struct ctdb_connection *ctdb,
72                    const char *name, int persistent, uint32_t tdb_flags,
73                    ctdb_attachdb_cb callback,
74                    void *private_data);
75 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
76                               const char *name, int persistent,
77                               uint32_t tdb_flags);
78
79 struct ctdb_lock;
80
81 /*
82  * functions to read a record from the database
83  * when the callback is invoked, the client will hold an exclusive lock
84  * on the record, the client MUST NOT block during holding this lock and MUST
85  * release it quickly by performing ctdb_release_lock(lock).
86  *
87  * When the lock is released, data is freed too, so make sure to copy the data
88  * before that.
89  */
90 typedef void (*ctdb_readrecordlock_cb)(int status, struct ctdb_lock *lock, TDB_DATA data, void *private_data);
91
92 struct ctdb_request *
93 ctdb_readrecordlock_send(struct ctdb_db *ctdb_db,
94                 TDB_DATA key,
95                 ctdb_readrecordlock_cb callback,
96                 void *private_data);
97 int ctdb_readrecordlock_recv(struct ctdb_connection *ctdb,
98                 struct ctdb_request *handle,
99                 TDB_DATA **data);
100 int ctdb_readrecordlock(struct ctdb_connection *ctdb,
101                 struct ctdb_db *ctdb_db,
102                 TDB_DATA key,
103                 TDB_DATA **data);
104
105
106
107 /*
108  * Function to write data to a record
109  * This function may ONLY be called while holding a lock to the record
110  * created by ctdb_readrecordlock*
111  * Either from the callback provided to ctdb_readrecordlock_send()
112  * or after calling ctdb_readrecordlock_recv() but before calling
113  * ctdb_release_lock() to release the lock.
114  */
115 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
116
117
118 void ctdb_release_lock(struct ctdb_lock *lock);
119
120 /*
121  * messaging functions
122  * these functions provide a messaging layer for applications to communicate
123  * with eachother across
124  */
125 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *, uint64_t srvid, TDB_DATA data, void *);
126
127 /*
128  * register a message handler and start listening on a service port
129  */
130 typedef void (*ctdb_set_message_handler_cb)(int status, void *private_data);
131
132 struct ctdb_request *
133 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
134                               ctdb_set_message_handler_cb callback,
135                               ctdb_message_fn_t handler, void *private_data);
136
137 int ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
138                                   struct ctdb_request *handle);
139
140 int ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
141                              ctdb_message_fn_t handler, void *private_data);
142
143
144
145 /*
146  * unregister a message handler and stop listening on teh specified port
147  */
148 typedef void (*ctdb_remove_message_handler_cb)(int status, void *private_data);
149
150 struct ctdb_request *
151 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
152                                  ctdb_remove_message_handler_cb callback,
153                                  void *private_data);
154
155 int ctdb_remove_message_handler_recv(struct ctdb_connection *ctdb,
156                                   struct ctdb_request *handle);
157
158 int ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
159
160
161
162 /*
163  * send a message to a specific node/port
164  * this function is non-blocking
165  */
166 int ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
167
168
169
170 /*
171  * functions to read the pnn number of the local node
172  */
173 typedef void (*ctdb_getpnn_cb)(int status, uint32_t pnn, void *private_data);
174
175 struct ctdb_request *
176 ctdb_getpnn_send(struct ctdb_connection *ctdb,
177                  uint32_t destnode,
178                  ctdb_getpnn_cb callback,
179                  void *private_data);
180 int ctdb_getpnn(struct ctdb_connection *ctdb,
181                 uint32_t destnode,
182                 uint32_t *pnn);
183
184
185
186
187 /*
188  * functions to read the recovery master of a node
189  */
190 typedef void (*ctdb_getrecmaster_cb)(int status,
191                                      uint32_t recmaster, void *private_data);
192
193 struct ctdb_request *
194 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
195                         uint32_t destnode,
196                         ctdb_getrecmaster_cb callback,
197                         void *private_data);
198 int ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
199                         struct ctdb_request *handle,
200                         uint32_t *recmaster);
201 int ctdb_getrecmaster(struct ctdb_connection *ctdb,
202                         uint32_t destnode,
203                         uint32_t *recmaster);
204
205
206
207
208 /*
209  * cancel a request
210  */
211 int ctdb_cancel(struct ctdb_request *);
212
213 #endif