2 Fetch a single record using readonly
4 Copyright (C) Amitay Isaacs 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/tevent_unix.h"
26 #include "client/client.h"
27 #include "tests/src/test_options.h"
28 #include "tests/src/cluster_wait.h"
31 struct fetch_readonly_state
{
32 struct tevent_context
*ev
;
35 static void fetch_readonly_done(struct tevent_req
*subreq
);
37 static struct tevent_req
*fetch_readonly_send(TALLOC_CTX
*mem_ctx
,
38 struct tevent_context
*ev
,
39 struct ctdb_client_context
*client
,
40 struct ctdb_db_context
*db
,
44 struct tevent_req
*req
, *subreq
;
45 struct fetch_readonly_state
*state
;
48 req
= tevent_req_create(mem_ctx
, &state
, struct fetch_readonly_state
);
55 key
.dptr
= (uint8_t *)discard_const(keystr
);
56 key
.dsize
= strlen(keystr
);
58 subreq
= ctdb_fetch_lock_send(state
, ev
, client
, db
, key
, true);
59 if (tevent_req_nomem(subreq
, req
)) {
60 return tevent_req_post(req
, ev
);
62 tevent_req_set_callback(subreq
, fetch_readonly_done
, req
);
67 static void fetch_readonly_done(struct tevent_req
*subreq
)
69 struct tevent_req
*req
= tevent_req_callback_data(
70 subreq
, struct tevent_req
);
71 struct fetch_readonly_state
*state
= tevent_req_data(
72 req
, struct fetch_readonly_state
);
73 struct ctdb_record_handle
*h
;
76 h
= ctdb_fetch_lock_recv(subreq
, NULL
, state
, NULL
, &ret
);
79 tevent_req_error(req
, ret
);
87 static bool fetch_readonly_recv(struct tevent_req
*req
, int *perr
)
91 if (tevent_req_is_unix_error(req
, &err
)) {
100 int main(int argc
, const char *argv
[])
102 const struct test_options
*opts
;
104 struct tevent_context
*ev
;
105 struct ctdb_client_context
*client
;
106 struct ctdb_db_context
*ctdb_db
;
107 struct tevent_req
*req
;
111 setup_logging("fetch_readonly", DEBUG_STDERR
);
113 status
= process_options_database(argc
, argv
, &opts
);
118 mem_ctx
= talloc_new(NULL
);
119 if (mem_ctx
== NULL
) {
120 fprintf(stderr
, "Memory allocation error\n");
124 ev
= tevent_context_init(mem_ctx
);
126 fprintf(stderr
, "Memory allocation error\n");
130 ret
= ctdb_client_init(mem_ctx
, ev
, opts
->socket
, &client
);
132 fprintf(stderr
, "Failed to initialize client, %s\n",
137 if (! ctdb_recovery_wait(ev
, client
)) {
138 fprintf(stderr
, "Memory allocation error\n");
142 ret
= ctdb_attach(ev
, client
, tevent_timeval_zero(), opts
->dbname
, 0,
145 fprintf(stderr
, "Failed to attach to DB %s\n", opts
->dbname
);
149 req
= fetch_readonly_send(mem_ctx
, ev
, client
, ctdb_db
,
150 opts
->keystr
, opts
->timelimit
);
152 fprintf(stderr
, "Memory allocation error\n");
156 tevent_req_poll(req
, ev
);
158 status
= fetch_readonly_recv(req
, &ret
);
160 fprintf(stderr
, "fetch readonly loop test failed\n");
164 talloc_free(mem_ctx
);