client: add timeout argument to ctdb_attach
[sahlberg/ctdb.git] / tests / src / ctdb_store.c
1 /* 
2    simple tool to create a lot of records on a tdb and to read them out
3
4    Copyright (C) Andrew Tridgell  2006
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
27 #include <sys/time.h>
28 #include <time.h>
29
30 static int num_records = 10;
31 static int base_rec;
32
33 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
34 {
35         TDB_DATA key, data;
36         struct ctdb_db_context *ctdb_db;
37         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
38         int ret;
39         struct ctdb_record_handle *h;
40         uint32_t i;
41         
42         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
43
44         printf("creating %d records\n", num_records);
45         for (i=0;i<num_records;i++) {
46                 int r = base_rec + i;
47                 key.dptr = (uint8_t *)&r;
48                 key.dsize = sizeof(uint32_t); 
49
50                 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
51                 if (h == NULL) {
52                         printf("Failed to fetch record '%s' on node %d\n", 
53                                (const char *)key.dptr, ctdb_get_pnn(ctdb));
54                         talloc_free(tmp_ctx);
55                         return;
56                 }
57
58                 data.dptr = (uint8_t *)&i;
59                 data.dsize = sizeof(uint32_t);
60
61                 ret = ctdb_record_store(h, data);
62                 talloc_free(h);
63                 if (ret != 0) {
64                         printf("Failed to store record\n");
65                 }
66                 if (i % 1000 == 0) {
67                         printf("%u\r", i);
68                         fflush(stdout);
69                 }
70         }
71
72         printf("fetching all %d records\n", num_records);
73         while (1) {
74                 for (i=0;i<num_records;i++) {
75                         int r = base_rec + i;
76                         key.dptr = (uint8_t *)&r;
77                         key.dsize = sizeof(uint32_t); 
78
79                         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
80                         if (h == NULL) {
81                                 printf("Failed to fetch record '%s' on node %d\n", 
82                                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
83                                 talloc_free(tmp_ctx);
84                                 return;
85                         }
86                         talloc_free(h);
87                 }
88                 sleep(1);
89                 printf(".");
90                 fflush(stdout);
91         }
92
93         talloc_free(tmp_ctx);
94 }
95
96 /*
97   main program
98 */
99 int main(int argc, const char *argv[])
100 {
101         struct ctdb_context *ctdb;
102         struct ctdb_db_context *ctdb_db;
103
104         struct poptOption popt_options[] = {
105                 POPT_AUTOHELP
106                 POPT_CTDB_CMDLINE
107                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
108                 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
109                 POPT_TABLEEND
110         };
111         int opt;
112         const char **extra_argv;
113         int extra_argc = 0;
114         poptContext pc;
115         struct event_context *ev;
116
117         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
118
119         while ((opt = poptGetNextOpt(pc)) != -1) {
120                 switch (opt) {
121                 default:
122                         fprintf(stderr, "Invalid option %s: %s\n", 
123                                 poptBadOption(pc, 0), poptStrerror(opt));
124                         exit(1);
125                 }
126         }
127
128         /* talloc_enable_leak_report_full(); */
129
130         /* setup the remaining options for the main program to use */
131         extra_argv = poptGetArgs(pc);
132         if (extra_argv) {
133                 extra_argv++;
134                 while (extra_argv[extra_argc]) extra_argc++;
135         }
136
137         ev = event_context_init(NULL);
138
139         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
140
141         if (ctdb == NULL) {
142                 printf("failed to connect to ctdb daemon.\n");
143                 exit(1);
144         }
145
146         /* attach to a specific database */
147         ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb", false, 0);
148         if (!ctdb_db) {
149                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
150                 exit(1);
151         }
152
153         printf("Waiting for cluster\n");
154         while (1) {
155                 uint32_t recmode=1;
156                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
157                 if (recmode == 0) break;
158                 event_loop_once(ev);
159         }
160
161         store_records(ctdb, ev);
162
163         return 0;
164 }