ctdb-build: Tests don't include lib/util/*.c, link ctdb-util instead
[Samba.git] / ctdb / tests / src / ctdb_fetch_readonly_loop.c
blobbd171f5a3c08ec214326e55e102cbb813c1a1867
1 /*
2 simple ctdb test tool
3 This test just fetch_locks a record and releases it in a loop.
5 Copyright (C) Ronnie Sahlberg 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "popt.h"
25 #include "cmdline.h"
26 #include "ctdb_private.h"
28 static struct ctdb_db_context *ctdb_db;
30 const char *TESTKEY = "testkey";
31 static int count;
34 Just try locking/unlocking a single record once
36 static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev)
38 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
39 TDB_DATA key, data;
40 struct ctdb_record_handle *h;
41 static time_t t = 0, t2;
43 key.dptr = discard_const(TESTKEY);
44 key.dsize = strlen(TESTKEY);
46 // printf("Trying to fetch lock the record ...\n");
48 h = ctdb_fetch_readonly_lock(ctdb_db, tmp_ctx, key, &data, 1);
49 if (h == NULL) {
50 printf("Failed to fetch record '%s' on node %d\n",
51 (const char *)key.dptr, ctdb_get_pnn(ctdb));
52 talloc_free(tmp_ctx);
53 exit(10);
56 count++;
57 t2 = time(NULL);
58 if (t != 0 && t != t2) {
59 static int last_count = 0;
61 printf("count : %d\n", count - last_count);
62 last_count = count;
64 t = t2;
66 talloc_free(tmp_ctx);
70 main program
72 int main(int argc, const char *argv[])
74 struct ctdb_context *ctdb;
75 TDB_DATA key;
77 struct poptOption popt_options[] = {
78 POPT_AUTOHELP
79 POPT_CTDB_CMDLINE
80 { "record", 'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
81 POPT_TABLEEND
83 int opt, ret;
84 const char **extra_argv;
85 int extra_argc = 0;
86 poptContext pc;
87 struct event_context *ev;
89 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
91 while ((opt = poptGetNextOpt(pc)) != -1) {
92 switch (opt) {
93 default:
94 fprintf(stderr, "Invalid option %s: %s\n",
95 poptBadOption(pc, 0), poptStrerror(opt));
96 exit(1);
100 /* setup the remaining options for the main program to use */
101 extra_argv = poptGetArgs(pc);
102 if (extra_argv) {
103 extra_argv++;
104 while (extra_argv[extra_argc]) extra_argc++;
107 ev = event_context_init(NULL);
109 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(5, 0));
110 if (ctdb == NULL) {
111 exit(1);
114 key.dptr = discard_const(TESTKEY);
115 key.dsize = strlen(TESTKEY);
117 ret = ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
118 if (ret != 0) {
119 printf("failed to get vnnmap\n");
120 exit(10);
122 printf("Record:%s\n", TESTKEY);
123 printf("Lmaster : %d\n", ctdb_lmaster(ctdb, &key));
125 /* attach to a specific database */
126 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(5, 0), "test.tdb", false, 0);
127 if (!ctdb_db) {
128 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
129 exit(1);
132 printf("Waiting for cluster\n");
133 while (1) {
134 uint32_t recmode=1;
135 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
136 if (recmode == 0) break;
137 event_loop_once(ev);
140 while (1) {
141 fetch_lock_once(ctdb, ev);
144 return 0;