Merge remote branch 'ddiss/master_pmda_namespace'
[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 "system/filesys.h"
23 #include "popt.h"
24 #include <poll.h>
25 #include <err.h>
26 #include "ctdb.h"
27
28 #define TESTKEY "testkey"
29
30 static void rrl_cb(struct ctdb_db *ctdb_db,
31                    struct ctdb_lock *lock, TDB_DATA outdata, void *private)
32 {
33         bool *rrl_cb_called = private;
34
35         printf("Record fetchlocked.\n");
36         printf("Press enter to release the record ...\n");
37         (void)getchar();
38         printf("Record released.\n");
39
40         *rrl_cb_called = true;
41         return;
42 }
43
44 /*
45         Just try locking/unlocking a single record once
46 */
47 static void fetch_lock_once(struct ctdb_connection *ctdb, struct ctdb_db *ctdb_db)
48 {
49         TDB_DATA key;
50         bool rrl_cb_finished = false;
51
52         key.dptr = discard_const(TESTKEY);
53         key.dsize = strlen(TESTKEY);
54
55         printf("Trying to fetch lock the record ...\n");
56
57         /* In the non-contended case the callback might be invoked
58          * immediately, before ctdb_readrecordlock_async() returns.
59          * In the contended case the callback will be invoked later.
60          *
61          * Normally an application would not care whether the callback
62          * has already been invoked here or not, but if the application
63          * needs to know, it can use the *private_data pointer
64          * to pass data through to the callback and back.
65          */
66         if (!ctdb_readrecordlock_async(ctdb_db, key,
67                                        rrl_cb, &rrl_cb_finished)) {
68                 printf("Failed to send READRECORDLOCK\n");
69                 exit(10);
70         }
71         while (!rrl_cb_finished) {
72                 struct pollfd pfd;
73
74                 pfd.fd = ctdb_get_fd(ctdb);
75                 pfd.events = ctdb_which_events(ctdb);
76                 if (poll(&pfd, 1, -1) < 0) {
77                         printf("Poll failed");
78                         exit(10);
79                 }
80                 if (ctdb_service(ctdb, pfd.revents) < 0) {
81                         err(1, "Failed to service");
82                 }
83         }
84 }
85
86 /*
87   main program
88 */
89 int main(int argc, const char *argv[])
90 {
91         struct ctdb_connection *ctdb;
92         struct ctdb_db *ctdb_db;
93
94         struct poptOption popt_options[] = {
95                 POPT_AUTOHELP
96                 POPT_TABLEEND
97         };
98         int opt;
99         const char **extra_argv;
100         int extra_argc = 0;
101         poptContext pc;
102
103         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
104
105         while ((opt = poptGetNextOpt(pc)) != -1) {
106                 switch (opt) {
107                 default:
108                         fprintf(stderr, "Invalid option %s: %s\n", 
109                                 poptBadOption(pc, 0), poptStrerror(opt));
110                         exit(1);
111                 }
112         }
113
114         /* setup the remaining options for the main program to use */
115         extra_argv = poptGetArgs(pc);
116         if (extra_argv) {
117                 extra_argv++;
118                 while (extra_argv[extra_argc]) extra_argc++;
119         }
120
121         ctdb = ctdb_connect("/tmp/ctdb.socket",
122                                        ctdb_log_file, stderr);
123         if (!ctdb)
124                 err(1, "Connecting to /tmp/ctdb.socket");
125
126         /* attach to a specific database */
127         ctdb_db = ctdb_attachdb(ctdb, "test.tdb", false, 0);
128         if (!ctdb_db) {
129                 printf("ctdb_attachdb failed\n");
130                 exit(1);
131         }
132
133         printf("Waiting for cluster\n");
134         while (1) {
135                 uint32_t recmode=1;
136                 ctdb_getrecmode(ctdb, CTDB_CURRENT_NODE, &recmode);
137                 if (recmode == 0) break;
138                 sleep(1);
139         }
140
141         fetch_lock_once(ctdb, ctdb_db);
142
143         return 0;
144 }