4 Copyright (C) Ronnie Sahlberg 2009
5 Copyright (C) Michael Adam 2010-2011
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/>.
23 #include "lib/tevent/tevent.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "system/dir.h"
28 #include "../include/ctdb_private.h"
30 #include "lib/util/dlinklist.h"
31 #include "lib/tevent/tevent.h"
32 #include "../include/ctdb_private.h"
33 #include "../common/rb_tree.h"
35 #define TIMELIMIT() timeval_current_ofs(10, 0)
37 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
39 struct ctdb_vacuum_child_context {
40 struct ctdb_vacuum_child_context *next, *prev;
41 struct ctdb_vacuum_handle *vacuum_handle;
42 /* fd child writes status to */
45 enum vacuum_child_status status;
46 struct timeval start_time;
49 struct ctdb_vacuum_handle {
50 struct ctdb_db_context *ctdb_db;
51 struct ctdb_vacuum_child_context *child_ctx;
52 uint32_t fast_path_count;
56 /* a list of records to possibly delete */
58 uint32_t vacuum_limit;
59 uint32_t repack_limit;
60 struct ctdb_context *ctdb;
61 struct ctdb_db_context *ctdb_db;
62 struct tdb_context *dest_db;
63 trbt_tree_t *delete_tree;
64 uint32_t delete_count;
65 struct ctdb_marshall_buffer **list;
72 uint32_t fast_added_to_vacuum_fetch_list;
73 uint32_t fast_added_to_delete_tree;
74 uint32_t fast_deleted;
75 uint32_t fast_skipped;
78 uint32_t full_added_to_vacuum_fetch_list;
79 uint32_t full_added_to_delete_tree;
80 uint32_t full_skipped;
85 /* tuning information stored for every db */
86 struct vacuum_tuning_data {
87 uint32_t last_num_repack;
88 uint32_t last_num_empty;
89 uint32_t last_interval;
90 uint32_t new_interval;
91 struct timeval last_start;
95 /* this structure contains the information for one record to be deleted */
96 struct delete_record_data {
97 struct ctdb_context *ctdb;
98 struct ctdb_db_context *ctdb_db;
99 struct ctdb_ltdb_header hdr;
103 struct delete_records_list {
104 struct ctdb_marshall_buffer *records;
108 * Store key and header in a tree, indexed by the key hash.
110 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
111 struct ctdb_db_context *ctdb_db,
113 const struct ctdb_ltdb_header *hdr,
116 struct delete_record_data *dd;
119 dd = talloc_zero(tree, struct delete_record_data);
121 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
126 dd->ctdb_db = ctdb_db;
127 dd->key.dsize = key.dsize;
128 dd->key.dptr = talloc_memdup(dd, key.dptr, key.dsize);
129 if (dd->key.dptr == NULL) {
130 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
136 hash = ctdb_hash(&key);
138 trbt_insert32(tree, hash, dd);
143 static int add_record_to_delete_tree(struct vacuum_data *vdata, TDB_DATA key,
144 struct ctdb_ltdb_header *hdr)
146 struct ctdb_context *ctdb = vdata->ctdb;
147 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
151 hash = ctdb_hash(&key);
153 if (trbt_lookup32(vdata->delete_tree, hash)) {
154 DEBUG(DEBUG_INFO, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
158 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
165 vdata->delete_count++;
171 * Add a record to the list of records to be sent
172 * to their lmaster with VACUUM_FETCH.
174 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
177 struct ctdb_context *ctdb = vdata->ctdb;
178 struct ctdb_rec_data *rec;
182 lmaster = ctdb_lmaster(ctdb, &key);
184 rec = ctdb_marshall_record(vdata->list[lmaster], ctdb->pnn, key, NULL, tdb_null);
186 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
187 vdata->traverse_error = true;
191 old_size = talloc_get_size(vdata->list[lmaster]);
192 vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster],
193 old_size + rec->length);
194 if (vdata->list[lmaster] == NULL) {
195 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
196 vdata->traverse_error = true;
200 vdata->list[lmaster]->count++;
201 memcpy(old_size+(uint8_t *)vdata->list[lmaster], rec, rec->length);
210 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
211 struct timeval t, void *private_data);
215 * traverse function for gathering the records that can be deleted
217 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
219 struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
220 struct ctdb_context *ctdb = vdata->ctdb;
222 struct ctdb_ltdb_header *hdr;
227 lmaster = ctdb_lmaster(ctdb, &key);
228 if (lmaster >= ctdb->num_nodes) {
230 DEBUG(DEBUG_CRIT, (__location__
231 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
234 (unsigned)ctdb->num_nodes,
235 (unsigned)ctdb_hash(&key)));
239 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
240 /* it is not a deleted record */
241 vdata->full_skipped++;
245 hdr = (struct ctdb_ltdb_header *)data.dptr;
247 if (hdr->dmaster != ctdb->pnn) {
248 vdata->full_skipped++;
252 if (lmaster == ctdb->pnn) {
254 * We are both lmaster and dmaster, and the record is empty.
255 * So we should be able to delete it.
257 res = add_record_to_delete_tree(vdata, key, hdr);
261 vdata->full_added_to_delete_tree++;
265 * We are not lmaster.
266 * Add the record to the blob ready to send to the nodes.
268 res = add_record_to_vacuum_fetch_list(vdata, key);
272 vdata->full_added_to_vacuum_fetch_list++;
280 * traverse the tree of records to delete and marshall them into
283 static int delete_traverse(void *param, void *data)
285 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
286 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
287 struct ctdb_rec_data *rec;
290 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
292 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
296 old_size = talloc_get_size(recs->records);
297 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
298 if (recs->records == NULL) {
299 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
302 recs->records->count++;
303 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
308 * traverse function for the traversal of the delete_queue,
309 * the fast-path vacuuming list.
311 * - If the record has been migrated off the node
312 * or has been revived (filled with data) on the node,
313 * then skip the record.
315 * - If the current node is the record's lmaster and it is
316 * a record that has never been migrated with data, then
317 * delete the record from the local tdb.
319 * - If the current node is the record's lmaster and it has
320 * been migrated with data, then schedule it for the normal
321 * vacuuming procedure (i.e. add it to the delete_list).
323 * - If the current node is NOT the record's lmaster then
324 * add it to the list of records that are to be sent to
325 * the lmaster with the VACUUM_FETCH message.
327 static int delete_queue_traverse(void *param, void *data)
329 struct delete_record_data *dd =
330 talloc_get_type(data, struct delete_record_data);
331 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
332 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
333 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
335 struct ctdb_ltdb_header *header;
341 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
343 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
348 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
349 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
350 /* Does not exist or not a ctdb record. Skip. */
354 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
355 /* The record has been recycled (filled with data). Skip. */
359 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
361 if (header->dmaster != ctdb->pnn) {
362 /* The record has been migrated off the node. Skip. */
367 if (header->rsn != dd->hdr.rsn) {
369 * The record has been migrated off the node and back again.
370 * But not requeued for deletion. Skip it.
376 * We are dmaster, and the record has no data, and it has
377 * not been migrated after it has been queued for deletion.
379 * At this stage, the record could still have been revived locally
380 * and last been written with empty data. This can only be
381 * fixed with the addition of an active or delete flag. (TODO)
384 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
386 if (lmaster != ctdb->pnn) {
387 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
391 (__location__ " Error adding record to list "
392 "of records to send to lmaster.\n"));
395 vdata->fast_added_to_vacuum_fetch_list++;
400 /* use header->flags or dd->hdr.flags ?? */
401 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
402 res = add_record_to_delete_tree(vdata, dd->key, &dd->hdr);
406 (__location__ " Error adding record to list "
407 "of records for deletion on lmaster.\n"));
410 vdata->fast_added_to_delete_tree++;
413 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
417 (__location__ " Error deleting record from local "
421 vdata->fast_deleted++;
428 vdata->fast_skipped++;
431 if (tdb_data.dptr != NULL) {
434 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
441 * - Always do the fast vacuuming run, which traverses
442 * the in-memory delete queue: these records have been
443 * scheduled for deletion.
444 * - Only if explicitly requested, the database is traversed
445 * in order to use the traditional heuristics on empty records
446 * to trigger deletion.
447 * This is done only every VacuumFastPathCount'th vacuuming run.
449 * The traverse runs fill two lists:
452 * This is the list of empty records the current
453 * node is lmaster and dmaster for. These records are later
454 * deleted first on other nodes and then locally.
456 * The fast vacuuming run has a short cut for those records
457 * that have never been migrated with data: these records
458 * are immediately deleted locally, since they have left
459 * no trace on other nodes.
461 * - The vacuum_fetch lists
462 * (one for each other lmaster node):
463 * The records in this list are sent for deletion to
464 * their lmaster in a bulk VACUUM_FETCH message.
466 * The lmaster then migrates all these records to itelf
467 * so that they can be vacuumed there.
469 * This executes in the child context.
471 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
472 struct vacuum_data *vdata,
473 bool full_vacuum_run)
475 struct ctdb_context *ctdb = ctdb_db->ctdb;
476 const char *name = ctdb_db->db_name;
479 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
480 "%s db_id[0x%08x]\n",
481 full_vacuum_run ? "full" : "fast",
482 ctdb_db->db_name, ctdb_db->db_id));
484 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
486 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
490 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
492 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
498 vdata->fast_added_to_delete_tree = 0;
499 vdata->fast_added_to_vacuum_fetch_list = 0;
500 vdata->fast_deleted = 0;
501 vdata->fast_skipped = 0;
502 vdata->fast_error = 0;
503 vdata->fast_total = 0;
504 vdata->full_added_to_delete_tree = 0;
505 vdata->full_added_to_vacuum_fetch_list = 0;
506 vdata->full_skipped = 0;
507 vdata->full_error = 0;
508 vdata->full_total = 0;
510 /* the list needs to be of length num_nodes */
511 vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->num_nodes);
512 if (vdata->list == NULL) {
513 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
516 for (i = 0; i < ctdb->num_nodes; i++) {
517 vdata->list[i] = (struct ctdb_marshall_buffer *)
518 talloc_zero_size(vdata->list,
519 offsetof(struct ctdb_marshall_buffer, data));
520 if (vdata->list[i] == NULL) {
521 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
524 vdata->list[i]->db_id = ctdb_db->db_id;
528 * Traverse the delete_queue.
529 * This builds the same lists as the db traverse.
531 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
533 if (vdata->fast_total > 0) {
536 " fast vacuuming delete_queue traverse statistics: "
545 (unsigned)vdata->fast_total,
546 (unsigned)vdata->fast_deleted,
547 (unsigned)vdata->fast_skipped,
548 (unsigned)vdata->fast_error,
549 (unsigned)vdata->fast_added_to_delete_tree,
550 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
554 * read-only traverse of the database, looking for records that
555 * might be able to be vacuumed.
557 * This is not done each time but only every tunable
558 * VacuumFastPathCount times.
560 if (full_vacuum_run) {
561 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
562 if (ret == -1 || vdata->traverse_error) {
563 DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
566 if (vdata->full_total > 0) {
569 " full vacuuming db traverse statistics: "
577 (unsigned)vdata->full_total,
578 (unsigned)vdata->full_skipped,
579 (unsigned)vdata->full_error,
580 (unsigned)vdata->full_added_to_delete_tree,
581 (unsigned)vdata->full_added_to_vacuum_fetch_list));
586 * For records where we are not the lmaster,
587 * tell the lmaster to fetch the record.
589 for (i = 0; i < ctdb->num_nodes; i++) {
592 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
596 if (vdata->list[i]->count == 0) {
600 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
601 vdata->list[i]->count, ctdb->nodes[i]->pnn,
604 data.dsize = talloc_get_size(vdata->list[i]);
605 data.dptr = (void *)vdata->list[i];
606 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
607 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
608 "fetch message to %u\n",
609 ctdb->nodes[i]->pnn));
614 /* Process all records we can delete (if any) */
615 if (vdata->delete_count > 0) {
616 struct delete_records_list *recs;
617 TDB_DATA indata, outdata;
619 struct ctdb_node_map *nodemap;
620 uint32_t *active_nodes;
621 int num_active_nodes;
623 recs = talloc_zero(vdata, struct delete_records_list);
625 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
628 recs->records = (struct ctdb_marshall_buffer *)
629 talloc_zero_size(vdata,
630 offsetof(struct ctdb_marshall_buffer, data));
631 if (recs->records == NULL) {
632 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
635 recs->records->db_id = ctdb_db->db_id;
638 * traverse the tree of all records we want to delete and
639 * create a blob we can send to the other nodes.
641 trbt_traversearray32(vdata->delete_tree, 1, delete_traverse, recs);
643 indata.dsize = talloc_get_size(recs->records);
644 indata.dptr = (void *)recs->records;
647 * now tell all the active nodes to delete all these records
651 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
653 recs, /* talloc context */
656 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
660 active_nodes = list_of_active_nodes(ctdb, nodemap,
661 nodemap, /* talloc context */
662 false /* include self */);
664 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
666 for (i = 0; i < num_active_nodes; i++) {
667 struct ctdb_marshall_buffer *records;
668 struct ctdb_rec_data *rec;
670 ret = ctdb_control(ctdb, active_nodes[i], 0,
671 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
672 indata, recs, &outdata, &res,
674 if (ret != 0 || res != 0) {
675 DEBUG(DEBUG_ERR, ("Failed to delete records on "
676 "node %u: ret[%d] res[%d]\n",
677 active_nodes[i], ret, res));
682 * outdata countains the list of records coming back
683 * from the node which the node could not delete
685 records = (struct ctdb_marshall_buffer *)outdata.dptr;
686 rec = (struct ctdb_rec_data *)&records->data[0];
687 while (records->count-- > 1) {
688 TDB_DATA reckey, recdata;
689 struct ctdb_ltdb_header *rechdr;
691 reckey.dptr = &rec->data[0];
692 reckey.dsize = rec->keylen;
693 recdata.dptr = &rec->data[reckey.dsize];
694 recdata.dsize = rec->datalen;
696 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
697 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
700 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
701 recdata.dptr += sizeof(*rechdr);
702 recdata.dsize -= sizeof(*rechdr);
705 * that other node couldnt delete the record
706 * so we should delete it and thereby remove it from the tree
708 talloc_free(trbt_lookup32(vdata->delete_tree, ctdb_hash(&reckey)));
710 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
714 /* free nodemap and active_nodes */
715 talloc_free(nodemap);
718 * The only records remaining in the tree would be those
719 * records where all other nodes could successfully
720 * delete them, so we can safely delete them on the
721 * lmaster as well. Deletion implictely happens while
722 * we repack the database. The repack algorithm revisits
723 * the tree in order to find the records that don't need
724 * to be copied / repacked.
728 /* this ensures we run our event queue */
729 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
736 * traverse function for repacking
738 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
740 struct vacuum_data *vdata = (struct vacuum_data *)private;
743 uint32_t hash = ctdb_hash(&key);
744 struct delete_record_data *kd;
746 * check if we can ignore this record because it's in the delete_tree
748 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_tree, hash);
750 * there might be hash collisions so we have to compare the keys here to be sure
752 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
753 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
755 * we have to check if the record hasn't changed in the meantime in order to
756 * savely remove it from the database
758 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
759 hdr->dmaster == kd->ctdb->pnn &&
760 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
761 kd->hdr.rsn == hdr->rsn) {
767 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
768 vdata->traverse_error = true;
778 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
780 struct tdb_context *tmp_db;
782 if (tdb_transaction_start(tdb) != 0) {
783 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
787 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
788 TDB_INTERNAL|TDB_DISALLOW_NESTING,
790 if (tmp_db == NULL) {
791 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
792 tdb_transaction_cancel(tdb);
796 vdata->traverse_error = false;
797 vdata->dest_db = tmp_db;
798 vdata->vacuum = true;
803 * repack and vacuum on-the-fly by not writing the records that are
806 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
807 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
808 tdb_transaction_cancel(tdb);
813 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
815 if (vdata->traverse_error) {
816 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
817 tdb_transaction_cancel(tdb);
822 if (tdb_wipe_all(tdb) != 0) {
823 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
824 tdb_transaction_cancel(tdb);
829 vdata->traverse_error = false;
830 vdata->dest_db = tdb;
831 vdata->vacuum = false;
834 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
835 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
836 tdb_transaction_cancel(tdb);
841 if (vdata->traverse_error) {
842 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
843 tdb_transaction_cancel(tdb);
851 if (tdb_transaction_commit(tdb) != 0) {
852 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
855 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
861 * repack and vaccum a db
862 * called from the child context
864 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
866 bool full_vacuum_run)
868 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
869 uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
870 const char *name = ctdb_db->db_name;
872 struct vacuum_data *vdata;
874 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
875 if (freelist_size == -1) {
876 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
880 vdata = talloc_zero(mem_ctx, struct vacuum_data);
882 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
886 vdata->ctdb = ctdb_db->ctdb;
887 vdata->vacuum_limit = vacuum_limit;
888 vdata->repack_limit = repack_limit;
889 vdata->delete_tree = trbt_create(vdata, 0);
890 vdata->ctdb_db = ctdb_db;
891 if (vdata->delete_tree == NULL) {
892 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
897 vdata->start = timeval_current();
900 * gather all records that can be deleted in vdata
902 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
903 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
907 * decide if a repack is necessary
909 if (freelist_size < repack_limit && vdata->delete_count < vacuum_limit)
915 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
916 name, freelist_size, vdata->delete_count));
919 * repack and implicitely get rid of the records we can delete
921 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
922 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
931 static int get_vacuum_interval(struct ctdb_db_context *ctdb_db)
933 uint interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
938 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
940 double l = timeval_elapsed(&child_ctx->start_time);
941 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
942 struct ctdb_context *ctdb = ctdb_db->ctdb;
944 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
946 if (child_ctx->child_pid != -1) {
947 kill(child_ctx->child_pid, SIGKILL);
949 /* Bump the number of successful fast-path runs. */
950 child_ctx->vacuum_handle->fast_path_count++;
953 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
955 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
956 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
957 ctdb_vacuum_event, child_ctx->vacuum_handle);
963 * this event is generated when a vacuum child process times out
965 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
966 struct timeval t, void *private_data)
968 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
970 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
972 child_ctx->status = VACUUM_TIMEOUT;
974 talloc_free(child_ctx);
979 * this event is generated when a vacuum child process has completed
981 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
982 uint16_t flags, void *private_data)
984 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
988 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
989 child_ctx->child_pid = -1;
991 ret = read(child_ctx->fd[0], &c, 1);
992 if (ret != 1 || c != 0) {
993 child_ctx->status = VACUUM_ERROR;
994 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));
996 child_ctx->status = VACUUM_OK;
999 talloc_free(child_ctx);
1003 * this event is called every time we need to start a new vacuum process
1006 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1007 struct timeval t, void *private_data)
1009 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1010 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1011 struct ctdb_context *ctdb = ctdb_db->ctdb;
1012 struct ctdb_vacuum_child_context *child_ctx;
1013 struct tevent_fd *fde;
1016 /* we dont vacuum if we are in recovery mode, or db frozen */
1017 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1018 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1019 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1020 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1021 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1024 event_add_timed(ctdb->ev, vacuum_handle,
1025 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1026 ctdb_vacuum_event, vacuum_handle);
1030 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1031 if (child_ctx == NULL) {
1032 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1033 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1037 ret = pipe(child_ctx->fd);
1039 talloc_free(child_ctx);
1040 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1041 event_add_timed(ctdb->ev, vacuum_handle,
1042 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1043 ctdb_vacuum_event, vacuum_handle);
1047 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1048 vacuum_handle->fast_path_count = 0;
1051 child_ctx->child_pid = ctdb_fork(ctdb);
1052 if (child_ctx->child_pid == (pid_t)-1) {
1053 close(child_ctx->fd[0]);
1054 close(child_ctx->fd[1]);
1055 talloc_free(child_ctx);
1056 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1057 event_add_timed(ctdb->ev, vacuum_handle,
1058 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1059 ctdb_vacuum_event, vacuum_handle);
1064 if (child_ctx->child_pid == 0) {
1066 bool full_vacuum_run = false;
1067 close(child_ctx->fd[0]);
1069 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1071 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1072 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1079 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1080 (vacuum_handle->fast_path_count == 0))
1082 full_vacuum_run = true;
1084 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1087 write(child_ctx->fd[1], &cc, 1);
1091 set_close_on_exec(child_ctx->fd[0]);
1092 close(child_ctx->fd[1]);
1094 child_ctx->status = VACUUM_RUNNING;
1095 child_ctx->start_time = timeval_current();
1097 DLIST_ADD(ctdb->vacuumers, child_ctx);
1098 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1101 * Clear the fastpath vacuuming list in the parent.
1103 talloc_free(ctdb_db->delete_queue);
1104 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1105 if (ctdb_db->delete_queue == NULL) {
1106 /* fatal here? ... */
1107 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1108 "in parent context. Shutting down\n");
1111 event_add_timed(ctdb->ev, child_ctx,
1112 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1113 vacuum_child_timeout, child_ctx);
1115 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1117 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1118 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1119 tevent_fd_set_auto_close(fde);
1121 vacuum_handle->child_ctx = child_ctx;
1122 child_ctx->vacuum_handle = vacuum_handle;
1125 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1127 /* Simply free them all. */
1128 while (ctdb->vacuumers) {
1129 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1130 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1131 (int)ctdb->vacuumers->child_pid));
1132 /* vacuum_child_destructor kills it, removes from list */
1133 talloc_free(ctdb->vacuumers);
1137 /* this function initializes the vacuuming context for a database
1138 * starts the vacuuming events
1140 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1142 if (ctdb_db->persistent != 0) {
1143 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1147 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1148 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1150 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1151 ctdb_db->vacuum_handle->fast_path_count = 0;
1153 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1154 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1155 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1160 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1161 const struct ctdb_ltdb_header *hdr,
1164 struct delete_record_data *kd;
1167 hash = (uint32_t)ctdb_hash(&key);
1169 DEBUG(DEBUG_DEBUG, (__location__
1170 " remove_record_from_delete_queue: db[%s] "
1174 "migrated_with_data[%s]\n",
1175 ctdb_db->db_name, ctdb_db->db_id,
1177 ctdb_lmaster(ctdb_db->ctdb, &key),
1178 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1180 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1184 if (kd->key.dsize != key.dsize) {
1187 if (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0) {
1197 * Insert a record into the ctdb_db context's delete queue,
1198 * handling hash collisions.
1200 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1201 const struct ctdb_ltdb_header *hdr,
1204 struct delete_record_data *kd;
1208 hash = (uint32_t)ctdb_hash(&key);
1210 DEBUG(DEBUG_INFO, (__location__ " Schedule for deletion: db[%s] "
1214 "migrated_with_data[%s]\n",
1215 ctdb_db->db_name, ctdb_db->db_id,
1217 ctdb_lmaster(ctdb_db->ctdb, &key),
1218 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1220 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1222 if ((kd->key.dsize != key.dsize) ||
1223 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1226 ("schedule for deletion: Hash collision (0x%08x)."
1227 " Skipping the record.\n", hash));
1231 ("schedule for deletion: Overwriting entry for "
1232 "key with hash 0x%08x.\n", hash));
1236 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1237 ctdb_db->delete_queue,
1247 * Schedule a record for deletetion.
1248 * Called from the parent context.
1250 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1253 struct ctdb_control_schedule_for_deletion *dd;
1254 struct ctdb_db_context *ctdb_db;
1258 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1260 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1261 if (ctdb_db == NULL) {
1262 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1267 key.dsize = dd->keylen;
1270 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1275 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1276 const struct ctdb_ltdb_header *hdr,
1280 struct ctdb_control_schedule_for_deletion *dd;
1284 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1285 /* main daemon - directly queue */
1286 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1291 /* child process: send the main daemon a control */
1293 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1294 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1295 if (indata.dptr == NULL) {
1296 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1299 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1300 dd->db_id = ctdb_db->db_id;
1302 dd->keylen = key.dsize;
1303 memcpy(dd->key, key.dptr, key.dsize);
1305 ret = ctdb_control(ctdb_db->ctdb,
1308 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1309 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1314 NULL, /* timeout : NULL == wait forever */
1315 NULL); /* error message */
1317 talloc_free(indata.dptr);
1319 if (ret != 0 || status != 0) {
1320 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1321 "SCHEDULE_FOR_DELETION "
1331 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1332 const struct ctdb_ltdb_header *hdr,
1335 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1337 * Only remove the record from the delete queue if called
1338 * in the main daemon.
1343 remove_record_from_delete_queue(ctdb_db, hdr, key);