ctdb-tests: Replace ctdb_update_record with update_record using new client API
authorAmitay Isaacs <amitay@gmail.com>
Wed, 20 Apr 2016 04:27:40 +0000 (14:27 +1000)
committerMartin Schwenke <martins@samba.org>
Fri, 22 Jul 2016 04:50:22 +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_update_record.c [deleted file]
ctdb/tests/src/update_record.c [new file with mode: 0644]
ctdb/wscript

index 05deaf0c1946032e810b724c0da2fa9d9b0bda36..48e16dfed8c9c82683504b096e589a4663263d80 100755 (executable)
@@ -123,7 +123,8 @@ echo "Create test database \"${testdb}\""
 try_command_on_node 0 $CTDB attach $testdb
 
 echo "Create some records..."
-try_command_on_node all $CTDB_TEST_WRAPPER ctdb_update_record
+try_command_on_node all $CTDB_TEST_WRAPPER $VALGRIND update_record \
+       -D ${testdb} -k testkey
 
 ######################################################################
 
@@ -153,7 +154,8 @@ fi
 
 echo "Create 1 read-only delegation ..."
 # dmaster=1
-try_command_on_node 1 $CTDB_TEST_WRAPPER ctdb_update_record
+try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
+       -D ${testdb} -k testkey
 
 # Fetch read-only to node 0
 try_command_on_node 0 $CTDB_TEST_WRAPPER $VALGRIND fetch_readonly \
@@ -165,7 +167,8 @@ check_readonly 1 0
 
 echo "Verify that a fetchlock revokes read-only delegations..."
 # Node 1 becomes dmaster
-try_command_on_node 1 $CTDB_TEST_WRAPPER ctdb_update_record
+try_command_on_node 1 $CTDB_TEST_WRAPPER $VALGRIND update_record \
+       -D ${testdb} -k testkey
 
 check_no_readonly
 
@@ -173,7 +176,8 @@ check_no_readonly
 
 echo "Create more read-only delegations..."
 dmaster=1
-try_command_on_node $dmaster $CTDB_TEST_WRAPPER ctdb_update_record
+try_command_on_node $dmaster $CTDB_TEST_WRAPPER $VALGRIND update_record \
+       -D ${testdb} -k testkey
 
 others=""
 for n in $all_nodes ; do
diff --git a/ctdb/tests/src/ctdb_update_record.c b/ctdb/tests/src/ctdb_update_record.c
deleted file mode 100644 (file)
index 7b5b544..0000000
+++ /dev/null
@@ -1,171 +0,0 @@
-/* 
-   simple ctdb test tool
-   This test just fetch_locks a record bumps the RSN and then writes new content
-
-   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 "lib/util/time.h"
-
-#include "ctdb_private.h"
-#include "ctdb_client.h"
-
-#include "common/cmdline.h"
-#include "common/common.h"
-
-static struct ctdb_db_context *ctdb_db;
-
-#define TESTKEY "testkey"
-
-
-/*
-       Just try locking/unlocking a single record once
-*/
-static void fetch_lock_once(struct ctdb_context *ctdb,
-                           struct tevent_context *ev, uint32_t generation)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA key, data;
-       struct ctdb_record_handle *h;
-       struct ctdb_ltdb_header *header;
-       int ret;
-
-       key.dptr = discard_const(TESTKEY);
-       key.dsize = strlen(TESTKEY);
-
-       printf("Trying to fetch lock the record ...\n");
-
-       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);
-               exit(10);
-       }
-
-       printf("Record fetchlocked.\n");
-       header = talloc_memdup(tmp_ctx, ctdb_header_from_record_handle(h), sizeof(*header));
-               printf("RSN:%d\n", (int)header->rsn);
-       talloc_free(h);
-       printf("Record released.\n");
-
-       printf("Write new record with RSN+10\n");
-       header->rsn += 10;
-       data.dptr = (void *)talloc_asprintf(tmp_ctx, "%d", (int)header->rsn);
-       data.dsize = strlen((char *)data.dptr);
-
-       ret = ctdb_ctrl_updaterecord(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb_db, key, header, data);
-       if (ret != 0) {
-               printf("Failed to writerecord,  ret==%d\n", ret);       
-               exit(1);
-       }
-
-       printf("re-fetch the record\n");
-       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);
-               exit(10);
-       }
-
-       printf("Record fetchlocked.\n");
-       header = talloc_memdup(tmp_ctx, ctdb_header_from_record_handle(h), sizeof(*header));
-               printf("RSN:%d\n", (int)header->rsn);
-       talloc_free(h);
-       printf("Record released.\n");
-
-       talloc_free(tmp_ctx);
-}
-
-/*
-  main program
-*/
-int main(int argc, const char *argv[])
-{
-       struct ctdb_context *ctdb;
-
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               POPT_CTDB_CMDLINE
-               POPT_TABLEEND
-       };
-       int opt;
-       const char **extra_argv;
-       int extra_argc = 0;
-       poptContext pc;
-       struct tevent_context *ev;
-       struct ctdb_vnn_map *vnnmap=NULL;
-
-       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(5, 0));
-       if (ctdb == NULL) {
-               exit(1);
-       }
-
-       /* attach to a specific database */
-       ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(5, 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);
-       }
-
-
-       if (ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &vnnmap) != 0) {
-               printf("Unable to get vnnmap from local node\n");
-               exit(1);
-       }
-       printf("Current Generation %d\n", (int)vnnmap->generation);
-
-       fetch_lock_once(ctdb, ev, vnnmap->generation);
-
-       return 0;
-}
diff --git a/ctdb/tests/src/update_record.c b/ctdb/tests/src/update_record.c
new file mode 100644 (file)
index 0000000..78291d7
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+   Update a record and increase it's RSN
+
+   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 "protocol/protocol_api.h"
+#include "client/client.h"
+#include "tests/src/test_options.h"
+#include "tests/src/cluster_wait.h"
+
+struct update_record_state {
+       struct tevent_context *ev;
+       struct ctdb_client_context *client;
+       struct ctdb_db_context *db;
+       int timelimit;
+       TDB_DATA key;
+};
+
+static void update_record_fetch_done(struct tevent_req *subreq);
+static void update_record_update_done(struct tevent_req *subreq);
+
+static struct tevent_req *update_record_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 update_record_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct update_record_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->ev = ev;
+       state->client = client;
+       state->db = db;
+       state->timelimit = timelimit;
+       state->key.dptr = (uint8_t *)discard_const(keystr);
+       state->key.dsize = strlen(keystr);
+
+       subreq = ctdb_fetch_lock_send(state, state->ev, state->client,
+                                     state->db, state->key, false);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, update_record_fetch_done, req);
+
+       return req;
+}
+
+static void update_record_fetch_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct update_record_state *state = tevent_req_data(
+               req, struct update_record_state);
+       struct ctdb_record_handle *h;
+       struct ctdb_ltdb_header header;
+       struct ctdb_rec_buffer *recbuf;
+       struct ctdb_req_control request;
+       TDB_DATA data;
+       int ret;
+
+       h = ctdb_fetch_lock_recv(subreq, &header, NULL, NULL, &ret);
+       TALLOC_FREE(subreq);
+       if (h == NULL) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       talloc_free(h);
+
+       header.rsn += 10;
+
+       recbuf = ctdb_rec_buffer_init(state, ctdb_db_id(state->db));
+       if (tevent_req_nomem(recbuf, req)) {
+               return;
+       }
+
+       data.dptr = (uint8_t *)talloc_asprintf(recbuf, "%"PRIu64, header.rsn);
+       if (tevent_req_nomem(data.dptr, req)) {
+               return;
+       }
+       data.dsize = strlen((char *)data.dptr);
+
+       ret = ctdb_rec_buffer_add(state, recbuf, 0, &header, state->key, data);
+       if (ret != 0) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       ctdb_req_control_update_record(&request, recbuf);
+       subreq = ctdb_client_control_send(state, state->ev, state->client,
+                                         CTDB_CURRENT_NODE,
+                                         tevent_timeval_current_ofs(
+                                                 state->timelimit, 0),
+                                         &request);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, update_record_update_done, req);
+
+       talloc_free(recbuf);
+}
+
+static void update_record_update_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct update_record_state *state = tevent_req_data(
+               req, struct update_record_state);
+       struct ctdb_reply_control *reply;
+       int ret;
+       bool status;
+
+       status = ctdb_client_control_recv(subreq, &ret, state, &reply);
+       TALLOC_FREE(subreq);
+       if (! status) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       ret = ctdb_reply_control_update_record(reply);
+       if (ret != 0) {
+               tevent_req_error(req, ret);
+               return;
+       }
+
+       talloc_free(reply);
+
+       tevent_req_done(req);
+}
+
+static bool update_record_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), %s\n",
+                       opts->socket, 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 DB %s\n", opts->dbname);
+               exit(1);
+       }
+
+       req = update_record_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 = update_record_recv(req, &ret);
+       if (! status) {
+               fprintf(stderr, "update record failed\n");
+               exit(1);
+       }
+
+       talloc_free(mem_ctx);
+       return 0;
+}
index 4e23ea230541dd1149f6f1e47256711283f6eb0e..208ae378f0b348b9f69a5473a5a22cf3ad2c7cdf 100755 (executable)
@@ -678,7 +678,6 @@ def build(bld):
     ctdb_tests = [
         'rb_test',
         'ctdb_trackingdb_test',
-        'ctdb_update_record',
         'ctdb_update_record_persistent',
         'ctdb_store',
         'ctdb_traverse',
@@ -710,7 +709,8 @@ def build(bld):
         'fetch_loop',
         'fetch_readonly',
         'fetch_readonly_loop',
-        'transaction_loop'
+        'transaction_loop',
+        'update_record'
     ]
 
     for target in ctdb_tests: