added event context
[vlendec/samba-autobuild/.git] / ctdb / ctdb_test.c
1 /* 
2    ctdb test harness
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 enum my_functions {FUNC_SORT=1, FUNC_FETCH=2};
27
28 static int int_compare(int *i1, int *i2)
29 {
30         return *i1 - *i2;
31 }
32
33 /*
34   add an integer into a record in sorted order
35 */
36 static int sort_func(struct ctdb_call *call)
37 {
38         if (call->call_data == NULL ||
39             call->call_data->dsize != sizeof(int)) {
40                 return CTDB_ERR_INVALID;
41         }
42         call->new_data = talloc(call, TDB_DATA);
43         if (call->new_data == NULL) {
44                 return CTDB_ERR_NOMEM;
45         }
46         call->new_data->dptr = talloc_size(call, 
47                                            call->record_data.dsize + 
48                                            call->call_data->dsize);
49         if (call->new_data->dptr == NULL) {
50                 return CTDB_ERR_NOMEM;
51         }
52         call->new_data->dsize = call->record_data.dsize + call->call_data->dsize;
53         memcpy(call->new_data->dptr+call->record_data.dsize,
54                call->call_data->dptr, call->call_data->dsize);
55         qsort(call->new_data->dptr, call->new_data->dsize / sizeof(int),
56               sizeof(int), (comparison_fn_t)int_compare);
57         return 0;
58 }
59
60 /*
61   ctdb call function to fetch a record
62 */
63 static int fetch_func(struct ctdb_call *call)
64 {
65         call->reply_data = &call->record_data;
66         return 0;
67 }
68
69 /*
70   main program
71 */
72 int main(int argc, const char *argv[])
73 {
74         struct ctdb_context *ctdb;
75         const char *nlist = NULL;
76         const char *myaddress = NULL;
77
78         struct poptOption popt_options[] = {
79                 POPT_AUTOHELP
80                 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
81                 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
82         };
83         int opt;
84         const char **extra_argv;
85         int extra_argc = 0;
86         int i, ret;
87         TDB_DATA key, data;
88         poptContext pc;
89         struct event_context *ev;
90
91         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
92
93         while ((opt = poptGetNextOpt(pc)) != -1) {
94                 switch (opt) {
95                 default:
96                         fprintf(stderr, "Invalid option %s: %s\n", 
97                                 poptBadOption(pc, 0), poptStrerror(opt));
98                         exit(1);
99                 }
100         }
101
102         /* setup the remaining options for the main program to use */
103         extra_argv = poptGetArgs(pc);
104         if (extra_argv) {
105                 extra_argv++;
106                 while (extra_argv[extra_argc]) extra_argc++;
107         }
108
109         if (nlist == NULL || myaddress == NULL) {
110                 printf("You must provide a node list with --nlist and an address with --listen\n");
111                 exit(1);
112         }
113
114         ev = event_context_init(NULL);
115
116         /* initialise ctdb */
117         ctdb = ctdb_init(ev);
118         if (ctdb == NULL) {
119                 printf("Failed to init ctdb\n");
120                 exit(1);
121         }
122
123         /* tell ctdb what address to listen on */
124         ret = ctdb_set_address(ctdb, myaddress);
125         if (ret == -1) {
126                 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
127                 exit(1);
128         }
129
130         /* tell ctdb what nodes are available */
131         ret = ctdb_set_nlist(ctdb, nlist);
132         if (ret == -1) {
133                 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
134                 exit(1);
135         }
136
137         /* setup a ctdb call function */
138         ret = ctdb_set_call(ctdb, sort_func,  FUNC_SORT);
139         ret = ctdb_set_call(ctdb, fetch_func, FUNC_FETCH);
140
141         /* attach to a specific database */
142         ret = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
143         if (ret == -1) {
144                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
145                 exit(1);
146         }
147
148         /* start the protocol running */
149         ret = ctdb_start(ctdb);
150        
151         key.dptr = "test";
152         key.dsize = strlen("test")+1;
153
154         /* add some random data */
155         for (i=0;i<100;i++) {
156                 int v = random();
157                 data.dptr = (uint8_t *)&v;
158                 data.dsize = sizeof(v);
159                 ret = ctdb_call(ctdb, key, FUNC_SORT, &data, NULL);
160                 if (ret == -1) {
161                         printf("ctdb_call FUNC_SORT failed - %s\n", ctdb_errstr(ctdb));
162                         exit(1);
163                 }
164         }
165
166         /* fetch the record */
167         ret = ctdb_call(ctdb, key, FUNC_FETCH, NULL, &data);
168         if (ret == -1) {
169                 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
170                 exit(1);
171         }
172
173         for (i=0;i<data.dsize/sizeof(int);i++) {
174                 printf("%3d\n", ((int *)data.dptr)[i]);
175         }
176         
177         /* shut it down */
178         talloc_free(ctdb);
179         return 0;
180 }