r23798: updated old Temple Place FSF addresses to new URL
[kamenim/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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/events/events.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
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_db_context *ctdb_db;
61         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
62         int dest, ret;
63         struct ctdb_record_handle *h;
64
65         key.dptr = discard_const(TESTKEY);
66         key.dsize = strlen(TESTKEY);
67
68         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
69
70         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
71         if (h == 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_record_store(h, data);
91         talloc_free(h);
92         if (ret != 0) {
93                 printf("Failed to store record\n");
94         }
95
96         talloc_free(tmp_ctx);
97
98         /* tell the next node to do the same */
99         nulldata.dptr = NULL;
100         nulldata.dsize = 0;
101
102         dest = (ctdb_get_vnn(ctdb) + 1) % ctdb_get_num_nodes(ctdb);
103         ctdb_send_message(ctdb, dest, 0, nulldata);
104 }
105
106 /*
107   handler for messages in bench_ring()
108 */
109 static void message_handler(struct ctdb_context *ctdb, uint32_t srvid, 
110                             TDB_DATA data, void *private_data)
111 {
112         msg_count++;
113         bench_fetch_1node(ctdb);
114 }
115
116
117 /*
118   benchmark the following:
119
120   fetch a record
121   store a expanded record
122   send a message to next node to tell it to do the same
123
124 */
125 static void bench_fetch(struct ctdb_context *ctdb, struct event_context *ev)
126 {
127         int vnn=ctdb_get_vnn(ctdb);
128
129         if (vnn == ctdb_get_num_nodes(ctdb)-1) {
130                 bench_fetch_1node(ctdb);
131         }
132         
133         start_timer();
134
135         while (end_timer() < timelimit) {
136                 if (vnn == 0 && msg_count % 100 == 0) {
137                         printf("Fetch: %.2f msgs/sec\r", msg_count/end_timer());
138                         fflush(stdout);
139                 }
140                 if (event_loop_once(ev) != 0) {
141                         printf("Event loop failed!\n");
142                         break;
143                 }
144
145                 if (LogLevel > 9) {
146                         talloc_report_null_full();
147                 }
148         }
149
150         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
151 }
152
153 enum my_functions {FUNC_FETCH=1};
154
155 /*
156   ctdb call function to fetch a record
157 */
158 static int fetch_func(struct ctdb_call_info *call)
159 {
160         call->reply_data = &call->record_data;
161         return 0;
162 }
163
164 /*
165   main program
166 */
167 int main(int argc, const char *argv[])
168 {
169         struct ctdb_context *ctdb;
170         struct ctdb_db_context *ctdb_db;
171
172         struct poptOption popt_options[] = {
173                 POPT_AUTOHELP
174                 POPT_CTDB_CMDLINE
175                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
176                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
177                 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
178                 POPT_TABLEEND
179         };
180         int opt;
181         const char **extra_argv;
182         int extra_argc = 0;
183         int ret;
184         poptContext pc;
185         struct event_context *ev;
186         struct ctdb_call call;
187
188         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
189
190         while ((opt = poptGetNextOpt(pc)) != -1) {
191                 switch (opt) {
192                 default:
193                         fprintf(stderr, "Invalid option %s: %s\n", 
194                                 poptBadOption(pc, 0), poptStrerror(opt));
195                         exit(1);
196                 }
197         }
198
199         /* talloc_enable_leak_report_full(); */
200
201         /* setup the remaining options for the main program to use */
202         extra_argv = poptGetArgs(pc);
203         if (extra_argv) {
204                 extra_argv++;
205                 while (extra_argv[extra_argc]) extra_argc++;
206         }
207
208         ev = event_context_init(NULL);
209
210         ctdb = ctdb_cmdline_init(ev);
211
212         /* attach to a specific database */
213         ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
214         if (!ctdb_db) {
215                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
216                 exit(1);
217         }
218
219         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
220
221         /* start the protocol running */
222         ret = ctdb_start(ctdb);
223
224         ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
225
226         /* wait until all nodes are connected (should not be needed
227            outside of test code) */
228         ctdb_connect_wait(ctdb);
229
230         bench_fetch(ctdb, ev);
231
232         ZERO_STRUCT(call);
233         call.key.dptr = discard_const(TESTKEY);
234         call.key.dsize = strlen(TESTKEY);
235
236         /* fetch the record */
237         call.call_id = FUNC_FETCH;
238         call.call_data.dptr = NULL;
239         call.call_data.dsize = 0;
240
241         ret = ctdb_call(ctdb_db, &call);
242         if (ret == -1) {
243                 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
244                 exit(1);
245         }
246
247         printf("DATA:\n%s\n", (char *)call.reply_data.dptr);
248
249         /* go into a wait loop to allow other nodes to complete */
250         ctdb_shutdown(ctdb);
251
252         return 0;
253 }