TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / tests / src / ctdb_persistent.c
1 /* 
2    simple tool to test persistent databases
3
4    Copyright (C) Andrew Tridgell  2006-2007
5    Copyright (c) Ronnie sahlberg  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/tevent/tevent.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29
30 static struct timeval tp1,tp2;
31
32 static void start_timer(void)
33 {
34         gettimeofday(&tp1,NULL);
35 }
36
37 static double end_timer(void)
38 {
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));
42 }
43
44 static int timelimit = 10;
45
46 static unsigned int pnn;
47
48 static TDB_DATA old_data;
49
50 static int success = true;
51
52 static void each_second(struct event_context *ev, struct timed_event *te, 
53                                          struct timeval t, void *private_data)
54 {
55         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
56         int i;
57         uint32_t *old_counters;
58
59
60         printf("[%4u] Counters: ", getpid());
61         old_counters = (uint32_t *)old_data.dptr;
62         for (i=0;i<old_data.dsize/sizeof(uint32_t); i++) {
63                 printf("%6u ", old_counters[i]);
64         }
65         printf("\n"); 
66
67         event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
68 }
69
70 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
71 {
72         int i;
73         uint32_t *counters, *old_counters;
74         unsigned char *tmp_dptr;
75
76         counters     = (uint32_t *)data.dptr;
77         old_counters = (uint32_t *)old_data.dptr;
78
79         /* check that all the counters are monotonic increasing */
80         for (i=0; i<old_data.dsize/sizeof(uint32_t); i++) {
81                 if (counters[i]<old_counters[i]) {
82                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n", 
83                                getpid(), i, old_counters[i], counters[i]);
84                         success = false;
85                 }
86         }
87
88         if (old_data.dsize != data.dsize) {
89                 old_data.dsize = data.dsize;
90                 tmp_dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
91                 if (tmp_dptr == NULL) {
92                         printf("[%4u] ERROR: talloc_realloc_size failed.\n", getpid());
93                         success = false;
94                         return;
95                 } else {
96                         old_data.dptr = tmp_dptr;
97                 }
98         }
99
100         memcpy(old_data.dptr, data.dptr, data.dsize);
101 }
102
103
104
105 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
106 {
107         TDB_DATA key;
108         struct ctdb_db_context *ctdb_db;
109
110         ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
111
112         key.dptr = discard_const("testkey");
113         key.dsize = strlen((const char *)key.dptr)+1;
114
115         start_timer();
116         while (end_timer() < timelimit) {
117                 TDB_DATA data;
118                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
119                 struct ctdb_transaction_handle *h;
120                 int ret;
121                 uint32_t *counters;
122
123                 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
124                 if (h == NULL) {
125                         printf("Failed to start transaction on node %d\n",
126                                ctdb_get_pnn(ctdb));
127                         talloc_free(tmp_ctx);
128                         return;
129                 }
130
131                 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
132                 if (ret != 0) {
133                         DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
134                         exit(1);
135                 }
136
137                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
138                         unsigned char *ptr = data.dptr;
139                         
140                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
141                         memcpy(data.dptr, ptr, data.dsize);
142                         talloc_free(ptr);
143
144                         data.dsize = sizeof(uint32_t) * (pnn+1);
145                 }
146
147                 if (data.dptr == NULL) {
148                         printf("Failed to realloc array\n");
149                         talloc_free(tmp_ctx);
150                         return;
151                 }
152
153                 counters = (uint32_t *)data.dptr;
154
155                 /* bump our counter */
156                 counters[pnn]++;
157
158                 ret = ctdb_transaction_store(h, key, data);
159                 if (ret != 0) {
160                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
161                         exit(1);
162                 }
163
164                 ret = ctdb_transaction_commit(h);
165                 if (ret != 0) {
166                         DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
167                         //exit(1);
168                 }
169
170                 /* store the counters and verify that they are sane */
171                 if (pnn == 0) {
172                         check_counters(ctdb, data);
173                 }
174
175                 talloc_free(tmp_ctx);
176         }
177
178 }
179
180 /*
181   main program
182 */
183 int main(int argc, const char *argv[])
184 {
185         struct ctdb_context *ctdb;
186         struct ctdb_db_context *ctdb_db;
187         int unsafe_writes = 0;
188         struct poptOption popt_options[] = {
189                 POPT_AUTOHELP
190                 POPT_CTDB_CMDLINE
191                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
192                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
193                 POPT_TABLEEND
194         };
195         int opt;
196         const char **extra_argv;
197         int extra_argc = 0;
198         poptContext pc;
199         struct event_context *ev;
200
201         setlinebuf(stdout);
202
203         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
204
205         while ((opt = poptGetNextOpt(pc)) != -1) {
206                 switch (opt) {
207                 default:
208                         fprintf(stderr, "Invalid option %s: %s\n", 
209                                 poptBadOption(pc, 0), poptStrerror(opt));
210                         exit(1);
211                 }
212         }
213
214         /* setup the remaining options for the main program to use */
215         extra_argv = poptGetArgs(pc);
216         if (extra_argv) {
217                 extra_argv++;
218                 while (extra_argv[extra_argc]) extra_argc++;
219         }
220
221         ev = event_context_init(NULL);
222
223         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
224         if (ctdb == NULL) {
225                 printf("Could not attach to daemon\n");
226                 return 1;
227         }
228
229         /* attach to a specific database */
230         if (unsafe_writes == 1) {
231                 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
232                                       "persistent.tdb", true, TDB_NOSYNC);
233         } else {
234                 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
235                                       "persistent.tdb", true, 0);
236         }
237
238         if (!ctdb_db) {
239                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
240                 exit(1);
241         }
242
243         printf("Waiting for cluster\n");
244         while (1) {
245                 uint32_t recmode=1;
246                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
247                 if (recmode == 0) break;
248                 event_loop_once(ev);
249         }
250
251         pnn = ctdb_get_pnn(ctdb);
252         printf("Starting test on node %u. running for %u seconds\n", pnn, timelimit);
253
254         if (pnn == 0) {
255                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
256         }
257
258         test_store_records(ctdb, ev);
259
260         if (pnn == 0) {
261                 if (success != true) {
262                         printf("The test FAILED\n");
263                         return 1;
264                 } else {
265                         printf("SUCCESS!\n");
266                 }
267         }
268         return 0;
269 }