summaryrefslogtreecommitdiff
path: root/ctdb/tests/src/fetch_readonly.c
blob: ff126bdf5ae55c152bb4320d78fc09f1c5cf3d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
   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/debug.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;

	setup_logging("fetch_readonly", DEBUG_STDERR);

	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;
}