rename ctdb.h to ctdb_protocol.h
[sahlberg/ctdb.git] / 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 "lib/events/events.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
25 #include "include/ctdb_protocol.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
45 static int timelimit = 10;
46 static int num_records = 10;
47 static int num_nodes;
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_pnn(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_pnn(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_pnn(ctdb) + 1) % num_nodes;
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, uint64_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 pnn=ctdb_get_pnn(ctdb);
128
129         if (pnn == num_nodes - 1) {
130                 bench_fetch_1node(ctdb);
131         }
132         
133         start_timer();
134
135         while (end_timer() < timelimit) {
136                 if (pnn == 0 && msg_count % 100 == 0 && end_timer() > 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
146         printf("Fetch: %.2f msgs/sec\n", msg_count/end_timer());
147 }
148
149 /*
150   handler for reconfigure message
151 */
152 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid, 
153                                 TDB_DATA data, void *private_data)
154 {
155         int *ready = (int *)private_data;
156         *ready = 1;
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
167         struct poptOption popt_options[] = {
168                 POPT_AUTOHELP
169                 POPT_CTDB_CMDLINE
170                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
171                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
172                 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
173                 POPT_TABLEEND
174         };
175         int opt;
176         const char **extra_argv;
177         int extra_argc = 0;
178         poptContext pc;
179         struct event_context *ev;
180         TDB_DATA key, data;
181         struct ctdb_record_handle *h;
182         int cluster_ready=0;
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         /* talloc_enable_leak_report_full(); */
196
197         /* setup the remaining options for the main program to use */
198         extra_argv = poptGetArgs(pc);
199         if (extra_argv) {
200                 extra_argv++;
201                 while (extra_argv[extra_argc]) extra_argc++;
202         }
203
204         if (num_nodes == 0) {
205                 printf("You must specify the number of nodes\n");
206                 exit(1);
207         }
208
209         ev = event_context_init(NULL);
210
211         ctdb = ctdb_cmdline_client(ev);
212
213         ctdb_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler, 
214                                  &cluster_ready);
215
216         /* attach to a specific database */
217         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
218         if (!ctdb_db) {
219                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
220                 exit(1);
221         }
222
223         ctdb_set_message_handler(ctdb, 0, message_handler, &msg_count);
224
225         printf("Waiting for cluster\n");
226         while (1) {
227                 uint32_t recmode=1;
228                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
229                 if (recmode == 0) break;
230                 event_loop_once(ev);
231         }
232
233         bench_fetch(ctdb, ev);
234
235         key.dptr = discard_const(TESTKEY);
236         key.dsize = strlen(TESTKEY);
237
238         printf("Fetching final record\n");
239
240         h = ctdb_fetch_lock(ctdb_db, ctdb, key, &data);
241
242         if (h == NULL) {
243                 printf("Failed to fetch record '%s' on node %d\n", 
244                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
245                 exit(1);
246         }
247
248         printf("DATA:\n%s\n", (char *)data.dptr);
249
250         return 0;
251 }