2 Samba Linux/Unix CIFS implementation
4 simple tool to test persistent databases
6 Copyright (C) Michael Adam 2009
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.
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.
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/>.
23 #include "system/filesys.h"
24 #include "lib/cmdline/cmdline.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
28 #include "lib/util/util_tdb.h"
29 #include "lib/param/param.h"
32 #include "lib/events/events.h"
33 #include "system/filesys.h"
41 #define DEFAULT_DB_NAME "transaction.tdb"
43 static int timelimit
= 10;
44 static int torture_delay
= 0;
45 static int verbose
= 0;
46 static int no_trans
= 0;
47 static const char *db_name
= DEFAULT_DB_NAME
;
50 static unsigned int pnn
;
52 static TDB_DATA old_data
;
54 static int success
= true;
56 static void print_counters(void)
59 uint32_t *old_counters
;
61 printf("[%4u] Counters: ", (unsigned int)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
]);
69 static void each_second(struct tevent_context
*ev
,
70 struct tevent_timer
*te
,
74 struct db_context
*db
= talloc_get_type(private_data
, struct db_context
);
78 tevent_add_timer(ev
, db
, timeval_current_ofs(1, 0), each_second
, db
);
81 static bool check_counters(struct db_context
*db
, TDB_DATA data
)
84 uint32_t *counters
, *old_counters
;
86 counters
= (uint32_t *)data
.dptr
;
87 old_counters
= (uint32_t *)old_data
.dptr
;
89 /* check that all the counters are monotonic increasing */
90 for (i
=0; i
< old_data
.dsize
/sizeof(uint32_t); i
++) {
91 if (counters
[i
] < old_counters
[i
]) {
92 printf("[%4u] ERROR: counters has decreased for node %u From %u to %u\n",
93 (unsigned int)getpid(), i
, old_counters
[i
], counters
[i
]);
99 if (old_data
.dsize
!= data
.dsize
) {
100 old_data
.dsize
= data
.dsize
;
101 old_data
.dptr
= (unsigned char*)talloc_realloc_size(db
, old_data
.dptr
, old_data
.dsize
);
104 memcpy(old_data
.dptr
, data
.dptr
, data
.dsize
);
105 if (verbose
) print_counters();
111 static void do_sleep(unsigned int sec
)
119 for (i
=0; i
<sec
; i
++) {
120 if (verbose
) printf(".");
124 if (verbose
) printf("\n");
127 static void test_store_records(struct db_context
*db
, struct tevent_context
*ev
)
131 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
132 struct timeval start
;
134 key
= string_term_tdb_data("testkey");
136 start
= timeval_current();
137 while ((timelimit
== 0) || (timeval_elapsed(&start
) < timelimit
)) {
138 struct db_record
*rec
;
145 if (verbose
) DEBUG(1, ("starting transaction\n"));
146 ret
= dbwrap_transaction_start(db
);
148 DEBUG(0, ("Failed to start transaction on node "
152 if (verbose
) DEBUG(1, ("transaction started\n"));
153 do_sleep(torture_delay
);
156 if (verbose
) DEBUG(1, ("calling fetch_lock\n"));
157 rec
= dbwrap_fetch_locked(db
, tmp_ctx
, key
);
159 DEBUG(0, ("Failed to fetch record\n"));
162 if (verbose
) DEBUG(1, ("fetched record ok\n"));
163 do_sleep(torture_delay
);
164 value
= dbwrap_record_get_value(rec
);
166 data
.dsize
= MAX(value
.dsize
, sizeof(uint32_t) * (pnn
+1));
167 data
.dptr
= (unsigned char *)talloc_zero_size(tmp_ctx
,
169 if (data
.dptr
== NULL
) {
170 DEBUG(0, ("Failed to allocate data\n"));
173 memcpy(data
.dptr
, value
.dptr
, value
.dsize
);
175 counters
= (uint32_t *)data
.dptr
;
177 /* bump our counter */
180 if (verbose
) DEBUG(1, ("storing data\n"));
181 status
= dbwrap_record_store(rec
, data
, TDB_REPLACE
);
182 if (!NT_STATUS_IS_OK(status
)) {
183 DEBUG(0, ("Failed to store record\n"));
185 ret
= dbwrap_transaction_cancel(db
);
187 DEBUG(0, ("Error cancelling transaction.\n"));
193 if (verbose
) DEBUG(1, ("stored data ok\n"));
194 do_sleep(torture_delay
);
197 if (verbose
) DEBUG(1, ("calling transaction_commit\n"));
198 ret
= dbwrap_transaction_commit(db
);
200 DEBUG(0, ("Failed to commit transaction\n"));
203 if (verbose
) DEBUG(1, ("transaction committed\n"));
206 /* store the counters and verify that they are sane */
207 if (verbose
|| (pnn
== 0)) {
208 if (!check_counters(db
, data
)) {
212 talloc_free(data
.dptr
);
214 do_sleep(torture_delay
);
223 talloc_free(tmp_ctx
);
230 int main(int argc
, const char *argv
[])
233 struct tevent_context
*ev_ctx
;
234 struct messaging_context
*msg_ctx
;
235 struct db_context
*db
;
237 int unsafe_writes
= 0;
238 struct poptOption popt_options
[] = {
241 { "timelimit", 't', POPT_ARG_INT
, &timelimit
, 0, "timelimit", "INTEGER" },
242 { "delay", 'D', POPT_ARG_INT
, &torture_delay
, 0, "delay (in seconds) between operations", "INTEGER" },
243 { "verbose", 'v', POPT_ARG_NONE
, &verbose
, 0, "switch on verbose mode", NULL
},
244 { "db-name", 'N', POPT_ARG_STRING
, &db_name
, 0, "name of the test db", "NAME" },
245 { "no-trans", 'n', POPT_ARG_NONE
, &no_trans
, 0, "use fetch_lock/record store instead of transactions", NULL
},
246 { "unsafe-writes", 'u', POPT_ARG_NONE
, &unsafe_writes
, 0, "do not use tdb transactions when writing", NULL
},
251 const char **extra_argv
;
257 struct loadparm_context
*lp_ctx
= NULL
;
259 mem_ctx
= talloc_stackframe();
262 setbuf(stdout
, (char *)NULL
); /* don't buffer */
269 ok
= samba_cmdline_init(mem_ctx
,
270 SAMBA_CMDLINE_CONFIG_CLIENT
,
271 false /* require_smbconf */);
273 DBG_ERR("Failed to init cmdline parser!\n");
274 TALLOC_FREE(mem_ctx
);
277 lp_ctx
= samba_cmdline_get_lp_ctx();
278 lpcfg_set_cmdline(lp_ctx
, "log level", "0");
280 pc
= samba_popt_get_context(getprogname(),
284 POPT_CONTEXT_KEEP_FIRST
);
286 DBG_ERR("Failed to setup popt context!\n");
287 TALLOC_FREE(mem_ctx
);
291 while ((opt
= poptGetNextOpt(pc
)) != -1) {
294 fprintf(stderr
, "Invalid option %s: %s\n",
295 poptBadOption(pc
, 0), poptStrerror(opt
));
300 /* setup the remaining options for the main program to use */
301 extra_argv
= poptGetArgs(pc
);
304 while (extra_argv
[extra_argc
]) extra_argc
++;
307 ev_ctx
= samba_tevent_context_init(mem_ctx
);
308 if (ev_ctx
== NULL
) {
309 d_fprintf(stderr
, "ERROR: could not init event context\n");
313 msg_ctx
= messaging_init(mem_ctx
, ev_ctx
);
314 if (msg_ctx
== NULL
) {
315 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
319 if (unsafe_writes
== 1) {
320 tdb_flags
= TDB_NOSYNC
;
322 tdb_flags
= TDB_DEFAULT
;
326 tdb_flags
|= TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
329 db
= db_open(mem_ctx
, db_name
, 0, tdb_flags
, O_RDWR
| O_CREAT
, 0644,
330 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
333 d_fprintf(stderr
, "failed to open db '%s': %s\n", db_name
,
338 if (get_my_vnn() == NONCLUSTER_VNN
) {
343 printf("Starting test on node %u. running for %u seconds. "
344 "sleep delay: %u seconds.\n", pnn
, timelimit
, torture_delay
);
346 if (!verbose
&& (pnn
== 0)) {
347 tevent_add_timer(ev_ctx
, db
, timeval_current_ofs(1, 0), each_second
, db
);
350 test_store_records(db
, ev_ctx
);
352 if (verbose
|| (pnn
== 0)) {
353 if (success
!= true) {
354 printf("The test FAILED\n");
357 printf("SUCCESS!\n");
364 talloc_free(mem_ctx
);