try to restart statd everytime it fails, not just the first time
[sahlberg/ctdb.git] / tests / src / ctdb_transaction.c
1 /* 
2    simple tool to test persistent databases
3
4    Copyright (C) Andrew Tridgell  2006-2007
5    Copyright (c) Ronnie sahlberg  2007
6    Copyright (C) Michael Adam     2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "system/filesys.h"
25 #include "popt.h"
26 #include "cmdline.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 static int delay = 0;
47 static int verbose = 0;
48 static int no_trans = 0;
49
50 static unsigned int pnn;
51
52 static TDB_DATA old_data;
53
54 static int success = true;
55
56 static void print_counters(void)
57 {
58         int i;
59         uint32_t *old_counters;
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
69 static void each_second(struct event_context *ev, struct timed_event *te,
70                                          struct timeval t, void *private_data)
71 {
72         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
73
74         print_counters();
75
76         event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
77 }
78
79 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
80 {
81         int i;
82         uint32_t *counters, *old_counters;
83
84         counters     = (uint32_t *)data.dptr;
85         old_counters = (uint32_t *)old_data.dptr;
86
87         /* check that all the counters are monotonic increasing */
88         for (i=0; i<old_data.dsize/sizeof(uint32_t); i++) {
89                 if (counters[i]<old_counters[i]) {
90                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n", 
91                                getpid(), i, old_counters[i], counters[i]);
92                         success = false;
93                 }
94         }
95
96         if (old_data.dsize != data.dsize) {
97                 old_data.dsize = data.dsize;
98                 old_data.dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
99         }
100
101         memcpy(old_data.dptr, data.dptr, data.dsize);
102         if (verbose) print_counters();
103 }
104
105
106 static void do_sleep(unsigned int sec)
107 {
108         unsigned int i;
109         for (i=0; i<sec; i++) {
110                 if (verbose) printf(".");
111                 sleep(1);
112         }
113         if (verbose) printf("\n");
114 }
115
116 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
117 {
118         TDB_DATA key;
119         struct ctdb_db_context *ctdb_db;
120         int ret;
121         uint32_t *counters;
122         ctdb_db = ctdb_db_handle(ctdb, "transaction.tdb");
123
124         key.dptr = discard_const("testkey");
125         key.dsize = strlen((const char *)key.dptr)+1;
126
127         start_timer();
128         while ((timelimit == 0) || (end_timer() < timelimit)) {
129                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
130                 TDB_DATA data;
131                 struct ctdb_transaction_handle *h;
132                 struct ctdb_record_handle *rec;
133
134
135                 if (!no_trans) {
136                         if (verbose) printf("starting transaction\n");
137                         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
138                         if (h == NULL) {
139                                 printf("Failed to start transaction on node %d\n",
140                                        ctdb_get_pnn(ctdb));
141                                 talloc_free(tmp_ctx);
142                                 return;
143                         }
144                         if (verbose) printf("transaction started\n");
145                         do_sleep(delay);
146
147                         if (verbose) printf("calling transaction_fetch\n");
148                         ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
149                         if (ret != 0) {
150                                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
151                                 exit(1);
152                         }
153                         if (verbose) printf("fetched data ok\n");
154                 } else {
155                         if (verbose) printf("calling fetch_lock\n");
156                         rec = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
157                         if (rec == NULL) {
158                                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
159                                 exit(1);
160                         }
161                         if (verbose) printf("fetched record ok\n");
162                 }
163                 do_sleep(delay);
164
165                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
166                         unsigned char *ptr = data.dptr;
167
168                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
169                         memcpy(data.dptr, ptr, data.dsize);
170                         talloc_free(ptr);
171
172                         data.dsize = sizeof(uint32_t) * (pnn+1);
173                 }
174
175                 if (data.dptr == NULL) {
176                         printf("Failed to realloc array\n");
177                         talloc_free(tmp_ctx);
178                         return;
179                 }
180
181                 counters = (uint32_t *)data.dptr;
182
183                 /* bump our counter */
184                 counters[pnn]++;
185
186                 if (!no_trans) {
187                         if (verbose) printf("calling transaction_store\n");
188                         ret = ctdb_transaction_store(h, key, data);
189                         if (ret != 0) {
190                                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
191                                 exit(1);
192                         }
193                         if (verbose) printf("stored data ok\n");
194                         do_sleep(delay);
195
196                         if (verbose) printf("calling transaction_commit\n");
197                         ret = ctdb_transaction_commit(h);
198                         if (ret != 0) {
199                                 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
200                                 exit(1);
201                         }
202                         if (verbose) printf("transaction committed\n");
203                 } else {
204                         if (verbose) printf("calling record_store\n");
205                         ret = ctdb_record_store(rec, data);
206                         if (ret != 0) {
207                                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
208                                 exit(1);
209                         }
210                         if (verbose) printf("stored record ok\n");
211                 }
212
213                 /* store the counters and verify that they are sane */
214                 if (verbose || (pnn == 0)) {
215                         check_counters(ctdb, data);
216                 }
217
218                 do_sleep(delay);
219
220                 talloc_free(tmp_ctx);
221         }
222
223 }
224
225 /*
226   main program
227 */
228 int main(int argc, const char *argv[])
229 {
230         struct ctdb_context *ctdb;
231         struct ctdb_db_context *ctdb_db;
232         int unsafe_writes = 0;
233         struct poptOption popt_options[] = {
234                 POPT_AUTOHELP
235                 POPT_CTDB_CMDLINE
236                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
237                 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "integer" },
238                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
239                 { "no-trans", 'n', POPT_ARG_NONE, &no_trans, 0, "use fetch_lock/record store instead of transactions", NULL },
240                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
241                 POPT_TABLEEND
242         };
243         int opt;
244         const char **extra_argv;
245         int extra_argc = 0;
246         poptContext pc;
247         struct event_context *ev;
248
249         if (verbose) {
250                 setbuf(stdout, (char *)NULL); /* don't buffer */
251         } else {
252                 setlinebuf(stdout);
253         }
254
255         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
256
257         while ((opt = poptGetNextOpt(pc)) != -1) {
258                 switch (opt) {
259                 default:
260                         fprintf(stderr, "Invalid option %s: %s\n", 
261                                 poptBadOption(pc, 0), poptStrerror(opt));
262                         exit(1);
263                 }
264         }
265
266         /* setup the remaining options for the main program to use */
267         extra_argv = poptGetArgs(pc);
268         if (extra_argv) {
269                 extra_argv++;
270                 while (extra_argv[extra_argc]) extra_argc++;
271         }
272
273         ev = event_context_init(NULL);
274
275         ctdb = ctdb_cmdline_client(ev);
276         if (ctdb == NULL) {
277                 printf("Could not attach to daemon\n");
278                 return 1;
279         }
280
281         /* attach to a specific database */
282         if (unsafe_writes == 1) {
283                 ctdb_db = ctdb_attach(ctdb, "transaction.tdb", true, TDB_NOSYNC);
284         } else {
285                 ctdb_db = ctdb_attach(ctdb, "transaction.tdb", true, 0);
286         }
287
288         if (!ctdb_db) {
289                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
290                 exit(1);
291         }
292
293         printf("Waiting for cluster\n");
294         while (1) {
295                 uint32_t recmode=1;
296                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
297                 if (recmode == 0) break;
298                 event_loop_once(ev);
299         }
300
301         pnn = ctdb_get_pnn(ctdb);
302         printf("Starting test on node %u. running for %u seconds. sleep delay: %u seconds.\n", pnn, timelimit, delay);
303
304         if (!verbose && (pnn == 0)) {
305                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
306         }
307
308         test_store_records(ctdb, ev);
309
310         if (verbose || (pnn == 0)) {
311                 if (success != true) {
312                         printf("The test FAILED\n");
313                         return 1;
314                 } else {
315                         printf("SUCCESS!\n");
316                 }
317         }
318         return 0;
319 }