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