add a licence file
[martins/samba.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/events/events.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
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                 old_data.dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
90         }
91
92         memcpy(old_data.dptr, data.dptr, data.dsize);
93 }
94
95
96
97 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
98 {
99         TDB_DATA key;
100         struct ctdb_db_context *ctdb_db;
101
102         ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
103
104         key.dptr = discard_const("testkey");
105         key.dsize = strlen((const char *)key.dptr)+1;
106
107         start_timer();
108         while (end_timer() < timelimit) {
109                 TDB_DATA data;
110                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
111                 struct ctdb_record_handle *h;
112                 int ret;
113                 uint32_t *counters;
114
115                 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
116                 if (h == NULL) {
117                         printf("Failed to fetch record '%s' on node %d\n", 
118                                (const char *)key.dptr, ctdb_get_pnn(ctdb));
119                         talloc_free(tmp_ctx);
120                         return;
121                 }
122
123                 if (data.dsize < sizeof(uint32_t) * (pnn+1)) {
124                         unsigned char *ptr = data.dptr;
125
126                         data.dptr = talloc_zero_size(tmp_ctx, sizeof(uint32_t) * (pnn+1));
127                         memcpy(data.dptr, ptr, data.dsize);
128                         talloc_free(ptr);
129
130                         data.dsize = sizeof(uint32_t) * (pnn+1);
131                 }
132
133                 if (data.dptr == NULL) {
134                         printf("Failed to realloc array\n");
135                         talloc_free(tmp_ctx);
136                         return;
137                 }
138
139                 counters = (uint32_t *)data.dptr;
140
141                 /* bump our counter */
142                 counters[pnn]++;
143
144                 ret = ctdb_record_store(h, data);
145                 if (ret != 0) {
146                         DEBUG(DEBUG_ERR,("Failed to store record\n"));
147                         exit(1);
148                 }
149
150                 /* store the counters and verify that they are sane */
151                 if (pnn == 0) {
152                         check_counters(ctdb, data);
153                 }
154
155                 talloc_free(h);
156                 talloc_free(tmp_ctx);
157         }
158
159 }
160
161 /*
162   main program
163 */
164 int main(int argc, const char *argv[])
165 {
166         struct ctdb_context *ctdb;
167         struct ctdb_db_context *ctdb_db;
168         int unsafe_writes = 0;
169         struct poptOption popt_options[] = {
170                 POPT_AUTOHELP
171                 POPT_CTDB_CMDLINE
172                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
173                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
174                 POPT_TABLEEND
175         };
176         int opt;
177         const char **extra_argv;
178         int extra_argc = 0;
179         poptContext pc;
180         struct event_context *ev;
181
182         setlinebuf(stdout);
183
184         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
185
186         while ((opt = poptGetNextOpt(pc)) != -1) {
187                 switch (opt) {
188                 default:
189                         fprintf(stderr, "Invalid option %s: %s\n", 
190                                 poptBadOption(pc, 0), poptStrerror(opt));
191                         exit(1);
192                 }
193         }
194
195         /* setup the remaining options for the main program to use */
196         extra_argv = poptGetArgs(pc);
197         if (extra_argv) {
198                 extra_argv++;
199                 while (extra_argv[extra_argc]) extra_argc++;
200         }
201
202         ev = event_context_init(NULL);
203
204         ctdb = ctdb_cmdline_client(ev);
205         if (ctdb == NULL) {
206                 printf("Could not attach to daemon\n");
207                 return 1;
208         }
209
210         /* attach to a specific database */
211         if (unsafe_writes == 1) {
212                 ctdb_db = ctdb_attach(ctdb, "persistent.tdb", true, TDB_NOSYNC);
213         } else {
214                 ctdb_db = ctdb_attach(ctdb, "persistent.tdb", true, 0);
215         }
216
217         if (!ctdb_db) {
218                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
219                 exit(1);
220         }
221
222         printf("Waiting for cluster\n");
223         while (1) {
224                 uint32_t recmode=1;
225                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
226                 if (recmode == 0) break;
227                 event_loop_once(ev);
228         }
229
230         pnn = ctdb_get_pnn(ctdb);
231         printf("Starting test on node %u. running for %u seconds\n", pnn, timelimit);
232
233         if (pnn == 0) {
234                 event_add_timed(ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
235         }
236
237         test_store_records(ctdb, ev);
238
239         if (pnn == 0) {
240                 if (success != true) {
241                         printf("The test FAILED\n");
242                         return 1;
243                 } else {
244                         printf("SUCCESS!\n");
245                 }
246         }
247         return 0;
248 }