Merge branch 'master' of ctdb into 'master' of samba
[sfrench/samba-autobuild/.git] / ctdb / tests / src / ctdb_fetch.c
1 /* 
2    simple ctdb benchmark
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt.h"
23 #include "cmdline.h"
24
25 #include <sys/time.h>
26 #include <time.h>
27
28 static struct timeval tp1,tp2;
29
30 static void start_timer(void)
31 {
32         gettimeofday(&tp1,NULL);
33 }
34
35 static double end_timer(void)
36 {
37         gettimeofday(&tp2,NULL);
38         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
39                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
40 }
41
42
43 static int timelimit = 10;
44 static int num_records = 10;
45 static int num_nodes;
46 static int msg_count;
47
48 #define TESTKEY "testkey"
49
50 /*
51   fetch a record
52   store a expanded record
53   send a message to next node to tell it to do the same
54 */
55 static void bench_fetch_1node(struct ctdb_context *ctdb)
56 {
57         TDB_DATA key, data, nulldata;
58         struct ctdb_db_context *ctdb_db;
59         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
60         int dest, ret;
61         struct ctdb_record_handle *h;
62
63         key.dptr = discard_const(TESTKEY);
64         key.dsize = strlen(TESTKEY);
65
66         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
67
68         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
69         if (h == NULL) {
70                 printf("Failed to fetch record '%s' on node %d\n", 
71                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
72                 talloc_free(tmp_ctx);
73                 return;
74         }
75
76         if (data.dsize > 1000) {
77                 data.dsize = 0;
78         }
79
80         if (data.dsize == 0) {
81                 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
82         }
83         data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr, 
84                                                       "msg_count=%d on node %d\n",
85                                                       msg_count, ctdb_get_pnn(ctdb));
86         if (data.dptr == NULL) {
87                 printf("Failed to create record\n");
88                 talloc_free(tmp_ctx);
89                 return;
90         }
91         data.dsize = strlen((const char *)data.dptr)+1;
92
93         ret = ctdb_record_store(h, data);
94         talloc_free(h);
95         if (ret != 0) {
96                 printf("Failed to store record\n");
97         }
98
99         talloc_free(tmp_ctx);
100
101         /* tell the next node to do the same */
102         nulldata.dptr = NULL;
103         nulldata.dsize = 0;
104
105         dest = (ctdb_get_pnn(ctdb) + 1) % num_nodes;
106         ctdb_client_send_message(ctdb, dest, 0, nulldata);
107 }
108
109 /*
110   handler for messages in bench_ring()
111 */
112 static void message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
113                             TDB_DATA data, void *private_data)
114 {
115         msg_count++;
116         bench_fetch_1node(ctdb);
117 }
118
119
120 /*
121  * timeout handler - noop
122  */
123 static void timeout_handler(struct event_context *ev, struct timed_event *timer,
124                             struct timeval curtime, void *private_data)
125 {
126         return;
127 }
128
129 /*
130   benchmark the following:
131
132   fetch a record
133   store a expanded record
134   send a message to next node to tell it to do the same
135
136 */
137 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
138 {
139         int pnn=ctdb_get_pnn(ctdb);
140
141         if (pnn == num_nodes - 1) {
142                 bench_fetch_1node(ctdb);
143         }
144         
145         start_timer();
146         event_add_timed(ev, ctdb, timeval_current_ofs(timelimit,0), timeout_handler, NULL);
147
148         while (end_timer() < timelimit) {
149                 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 0) {
150                         printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
151                         fflush(stdout);
152                 }
153                 if (event_loop_once(ev) != 0) {
154                         printf("Event loop failed!\n");
155                         break;
156                 }
157         }
158
159         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
160 }
161
162 /*
163   handler for reconfigure message
164 */
165 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid, 
166                                 TDB_DATA data, void *private_data)
167 {
168         int *ready = (int *)private_data;
169         *ready = 1;
170 }
171
172 /*
173   main program
174 */
175 int main(int argc, const char *argv[])
176 {
177         struct ctdb_context *ctdb;
178         struct ctdb_db_context *ctdb_db;
179
180         struct poptOption popt_options[] = {
181                 POPT_AUTOHELP
182                 POPT_CTDB_CMDLINE
183                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
184                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
185                 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
186                 POPT_TABLEEND
187         };
188         int opt;
189         const char **extra_argv;
190         int extra_argc = 0;
191         poptContext pc;
192         struct event_context *ev;
193         TDB_DATA key, data;
194         struct ctdb_record_handle *h;
195         int cluster_ready=0;
196
197         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
198
199         while ((opt = poptGetNextOpt(pc)) != -1) {
200                 switch (opt) {
201                 default:
202                         fprintf(stderr, "Invalid option %s: %s\n", 
203                                 poptBadOption(pc, 0), poptStrerror(opt));
204                         exit(1);
205                 }
206         }
207
208         /* talloc_enable_leak_report_full(); */
209
210         /* setup the remaining options for the main program to use */
211         extra_argv = poptGetArgs(pc);
212         if (extra_argv) {
213                 extra_argv++;
214                 while (extra_argv[extra_argc]) extra_argc++;
215         }
216
217         if (num_nodes == 0) {
218                 printf("You must specify the number of nodes\n");
219                 exit(1);
220         }
221
222         ev = event_context_init(NULL);
223         tevent_loop_allow_nesting(ev);
224
225         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
226
227         if (ctdb == NULL) {
228                 printf("failed to connect to ctdb daemon.\n");
229                 exit(1);
230         }
231
232         ctdb_client_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler, 
233                                  &cluster_ready);
234
235         /* attach to a specific database */
236         ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
237                               false, 0);
238         if (!ctdb_db) {
239                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
240                 exit(1);
241         }
242
243         ctdb_client_set_message_handler(ctdb, 0, message_handler, &msg_count);
244
245         printf("Waiting for cluster\n");
246         while (1) {
247                 uint32_t recmode=1;
248                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
249                 if (recmode == 0) break;
250                 event_loop_once(ev);
251         }
252
253         /* This test has a race condition. If CTDB receives the message from previous
254          * node, before this node has registered for that message, this node will never
255          * receive that message and will block on receive. Sleeping for some time will
256          * hopefully ensure that the test program on all the nodes register for messages.
257          */
258         printf("Sleeping for %d seconds\n", num_nodes);
259         sleep(num_nodes);
260         bench_fetch(ctdb, ev);
261
262         key.dptr = discard_const(TESTKEY);
263         key.dsize = strlen(TESTKEY);
264
265         printf("Fetching final record\n");
266
267         h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
268
269         if (h == NULL) {
270                 printf("Failed to fetch record '%s' on node %d\n", 
271                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
272                 exit(1);
273         }
274
275         printf("DATA:\n%s\n", (char *)data.dptr);
276
277         return 0;
278 }