add a ctdb uptime command that prints when ctdb was started and when the
[sahlberg/ctdb.git] / include / ctdb.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _CTDB_H
21 #define _CTDB_H
22
23 #define CTDB_IMMEDIATE_MIGRATION        0x00000001
24 struct ctdb_call {
25         int call_id;
26         TDB_DATA key;
27         TDB_DATA call_data;
28         TDB_DATA reply_data;
29         uint32_t status;
30         uint32_t flags;
31 };
32
33 /*
34   structure passed to a ctdb call backend function
35 */
36 struct ctdb_call_info {
37         TDB_DATA key;          /* record key */
38         TDB_DATA record_data;  /* current data in the record */
39         TDB_DATA *new_data;    /* optionally updated record data */
40         TDB_DATA *call_data;   /* optionally passed from caller */
41         TDB_DATA *reply_data;  /* optionally returned by function */
42         uint32_t status;       /* optional reply status - defaults to zero */
43 };
44
45 #define CTDB_ERR_INVALID 1
46 #define CTDB_ERR_NOMEM 2
47
48 /*
49   ctdb flags
50 */
51 #define CTDB_FLAG_TORTURE      (1<<1)
52
53 /* 
54    a message handler ID meaning "give me all messages"
55  */
56 #define CTDB_SRVID_ALL (~(uint64_t)0)
57
58 /*
59   srvid type : RECOVERY
60 */
61 #define CTDB_SRVID_RECOVERY     0xF100000000000000LL
62
63 /* 
64    a message handler ID meaning that the cluster has been reconfigured
65  */
66 #define CTDB_SRVID_RECONFIGURE 0xF200000000000000LL
67
68 /* 
69    a message handler ID meaning that an IP address has been released
70  */
71 #define CTDB_SRVID_RELEASE_IP 0xF300000000000000LL
72
73 /* 
74    a message ID meaning that a nodes flags have changed
75  */
76 #define CTDB_SRVID_NODE_FLAGS_CHANGED 0xF400000000000000LL
77
78 /* 
79    a message ID meaning that a node should be banned
80  */
81 #define CTDB_SRVID_BAN_NODE 0xF500000000000000LL
82
83 /* 
84    a message ID meaning that a node should be unbanned
85  */
86 #define CTDB_SRVID_UNBAN_NODE 0xF600000000000000LL
87
88 /*
89   a message to tell the recovery daemon to fetch a set of records
90  */
91 #define CTDB_SRVID_VACUUM_FETCH 0xF700000000000000LL
92
93
94 /* used on the domain socket, send a pdu to the local daemon */
95 #define CTDB_CURRENT_NODE     0xF0000001
96 /* send a broadcast to all nodes in the cluster, active or not */
97 #define CTDB_BROADCAST_ALL    0xF0000002
98 /* send a broadcast to all nodes in the current vnn map */
99 #define CTDB_BROADCAST_VNNMAP 0xF0000003
100 /* send a broadcast to all connected nodes */
101 #define CTDB_BROADCAST_CONNECTED 0xF0000004
102
103
104 enum control_state {CTDB_CONTROL_WAIT, CTDB_CONTROL_DONE, CTDB_CONTROL_ERROR, CTDB_CONTROL_TIMEOUT};
105
106 struct ctdb_client_control_state {
107         struct ctdb_context *ctdb;
108         uint32_t reqid;
109         int32_t status;
110         TDB_DATA outdata;
111         enum control_state state;
112         char *errormsg;
113         struct ctdb_req_control *c;
114
115         /* if we have a callback registered for the completion (or failure) of
116            this control
117            if a callback is used, it MUST talloc_free the cb_data passed to it
118         */
119         struct {
120                 void (*fn)(struct ctdb_client_control_state *);
121                 void *private_data;
122         } async;        
123 };
124
125
126 struct event_context;
127
128 /*
129   initialise ctdb subsystem
130 */
131 struct ctdb_context *ctdb_init(struct event_context *ev);
132
133 /*
134   choose the transport
135 */
136 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport);
137
138 /*
139   set the directory for the local databases
140 */
141 int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir);
142 int ctdb_set_tdb_dir_persistent(struct ctdb_context *ctdb, const char *dir);
143
144 /*
145   set some flags
146 */
147 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags);
148
149 /*
150   set max acess count before a dmaster migration
151 */
152 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count);
153
154 /*
155   tell ctdb what address to listen on, in transport specific format
156 */
157 int ctdb_set_address(struct ctdb_context *ctdb, const char *address);
158
159 int ctdb_set_socketname(struct ctdb_context *ctdb, const char *socketname);
160
161 /*
162   tell ctdb what nodes are available. This takes a filename, which will contain
163   1 node address per line, in a transport specific format
164 */
165 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist);
166
167 /*
168   Check that a specific ip address exists in the node list and returns
169   the id for the node or -1
170 */
171 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const char *nodeip);
172
173 /*
174   start the ctdb protocol
175 */
176 int ctdb_start(struct ctdb_context *ctdb);
177 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork);
178
179 /*
180   attach to a ctdb database
181 */
182 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, bool persistent);
183
184 /*
185   find an attached ctdb_db handle given a name
186  */
187 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name);
188
189 /*
190   error string for last ctdb error
191 */
192 const char *ctdb_errstr(struct ctdb_context *);
193
194 /* a ctdb call function */
195 typedef int (*ctdb_fn_t)(struct ctdb_call_info *);
196
197 /*
198   setup a ctdb call function
199 */
200 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, uint32_t id);
201
202
203
204 /*
205   make a ctdb call. The associated ctdb call function will be called on the DMASTER
206   for the given record
207 */
208 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
209
210 /*
211   initiate an ordered ctdb cluster shutdown
212   this function will never return
213 */
214 void ctdb_shutdown(struct ctdb_context *ctdb);
215
216 /* return pnn of this node */
217 uint32_t ctdb_get_pnn(struct ctdb_context *ctdb);
218
219 /*
220   return the number of nodes
221 */
222 uint32_t ctdb_get_num_nodes(struct ctdb_context *ctdb);
223
224 /* setup a handler for ctdb messages */
225 typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, 
226                                   TDB_DATA data, void *);
227 int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
228                              ctdb_message_fn_t handler,
229                              void *private_data);
230
231
232 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
233 struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
234 int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call);
235
236 /* send a ctdb message */
237 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn,
238                       uint64_t srvid, TDB_DATA data);
239
240
241 /* 
242    Fetch a ctdb record from a remote node
243  . Underneath this will force the
244    dmaster for the record to be moved to the local node. 
245 */
246 struct ctdb_record_handle *ctdb_fetch_lock(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx, 
247                                            TDB_DATA key, TDB_DATA *data);
248
249 int ctdb_record_store(struct ctdb_record_handle *h, TDB_DATA data);
250
251 int ctdb_fetch(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx, 
252                TDB_DATA key, TDB_DATA *data);
253
254 int ctdb_register_message_handler(struct ctdb_context *ctdb, 
255                                   TALLOC_CTX *mem_ctx,
256                                   uint64_t srvid,
257                                   ctdb_message_fn_t handler,
258                                   void *private_data);
259
260 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id);
261
262
263 struct ctdb_context *ctdb_cmdline_client(struct event_context *ev);
264
265 struct ctdb_statistics;
266 int ctdb_ctrl_statistics(struct ctdb_context *ctdb, uint32_t destnode, struct ctdb_statistics *status);
267
268 int ctdb_ctrl_shutdown(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
269
270 struct ctdb_vnn_map;
271 int ctdb_ctrl_getvnnmap(struct ctdb_context *ctdb, 
272                 struct timeval timeout, uint32_t destnode, 
273                 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **vnnmap);
274 int ctdb_ctrl_setvnnmap(struct ctdb_context *ctdb,
275                 struct timeval timeout, uint32_t destnode, 
276                 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map *vnnmap);
277
278 /* table that contains a list of all dbids on a node
279  */
280 struct ctdb_dbid_map {
281         uint32_t num;
282         struct ctdb_dbid {
283                 uint32_t dbid;
284                 bool persistent;
285         } dbs[1];
286 };
287 int ctdb_ctrl_getdbmap(struct ctdb_context *ctdb, 
288         struct timeval timeout, uint32_t destnode, 
289         TALLOC_CTX *mem_ctx, struct ctdb_dbid_map **dbmap);
290
291
292 struct ctdb_node_map;
293
294 int ctdb_ctrl_getnodemap(struct ctdb_context *ctdb, 
295                     struct timeval timeout, uint32_t destnode, 
296                     TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap);
297
298 struct ctdb_key_list {
299         uint32_t dbid;
300         uint32_t num;
301         TDB_DATA *keys;
302         struct ctdb_ltdb_header *headers;
303         TDB_DATA *data;
304 };
305
306 int ctdb_ctrl_pulldb(
307        struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
308        uint32_t lmaster, TALLOC_CTX *mem_ctx,
309        struct timeval timeout, TDB_DATA *outdata);
310
311 struct ctdb_client_control_state *ctdb_ctrl_pulldb_send(
312        struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
313        uint32_t lmaster, TALLOC_CTX *mem_ctx, struct timeval timeout);
314
315 int ctdb_ctrl_pulldb_recv(
316        struct ctdb_context *ctdb,
317        TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state,
318        TDB_DATA *outdata);
319
320 int ctdb_ctrl_pushdb(
321        struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
322        TALLOC_CTX *mem_ctx,
323        struct timeval timeout, TDB_DATA indata);
324
325 struct ctdb_client_control_state *ctdb_ctrl_pushdb_send(
326        struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
327        TALLOC_CTX *mem_ctx, struct timeval timeout,
328        TDB_DATA indata);
329
330 int ctdb_ctrl_pushdb_recv(
331        struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
332        struct ctdb_client_control_state *state);
333
334
335 int ctdb_ctrl_copydb(struct ctdb_context *ctdb, 
336         struct timeval timeout, uint32_t sourcenode, 
337         uint32_t destnode, uint32_t dbid, uint32_t lmaster, 
338         TALLOC_CTX *mem_ctx);
339
340 int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **path);
341 int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **name);
342 int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, const char *name, bool persistent);
343
344 int ctdb_ctrl_process_exists(struct ctdb_context *ctdb, uint32_t destnode, pid_t pid);
345
346 int ctdb_ctrl_ping(struct ctdb_context *ctdb, uint32_t destnode);
347
348 int ctdb_ctrl_get_config(struct ctdb_context *ctdb);
349
350 int ctdb_ctrl_get_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *level);
351 int ctdb_ctrl_set_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, uint32_t level);
352
353 /*
354   change dmaster for all keys in the database to the new value
355  */
356 int ctdb_ctrl_setdmaster(struct ctdb_context *ctdb, 
357         struct timeval timeout, uint32_t destnode, 
358         TALLOC_CTX *mem_ctx, uint32_t dbid, uint32_t dmaster);
359
360 /*
361   write a record on a specific db (this implicitely updates dmaster of the record to locally be the vnn of the node where the control is executed on)
362  */
363 int ctdb_ctrl_write_record(struct ctdb_context *ctdb, uint32_t destnode, TALLOC_CTX *mem_ctx, uint32_t dbid, TDB_DATA key, TDB_DATA data);
364
365 #define CTDB_RECOVERY_NORMAL            0
366 #define CTDB_RECOVERY_ACTIVE            1
367
368 /*
369   get the recovery mode of a remote node
370  */
371 int ctdb_ctrl_getrecmode(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmode);
372
373 struct ctdb_client_control_state *ctdb_ctrl_getrecmode_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
374
375 int ctdb_ctrl_getrecmode_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmode);
376
377
378 /*
379   set the recovery mode of a remote node
380  */
381 int ctdb_ctrl_setrecmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmode);
382 /*
383   get the monitoring mode of a remote node
384  */
385 int ctdb_ctrl_getmonmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *monmode);
386
387 /*
388   get the recovery master of a remote node
389  */
390 int ctdb_ctrl_getrecmaster(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmaster);
391
392 struct ctdb_client_control_state *ctdb_ctrl_getrecmaster_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
393
394 int ctdb_ctrl_getrecmaster_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmaster);
395
396
397
398 /*
399   set the recovery master of a remote node
400  */
401 int ctdb_ctrl_setrecmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmaster);
402
403 uint32_t *ctdb_get_connected_nodes(struct ctdb_context *ctdb, 
404                                    struct timeval timeout, 
405                                    TALLOC_CTX *mem_ctx,
406                                    uint32_t *num_nodes);
407
408 int ctdb_statistics_reset(struct ctdb_context *ctdb, uint32_t destnode);
409
410 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog);
411
412 typedef int (*ctdb_traverse_func)(struct ctdb_context *, TDB_DATA, TDB_DATA, void *);
413 int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *private_data);
414
415 int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f);
416
417 /*
418   get the pid of a ctdb daemon
419  */
420 int ctdb_ctrl_getpid(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *pid);
421
422 int ctdb_ctrl_freeze(struct ctdb_context *ctdb, struct timeval timeout, 
423                         uint32_t destnode);
424
425 struct ctdb_client_control_state *
426 ctdb_ctrl_freeze_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, 
427                         struct timeval timeout, uint32_t destnode);
428
429 int ctdb_ctrl_freeze_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, 
430                         struct ctdb_client_control_state *state);
431
432 int ctdb_ctrl_thaw(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
433
434 int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
435
436 int ctdb_ctrl_get_tunable(struct ctdb_context *ctdb, 
437                           struct timeval timeout, 
438                           uint32_t destnode,
439                           const char *name, uint32_t *value);
440
441 int ctdb_ctrl_set_tunable(struct ctdb_context *ctdb, 
442                           struct timeval timeout, 
443                           uint32_t destnode,
444                           const char *name, uint32_t value);
445
446 int ctdb_ctrl_list_tunables(struct ctdb_context *ctdb, 
447                             struct timeval timeout, 
448                             uint32_t destnode,
449                             TALLOC_CTX *mem_ctx,
450                             const char ***list, uint32_t *count);
451
452 int ctdb_ctrl_modflags(struct ctdb_context *ctdb, 
453                        struct timeval timeout, 
454                        uint32_t destnode, 
455                        uint32_t set, uint32_t clear);
456
457 enum ctdb_server_id_type { SERVER_TYPE_SAMBA=1 };
458
459 struct ctdb_server_id {
460         enum ctdb_server_id_type type;
461         uint32_t pnn;
462         uint32_t server_id;
463 };
464
465 struct ctdb_server_id_list {
466         uint32_t num;
467         struct ctdb_server_id server_ids[1];
468 };
469
470
471 int ctdb_ctrl_register_server_id(struct ctdb_context *ctdb,
472                 struct timeval timeout,
473                 struct ctdb_server_id *id);
474 int ctdb_ctrl_unregister_server_id(struct ctdb_context *ctdb, 
475                 struct timeval timeout, 
476                 struct ctdb_server_id *id);
477 int ctdb_ctrl_check_server_id(struct ctdb_context *ctdb,
478                 struct timeval timeout, uint32_t destnode, 
479                 struct ctdb_server_id *id, uint32_t *status);
480 int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
481                 TALLOC_CTX *mem_ctx,
482                 struct timeval timeout, uint32_t destnode, 
483                 struct ctdb_server_id_list **svid_list);
484
485 struct ctdb_uptime {
486         struct timeval current_time;
487         struct timeval ctdbd_start_time;
488         struct timeval last_recovery_time;
489 };
490
491 int ctdb_socket_connect(struct ctdb_context *ctdb);
492
493 /*
494   get the uptime of a remote node
495  */
496 int ctdb_ctrl_uptime(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, struct ctdb_uptime **uptime);
497
498 struct ctdb_client_control_state *ctdb_ctrl_uptime_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
499
500 int ctdb_ctrl_uptime_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, struct ctdb_uptime **uptime);
501
502 #endif