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 if (strcmp(transport, "tcp") == 0) {
37 return ctdb_tcp_init(ctdb);
39 ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
46 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
52 set max acess count before a dmaster migration
54 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count)
56 ctdb->max_lacount = count;
60 add a node to the list of active nodes
62 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
64 struct ctdb_node *node, **nodep;
66 nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
67 CTDB_NO_MEMORY(ctdb, nodep);
70 nodep = &ctdb->nodes[ctdb->num_nodes];
71 (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
72 CTDB_NO_MEMORY(ctdb, *nodep);
75 if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
79 node->name = talloc_asprintf(node, "%s:%u",
80 node->address.address,
82 /* for now we just set the vnn to the line in the file - this
84 node->vnn = ctdb->num_nodes;
86 if (ctdb->methods->add_node(node) != 0) {
91 if (ctdb_same_address(&ctdb->address, &node->address)) {
92 ctdb->vnn = node->vnn;
101 setup the node list from a file
103 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
109 lines = file_lines_load(nlist, &nlines, ctdb);
111 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
115 for (i=0;i<nlines;i++) {
116 if (ctdb_add_node(ctdb, lines[i]) != 0) {
127 setup the local node address
129 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
131 if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
135 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
136 ctdb->address.address,
142 add a node to the list of active nodes
144 int ctdb_set_call(struct ctdb_context *ctdb, ctdb_fn_t fn, int id)
146 struct ctdb_registered_call *call;
148 call = talloc(ctdb, struct ctdb_registered_call);
152 DLIST_ADD(ctdb->calls, call);
157 return the vnn of this node
159 uint32_t ctdb_get_vnn(struct ctdb_context *ctdb)
165 start the protocol going
167 int ctdb_start(struct ctdb_context *ctdb)
169 return ctdb->methods->start(ctdb);
173 called by the transport layer when a packet comes in
175 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
177 struct ctdb_req_header *hdr;
178 if (length < sizeof(*hdr)) {
179 ctdb_set_error(ctdb, "Bad packet length %d\n", length);
182 hdr = (struct ctdb_req_header *)data;
183 if (length != hdr->length) {
184 ctdb_set_error(ctdb, "Bad header length %d expected %d\n",
185 hdr->length, length);
189 switch (hdr->operation) {
191 ctdb_request_call(ctdb, hdr);
194 case CTDB_REPLY_CALL:
195 ctdb_reply_call(ctdb, hdr);
198 case CTDB_REPLY_ERROR:
199 ctdb_reply_error(ctdb, hdr);
202 case CTDB_REPLY_REDIRECT:
203 ctdb_reply_redirect(ctdb, hdr);
206 case CTDB_REQ_DMASTER:
207 ctdb_request_dmaster(ctdb, hdr);
210 case CTDB_REPLY_DMASTER:
211 ctdb_reply_dmaster(ctdb, hdr);
215 printf("Packet with unknown operation %d\n", hdr->operation);
222 called by the transport layer when a node is dead
224 static void ctdb_node_dead(struct ctdb_node *node)
226 node->ctdb->num_connected--;
227 printf("%s: node %s is dead: %d connected\n",
228 node->ctdb->name, node->name, node->ctdb->num_connected);
232 called by the transport layer when a node is dead
234 static void ctdb_node_connected(struct ctdb_node *node)
236 node->ctdb->num_connected++;
237 printf("%s: connected to %s - %d connected\n",
238 node->ctdb->name, node->name, node->ctdb->num_connected);
242 wait for all nodes to be connected
244 void ctdb_connect_wait(struct ctdb_context *ctdb)
246 int expected = ctdb->num_nodes - 1;
247 if (ctdb->flags & CTDB_FLAG_SELF_CONNECT) {
250 while (ctdb->num_connected != expected) {
251 event_loop_once(ctdb->ev);
256 wait until we're the only node left
258 void ctdb_wait_loop(struct ctdb_context *ctdb)
261 if (ctdb->flags & CTDB_FLAG_SELF_CONNECT) {
264 while (ctdb->num_connected > expected) {
265 event_loop_once(ctdb->ev);
269 static const struct ctdb_upcalls ctdb_upcalls = {
270 .recv_pkt = ctdb_recv_pkt,
271 .node_dead = ctdb_node_dead,
272 .node_connected = ctdb_node_connected
276 initialise the ctdb daemon.
278 NOTE: In current code the daemon does not fork. This is for testing purposes only
279 and to simplify the code.
281 struct ctdb_context *ctdb_init(struct event_context *ev)
283 struct ctdb_context *ctdb;
285 ctdb = talloc_zero(ev, struct ctdb_context);
287 ctdb->upcalls = &ctdb_upcalls;
288 ctdb->idr = idr_init(ctdb);
289 ctdb->max_lacount = CTDB_DEFAULT_MAX_LACOUNT;