remove the old ctdb_attach() function and move all callers over to the new ctdb_attac...
[sahlberg/ctdb.git] / tests / src / 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 #include "include/ctdb_protocol.h"
27
28 #include <sys/time.h>
29 #include <time.h>
30
31 static struct timeval tp1,tp2;
32
33 static void start_timer(void)
34 {
35         gettimeofday(&tp1,NULL);
36 }
37
38 static double end_timer(void)
39 {
40         gettimeofday(&tp2,NULL);
41         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
42                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
43 }
44
45 static int timelimit = 10;
46
47 static unsigned int pnn;
48
49 static TDB_DATA old_data;
50
51 static int success = true;
52
53 static void each_second(struct event_context *ev, struct timed_event *te, 
54                                          struct timeval t, void *private_data)
55 {
56         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
57         int i;
58         uint32_t *old_counters;
59
60
61         printf("[%4u] Counters: ", getpid());
62         old_counters = (uint32_t *)old_data.dptr;
63         for (i=0;i<old_data.dsize/sizeof(uint32_t); i++) {
64                 printf("%6u ", old_counters[i]);
65         }
66         printf("\n"); 
67
68         event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
69 }
70
71 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
72 {
73         int i;
74         uint32_t *counters, *old_counters;
75         unsigned char *tmp_dptr;
76
77         counters     = (uint32_t *)data.dptr;
78         old_counters = (uint32_t *)old_data.dptr;
79
80         /* check that all the counters are monotonic increasing */
81         for (i=0; i<old_data.dsize/sizeof(uint32_t); i++) {
82                 if (counters[i]<old_counters[i]) {
83                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n", 
84                                getpid(), i, old_counters[i], counters[i]);
85                         success = false;
86                 }
87         }
88
89         if (old_data.dsize != data.dsize) {
90                 old_data.dsize = data.dsize;
91                 tmp_dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
92                 if (tmp_dptr == NULL) {
93                         printf("[%4u] ERROR: talloc_realloc_size failed.\n", getpid());
94                         success = false;
95                         return;
96                 } else {
97                         old_data.dptr = tmp_dptr;
98                 }
99         }
100
101         memcpy(old_data.dptr, data.dptr, data.dsize);
102 }
103
104
105
106 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
107 {
108         TDB_DATA key;
109         struct ctdb_db_context *ctdb_db;
110
111         ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
112
113         key.dptr = discard_const("testkey");
114         key.dsize = strlen((const char *)key.dptr)+1;
115
116         start_timer();
117         while (end_timer() < timelimit) {
118                 TDB_DATA data;
119                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
120                 struct ctdb_transaction_handle *h;
121                 int ret;
122                 uint32_t *counters;
123
124                 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
125                 if (h == NULL) {
126                         printf("Failed to start transaction on node %d\n",
127                                ctdb_get_pnn(ctdb));
128                         talloc_free(tmp_ctx);
129                         return;
130                 }
131
132                 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
133                 if (ret != 0) {
134                         DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
135                         exit(1);
136                 }
137
138                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
139                         unsigned char *ptr = data.dptr;
140                         
141                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
142                         memcpy(data.dptr, ptr, data.dsize);
143                         talloc_free(ptr);
144
145                         data.dsize = sizeof(uint32_t) * (pnn+1);
146                 }
147
148                 if (data.dptr == NULL) {
149                         printf("Failed to realloc array\n");
150                         talloc_free(tmp_ctx);
151                         return;
152                 }
153
154                 counters = (uint32_t *)data.dptr;
155
156                 /* bump our counter */
157                 counters[pnn]++;
158
159                 ret = ctdb_transaction_store(h, key, data);
160                 if (ret != 0) {
161                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
162                         exit(1);
163                 }
164
165                 ret = ctdb_transaction_commit(h);
166                 if (ret != 0) {
167                         DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
168                         //exit(1);
169                 }
170
171                 /* store the counters and verify that they are sane */
172                 if (pnn == 0) {
173                         check_counters(ctdb, data);
174                 }
175
176                 talloc_free(tmp_ctx);
177         }
178
179 }
180
181 /*
182   main program
183 */
184 int main(int argc, const char *argv[])
185 {
186         struct ctdb_context *ctdb;
187         struct ctdb_db_context *ctdb_db;
188         int unsafe_writes = 0;
189         struct poptOption popt_options[] = {
190                 POPT_AUTOHELP
191                 POPT_CTDB_CMDLINE
192                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
193                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
194                 POPT_TABLEEND
195         };
196         int ret, opt;
197         const char **extra_argv;
198         int extra_argc = 0;
199         poptContext pc;
200         struct event_context *ev;
201
202         setlinebuf(stdout);
203
204         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
205
206         while ((opt = poptGetNextOpt(pc)) != -1) {
207                 switch (opt) {
208                 default:
209                         fprintf(stderr, "Invalid option %s: %s\n", 
210                                 poptBadOption(pc, 0), poptStrerror(opt));
211                         exit(1);
212                 }
213         }
214
215         /* setup the remaining options for the main program to use */
216         extra_argv = poptGetArgs(pc);
217         if (extra_argv) {
218                 extra_argv++;
219                 while (extra_argv[extra_argc]) extra_argc++;
220         }
221
222         ev = event_context_init(NULL);
223
224         ctdb = ctdb_cmdline_client(ev);
225         if (ctdb == NULL) {
226                 printf("Could not attach to daemon\n");
227                 return 1;
228         }
229
230         /* attach to a specific database */
231         if (unsafe_writes == 1) {
232           ret = ctdb_attachdb(ctdb, "persistent.tdb", true, TDB_NOSYNC, &ctdb_db);
233         } else {
234           ret = ctdb_attachdb(ctdb, "persistent.tdb", true, 0, &ctdb_db);
235         }
236
237         if (ret) {
238                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
239                 exit(1);
240         }
241
242         printf("Waiting for cluster\n");
243         while (1) {
244                 uint32_t recmode=1;
245                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
246                 if (recmode == 0) break;
247                 event_loop_once(ev);
248         }
249
250         pnn = ctdb_get_pnn(ctdb);
251         printf("Starting test on node %u. running for %u seconds\n", pnn, timelimit);
252
253         if (pnn == 0) {
254                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
255         }
256
257         test_store_records(ctdb, ev);
258
259         if (pnn == 0) {
260                 if (success != true) {
261                         printf("The test FAILED\n");
262                         return 1;
263                 } else {
264                         printf("SUCCESS!\n");
265                 }
266         }
267         return 0;
268 }