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