s3: use TDB_INCOMPATIBLE_HASH (the jenkins hash) on all TDB_CLEAR_IF_FIRST tdb's.
[obnox/samba-ctdb.git] / source3 / utils / dbwrap_torture.c
1 /*
2    Samba Linux/Unix CIFS implementation
3
4    simple tool to test persistent databases
5
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
24 #if 0
25 #include "lib/events/events.h"
26 #include "system/filesys.h"
27 #include "popt.h"
28 #include "cmdline.h"
29
30 #include <sys/time.h>
31 #include <time.h>
32 #endif
33
34 extern bool AllowDebugChange;
35
36 #define DEFAULT_DB_NAME "transaction.tdb"
37
38 static int timelimit = 10;
39 static int delay = 0;
40 static int verbose = 0;
41 static int no_trans = 0;
42 static char *db_name = (char *)discard_const(DEFAULT_DB_NAME);
43
44
45 static unsigned int pnn;
46
47 static TDB_DATA old_data;
48
49 static int success = true;
50
51 static void print_counters(void)
52 {
53         int i;
54         uint32_t *old_counters;
55
56         printf("[%4u] Counters: ", getpid());
57         old_counters = (uint32_t *)old_data.dptr;
58         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
59                 printf("%6u ", old_counters[i]);
60         }
61         printf("\n");
62 }
63
64 static void each_second(struct tevent_context *ev,
65                         struct tevent_timer *te,
66                         struct timeval t,
67                         void *private_data)
68 {
69         struct db_context *db = talloc_get_type(private_data, struct db_context);
70
71         print_counters();
72
73         tevent_add_timer(ev, db, timeval_current_ofs(1, 0), each_second, db);
74 }
75
76 static bool check_counters(struct db_context *db, TDB_DATA data)
77 {
78         int i;
79         uint32_t *counters, *old_counters;
80
81         counters     = (uint32_t *)data.dptr;
82         old_counters = (uint32_t *)old_data.dptr;
83
84         /* check that all the counters are monotonic increasing */
85         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
86                 if (counters[i] < old_counters[i]) {
87                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n",
88                                getpid(), i, old_counters[i], counters[i]);
89                         success = false;
90                         return false;
91                 }
92         }
93
94         if (old_data.dsize != data.dsize) {
95                 old_data.dsize = data.dsize;
96                 old_data.dptr = (unsigned char*)talloc_realloc_size(db, old_data.dptr, old_data.dsize);
97         }
98
99         memcpy(old_data.dptr, data.dptr, data.dsize);
100         if (verbose) print_counters();
101
102         return true;
103 }
104
105
106 static void do_sleep(unsigned int sec)
107 {
108         unsigned int i;
109
110         if (sec == 0) {
111                 return;
112         }
113
114         for (i=0; i<sec; i++) {
115                 if (verbose) printf(".");
116                 sleep(1);
117         }
118
119         if (verbose) printf("\n");
120 }
121
122 static void test_store_records(struct db_context *db, struct tevent_context *ev)
123 {
124         TDB_DATA key;
125         uint32_t *counters;
126         TALLOC_CTX *tmp_ctx = talloc_stackframe();
127         struct timeval start;
128
129         key.dptr = (unsigned char *)discard_const("testkey");
130         key.dsize = strlen((const char *)key.dptr)+1;
131
132         start = timeval_current();
133         while ((timelimit == 0) || (timeval_elapsed(&start) < timelimit)) {
134                 struct db_record *rec;
135                 TDB_DATA data;
136                 int ret;
137                 NTSTATUS status;
138
139                 if (!no_trans) {
140                         if (verbose) DEBUG(1, ("starting transaction\n"));
141                         ret = db->transaction_start(db);
142                         if (ret != 0) {
143                                 DEBUG(0, ("Failed to start transaction on node "
144                                           "%d\n", pnn));
145                                 goto fail;
146                         }
147                         if (verbose) DEBUG(1, ("transaction started\n"));
148                         do_sleep(delay);
149                 }
150
151                 if (verbose) DEBUG(1, ("calling fetch_lock\n"));
152                 rec = db->fetch_locked(db, tmp_ctx, key);
153                 if (rec == NULL) {
154                         DEBUG(0, ("Failed to fetch record\n"));
155                         goto fail;
156                 }
157                 if (verbose) DEBUG(1, ("fetched record ok\n"));
158                 do_sleep(delay);
159
160                 data.dsize = MAX(rec->value.dsize, sizeof(uint32_t) * (pnn+1));
161                 data.dptr = (unsigned char *)talloc_zero_size(tmp_ctx,
162                                                               data.dsize);
163                 if (data.dptr == NULL) {
164                         DEBUG(0, ("Failed to allocate data\n"));
165                         goto fail;
166                 }
167                 memcpy(data.dptr, rec->value.dptr,rec->value.dsize);
168
169                 counters = (uint32_t *)data.dptr;
170
171                 /* bump our counter */
172                 counters[pnn]++;
173
174                 if (verbose) DEBUG(1, ("storing data\n"));
175                 status = rec->store(rec, data, TDB_REPLACE);
176                 if (!NT_STATUS_IS_OK(status)) {
177                         DEBUG(0, ("Failed to store record\n"));
178                         if (!no_trans) {
179                                 ret = db->transaction_cancel(db);
180                                 if (ret != 0) {
181                                         DEBUG(0, ("Error cancelling transaction.\n"));
182                                 }
183                         }
184                         goto fail;
185                 }
186                 talloc_free(rec);
187                 if (verbose) DEBUG(1, ("stored data ok\n"));
188                 do_sleep(delay);
189
190                 if (!no_trans) {
191                         if (verbose) DEBUG(1, ("calling transaction_commit\n"));
192                         ret = db->transaction_commit(db);
193                         if (ret != 0) {
194                                 DEBUG(0, ("Failed to commit transaction\n"));
195                                 goto fail;
196                         }
197                         if (verbose) DEBUG(1, ("transaction committed\n"));
198                 }
199
200                 /* store the counters and verify that they are sane */
201                 if (verbose || (pnn == 0)) {
202                         if (!check_counters(db, data)) {
203                                 goto fail;
204                         }
205                 }
206                 talloc_free(data.dptr);
207
208                 do_sleep(delay);
209         }
210
211         goto done;
212
213 fail:
214         success = false;
215
216 done:
217         talloc_free(tmp_ctx);
218         return;
219 }
220
221 /*
222   main program
223 */
224 int main(int argc, const char *argv[])
225 {
226         TALLOC_CTX *mem_ctx;
227         struct tevent_context *ev_ctx;
228         struct messaging_context *msg_ctx;
229         struct db_context *db;
230
231         int unsafe_writes = 0;
232         struct poptOption popt_options[] = {
233                 POPT_AUTOHELP
234                 POPT_COMMON_SAMBA
235                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "INTEGER" },
236                 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "INTEGER" },
237                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
238                 { "db-name", 'N', POPT_ARG_STRING, &db_name, 0, "name of the test db", "NAME" },
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         int tdb_flags;
248
249         int ret = 1;
250
251         mem_ctx = talloc_stackframe();
252
253         if (verbose) {
254                 setbuf(stdout, (char *)NULL); /* don't buffer */
255         } else {
256                 setlinebuf(stdout);
257         }
258
259         DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
260
261         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
262
263         while ((opt = poptGetNextOpt(pc)) != -1) {
264                 switch (opt) {
265                 default:
266                         fprintf(stderr, "Invalid option %s: %s\n",
267                                 poptBadOption(pc, 0), poptStrerror(opt));
268                         goto done;
269                 }
270         }
271
272         /* setup the remaining options for the main program to use */
273         extra_argv = poptGetArgs(pc);
274         if (extra_argv) {
275                 extra_argv++;
276                 while (extra_argv[extra_argc]) extra_argc++;
277         }
278
279         load_case_tables();
280         dbf = x_stderr;
281         AllowDebugChange = false;
282         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
283
284         ev_ctx = tevent_context_init(mem_ctx);
285         if (ev_ctx == NULL) {
286                 d_fprintf(stderr, "ERROR: could not init event context\n");
287                 goto done;
288         }
289
290         msg_ctx = messaging_init(mem_ctx, server_id_self(), ev_ctx);
291         if (msg_ctx == NULL) {
292                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
293                 goto done;
294         }
295
296         if (unsafe_writes == 1) {
297                 tdb_flags = TDB_NOSYNC;
298         } else {
299                 tdb_flags = TDB_DEFAULT;
300         }
301
302         if (no_trans) {
303                 tdb_flags |= TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
304         }
305
306         db = db_open(mem_ctx, db_name, 0, tdb_flags,  O_RDWR | O_CREAT, 0644);
307
308         if (db == NULL) {
309                 d_fprintf(stderr, "failed to open db '%s': %s\n", db_name,
310                           strerror(errno));
311                 goto done;
312         }
313
314         if (get_my_vnn() == NONCLUSTER_VNN) {
315                 set_my_vnn(0);
316         }
317         pnn = get_my_vnn();
318
319         printf("Starting test on node %u. running for %u seconds. "
320                "sleep delay: %u seconds.\n", pnn, timelimit, delay);
321
322         if (!verbose && (pnn == 0)) {
323                 tevent_add_timer(ev_ctx, db, timeval_current_ofs(1, 0), each_second, db);
324         }
325
326         test_store_records(db, ev_ctx);
327
328         if (verbose || (pnn == 0)) {
329                 if (success != true) {
330                         printf("The test FAILED\n");
331                         ret = 2;
332                 } else {
333                         printf("SUCCESS!\n");
334                         ret = 0;
335                 }
336         }
337
338 done:
339         talloc_free(mem_ctx);
340         return ret;
341 }