b70621f2f673ad28557f1af27f490c0fcec65680
[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
49 static unsigned int pnn;
50
51 static TDB_DATA old_data;
52
53 static int success = true;
54
55 static void print_counters(void)
56 {
57         int i;
58         uint32_t *old_counters;
59
60         printf("[%4u] Counters: ", getpid());
61         old_counters = (uint32_t *)old_data.dptr;
62         for (i=0;i<old_data.dsize/sizeof(uint32_t); i++) {
63                 printf("%6u ", old_counters[i]);
64         }
65         printf("\n");
66 }
67
68 static void each_second(struct event_context *ev, struct timed_event *te,
69                                          struct timeval t, void *private_data)
70 {
71         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
72
73         print_counters();
74
75         event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
76 }
77
78 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
79 {
80         int i;
81         uint32_t *counters, *old_counters;
82
83         counters     = (uint32_t *)data.dptr;
84         old_counters = (uint32_t *)old_data.dptr;
85
86         /* check that all the counters are monotonic increasing */
87         for (i=0; i<old_data.dsize/sizeof(uint32_t); i++) {
88                 if (counters[i]<old_counters[i]) {
89                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n", 
90                                getpid(), i, old_counters[i], counters[i]);
91                         success = false;
92                 }
93         }
94
95         if (old_data.dsize != data.dsize) {
96                 old_data.dsize = data.dsize;
97                 old_data.dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
98         }
99
100         memcpy(old_data.dptr, data.dptr, data.dsize);
101         if (verbose) print_counters();
102 }
103
104
105 static void do_sleep(unsigned int sec)
106 {
107         unsigned int i;
108         for (i=0; i<sec; i++) {
109                 if (verbose) printf(".");
110                 sleep(1);
111         }
112         if (verbose) printf("\n");
113 }
114
115 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
116 {
117         TDB_DATA key;
118         struct ctdb_db_context *ctdb_db;
119         int ret;
120         uint32_t *counters;
121         ctdb_db = ctdb_db_handle(ctdb, "transaction.tdb");
122
123         key.dptr = discard_const("testkey");
124         key.dsize = strlen((const char *)key.dptr)+1;
125
126         start_timer();
127         while ((timelimit == 0) || (end_timer() < timelimit)) {
128                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
129                 TDB_DATA data;
130                 struct ctdb_transaction_handle *h;
131
132                 if (verbose) DEBUG(DEBUG_ERR, ("starting transaction\n"));
133                 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
134                 if (h == NULL) {
135                         DEBUG(DEBUG_ERR, ("Failed to start transaction on node %d\n",
136                                ctdb_get_pnn(ctdb)));
137                         talloc_free(tmp_ctx);
138                         return;
139                 }
140                 if (verbose) DEBUG(DEBUG_ERR, ("transaction started\n"));
141                 do_sleep(delay);
142
143                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_fetch\n"));
144                 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
145                 if (ret != 0) {
146                         DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
147                         exit(1);
148                 }
149                 if (verbose) DEBUG(DEBUG_ERR, ("fetched data ok\n"));
150                 do_sleep(delay);
151
152                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
153                         unsigned char *ptr = data.dptr;
154
155                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
156                         memcpy(data.dptr, ptr, data.dsize);
157                         talloc_free(ptr);
158
159                         data.dsize = sizeof(uint32_t) * (pnn+1);
160                 }
161
162                 if (data.dptr == NULL) {
163                         DEBUG(DEBUG_ERR, ("Failed to realloc array\n"));
164                         talloc_free(tmp_ctx);
165                         return;
166                 }
167
168                 counters = (uint32_t *)data.dptr;
169
170                 /* bump our counter */
171                 counters[pnn]++;
172
173                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_store\n"));
174                 ret = ctdb_transaction_store(h, key, data);
175                 if (ret != 0) {
176                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
177                         exit(1);
178                 }
179                 if (verbose) DEBUG(DEBUG_ERR, ("stored data ok\n"));
180                 do_sleep(delay);
181
182                 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_commit\n"));
183                 ret = ctdb_transaction_commit(h);
184                 if (ret != 0) {
185                         DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
186                         check_counters(ctdb, data);
187                         exit(1);
188                 }
189                 if (verbose) DEBUG(DEBUG_ERR, ("transaction committed\n"));
190
191                 /* store the counters and verify that they are sane */
192                 if (verbose || (pnn == 0)) {
193                         check_counters(ctdb, data);
194                 }
195
196                 do_sleep(delay);
197
198                 talloc_free(tmp_ctx);
199         }
200
201 }
202
203 /*
204   main program
205 */
206 int main(int argc, const char *argv[])
207 {
208         struct ctdb_context *ctdb;
209         struct ctdb_db_context *ctdb_db;
210         int unsafe_writes = 0;
211         struct poptOption popt_options[] = {
212                 POPT_AUTOHELP
213                 POPT_CTDB_CMDLINE
214                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
215                 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "integer" },
216                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
217                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
218                 POPT_TABLEEND
219         };
220         int opt;
221         const char **extra_argv;
222         int extra_argc = 0;
223         poptContext pc;
224         struct event_context *ev;
225
226         printf("SUCCESS (transaction test disabled while transactions are being rewritten)\n");
227         exit(0);
228
229         if (verbose) {
230                 setbuf(stdout, (char *)NULL); /* don't buffer */
231         } else {
232                 setlinebuf(stdout);
233         }
234
235         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
236
237         while ((opt = poptGetNextOpt(pc)) != -1) {
238                 switch (opt) {
239                 default:
240                         fprintf(stderr, "Invalid option %s: %s\n", 
241                                 poptBadOption(pc, 0), poptStrerror(opt));
242                         exit(1);
243                 }
244         }
245
246         /* setup the remaining options for the main program to use */
247         extra_argv = poptGetArgs(pc);
248         if (extra_argv) {
249                 extra_argv++;
250                 while (extra_argv[extra_argc]) extra_argc++;
251         }
252
253         ev = event_context_init(NULL);
254
255         ctdb = ctdb_cmdline_client(ev);
256         if (ctdb == NULL) {
257                 DEBUG(DEBUG_ERR, ("Could not attach to daemon\n"));
258                 return 1;
259         }
260
261         /* attach to a specific database */
262         if (unsafe_writes == 1) {
263                 ctdb_db = ctdb_attach(ctdb, "transaction.tdb", true, TDB_NOSYNC);
264         } else {
265                 ctdb_db = ctdb_attach(ctdb, "transaction.tdb", true, 0);
266         }
267
268         if (!ctdb_db) {
269                 DEBUG(DEBUG_ERR, ("ctdb_attach failed - %s\n", ctdb_errstr(ctdb)));
270                 exit(1);
271         }
272
273         DEBUG(DEBUG_ERR, ("Waiting for cluster\n"));
274         while (1) {
275                 uint32_t recmode=1;
276                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
277                 if (recmode == 0) break;
278                 event_loop_once(ev);
279         }
280
281         pnn = ctdb_get_pnn(ctdb);
282         printf("Starting test on node %u. running for %u seconds. sleep delay: %u seconds.\n", pnn, timelimit, delay);
283
284         if (!verbose && (pnn == 0)) {
285                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
286         }
287
288         test_store_records(ctdb, ev);
289
290         if (verbose || (pnn == 0)) {
291                 if (success != true) {
292                         printf("The test FAILED\n");
293                         return 1;
294                 } else {
295                         printf("SUCCESS!\n");
296                 }
297         }
298         return 0;
299 }