changed ctdb_bench.c to use messages instead of calls
[martins/samba.git] / ctdb / common / ctdb.c
1 /* 
2    ctdb main protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
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.
10
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.
15
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
19 */
20
21 #include "includes.h"
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"
28
29 /*
30   choose the transport we will use
31 */
32 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
33 {
34         int ctdb_tcp_init(struct ctdb_context *ctdb);
35
36         if (strcmp(transport, "tcp") == 0) {
37                 return ctdb_tcp_init(ctdb);
38         }
39         ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
40         return -1;
41 }
42
43 /*
44   set some ctdb flags
45 */
46 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
47 {
48         ctdb->flags |= flags;
49 }
50
51 /*
52   set max acess count before a dmaster migration
53 */
54 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count)
55 {
56         ctdb->max_lacount = count;
57 }
58
59 /*
60   add a node to the list of active nodes
61 */
62 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
63 {
64         struct ctdb_node *node, **nodep;
65
66         nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
67         CTDB_NO_MEMORY(ctdb, nodep);
68
69         ctdb->nodes = nodep;
70         nodep = &ctdb->nodes[ctdb->num_nodes];
71         (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
72         CTDB_NO_MEMORY(ctdb, *nodep);
73         node = *nodep;
74
75         if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
76                 return -1;
77         }
78         node->ctdb = ctdb;
79         node->name = talloc_asprintf(node, "%s:%u", 
80                                      node->address.address, 
81                                      node->address.port);
82         /* for now we just set the vnn to the line in the file - this
83            will change! */
84         node->vnn = ctdb->num_nodes;
85
86         if (ctdb->methods->add_node(node) != 0) {
87                 talloc_free(node);
88                 return -1;
89         }
90
91         if (ctdb_same_address(&ctdb->address, &node->address)) {
92                 ctdb->vnn = node->vnn;
93         }
94
95         ctdb->num_nodes++;
96
97         return 0;
98 }
99
100 /*
101   setup the node list from a file
102 */
103 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
104 {
105         char **lines;
106         int nlines;
107         int i;
108
109         lines = file_lines_load(nlist, &nlines, ctdb);
110         if (lines == NULL) {
111                 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
112                 return -1;
113         }
114
115         for (i=0;i<nlines;i++) {
116                 if (ctdb_add_node(ctdb, lines[i]) != 0) {
117                         talloc_free(lines);
118                         return -1;
119                 }
120         }
121         
122         talloc_free(lines);
123         return 0;
124 }
125
126 /*
127   setup the local node address
128 */
129 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
130 {
131         if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
132                 return -1;
133         }
134         
135         ctdb->name = talloc_asprintf(ctdb, "%s:%u", 
136                                      ctdb->address.address, 
137                                      ctdb->address.port);
138         return 0;
139 }
140
141 /*
142   add a node to the list of active nodes
143 */
144 int ctdb_set_call(struct ctdb_context *ctdb, ctdb_fn_t fn, int id)
145 {
146         struct ctdb_registered_call *call;
147
148         call = talloc(ctdb, struct ctdb_registered_call);
149         call->fn = fn;
150         call->id = id;
151
152         DLIST_ADD(ctdb->calls, call);   
153         return 0;
154 }
155
156 /*
157   return the vnn of this node
158 */
159 uint32_t ctdb_get_vnn(struct ctdb_context *ctdb)
160 {
161         return ctdb->vnn;
162 }
163
164 /*
165   return the number of nodes
166 */
167 uint32_t ctdb_get_num_nodes(struct ctdb_context *ctdb)
168 {
169         return ctdb->num_nodes;
170 }
171
172
173 /*
174   start the protocol going
175 */
176 int ctdb_start(struct ctdb_context *ctdb)
177 {
178         return ctdb->methods->start(ctdb);
179 }
180
181 /*
182   called by the transport layer when a packet comes in
183 */
184 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
185 {
186         struct ctdb_req_header *hdr;
187
188         if (length < sizeof(*hdr)) {
189                 ctdb_set_error(ctdb, "Bad packet length %d\n", length);
190                 return;
191         }
192         hdr = (struct ctdb_req_header *)data;
193         if (length != hdr->length) {
194                 ctdb_set_error(ctdb, "Bad header length %d expected %d\n", 
195                                hdr->length, length);
196                 return;
197         }
198
199         switch (hdr->operation) {
200         case CTDB_REQ_CALL:
201                 ctdb_request_call(ctdb, hdr);
202                 break;
203
204         case CTDB_REPLY_CALL:
205                 ctdb_reply_call(ctdb, hdr);
206                 break;
207
208         case CTDB_REPLY_ERROR:
209                 ctdb_reply_error(ctdb, hdr);
210                 break;
211
212         case CTDB_REPLY_REDIRECT:
213                 ctdb_reply_redirect(ctdb, hdr);
214                 break;
215
216         case CTDB_REQ_DMASTER:
217                 ctdb_request_dmaster(ctdb, hdr);
218                 break;
219
220         case CTDB_REPLY_DMASTER:
221                 ctdb_reply_dmaster(ctdb, hdr);
222                 break;
223
224         case CTDB_REQ_MESSAGE:
225                 ctdb_request_message(ctdb, hdr);
226                 break;
227
228         default:
229                 printf("Packet with unknown operation %d\n", hdr->operation);
230                 talloc_free(hdr);
231                 break;
232         }
233 }
234
235 /*
236   called by the transport layer when a node is dead
237 */
238 static void ctdb_node_dead(struct ctdb_node *node)
239 {
240         node->ctdb->num_connected--;
241         printf("%s: node %s is dead: %d connected\n", 
242                node->ctdb->name, node->name, node->ctdb->num_connected);
243 }
244
245 /*
246   called by the transport layer when a node is connected
247 */
248 static void ctdb_node_connected(struct ctdb_node *node)
249 {
250         node->ctdb->num_connected++;
251         printf("%s: connected to %s - %d connected\n", 
252                node->ctdb->name, node->name, node->ctdb->num_connected);
253 }
254
255 /*
256   wait for all nodes to be connected
257 */
258 void ctdb_connect_wait(struct ctdb_context *ctdb)
259 {
260         int expected = ctdb->num_nodes - 1;
261         if (ctdb->flags & CTDB_FLAG_SELF_CONNECT) {
262                 expected++;
263         }
264         while (ctdb->num_connected != expected) {
265                 event_loop_once(ctdb->ev);
266         }
267 }
268
269 /*
270   wait until we're the only node left
271 */
272 void ctdb_wait_loop(struct ctdb_context *ctdb)
273 {
274         int expected = 0;
275         if (ctdb->flags & CTDB_FLAG_SELF_CONNECT) {
276                 expected++;
277         }
278         while (ctdb->num_connected > expected) {
279                 event_loop_once(ctdb->ev);
280         }
281 }
282
283
284 /*
285   queue a packet or die
286 */
287 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
288 {
289         struct ctdb_node *node;
290         node = ctdb->nodes[hdr->destnode];
291         if (ctdb->methods->queue_pkt(node, (uint8_t *)hdr, hdr->length) != 0) {
292                 ctdb_fatal(ctdb, "Unable to queue packet\n");
293         }
294 }
295
296
297 static const struct ctdb_upcalls ctdb_upcalls = {
298         .recv_pkt       = ctdb_recv_pkt,
299         .node_dead      = ctdb_node_dead,
300         .node_connected = ctdb_node_connected
301 };
302
303 /*
304   initialise the ctdb daemon. 
305
306   NOTE: In current code the daemon does not fork. This is for testing purposes only
307   and to simplify the code.
308 */
309 struct ctdb_context *ctdb_init(struct event_context *ev)
310 {
311         struct ctdb_context *ctdb;
312
313         ctdb = talloc_zero(ev, struct ctdb_context);
314         ctdb->ev = ev;
315         ctdb->upcalls = &ctdb_upcalls;
316         ctdb->idr = idr_init(ctdb);
317         ctdb->max_lacount = CTDB_DEFAULT_MAX_LACOUNT;
318
319         return ctdb;
320 }
321