4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
23 #include <netinet/in.h>
25 #define CTDB_IMMEDIATE_MIGRATION 0x00000001
36 structure passed to a ctdb call backend function
38 struct ctdb_call_info {
39 TDB_DATA key; /* record key */
40 TDB_DATA record_data; /* current data in the record */
41 TDB_DATA *new_data; /* optionally updated record data */
42 TDB_DATA *call_data; /* optionally passed from caller */
43 TDB_DATA *reply_data; /* optionally returned by function */
44 uint32_t status; /* optional reply status - defaults to zero */
47 #define CTDB_ERR_INVALID 1
48 #define CTDB_ERR_NOMEM 2
53 #define CTDB_FLAG_TORTURE (1<<1)
56 a message handler ID meaning "give me all messages"
58 #define CTDB_SRVID_ALL (~(uint64_t)0)
63 #define CTDB_SRVID_RECOVERY 0xF100000000000000LL
66 a message handler ID meaning that the cluster has been reconfigured
68 #define CTDB_SRVID_RECONFIGURE 0xF200000000000000LL
71 a message handler ID meaning that an IP address has been released
73 #define CTDB_SRVID_RELEASE_IP 0xF300000000000000LL
76 a message ID to set the node flags in the recovery daemon
78 #define CTDB_SRVID_SET_NODE_FLAGS 0xF400000000000000LL
81 a message ID meaning that a node should be banned
83 #define CTDB_SRVID_BAN_NODE 0xF500000000000000LL
86 a message ID meaning that a node should be unbanned
88 #define CTDB_SRVID_UNBAN_NODE 0xF600000000000000LL
91 a message to tell the recovery daemon to fetch a set of records
93 #define CTDB_SRVID_VACUUM_FETCH 0xF700000000000000LL
96 a message to tell the recovery daemon to write a talloc memdump
99 #define CTDB_SRVID_MEM_DUMP 0xF800000000000000LL
102 a message ID to get the recovery daemon to push the node flags out
104 #define CTDB_SRVID_PUSH_NODE_FLAGS 0xF900000000000000LL
108 /* used on the domain socket, send a pdu to the local daemon */
109 #define CTDB_CURRENT_NODE 0xF0000001
110 /* send a broadcast to all nodes in the cluster, active or not */
111 #define CTDB_BROADCAST_ALL 0xF0000002
112 /* send a broadcast to all nodes in the current vnn map */
113 #define CTDB_BROADCAST_VNNMAP 0xF0000003
114 /* send a broadcast to all connected nodes */
115 #define CTDB_BROADCAST_CONNECTED 0xF0000004
117 /* the key used for transaction locking on persistent databases */
118 #define CTDB_TRANSACTION_LOCK_KEY "__transaction_lock__"
120 enum control_state {CTDB_CONTROL_WAIT, CTDB_CONTROL_DONE, CTDB_CONTROL_ERROR, CTDB_CONTROL_TIMEOUT};
122 struct ctdb_client_control_state {
123 struct ctdb_context *ctdb;
127 enum control_state state;
129 struct ctdb_req_control *c;
131 /* if we have a callback registered for the completion (or failure) of
133 if a callback is used, it MUST talloc_free the cb_data passed to it
136 void (*fn)(struct ctdb_client_control_state *);
142 struct event_context;
145 initialise ctdb subsystem
147 struct ctdb_context *ctdb_init(struct event_context *ev);
152 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport);
155 set the directory for the local databases
157 int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir);
158 int ctdb_set_tdb_dir_persistent(struct ctdb_context *ctdb, const char *dir);
163 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags);
166 set max acess count before a dmaster migration
168 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count);
171 tell ctdb what address to listen on, in transport specific format
173 int ctdb_set_address(struct ctdb_context *ctdb, const char *address);
175 int ctdb_set_socketname(struct ctdb_context *ctdb, const char *socketname);
178 tell ctdb what nodes are available. This takes a filename, which will contain
179 1 node address per line, in a transport specific format
181 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist);
184 Check that a specific ip address exists in the node list and returns
185 the id for the node or -1
187 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const char *nodeip);
190 start the ctdb protocol
192 int ctdb_start(struct ctdb_context *ctdb);
193 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork);
196 attach to a ctdb database
198 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, bool persistent, uint32_t tdb_flags);
201 find an attached ctdb_db handle given a name
203 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name);
206 error string for last ctdb error
208 const char *ctdb_errstr(struct ctdb_context *);
210 /* a ctdb call function */
211 typedef int (*ctdb_fn_t)(struct ctdb_call_info *);
214 setup a ctdb call function
216 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, uint32_t id);
221 make a ctdb call. The associated ctdb call function will be called on the DMASTER
224 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
227 initiate an ordered ctdb cluster shutdown
228 this function will never return
230 void ctdb_shutdown(struct ctdb_context *ctdb);
232 /* return pnn of this node */
233 uint32_t ctdb_get_pnn(struct ctdb_context *ctdb);
236 return the number of nodes
238 uint32_t ctdb_get_num_nodes(struct ctdb_context *ctdb);
240 /* setup a handler for ctdb messages */
241 typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid,
242 TDB_DATA data, void *);
243 int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
244 ctdb_message_fn_t handler,
248 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
249 struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
250 int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call);
252 /* send a ctdb message */
253 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn,
254 uint64_t srvid, TDB_DATA data);
258 Fetch a ctdb record from a remote node
259 . Underneath this will force the
260 dmaster for the record to be moved to the local node.
262 struct ctdb_record_handle *ctdb_fetch_lock(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx,
263 TDB_DATA key, TDB_DATA *data);
265 int ctdb_record_store(struct ctdb_record_handle *h, TDB_DATA data);
267 int ctdb_fetch(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx,
268 TDB_DATA key, TDB_DATA *data);
270 int ctdb_register_message_handler(struct ctdb_context *ctdb,
273 ctdb_message_fn_t handler,
276 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id);
279 struct ctdb_context *ctdb_cmdline_client(struct event_context *ev);
281 struct ctdb_statistics;
282 int ctdb_ctrl_statistics(struct ctdb_context *ctdb, uint32_t destnode, struct ctdb_statistics *status);
284 int ctdb_ctrl_shutdown(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
287 int ctdb_ctrl_getvnnmap(struct ctdb_context *ctdb,
288 struct timeval timeout, uint32_t destnode,
289 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **vnnmap);
290 int ctdb_ctrl_setvnnmap(struct ctdb_context *ctdb,
291 struct timeval timeout, uint32_t destnode,
292 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map *vnnmap);
294 /* table that contains a list of all dbids on a node
296 struct ctdb_dbid_map {
303 int ctdb_ctrl_getdbmap(struct ctdb_context *ctdb,
304 struct timeval timeout, uint32_t destnode,
305 TALLOC_CTX *mem_ctx, struct ctdb_dbid_map **dbmap);
308 struct ctdb_node_map;
310 int ctdb_ctrl_getnodemap(struct ctdb_context *ctdb,
311 struct timeval timeout, uint32_t destnode,
312 TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap);
314 int ctdb_ctrl_getnodemapv4(struct ctdb_context *ctdb,
315 struct timeval timeout, uint32_t destnode,
316 TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap);
318 int ctdb_ctrl_reload_nodes_file(struct ctdb_context *ctdb,
319 struct timeval timeout, uint32_t destnode);
321 struct ctdb_key_list {
325 struct ctdb_ltdb_header *headers;
329 int ctdb_ctrl_pulldb(
330 struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
331 uint32_t lmaster, TALLOC_CTX *mem_ctx,
332 struct timeval timeout, TDB_DATA *outdata);
334 struct ctdb_client_control_state *ctdb_ctrl_pulldb_send(
335 struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
336 uint32_t lmaster, TALLOC_CTX *mem_ctx, struct timeval timeout);
338 int ctdb_ctrl_pulldb_recv(
339 struct ctdb_context *ctdb,
340 TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state,
343 int ctdb_ctrl_pushdb(
344 struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
346 struct timeval timeout, TDB_DATA indata);
348 struct ctdb_client_control_state *ctdb_ctrl_pushdb_send(
349 struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
350 TALLOC_CTX *mem_ctx, struct timeval timeout,
353 int ctdb_ctrl_pushdb_recv(
354 struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
355 struct ctdb_client_control_state *state);
358 int ctdb_ctrl_copydb(struct ctdb_context *ctdb,
359 struct timeval timeout, uint32_t sourcenode,
360 uint32_t destnode, uint32_t dbid, uint32_t lmaster,
361 TALLOC_CTX *mem_ctx);
363 int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **path);
364 int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **name);
365 int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, const char *name, bool persistent);
367 int ctdb_ctrl_process_exists(struct ctdb_context *ctdb, uint32_t destnode, pid_t pid);
369 int ctdb_ctrl_ping(struct ctdb_context *ctdb, uint32_t destnode);
371 int ctdb_ctrl_get_config(struct ctdb_context *ctdb);
373 int ctdb_ctrl_get_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, int32_t *level);
374 int ctdb_ctrl_set_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, int32_t level);
377 change dmaster for all keys in the database to the new value
379 int ctdb_ctrl_setdmaster(struct ctdb_context *ctdb,
380 struct timeval timeout, uint32_t destnode,
381 TALLOC_CTX *mem_ctx, uint32_t dbid, uint32_t dmaster);
384 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)
386 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);
388 #define CTDB_RECOVERY_NORMAL 0
389 #define CTDB_RECOVERY_ACTIVE 1
392 get the recovery mode of a remote node
394 int ctdb_ctrl_getrecmode(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmode);
396 struct ctdb_client_control_state *ctdb_ctrl_getrecmode_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
398 int ctdb_ctrl_getrecmode_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmode);
402 set the recovery mode of a remote node
404 int ctdb_ctrl_setrecmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmode);
406 get the monitoring mode of a remote node
408 int ctdb_ctrl_getmonmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *monmode);
411 set the monitoring mode of a remote node to active
413 int ctdb_ctrl_enable_monmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
416 set the monitoring mode of a remote node to disabled
418 int ctdb_ctrl_disable_monmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
422 get the recovery master of a remote node
424 int ctdb_ctrl_getrecmaster(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmaster);
426 struct ctdb_client_control_state *ctdb_ctrl_getrecmaster_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
428 int ctdb_ctrl_getrecmaster_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmaster);
433 set the recovery master of a remote node
435 int ctdb_ctrl_setrecmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmaster);
437 uint32_t *ctdb_get_connected_nodes(struct ctdb_context *ctdb,
438 struct timeval timeout,
440 uint32_t *num_nodes);
442 int ctdb_statistics_reset(struct ctdb_context *ctdb, uint32_t destnode);
444 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog);
446 typedef int (*ctdb_traverse_func)(struct ctdb_context *, TDB_DATA, TDB_DATA, void *);
447 int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *private_data);
449 int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f);
452 get the pid of a ctdb daemon
454 int ctdb_ctrl_getpid(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *pid);
456 int ctdb_ctrl_freeze(struct ctdb_context *ctdb, struct timeval timeout,
459 struct ctdb_client_control_state *
460 ctdb_ctrl_freeze_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
461 struct timeval timeout, uint32_t destnode);
463 int ctdb_ctrl_freeze_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
464 struct ctdb_client_control_state *state);
466 int ctdb_ctrl_thaw(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
468 int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
470 int ctdb_ctrl_get_tunable(struct ctdb_context *ctdb,
471 struct timeval timeout,
473 const char *name, uint32_t *value);
475 int ctdb_ctrl_set_tunable(struct ctdb_context *ctdb,
476 struct timeval timeout,
478 const char *name, uint32_t value);
480 int ctdb_ctrl_list_tunables(struct ctdb_context *ctdb,
481 struct timeval timeout,
484 const char ***list, uint32_t *count);
486 int ctdb_ctrl_modflags(struct ctdb_context *ctdb,
487 struct timeval timeout,
489 uint32_t set, uint32_t clear);
491 enum ctdb_server_id_type { SERVER_TYPE_SAMBA=1 };
493 struct ctdb_server_id {
494 enum ctdb_server_id_type type;
499 struct ctdb_server_id_list {
501 struct ctdb_server_id server_ids[1];
505 int ctdb_ctrl_register_server_id(struct ctdb_context *ctdb,
506 struct timeval timeout,
507 struct ctdb_server_id *id);
508 int ctdb_ctrl_unregister_server_id(struct ctdb_context *ctdb,
509 struct timeval timeout,
510 struct ctdb_server_id *id);
511 int ctdb_ctrl_check_server_id(struct ctdb_context *ctdb,
512 struct timeval timeout, uint32_t destnode,
513 struct ctdb_server_id *id, uint32_t *status);
514 int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
516 struct timeval timeout, uint32_t destnode,
517 struct ctdb_server_id_list **svid_list);
520 struct timeval current_time;
521 struct timeval ctdbd_start_time;
522 struct timeval last_recovery_started;
523 struct timeval last_recovery_finished;
527 definitions for different socket structures
529 typedef struct sockaddr_in ctdb_addr_in;
530 typedef struct sockaddr_in6 ctdb_addr_in6;
538 struct for tcp_client control
539 this is an ipv4 only version of this structure used by samba
540 samba will later be migrated over to use the
541 ctdb_control_tcp_addr structure instead
543 struct ctdb_control_tcp {
544 struct sockaddr_in src; // samba uses this
545 struct sockaddr_in dest;// samba uses this
547 /* new style structure */
548 struct ctdb_control_tcp_addr {
553 int ctdb_socket_connect(struct ctdb_context *ctdb);
556 get the uptime of a remote node
558 int ctdb_ctrl_uptime(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, struct ctdb_uptime **uptime);
560 struct ctdb_client_control_state *ctdb_ctrl_uptime_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
562 int ctdb_ctrl_uptime_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, struct ctdb_uptime **uptime);
564 int ctdb_ctrl_end_recovery(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
566 int ctdb_ctrl_getreclock(struct ctdb_context *ctdb,
567 struct timeval timeout, uint32_t destnode,
568 TALLOC_CTX *mem_ctx, const char **reclock);
571 uint32_t *list_of_connected_nodes(struct ctdb_context *ctdb,
572 struct ctdb_node_map *node_map,
575 uint32_t *list_of_active_nodes(struct ctdb_context *ctdb,
576 struct ctdb_node_map *node_map,
579 uint32_t *list_of_vnnmap_nodes(struct ctdb_context *ctdb,
580 struct ctdb_vnn_map *vnn_map,
584 int ctdb_read_pnn_lock(int fd, int32_t pnn);
587 get capabilities of a remote node
589 int ctdb_ctrl_getcapabilities(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *capabilities);
591 struct ctdb_client_control_state *ctdb_ctrl_getcapabilities_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
593 int ctdb_ctrl_getcapabilities_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *capabilities);
595 struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx,
596 struct ctdb_marshall_buffer *m,
600 struct ctdb_ltdb_header *header,
602 TDB_DATA ctdb_marshall_finish(struct ctdb_marshall_buffer *m);
604 struct ctdb_transaction_handle *ctdb_transaction_start(struct ctdb_db_context *ctdb_db,
605 TALLOC_CTX *mem_ctx);
606 int ctdb_transaction_fetch(struct ctdb_transaction_handle *h,
608 TDB_DATA key, TDB_DATA *data);
609 int ctdb_transaction_store(struct ctdb_transaction_handle *h,
610 TDB_DATA key, TDB_DATA data);
611 int ctdb_transaction_commit(struct ctdb_transaction_handle *h);
613 int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb);