3 This test just fetch_locks a record and releases it once.
5 Copyright (C) Ronnie Sahlberg 2009
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.
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.
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/>.
22 #include "system/filesys.h"
27 #define TESTKEY "testkey"
29 static void rrl_cb(struct ctdb_db
*ctdb_db
,
30 struct ctdb_lock
*lock
, TDB_DATA outdata
, void *private)
32 bool *rrl_cb_called
= private;
34 printf("Record fetchlocked.\n");
35 printf("Press enter to release the record ...\n");
37 printf("Record released.\n");
39 *rrl_cb_called
= true;
44 Just try locking/unlocking a single record once
46 static void fetch_lock_once(struct ctdb_connection
*ctdb
, struct ctdb_db
*ctdb_db
)
49 bool rrl_cb_finished
= false;
51 key
.dptr
= discard_const(TESTKEY
);
52 key
.dsize
= strlen(TESTKEY
);
54 printf("Trying to fetch lock the record ...\n");
56 /* In the non-contended case the callback might be invoked
57 * immediately, before ctdb_readrecordlock_async() returns.
58 * In the contended case the callback will be invoked later.
60 * Normally an application would not care whether the callback
61 * has already been invoked here or not, but if the application
62 * needs to know, it can use the *private_data pointer
63 * to pass data through to the callback and back.
65 if (!ctdb_readrecordlock_async(ctdb_db
, key
,
66 rrl_cb
, &rrl_cb_finished
)) {
67 printf("Failed to send READRECORDLOCK\n");
70 while (!rrl_cb_finished
) {
73 pfd
.fd
= ctdb_get_fd(ctdb
);
74 pfd
.events
= ctdb_which_events(ctdb
);
75 if (poll(&pfd
, 1, -1) < 0) {
76 printf("Poll failed");
79 if (ctdb_service(ctdb
, pfd
.revents
) < 0) {
80 printf("Failed to service\n");
89 int main(int argc
, const char *argv
[])
91 struct ctdb_connection
*ctdb
;
92 struct ctdb_db
*ctdb_db
;
94 struct poptOption popt_options
[] = {
99 const char **extra_argv
;
103 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
105 while ((opt
= poptGetNextOpt(pc
)) != -1) {
108 fprintf(stderr
, "Invalid option %s: %s\n",
109 poptBadOption(pc
, 0), poptStrerror(opt
));
114 /* setup the remaining options for the main program to use */
115 extra_argv
= poptGetArgs(pc
);
118 while (extra_argv
[extra_argc
]) extra_argc
++;
121 ctdb
= ctdb_connect("/tmp/ctdb.socket",
122 ctdb_log_file
, stderr
);
124 printf("Connecting to /tmp/ctdb.socket\n");
128 /* attach to a specific database */
129 ctdb_db
= ctdb_attachdb(ctdb
, "test.tdb", false, 0);
131 printf("ctdb_attachdb failed\n");
135 printf("Waiting for cluster\n");
138 ctdb_getrecmode(ctdb
, CTDB_CURRENT_NODE
, &recmode
);
139 if (recmode
== 0) break;
143 fetch_lock_once(ctdb
, ctdb_db
);