summaryrefslogtreecommitdiff
path: root/ctdb/libctdb/tst.c
blob: 1136aaab209739cc752c0cb3f832403ceb4f9dc1 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Example program to demonstrate the libctdb api
 *
 * 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/>.
 */

/*
 * This program needs to be linked with libtdb and libctdb
 * (You need these packages installed: libtdb libtdb-devel
 *  ctdb and ctdb-devel)
 *
 * This program can then be compiled using
 *    gcc -o tst tst.c -ltdb -lctdb
 *
 *
 */
#include <stdio.h>
#include <stdint.h>
#include <poll.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include <stdbool.h>
#include <syslog.h>
#include <tdb.h>
#include <ctdb.h>
#include <ctdb_protocol.h>

TDB_DATA key;


char *ctdb_addr_to_str(ctdb_sock_addr *addr)
{
	static char cip[128] = "";

	switch (addr->sa.sa_family) {
	case AF_INET:
		inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr, cip, sizeof(cip));
		break;
	case AF_INET6:
		inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr, cip, sizeof(cip));
		break;
	default:
		printf("ERROR, unknown family %u\n", addr->sa.sa_family);
	}

	return cip;
}

void print_nodemap(struct ctdb_node_map *nodemap)
{
	int i;

	printf("number of nodes:%d\n", nodemap->num);
	for (i=0;i<nodemap->num;i++) {
		printf("Node:%d Address:%s Flags:%s%s%s%s%s%s\n",
			nodemap->nodes[i].pnn,
			ctdb_addr_to_str(&nodemap->nodes[i].addr),
			nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED?"DISCONNECTED ":"",
			nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY?"UNHEALTHY ":"",
			nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED?"ADMIN DISABLED ":"",
			nodemap->nodes[i].flags&NODE_FLAGS_BANNED?"BANNED ":"",
			nodemap->nodes[i].flags&NODE_FLAGS_DELETED?"DELETED ":"",
			nodemap->nodes[i].flags&NODE_FLAGS_STOPPED?"STOPPED ":"");
	}
}

void msg_h(struct ctdb_connection *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
{
	printf("Message received on port %llx : %s\n", srvid, data.dptr);
}

void rip_h(struct ctdb_connection *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
{
	printf("RELEASE IP message for %s\n", data.dptr);
}

void tip_h(struct ctdb_connection *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
{
	printf("TAKE IP message for %s\n", data.dptr);
}

static void gnm_cb(struct ctdb_connection *ctdb,
		   struct ctdb_request *req, void *private)
{
	bool status;
	struct ctdb_node_map *nodemap;

	status = ctdb_getnodemap_recv(ctdb, req, &nodemap);
	ctdb_request_free(req);
	if (!status) {
		printf("Error reading NODEMAP\n");
		return;
	}
	printf("ASYNC response to getnodemap:\n");
	print_nodemap(nodemap);
	ctdb_free_nodemap(nodemap);
}

void print_ips(struct ctdb_all_public_ips *ips)
{
	int i;
	
	printf("Num public ips:%d\n", ips->num);
	for (i=0; i<ips->num;i++) {
		printf("%s    hosted on node %d\n",
			ctdb_addr_to_str(&ips->ips[i].addr),
			ips->ips[i].pnn);
	}
}

static void ips_cb(struct ctdb_connection *ctdb,
		   struct ctdb_request *req, void *private)
{
	bool status;
	struct ctdb_all_public_ips *ips;

	status = ctdb_getpublicips_recv(ctdb, req, &ips);
	ctdb_request_free(req);
	if (!status) {
		printf("Error reading PUBLIC IPS\n");
		return;
	}
	printf("ASYNC response to getpublicips:\n");
	print_ips(ips);
	ctdb_free_publicips(ips);
}

static void pnn_cb(struct ctdb_connection *ctdb,
		   struct ctdb_request *req, void *private)
{
	bool status;
	uint32_t pnn;

	status = ctdb_getpnn_recv(ctdb, req, &pnn);
	ctdb_request_free(req);
	if (!status) {
		printf("Error reading PNN\n");
		return;
	}
	printf("ASYNC RESPONSE TO GETPNN:  pnn:%d\n", pnn);
}

static void rm_cb(struct ctdb_connection *ctdb,
		  struct ctdb_request *req, void *private)
{
	bool status;
	uint32_t rm;

	status = ctdb_getrecmaster_recv(ctdb, req, &rm);
	ctdb_request_free(req);
	if (!status) {
		printf("Error reading RECMASTER\n");
		return;
	}

	printf("GETRECMASTER ASYNC: recmaster:%d\n", rm);
}

/*
 * example on how to first read(non-existing recortds are implicitely created
 * on demand) a record and change it in the callback.
 * This forms the atom for the read-modify-write cycle.
 *
 * Pure read, or pure write are just special cases of this cycle.
 */
static void rrl_cb(struct ctdb_db *ctdb_db,
		   struct ctdb_lock *lock, TDB_DATA outdata, void *private)
{
	TDB_DATA data;
	char tmp[256];
	bool *rrl_cb_called = private;

	*rrl_cb_called = true;

	if (!lock) {
		printf("rrl_cb returned error\n");
		return;
	}

	printf("rrl size:%d data:%.*s\n", outdata.dsize,
	       outdata.dsize, outdata.dptr);
	if (outdata.dsize == 0) {
		tmp[0] = 0;
	} else {
		strcpy(tmp, outdata.dptr);
	}
	strcat(tmp, "*");

	data.dptr  = tmp;
	data.dsize = strlen(tmp) + 1;
	if (!ctdb_writerecord(ctdb_db, lock, data))
		printf("Error writing data!\n");

	/* Release the lock as quickly as possible */
	ctdb_release_lock(ctdb_db, lock);

	printf("Wrote new record : %s\n", tmp);

}

static bool registered = false;
void message_handler_cb(struct ctdb_connection *ctdb,
			struct ctdb_request *req, void *private)
{
	if (!ctdb_set_message_handler_recv(ctdb, req)) {
		err(1, "registering message");
	}
	ctdb_request_free(req);
	printf("Message handler registered\n");
	registered = true;
}

static int traverse_callback(struct ctdb_connection *ctdb_connection, struct ctdb_db *ctdb_db, int status, TDB_DATA key, TDB_DATA data, void *private_data)
{
	if (status == TRAVERSE_STATUS_FINISHED) {
		printf("Traverse finished\n");
		return 0;
	}
	if (status == TRAVERSE_STATUS_ERROR) {
		printf("Traverse failed\n");
		return 1;
	}

	printf("traverse callback   status:%d\n", status);
	printf("key: %d [%s]\n", key.dsize, key.dptr);
	printf("data:%d [%s]\n", data.dsize, data.dptr);

	return 0;
}


int main(int argc, char *argv[])
{
	struct ctdb_connection *ctdb_connection;
	struct ctdb_request *handle;
	struct ctdb_db *ctdb_db_context;
	struct ctdb_node_map *nodemap;
	struct pollfd pfd;
	uint32_t recmaster;
	TDB_DATA msg;
	bool rrl_cb_called = false;
	uint64_t srvid;

	ctdb_log_level = LOG_DEBUG;
	ctdb_connection = ctdb_connect("/tmp/ctdb.socket",
				       ctdb_log_file, stderr);
	if (!ctdb_connection)
		err(1, "Connecting to /tmp/ctdb.socket");

	pfd.fd = ctdb_get_fd(ctdb_connection);

	srvid = CTDB_SRVID_TEST_RANGE|55;
	handle = ctdb_set_message_handler_send(ctdb_connection, srvid,
					       msg_h, NULL,
					       message_handler_cb, &srvid);
	if (handle == NULL) {
		printf("Failed to register message port\n");
		exit(10);
	}

	/* Hack for testing: this makes sure registrations went out. */
	while (!registered) {
		ctdb_service(ctdb_connection, POLLIN|POLLOUT);
	}

	handle = ctdb_set_message_handler_send(ctdb_connection,
					       CTDB_SRVID_RELEASE_IP,
					       rip_h, NULL,
					       message_handler_cb, NULL);
	if (handle == NULL) {
		printf("Failed to register message port for RELEASE IP\n");
		exit(10);
	}

	handle = ctdb_set_message_handler_send(ctdb_connection,
					       CTDB_SRVID_TAKE_IP,
					       tip_h, NULL,
					       message_handler_cb, NULL);
	if (handle == NULL) {
		printf("Failed to register message port for TAKE IP\n");
		exit(10);
	}

	msg.dptr="HelloWorld";
	msg.dsize = strlen(msg.dptr);

	srvid = CTDB_SRVID_TEST_RANGE|55;
	if (!ctdb_send_message(ctdb_connection, 0, srvid, msg)) {
		printf("Failed to send message. Aborting\n");
		exit(10);
	}

	handle = ctdb_getrecmaster_send(ctdb_connection, 0, rm_cb, NULL);
	if (handle == NULL) {
		printf("Failed to send get_recmaster control\n");
		exit(10);
	}

	ctdb_db_context = ctdb_attachdb(ctdb_connection, "test_test.tdb",
					false, 0);
	if (!ctdb_db_context) {
		printf("Failed to attach to database\n");
		exit(10);
	}

	/*
	 * SYNC call with callback to read the recmaster
	 * calls the blocking sync function.
	 * Avoid this mode for performance critical tasks
	 */
	if (!ctdb_getrecmaster(ctdb_connection, CTDB_CURRENT_NODE, &recmaster)) {
		printf("Failed to receive response to getrecmaster\n");
		exit(10);
	}
	printf("GETRECMASTER SYNC: recmaster:%d\n", recmaster);


	handle = ctdb_getpnn_send(ctdb_connection, CTDB_CURRENT_NODE,
				  pnn_cb, NULL);
	if (handle == NULL) {
		printf("Failed to send get_pnn control\n");
		exit(10);
	}

	/* In the non-contended case the callback might be invoked
	 * immediately, before ctdb_readrecordlock_async() returns.
	 * In the contended case the callback will be invoked later.
	 *
	 * Normally an application would not care whether the callback
	 * has already been invoked here or not, but if the application
	 * needs to know, it can use the *private_data pointer
	 * to pass data through to the callback and back.
	 */
	if (!ctdb_readrecordlock_async(ctdb_db_context, key,
				       rrl_cb, &rrl_cb_called)) {
		printf("Failed to send READRECORDLOCK\n");
		exit(10);
	}
	if (!rrl_cb_called) {
		printf("READRECORDLOCK is async\n");
	}

	/*
	 * Read the nodemap from a node (async)
	 */
	handle = ctdb_getnodemap_send(ctdb_connection, CTDB_CURRENT_NODE,
				  gnm_cb, NULL);
	if (handle == NULL) {
		printf("Failed to send get_nodemap control\n");
		exit(10);
	}

	/*
	 * Read the list of public ips from a node (async)
	 */
	handle = ctdb_getpublicips_send(ctdb_connection, CTDB_CURRENT_NODE,
				  ips_cb, NULL);
	if (handle == NULL) {
		printf("Failed to send getpublicips control\n");
		exit(10);
	}

	/*
	 * Read the nodemap from a node (sync)
	 */
	if (!ctdb_getnodemap(ctdb_connection, CTDB_CURRENT_NODE,
			     &nodemap)) {
		printf("Failed to receive response to getrecmaster\n");
		exit(10);
	}
	printf("SYNC response to getnodemap:\n");
	print_nodemap(nodemap);
	ctdb_free_nodemap(nodemap);

	printf("Traverse the test_test.tdb database\n");
	ctdb_traverse_async(ctdb_db_context, traverse_callback, NULL);

	for (;;) {

	  pfd.events = ctdb_which_events(ctdb_connection);
	  if (poll(&pfd, 1, -1) < 0) {
	    printf("Poll failed");
	    exit(10);
	  }
	  if (ctdb_service(ctdb_connection, pfd.revents) < 0) {
		  err(1, "Failed to service");
	  }
	}

	return 0;
}