From 18b6969dd1760521f839e5a928f1c7e9b3c34d4e Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Fri, 28 Oct 2011 15:50:44 +1100 Subject: [PATCH] Tests: convert test tool for fetching a readonly lock to libctdb --- Makefile.in | 4 +- tests/src/ctdb_fetch_readonly_once.c | 90 +++++++++++++++------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/Makefile.in b/Makefile.in index 7ec430c1..1f712851 100755 --- a/Makefile.in +++ b/Makefile.in @@ -181,9 +181,9 @@ tests/bin/ctdb_fetch_lock_once: libctdb/libctdb.a tests/src/ctdb_fetch_lock_once @echo Linking $@ @$(CC) $(CFLAGS) -o $@ tests/src/ctdb_fetch_lock_once.o @TDB_OBJ@ @POPT_OBJ@ libctdb/libctdb.a $(LIB_FLAGS) -tests/bin/ctdb_fetch_readonly_once: $(CTDB_CLIENT_OBJ) tests/src/ctdb_fetch_readonly_once.o +tests/bin/ctdb_fetch_readonly_once: libctdb/libctdb.a tests/src/ctdb_fetch_readonly_once.o @echo Linking $@ - @$(CC) $(CFLAGS) -o $@ tests/src/ctdb_fetch_readonly_once.o $(CTDB_CLIENT_OBJ) $(LIB_FLAGS) + @$(CC) $(CFLAGS) -o $@ tests/src/ctdb_fetch_readonly_once.o @TDB_OBJ@ @POPT_OBJ@ libctdb/libctdb.a $(LIB_FLAGS) tests/bin/ctdb_fetch_readonly_loop: $(CTDB_CLIENT_OBJ) tests/src/ctdb_fetch_readonly_loop.o @echo Linking $@ diff --git a/tests/src/ctdb_fetch_readonly_once.c b/tests/src/ctdb_fetch_readonly_once.c index 40d071f2..4fa5f330 100644 --- a/tests/src/ctdb_fetch_readonly_once.c +++ b/tests/src/ctdb_fetch_readonly_once.c @@ -19,44 +19,56 @@ */ #include "includes.h" -#include "lib/tevent/tevent.h" #include "system/filesys.h" #include "popt.h" -#include "cmdline.h" -#include "ctdb_private.h" - -static struct ctdb_db_context *ctdb_db; +#include +#include "ctdb.h" const char *TESTKEY = "testkey"; +static void rorl_cb(struct ctdb_db *ctdb_db, + struct ctdb_lock *lock, TDB_DATA outdata, void *private) +{ + int *finished = private; + + printf("Record fetchlocked.\n"); + printf("Press enter to release the record ...\n"); + (void)getchar(); + + *finished = 1; +} /* Just try locking/unlocking a single record once */ -static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev) +static void fetch_readonly_once(struct ctdb_connection *ctdb, struct ctdb_db *ctdb_db, TDB_DATA key) { - TALLOC_CTX *tmp_ctx = talloc_new(ctdb); - TDB_DATA key, data; - struct ctdb_record_handle *h; - - key.dptr = discard_const(TESTKEY); - key.dsize = strlen(TESTKEY); + int finished; printf("Trying to fetch lock the record ...\n"); - h = ctdb_fetch_readonly_lock(ctdb_db, tmp_ctx, key, &data, true); - if (h == NULL) { - printf("Failed to fetch record '%s' on node %d\n", - (const char *)key.dptr, ctdb_get_pnn(ctdb)); - talloc_free(tmp_ctx); + finished = 0; + if (!ctdb_readonlyrecordlock_async(ctdb_db, key, + rorl_cb, &finished)) { + printf("Failed to send READONLYRECORDLOCK\n"); exit(10); } - printf("Record fetchlocked.\n"); - printf("Press enter to release the record ...\n"); - (void)getchar(); + while (!finished) { + struct pollfd pfd; + + pfd.fd = ctdb_get_fd(ctdb); + pfd.events = ctdb_which_events(ctdb); + if (poll(&pfd, 1, -1) < 0) { + fprintf(stderr, "Poll failed"); + exit(10); + } + if (ctdb_service(ctdb, pfd.revents) < 0) { + fprintf(stderr, "Failed to service"); + exit(10); + } + } - talloc_free(tmp_ctx); printf("Record released.\n"); } @@ -65,20 +77,20 @@ static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev) */ int main(int argc, const char *argv[]) { - struct ctdb_context *ctdb; + struct ctdb_connection *ctdb; + struct ctdb_db *ctdb_db; + TDB_DATA key; struct poptOption popt_options[] = { POPT_AUTOHELP - POPT_CTDB_CMDLINE { "record", 'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" }, POPT_TABLEEND }; - int opt, ret; + int opt; const char **extra_argv; int extra_argc = 0; poptContext pc; - struct event_context *ev; pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST); @@ -98,37 +110,33 @@ int main(int argc, const char *argv[]) while (extra_argv[extra_argc]) extra_argc++; } - ev = event_context_init(NULL); + ctdb = ctdb_connect("/tmp/ctdb.socket", + ctdb_log_file, stderr); - ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(5, 0)); + if (!ctdb) { + fprintf(stderr, "Connecting to /tmp/ctdb.socket"); + exit(10); + } key.dptr = discard_const(TESTKEY); key.dsize = strlen(TESTKEY); - ret = ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map); - if (ret != 0) { - printf("failed to get vnnmap\n"); - exit(10); - } - printf("Record:%s\n", TESTKEY); - printf("Lmaster : %d\n", ctdb_lmaster(ctdb, &key)); - /* attach to a specific database */ - ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(5, 0), "test.tdb", false, 0); + ctdb_db = ctdb_attachdb(ctdb, "test.tdb", false, 0); if (!ctdb_db) { - printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb)); - exit(1); + fprintf(stderr, "ctdb_attachdb failed\n"); + exit(10); } printf("Waiting for cluster\n"); while (1) { uint32_t recmode=1; - ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode); + ctdb_getrecmode(ctdb, CTDB_CURRENT_NODE, &recmode); if (recmode == 0) break; - event_loop_once(ev); + sleep(1); } - fetch_lock_once(ctdb, ev); + fetch_readonly_once(ctdb, ctdb_db, key); return 0; } -- 2.34.1