r22231: merge from bzr ctdb tree
[samba.git] / source4 / cluster / ctdb / tests / ctdb_fetch.c
1 /* 
2    simple ctdb benchmark
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.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
44 static int timelimit = 10;
45 static int num_records = 10;
46 static int num_msgs = 1;
47
48 static int msg_count;
49
50 #define TESTKEY "testkey"
51
52 /*
53   fetch a record
54   store a expanded record
55   send a message to next node to tell it to do the same
56 */
57 static void bench_fetch_1node(struct ctdb_context *ctdb)
58 {
59         TDB_DATA key, data, nulldata;
60         struct ctdb_record_handle *rec;
61         struct ctdb_db_context *ctdb_db;
62         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
63         int dest, ret;
64
65         key.dptr = discard_const("testkey");
66         key.dsize = strlen((const char *)key.dptr);
67
68         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
69
70         rec = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
71         if (rec == NULL) {
72                 printf("Failed to fetch record '%s' on node %d\n", 
73                        (const char *)key.dptr, ctdb_get_vnn(ctdb));
74                 talloc_free(tmp_ctx);
75                 return;
76         }
77
78         if (data.dsize > 1000) {
79                 data.dsize = 0;
80         }
81
82         if (data.dsize == 0) {
83                 data.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "Test data\n");
84         }
85         data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr, 
86                                                       "msg_count=%d on node %d\n",
87                                                       msg_count, ctdb_get_vnn(ctdb));
88         data.dsize = strlen((const char *)data.dptr)+1;
89
90         ret = ctdb_store_unlock(rec, data);
91         if (ret != 0) {
92                 printf("Failed to store record\n");
93         }
94
95         talloc_free(tmp_ctx);
96
97         /* tell the next node to do the same */
98         nulldata.dptr = NULL;
99         nulldata.dsize = 0;
100
101         dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
102         ctdb_send_message(ctdb, dest, 0, nulldata);
103 }
104
105 /*
106   handler for messages in bench_ring()
107 */
108 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
109                             TDB_DATA data, void *private_data)
110 {
111         msg_count++;
112         bench_fetch_1node(ctdb);
113 }
114
115
116 /*
117   benchmark the following:
118
119   fetch a record
120   store a expanded record
121   send a message to next node to tell it to do the same
122
123 */
124 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
125 {
126         int vnn=ctdb_get_vnn(ctdb);
127
128         if (vnn == ctdb_get_num_nodes(ctdb)-1) {
129                 bench_fetch_1node(ctdb);
130         }
131         
132         start_timer();
133
134         while (end_timer() < timelimit) {
135                 if (vnn == 0 && msg_count % 100 == 0) {
136                         printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
137                         fflush(stdout);
138                 }
139                 if (event_loop_once(ev) != 0) {
140                         printf("Event loop failed!\n");
141                         break;
142                 }
143         }
144
145         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
146 }
147
148 enum my_functions {FUNC_FETCH=1};
149
150 /*
151   ctdb call function to fetch a record
152 */
153 static int fetch_func(struct ctdb_call_info *call)
154 {
155         call->reply_data = &call->record_data;
156         return 0;
157 }
158
159 /*
160   main program
161 */
162 int main(int argc, const char *argv[])
163 {
164         struct ctdb_context *ctdb;
165         struct ctdb_db_context *ctdb_db;
166         const char *nlist = NULL;
167         const char *transport = "tcp";
168         const char *myaddress = NULL;
169         int self_connect=0;
170         int daemon_mode=0;
171
172         struct poptOption popt_options[] = {
173                 POPT_AUTOHELP
174                 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
175                 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
176                 { "transport", 0, POPT_ARG_STRING, &transport, 0, "protocol transport", NULL },
177                 { "self-connect", 0, POPT_ARG_NONE, &self_connect, 0, "enable self connect", "boolean" },
178                 { "daemon", 0, POPT_ARG_NONE, &daemon_mode, 0, "spawn a ctdb daemon", "boolean" },
179                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
180                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
181                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
182                 POPT_TABLEEND
183         };
184         int opt;
185         const char **extra_argv;
186         int extra_argc = 0;
187         int ret;
188         poptContext pc;
189         struct event_context *ev;
190         struct ctdb_call call;
191
192         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
193
194         while ((opt = poptGetNextOpt(pc)) != -1) {
195                 switch (opt) {
196                 default:
197                         fprintf(stderr, "Invalid option %s: %s\n", 
198                                 poptBadOption(pc, 0), poptStrerror(opt));
199                         exit(1);
200                 }
201         }
202
203         /* setup the remaining options for the main program to use */
204         extra_argv = poptGetArgs(pc);
205         if (extra_argv) {
206                 extra_argv++;
207                 while (extra_argv[extra_argc]) extra_argc++;
208         }
209
210         if (nlist == NULL || myaddress == NULL) {
211                 printf("You must provide a node list with --nlist and an address with --listen\n");
212                 exit(1);
213         }
214
215         ev = event_context_init(NULL);
216
217         /* initialise ctdb */
218         ctdb = ctdb_init(ev);
219         if (ctdb == NULL) {
220                 printf("Failed to init ctdb\n");
221                 exit(1);
222         }
223
224         if (self_connect) {
225                 ctdb_set_flags(ctdb, CTDB_FLAG_SELF_CONNECT);
226         }
227         if (daemon_mode) {
228                 ctdb_set_flags(ctdb, CTDB_FLAG_DAEMON_MODE);
229         }
230
231         ret = ctdb_set_transport(ctdb, transport);
232         if (ret == -1) {
233                 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
234                 exit(1);
235         }
236
237         /* tell ctdb what address to listen on */
238         ret = ctdb_set_address(ctdb, myaddress);
239         if (ret == -1) {
240                 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
241                 exit(1);
242         }
243
244         /* tell ctdb what nodes are available */
245         ret = ctdb_set_nlist(ctdb, nlist);
246         if (ret == -1) {
247                 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
248                 exit(1);
249         }
250
251         /* attach to a specific database */
252         ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
253         if (!ctdb_db) {
254                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
255                 exit(1);
256         }
257
258         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
259
260         /* start the protocol running */
261         ret = ctdb_start(ctdb);
262
263         ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
264
265         /* wait until all nodes are connected (should not be needed
266            outside of test code) */
267         ctdb_connect_wait(ctdb);
268
269         bench_fetch(ctdb, ev);
270
271         ZERO_STRUCT(call);
272         call.key.dptr = discard_const(TESTKEY);
273         call.key.dsize = strlen(TESTKEY);
274
275         /* fetch the record */
276         call.call_id = FUNC_FETCH;
277         call.call_data.dptr = NULL;
278         call.call_data.dsize = 0;
279
280         ret = ctdb_call(ctdb_db, &call);
281         if (ret == -1) {
282                 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
283                 exit(1);
284         }
285
286         printf("DATA:\n%s\n", (char *)call.reply_data.dptr);
287
288         /* shut it down */
289         talloc_free(ctdb);
290         return 0;
291 }