ctdb-tests: Replace ctdb_fetch_readonly_once with fetch_readonly using new client API
authorAmitay Isaacs <amitay@gmail.com>
Wed, 20 Apr 2016 04:49:47 +0000 (14:49 +1000)
committerMartin Schwenke <martins@samba.org>
Fri, 22 Jul 2016 04:50:21 +0000 (06:50 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/tests/simple/75_readonly_records_basic.sh
ctdb/tests/src/ctdb_fetch_readonly_once.c [deleted file]
ctdb/tests/src/fetch_readonly.c [new file with mode: 0644]
ctdb/wscript

index e4d570816092f5f16e3b2e191ca25052178e8a07..05deaf0c1946032e810b724c0da2fa9d9b0bda36 100755 (executable)
@@ -128,7 +128,8 @@ try_command_on_node all $CTDB_TEST_WRAPPER ctdb_update_record
 ######################################################################
 
 echo "Try some readonly fetches, these should all be upgraded to full fetchlocks..."
-try_command_on_node all $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
+try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
+       -D ${testdb} -k testkey
 
 check_no_readonly
 
@@ -155,7 +156,8 @@ echo "Create 1 read-only delegation ..."
 try_command_on_node 1 $CTDB_TEST_WRAPPER ctdb_update_record
 
 # Fetch read-only to node 0
-try_command_on_node 0 $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
+try_command_on_node 0 $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
+       -D ${testdb} -k testkey
 
 check_readonly 1 0
 
@@ -177,8 +179,8 @@ others=""
 for n in $all_nodes ; do
     if [ "$n" != "$dmaster" ] ; then
        # Fetch read-only copy to this node
-       try_command_on_node $n \
-           $CTDB_TEST_WRAPPER "ctdb_fetch_readonly_once </dev/null"
+       try_command_on_node $n $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
+               -D ${testdb} -k testkey
        others="${others} ${n}"
     fi
 done
diff --git a/ctdb/tests/src/ctdb_fetch_readonly_once.c b/ctdb/tests/src/ctdb_fetch_readonly_once.c
deleted file mode 100644 (file)
index 2119273..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-/* 
-   simple ctdb test tool
-   This test just fetch_locks a record and releases it once.
-
-   Copyright (C) Ronnie Sahlberg 2009
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-   
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-   
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "replace.h"
-#include "system/filesys.h"
-#include "system/network.h"
-
-#include <popt.h>
-#include <talloc.h>
-#include <tevent.h>
-#include <tdb.h>
-
-#include "lib/util/time.h"
-
-#include "ctdb_private.h"
-#include "ctdb_client.h"
-
-#include "common/common.h"
-#include "common/cmdline.h"
-
-const char *TESTKEY = "testkey";
-
-/*
-       Just try locking/unlocking a single record once
-*/
-static void fetch_readonly_once(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key)
-{
-       TDB_DATA data;
-       struct ctdb_record_handle *h;
-
-       printf("Trying to readonly fetch lock the record ...\n");
-
-       h = ctdb_fetch_readonly_lock(ctdb_db, ctdb, key, &data, 1);
-       if (h == NULL) {
-               fprintf(stderr, "Failed to get readonly lock\n");
-               exit(1);
-       }
-
-       talloc_free(h);
-       printf("Record released.\n");
-}
-
-/*
-  main program
-*/
-int main(int argc, const char *argv[])
-{
-       struct ctdb_context *ctdb;
-       struct ctdb_db_context *ctdb_db;
-       struct tevent_context *ev;
-
-       TDB_DATA key;
-
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               { "record",      'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
-               POPT_TABLEEND
-       };
-       int opt;
-       const char **extra_argv;
-       int extra_argc = 0;
-       poptContext pc;
-
-       pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
-
-       while ((opt = poptGetNextOpt(pc)) != -1) {
-               switch (opt) {
-               default:
-                       fprintf(stderr, "Invalid option %s: %s\n", 
-                               poptBadOption(pc, 0), poptStrerror(opt));
-                       exit(1);
-               }
-       }
-
-       /* setup the remaining options for the main program to use */
-       extra_argv = poptGetArgs(pc);
-       if (extra_argv) {
-               extra_argv++;
-               while (extra_argv[extra_argc]) extra_argc++;
-       }
-
-       ev = tevent_context_init(NULL);
-
-       ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
-       if (ctdb == NULL) {
-               printf("failed to connect to ctdb daemon.\n");
-               exit(1);
-       }
-
-       key.dptr  = discard_const(TESTKEY);
-       key.dsize = strlen(TESTKEY);
-
-       /* attach to a specific database */
-       ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(3, 0), "test.tdb",
-                             false, 0);
-       if (!ctdb_db) {
-               fprintf(stderr, "ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
-               exit(10);
-       }
-
-       printf("Waiting for cluster\n");
-       while (1) {
-               uint32_t recmode=1;
-               ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
-               if (recmode == 0) break;
-               tevent_loop_once(ev);
-       }
-
-       fetch_readonly_once(ctdb, ctdb_db, key);
-
-       return 0;
-}
diff --git a/ctdb/tests/src/fetch_readonly.c b/ctdb/tests/src/fetch_readonly.c
new file mode 100644 (file)
index 0000000..5d2972a
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+   Fetch a single record using readonly
+
+   Copyright (C) Amitay Isaacs  2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/network.h"
+
+#include "lib/util/tevent_unix.h"
+
+#include "client/client.h"
+#include "tests/src/test_options.h"
+#include "tests/src/cluster_wait.h"
+
+
+struct fetch_readonly_state {
+       struct tevent_context *ev;
+};
+
+static void fetch_readonly_done(struct tevent_req *subreq);
+
+static struct tevent_req *fetch_readonly_send(TALLOC_CTX *mem_ctx,
+                                         struct tevent_context *ev,
+                                         struct ctdb_client_context *client,
+                                         struct ctdb_db_context *db,
+                                         const char *keystr,
+                                         int timelimit)
+{
+       struct tevent_req *req, *subreq;
+       struct fetch_readonly_state *state;
+       TDB_DATA key;
+
+       req = tevent_req_create(mem_ctx, &state, struct fetch_readonly_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->ev = ev;
+
+       key.dptr = (uint8_t *)discard_const(keystr);
+       key.dsize = strlen(keystr);
+
+       subreq = ctdb_fetch_lock_send(state, ev, client, db, key, true);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, fetch_readonly_done, req);
+
+       return req;
+}
+
+static void fetch_readonly_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fetch_readonly_state *state = tevent_req_data(
+               req, struct fetch_readonly_state);
+       struct ctdb_record_handle *h;
+       int ret;
+
+       h = ctdb_fetch_lock_recv(subreq, NULL, state, NULL, &ret);
+       TALLOC_FREE(subreq);
+       if (h == NULL) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       talloc_free(h);
+       tevent_req_done(req);
+}
+
+static bool fetch_readonly_recv(struct tevent_req *req, int *perr)
+{
+       int err;
+
+       if (tevent_req_is_unix_error(req, &err)) {
+               if (perr != NULL) {
+                       *perr = err;
+               }
+               return false;
+       }
+       return true;
+}
+
+int main(int argc, const char *argv[])
+{
+       const struct test_options *opts;
+       TALLOC_CTX *mem_ctx;
+       struct tevent_context *ev;
+       struct ctdb_client_context *client;
+       struct ctdb_db_context *ctdb_db;
+       struct tevent_req *req;
+       int ret;
+       bool status;
+
+       status = process_options_database(argc, argv, &opts);
+       if (! status) {
+               exit(1);
+       }
+
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               exit(1);
+       }
+
+       ev = tevent_context_init(mem_ctx);
+       if (ev == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               exit(1);
+       }
+
+       ret = ctdb_client_init(mem_ctx, ev, opts->socket, &client);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to initialize client, %s\n",
+                       strerror(ret));
+               exit(1);
+       }
+
+       if (! ctdb_recovery_wait(ev, client)) {
+               fprintf(stderr, "Memory allocation error\n");
+               exit(1);
+       }
+
+       ret = ctdb_attach(ev, client, tevent_timeval_zero(), opts->dbname, 0,
+                         &ctdb_db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", opts->dbname);
+               exit(1);
+       }
+
+       req = fetch_readonly_send(mem_ctx, ev, client, ctdb_db,
+                                 opts->keystr, opts->timelimit);
+       if (req == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               exit(1);
+       }
+
+       tevent_req_poll(req, ev);
+
+       status = fetch_readonly_recv(req, &ret);
+       if (! status) {
+               fprintf(stderr, "fetch readonly loop test failed\n");
+               exit(1);
+       }
+
+       talloc_free(mem_ctx);
+       return 0;
+}
index 2d78fb273be8b454a8116400a8e62c8b528d6dbe..2e0dac2e3a74ec45846bc20f195d8bf03f0f8116 100755 (executable)
@@ -677,7 +677,6 @@ def build(bld):
     # Test binaries
     ctdb_tests = [
         'rb_test',
-        'ctdb_fetch_readonly_once',
         'ctdb_fetch_readonly_loop',
         'ctdb_trackingdb_test',
         'ctdb_update_record',
@@ -710,7 +709,8 @@ def build(bld):
         'g_lock_loop',
         'message_ring',
         'fetch_ring',
-        'fetch_loop'
+        'fetch_loop',
+        'fetch_readonly'
     ]
 
     for target in ctdb_tests: