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 struct ctdb_context
*ctdb
;
57 struct ctdb_db_context
*ctdb_db
;
58 struct tdb_context
*dest_db
;
59 trbt_tree_t
*delete_list
;
60 struct ctdb_marshall_buffer
**vacuum_fetch_list
;
66 uint32_t added_to_vacuum_fetch_list
;
67 uint32_t added_to_delete_list
;
81 uint32_t remote_error
;
94 /* this structure contains the information for one record to be deleted */
95 struct delete_record_data
{
96 struct ctdb_context
*ctdb
;
97 struct ctdb_db_context
*ctdb_db
;
98 struct ctdb_ltdb_header hdr
;
103 struct delete_records_list
{
104 struct ctdb_marshall_buffer
*records
;
105 struct vacuum_data
*vdata
;
108 static int insert_record_into_delete_queue(struct ctdb_db_context
*ctdb_db
,
109 const struct ctdb_ltdb_header
*hdr
,
113 * Store key and header in a tree, indexed by the key hash.
115 static int insert_delete_record_data_into_tree(struct ctdb_context
*ctdb
,
116 struct ctdb_db_context
*ctdb_db
,
118 const struct ctdb_ltdb_header
*hdr
,
121 struct delete_record_data
*dd
;
125 len
= offsetof(struct delete_record_data
, keydata
) + key
.dsize
;
127 dd
= (struct delete_record_data
*)talloc_size(tree
, len
);
129 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
132 talloc_set_name_const(dd
, "struct delete_record_data");
135 dd
->ctdb_db
= ctdb_db
;
136 dd
->key
.dsize
= key
.dsize
;
137 dd
->key
.dptr
= dd
->keydata
;
138 memcpy(dd
->keydata
, key
.dptr
, key
.dsize
);
142 hash
= ctdb_hash(&key
);
144 trbt_insert32(tree
, hash
, dd
);
149 static int add_record_to_delete_list(struct vacuum_data
*vdata
, TDB_DATA key
,
150 struct ctdb_ltdb_header
*hdr
)
152 struct ctdb_context
*ctdb
= vdata
->ctdb
;
153 struct ctdb_db_context
*ctdb_db
= vdata
->ctdb_db
;
157 hash
= ctdb_hash(&key
);
159 if (trbt_lookup32(vdata
->delete_list
, hash
)) {
160 DEBUG(DEBUG_INFO
, (__location__
" Hash collision when vacuuming, skipping this record.\n"));
164 ret
= insert_delete_record_data_into_tree(ctdb
, ctdb_db
,
171 vdata
->count
.delete_list
.total
++;
177 * Add a record to the list of records to be sent
178 * to their lmaster with VACUUM_FETCH.
180 static int add_record_to_vacuum_fetch_list(struct vacuum_data
*vdata
,
183 struct ctdb_context
*ctdb
= vdata
->ctdb
;
184 struct ctdb_rec_data
*rec
;
187 struct ctdb_marshall_buffer
*vfl
;
189 lmaster
= ctdb_lmaster(ctdb
, &key
);
191 vfl
= vdata
->vacuum_fetch_list
[lmaster
];
193 rec
= ctdb_marshall_record(vfl
, ctdb
->pnn
, key
, NULL
, tdb_null
);
195 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
196 vdata
->traverse_error
= true;
200 old_size
= talloc_get_size(vfl
);
201 vfl
= talloc_realloc_size(NULL
, vfl
, old_size
+ rec
->length
);
203 DEBUG(DEBUG_ERR
,(__location__
" Failed to expand\n"));
204 vdata
->traverse_error
= true;
207 vdata
->vacuum_fetch_list
[lmaster
] = vfl
;
210 memcpy(old_size
+(uint8_t *)vfl
, rec
, rec
->length
);
217 static void ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
218 struct timeval t
, void *private_data
);
220 static int vacuum_record_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
222 struct ctdb_ltdb_header
*header
=
223 (struct ctdb_ltdb_header
*)private_data
;
225 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
229 *header
= *(struct ctdb_ltdb_header
*)data
.dptr
;
235 * traverse function for gathering the records that can be deleted
237 static int vacuum_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
,
240 struct vacuum_data
*vdata
= talloc_get_type(private_data
,
242 struct ctdb_context
*ctdb
= vdata
->ctdb
;
243 struct ctdb_db_context
*ctdb_db
= vdata
->ctdb_db
;
245 struct ctdb_ltdb_header
*hdr
;
248 vdata
->count
.db_traverse
.total
++;
250 lmaster
= ctdb_lmaster(ctdb
, &key
);
251 if (lmaster
>= ctdb
->num_nodes
) {
252 vdata
->count
.db_traverse
.error
++;
253 DEBUG(DEBUG_CRIT
, (__location__
254 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
257 (unsigned)ctdb
->num_nodes
,
258 (unsigned)ctdb_hash(&key
)));
262 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
263 /* it is not a deleted record */
264 vdata
->count
.db_traverse
.skipped
++;
268 hdr
= (struct ctdb_ltdb_header
*)data
.dptr
;
270 if (hdr
->dmaster
!= ctdb
->pnn
) {
271 vdata
->count
.db_traverse
.skipped
++;
276 * Add the record to this process's delete_queue for processing
277 * in the subsequent traverse in the fast vacuum run.
279 res
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
281 vdata
->count
.db_traverse
.error
++;
283 vdata
->count
.db_traverse
.scheduled
++;
290 * traverse the tree of records to delete and marshall them into
293 static int delete_marshall_traverse(void *param
, void *data
)
295 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
296 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
297 struct ctdb_rec_data
*rec
;
300 rec
= ctdb_marshall_record(dd
, recs
->records
->db_id
, dd
->key
, &dd
->hdr
, tdb_null
);
302 DEBUG(DEBUG_ERR
, (__location__
" failed to marshall record\n"));
306 old_size
= talloc_get_size(recs
->records
);
307 recs
->records
= talloc_realloc_size(NULL
, recs
->records
, old_size
+ rec
->length
);
308 if (recs
->records
== NULL
) {
309 DEBUG(DEBUG_ERR
,(__location__
" Failed to expand\n"));
312 recs
->records
->count
++;
313 memcpy(old_size
+(uint8_t *)(recs
->records
), rec
, rec
->length
);
318 * Variant of delete_marshall_traverse() that bumps the
319 * RSN of each traversed record in the database.
321 * This is needed to ensure that when rolling out our
322 * empty record copy before remote deletion, we as the
323 * record's dmaster keep a higher RSN than the non-dmaster
324 * nodes. This is needed to prevent old copies from
325 * resurrection in recoveries.
327 static int delete_marshall_traverse_first(void *param
, void *data
)
329 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
330 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
331 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
332 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
333 struct ctdb_ltdb_header header
;
335 uint32_t hash
= ctdb_hash(&(dd
->key
));
338 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
341 (__location__
" Error getting chainlock on record with "
342 "key hash [0x%08x] on database db[%s].\n",
343 hash
, ctdb_db
->db_name
));
344 recs
->vdata
->count
.delete_list
.skipped
++;
345 recs
->vdata
->count
.delete_list
.left
--;
351 * Verify that the record is still empty, its RSN has not
352 * changed and that we are still its lmaster and dmaster.
355 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
356 vacuum_record_parser
, &header
);
361 if (header
.flags
& CTDB_REC_RO_FLAGS
) {
362 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
363 "on database db[%s] has read-only flags. "
365 hash
, ctdb_db
->db_name
));
369 if (header
.dmaster
!= ctdb
->pnn
) {
370 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
371 "on database db[%s] has been migrated away. "
373 hash
, ctdb_db
->db_name
));
377 if (header
.rsn
!= dd
->hdr
.rsn
) {
378 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
379 "on database db[%s] seems to have been "
380 "migrated away and back again (with empty "
381 "data). skipping.\n",
382 hash
, ctdb_db
->db_name
));
386 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
388 if (lmaster
!= ctdb
->pnn
) {
389 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
390 "delete list (key hash [0x%08x], db[%s]). "
391 "Strange! skipping.\n",
392 hash
, ctdb_db
->db_name
));
397 * Increment the record's RSN to ensure the dmaster (i.e. the current
398 * node) has the highest RSN of the record in the cluster.
399 * This is to prevent old record copies from resurrecting in recoveries
400 * if something should fail during the deletion process.
401 * Note that ctdb_ltdb_store_server() increments the RSN if called
402 * on the record's dmaster.
405 res
= ctdb_ltdb_store(ctdb_db
, dd
->key
, &header
, tdb_null
);
407 DEBUG(DEBUG_ERR
, (__location__
": Failed to store record with "
408 "key hash [0x%08x] on database db[%s].\n",
409 hash
, ctdb_db
->db_name
));
413 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
418 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
420 recs
->vdata
->count
.delete_list
.skipped
++;
421 recs
->vdata
->count
.delete_list
.left
--;
430 return delete_marshall_traverse(param
, data
);
434 * traverse function for the traversal of the delete_queue,
435 * the fast-path vacuuming list.
437 * - If the record has been migrated off the node
438 * or has been revived (filled with data) on the node,
439 * then skip the record.
441 * - If the current node is the record's lmaster and it is
442 * a record that has never been migrated with data, then
443 * delete the record from the local tdb.
445 * - If the current node is the record's lmaster and it has
446 * been migrated with data, then schedule it for the normal
447 * vacuuming procedure (i.e. add it to the delete_list).
449 * - If the current node is NOT the record's lmaster then
450 * add it to the list of records that are to be sent to
451 * the lmaster with the VACUUM_FETCH message.
453 static int delete_queue_traverse(void *param
, void *data
)
455 struct delete_record_data
*dd
=
456 talloc_get_type(data
, struct delete_record_data
);
457 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
458 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
459 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
; /* or dd->ctdb ??? */
461 struct ctdb_ltdb_header header
;
463 uint32_t hash
= ctdb_hash(&(dd
->key
));
465 vdata
->count
.delete_queue
.total
++;
467 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
470 (__location__
" Error getting chainlock on record with "
471 "key hash [0x%08x] on database db[%s].\n",
472 hash
, ctdb_db
->db_name
));
473 vdata
->count
.delete_queue
.error
++;
477 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
478 vacuum_record_parser
, &header
);
483 if (header
.dmaster
!= ctdb
->pnn
) {
484 /* The record has been migrated off the node. Skip. */
488 if (header
.rsn
!= dd
->hdr
.rsn
) {
490 * The record has been migrated off the node and back again.
491 * But not requeued for deletion. Skip it.
497 * We are dmaster, and the record has no data, and it has
498 * not been migrated after it has been queued for deletion.
500 * At this stage, the record could still have been revived locally
501 * and last been written with empty data. This can only be
502 * fixed with the addition of an active or delete flag. (TODO)
505 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
507 if (lmaster
!= ctdb
->pnn
) {
508 res
= add_record_to_vacuum_fetch_list(vdata
, dd
->key
);
512 (__location__
" Error adding record to list "
513 "of records to send to lmaster.\n"));
514 vdata
->count
.delete_queue
.error
++;
516 vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
++;
521 /* use header->flags or dd->hdr.flags ?? */
522 if (dd
->hdr
.flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
) {
523 res
= add_record_to_delete_list(vdata
, dd
->key
, &dd
->hdr
);
527 (__location__
" Error adding record to list "
528 "of records for deletion on lmaster.\n"));
529 vdata
->count
.delete_queue
.error
++;
531 vdata
->count
.delete_queue
.added_to_delete_list
++;
534 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
538 (__location__
" Error deleting record with key "
539 "hash [0x%08x] from local data base db[%s].\n",
540 hash
, ctdb_db
->db_name
));
541 vdata
->count
.delete_queue
.error
++;
546 (__location__
" Deleted record with key hash "
547 "[0x%08x] from local data base db[%s].\n",
548 hash
, ctdb_db
->db_name
));
549 vdata
->count
.delete_queue
.deleted
++;
555 vdata
->count
.delete_queue
.skipped
++;
558 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
564 * Delete the records that we are lmaster and dmaster for and
565 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
568 static int delete_record_traverse(void *param
, void *data
)
570 struct delete_record_data
*dd
=
571 talloc_get_type(data
, struct delete_record_data
);
572 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
573 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
574 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
576 struct ctdb_ltdb_header header
;
578 uint32_t hash
= ctdb_hash(&(dd
->key
));
580 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
583 (__location__
" Error getting chainlock on record with "
584 "key hash [0x%08x] on database db[%s].\n",
585 hash
, ctdb_db
->db_name
));
586 vdata
->count
.delete_list
.local_error
++;
587 vdata
->count
.delete_list
.left
--;
593 * Verify that the record is still empty, its RSN has not
594 * changed and that we are still its lmaster and dmaster.
597 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
598 vacuum_record_parser
, &header
);
603 if (header
.flags
& CTDB_REC_RO_FLAGS
) {
604 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
605 "on database db[%s] has read-only flags. "
607 hash
, ctdb_db
->db_name
));
611 if (header
.dmaster
!= ctdb
->pnn
) {
612 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
613 "on database db[%s] has been migrated away. "
615 hash
, ctdb_db
->db_name
));
619 if (header
.rsn
!= dd
->hdr
.rsn
+ 1) {
621 * The record has been migrated off the node and back again.
622 * But not requeued for deletion. Skip it.
623 * (Note that the first marshall traverse has bumped the RSN
626 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
627 "on database db[%s] seems to have been "
628 "migrated away and back again (with empty "
629 "data). skipping.\n",
630 hash
, ctdb_db
->db_name
));
634 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
636 if (lmaster
!= ctdb
->pnn
) {
637 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
638 "delete list (key hash [0x%08x], db[%s]). "
639 "Strange! skipping.\n",
640 hash
, ctdb_db
->db_name
));
644 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
648 (__location__
" Error deleting record with key hash "
649 "[0x%08x] from local data base db[%s].\n",
650 hash
, ctdb_db
->db_name
));
651 vdata
->count
.delete_list
.local_error
++;
656 (__location__
" Deleted record with key hash [0x%08x] from "
657 "local data base db[%s].\n", hash
, ctdb_db
->db_name
));
659 vdata
->count
.delete_list
.deleted
++;
663 vdata
->count
.delete_list
.skipped
++;
666 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
669 vdata
->count
.delete_list
.left
--;
675 * Traverse the delete_queue.
676 * Records are either deleted directly or filled
677 * into the delete list or the vacuum fetch lists
678 * for further processing.
680 static void ctdb_process_delete_queue(struct ctdb_db_context
*ctdb_db
,
681 struct vacuum_data
*vdata
)
686 ret
= trbt_traversearray32(ctdb_db
->delete_queue
, 1,
687 delete_queue_traverse
, vdata
);
690 DEBUG(DEBUG_ERR
, (__location__
" Error traversing "
691 "the delete queue.\n"));
694 sum
= vdata
->count
.delete_queue
.deleted
695 + vdata
->count
.delete_queue
.skipped
696 + vdata
->count
.delete_queue
.error
697 + vdata
->count
.delete_queue
.added_to_delete_list
698 + vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
;
700 if (vdata
->count
.delete_queue
.total
!= sum
) {
701 DEBUG(DEBUG_ERR
, (__location__
" Inconsistency in fast vacuum "
702 "counts for db[%s]: total[%u] != sum[%u]\n",
704 (unsigned)vdata
->count
.delete_queue
.total
,
708 if (vdata
->count
.delete_queue
.total
> 0) {
711 " fast vacuuming delete_queue traverse statistics: "
720 (unsigned)vdata
->count
.delete_queue
.total
,
721 (unsigned)vdata
->count
.delete_queue
.deleted
,
722 (unsigned)vdata
->count
.delete_queue
.skipped
,
723 (unsigned)vdata
->count
.delete_queue
.error
,
724 (unsigned)vdata
->count
.delete_queue
.added_to_delete_list
,
725 (unsigned)vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
));
732 * read-only traverse of the database, looking for records that
733 * might be able to be vacuumed.
735 * This is not done each time but only every tunable
736 * VacuumFastPathCount times.
738 static void ctdb_vacuum_traverse_db(struct ctdb_db_context
*ctdb_db
,
739 struct vacuum_data
*vdata
)
743 ret
= tdb_traverse_read(ctdb_db
->ltdb
->tdb
, vacuum_traverse
, vdata
);
744 if (ret
== -1 || vdata
->traverse_error
) {
745 DEBUG(DEBUG_ERR
, (__location__
" Traverse error in vacuuming "
746 "'%s'\n", ctdb_db
->db_name
));
749 if (vdata
->count
.db_traverse
.total
> 0) {
752 " full vacuuming db traverse statistics: "
759 (unsigned)vdata
->count
.db_traverse
.total
,
760 (unsigned)vdata
->count
.db_traverse
.skipped
,
761 (unsigned)vdata
->count
.db_traverse
.error
,
762 (unsigned)vdata
->count
.db_traverse
.scheduled
));
769 * Process the vacuum fetch lists:
770 * For records for which we are not the lmaster, tell the lmaster to
773 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context
*ctdb_db
,
774 struct vacuum_data
*vdata
)
777 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
779 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
781 struct ctdb_marshall_buffer
*vfl
= vdata
->vacuum_fetch_list
[i
];
783 if (ctdb
->nodes
[i
]->pnn
== ctdb
->pnn
) {
787 if (vfl
->count
== 0) {
791 DEBUG(DEBUG_INFO
, ("Found %u records for lmaster %u in '%s'\n",
792 vfl
->count
, ctdb
->nodes
[i
]->pnn
,
795 data
.dsize
= talloc_get_size(vfl
);
796 data
.dptr
= (void *)vfl
;
797 if (ctdb_client_send_message(ctdb
, ctdb
->nodes
[i
]->pnn
,
798 CTDB_SRVID_VACUUM_FETCH
,
801 DEBUG(DEBUG_ERR
, (__location__
" Failed to send vacuum "
802 "fetch message to %u\n",
803 ctdb
->nodes
[i
]->pnn
));
811 * Process the delete list:
813 * This is the last step of vacuuming that consistently deletes
814 * those records that have been migrated with data and can hence
815 * not be deleted when leaving a node.
817 * In this step, the lmaster does the final deletion of those empty
818 * records that it is also dmaster for. It has ususally received
819 * at least some of these records previously from the former dmasters
820 * with the vacuum fetch message.
822 * This last step is implemented as a 3-phase process to protect from
823 * races leading to data corruption:
825 * 1) Send the lmaster's copy to all other active nodes with the
826 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
827 * 2) Send the records that could successfully be stored remotely
828 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
829 * control. The remote notes delete their local copy.
830 * 3) The lmaster locally deletes its copies of all records that
831 * could successfully be deleted remotely in step #2.
833 static void ctdb_process_delete_list(struct ctdb_db_context
*ctdb_db
,
834 struct vacuum_data
*vdata
)
837 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
838 struct delete_records_list
*recs
;
840 struct ctdb_node_map
*nodemap
;
841 uint32_t *active_nodes
;
842 int num_active_nodes
;
846 if (vdata
->count
.delete_list
.total
== 0) {
850 tmp_ctx
= talloc_new(vdata
);
851 if (tmp_ctx
== NULL
) {
852 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
856 vdata
->count
.delete_list
.left
= vdata
->count
.delete_list
.total
;
859 * get the list of currently active nodes
862 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
867 DEBUG(DEBUG_ERR
,(__location__
" unable to get node map\n"));
871 active_nodes
= list_of_active_nodes(ctdb
, nodemap
,
872 nodemap
, /* talloc context */
873 false /* include self */);
875 num_active_nodes
= talloc_get_size(active_nodes
)/sizeof(*active_nodes
);
878 * Now delete the records all active nodes in a three-phase process:
879 * 1) send all active remote nodes the current empty copy with this
881 * 2) if all nodes could store the new copy,
882 * tell all the active remote nodes to delete all their copy
883 * 3) if all remote nodes deleted their record copy, delete it locally
888 * Send currently empty record copy to all active nodes for storing.
891 recs
= talloc_zero(tmp_ctx
, struct delete_records_list
);
893 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
896 recs
->records
= (struct ctdb_marshall_buffer
*)
897 talloc_zero_size(recs
,
898 offsetof(struct ctdb_marshall_buffer
, data
));
899 if (recs
->records
== NULL
) {
900 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
903 recs
->records
->db_id
= ctdb_db
->db_id
;
907 * traverse the tree of all records we want to delete and
908 * create a blob we can send to the other nodes.
910 * We call delete_marshall_traverse_first() to bump the
911 * records' RSNs in the database, to ensure we (as dmaster)
912 * keep the highest RSN of the records in the cluster.
914 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
915 delete_marshall_traverse_first
, recs
);
917 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
918 "delete list for first marshalling.\n"));
921 indata
.dsize
= talloc_get_size(recs
->records
);
922 indata
.dptr
= (void *)recs
->records
;
924 for (i
= 0; i
< num_active_nodes
; i
++) {
925 struct ctdb_marshall_buffer
*records
;
926 struct ctdb_rec_data
*rec
;
930 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
931 CTDB_CONTROL_RECEIVE_RECORDS
, 0,
932 indata
, recs
, &outdata
, &res
,
934 if (ret
!= 0 || res
!= 0) {
935 DEBUG(DEBUG_ERR
, ("Error storing record copies on "
936 "node %u: ret[%d] res[%d]\n",
937 active_nodes
[i
], ret
, res
));
942 * outdata contains the list of records coming back
943 * from the node: These are the records that the
944 * remote node could not store. We remove these from
945 * the list to process further.
947 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
948 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
949 while (records
->count
-- > 1) {
950 TDB_DATA reckey
, recdata
;
951 struct ctdb_ltdb_header
*rechdr
;
952 struct delete_record_data
*dd
;
954 reckey
.dptr
= &rec
->data
[0];
955 reckey
.dsize
= rec
->keylen
;
956 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
957 recdata
.dsize
= rec
->datalen
;
959 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
960 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
963 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
964 recdata
.dptr
+= sizeof(*rechdr
);
965 recdata
.dsize
-= sizeof(*rechdr
);
967 dd
= (struct delete_record_data
*)trbt_lookup32(
972 * The other node could not store the record
973 * copy and it is the first node that failed.
974 * So we should remove it from the tree and
978 vdata
->count
.delete_list
.remote_error
++;
979 vdata
->count
.delete_list
.left
--;
981 DEBUG(DEBUG_ERR
, (__location__
" Failed to "
982 "find record with hash 0x%08x coming "
983 "back from RECEIVE_RECORDS "
984 "control in delete list.\n",
985 ctdb_hash(&reckey
)));
986 vdata
->count
.delete_list
.local_error
++;
987 vdata
->count
.delete_list
.left
--;
990 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
994 if (vdata
->count
.delete_list
.left
== 0) {
1000 * Send the remaining records to all active nodes for deletion.
1002 * The lmaster's (i.e. our) copies of these records have been stored
1003 * successfully on the other nodes.
1007 * Create a marshall blob from the remaining list of records to delete.
1010 talloc_free(recs
->records
);
1012 recs
->records
= (struct ctdb_marshall_buffer
*)
1013 talloc_zero_size(recs
,
1014 offsetof(struct ctdb_marshall_buffer
, data
));
1015 if (recs
->records
== NULL
) {
1016 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1019 recs
->records
->db_id
= ctdb_db
->db_id
;
1021 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
1022 delete_marshall_traverse
, recs
);
1024 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
1025 "delete list for second marshalling.\n"));
1028 indata
.dsize
= talloc_get_size(recs
->records
);
1029 indata
.dptr
= (void *)recs
->records
;
1031 for (i
= 0; i
< num_active_nodes
; i
++) {
1032 struct ctdb_marshall_buffer
*records
;
1033 struct ctdb_rec_data
*rec
;
1037 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
1038 CTDB_CONTROL_TRY_DELETE_RECORDS
, 0,
1039 indata
, recs
, &outdata
, &res
,
1041 if (ret
!= 0 || res
!= 0) {
1042 DEBUG(DEBUG_ERR
, ("Failed to delete records on "
1043 "node %u: ret[%d] res[%d]\n",
1044 active_nodes
[i
], ret
, res
));
1049 * outdata contains the list of records coming back
1050 * from the node: These are the records that the
1051 * remote node could not delete. We remove these from
1052 * the list to delete locally.
1054 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
1055 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
1056 while (records
->count
-- > 1) {
1057 TDB_DATA reckey
, recdata
;
1058 struct ctdb_ltdb_header
*rechdr
;
1059 struct delete_record_data
*dd
;
1061 reckey
.dptr
= &rec
->data
[0];
1062 reckey
.dsize
= rec
->keylen
;
1063 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
1064 recdata
.dsize
= rec
->datalen
;
1066 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1067 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
1070 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
1071 recdata
.dptr
+= sizeof(*rechdr
);
1072 recdata
.dsize
-= sizeof(*rechdr
);
1074 dd
= (struct delete_record_data
*)trbt_lookup32(
1076 ctdb_hash(&reckey
));
1079 * The other node could not delete the
1080 * record and it is the first node that
1081 * failed. So we should remove it from
1082 * the tree and update statistics.
1085 vdata
->count
.delete_list
.remote_error
++;
1086 vdata
->count
.delete_list
.left
--;
1088 DEBUG(DEBUG_ERR
, (__location__
" Failed to "
1089 "find record with hash 0x%08x coming "
1090 "back from TRY_DELETE_RECORDS "
1091 "control in delete list.\n",
1092 ctdb_hash(&reckey
)));
1093 vdata
->count
.delete_list
.local_error
++;
1094 vdata
->count
.delete_list
.left
--;
1097 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
1101 if (vdata
->count
.delete_list
.left
== 0) {
1107 * Delete the remaining records locally.
1109 * These records have successfully been deleted on all
1110 * active remote nodes.
1113 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
1114 delete_record_traverse
, vdata
);
1116 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
1117 "delete list for deletion.\n"));
1122 if (vdata
->count
.delete_list
.left
!= 0) {
1123 DEBUG(DEBUG_ERR
, (__location__
" Vaccum db[%s] error: "
1124 "there are %u records left for deletion after "
1125 "processing delete list\n",
1127 (unsigned)vdata
->count
.delete_list
.left
));
1130 sum
= vdata
->count
.delete_list
.deleted
1131 + vdata
->count
.delete_list
.skipped
1132 + vdata
->count
.delete_list
.remote_error
1133 + vdata
->count
.delete_list
.local_error
1134 + vdata
->count
.delete_list
.left
;
1136 if (vdata
->count
.delete_list
.total
!= sum
) {
1137 DEBUG(DEBUG_ERR
, (__location__
" Inconsistency in vacuum "
1138 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1140 (unsigned)vdata
->count
.delete_list
.total
,
1144 if (vdata
->count
.delete_list
.total
> 0) {
1147 " vacuum delete list statistics: "
1156 (unsigned)vdata
->count
.delete_list
.total
,
1157 (unsigned)vdata
->count
.delete_list
.deleted
,
1158 (unsigned)vdata
->count
.delete_list
.skipped
,
1159 (unsigned)vdata
->count
.delete_list
.remote_error
,
1160 (unsigned)vdata
->count
.delete_list
.local_error
,
1161 (unsigned)vdata
->count
.delete_list
.left
));
1165 talloc_free(tmp_ctx
);
1171 * initialize the vacuum_data
1173 static struct vacuum_data
*ctdb_vacuum_init_vacuum_data(
1174 struct ctdb_db_context
*ctdb_db
,
1175 TALLOC_CTX
*mem_ctx
)
1178 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1179 struct vacuum_data
*vdata
;
1181 vdata
= talloc_zero(mem_ctx
, struct vacuum_data
);
1182 if (vdata
== NULL
) {
1183 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1187 vdata
->ctdb
= ctdb_db
->ctdb
;
1188 vdata
->ctdb_db
= ctdb_db
;
1189 vdata
->delete_list
= trbt_create(vdata
, 0);
1190 if (vdata
->delete_list
== NULL
) {
1191 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1195 vdata
->start
= timeval_current();
1197 vdata
->count
.delete_queue
.added_to_delete_list
= 0;
1198 vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
= 0;
1199 vdata
->count
.delete_queue
.deleted
= 0;
1200 vdata
->count
.delete_queue
.skipped
= 0;
1201 vdata
->count
.delete_queue
.error
= 0;
1202 vdata
->count
.delete_queue
.total
= 0;
1203 vdata
->count
.db_traverse
.scheduled
= 0;
1204 vdata
->count
.db_traverse
.skipped
= 0;
1205 vdata
->count
.db_traverse
.error
= 0;
1206 vdata
->count
.db_traverse
.total
= 0;
1207 vdata
->count
.delete_list
.total
= 0;
1208 vdata
->count
.delete_list
.left
= 0;
1209 vdata
->count
.delete_list
.remote_error
= 0;
1210 vdata
->count
.delete_list
.local_error
= 0;
1211 vdata
->count
.delete_list
.skipped
= 0;
1212 vdata
->count
.delete_list
.deleted
= 0;
1214 /* the list needs to be of length num_nodes */
1215 vdata
->vacuum_fetch_list
= talloc_zero_array(vdata
,
1216 struct ctdb_marshall_buffer
*,
1218 if (vdata
->vacuum_fetch_list
== NULL
) {
1219 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1222 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
1223 vdata
->vacuum_fetch_list
[i
] = (struct ctdb_marshall_buffer
*)
1224 talloc_zero_size(vdata
->vacuum_fetch_list
,
1225 offsetof(struct ctdb_marshall_buffer
, data
));
1226 if (vdata
->vacuum_fetch_list
[i
] == NULL
) {
1227 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1231 vdata
->vacuum_fetch_list
[i
]->db_id
= ctdb_db
->db_id
;
1243 * - Always do the fast vacuuming run, which traverses
1244 * the in-memory delete queue: these records have been
1245 * scheduled for deletion.
1246 * - Only if explicitly requested, the database is traversed
1247 * in order to use the traditional heuristics on empty records
1248 * to trigger deletion.
1249 * This is done only every VacuumFastPathCount'th vacuuming run.
1251 * The traverse runs fill two lists:
1253 * - The delete_list:
1254 * This is the list of empty records the current
1255 * node is lmaster and dmaster for. These records are later
1256 * deleted first on other nodes and then locally.
1258 * The fast vacuuming run has a short cut for those records
1259 * that have never been migrated with data: these records
1260 * are immediately deleted locally, since they have left
1261 * no trace on other nodes.
1263 * - The vacuum_fetch lists
1264 * (one for each other lmaster node):
1265 * The records in this list are sent for deletion to
1266 * their lmaster in a bulk VACUUM_FETCH message.
1268 * The lmaster then migrates all these records to itelf
1269 * so that they can be vacuumed there.
1271 * This executes in the child context.
1273 static int ctdb_vacuum_db(struct ctdb_db_context
*ctdb_db
,
1274 bool full_vacuum_run
)
1276 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1278 struct vacuum_data
*vdata
;
1279 TALLOC_CTX
*tmp_ctx
;
1281 DEBUG(DEBUG_INFO
, (__location__
" Entering %s vacuum run for db "
1282 "%s db_id[0x%08x]\n",
1283 full_vacuum_run
? "full" : "fast",
1284 ctdb_db
->db_name
, ctdb_db
->db_id
));
1286 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &ctdb
->vnn_map
);
1288 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from local node\n"));
1292 pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1294 DEBUG(DEBUG_ERR
, ("Unable to get pnn from local node\n"));
1300 tmp_ctx
= talloc_new(ctdb_db
);
1301 if (tmp_ctx
== NULL
) {
1302 DEBUG(DEBUG_ERR
, ("Out of memory!\n"));
1306 vdata
= ctdb_vacuum_init_vacuum_data(ctdb_db
, tmp_ctx
);
1307 if (vdata
== NULL
) {
1308 talloc_free(tmp_ctx
);
1312 if (full_vacuum_run
) {
1313 ctdb_vacuum_traverse_db(ctdb_db
, vdata
);
1316 ctdb_process_delete_queue(ctdb_db
, vdata
);
1318 ctdb_process_vacuum_fetch_lists(ctdb_db
, vdata
);
1320 ctdb_process_delete_list(ctdb_db
, vdata
);
1322 talloc_free(tmp_ctx
);
1324 /* this ensures we run our event queue */
1325 ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1331 * repack and vaccum a db
1332 * called from the child context
1334 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context
*ctdb_db
,
1335 TALLOC_CTX
*mem_ctx
,
1336 bool full_vacuum_run
)
1338 uint32_t repack_limit
= ctdb_db
->ctdb
->tunable
.repack_limit
;
1339 const char *name
= ctdb_db
->db_name
;
1340 int freelist_size
= 0;
1343 if (ctdb_vacuum_db(ctdb_db
, full_vacuum_run
) != 0) {
1344 DEBUG(DEBUG_ERR
,(__location__
" Failed to vacuum '%s'\n", name
));
1347 if (repack_limit
!= 0) {
1348 freelist_size
= tdb_freelist_size(ctdb_db
->ltdb
->tdb
);
1349 if (freelist_size
== -1) {
1350 DEBUG(DEBUG_ERR
,(__location__
" Failed to get freelist size for '%s'\n", name
));
1356 * decide if a repack is necessary
1358 if ((repack_limit
== 0 || (uint32_t)freelist_size
< repack_limit
))
1363 DEBUG(DEBUG_INFO
, ("Repacking %s with %u freelist entries\n",
1364 name
, freelist_size
));
1366 ret
= tdb_repack(ctdb_db
->ltdb
->tdb
);
1368 DEBUG(DEBUG_ERR
,(__location__
" Failed to repack '%s'\n", name
));
1375 static uint32_t get_vacuum_interval(struct ctdb_db_context
*ctdb_db
)
1377 uint32_t interval
= ctdb_db
->ctdb
->tunable
.vacuum_interval
;
1382 static int vacuum_child_destructor(struct ctdb_vacuum_child_context
*child_ctx
)
1384 double l
= timeval_elapsed(&child_ctx
->start_time
);
1385 struct ctdb_db_context
*ctdb_db
= child_ctx
->vacuum_handle
->ctdb_db
;
1386 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1388 DEBUG(DEBUG_INFO
,("Vacuuming took %.3f seconds for database %s\n", l
, ctdb_db
->db_name
));
1390 if (child_ctx
->child_pid
!= -1) {
1391 ctdb_kill(ctdb
, child_ctx
->child_pid
, SIGKILL
);
1393 /* Bump the number of successful fast-path runs. */
1394 child_ctx
->vacuum_handle
->fast_path_count
++;
1397 DLIST_REMOVE(ctdb
->vacuumers
, child_ctx
);
1399 event_add_timed(ctdb
->ev
, child_ctx
->vacuum_handle
,
1400 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1401 ctdb_vacuum_event
, child_ctx
->vacuum_handle
);
1407 * this event is generated when a vacuum child process times out
1409 static void vacuum_child_timeout(struct event_context
*ev
, struct timed_event
*te
,
1410 struct timeval t
, void *private_data
)
1412 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1414 DEBUG(DEBUG_ERR
,("Vacuuming child process timed out for db %s\n", child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1416 child_ctx
->status
= VACUUM_TIMEOUT
;
1418 talloc_free(child_ctx
);
1423 * this event is generated when a vacuum child process has completed
1425 static void vacuum_child_handler(struct event_context
*ev
, struct fd_event
*fde
,
1426 uint16_t flags
, void *private_data
)
1428 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1432 DEBUG(DEBUG_INFO
,("Vacuuming child process %d finished for db %s\n", child_ctx
->child_pid
, child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1433 child_ctx
->child_pid
= -1;
1435 ret
= read(child_ctx
->fd
[0], &c
, 1);
1436 if (ret
!= 1 || c
!= 0) {
1437 child_ctx
->status
= VACUUM_ERROR
;
1438 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
));
1440 child_ctx
->status
= VACUUM_OK
;
1443 talloc_free(child_ctx
);
1447 * this event is called every time we need to start a new vacuum process
1450 ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
1451 struct timeval t
, void *private_data
)
1453 struct ctdb_vacuum_handle
*vacuum_handle
= talloc_get_type(private_data
, struct ctdb_vacuum_handle
);
1454 struct ctdb_db_context
*ctdb_db
= vacuum_handle
->ctdb_db
;
1455 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1456 struct ctdb_vacuum_child_context
*child_ctx
;
1457 struct tevent_fd
*fde
;
1460 /* we dont vacuum if we are in recovery mode, or db frozen */
1461 if (ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
||
1462 ctdb
->freeze_mode
[ctdb_db
->priority
] != CTDB_FREEZE_NONE
) {
1463 DEBUG(DEBUG_INFO
, ("Not vacuuming %s (%s)\n", ctdb_db
->db_name
,
1464 ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
? "in recovery"
1465 : ctdb
->freeze_mode
[ctdb_db
->priority
] == CTDB_FREEZE_PENDING
1468 event_add_timed(ctdb
->ev
, vacuum_handle
,
1469 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1470 ctdb_vacuum_event
, vacuum_handle
);
1474 child_ctx
= talloc(vacuum_handle
, struct ctdb_vacuum_child_context
);
1475 if (child_ctx
== NULL
) {
1476 DEBUG(DEBUG_CRIT
, (__location__
" Failed to allocate child context for vacuuming of %s\n", ctdb_db
->db_name
));
1477 ctdb_fatal(ctdb
, "Out of memory when crating vacuum child context. Shutting down\n");
1481 ret
= pipe(child_ctx
->fd
);
1483 talloc_free(child_ctx
);
1484 DEBUG(DEBUG_ERR
, ("Failed to create pipe for vacuum child process.\n"));
1485 event_add_timed(ctdb
->ev
, vacuum_handle
,
1486 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1487 ctdb_vacuum_event
, vacuum_handle
);
1491 if (vacuum_handle
->fast_path_count
> ctdb
->tunable
.vacuum_fast_path_count
) {
1492 vacuum_handle
->fast_path_count
= 0;
1495 child_ctx
->child_pid
= ctdb_fork(ctdb
);
1496 if (child_ctx
->child_pid
== (pid_t
)-1) {
1497 close(child_ctx
->fd
[0]);
1498 close(child_ctx
->fd
[1]);
1499 talloc_free(child_ctx
);
1500 DEBUG(DEBUG_ERR
, ("Failed to fork vacuum child process.\n"));
1501 event_add_timed(ctdb
->ev
, vacuum_handle
,
1502 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1503 ctdb_vacuum_event
, vacuum_handle
);
1508 if (child_ctx
->child_pid
== 0) {
1510 bool full_vacuum_run
= false;
1511 close(child_ctx
->fd
[0]);
1513 DEBUG(DEBUG_INFO
,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db
->db_name
));
1514 ctdb_set_process_name("ctdb_vacuum");
1515 if (switch_from_server_to_client(ctdb
, "vacuum-%s", ctdb_db
->db_name
) != 0) {
1516 DEBUG(DEBUG_CRIT
, (__location__
"ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1520 if ((ctdb
->tunable
.vacuum_fast_path_count
> 0) &&
1521 (vacuum_handle
->fast_path_count
== 0))
1523 full_vacuum_run
= true;
1525 cc
= ctdb_vacuum_and_repack_db(ctdb_db
, child_ctx
,
1528 write(child_ctx
->fd
[1], &cc
, 1);
1532 set_close_on_exec(child_ctx
->fd
[0]);
1533 close(child_ctx
->fd
[1]);
1535 child_ctx
->status
= VACUUM_RUNNING
;
1536 child_ctx
->start_time
= timeval_current();
1538 DLIST_ADD(ctdb
->vacuumers
, child_ctx
);
1539 talloc_set_destructor(child_ctx
, vacuum_child_destructor
);
1542 * Clear the fastpath vacuuming list in the parent.
1544 talloc_free(ctdb_db
->delete_queue
);
1545 ctdb_db
->delete_queue
= trbt_create(ctdb_db
, 0);
1546 if (ctdb_db
->delete_queue
== NULL
) {
1547 /* fatal here? ... */
1548 ctdb_fatal(ctdb
, "Out of memory when re-creating vacuum tree "
1549 "in parent context. Shutting down\n");
1552 event_add_timed(ctdb
->ev
, child_ctx
,
1553 timeval_current_ofs(ctdb
->tunable
.vacuum_max_run_time
, 0),
1554 vacuum_child_timeout
, child_ctx
);
1556 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d to child vacuum process\n", child_ctx
->fd
[0]));
1558 fde
= event_add_fd(ctdb
->ev
, child_ctx
, child_ctx
->fd
[0],
1559 EVENT_FD_READ
, vacuum_child_handler
, child_ctx
);
1560 tevent_fd_set_auto_close(fde
);
1562 vacuum_handle
->child_ctx
= child_ctx
;
1563 child_ctx
->vacuum_handle
= vacuum_handle
;
1566 void ctdb_stop_vacuuming(struct ctdb_context
*ctdb
)
1568 /* Simply free them all. */
1569 while (ctdb
->vacuumers
) {
1570 DEBUG(DEBUG_INFO
, ("Aborting vacuuming for %s (%i)\n",
1571 ctdb
->vacuumers
->vacuum_handle
->ctdb_db
->db_name
,
1572 (int)ctdb
->vacuumers
->child_pid
));
1573 /* vacuum_child_destructor kills it, removes from list */
1574 talloc_free(ctdb
->vacuumers
);
1578 /* this function initializes the vacuuming context for a database
1579 * starts the vacuuming events
1581 int ctdb_vacuum_init(struct ctdb_db_context
*ctdb_db
)
1583 if (ctdb_db
->persistent
!= 0) {
1584 DEBUG(DEBUG_ERR
,("Vacuuming is disabled for persistent database %s\n", ctdb_db
->db_name
));
1588 ctdb_db
->vacuum_handle
= talloc(ctdb_db
, struct ctdb_vacuum_handle
);
1589 CTDB_NO_MEMORY(ctdb_db
->ctdb
, ctdb_db
->vacuum_handle
);
1591 ctdb_db
->vacuum_handle
->ctdb_db
= ctdb_db
;
1592 ctdb_db
->vacuum_handle
->fast_path_count
= 0;
1594 event_add_timed(ctdb_db
->ctdb
->ev
, ctdb_db
->vacuum_handle
,
1595 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1596 ctdb_vacuum_event
, ctdb_db
->vacuum_handle
);
1601 static void remove_record_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1602 const struct ctdb_ltdb_header
*hdr
,
1605 struct delete_record_data
*kd
;
1608 hash
= (uint32_t)ctdb_hash(&key
);
1610 DEBUG(DEBUG_DEBUG
, (__location__
1611 " remove_record_from_delete_queue: "
1616 "migrated_with_data[%s]\n",
1617 ctdb_db
->db_name
, ctdb_db
->db_id
,
1619 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1620 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1622 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1624 DEBUG(DEBUG_DEBUG
, (__location__
1625 " remove_record_from_delete_queue: "
1626 "record not in queue (hash[0x%08x])\n.",
1631 if ((kd
->key
.dsize
!= key
.dsize
) ||
1632 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1634 DEBUG(DEBUG_DEBUG
, (__location__
1635 " remove_record_from_delete_queue: "
1636 "hash collision for key with hash[0x%08x] "
1637 "in db[%s] - skipping\n",
1638 hash
, ctdb_db
->db_name
));
1642 DEBUG(DEBUG_DEBUG
, (__location__
1643 " remove_record_from_delete_queue: "
1644 "removing key with hash[0x%08x]\n",
1653 * Insert a record into the ctdb_db context's delete queue,
1654 * handling hash collisions.
1656 static int insert_record_into_delete_queue(struct ctdb_db_context
*ctdb_db
,
1657 const struct ctdb_ltdb_header
*hdr
,
1660 struct delete_record_data
*kd
;
1664 hash
= (uint32_t)ctdb_hash(&key
);
1666 DEBUG(DEBUG_INFO
, (__location__
" schedule for deletion: db[%s] "
1670 "migrated_with_data[%s]\n",
1671 ctdb_db
->db_name
, ctdb_db
->db_id
,
1673 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1674 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1676 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1678 if ((kd
->key
.dsize
!= key
.dsize
) ||
1679 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1682 (__location__
" schedule for deletion: "
1683 "hash collision for key hash [0x%08x]. "
1684 "Skipping the record.\n", hash
));
1688 (__location__
" schedule for deletion: "
1689 "updating entry for key with hash [0x%08x].\n",
1694 ret
= insert_delete_record_data_into_tree(ctdb_db
->ctdb
, ctdb_db
,
1695 ctdb_db
->delete_queue
,
1699 (__location__
" schedule for deletion: error "
1700 "inserting key with hash [0x%08x] into delete queue\n",
1709 * Schedule a record for deletetion.
1710 * Called from the parent context.
1712 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context
*ctdb
,
1715 struct ctdb_control_schedule_for_deletion
*dd
;
1716 struct ctdb_db_context
*ctdb_db
;
1720 dd
= (struct ctdb_control_schedule_for_deletion
*)indata
.dptr
;
1722 ctdb_db
= find_ctdb_db(ctdb
, dd
->db_id
);
1723 if (ctdb_db
== NULL
) {
1724 DEBUG(DEBUG_ERR
, (__location__
" Unknown db id 0x%08x\n",
1729 key
.dsize
= dd
->keylen
;
1732 ret
= insert_record_into_delete_queue(ctdb_db
, &dd
->hdr
, key
);
1737 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context
*ctdb_db
,
1738 const struct ctdb_ltdb_header
*hdr
,
1742 struct ctdb_control_schedule_for_deletion
*dd
;
1746 if (ctdb_db
->ctdb
->ctdbd_pid
== getpid()) {
1747 /* main daemon - directly queue */
1748 ret
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
1753 /* if we dont have a connection to the daemon we can not send
1754 a control. For example sometimes from update_record control child
1757 if (!ctdb_db
->ctdb
->can_send_controls
) {
1762 /* child process: send the main daemon a control */
1763 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + key
.dsize
;
1764 indata
.dptr
= talloc_zero_array(ctdb_db
, uint8_t, indata
.dsize
);
1765 if (indata
.dptr
== NULL
) {
1766 DEBUG(DEBUG_ERR
, (__location__
" out of memory\n"));
1769 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
1770 dd
->db_id
= ctdb_db
->db_id
;
1772 dd
->keylen
= key
.dsize
;
1773 memcpy(dd
->key
, key
.dptr
, key
.dsize
);
1775 ret
= ctdb_control(ctdb_db
->ctdb
,
1778 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
1779 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
1784 NULL
, /* timeout : NULL == wait forever */
1785 NULL
); /* error message */
1787 talloc_free(indata
.dptr
);
1789 if (ret
!= 0 || status
!= 0) {
1790 DEBUG(DEBUG_ERR
, (__location__
" Error sending "
1791 "SCHEDULE_FOR_DELETION "
1801 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1802 const struct ctdb_ltdb_header
*hdr
,
1805 if (ctdb_db
->ctdb
->ctdbd_pid
!= getpid()) {
1807 * Only remove the record from the delete queue if called
1808 * in the main daemon.
1813 remove_record_from_delete_queue(ctdb_db
, hdr
, key
);