2 ctdb main protocol code
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 2 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
22 #include "lib/tdb/include/tdb.h"
23 #include "lib/events/events.h"
24 #include "lib/util/dlinklist.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "../include/ctdb_private.h"
30 choose the transport we will use
32 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
34 int ctdb_tcp_init(struct ctdb_context *ctdb);
36 int ctdb_ibw_init(struct ctdb_context *ctdb);
37 #endif /* USE_INFINIBAND */
39 if (strcmp(transport, "tcp") == 0) {
40 return ctdb_tcp_init(ctdb);
43 if (strcmp(transport, "ib") == 0) {
44 return ctdb_ibw_init(ctdb);
46 #endif /* USE_INFINIBAND */
48 ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
55 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
63 void ctdb_clear_flags(struct ctdb_context *ctdb, unsigned flags)
65 ctdb->flags &= ~flags;
69 set max acess count before a dmaster migration
71 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count)
73 ctdb->max_lacount = count;
77 set the directory for the local databases
79 int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir)
81 ctdb->db_directory = talloc_strdup(ctdb, dir);
82 if (ctdb->db_directory == NULL) {
89 add a node to the list of active nodes
91 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
93 struct ctdb_node *node, **nodep;
95 nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
96 CTDB_NO_MEMORY(ctdb, nodep);
99 nodep = &ctdb->nodes[ctdb->num_nodes];
100 (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
101 CTDB_NO_MEMORY(ctdb, *nodep);
104 if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
108 node->name = talloc_asprintf(node, "%s:%u",
109 node->address.address,
111 /* for now we just set the vnn to the line in the file - this
113 node->vnn = ctdb->num_nodes;
115 if (ctdb->methods->add_node(node) != 0) {
120 if (ctdb_same_address(&ctdb->address, &node->address)) {
121 ctdb->vnn = node->vnn;
130 setup the node list from a file
132 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
138 lines = file_lines_load(nlist, &nlines, ctdb);
140 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
144 for (i=0;i<nlines;i++) {
145 if (ctdb_add_node(ctdb, lines[i]) != 0) {
156 setup the local node address
158 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
160 if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
164 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
165 ctdb->address.address,
171 add a node to the list of active nodes
173 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, int id)
175 struct ctdb_registered_call *call;
177 call = talloc(ctdb_db, struct ctdb_registered_call);
181 DLIST_ADD(ctdb_db->calls, call);
186 return the vnn of this node
188 uint32_t ctdb_get_vnn(struct ctdb_context *ctdb)
194 return the number of nodes
196 uint32_t ctdb_get_num_nodes(struct ctdb_context *ctdb)
198 return ctdb->num_nodes;
203 called by the transport layer when a packet comes in
205 void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
207 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
210 /* place the packet as a child of the tmp_ctx. We then use
211 talloc_free() below to free it. If any of the calls want
212 to keep it, then they will steal it somewhere else, and the
213 talloc_free() will only free the tmp_ctx */
214 tmp_ctx = talloc_new(ctdb);
215 talloc_steal(tmp_ctx, hdr);
217 if (length < sizeof(*hdr)) {
218 ctdb_set_error(ctdb, "Bad packet length %d\n", length);
221 if (length != hdr->length) {
222 ctdb_set_error(ctdb, "Bad header length %d expected %d\n",
223 hdr->length, length);
227 if (hdr->ctdb_magic != CTDB_MAGIC) {
228 ctdb_set_error(ctdb, "Non CTDB packet rejected\n");
232 if (hdr->ctdb_version != CTDB_VERSION) {
233 ctdb_set_error(ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
237 DEBUG(3,(__location__ " ctdb request %d of type %d length %d from "
238 "node %d to %d\n", hdr->reqid, hdr->operation, hdr->length,
239 hdr->srcnode, hdr->destnode));
241 switch (hdr->operation) {
243 ctdb_request_call(ctdb, hdr);
246 case CTDB_REPLY_CALL:
247 ctdb_reply_call(ctdb, hdr);
250 case CTDB_REPLY_ERROR:
251 ctdb_reply_error(ctdb, hdr);
254 case CTDB_REPLY_REDIRECT:
255 ctdb_reply_redirect(ctdb, hdr);
258 case CTDB_REQ_DMASTER:
259 ctdb_request_dmaster(ctdb, hdr);
262 case CTDB_REPLY_DMASTER:
263 ctdb_reply_dmaster(ctdb, hdr);
266 case CTDB_REQ_MESSAGE:
267 ctdb_request_message(ctdb, hdr);
270 case CTDB_REQ_FINISHED:
271 ctdb_request_finished(ctdb, hdr);
275 DEBUG(0,("%s: Packet with unknown operation %d\n",
276 __location__, hdr->operation));
281 talloc_free(tmp_ctx);
285 called by the transport layer when a node is dead
287 static void ctdb_node_dead(struct ctdb_node *node)
289 node->ctdb->num_connected--;
290 DEBUG(1,("%s: node %s is dead: %d connected\n",
291 node->ctdb->name, node->name, node->ctdb->num_connected));
295 called by the transport layer when a node is connected
297 static void ctdb_node_connected(struct ctdb_node *node)
299 node->ctdb->num_connected++;
300 DEBUG(1,("%s: connected to %s - %d connected\n",
301 node->ctdb->name, node->name, node->ctdb->num_connected));
305 wait for all nodes to be connected
307 void ctdb_daemon_connect_wait(struct ctdb_context *ctdb)
309 int expected = ctdb->num_nodes - 1;
310 if (ctdb->flags & CTDB_FLAG_SELF_CONNECT) {
313 while (ctdb->num_connected != expected) {
314 DEBUG(3,("ctdb_connect_wait: waiting for %d nodes (have %d)\n",
315 expected, ctdb->num_connected));
316 event_loop_once(ctdb->ev);
318 DEBUG(3,("ctdb_connect_wait: got all %d nodes\n", expected));
322 queue a packet or die
324 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
326 struct ctdb_node *node;
327 node = ctdb->nodes[hdr->destnode];
328 if (ctdb->methods->queue_pkt(node, (uint8_t *)hdr, hdr->length) != 0) {
329 ctdb_fatal(ctdb, "Unable to queue packet\n");
334 static const struct ctdb_upcalls ctdb_upcalls = {
335 .recv_pkt = ctdb_recv_pkt,
336 .node_dead = ctdb_node_dead,
337 .node_connected = ctdb_node_connected
341 initialise the ctdb daemon.
343 NOTE: In current code the daemon does not fork. This is for testing purposes only
344 and to simplify the code.
346 struct ctdb_context *ctdb_init(struct event_context *ev)
348 struct ctdb_context *ctdb;
350 ctdb = talloc_zero(ev, struct ctdb_context);
352 ctdb->upcalls = &ctdb_upcalls;
353 ctdb->idr = idr_init(ctdb);
354 ctdb->max_lacount = CTDB_DEFAULT_MAX_LACOUNT;