662f4704d4465ca604efc718a5f89eab2706ec0b
[sahlberg/ctdb.git] / tests / src / ctdb_fetch_readonly_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 #include "ctdb_private.h"
27
28 static struct ctdb_db_context *ctdb_db;
29
30 char *TESTKEY = "testkey";
31
32
33 /*
34         Just try locking/unlocking a single record once
35 */
36 static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev)
37 {
38         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
39         TDB_DATA key, data;
40         struct ctdb_record_handle *h;
41
42         key.dptr = discard_const(TESTKEY);
43         key.dsize = strlen(TESTKEY);
44
45         printf("Trying to fetch lock the record ...\n");
46
47         h = ctdb_fetch_readonly_lock(ctdb_db, tmp_ctx, key, &data, true);
48         if (h == NULL) {
49                 printf("Failed to fetch record '%s' on node %d\n", 
50                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
51                 talloc_free(tmp_ctx);
52                 exit(10);
53         }
54
55         printf("Record fetchlocked.\n");
56         printf("Press enter to release the record ...\n");
57         (void)getchar();
58
59         talloc_free(tmp_ctx);
60         printf("Record released.\n");
61 }
62
63 /*
64   main program
65 */
66 int main(int argc, const char *argv[])
67 {
68         struct ctdb_context *ctdb;
69         TDB_DATA key;
70
71         struct poptOption popt_options[] = {
72                 POPT_AUTOHELP
73                 POPT_CTDB_CMDLINE
74                 { "record",      'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
75                 POPT_TABLEEND
76         };
77         int opt, ret;
78         const char **extra_argv;
79         int extra_argc = 0;
80         poptContext pc;
81         struct event_context *ev;
82
83         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
84
85         while ((opt = poptGetNextOpt(pc)) != -1) {
86                 switch (opt) {
87                 default:
88                         fprintf(stderr, "Invalid option %s: %s\n", 
89                                 poptBadOption(pc, 0), poptStrerror(opt));
90                         exit(1);
91                 }
92         }
93
94         /* setup the remaining options for the main program to use */
95         extra_argv = poptGetArgs(pc);
96         if (extra_argv) {
97                 extra_argv++;
98                 while (extra_argv[extra_argc]) extra_argc++;
99         }
100
101         ev = event_context_init(NULL);
102
103         ctdb = ctdb_cmdline_client(ev);
104
105         key.dptr  = TESTKEY;
106         key.dsize = strlen(TESTKEY);
107
108         ret = ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
109         if (ret != 0) {
110                 printf("failed to get vnnmap\n");
111                 exit(10);
112         }
113         printf("Record:%s\n", TESTKEY);
114         printf("Lmaster : %d\n", ctdb_lmaster(ctdb, &key)); 
115
116         /* attach to a specific database */
117         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
118         if (!ctdb_db) {
119                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
120                 exit(1);
121         }
122
123         printf("Waiting for cluster\n");
124         while (1) {
125                 uint32_t recmode=1;
126                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
127                 if (recmode == 0) break;
128                 event_loop_once(ev);
129         }
130
131         fetch_lock_once(ctdb, ev);
132
133         return 0;
134 }