2 implementation of the update record control
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/network.h"
23 #include "system/time.h"
28 #include "lib/tdb_wrap/tdb_wrap.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/samba_util.h"
31 #include "lib/util/sys_rw.h"
32 #include "lib/util/util_process.h"
34 #include "ctdb_private.h"
35 #include "ctdb_client.h"
37 #include "common/system.h"
38 #include "common/common.h"
39 #include "common/logging.h"
41 struct ctdb_persistent_write_state
{
42 struct ctdb_db_context
*ctdb_db
;
43 struct ctdb_marshall_buffer
*m
;
44 struct ctdb_req_control_old
*c
;
48 /* don't create/update records that does not exist locally */
49 #define UPDATE_FLAGS_REPLACE_ONLY 1
52 called from a child process to write the data
54 static int ctdb_persistent_store(struct ctdb_persistent_write_state
*state
)
58 struct ctdb_rec_data_old
*rec
= NULL
;
59 struct ctdb_marshall_buffer
*m
= state
->m
;
61 ret
= tdb_transaction_start(state
->ctdb_db
->ltdb
->tdb
);
63 DEBUG(DEBUG_ERR
,("Failed to start transaction for db_id 0x%08x in ctdb_persistent_store\n",
64 state
->ctdb_db
->db_id
));
68 for (i
=0;i
<m
->count
;i
++) {
69 struct ctdb_ltdb_header oldheader
;
70 struct ctdb_ltdb_header header
;
71 TDB_DATA key
, data
, olddata
;
72 TALLOC_CTX
*tmp_ctx
= talloc_new(state
);
74 rec
= ctdb_marshall_loop_next(m
, rec
, NULL
, &header
, &key
, &data
);
77 D_ERR("Failed to get next record %u for db_id 0x%08x "
78 "in ctdb_persistent_store\n",
80 state
->ctdb_db
->db_id
);
85 /* we must check if the record exists or not because
86 ctdb_ltdb_fetch will unconditionally create a record
88 if (state
->flags
& UPDATE_FLAGS_REPLACE_ONLY
) {
90 trec
= tdb_fetch(state
->ctdb_db
->ltdb
->tdb
, key
);
91 if (trec
.dsize
== 0) {
98 /* fetch the old header and ensure the rsn is less than the new rsn */
99 ret
= ctdb_ltdb_fetch(state
->ctdb_db
, key
, &oldheader
, tmp_ctx
, &olddata
);
101 DEBUG(DEBUG_ERR
,("Failed to fetch old record for db_id 0x%08x in ctdb_persistent_store\n",
102 state
->ctdb_db
->db_id
));
103 talloc_free(tmp_ctx
);
107 if (oldheader
.rsn
>= header
.rsn
&&
108 (olddata
.dsize
!= data
.dsize
||
109 memcmp(olddata
.dptr
, data
.dptr
, data
.dsize
) != 0)) {
110 DEBUG(DEBUG_CRIT
,("existing header for db_id 0x%08x has larger RSN %llu than new RSN %llu in ctdb_persistent_store\n",
111 state
->ctdb_db
->db_id
,
112 (unsigned long long)oldheader
.rsn
, (unsigned long long)header
.rsn
));
113 talloc_free(tmp_ctx
);
117 talloc_free(tmp_ctx
);
119 ret
= ctdb_ltdb_store(state
->ctdb_db
, key
, &header
, data
);
121 DEBUG(DEBUG_CRIT
,("Failed to store record for db_id 0x%08x in ctdb_persistent_store\n",
122 state
->ctdb_db
->db_id
));
127 ret
= tdb_transaction_commit(state
->ctdb_db
->ltdb
->tdb
);
129 DEBUG(DEBUG_ERR
,("Failed to commit transaction for db_id 0x%08x in ctdb_persistent_store\n",
130 state
->ctdb_db
->db_id
));
137 tdb_transaction_cancel(state
->ctdb_db
->ltdb
->tdb
);
143 called when we the child has completed the persistent write
146 static void ctdb_persistent_write_callback(int status
, void *private_data
)
148 struct ctdb_persistent_write_state
*state
= talloc_get_type(private_data
,
149 struct ctdb_persistent_write_state
);
152 ctdb_request_control_reply(state
->ctdb_db
->ctdb
, state
->c
, NULL
, status
, NULL
);
158 called if our lockwait child times out
160 static void ctdb_persistent_lock_timeout(struct tevent_context
*ev
,
161 struct tevent_timer
*te
,
162 struct timeval t
, void *private_data
)
164 struct ctdb_persistent_write_state
*state
= talloc_get_type(private_data
,
165 struct ctdb_persistent_write_state
);
166 ctdb_request_control_reply(state
->ctdb_db
->ctdb
, state
->c
, NULL
, -1, "timeout in ctdb_persistent_lock");
170 struct childwrite_handle
{
171 struct ctdb_context
*ctdb
;
172 struct ctdb_db_context
*ctdb_db
;
173 struct tevent_fd
*fde
;
177 void (*callback
)(int, void *);
178 struct timeval start_time
;
181 static int childwrite_destructor(struct childwrite_handle
*h
)
183 CTDB_DECREMENT_STAT(h
->ctdb
, pending_childwrite_calls
);
184 ctdb_kill(h
->ctdb
, h
->child
, SIGKILL
);
188 /* called when the child process has finished writing the record to the
191 static void childwrite_handler(struct tevent_context
*ev
,
192 struct tevent_fd
*fde
,
193 uint16_t flags
, void *private_data
)
195 struct childwrite_handle
*h
= talloc_get_type(private_data
,
196 struct childwrite_handle
);
197 void *p
= h
->private_data
;
198 void (*callback
)(int, void *) = h
->callback
;
199 pid_t child
= h
->child
;
200 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
204 CTDB_UPDATE_LATENCY(h
->ctdb
, h
->ctdb_db
, "persistent", childwrite_latency
, h
->start_time
);
205 CTDB_DECREMENT_STAT(h
->ctdb
, pending_childwrite_calls
);
207 /* the handle needs to go away when the context is gone - when
208 the handle goes away this implicitly closes the pipe, which
210 talloc_steal(tmp_ctx
, h
);
212 talloc_set_destructor(h
, NULL
);
214 ret
= sys_read(h
->fd
[0], &c
, 1);
216 DEBUG(DEBUG_ERR
, (__location__
" Read returned %d. Childwrite failed\n", ret
));
222 ctdb_kill(h
->ctdb
, child
, SIGKILL
);
223 talloc_free(tmp_ctx
);
226 /* this creates a child process which will take out a tdb transaction
227 and write the record to the database.
229 static struct childwrite_handle
*ctdb_childwrite(
230 struct ctdb_db_context
*ctdb_db
,
231 void (*callback
)(int, void *private_data
),
232 struct ctdb_persistent_write_state
*state
)
234 struct childwrite_handle
*result
;
236 pid_t parent
= getpid();
238 CTDB_INCREMENT_STAT(ctdb_db
->ctdb
, childwrite_calls
);
239 CTDB_INCREMENT_STAT(ctdb_db
->ctdb
, pending_childwrite_calls
);
241 if (!(result
= talloc_zero(state
, struct childwrite_handle
))) {
242 CTDB_DECREMENT_STAT(ctdb_db
->ctdb
, pending_childwrite_calls
);
246 ret
= pipe(result
->fd
);
250 CTDB_DECREMENT_STAT(ctdb_db
->ctdb
, pending_childwrite_calls
);
254 result
->child
= ctdb_fork(ctdb_db
->ctdb
);
256 if (result
->child
== (pid_t
)-1) {
257 close(result
->fd
[0]);
258 close(result
->fd
[1]);
260 CTDB_DECREMENT_STAT(ctdb_db
->ctdb
, pending_childwrite_calls
);
264 result
->callback
= callback
;
265 result
->private_data
= state
;
266 result
->ctdb
= ctdb_db
->ctdb
;
267 result
->ctdb_db
= ctdb_db
;
269 if (result
->child
== 0) {
272 close(result
->fd
[0]);
273 prctl_set_comment("ctdb_write_persistent");
274 ret
= ctdb_persistent_store(state
);
276 DEBUG(DEBUG_ERR
, (__location__
" Failed to write persistent data\n"));
280 sys_write(result
->fd
[1], &c
, 1);
282 ctdb_wait_for_process_to_exit(parent
);
286 close(result
->fd
[1]);
287 set_close_on_exec(result
->fd
[0]);
289 talloc_set_destructor(result
, childwrite_destructor
);
291 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d for ctdb_childwrite\n", result
->fd
[0]));
293 result
->fde
= tevent_add_fd(ctdb_db
->ctdb
->ev
, result
, result
->fd
[0],
294 TEVENT_FD_READ
, childwrite_handler
,
296 if (result
->fde
== NULL
) {
298 CTDB_DECREMENT_STAT(ctdb_db
->ctdb
, pending_childwrite_calls
);
301 tevent_fd_set_auto_close(result
->fde
);
303 result
->start_time
= timeval_current();
309 update a record on this node if the new record has a higher rsn than the
312 int32_t ctdb_control_update_record(struct ctdb_context
*ctdb
,
313 struct ctdb_req_control_old
*c
, TDB_DATA recdata
,
316 struct ctdb_db_context
*ctdb_db
;
317 struct ctdb_persistent_write_state
*state
;
318 struct childwrite_handle
*handle
;
319 struct ctdb_marshall_buffer
*m
= (struct ctdb_marshall_buffer
*)recdata
.dptr
;
321 if (ctdb
->recovery_mode
!= CTDB_RECOVERY_NORMAL
) {
322 DEBUG(DEBUG_INFO
,("rejecting ctdb_control_update_record when recovery active\n"));
326 ctdb_db
= find_ctdb_db(ctdb
, m
->db_id
);
327 if (ctdb_db
== NULL
) {
328 DEBUG(DEBUG_ERR
,("Unknown database 0x%08x in ctdb_control_update_record\n", m
->db_id
));
332 if (ctdb_db
->unhealthy_reason
) {
333 DEBUG(DEBUG_ERR
,("db(%s) unhealty in ctdb_control_update_record: %s\n",
334 ctdb_db
->db_name
, ctdb_db
->unhealthy_reason
));
338 state
= talloc(ctdb
, struct ctdb_persistent_write_state
);
339 CTDB_NO_MEMORY(ctdb
, state
);
341 state
->ctdb_db
= ctdb_db
;
345 if (ctdb_db_volatile(ctdb_db
)) {
346 state
->flags
= UPDATE_FLAGS_REPLACE_ONLY
;
349 /* create a child process to take out a transaction and
352 handle
= ctdb_childwrite(ctdb_db
, ctdb_persistent_write_callback
, state
);
353 if (handle
== NULL
) {
354 DEBUG(DEBUG_ERR
,("Failed to setup childwrite handler in ctdb_control_update_record\n"));
359 /* we need to wait for the replies */
362 /* need to keep the control structure around */
363 talloc_steal(state
, c
);
365 /* but we won't wait forever */
366 tevent_add_timer(ctdb
->ev
, state
,
367 timeval_current_ofs(ctdb
->tunable
.control_timeout
, 0),
368 ctdb_persistent_lock_timeout
, state
);