4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "system/wait.h"
26 #include "../include/ctdb_private.h"
28 struct ctdb_persistent_state
{
29 struct ctdb_context
*ctdb
;
30 struct ctdb_db_context
*ctdb_db
; /* used by trans3_commit */
31 struct ctdb_client
*client
; /* used by trans3_commit */
32 struct ctdb_req_control
*c
;
36 uint32_t num_failed
, num_sent
;
40 1) all nodes fail, and all nodes reply
41 2) some nodes fail, all nodes reply
47 called when a node has acknowledged a ctdb_control_update_record call
49 static void ctdb_persistent_callback(struct ctdb_context
*ctdb
,
50 int32_t status
, TDB_DATA data
,
54 struct ctdb_persistent_state
*state
= talloc_get_type(private_data
,
55 struct ctdb_persistent_state
);
57 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
58 DEBUG(DEBUG_INFO
, ("ctdb_persistent_callback: ignoring reply "
59 "during recovery\n"));
64 DEBUG(DEBUG_ERR
,("ctdb_persistent_callback failed with status %d (%s)\n",
65 status
, errormsg
?errormsg
:"no error message given"));
66 state
->status
= status
;
67 state
->errormsg
= errormsg
;
71 * If a node failed to complete the update_record control,
72 * then either a recovery is already running or something
73 * bad is going on. So trigger a recovery and let the
74 * recovery finish the transaction, sending back the reply
75 * for the trans3_commit control to the client.
77 ctdb
->recovery_mode
= CTDB_RECOVERY_ACTIVE
;
83 if (state
->num_pending
!= 0) {
87 ctdb_request_control_reply(state
->ctdb
, state
->c
, NULL
, 0, state
->errormsg
);
92 called if persistent store times out
94 static void ctdb_persistent_store_timeout(struct event_context
*ev
, struct timed_event
*te
,
95 struct timeval t
, void *private_data
)
97 struct ctdb_persistent_state
*state
= talloc_get_type(private_data
, struct ctdb_persistent_state
);
99 if (state
->ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
100 DEBUG(DEBUG_INFO
, ("ctdb_persistent_store_timeout: ignoring "
101 "timeout during recovery\n"));
105 ctdb_request_control_reply(state
->ctdb
, state
->c
, NULL
, 1,
106 "timeout in ctdb_persistent_state");
112 * Finish pending trans3 commit controls, i.e. send
113 * reply to the client. This is called by the end-recovery
114 * control to fix the situation when a recovery interrupts
115 * the usual progress of a transaction.
117 void ctdb_persistent_finish_trans3_commits(struct ctdb_context
*ctdb
)
119 struct ctdb_db_context
*ctdb_db
;
121 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
122 DEBUG(DEBUG_INFO
, ("ctdb_persistent_finish_trans3_commits: "
123 "skipping execution when recovery is "
128 for (ctdb_db
= ctdb
->db_list
; ctdb_db
; ctdb_db
= ctdb_db
->next
) {
129 struct ctdb_persistent_state
*state
;
131 if (ctdb_db
->persistent_state
== NULL
) {
135 state
= ctdb_db
->persistent_state
;
137 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 2,
138 "trans3 commit ended by recovery");
140 /* The destructor sets ctdb_db->persistent_state to NULL. */
145 static int ctdb_persistent_state_destructor(struct ctdb_persistent_state
*state
)
147 if (state
->client
!= NULL
) {
148 state
->client
->db_id
= 0;
151 if (state
->ctdb_db
!= NULL
) {
152 state
->ctdb_db
->persistent_state
= NULL
;
159 * Store a set of persistent records.
160 * This is used to roll out a transaction to all nodes.
162 int32_t ctdb_control_trans3_commit(struct ctdb_context
*ctdb
,
163 struct ctdb_req_control
*c
,
164 TDB_DATA recdata
, bool *async_reply
)
166 struct ctdb_client
*client
;
167 struct ctdb_persistent_state
*state
;
169 struct ctdb_marshall_buffer
*m
= (struct ctdb_marshall_buffer
*)recdata
.dptr
;
170 struct ctdb_db_context
*ctdb_db
;
172 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
173 DEBUG(DEBUG_INFO
,("rejecting ctdb_control_trans3_commit when recovery active\n"));
177 client
= ctdb_reqid_find(ctdb
, c
->client_id
, struct ctdb_client
);
178 if (client
== NULL
) {
179 DEBUG(DEBUG_ERR
,(__location__
" can not match persistent_store "
180 "to a client. Returning error\n"));
184 if (client
->db_id
!= 0) {
185 DEBUG(DEBUG_ERR
,(__location__
" ERROR: trans3_commit: "
186 "client-db_id[0x%08x] != 0 "
187 "(client_id[0x%08x]): trans3_commit active?\n",
188 client
->db_id
, client
->client_id
));
192 ctdb_db
= find_ctdb_db(ctdb
, m
->db_id
);
193 if (ctdb_db
== NULL
) {
194 DEBUG(DEBUG_ERR
,(__location__
" ctdb_control_trans3_commit: "
195 "Unknown database db_id[0x%08x]\n", m
->db_id
));
199 if (ctdb_db
->persistent_state
!= NULL
) {
200 DEBUG(DEBUG_ERR
, (__location__
" Error: "
201 "ctdb_control_trans3_commit "
202 "called while a transaction commit is "
203 "active. db_id[0x%08x]\n", m
->db_id
));
207 ctdb_db
->persistent_state
= talloc_zero(ctdb_db
,
208 struct ctdb_persistent_state
);
209 CTDB_NO_MEMORY(ctdb
, ctdb_db
->persistent_state
);
211 client
->db_id
= m
->db_id
;
213 state
= ctdb_db
->persistent_state
;
215 state
->ctdb_db
= ctdb_db
;
217 state
->client
= client
;
219 talloc_set_destructor(state
, ctdb_persistent_state_destructor
);
221 for (i
= 0; i
< ctdb
->vnn_map
->size
; i
++) {
222 struct ctdb_node
*node
= ctdb
->nodes
[ctdb
->vnn_map
->map
[i
]];
225 /* only send to active nodes */
226 if (node
->flags
& NODE_FLAGS_INACTIVE
) {
230 ret
= ctdb_daemon_send_control(ctdb
, node
->pnn
, 0,
231 CTDB_CONTROL_UPDATE_RECORD
,
232 c
->client_id
, 0, recdata
,
233 ctdb_persistent_callback
,
236 DEBUG(DEBUG_ERR
,("Unable to send "
237 "CTDB_CONTROL_UPDATE_RECORD "
238 "to pnn %u\n", node
->pnn
));
243 state
->num_pending
++;
247 if (state
->num_pending
== 0) {
252 /* we need to wait for the replies */
255 /* need to keep the control structure around */
256 talloc_steal(state
, c
);
258 /* but we won't wait forever */
259 event_add_timed(ctdb
->ev
, state
,
260 timeval_current_ofs(ctdb
->tunable
.control_timeout
, 0),
261 ctdb_persistent_store_timeout
, state
);
268 backwards compatibility:
270 start a persistent store operation. passing both the key, header and
271 data to the daemon. If the client disconnects before it has issued
272 a persistent_update call to the daemon we trigger a full recovery
273 to ensure the databases are brought back in sync.
274 for now we ignore the recdata that the client has passed to us.
276 int32_t ctdb_control_start_persistent_update(struct ctdb_context
*ctdb
,
277 struct ctdb_req_control
*c
,
280 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
, c
->client_id
, struct ctdb_client
);
282 if (client
== NULL
) {
283 DEBUG(DEBUG_ERR
,(__location__
" can not match start_persistent_update to a client. Returning error\n"));
287 client
->num_persistent_updates
++;
293 backwards compatibility:
295 called to tell ctdbd that it is no longer doing a persistent update
297 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context
*ctdb
,
298 struct ctdb_req_control
*c
,
301 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
, c
->client_id
, struct ctdb_client
);
303 if (client
== NULL
) {
304 DEBUG(DEBUG_ERR
,(__location__
" can not match cancel_persistent_update to a client. Returning error\n"));
308 if (client
->num_persistent_updates
> 0) {
309 client
->num_persistent_updates
--;
315 static int32_t ctdb_get_db_seqnum(struct ctdb_context
*ctdb
,
320 struct ctdb_db_context
*ctdb_db
;
321 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
324 TALLOC_CTX
*mem_ctx
= talloc_new(ctdb
);
325 struct ctdb_ltdb_header header
;
327 ctdb_db
= find_ctdb_db(ctdb
, db_id
);
329 DEBUG(DEBUG_ERR
,(__location__
" Unknown db 0x%08x\n", db_id
));
334 key
.dptr
= (uint8_t *)discard_const(keyname
);
335 key
.dsize
= strlen(keyname
) + 1;
337 ret
= (int32_t)ctdb_ltdb_fetch(ctdb_db
, key
, &header
, mem_ctx
, &data
);
342 if (data
.dsize
!= sizeof(uint64_t)) {
347 *seqnum
= *(uint64_t *)data
.dptr
;
350 talloc_free(mem_ctx
);
355 * Get the sequence number of a persistent database.
357 int32_t ctdb_control_get_db_seqnum(struct ctdb_context
*ctdb
,
365 db_id
= *(uint32_t *)indata
.dptr
;
366 ret
= ctdb_get_db_seqnum(ctdb
, db_id
, &seqnum
);
371 outdata
->dsize
= sizeof(uint64_t);
372 outdata
->dptr
= (uint8_t *)talloc_zero(outdata
, uint64_t);
373 if (outdata
->dptr
== NULL
) {
378 *(outdata
->dptr
) = seqnum
;