0bf92b345e817150171833b4a2eb5c7e305748c3
[vlendec/samba-autobuild/.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 "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
25
26 #include <sys/time.h>
27 #include <time.h>
28
29 static struct timeval tp1,tp2;
30
31 static void start_timer(void)
32 {
33         gettimeofday(&tp1,NULL);
34 }
35
36 static double end_timer(void)
37 {
38         gettimeofday(&tp2,NULL);
39         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
40                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
41 }
42
43 static int timelimit = 10;
44
45 static unsigned int pnn;
46
47 static TDB_DATA old_data;
48
49 static int success = true;
50
51 static void each_second(struct event_context *ev, struct timed_event *te, 
52                                          struct timeval t, void *private_data)
53 {
54         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
55         int i;
56         uint32_t *old_counters;
57
58
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]);
63         }
64         printf("\n"); 
65
66         event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
67 }
68
69 static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
70 {
71         int i;
72         uint32_t *counters, *old_counters;
73         unsigned char *tmp_dptr;
74
75         counters     = (uint32_t *)data.dptr;
76         old_counters = (uint32_t *)old_data.dptr;
77
78         /* check that all the counters are monotonic increasing */
79         for (i=0; i<old_data.dsize/sizeof(uint32_t); i++) {
80                 if (counters[i]<old_counters[i]) {
81                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n", 
82                                getpid(), i, old_counters[i], counters[i]);
83                         success = false;
84                 }
85         }
86
87         if (old_data.dsize != data.dsize) {
88                 old_data.dsize = data.dsize;
89                 tmp_dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
90                 if (tmp_dptr == NULL) {
91                         printf("[%4u] ERROR: talloc_realloc_size failed.\n", getpid());
92                         success = false;
93                         return;
94                 } else {
95                         old_data.dptr = tmp_dptr;
96                 }
97         }
98
99         memcpy(old_data.dptr, data.dptr, data.dsize);
100 }
101
102
103
104 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
105 {
106         TDB_DATA key;
107         struct ctdb_db_context *ctdb_db;
108
109         ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
110
111         key.dptr = discard_const("testkey");
112         key.dsize = strlen((const char *)key.dptr)+1;
113
114         start_timer();
115         while (end_timer() < timelimit) {
116                 TDB_DATA data;
117                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
118                 struct ctdb_transaction_handle *h;
119                 int ret;
120                 uint32_t *counters;
121
122                 h = ctdb_transaction_start(ctdb_db, tmp_ctx);
123                 if (h == NULL) {
124                         printf("Failed to start transaction on node %d\n",
125                                ctdb_get_pnn(ctdb));
126                         talloc_free(tmp_ctx);
127                         return;
128                 }
129
130                 ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
131                 if (ret != 0) {
132                         DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
133                         exit(1);
134                 }
135
136                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
137                         unsigned char *ptr = data.dptr;
138                         
139                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
140                         memcpy(data.dptr, ptr, data.dsize);
141                         talloc_free(ptr);
142
143                         data.dsize = sizeof(uint32_t) * (pnn+1);
144                 }
145
146                 if (data.dptr == NULL) {
147                         printf("Failed to realloc array\n");
148                         talloc_free(tmp_ctx);
149                         return;
150                 }
151
152                 counters = (uint32_t *)data.dptr;
153
154                 /* bump our counter */
155                 counters[pnn]++;
156
157                 ret = ctdb_transaction_store(h, key, data);
158                 if (ret != 0) {
159                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
160                         exit(1);
161                 }
162
163                 ret = ctdb_transaction_commit(h);
164                 if (ret != 0) {
165                         DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
166                         //exit(1);
167                 }
168
169                 /* store the counters and verify that they are sane */
170                 if (pnn == 0) {
171                         check_counters(ctdb, data);
172                 }
173
174                 talloc_free(tmp_ctx);
175         }
176
177 }
178
179 /*
180   main program
181 */
182 int main(int argc, const char *argv[])
183 {
184         struct ctdb_context *ctdb;
185         struct ctdb_db_context *ctdb_db;
186         int unsafe_writes = 0;
187         struct poptOption popt_options[] = {
188                 POPT_AUTOHELP
189                 POPT_CTDB_CMDLINE
190                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
191                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
192                 POPT_TABLEEND
193         };
194         int opt;
195         const char **extra_argv;
196         int extra_argc = 0;
197         poptContext pc;
198         struct event_context *ev;
199
200         setlinebuf(stdout);
201
202         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
203
204         while ((opt = poptGetNextOpt(pc)) != -1) {
205                 switch (opt) {
206                 default:
207                         fprintf(stderr, "Invalid option %s: %s\n", 
208                                 poptBadOption(pc, 0), poptStrerror(opt));
209                         exit(1);
210                 }
211         }
212
213         /* setup the remaining options for the main program to use */
214         extra_argv = poptGetArgs(pc);
215         if (extra_argv) {
216                 extra_argv++;
217                 while (extra_argv[extra_argc]) extra_argc++;
218         }
219
220         ev = event_context_init(NULL);
221
222         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
223         if (ctdb == NULL) {
224                 printf("Could not attach to daemon\n");
225                 return 1;
226         }
227
228         /* attach to a specific database */
229         if (unsafe_writes == 1) {
230                 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
231                                       "persistent.tdb", true, TDB_NOSYNC);
232         } else {
233                 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0),
234                                       "persistent.tdb", true, 0);
235         }
236
237         if (!ctdb_db) {
238                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
239                 exit(1);
240         }
241
242         printf("Waiting for cluster\n");
243         while (1) {
244                 uint32_t recmode=1;
245                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
246                 if (recmode == 0) break;
247                 event_loop_once(ev);
248         }
249
250         pnn = ctdb_get_pnn(ctdb);
251         printf("Starting test on node %u. running for %u seconds\n", pnn, timelimit);
252
253         if (pnn == 0) {
254                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
255         }
256
257         test_store_records(ctdb, ev);
258
259         if (pnn == 0) {
260                 if (success != true) {
261                         printf("The test FAILED\n");
262                         return 1;
263                 } else {
264                         printf("SUCCESS!\n");
265                 }
266         }
267         return 0;
268 }