775ba0c7ad24be2159164890cac9281d2381d6e5
[tridge/ctdb.git] / tests / ctdb_persistent.c
1 /* 
2    simple tool to test persistent databases
3
4    Copyright (C) Andrew Tridgell  2006-2007
5    Copyright (c) Ronnie sahlberg  2007
6
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29
30 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
31 {
32         TDB_DATA key, data;
33         struct ctdb_db_context *ctdb_db;
34         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
35         int ret, i;
36         struct ctdb_record_handle *h;
37         unsigned node=0, count=0;
38         
39         ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
40
41         key.dptr = discard_const("testkey");
42         key.dsize = strlen((const char *)key.dptr)+1;
43
44         for (i=0;i<10;i++) {
45                 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
46                 if (h == NULL) {
47                         printf("Failed to fetch record '%s' on node %d\n", 
48                                (const char *)key.dptr, ctdb_get_pnn(ctdb));
49                         talloc_free(tmp_ctx);
50                         return;
51                 }
52                 
53                 printf("Current value: %*.*s\n", (int)data.dsize, (int)data.dsize, data.dptr);
54                 
55                 if (data.dsize != 0) {
56                         if (sscanf((char *)data.dptr, "Node %u Count %u", &node, &count) != 2) {
57                                 printf("Badly formatted node data!\n");
58                                 exit(1);
59                         }
60                 }
61                 
62                 node = ctdb_get_pnn(ctdb);
63                 count++;
64                 
65                 data.dptr = (uint8_t *)talloc_asprintf(h, "Node %u Count %u", node, count);
66                 data.dsize = strlen((char *)data.dptr)+1;
67                 
68                 ret = ctdb_record_store(h, data);
69                 if (ret != 0) {
70                         DEBUG(0,("Failed to store record\n"));
71                         exit(1);
72                 }
73                 talloc_free(h);
74         }
75
76         talloc_free(tmp_ctx);
77 }
78
79 /*
80   main program
81 */
82 int main(int argc, const char *argv[])
83 {
84         struct ctdb_context *ctdb;
85         struct ctdb_db_context *ctdb_db;
86
87         struct poptOption popt_options[] = {
88                 POPT_AUTOHELP
89                 POPT_CTDB_CMDLINE
90                 POPT_TABLEEND
91         };
92         int opt;
93         const char **extra_argv;
94         int extra_argc = 0;
95         poptContext pc;
96         struct event_context *ev;
97
98         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
99
100         while ((opt = poptGetNextOpt(pc)) != -1) {
101                 switch (opt) {
102                 default:
103                         fprintf(stderr, "Invalid option %s: %s\n", 
104                                 poptBadOption(pc, 0), poptStrerror(opt));
105                         exit(1);
106                 }
107         }
108
109         /* setup the remaining options for the main program to use */
110         extra_argv = poptGetArgs(pc);
111         if (extra_argv) {
112                 extra_argv++;
113                 while (extra_argv[extra_argc]) extra_argc++;
114         }
115
116         ev = event_context_init(NULL);
117
118         ctdb = ctdb_cmdline_client(ev);
119
120         /* attach to a specific database */
121         ctdb_db = ctdb_attach(ctdb, "persistent.tdb", true);
122         if (!ctdb_db) {
123                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
124                 exit(1);
125         }
126
127         printf("Waiting for cluster\n");
128         while (1) {
129                 uint32_t recmode=1;
130                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
131                 if (recmode == 0) break;
132                 event_loop_once(ev);
133         }
134
135         printf("Starting test\n");
136         test_store_records(ctdb, ev);
137
138         return 0;
139 }