1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
Tests for ctdb_takeover.c
Copyright (C) Martin Schwenke 2011
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/network.h"
#include <assert.h>
#include <talloc.h>
#include "lib/util/debug.h"
#include "protocol/protocol.h"
#include "protocol/protocol_util.h"
#include "common/logging.h"
#include "common/system.h"
#include "server/ipalloc.h"
#include "ipalloc_read_known_ips.h"
static void print_ctdb_public_ip_list(TALLOC_CTX *mem_ctx,
struct public_ip_list * ips)
{
while (ips) {
printf("%s %d\n",
ctdb_sock_addr_to_string(mem_ctx, &(ips->addr), false),
ips->pnn);
ips = ips->next;
}
}
static uint32_t *get_tunable_values(TALLOC_CTX *tmp_ctx,
int numnodes,
const char *tunable);
static enum ctdb_runstate *get_runstate(TALLOC_CTX *tmp_ctx,
int numnodes);
static void read_ctdb_public_ip_info(TALLOC_CTX *ctx,
int numnodes,
bool multi,
struct ctdb_public_ip_list ** known,
struct ctdb_public_ip_list ** avail)
{
int n;
enum ctdb_runstate *runstate;
*known = ipalloc_read_known_ips(ctx, numnodes, multi);
assert(*known != NULL);
*avail = talloc_zero_array(ctx, struct ctdb_public_ip_list,
numnodes);
assert(*avail != NULL);
runstate = get_runstate(ctx, numnodes);
for (n = 0; n < numnodes; n++) {
if (runstate[n] == CTDB_RUNSTATE_RUNNING) {
(*avail)[n] = (*known)[n];
}
}
}
static uint32_t *get_tunable_values(TALLOC_CTX *tmp_ctx,
int numnodes,
const char *tunable)
{
int i;
char *tok;
uint32_t *tvals = talloc_zero_array(tmp_ctx, uint32_t, numnodes);
char *t = getenv(tunable);
if (t == NULL) {
return tvals;
}
if (strcmp(t, "1") == 0) {
for (i = 0; i < numnodes; i++) {
tvals[i] = 1;
}
} else {
tok = strtok(t, ",");
i = 0;
while (tok != NULL) {
tvals[i] = (uint32_t)strtol(tok, NULL, 0);
i++;
tok = strtok(NULL, ",");
}
if (i != numnodes) {
fprintf(stderr,
"ERROR: Wrong number of values in %s\n",
tunable);
exit(1);
}
}
return tvals;
}
static enum ctdb_runstate *get_runstate(TALLOC_CTX *tmp_ctx,
int numnodes)
{
int i;
uint32_t *tvals;
enum ctdb_runstate *runstate =
talloc_zero_array(tmp_ctx, enum ctdb_runstate, numnodes);
char *t = getenv("CTDB_TEST_RUNSTATE");
if (t == NULL) {
for (i=0; i<numnodes; i++) {
runstate[i] = CTDB_RUNSTATE_RUNNING;
}
} else {
tvals = get_tunable_values(tmp_ctx, numnodes, "CTDB_TEST_RUNSTATE");
for (i=0; i<numnodes; i++) {
runstate[i] = (enum ctdb_runstate) tvals[i];
}
talloc_free(tvals);
}
return runstate;
}
/* Fake up enough CTDB state to be able to run the IP allocation
* algorithm. Usually this sets up some standard state, sets the node
* states from the command-line and reads the current IP layout from
* stdin.
*
* However, if read_ips_for_multiple_nodes is true then each node's
* idea of the IP layout is read separately from stdin. In this mode
* is doesn't make much sense to use read_ctdb_public_ip_info's
* optional ALLOWED_PNN,... list in the input, since each node is
* being handled separately anyway. IPs for each node are separated
* by a blank line. This mode is for testing weird behaviours where
* the IP layouts differs across nodes and we want to improve
* create_merged_ip_list(), so should only be used in tests of
* ipalloc(). Yes, it is a hack... :-)
*/
static void ctdb_test_init(TALLOC_CTX *mem_ctx,
const char nodestates[],
struct ipalloc_state **ipalloc_state,
bool read_ips_for_multiple_nodes)
{
struct ctdb_public_ip_list *known;
struct ctdb_public_ip_list *avail;
char *tok, *ns;
const char *t;
struct ctdb_node_map *nodemap;
uint32_t noiptakeover;
ctdb_sock_addr sa_zero = { .ip = { 0 } };
enum ipalloc_algorithm algorithm;
uint32_t n;
/* Avoid that const */
ns = talloc_strdup(mem_ctx, nodestates);
nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
assert(nodemap != NULL);
nodemap->num = 0;
tok = strtok(ns, ",");
while (tok != NULL) {
n = nodemap->num;
nodemap->node = talloc_realloc(nodemap, nodemap->node,
struct ctdb_node_and_flags, n+1);
nodemap->node[n].pnn = n;
nodemap->node[n].flags = (uint32_t) strtol(tok, NULL, 0);
nodemap->node[n].addr = sa_zero;
nodemap->num++;
tok = strtok(NULL, ",");
}
algorithm = IPALLOC_LCP2;
if ((t = getenv("CTDB_IP_ALGORITHM"))) {
if (strcmp(t, "lcp2") == 0) {
algorithm = IPALLOC_LCP2;
} else if (strcmp(t, "nondet") == 0) {
algorithm = IPALLOC_NONDETERMINISTIC;
} else if (strcmp(t, "det") == 0) {
algorithm = IPALLOC_DETERMINISTIC;
} else {
DEBUG(DEBUG_ERR,
("ERROR: unknown IP algorithm %s\n", t));
exit(1);
}
}
t = getenv("CTDB_SET_NoIPTakeover");
if (t != NULL) {
noiptakeover = (uint32_t) strtol(t, NULL, 0);
} else {
noiptakeover = 0;
}
*ipalloc_state = ipalloc_state_init(mem_ctx, nodemap->num,
algorithm,
(noiptakeover != 0),
false,
NULL);
assert(*ipalloc_state != NULL);
read_ctdb_public_ip_info(mem_ctx, nodemap->num,
read_ips_for_multiple_nodes,
&known, &avail);
/* Drop available IPs for INACTIVE/DISABLED nodes */
for (n = 0; n < nodemap->num; n++) {
uint32_t flags = nodemap->node[n].flags;
if ((flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED)) != 0) {
avail[n].num = 0;
}
}
ipalloc_set_public_ips(*ipalloc_state, known, avail);
}
/* IP layout is read from stdin. See comment for ctdb_test_init() for
* explanation of read_ips_for_multiple_nodes.
*/
static void ctdb_test_ipalloc(const char nodestates[],
bool read_ips_for_multiple_nodes)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
struct ipalloc_state *ipalloc_state;
ctdb_test_init(tmp_ctx, nodestates, &ipalloc_state,
read_ips_for_multiple_nodes);
print_ctdb_public_ip_list(tmp_ctx, ipalloc(ipalloc_state));
talloc_free(tmp_ctx);
}
static void usage(void)
{
fprintf(stderr, "usage: ctdb_takeover_tests <op>\n");
exit(1);
}
int main(int argc, const char *argv[])
{
int loglevel;
const char *debuglevelstr = getenv("CTDB_TEST_LOGLEVEL");
setup_logging("ctdb_takeover_tests", DEBUG_STDERR);
if (! debug_level_parse(debuglevelstr, &loglevel)) {
loglevel = DEBUG_DEBUG;
}
debuglevel_set(loglevel);
if (argc < 2) {
usage();
}
if (argc == 3 &&
strcmp(argv[1], "ipalloc") == 0) {
ctdb_test_ipalloc(argv[2], false);
} else if (argc == 4 &&
strcmp(argv[1], "ipalloc") == 0 &&
strcmp(argv[3], "multi") == 0) {
ctdb_test_ipalloc(argv[2], true);
} else {
usage();
}
return 0;
}
|