4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef _CTDB_PRIVATE_H
22 #define _CTDB_PRIVATE_H
26 /* location of daemon socket */
27 #define CTDB_PATH "/tmp/ctdb.socket"
29 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
30 #define CTDB_DS_ALIGNMENT 8
33 #define CTDB_NULL_FUNC 0xf0000001
36 an installed ctdb remote call
38 struct ctdb_registered_call {
39 struct ctdb_registered_call *next, *prev;
45 this address structure might need to be generalised later for some
56 #define ctdb_validate_vnn(ctdb, vnn) (((uint32_t)(vnn)) < (ctdb)->num_nodes)
59 /* called from the queue code when a packet comes in. Called with data==NULL
61 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
66 state associated with one node
69 struct ctdb_context *ctdb;
70 struct ctdb_address address;
71 const char *name; /* for debug messages */
72 void *private_data; /* private to transport */
77 transport specific methods
80 int (*start)(struct ctdb_context *); /* start protocol processing */
81 int (*add_node)(struct ctdb_node *); /* setup a new node */
82 int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
83 void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
87 transport calls up to the ctdb layer
90 /* recv_pkt is called when a packet comes in */
91 void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
93 /* node_dead is called when an attempt to send to a node fails */
94 void (*node_dead)(struct ctdb_node *);
96 /* node_connected is called when a connection to a node is established */
97 void (*node_connected)(struct ctdb_node *);
100 /* list of message handlers - needs to be changed to a more efficient data
101 structure so we can find a message handler given a srvid quickly */
102 struct ctdb_message_list {
103 struct ctdb_context *ctdb;
104 struct ctdb_message_list *next, *prev;
106 ctdb_message_fn_t message_handler;
107 void *message_private;
110 /* additional data required for the daemon mode */
111 struct ctdb_daemon_data {
114 struct ctdb_queue *queue;
118 ctdb status information
121 uint32_t client_packets_sent;
122 uint32_t client_packets_recv;
123 uint32_t node_packets_sent;
124 uint32_t node_packets_recv;
128 uint32_t reply_redirect;
129 uint32_t req_dmaster;
130 uint32_t reply_dmaster;
131 uint32_t reply_error;
132 uint32_t req_message;
133 uint32_t req_finished;
137 uint32_t req_message;
138 uint32_t req_finished;
139 uint32_t req_register;
140 uint32_t req_connect_wait;
141 uint32_t req_shutdown;
144 uint32_t total_calls;
145 uint32_t pending_calls;
146 uint32_t lockwait_calls;
147 uint32_t pending_lockwait_calls;
148 double max_call_latency;
149 double max_lockwait_latency;
152 /* main state of the ctdb daemon */
153 struct ctdb_context {
154 struct event_context *ev;
155 struct ctdb_address address;
157 const char *db_directory;
158 uint32_t vnn; /* our own vnn */
160 uint32_t num_connected;
161 uint32_t num_finished;
163 struct idr_context *idr;
164 struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
166 const struct ctdb_methods *methods; /* transport methods */
167 const struct ctdb_upcalls *upcalls; /* transport upcalls */
168 void *private_data; /* private to transport */
169 unsigned max_lacount;
170 struct ctdb_db_context *db_list;
171 struct ctdb_message_list *message_list;
172 struct ctdb_daemon_data daemon;
173 struct ctdb_status status;
176 struct ctdb_db_context {
177 struct ctdb_db_context *next, *prev;
178 struct ctdb_context *ctdb;
181 struct tdb_wrap *ltdb;
182 struct ctdb_registered_call *calls; /* list of registered calls */
186 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
187 ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
188 return -1; }} while (0)
190 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
191 ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
192 return NULL; }} while (0)
194 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
195 ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
198 /* arbitrary maximum timeout for ctdb operations */
199 #define CTDB_REQ_TIMEOUT 10
201 /* max number of redirects before we ask the lmaster */
202 #define CTDB_MAX_REDIRECT 2
204 /* number of consecutive calls from the same node before we give them
206 #define CTDB_DEFAULT_MAX_LACOUNT 7
209 the extended header for records in the ltdb
211 struct ctdb_ltdb_header {
218 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
221 state of a in-progress ctdb call
223 struct ctdb_call_state {
224 enum call_state state;
225 struct ctdb_req_call *c;
226 struct ctdb_db_context *ctdb_db;
227 struct ctdb_node *node;
229 struct ctdb_call call;
231 struct ctdb_ltdb_header header;
233 void (*fn)(struct ctdb_call_state *);
239 /* used for fetch_lock */
240 struct ctdb_fetch_handle {
241 struct ctdb_db_context *ctdb_db;
244 struct ctdb_ltdb_header header;
250 enum ctdb_operation {
253 CTDB_REPLY_REDIRECT = 2,
254 CTDB_REQ_DMASTER = 3,
255 CTDB_REPLY_DMASTER = 4,
256 CTDB_REPLY_ERROR = 5,
257 CTDB_REQ_MESSAGE = 6,
258 CTDB_REQ_FINISHED = 7,
260 /* only used on the domain socket */
261 CTDB_REQ_REGISTER = 1000,
262 CTDB_REQ_CONNECT_WAIT = 1001,
263 CTDB_REPLY_CONNECT_WAIT = 1002,
264 CTDB_REQ_SHUTDOWN = 1003,
265 CTDB_REQ_STATUS = 1004,
266 CTDB_REPLY_STATUS = 1005
269 #define CTDB_MAGIC 0x43544442 /* CTDB */
270 #define CTDB_VERSION 1
275 struct ctdb_req_header {
278 uint32_t ctdb_version;
285 struct ctdb_req_call {
286 struct ctdb_req_header hdr;
291 uint32_t calldatalen;
292 uint8_t data[1]; /* key[] followed by calldata[] */
295 struct ctdb_reply_call {
296 struct ctdb_req_header hdr;
302 struct ctdb_reply_error {
303 struct ctdb_req_header hdr;
309 struct ctdb_reply_redirect {
310 struct ctdb_req_header hdr;
314 struct ctdb_req_dmaster {
315 struct ctdb_req_header hdr;
323 struct ctdb_reply_dmaster {
324 struct ctdb_req_header hdr;
329 struct ctdb_req_register {
330 struct ctdb_req_header hdr;
334 struct ctdb_req_message {
335 struct ctdb_req_header hdr;
341 struct ctdb_req_finished {
342 struct ctdb_req_header hdr;
345 struct ctdb_req_shutdown {
346 struct ctdb_req_header hdr;
349 struct ctdb_req_connect_wait {
350 struct ctdb_req_header hdr;
353 struct ctdb_reply_connect_wait {
354 struct ctdb_req_header hdr;
356 uint32_t num_connected;
359 struct ctdb_req_status {
360 struct ctdb_req_header hdr;
363 struct ctdb_reply_status {
364 struct ctdb_req_header hdr;
365 struct ctdb_status status;
368 /* internal prototypes */
369 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
370 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
371 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
372 int ctdb_parse_address(struct ctdb_context *ctdb,
373 TALLOC_CTX *mem_ctx, const char *str,
374 struct ctdb_address *address);
375 uint32_t ctdb_hash(const TDB_DATA *key);
376 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
377 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
378 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
379 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
380 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
381 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
382 void ctdb_reply_redirect(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
384 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
385 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db,
386 TDB_DATA key, struct ctdb_ltdb_header *header,
387 TALLOC_CTX *mem_ctx, TDB_DATA *data);
388 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key,
389 struct ctdb_ltdb_header *header, TDB_DATA data);
390 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
391 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db,
392 TDB_DATA key, struct ctdb_req_header *hdr,
393 void (*recv_pkt)(void *, uint8_t *, uint32_t ),
395 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db,
396 TDB_DATA key, struct ctdb_ltdb_header *header,
397 struct ctdb_req_header *hdr, TDB_DATA *data,
398 void (*recv_pkt)(void *, uint8_t *, uint32_t ),
400 void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length);
402 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
403 struct ctdb_call *call,
404 struct ctdb_ltdb_header *header,
408 int ctdbd_start(struct ctdb_context *ctdb);
409 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
410 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
413 queue a packet for sending
415 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
418 setup the fd used by the queue
420 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
423 setup a packet queue on a socket
425 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
426 TALLOC_CTX *mem_ctx, int fd, int alignment,
428 ctdb_queue_cb_fn_t callback,
432 allocate a packet for use in client<->daemon communication
434 void *ctdbd_allocate_pkt(TALLOC_CTX *mem_ctx, size_t len);
438 lock a record in the ltdb, given a key
440 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
443 unlock a record in the ltdb, given a key
445 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
449 make a ctdb call to the local daemon - async send. Called from client context.
451 This constructs a ctdb_call request and queues it for processing.
452 This call never blocks.
454 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db,
455 struct ctdb_call *call);
458 make a recv call to the local ctdb daemon - called from client context
460 This is called when the program wants to wait for a ctdb_call to complete and get the
461 results. This call will block unless the call has already completed.
463 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
465 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint32_t srvid,
466 ctdb_message_fn_t handler,
469 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
470 uint32_t srvid, TDB_DATA data);
475 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
476 uint32_t srvid, TDB_DATA data);
480 wait for all nodes to be connected
482 void ctdb_daemon_connect_wait(struct ctdb_context *ctdb);
485 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
487 void (*callback)(void *), void *private_data);
489 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db,
490 struct ctdb_call *call);
492 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
494 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
495 struct ctdb_call *call,
496 struct ctdb_ltdb_header *header);
498 void ctdb_request_finished(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
500 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
501 struct ctdb_ltdb_header *header, TDB_DATA *data,
504 void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location);
505 #define idr_find_type(idp, id, type) (type *)_idr_find_type(idp, id, #type, __location__)
507 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
509 int ctdb_socket_connect(struct ctdb_context *ctdb);
511 void ctdb_latency(double *latency, struct timeval t);