remove the old ctdb_attach() function and move all callers over to the new ctdb_attac...
[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 #include "include/ctdb_protocol.h"
28
29 #include <sys/time.h>
30 #include <time.h>
31
32 static struct timeval tp1,tp2;
33
34 static void start_timer(void)
35 {
36         gettimeofday(&tp1,NULL);
37 }
38
39 static double end_timer(void)
40 {
41         gettimeofday(&tp2,NULL);
42         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
43                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
44 }
45
46 static int timelimit = 10;
47 static int delay = 0;
48 static int verbose = 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
133                 if (verbose) DEBUG(DEBUG_ERR, ("starting transaction\n"));
134                 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
135                 if (h == NULL) {
136                         DEBUG(DEBUG_ERR, ("Failed to start transaction on node %d\n",
137                                ctdb_get_pnn(ctdb)));
138                         talloc_free(tmp_ctx);
139                         return;
140                 }
141                 if (verbose) DEBUG(DEBUG_ERR, ("transaction started\n"));
142                 do_sleep(delay);
143
144                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_fetch\n"));
145                 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
146                 if (ret != 0) {
147                         DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
148                         exit(1);
149                 }
150                 if (verbose) DEBUG(DEBUG_ERR, ("fetched data ok\n"));
151                 do_sleep(delay);
152
153                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
154                         unsigned char *ptr = data.dptr;
155
156                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
157                         memcpy(data.dptr, ptr, data.dsize);
158                         talloc_free(ptr);
159
160                         data.dsize = sizeof(uint32_t) * (pnn+1);
161                 }
162
163                 if (data.dptr == NULL) {
164                         DEBUG(DEBUG_ERR, ("Failed to realloc array\n"));
165                         talloc_free(tmp_ctx);
166                         return;
167                 }
168
169                 counters = (uint32_t *)data.dptr;
170
171                 /* bump our counter */
172                 counters[pnn]++;
173
174                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_store\n"));
175                 ret = ctdb_transaction_store(h, key, data);
176                 if (ret != 0) {
177                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
178                         exit(1);
179                 }
180                 if (verbose) DEBUG(DEBUG_ERR, ("stored data ok\n"));
181                 do_sleep(delay);
182
183                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_commit\n"));
184                 ret = ctdb_transaction_commit(h);
185                 if (ret != 0) {
186                         DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
187                         check_counters(ctdb, data);
188                         exit(1);
189                 }
190                 if (verbose) DEBUG(DEBUG_ERR, ("transaction committed\n"));
191
192                 /* store the counters and verify that they are sane */
193                 if (verbose || (pnn == 0)) {
194                         check_counters(ctdb, data);
195                 }
196
197                 do_sleep(delay);
198
199                 talloc_free(tmp_ctx);
200         }
201
202 }
203
204 /*
205   main program
206 */
207 int main(int argc, const char *argv[])
208 {
209         struct ctdb_context *ctdb;
210         struct ctdb_db_context *ctdb_db;
211         int unsafe_writes = 0;
212         struct poptOption popt_options[] = {
213                 POPT_AUTOHELP
214                 POPT_CTDB_CMDLINE
215                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
216                 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "integer" },
217                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
218                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
219                 POPT_TABLEEND
220         };
221         int ret, opt;
222         const char **extra_argv;
223         int extra_argc = 0;
224         poptContext pc;
225         struct event_context *ev;
226
227         printf("SUCCESS (transaction test disabled while transactions are being rewritten)\n");
228         exit(0);
229
230         if (verbose) {
231                 setbuf(stdout, (char *)NULL); /* don't buffer */
232         } else {
233                 setlinebuf(stdout);
234         }
235
236         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
237
238         while ((opt = poptGetNextOpt(pc)) != -1) {
239                 switch (opt) {
240                 default:
241                         fprintf(stderr, "Invalid option %s: %s\n", 
242                                 poptBadOption(pc, 0), poptStrerror(opt));
243                         exit(1);
244                 }
245         }
246
247         /* setup the remaining options for the main program to use */
248         extra_argv = poptGetArgs(pc);
249         if (extra_argv) {
250                 extra_argv++;
251                 while (extra_argv[extra_argc]) extra_argc++;
252         }
253
254         ev = event_context_init(NULL);
255
256         ctdb = ctdb_cmdline_client(ev);
257         if (ctdb == NULL) {
258                 DEBUG(DEBUG_ERR, ("Could not attach to daemon\n"));
259                 return 1;
260         }
261
262         /* attach to a specific database */
263         if (unsafe_writes == 1) {
264           ret = ctdb_attachdb(ctdb, "transaction.tdb", true, TDB_NOSYNC, &ctdb_db);
265         } else {
266           ret = ctdb_attachdb(ctdb, "transaction.tdb", true, 0, &ctdb_db);
267         }
268
269         if (ret) {
270                 DEBUG(DEBUG_ERR, ("ctdb_attach failed - %s\n", ctdb_errstr(ctdb)));
271                 exit(1);
272         }
273
274         DEBUG(DEBUG_ERR, ("Waiting for cluster\n"));
275         while (1) {
276                 uint32_t recmode=1;
277                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
278                 if (recmode == 0) break;
279                 event_loop_once(ev);
280         }
281
282         pnn = ctdb_get_pnn(ctdb);
283         printf("Starting test on node %u. running for %u seconds. sleep delay: %u seconds.\n", pnn, timelimit, delay);
284
285         if (!verbose && (pnn == 0)) {
286                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
287         }
288
289         test_store_records(ctdb, ev);
290
291         if (verbose || (pnn == 0)) {
292                 if (success != true) {
293                         printf("The test FAILED\n");
294                         return 1;
295                 } else {
296                         printf("SUCCESS!\n");
297                 }
298         }
299         return 0;
300 }