Add a new test tool that fetch locks a record and then blocks until it receives
[sahlberg/ctdb.git] / tests / src / ctdb_fetch_lock_once.c
1 /* 
2    simple ctdb test tool
3    This test just fetch_locks a record and releases it once.
4
5    Copyright (C) Ronnie Sahlberg 2009
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 static struct ctdb_db_context *ctdb_db;
28
29 #define TESTKEY "testkey"
30
31
32 /*
33         Just try locking/unlocking a single record once
34 */
35 static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev)
36 {
37         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
38         TDB_DATA key, data;
39         struct ctdb_record_handle *h;
40
41         key.dptr = discard_const(TESTKEY);
42         key.dsize = strlen(TESTKEY);
43
44         printf("Trying to fetch lock the record ...\n");
45
46         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
47         if (h == NULL) {
48                 printf("Failed to fetch record '%s' on node %d\n", 
49                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
50                 talloc_free(tmp_ctx);
51                 exit(10);
52         }
53
54         printf("Record fetchlocked.\n");
55         printf("Press enter to release the record ...\n");
56         (void)getchar();
57
58         talloc_free(tmp_ctx);
59         printf("Record released.\n");
60 }
61
62 /*
63   main program
64 */
65 int main(int argc, const char *argv[])
66 {
67         struct ctdb_context *ctdb;
68
69         struct poptOption popt_options[] = {
70                 POPT_AUTOHELP
71                 POPT_CTDB_CMDLINE
72                 POPT_TABLEEND
73         };
74         int opt;
75         const char **extra_argv;
76         int extra_argc = 0;
77         poptContext pc;
78         struct event_context *ev;
79
80         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
81
82         while ((opt = poptGetNextOpt(pc)) != -1) {
83                 switch (opt) {
84                 default:
85                         fprintf(stderr, "Invalid option %s: %s\n", 
86                                 poptBadOption(pc, 0), poptStrerror(opt));
87                         exit(1);
88                 }
89         }
90
91         /* setup the remaining options for the main program to use */
92         extra_argv = poptGetArgs(pc);
93         if (extra_argv) {
94                 extra_argv++;
95                 while (extra_argv[extra_argc]) extra_argc++;
96         }
97
98         ev = event_context_init(NULL);
99
100         ctdb = ctdb_cmdline_client(ev);
101
102         /* attach to a specific database */
103         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
104         if (!ctdb_db) {
105                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
106                 exit(1);
107         }
108
109         printf("Waiting for cluster\n");
110         while (1) {
111                 uint32_t recmode=1;
112                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
113                 if (recmode == 0) break;
114                 event_loop_once(ev);
115         }
116
117         fetch_lock_once(ctdb, ev);
118
119         return 0;
120 }