4 Copyright (C) Ronnie Sahlberg 2009
5 Copyright (C) Michael Adam 2010-2013
6 Copyright (C) Stefan Metzmacher 2010-2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.h"
29 #include "lib/util/dlinklist.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
35 enum vacuum_child_status
{ VACUUM_RUNNING
, VACUUM_OK
, VACUUM_ERROR
, VACUUM_TIMEOUT
};
37 struct ctdb_vacuum_child_context
{
38 struct ctdb_vacuum_child_context
*next
, *prev
;
39 struct ctdb_vacuum_handle
*vacuum_handle
;
40 /* fd child writes status to */
43 enum vacuum_child_status status
;
44 struct timeval start_time
;
47 struct ctdb_vacuum_handle
{
48 struct ctdb_db_context
*ctdb_db
;
49 struct ctdb_vacuum_child_context
*child_ctx
;
50 uint32_t fast_path_count
;
54 /* a list of records to possibly delete */
56 uint32_t vacuum_limit
;
57 uint32_t repack_limit
;
58 struct ctdb_context
*ctdb
;
59 struct ctdb_db_context
*ctdb_db
;
60 struct tdb_context
*dest_db
;
61 trbt_tree_t
*delete_list
;
62 uint32_t delete_count
;
63 struct ctdb_marshall_buffer
**vacuum_fetch_list
;
70 uint32_t fast_added_to_vacuum_fetch_list
;
71 uint32_t fast_added_to_delete_list
;
72 uint32_t fast_deleted
;
73 uint32_t fast_skipped
;
76 uint32_t full_added_to_vacuum_fetch_list
;
77 uint32_t full_added_to_delete_list
;
78 uint32_t full_skipped
;
82 uint32_t delete_remote_error
;
83 uint32_t delete_local_error
;
84 uint32_t delete_deleted
;
85 uint32_t delete_skipped
;
88 /* this structure contains the information for one record to be deleted */
89 struct delete_record_data
{
90 struct ctdb_context
*ctdb
;
91 struct ctdb_db_context
*ctdb_db
;
92 struct ctdb_ltdb_header hdr
;
97 struct delete_records_list
{
98 struct ctdb_marshall_buffer
*records
;
99 struct vacuum_data
*vdata
;
103 * Store key and header in a tree, indexed by the key hash.
105 static int insert_delete_record_data_into_tree(struct ctdb_context
*ctdb
,
106 struct ctdb_db_context
*ctdb_db
,
108 const struct ctdb_ltdb_header
*hdr
,
111 struct delete_record_data
*dd
;
115 len
= offsetof(struct delete_record_data
, keydata
) + key
.dsize
;
117 dd
= (struct delete_record_data
*)talloc_size(tree
, len
);
119 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
122 talloc_set_name_const(dd
, "struct delete_record_data");
125 dd
->ctdb_db
= ctdb_db
;
126 dd
->key
.dsize
= key
.dsize
;
127 dd
->key
.dptr
= dd
->keydata
;
128 memcpy(dd
->keydata
, key
.dptr
, key
.dsize
);
132 hash
= ctdb_hash(&key
);
134 trbt_insert32(tree
, hash
, dd
);
139 static int add_record_to_delete_list(struct vacuum_data
*vdata
, TDB_DATA key
,
140 struct ctdb_ltdb_header
*hdr
)
142 struct ctdb_context
*ctdb
= vdata
->ctdb
;
143 struct ctdb_db_context
*ctdb_db
= vdata
->ctdb_db
;
147 hash
= ctdb_hash(&key
);
149 if (trbt_lookup32(vdata
->delete_list
, hash
)) {
150 DEBUG(DEBUG_INFO
, (__location__
" Hash collision when vacuuming, skipping this record.\n"));
154 ret
= insert_delete_record_data_into_tree(ctdb
, ctdb_db
,
161 vdata
->delete_count
++;
167 * Add a record to the list of records to be sent
168 * to their lmaster with VACUUM_FETCH.
170 static int add_record_to_vacuum_fetch_list(struct vacuum_data
*vdata
,
173 struct ctdb_context
*ctdb
= vdata
->ctdb
;
174 struct ctdb_rec_data
*rec
;
177 struct ctdb_marshall_buffer
*vfl
;
179 lmaster
= ctdb_lmaster(ctdb
, &key
);
181 vfl
= vdata
->vacuum_fetch_list
[lmaster
];
183 rec
= ctdb_marshall_record(vfl
, ctdb
->pnn
, key
, NULL
, tdb_null
);
185 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
186 vdata
->traverse_error
= true;
190 old_size
= talloc_get_size(vfl
);
191 vfl
= talloc_realloc_size(NULL
, vfl
, old_size
+ rec
->length
);
193 DEBUG(DEBUG_ERR
,(__location__
" Failed to expand\n"));
194 vdata
->traverse_error
= true;
197 vdata
->vacuum_fetch_list
[lmaster
] = vfl
;
200 memcpy(old_size
+(uint8_t *)vfl
, rec
, rec
->length
);
209 static void ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
210 struct timeval t
, void *private_data
);
212 static int vacuum_record_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
214 struct ctdb_ltdb_header
*header
=
215 (struct ctdb_ltdb_header
*)private_data
;
217 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
221 *header
= *(struct ctdb_ltdb_header
*)data
.dptr
;
227 * traverse function for gathering the records that can be deleted
229 static int vacuum_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
,
232 struct vacuum_data
*vdata
= talloc_get_type(private_data
,
234 struct ctdb_context
*ctdb
= vdata
->ctdb
;
236 struct ctdb_ltdb_header
*hdr
;
241 lmaster
= ctdb_lmaster(ctdb
, &key
);
242 if (lmaster
>= ctdb
->num_nodes
) {
244 DEBUG(DEBUG_CRIT
, (__location__
245 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
248 (unsigned)ctdb
->num_nodes
,
249 (unsigned)ctdb_hash(&key
)));
253 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
254 /* it is not a deleted record */
255 vdata
->full_skipped
++;
259 hdr
= (struct ctdb_ltdb_header
*)data
.dptr
;
261 if (hdr
->dmaster
!= ctdb
->pnn
) {
262 vdata
->full_skipped
++;
266 if (lmaster
== ctdb
->pnn
) {
268 * We are both lmaster and dmaster, and the record is empty.
269 * So we should be able to delete it.
271 res
= add_record_to_delete_list(vdata
, key
, hdr
);
275 vdata
->full_added_to_delete_list
++;
279 * We are not lmaster.
280 * Add the record to the blob ready to send to the nodes.
282 res
= add_record_to_vacuum_fetch_list(vdata
, key
);
286 vdata
->full_added_to_vacuum_fetch_list
++;
294 * traverse the tree of records to delete and marshall them into
297 static int delete_marshall_traverse(void *param
, void *data
)
299 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
300 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
301 struct ctdb_rec_data
*rec
;
304 rec
= ctdb_marshall_record(dd
, recs
->records
->db_id
, dd
->key
, &dd
->hdr
, tdb_null
);
306 DEBUG(DEBUG_ERR
, (__location__
" failed to marshall record\n"));
310 old_size
= talloc_get_size(recs
->records
);
311 recs
->records
= talloc_realloc_size(NULL
, recs
->records
, old_size
+ rec
->length
);
312 if (recs
->records
== NULL
) {
313 DEBUG(DEBUG_ERR
,(__location__
" Failed to expand\n"));
316 recs
->records
->count
++;
317 memcpy(old_size
+(uint8_t *)(recs
->records
), rec
, rec
->length
);
322 * Variant of delete_marshall_traverse() that bumps the
323 * RSN of each traversed record in the database.
325 * This is needed to ensure that when rolling out our
326 * empty record copy before remote deletion, we as the
327 * record's dmaster keep a higher RSN than the non-dmaster
328 * nodes. This is needed to prevent old copies from
329 * resurrection in recoveries.
331 static int delete_marshall_traverse_first(void *param
, void *data
)
333 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
334 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
335 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
336 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
337 struct ctdb_ltdb_header
*header
;
338 TDB_DATA tdb_data
, ctdb_data
;
340 uint32_t hash
= ctdb_hash(&(dd
->key
));
343 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
346 (__location__
" Error getting chainlock on record with "
347 "key hash [0x%08x] on database db[%s].\n",
348 hash
, ctdb_db
->db_name
));
349 recs
->vdata
->delete_skipped
++;
355 * Verify that the record is still empty, its RSN has not
356 * changed and that we are still its lmaster and dmaster.
359 tdb_data
= tdb_fetch(ctdb_db
->ltdb
->tdb
, dd
->key
);
360 if (tdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
361 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
362 "on database db[%s] does not exist or is not"
363 " a ctdb-record. skipping.\n",
364 hash
, ctdb_db
->db_name
));
368 if (tdb_data
.dsize
> sizeof(struct ctdb_ltdb_header
)) {
369 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
370 "on database db[%s] has been recycled. "
372 hash
, ctdb_db
->db_name
));
376 header
= (struct ctdb_ltdb_header
*)tdb_data
.dptr
;
378 if (header
->flags
& CTDB_REC_RO_FLAGS
) {
379 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
380 "on database db[%s] has read-only flags. "
382 hash
, ctdb_db
->db_name
));
386 if (header
->dmaster
!= ctdb
->pnn
) {
387 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
388 "on database db[%s] has been migrated away. "
390 hash
, ctdb_db
->db_name
));
394 if (header
->rsn
!= dd
->hdr
.rsn
) {
395 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
396 "on database db[%s] seems to have been "
397 "migrated away and back again (with empty "
398 "data). skipping.\n",
399 hash
, ctdb_db
->db_name
));
403 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
405 if (lmaster
!= ctdb
->pnn
) {
406 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
407 "delete list (key hash [0x%08x], db[%s]). "
408 "Strange! skipping.\n",
409 hash
, ctdb_db
->db_name
));
414 * Increment the record's RSN to ensure the dmaster (i.e. the current
415 * node) has the highest RSN of the record in the cluster.
416 * This is to prevent old record copies from resurrecting in recoveries
417 * if something should fail during the deletion process.
418 * Note that ctdb_ltdb_store_server() increments the RSN if called
419 * on the record's dmaster.
422 ctdb_data
.dptr
= tdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
);
423 ctdb_data
.dsize
= tdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
425 res
= ctdb_ltdb_store(ctdb_db
, dd
->key
, header
, ctdb_data
);
427 DEBUG(DEBUG_ERR
, (__location__
": Failed to store record with "
428 "key hash [0x%08x] on database db[%s].\n",
429 hash
, ctdb_db
->db_name
));
433 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
438 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
440 recs
->vdata
->delete_skipped
++;
445 if (tdb_data
.dptr
!= NULL
) {
453 return delete_marshall_traverse(param
, data
);
457 * traverse function for the traversal of the delete_queue,
458 * the fast-path vacuuming list.
460 * - If the record has been migrated off the node
461 * or has been revived (filled with data) on the node,
462 * then skip the record.
464 * - If the current node is the record's lmaster and it is
465 * a record that has never been migrated with data, then
466 * delete the record from the local tdb.
468 * - If the current node is the record's lmaster and it has
469 * been migrated with data, then schedule it for the normal
470 * vacuuming procedure (i.e. add it to the delete_list).
472 * - If the current node is NOT the record's lmaster then
473 * add it to the list of records that are to be sent to
474 * the lmaster with the VACUUM_FETCH message.
476 static int delete_queue_traverse(void *param
, void *data
)
478 struct delete_record_data
*dd
=
479 talloc_get_type(data
, struct delete_record_data
);
480 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
481 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
482 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
; /* or dd->ctdb ??? */
484 struct ctdb_ltdb_header header
;
486 uint32_t hash
= ctdb_hash(&(dd
->key
));
490 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
493 (__location__
" Error getting chainlock on record with "
494 "key hash [0x%08x] on database db[%s].\n",
495 hash
, ctdb_db
->db_name
));
500 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
501 vacuum_record_parser
, &header
);
506 if (header
.dmaster
!= ctdb
->pnn
) {
507 /* The record has been migrated off the node. Skip. */
511 if (header
.rsn
!= dd
->hdr
.rsn
) {
513 * The record has been migrated off the node and back again.
514 * But not requeued for deletion. Skip it.
520 * We are dmaster, and the record has no data, and it has
521 * not been migrated after it has been queued for deletion.
523 * At this stage, the record could still have been revived locally
524 * and last been written with empty data. This can only be
525 * fixed with the addition of an active or delete flag. (TODO)
528 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
530 if (lmaster
!= ctdb
->pnn
) {
531 res
= add_record_to_vacuum_fetch_list(vdata
, dd
->key
);
535 (__location__
" Error adding record to list "
536 "of records to send to lmaster.\n"));
539 vdata
->fast_added_to_vacuum_fetch_list
++;
544 /* use header->flags or dd->hdr.flags ?? */
545 if (dd
->hdr
.flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
) {
546 res
= add_record_to_delete_list(vdata
, dd
->key
, &dd
->hdr
);
550 (__location__
" Error adding record to list "
551 "of records for deletion on lmaster.\n"));
554 vdata
->fast_added_to_delete_list
++;
557 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
561 (__location__
" Error deleting record with key "
562 "hash [0x%08x] from local data base db[%s].\n",
563 hash
, ctdb_db
->db_name
));
569 (__location__
" Deleted record with key hash "
570 "[0x%08x] from local data base db[%s].\n",
571 hash
, ctdb_db
->db_name
));
572 vdata
->fast_deleted
++;
578 vdata
->fast_skipped
++;
581 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
587 * Delete the records that we are lmaster and dmaster for and
588 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
591 static int delete_record_traverse(void *param
, void *data
)
593 struct delete_record_data
*dd
=
594 talloc_get_type(data
, struct delete_record_data
);
595 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
596 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
597 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
599 struct ctdb_ltdb_header
*header
;
602 uint32_t hash
= ctdb_hash(&(dd
->key
));
604 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
607 (__location__
" Error getting chainlock on record with "
608 "key hash [0x%08x] on database db[%s].\n",
609 hash
, ctdb_db
->db_name
));
610 vdata
->delete_local_error
++;
611 vdata
->delete_left
--;
617 * Verify that the record is still empty, its RSN has not
618 * changed and that we are still its lmaster and dmaster.
621 tdb_data
= tdb_fetch(ctdb_db
->ltdb
->tdb
, dd
->key
);
622 if (tdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
623 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
624 "on database db[%s] does not exist or is not"
625 " a ctdb-record. skipping.\n",
626 hash
, ctdb_db
->db_name
));
630 if (tdb_data
.dsize
> sizeof(struct ctdb_ltdb_header
)) {
631 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
632 "on database db[%s] has been recycled. "
634 hash
, ctdb_db
->db_name
));
638 header
= (struct ctdb_ltdb_header
*)tdb_data
.dptr
;
640 if (header
->flags
& CTDB_REC_RO_FLAGS
) {
641 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
642 "on database db[%s] has read-only flags. "
644 hash
, ctdb_db
->db_name
));
648 if (header
->dmaster
!= ctdb
->pnn
) {
649 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
650 "on database db[%s] has been migrated away. "
652 hash
, ctdb_db
->db_name
));
656 if (header
->rsn
!= dd
->hdr
.rsn
+ 1) {
658 * The record has been migrated off the node and back again.
659 * But not requeued for deletion. Skip it.
660 * (Note that the first marshall traverse has bumped the RSN
663 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
664 "on database db[%s] seems to have been "
665 "migrated away and back again (with empty "
666 "data). skipping.\n",
667 hash
, ctdb_db
->db_name
));
671 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
673 if (lmaster
!= ctdb
->pnn
) {
674 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
675 "delete list (key hash [0x%08x], db[%s]). "
676 "Strange! skipping.\n",
677 hash
, ctdb_db
->db_name
));
681 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
685 (__location__
" Error deleting record with key hash "
686 "[0x%08x] from local data base db[%s].\n",
687 hash
, ctdb_db
->db_name
));
688 vdata
->delete_local_error
++;
693 (__location__
" Deleted record with key hash [0x%08x] from "
694 "local data base db[%s].\n", hash
, ctdb_db
->db_name
));
696 vdata
->delete_deleted
++;
700 vdata
->delete_skipped
++;
705 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
708 vdata
->delete_left
--;
714 * Fast vacuuming run:
715 * Traverse the delete_queue.
716 * This fills the same lists as the database traverse.
718 static void ctdb_vacuum_db_fast(struct ctdb_db_context
*ctdb_db
,
719 struct vacuum_data
*vdata
)
723 trbt_traversearray32(ctdb_db
->delete_queue
, 1, delete_queue_traverse
, vdata
);
725 sum
= vdata
->fast_deleted
726 + vdata
->fast_skipped
728 + vdata
->fast_added_to_delete_list
729 + vdata
->fast_added_to_vacuum_fetch_list
;
731 if (vdata
->fast_total
!= sum
) {
732 DEBUG(DEBUG_ERR
, (__location__
" Inconsistency in fast vacuum "
733 "counts for db[%s]: total[%u] != sum[%u]\n",
734 ctdb_db
->db_name
, (unsigned)vdata
->fast_total
,
738 if (vdata
->fast_total
> 0) {
741 " fast vacuuming delete_queue traverse statistics: "
750 (unsigned)vdata
->fast_total
,
751 (unsigned)vdata
->fast_deleted
,
752 (unsigned)vdata
->fast_skipped
,
753 (unsigned)vdata
->fast_error
,
754 (unsigned)vdata
->fast_added_to_delete_list
,
755 (unsigned)vdata
->fast_added_to_vacuum_fetch_list
));
763 * read-only traverse of the database, looking for records that
764 * might be able to be vacuumed.
766 * This is not done each time but only every tunable
767 * VacuumFastPathCount times.
769 static int ctdb_vacuum_db_full(struct ctdb_db_context
*ctdb_db
,
770 struct vacuum_data
*vdata
)
774 ret
= tdb_traverse_read(ctdb_db
->ltdb
->tdb
, vacuum_traverse
, vdata
);
775 if (ret
== -1 || vdata
->traverse_error
) {
776 DEBUG(DEBUG_ERR
, (__location__
" Traverse error in vacuuming "
777 "'%s'\n", ctdb_db
->db_name
));
781 if (vdata
->full_total
> 0) {
784 " full vacuuming db traverse statistics: "
792 (unsigned)vdata
->full_total
,
793 (unsigned)vdata
->full_skipped
,
794 (unsigned)vdata
->full_error
,
795 (unsigned)vdata
->full_added_to_delete_list
,
796 (unsigned)vdata
->full_added_to_vacuum_fetch_list
));
803 * Process the vacuum fetch lists:
804 * For records for which we are not the lmaster, tell the lmaster to
807 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context
*ctdb_db
,
808 struct vacuum_data
*vdata
)
811 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
813 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
815 struct ctdb_marshall_buffer
*vfl
= vdata
->vacuum_fetch_list
[i
];
817 if (ctdb
->nodes
[i
]->pnn
== ctdb
->pnn
) {
821 if (vfl
->count
== 0) {
825 DEBUG(DEBUG_INFO
, ("Found %u records for lmaster %u in '%s'\n",
826 vfl
->count
, ctdb
->nodes
[i
]->pnn
,
829 data
.dsize
= talloc_get_size(vfl
);
830 data
.dptr
= (void *)vfl
;
831 if (ctdb_client_send_message(ctdb
, ctdb
->nodes
[i
]->pnn
,
832 CTDB_SRVID_VACUUM_FETCH
,
835 DEBUG(DEBUG_ERR
, (__location__
" Failed to send vacuum "
836 "fetch message to %u\n",
837 ctdb
->nodes
[i
]->pnn
));
846 * Process the delete list:
848 * This is the last step of vacuuming that consistently deletes
849 * those records that have been migrated with data and can hence
850 * not be deleted when leaving a node.
852 * In this step, the lmaster does the final deletion of those empty
853 * records that it is also dmaster for. It has ususally received
854 * at least some of these records previously from the former dmasters
855 * with the vacuum fetch message.
857 * This last step is implemented as a 3-phase process to protect from
858 * races leading to data corruption:
860 * 1) Send the lmaster's copy to all other active nodes with the
861 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
862 * 2) Send the records that could successfully be stored remotely
863 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
864 * control. The remote notes delete their local copy.
865 * 3) The lmaster locally deletes its copies of all records that
866 * could successfully be deleted remotely in step #2.
868 static int ctdb_process_delete_list(struct ctdb_db_context
*ctdb_db
,
869 struct vacuum_data
*vdata
)
872 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
873 struct delete_records_list
*recs
;
875 struct ctdb_node_map
*nodemap
;
876 uint32_t *active_nodes
;
877 int num_active_nodes
;
880 if (vdata
->delete_count
== 0) {
884 tmp_ctx
= talloc_new(vdata
);
885 if (tmp_ctx
== NULL
) {
886 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
890 vdata
->delete_left
= vdata
->delete_count
;
893 * get the list of currently active nodes
896 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
901 DEBUG(DEBUG_ERR
,(__location__
" unable to get node map\n"));
906 active_nodes
= list_of_active_nodes(ctdb
, nodemap
,
907 nodemap
, /* talloc context */
908 false /* include self */);
910 num_active_nodes
= talloc_get_size(active_nodes
)/sizeof(*active_nodes
);
913 * Now delete the records all active nodes in a three-phase process:
914 * 1) send all active remote nodes the current empty copy with this
916 * 2) if all nodes could store the new copy,
917 * tell all the active remote nodes to delete all their copy
918 * 3) if all remote nodes deleted their record copy, delete it locally
923 * Send currently empty record copy to all active nodes for storing.
926 recs
= talloc_zero(tmp_ctx
, struct delete_records_list
);
928 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
932 recs
->records
= (struct ctdb_marshall_buffer
*)
933 talloc_zero_size(recs
,
934 offsetof(struct ctdb_marshall_buffer
, data
));
935 if (recs
->records
== NULL
) {
936 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
940 recs
->records
->db_id
= ctdb_db
->db_id
;
944 * traverse the tree of all records we want to delete and
945 * create a blob we can send to the other nodes.
947 * We call delete_marshall_traverse_first() to bump the
948 * records' RSNs in the database, to ensure we (as dmaster)
949 * keep the highest RSN of the records in the cluster.
951 trbt_traversearray32(vdata
->delete_list
, 1,
952 delete_marshall_traverse_first
, recs
);
954 indata
.dsize
= talloc_get_size(recs
->records
);
955 indata
.dptr
= (void *)recs
->records
;
957 for (i
= 0; i
< num_active_nodes
; i
++) {
958 struct ctdb_marshall_buffer
*records
;
959 struct ctdb_rec_data
*rec
;
963 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
964 CTDB_CONTROL_RECEIVE_RECORDS
, 0,
965 indata
, recs
, &outdata
, &res
,
967 if (ret
!= 0 || res
!= 0) {
968 DEBUG(DEBUG_ERR
, ("Error storing record copies on "
969 "node %u: ret[%d] res[%d]\n",
970 active_nodes
[i
], ret
, res
));
976 * outdata contains the list of records coming back
977 * from the node: These are the records that the
978 * remote node could not store. We remove these from
979 * the list to process further.
981 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
982 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
983 while (records
->count
-- > 1) {
984 TDB_DATA reckey
, recdata
;
985 struct ctdb_ltdb_header
*rechdr
;
986 struct delete_record_data
*dd
;
988 reckey
.dptr
= &rec
->data
[0];
989 reckey
.dsize
= rec
->keylen
;
990 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
991 recdata
.dsize
= rec
->datalen
;
993 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
994 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
998 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
999 recdata
.dptr
+= sizeof(*rechdr
);
1000 recdata
.dsize
-= sizeof(*rechdr
);
1002 dd
= (struct delete_record_data
*)trbt_lookup32(
1004 ctdb_hash(&reckey
));
1007 * The other node could not store the record
1008 * copy and it is the first node that failed.
1009 * So we should remove it from the tree and
1010 * update statistics.
1013 vdata
->delete_remote_error
++;
1014 vdata
->delete_left
--;
1017 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
1021 if (vdata
->delete_left
== 0) {
1027 * Send the remaining records to all active nodes for deletion.
1029 * The lmaster's (i.e. our) copies of these records have been stored
1030 * successfully on the other nodes.
1034 * Create a marshall blob from the remaining list of records to delete.
1037 talloc_free(recs
->records
);
1039 recs
->records
= (struct ctdb_marshall_buffer
*)
1040 talloc_zero_size(recs
,
1041 offsetof(struct ctdb_marshall_buffer
, data
));
1042 if (recs
->records
== NULL
) {
1043 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1047 recs
->records
->db_id
= ctdb_db
->db_id
;
1049 trbt_traversearray32(vdata
->delete_list
, 1,
1050 delete_marshall_traverse
, recs
);
1052 indata
.dsize
= talloc_get_size(recs
->records
);
1053 indata
.dptr
= (void *)recs
->records
;
1055 for (i
= 0; i
< num_active_nodes
; i
++) {
1056 struct ctdb_marshall_buffer
*records
;
1057 struct ctdb_rec_data
*rec
;
1061 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
1062 CTDB_CONTROL_TRY_DELETE_RECORDS
, 0,
1063 indata
, recs
, &outdata
, &res
,
1065 if (ret
!= 0 || res
!= 0) {
1066 DEBUG(DEBUG_ERR
, ("Failed to delete records on "
1067 "node %u: ret[%d] res[%d]\n",
1068 active_nodes
[i
], ret
, res
));
1074 * outdata contains the list of records coming back
1075 * from the node: These are the records that the
1076 * remote node could not delete. We remove these from
1077 * the list to delete locally.
1079 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
1080 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
1081 while (records
->count
-- > 1) {
1082 TDB_DATA reckey
, recdata
;
1083 struct ctdb_ltdb_header
*rechdr
;
1084 struct delete_record_data
*dd
;
1086 reckey
.dptr
= &rec
->data
[0];
1087 reckey
.dsize
= rec
->keylen
;
1088 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
1089 recdata
.dsize
= rec
->datalen
;
1091 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1092 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
1096 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
1097 recdata
.dptr
+= sizeof(*rechdr
);
1098 recdata
.dsize
-= sizeof(*rechdr
);
1100 dd
= (struct delete_record_data
*)trbt_lookup32(
1102 ctdb_hash(&reckey
));
1105 * The other node could not delete the
1106 * record and it is the first node that
1107 * failed. So we should remove it from
1108 * the tree and update statistics.
1111 vdata
->delete_remote_error
++;
1112 vdata
->delete_left
--;
1115 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
1119 if (vdata
->delete_left
== 0) {
1125 * Delete the remaining records locally.
1127 * These records have successfully been deleted on all
1128 * active remote nodes.
1131 trbt_traversearray32(vdata
->delete_list
, 1,
1132 delete_record_traverse
, vdata
);
1136 if (vdata
->delete_count
> 0) {
1139 " vacuum delete list statistics: "
1148 (unsigned)vdata
->delete_count
,
1149 (unsigned)vdata
->delete_deleted
,
1150 (unsigned)vdata
->delete_skipped
,
1151 (unsigned)vdata
->delete_remote_error
,
1152 (unsigned)vdata
->delete_local_error
,
1153 (unsigned)vdata
->delete_left
));
1159 talloc_free(tmp_ctx
);
1165 * initialize the vacuum_data
1167 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context
*ctdb_db
,
1168 struct vacuum_data
*vdata
)
1171 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1173 vdata
->fast_added_to_delete_list
= 0;
1174 vdata
->fast_added_to_vacuum_fetch_list
= 0;
1175 vdata
->fast_deleted
= 0;
1176 vdata
->fast_skipped
= 0;
1177 vdata
->fast_error
= 0;
1178 vdata
->fast_total
= 0;
1179 vdata
->full_added_to_delete_list
= 0;
1180 vdata
->full_added_to_vacuum_fetch_list
= 0;
1181 vdata
->full_skipped
= 0;
1182 vdata
->full_error
= 0;
1183 vdata
->full_total
= 0;
1184 vdata
->delete_count
= 0;
1185 vdata
->delete_left
= 0;
1186 vdata
->delete_remote_error
= 0;
1187 vdata
->delete_local_error
= 0;
1188 vdata
->delete_skipped
= 0;
1189 vdata
->delete_deleted
= 0;
1191 /* the list needs to be of length num_nodes */
1192 vdata
->vacuum_fetch_list
= talloc_zero_array(vdata
,
1193 struct ctdb_marshall_buffer
*,
1195 if (vdata
->vacuum_fetch_list
== NULL
) {
1196 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1199 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
1200 vdata
->vacuum_fetch_list
[i
] = (struct ctdb_marshall_buffer
*)
1201 talloc_zero_size(vdata
->vacuum_fetch_list
,
1202 offsetof(struct ctdb_marshall_buffer
, data
));
1203 if (vdata
->vacuum_fetch_list
[i
] == NULL
) {
1204 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1207 vdata
->vacuum_fetch_list
[i
]->db_id
= ctdb_db
->db_id
;
1215 * - Always do the fast vacuuming run, which traverses
1216 * the in-memory delete queue: these records have been
1217 * scheduled for deletion.
1218 * - Only if explicitly requested, the database is traversed
1219 * in order to use the traditional heuristics on empty records
1220 * to trigger deletion.
1221 * This is done only every VacuumFastPathCount'th vacuuming run.
1223 * The traverse runs fill two lists:
1225 * - The delete_list:
1226 * This is the list of empty records the current
1227 * node is lmaster and dmaster for. These records are later
1228 * deleted first on other nodes and then locally.
1230 * The fast vacuuming run has a short cut for those records
1231 * that have never been migrated with data: these records
1232 * are immediately deleted locally, since they have left
1233 * no trace on other nodes.
1235 * - The vacuum_fetch lists
1236 * (one for each other lmaster node):
1237 * The records in this list are sent for deletion to
1238 * their lmaster in a bulk VACUUM_FETCH message.
1240 * The lmaster then migrates all these records to itelf
1241 * so that they can be vacuumed there.
1243 * This executes in the child context.
1245 static int ctdb_vacuum_db(struct ctdb_db_context
*ctdb_db
,
1246 struct vacuum_data
*vdata
,
1247 bool full_vacuum_run
)
1249 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1252 DEBUG(DEBUG_INFO
, (__location__
" Entering %s vacuum run for db "
1253 "%s db_id[0x%08x]\n",
1254 full_vacuum_run
? "full" : "fast",
1255 ctdb_db
->db_name
, ctdb_db
->db_id
));
1257 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &ctdb
->vnn_map
);
1259 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from local node\n"));
1263 pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1265 DEBUG(DEBUG_ERR
, ("Unable to get pnn from local node\n"));
1271 ret
= ctdb_vacuum_init_vacuum_data(ctdb_db
, vdata
);
1276 if (full_vacuum_run
) {
1277 ret
= ctdb_vacuum_db_full(ctdb_db
, vdata
);
1283 ctdb_vacuum_db_fast(ctdb_db
, vdata
);
1285 ret
= ctdb_process_vacuum_fetch_lists(ctdb_db
, vdata
);
1290 ret
= ctdb_process_delete_list(ctdb_db
, vdata
);
1295 /* this ensures we run our event queue */
1296 ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1303 * traverse function for repacking
1305 static int repack_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
,
1308 struct vacuum_data
*vdata
= (struct vacuum_data
*)private_data
;
1310 if (vdata
->vacuum
) {
1311 uint32_t hash
= ctdb_hash(&key
);
1312 struct delete_record_data
*kd
;
1314 * check if we can ignore this record because it's in the delete_list
1316 kd
= (struct delete_record_data
*)trbt_lookup32(vdata
->delete_list
, hash
);
1318 * there might be hash collisions so we have to compare the keys here to be sure
1320 if (kd
&& kd
->key
.dsize
== key
.dsize
&& memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) == 0) {
1321 struct ctdb_ltdb_header
*hdr
= (struct ctdb_ltdb_header
*)data
.dptr
;
1323 * we have to check if the record hasn't changed in the meantime in order to
1324 * savely remove it from the database
1326 if (data
.dsize
== sizeof(struct ctdb_ltdb_header
) &&
1327 hdr
->dmaster
== kd
->ctdb
->pnn
&&
1328 ctdb_lmaster(kd
->ctdb
, &(kd
->key
)) == kd
->ctdb
->pnn
&&
1329 kd
->hdr
.rsn
== hdr
->rsn
) {
1335 if (tdb_store(vdata
->dest_db
, key
, data
, TDB_INSERT
) != 0) {
1336 vdata
->traverse_error
= true;
1346 static int ctdb_repack_tdb(struct tdb_context
*tdb
, TALLOC_CTX
*mem_ctx
, struct vacuum_data
*vdata
)
1348 struct tdb_context
*tmp_db
;
1350 if (tdb_transaction_start(tdb
) != 0) {
1351 DEBUG(DEBUG_ERR
,(__location__
" Failed to start transaction\n"));
1355 tmp_db
= tdb_open("tmpdb", tdb_hash_size(tdb
),
1356 TDB_INTERNAL
|TDB_DISALLOW_NESTING
,
1358 if (tmp_db
== NULL
) {
1359 DEBUG(DEBUG_ERR
,(__location__
" Failed to create tmp_db\n"));
1360 tdb_transaction_cancel(tdb
);
1364 vdata
->traverse_error
= false;
1365 vdata
->dest_db
= tmp_db
;
1366 vdata
->vacuum
= true;
1367 vdata
->vacuumed
= 0;
1371 * repack and vacuum on-the-fly by not writing the records that are
1374 if (tdb_traverse_read(tdb
, repack_traverse
, vdata
) == -1) {
1375 DEBUG(DEBUG_ERR
,(__location__
" Failed to traverse copying out\n"));
1376 tdb_transaction_cancel(tdb
);
1381 DEBUG(DEBUG_INFO
,(__location__
" %u records vacuumed\n", vdata
->vacuumed
));
1383 if (vdata
->traverse_error
) {
1384 DEBUG(DEBUG_ERR
,(__location__
" Error during traversal\n"));
1385 tdb_transaction_cancel(tdb
);
1390 if (tdb_wipe_all(tdb
) != 0) {
1391 DEBUG(DEBUG_ERR
,(__location__
" Failed to wipe database\n"));
1392 tdb_transaction_cancel(tdb
);
1397 vdata
->traverse_error
= false;
1398 vdata
->dest_db
= tdb
;
1399 vdata
->vacuum
= false;
1402 if (tdb_traverse_read(tmp_db
, repack_traverse
, vdata
) == -1) {
1403 DEBUG(DEBUG_ERR
,(__location__
" Failed to traverse copying back\n"));
1404 tdb_transaction_cancel(tdb
);
1409 if (vdata
->traverse_error
) {
1410 DEBUG(DEBUG_ERR
,(__location__
" Error during second traversal\n"));
1411 tdb_transaction_cancel(tdb
);
1419 if (tdb_transaction_commit(tdb
) != 0) {
1420 DEBUG(DEBUG_ERR
,(__location__
" Failed to commit\n"));
1423 DEBUG(DEBUG_INFO
,(__location__
" %u records copied\n", vdata
->copied
));
1429 * repack and vaccum a db
1430 * called from the child context
1432 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context
*ctdb_db
,
1433 TALLOC_CTX
*mem_ctx
,
1434 bool full_vacuum_run
)
1436 uint32_t repack_limit
= ctdb_db
->ctdb
->tunable
.repack_limit
;
1437 uint32_t vacuum_limit
= ctdb_db
->ctdb
->tunable
.vacuum_limit
;
1438 const char *name
= ctdb_db
->db_name
;
1439 int freelist_size
= 0;
1440 struct vacuum_data
*vdata
;
1442 vdata
= talloc_zero(mem_ctx
, struct vacuum_data
);
1443 if (vdata
== NULL
) {
1444 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1448 vdata
->ctdb
= ctdb_db
->ctdb
;
1449 vdata
->vacuum_limit
= vacuum_limit
;
1450 vdata
->repack_limit
= repack_limit
;
1451 vdata
->delete_list
= trbt_create(vdata
, 0);
1452 vdata
->ctdb_db
= ctdb_db
;
1453 if (vdata
->delete_list
== NULL
) {
1454 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1459 vdata
->start
= timeval_current();
1462 * gather all records that can be deleted in vdata
1464 if (ctdb_vacuum_db(ctdb_db
, vdata
, full_vacuum_run
) != 0) {
1465 DEBUG(DEBUG_ERR
,(__location__
" Failed to vacuum '%s'\n", name
));
1468 if (repack_limit
!= 0) {
1469 freelist_size
= tdb_freelist_size(ctdb_db
->ltdb
->tdb
);
1470 if (freelist_size
== -1) {
1471 DEBUG(DEBUG_ERR
,(__location__
" Failed to get freelist size for '%s'\n", name
));
1478 * decide if a repack is necessary
1480 if ((repack_limit
== 0 || (uint32_t)freelist_size
< repack_limit
) &&
1481 vdata
->delete_left
< vacuum_limit
)
1487 DEBUG(DEBUG_INFO
,("Repacking %s with %u freelist entries and %u records to delete\n",
1488 name
, freelist_size
, vdata
->delete_left
));
1491 * repack and implicitely get rid of the records we can delete
1493 if (ctdb_repack_tdb(ctdb_db
->ltdb
->tdb
, mem_ctx
, vdata
) != 0) {
1494 DEBUG(DEBUG_ERR
,(__location__
" Failed to repack '%s'\n", name
));
1503 static uint32_t get_vacuum_interval(struct ctdb_db_context
*ctdb_db
)
1505 uint32_t interval
= ctdb_db
->ctdb
->tunable
.vacuum_interval
;
1510 static int vacuum_child_destructor(struct ctdb_vacuum_child_context
*child_ctx
)
1512 double l
= timeval_elapsed(&child_ctx
->start_time
);
1513 struct ctdb_db_context
*ctdb_db
= child_ctx
->vacuum_handle
->ctdb_db
;
1514 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1516 DEBUG(DEBUG_INFO
,("Vacuuming took %.3f seconds for database %s\n", l
, ctdb_db
->db_name
));
1518 if (child_ctx
->child_pid
!= -1) {
1519 ctdb_kill(ctdb
, child_ctx
->child_pid
, SIGKILL
);
1521 /* Bump the number of successful fast-path runs. */
1522 child_ctx
->vacuum_handle
->fast_path_count
++;
1525 DLIST_REMOVE(ctdb
->vacuumers
, child_ctx
);
1527 event_add_timed(ctdb
->ev
, child_ctx
->vacuum_handle
,
1528 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1529 ctdb_vacuum_event
, child_ctx
->vacuum_handle
);
1535 * this event is generated when a vacuum child process times out
1537 static void vacuum_child_timeout(struct event_context
*ev
, struct timed_event
*te
,
1538 struct timeval t
, void *private_data
)
1540 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1542 DEBUG(DEBUG_ERR
,("Vacuuming child process timed out for db %s\n", child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1544 child_ctx
->status
= VACUUM_TIMEOUT
;
1546 talloc_free(child_ctx
);
1551 * this event is generated when a vacuum child process has completed
1553 static void vacuum_child_handler(struct event_context
*ev
, struct fd_event
*fde
,
1554 uint16_t flags
, void *private_data
)
1556 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1560 DEBUG(DEBUG_INFO
,("Vacuuming child process %d finished for db %s\n", child_ctx
->child_pid
, child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1561 child_ctx
->child_pid
= -1;
1563 ret
= read(child_ctx
->fd
[0], &c
, 1);
1564 if (ret
!= 1 || c
!= 0) {
1565 child_ctx
->status
= VACUUM_ERROR
;
1566 DEBUG(DEBUG_ERR
, ("A vacuum child process failed with an error for database %s. ret=%d c=%d\n", child_ctx
->vacuum_handle
->ctdb_db
->db_name
, ret
, c
));
1568 child_ctx
->status
= VACUUM_OK
;
1571 talloc_free(child_ctx
);
1575 * this event is called every time we need to start a new vacuum process
1578 ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
1579 struct timeval t
, void *private_data
)
1581 struct ctdb_vacuum_handle
*vacuum_handle
= talloc_get_type(private_data
, struct ctdb_vacuum_handle
);
1582 struct ctdb_db_context
*ctdb_db
= vacuum_handle
->ctdb_db
;
1583 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1584 struct ctdb_vacuum_child_context
*child_ctx
;
1585 struct tevent_fd
*fde
;
1588 /* we dont vacuum if we are in recovery mode, or db frozen */
1589 if (ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
||
1590 ctdb
->freeze_mode
[ctdb_db
->priority
] != CTDB_FREEZE_NONE
) {
1591 DEBUG(DEBUG_INFO
, ("Not vacuuming %s (%s)\n", ctdb_db
->db_name
,
1592 ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
? "in recovery"
1593 : ctdb
->freeze_mode
[ctdb_db
->priority
] == CTDB_FREEZE_PENDING
1596 event_add_timed(ctdb
->ev
, vacuum_handle
,
1597 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1598 ctdb_vacuum_event
, vacuum_handle
);
1602 child_ctx
= talloc(vacuum_handle
, struct ctdb_vacuum_child_context
);
1603 if (child_ctx
== NULL
) {
1604 DEBUG(DEBUG_CRIT
, (__location__
" Failed to allocate child context for vacuuming of %s\n", ctdb_db
->db_name
));
1605 ctdb_fatal(ctdb
, "Out of memory when crating vacuum child context. Shutting down\n");
1609 ret
= pipe(child_ctx
->fd
);
1611 talloc_free(child_ctx
);
1612 DEBUG(DEBUG_ERR
, ("Failed to create pipe for vacuum child process.\n"));
1613 event_add_timed(ctdb
->ev
, vacuum_handle
,
1614 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1615 ctdb_vacuum_event
, vacuum_handle
);
1619 if (vacuum_handle
->fast_path_count
> ctdb
->tunable
.vacuum_fast_path_count
) {
1620 vacuum_handle
->fast_path_count
= 0;
1623 child_ctx
->child_pid
= ctdb_fork(ctdb
);
1624 if (child_ctx
->child_pid
== (pid_t
)-1) {
1625 close(child_ctx
->fd
[0]);
1626 close(child_ctx
->fd
[1]);
1627 talloc_free(child_ctx
);
1628 DEBUG(DEBUG_ERR
, ("Failed to fork vacuum child process.\n"));
1629 event_add_timed(ctdb
->ev
, vacuum_handle
,
1630 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1631 ctdb_vacuum_event
, vacuum_handle
);
1636 if (child_ctx
->child_pid
== 0) {
1638 bool full_vacuum_run
= false;
1639 close(child_ctx
->fd
[0]);
1641 DEBUG(DEBUG_INFO
,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db
->db_name
));
1642 ctdb_set_process_name("ctdb_vacuum");
1643 if (switch_from_server_to_client(ctdb
, "vacuum-%s", ctdb_db
->db_name
) != 0) {
1644 DEBUG(DEBUG_CRIT
, (__location__
"ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1651 if ((ctdb
->tunable
.vacuum_fast_path_count
> 0) &&
1652 (vacuum_handle
->fast_path_count
== 0))
1654 full_vacuum_run
= true;
1656 cc
= ctdb_vacuum_and_repack_db(ctdb_db
, child_ctx
,
1659 write(child_ctx
->fd
[1], &cc
, 1);
1663 set_close_on_exec(child_ctx
->fd
[0]);
1664 close(child_ctx
->fd
[1]);
1666 child_ctx
->status
= VACUUM_RUNNING
;
1667 child_ctx
->start_time
= timeval_current();
1669 DLIST_ADD(ctdb
->vacuumers
, child_ctx
);
1670 talloc_set_destructor(child_ctx
, vacuum_child_destructor
);
1673 * Clear the fastpath vacuuming list in the parent.
1675 talloc_free(ctdb_db
->delete_queue
);
1676 ctdb_db
->delete_queue
= trbt_create(ctdb_db
, 0);
1677 if (ctdb_db
->delete_queue
== NULL
) {
1678 /* fatal here? ... */
1679 ctdb_fatal(ctdb
, "Out of memory when re-creating vacuum tree "
1680 "in parent context. Shutting down\n");
1683 event_add_timed(ctdb
->ev
, child_ctx
,
1684 timeval_current_ofs(ctdb
->tunable
.vacuum_max_run_time
, 0),
1685 vacuum_child_timeout
, child_ctx
);
1687 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d to child vacuum process\n", child_ctx
->fd
[0]));
1689 fde
= event_add_fd(ctdb
->ev
, child_ctx
, child_ctx
->fd
[0],
1690 EVENT_FD_READ
, vacuum_child_handler
, child_ctx
);
1691 tevent_fd_set_auto_close(fde
);
1693 vacuum_handle
->child_ctx
= child_ctx
;
1694 child_ctx
->vacuum_handle
= vacuum_handle
;
1697 void ctdb_stop_vacuuming(struct ctdb_context
*ctdb
)
1699 /* Simply free them all. */
1700 while (ctdb
->vacuumers
) {
1701 DEBUG(DEBUG_INFO
, ("Aborting vacuuming for %s (%i)\n",
1702 ctdb
->vacuumers
->vacuum_handle
->ctdb_db
->db_name
,
1703 (int)ctdb
->vacuumers
->child_pid
));
1704 /* vacuum_child_destructor kills it, removes from list */
1705 talloc_free(ctdb
->vacuumers
);
1709 /* this function initializes the vacuuming context for a database
1710 * starts the vacuuming events
1712 int ctdb_vacuum_init(struct ctdb_db_context
*ctdb_db
)
1714 if (ctdb_db
->persistent
!= 0) {
1715 DEBUG(DEBUG_ERR
,("Vacuuming is disabled for persistent database %s\n", ctdb_db
->db_name
));
1719 ctdb_db
->vacuum_handle
= talloc(ctdb_db
, struct ctdb_vacuum_handle
);
1720 CTDB_NO_MEMORY(ctdb_db
->ctdb
, ctdb_db
->vacuum_handle
);
1722 ctdb_db
->vacuum_handle
->ctdb_db
= ctdb_db
;
1723 ctdb_db
->vacuum_handle
->fast_path_count
= 0;
1725 event_add_timed(ctdb_db
->ctdb
->ev
, ctdb_db
->vacuum_handle
,
1726 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1727 ctdb_vacuum_event
, ctdb_db
->vacuum_handle
);
1732 static void remove_record_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1733 const struct ctdb_ltdb_header
*hdr
,
1736 struct delete_record_data
*kd
;
1739 hash
= (uint32_t)ctdb_hash(&key
);
1741 DEBUG(DEBUG_DEBUG
, (__location__
1742 " remove_record_from_delete_queue: "
1747 "migrated_with_data[%s]\n",
1748 ctdb_db
->db_name
, ctdb_db
->db_id
,
1750 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1751 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1753 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1755 DEBUG(DEBUG_DEBUG
, (__location__
1756 " remove_record_from_delete_queue: "
1757 "record not in queue (hash[0x%08x])\n.",
1762 if ((kd
->key
.dsize
!= key
.dsize
) ||
1763 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1765 DEBUG(DEBUG_DEBUG
, (__location__
1766 " remove_record_from_delete_queue: "
1767 "hash collision for key with hash[0x%08x] "
1768 "in db[%s] - skipping\n",
1769 hash
, ctdb_db
->db_name
));
1773 DEBUG(DEBUG_DEBUG
, (__location__
1774 " remove_record_from_delete_queue: "
1775 "removing key with hash[0x%08x]\n",
1784 * Insert a record into the ctdb_db context's delete queue,
1785 * handling hash collisions.
1787 static int insert_record_into_delete_queue(struct ctdb_db_context
*ctdb_db
,
1788 const struct ctdb_ltdb_header
*hdr
,
1791 struct delete_record_data
*kd
;
1795 hash
= (uint32_t)ctdb_hash(&key
);
1797 DEBUG(DEBUG_INFO
, (__location__
" schedule for deletion: db[%s] "
1801 "migrated_with_data[%s]\n",
1802 ctdb_db
->db_name
, ctdb_db
->db_id
,
1804 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1805 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1807 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1809 if ((kd
->key
.dsize
!= key
.dsize
) ||
1810 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1813 (__location__
" schedule for deletion: "
1814 "hash collision for key hash [0x%08x]. "
1815 "Skipping the record.\n", hash
));
1819 (__location__
" schedule for deletion: "
1820 "updating entry for key with hash [0x%08x].\n",
1825 ret
= insert_delete_record_data_into_tree(ctdb_db
->ctdb
, ctdb_db
,
1826 ctdb_db
->delete_queue
,
1830 (__location__
" schedule for deletion: error "
1831 "inserting key with hash [0x%08x] into delete queue\n",
1840 * Schedule a record for deletetion.
1841 * Called from the parent context.
1843 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context
*ctdb
,
1846 struct ctdb_control_schedule_for_deletion
*dd
;
1847 struct ctdb_db_context
*ctdb_db
;
1851 dd
= (struct ctdb_control_schedule_for_deletion
*)indata
.dptr
;
1853 ctdb_db
= find_ctdb_db(ctdb
, dd
->db_id
);
1854 if (ctdb_db
== NULL
) {
1855 DEBUG(DEBUG_ERR
, (__location__
" Unknown db id 0x%08x\n",
1860 key
.dsize
= dd
->keylen
;
1863 ret
= insert_record_into_delete_queue(ctdb_db
, &dd
->hdr
, key
);
1868 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context
*ctdb_db
,
1869 const struct ctdb_ltdb_header
*hdr
,
1873 struct ctdb_control_schedule_for_deletion
*dd
;
1877 if (ctdb_db
->ctdb
->ctdbd_pid
== getpid()) {
1878 /* main daemon - directly queue */
1879 ret
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
1884 /* if we dont have a connection to the daemon we can not send
1885 a control. For example sometimes from update_record control child
1888 if (!ctdb_db
->ctdb
->can_send_controls
) {
1893 /* child process: send the main daemon a control */
1894 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + key
.dsize
;
1895 indata
.dptr
= talloc_zero_array(ctdb_db
, uint8_t, indata
.dsize
);
1896 if (indata
.dptr
== NULL
) {
1897 DEBUG(DEBUG_ERR
, (__location__
" out of memory\n"));
1900 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
1901 dd
->db_id
= ctdb_db
->db_id
;
1903 dd
->keylen
= key
.dsize
;
1904 memcpy(dd
->key
, key
.dptr
, key
.dsize
);
1906 ret
= ctdb_control(ctdb_db
->ctdb
,
1909 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
1910 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
1915 NULL
, /* timeout : NULL == wait forever */
1916 NULL
); /* error message */
1918 talloc_free(indata
.dptr
);
1920 if (ret
!= 0 || status
!= 0) {
1921 DEBUG(DEBUG_ERR
, (__location__
" Error sending "
1922 "SCHEDULE_FOR_DELETION "
1932 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1933 const struct ctdb_ltdb_header
*hdr
,
1936 if (ctdb_db
->ctdb
->ctdbd_pid
!= getpid()) {
1938 * Only remove the record from the delete queue if called
1939 * in the main daemon.
1944 remove_record_from_delete_queue(ctdb_db
, hdr
, key
);