ctdb-tests: Replace ctdb_fetch_one with fetch_loop using new client API
authorAmitay Isaacs <amitay@gmail.com>
Fri, 20 Nov 2015 05:24:34 +0000 (16:24 +1100)
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/src/ctdb_fetch_one.c [deleted file]
ctdb/tests/src/fetch_loop.c [new file with mode: 0644]
ctdb/wscript

diff --git a/ctdb/tests/src/ctdb_fetch_one.c b/ctdb/tests/src/ctdb_fetch_one.c
deleted file mode 100644 (file)
index e24684f..0000000
+++ /dev/null
@@ -1,155 +0,0 @@
-/* 
-   simple ctdb benchmark
-   This test just fetch_locks a record and releases it in a loop.
-
-   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/cmdline.h"
-#include "common/common.h"
-
-static int timelimit = 10;
-static int lock_count = 0;
-
-static struct ctdb_db_context *ctdb_db;
-
-#define TESTKEY "testkey"
-
-
-static void alarm_handler(int sig)
-{
-       printf("Locks:%d\n", lock_count);
-       lock_count=0;
-
-       timelimit--;
-       if (timelimit <= 0) {
-               exit(0);
-       }
-       alarm(1);
-}
-
-/*
-       Just try locking/unlocking the same record over and over
-*/
-static void bench_fetch_one_loop(struct ctdb_context *ctdb,
-                                struct tevent_context *ev)
-{
-       TDB_DATA key, data;
-
-       key.dptr = discard_const(TESTKEY);
-       key.dsize = strlen(TESTKEY);
-
-
-       while (1) {
-               TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-               struct ctdb_record_handle *h;
-
-               h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
-               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);
-                       continue;
-               }
-
-               talloc_free(tmp_ctx);
-               lock_count++;
-       }
-}
-
-/*
-  main program
-*/
-int main(int argc, const char *argv[])
-{
-       struct ctdb_context *ctdb;
-
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               POPT_CTDB_CMDLINE
-               { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
-               POPT_TABLEEND
-       };
-       int opt;
-       const char **extra_argv;
-       int extra_argc = 0;
-       poptContext pc;
-       struct tevent_context *ev;
-
-       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);
-       }
-
-       /* attach to a specific database */
-       ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
-                             false, 0);
-       if (!ctdb_db) {
-               printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
-               exit(1);
-       }
-
-       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);
-       }
-
-       signal(SIGALRM, alarm_handler);
-       alarm(1);
-
-       bench_fetch_one_loop(ctdb, ev);
-
-       return 0;
-}
diff --git a/ctdb/tests/src/fetch_loop.c b/ctdb/tests/src/fetch_loop.c
new file mode 100644 (file)
index 0000000..9cbcf17
--- /dev/null
@@ -0,0 +1,286 @@
+/*
+   simple ctdb benchmark
+
+   Copyright (C) Amitay Isaacs  2015
+
+   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"
+
+#define TESTDB "fetch_loop.tdb"
+#define TESTKEY        "testkey"
+
+struct fetch_loop_state {
+       struct tevent_context *ev;
+       struct ctdb_client_context *client;
+       struct ctdb_db_context *ctdb_db;
+       int num_nodes;
+       int timelimit;
+       TDB_DATA key;
+       int locks_count;
+};
+
+static void fetch_loop_start(struct tevent_req *subreq);
+static void fetch_loop_next(struct tevent_req *subreq);
+static void fetch_loop_each_second(struct tevent_req *subreq);
+static void fetch_loop_finish(struct tevent_req *subreq);
+
+static struct tevent_req *fetch_loop_send(TALLOC_CTX *mem_ctx,
+                                         struct tevent_context *ev,
+                                         struct ctdb_client_context *client,
+                                         struct ctdb_db_context *ctdb_db,
+                                         int num_nodes, int timelimit)
+{
+       struct tevent_req *req, *subreq;
+       struct fetch_loop_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct fetch_loop_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->ev = ev;
+       state->client = client;
+       state->ctdb_db = ctdb_db;
+       state->num_nodes = num_nodes;
+       state->timelimit = timelimit;
+       state->key.dptr = discard_const(TESTKEY);
+       state->key.dsize = strlen(TESTKEY);
+
+       subreq = cluster_wait_send(state, state->ev, state->client,
+                                  state->num_nodes);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, fetch_loop_start, req);
+
+       return req;
+}
+
+static void fetch_loop_start(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fetch_loop_state *state = tevent_req_data(
+               req, struct fetch_loop_state);
+       bool status;
+       int ret;
+
+       status = cluster_wait_recv(subreq, &ret);
+       TALLOC_FREE(subreq);
+       if (! status) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       subreq = ctdb_fetch_lock_send(state, state->ev, state->client,
+                                     state->ctdb_db, state->key, false);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, fetch_loop_next, req);
+
+       if (ctdb_client_pnn(state->client) == 0) {
+               subreq = tevent_wakeup_send(state, state->ev,
+                                           tevent_timeval_current_ofs(1, 0));
+               if (tevent_req_nomem(subreq, req)) {
+                       return;
+               }
+               tevent_req_set_callback(subreq, fetch_loop_each_second, req);
+       }
+
+       subreq = tevent_wakeup_send(state, state->ev,
+                                   tevent_timeval_current_ofs(
+                                           state->timelimit, 0));
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, fetch_loop_finish, req);
+}
+
+static void fetch_loop_next(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fetch_loop_state *state = tevent_req_data(
+               req, struct fetch_loop_state);
+       struct ctdb_record_handle *h;
+       TDB_DATA data;
+       int ret;
+
+       h = ctdb_fetch_lock_recv(subreq, NULL, state, &data, &ret);
+       TALLOC_FREE(subreq);
+       if (h == NULL) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       if (data.dsize == sizeof(uint32_t)) {
+               state->locks_count = *(uint32_t *)data.dptr;
+       }
+       TALLOC_FREE(data.dptr);
+
+       state->locks_count += 1;
+       data.dsize = sizeof(uint32_t);
+       data.dptr = (uint8_t *)&state->locks_count;
+
+       ret = ctdb_store_record(h, data);
+       if (ret != 0) {
+               talloc_free(h);
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       talloc_free(h);
+
+       subreq = ctdb_fetch_lock_send(state, state->ev, state->client,
+                                     state->ctdb_db, state->key, false);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, fetch_loop_next, req);
+}
+
+static void fetch_loop_each_second(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fetch_loop_state *state = tevent_req_data(
+               req, struct fetch_loop_state);
+       bool status;
+
+       status = tevent_wakeup_recv(subreq);
+       TALLOC_FREE(subreq);
+       if (! status) {
+               tevent_req_error(req, EIO);
+               return;
+       }
+
+       printf("Locks:%d\r", state->locks_count);
+       fflush(stdout);
+
+       subreq = tevent_wakeup_send(state, state->ev,
+                                   tevent_timeval_current_ofs(1, 0));
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, fetch_loop_each_second, req);
+}
+
+static void fetch_loop_finish(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct fetch_loop_state *state = tevent_req_data(
+               req, struct fetch_loop_state);
+       bool status;
+
+       status = tevent_wakeup_recv(subreq);
+       TALLOC_FREE(subreq);
+       if (! status) {
+               tevent_req_error(req, EIO);
+               return;
+       }
+
+       printf("Locks:%d\n", state->locks_count);
+
+       tevent_req_done(req);
+}
+
+static bool fetch_loop_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_basic(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(), TESTDB, 0,
+                         &ctdb_db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", TESTDB);
+               exit(1);
+       }
+
+       req = fetch_loop_send(mem_ctx, ev, client, ctdb_db,
+                              opts->num_nodes, opts->timelimit);
+       if (req == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               exit(1);
+       }
+
+       tevent_req_poll(req, ev);
+
+       status = fetch_loop_recv(req, &ret);
+       if (! status) {
+               fprintf(stderr, "fetch loop test failed\n");
+               exit(1);
+       }
+
+       talloc_free(mem_ctx);
+       return 0;
+}
index 79e1ca87598872f0edf01fa6597887f19878cd5d..2d78fb273be8b454a8116400a8e62c8b528d6dbe 100755 (executable)
@@ -677,7 +677,6 @@ def build(bld):
     # Test binaries
     ctdb_tests = [
         'rb_test',
-        'ctdb_fetch_one',
         'ctdb_fetch_readonly_once',
         'ctdb_fetch_readonly_loop',
         'ctdb_trackingdb_test',
@@ -710,7 +709,8 @@ def build(bld):
     ctdb_tests = [
         'g_lock_loop',
         'message_ring',
-        'fetch_ring'
+        'fetch_ring',
+        'fetch_loop'
     ]
 
     for target in ctdb_tests: