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"
28 #define TESTKEY "testkey"
30 static void rrl_cb(struct ctdb_db *ctdb_db,
31 struct ctdb_lock *lock, TDB_DATA outdata, void *private)
33 bool *rrl_cb_called = private;
35 printf("Record fetchlocked.\n");
36 printf("Press enter to release the record ...\n");
38 printf("Record released.\n");
40 *rrl_cb_called = true;
45 Just try locking/unlocking a single record once
47 static void fetch_lock_once(struct ctdb_connection *ctdb, struct ctdb_db *ctdb_db)
50 bool rrl_cb_finished = false;
52 key.dptr = discard_const(TESTKEY);
53 key.dsize = strlen(TESTKEY);
55 printf("Trying to fetch lock the record ...\n");
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.
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.
66 if (!ctdb_readrecordlock_async(ctdb_db, key,
67 rrl_cb, &rrl_cb_finished)) {
68 printf("Failed to send READRECORDLOCK\n");
71 while (!rrl_cb_finished) {
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");
80 if (ctdb_service(ctdb, pfd.revents) < 0) {
81 err(1, "Failed to service");
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 err(1, "Connecting to /tmp/ctdb.socket");
126 /* attach to a specific database */
127 ctdb_db = ctdb_attachdb(ctdb, "test.tdb", false, 0);
129 printf("ctdb_attachdb failed\n");
133 printf("Waiting for cluster\n");
136 ctdb_getrecmode(ctdb, CTDB_CURRENT_NODE, &recmode);
137 if (recmode == 0) break;
141 fetch_lock_once(ctdb, ctdb_db);