s3:lib/memcache: make use of talloc for memcache_elements
[Samba.git] / ctdb / tests / src / ctdb_transaction.c
blob78a63f1f126bcb160b9fbba6e033a85960cd2081
1 /*
2 simple tool to test persistent databases
4 Copyright (C) Andrew Tridgell 2006-2007
5 Copyright (c) Ronnie sahlberg 2007
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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
27 #include <sys/time.h>
28 #include <time.h>
30 static struct timeval tp1,tp2;
32 static void start_timer(void)
34 gettimeofday(&tp1,NULL);
37 static double end_timer(void)
39 gettimeofday(&tp2,NULL);
40 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
41 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
44 static int timelimit = 10;
45 static int delay = 0;
46 static int verbose = 0;
48 static unsigned int pnn;
50 static TDB_DATA old_data;
52 static bool success = false;
54 static void print_counters(void)
56 int i;
57 uint32_t *old_counters;
59 printf("[%4u] Counters: ", getpid());
60 old_counters = (uint32_t *)old_data.dptr;
61 for (i=0;i<old_data.dsize/sizeof(uint32_t); i++) {
62 printf("%6u ", old_counters[i]);
64 printf("\n");
67 static void each_second(struct event_context *ev, struct timed_event *te,
68 struct timeval t, void *private_data)
70 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
72 print_counters();
74 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
77 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
79 int i;
80 uint32_t *counters, *old_counters;
81 bool monotonous = true;
83 counters = (uint32_t *)data.dptr;
84 old_counters = (uint32_t *)old_data.dptr;
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 monotonous = false;
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);
100 memcpy(old_data.dptr, data.dptr, data.dsize);
101 if (verbose) print_counters();
103 success = monotonous;
107 static void do_sleep(unsigned int sec)
109 unsigned int i;
110 for (i=0; i<sec; i++) {
111 if (verbose) printf(".");
112 sleep(1);
114 if (verbose) printf("\n");
117 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
119 TDB_DATA key;
120 struct ctdb_db_context *ctdb_db;
121 int ret;
122 uint32_t *counters;
123 ctdb_db = ctdb_db_handle(ctdb, "transaction.tdb");
125 key.dptr = discard_const("testkey");
126 key.dsize = strlen((const char *)key.dptr)+1;
128 start_timer();
129 while ((timelimit == 0) || (end_timer() < timelimit)) {
130 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
131 TDB_DATA data;
132 struct ctdb_transaction_handle *h;
134 if (verbose) DEBUG(DEBUG_ERR, ("starting transaction\n"));
135 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
136 if (h == NULL) {
137 DEBUG(DEBUG_ERR, ("Failed to start transaction on node %d\n",
138 ctdb_get_pnn(ctdb)));
139 talloc_free(tmp_ctx);
140 return;
142 if (verbose) DEBUG(DEBUG_ERR, ("transaction started\n"));
143 do_sleep(delay);
145 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_fetch\n"));
146 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
147 if (ret != 0) {
148 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
149 exit(1);
151 if (verbose) DEBUG(DEBUG_ERR, ("fetched data ok\n"));
152 do_sleep(delay);
154 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
155 unsigned char *ptr = data.dptr;
157 data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
158 memcpy(data.dptr, ptr, data.dsize);
159 talloc_free(ptr);
161 data.dsize = sizeof(uint32_t) * (pnn+1);
164 if (data.dptr == NULL) {
165 DEBUG(DEBUG_ERR, ("Failed to realloc array\n"));
166 talloc_free(tmp_ctx);
167 return;
170 counters = (uint32_t *)data.dptr;
172 /* bump our counter */
173 counters[pnn]++;
175 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_store\n"));
176 ret = ctdb_transaction_store(h, key, data);
177 if (ret != 0) {
178 DEBUG(DEBUG_ERR,("Failed to store record\n"));
179 exit(1);
181 if (verbose) DEBUG(DEBUG_ERR, ("stored data ok\n"));
182 do_sleep(delay);
184 if (verbose) DEBUG(DEBUG_ERR, ("calling transaction_commit\n"));
185 ret = ctdb_transaction_commit(h);
186 if (ret != 0) {
187 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
188 check_counters(ctdb, data);
189 exit(1);
191 if (verbose) DEBUG(DEBUG_ERR, ("transaction committed\n"));
193 /* store the counters and verify that they are sane */
194 if (verbose || (pnn == 0)) {
195 check_counters(ctdb, data);
198 do_sleep(delay);
200 talloc_free(tmp_ctx);
206 main program
208 int main(int argc, const char *argv[])
210 struct ctdb_context *ctdb;
211 struct ctdb_db_context *ctdb_db;
212 int unsafe_writes = 0;
213 struct poptOption popt_options[] = {
214 POPT_AUTOHELP
215 POPT_CTDB_CMDLINE
216 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
217 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "integer" },
218 { "verbose", 'v', POPT_ARG_NONE, &verbose, 0, "switch on verbose mode", NULL },
219 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
220 POPT_TABLEEND
222 int opt;
223 const char **extra_argv;
224 int extra_argc = 0;
225 poptContext pc;
226 struct event_context *ev;
228 if (verbose) {
229 setbuf(stdout, (char *)NULL); /* don't buffer */
230 } else {
231 setlinebuf(stdout);
234 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
236 while ((opt = poptGetNextOpt(pc)) != -1) {
237 switch (opt) {
238 default:
239 fprintf(stderr, "Invalid option %s: %s\n",
240 poptBadOption(pc, 0), poptStrerror(opt));
241 exit(1);
245 /* setup the remaining options for the main program to use */
246 extra_argv = poptGetArgs(pc);
247 if (extra_argv) {
248 extra_argv++;
249 while (extra_argv[extra_argc]) extra_argc++;
252 ev = event_context_init(NULL);
254 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
255 if (ctdb == NULL) {
256 DEBUG(DEBUG_ERR, ("Could not attach to daemon\n"));
257 return 1;
260 /* attach to a specific database */
261 if (unsafe_writes == 1) {
262 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
263 "transaction.tdb", true, TDB_NOSYNC);
264 } else {
265 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
266 "transaction.tdb", true, 0);
269 if (!ctdb_db) {
270 DEBUG(DEBUG_ERR, ("ctdb_attach failed - %s\n", ctdb_errstr(ctdb)));
271 exit(1);
274 DEBUG(DEBUG_ERR, ("Waiting for cluster\n"));
275 while (1) {
276 uint32_t recmode=1;
277 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
278 if (recmode == 0) break;
279 event_loop_once(ev);
282 pnn = ctdb_get_pnn(ctdb);
283 printf("Starting test on node %u. running for %u seconds. sleep delay: %u seconds.\n", pnn, timelimit, delay);
285 if (!verbose && (pnn == 0)) {
286 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
289 test_store_records(ctdb, ev);
291 if (verbose || (pnn == 0)) {
292 if (success != true) {
293 printf("The test FAILED\n");
294 return 1;
295 } else {
296 printf("SUCCESS!\n");
299 return 0;