TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / tests / src / ctdb_randrec.c
1 /* 
2    create a lot of random records, both current records and deleted records
3
4    Copyright (C) Andrew Tridgell  2008
5         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/tevent/tevent.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26 #include "ctdb_private.h"
27
28 #include <sys/time.h>
29 #include <time.h>
30
31 static struct timeval tp1,tp2;
32
33 static void start_timer(void)
34 {
35         gettimeofday(&tp1,NULL);
36 }
37
38 static double end_timer(void)
39 {
40         gettimeofday(&tp2,NULL);
41         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
42                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
43 }
44
45 static int num_records = 10;
46 static int delete_pct = 75;
47 static int base_rec;
48
49 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
50 {
51         TDB_DATA key, data;
52         struct ctdb_db_context *ctdb_db;
53         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
54         int ret;
55         struct ctdb_record_handle *h;
56         uint32_t i=0;
57         
58         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
59
60         srandom(time(NULL) ^ getpid());
61
62         start_timer();
63
64         printf("working with %d records\n", num_records);
65         while (1) {
66                 unsigned r = random() % num_records;
67                 key.dptr = (uint8_t *)&r;
68                 key.dsize = sizeof(r); 
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 (random() % 100 < delete_pct) {
79                         data.dptr = NULL;
80                         data.dsize = 0;
81                 } else {
82                         data.dptr = talloc_zero_size(h, data.dsize + sizeof(r));
83                         data.dsize += sizeof(r);
84                 }
85
86                 ret = ctdb_record_store(h, data);
87                 if (ret != 0) {
88                         printf("Failed to store record\n");
89                 }
90
91                 if (data.dptr == NULL && data.dsize == 0) {
92                         struct ctdb_control_schedule_for_deletion *dd;
93                         TDB_DATA indata;
94                         int32_t status;
95
96                         indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
97                         indata.dptr = talloc_zero_array(ctdb, uint8_t, indata.dsize);
98                         if (indata.dptr == NULL) {
99                                 printf("out of memory\n");
100                                 exit(1);
101                         }
102                         dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
103                         dd->db_id = ctdb_db->db_id;
104                         dd->hdr = *ctdb_header_from_record_handle(h);
105                         dd->keylen = key.dsize;
106                         memcpy(dd->key, key.dptr, key.dsize);
107
108                         ret = ctdb_control(ctdb,
109                                            CTDB_CURRENT_NODE,
110                                            ctdb_db->db_id,
111                                            CTDB_CONTROL_SCHEDULE_FOR_DELETION,
112                                            0, /* flags */
113                                            indata,
114                                            NULL, /* mem_ctx */
115                                            NULL, /* outdata */
116                                            &status,
117                                            NULL, /* timeout : NULL == wait forever */
118                                            NULL); /* error message */
119
120                         talloc_free(indata.dptr);
121
122                         if (ret != 0 || status != 0) {
123                                 DEBUG(DEBUG_ERR, (__location__ " Error sending "
124                                                   "SCHEDULE_FOR_DELETION "
125                                                   "control.\n"));
126                         }
127                 }
128
129                 talloc_free(h);
130
131                 if (i % 1000 == 0) {
132                         printf("%7.0f recs/second   %u total\r", 1000.0 / end_timer(), i);
133                         fflush(stdout);
134                         start_timer();
135                 }
136                 i++;
137         }
138
139         talloc_free(tmp_ctx);
140 }
141
142 /*
143   main program
144 */
145 int main(int argc, const char *argv[])
146 {
147         struct ctdb_context *ctdb;
148         struct ctdb_db_context *ctdb_db;
149
150         struct poptOption popt_options[] = {
151                 POPT_AUTOHELP
152                 POPT_CTDB_CMDLINE
153                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
154                 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
155                 { "delete-pct", 'p', POPT_ARG_INT, &delete_pct, 0, "delete_pct", "integer" },
156                 POPT_TABLEEND
157         };
158         int opt;
159         const char **extra_argv;
160         int extra_argc = 0;
161         poptContext pc;
162         struct event_context *ev;
163
164         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
165
166         while ((opt = poptGetNextOpt(pc)) != -1) {
167                 switch (opt) {
168                 default:
169                         fprintf(stderr, "Invalid option %s: %s\n", 
170                                 poptBadOption(pc, 0), poptStrerror(opt));
171                         exit(1);
172                 }
173         }
174
175         /* setup the remaining options for the main program to use */
176         extra_argv = poptGetArgs(pc);
177         if (extra_argv) {
178                 extra_argv++;
179                 while (extra_argv[extra_argc]) extra_argc++;
180         }
181
182         ev = event_context_init(NULL);
183
184         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
185
186         if (ctdb == NULL) {
187                 printf("failed to connect to daemon\n");
188                 exit(1);
189         }
190
191         /* attach to a specific database */
192         ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
193                               false, 0);
194         if (!ctdb_db) {
195                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
196                 exit(1);
197         }
198
199         store_records(ctdb, ev);
200
201         return 0;
202 }