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_list;
64 uint32_t delete_count;
65 struct ctdb_marshall_buffer **vacuum_fetch_list;
72 uint32_t fast_added_to_vacuum_fetch_list;
73 uint32_t fast_added_to_delete_list;
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_list;
80 uint32_t full_skipped;
84 uint32_t delete_remote_error;
85 uint32_t delete_local_error;
86 uint32_t delete_deleted;
87 uint32_t delete_skipped;
90 /* this structure contains the information for one record to be deleted */
91 struct delete_record_data {
92 struct ctdb_context *ctdb;
93 struct ctdb_db_context *ctdb_db;
94 struct ctdb_ltdb_header hdr;
98 struct delete_records_list {
99 struct ctdb_marshall_buffer *records;
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;
114 dd = talloc_zero(tree, struct delete_record_data);
116 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
121 dd->ctdb_db = ctdb_db;
122 dd->key.dsize = key.dsize;
123 dd->key.dptr = talloc_memdup(dd, key.dptr, key.dsize);
124 if (dd->key.dptr == NULL) {
125 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
131 hash = ctdb_hash(&key);
133 trbt_insert32(tree, hash, dd);
138 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
139 struct ctdb_ltdb_header *hdr)
141 struct ctdb_context *ctdb = vdata->ctdb;
142 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
146 hash = ctdb_hash(&key);
148 if (trbt_lookup32(vdata->delete_list, hash)) {
149 DEBUG(DEBUG_INFO, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
153 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
160 vdata->delete_count++;
166 * Add a record to the list of records to be sent
167 * to their lmaster with VACUUM_FETCH.
169 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
172 struct ctdb_context *ctdb = vdata->ctdb;
173 struct ctdb_rec_data *rec;
176 struct ctdb_marshall_buffer *vfl;
178 lmaster = ctdb_lmaster(ctdb, &key);
180 vfl = vdata->vacuum_fetch_list[lmaster];
182 rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
184 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
185 vdata->traverse_error = true;
189 old_size = talloc_get_size(vfl);
190 vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
192 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
193 vdata->traverse_error = true;
196 vdata->vacuum_fetch_list[lmaster] = vfl;
199 memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
208 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
209 struct timeval t, void *private_data);
213 * traverse function for gathering the records that can be deleted
215 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
217 struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
218 struct ctdb_context *ctdb = vdata->ctdb;
220 struct ctdb_ltdb_header *hdr;
225 lmaster = ctdb_lmaster(ctdb, &key);
226 if (lmaster >= ctdb->num_nodes) {
228 DEBUG(DEBUG_CRIT, (__location__
229 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
232 (unsigned)ctdb->num_nodes,
233 (unsigned)ctdb_hash(&key)));
237 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
238 /* it is not a deleted record */
239 vdata->full_skipped++;
243 hdr = (struct ctdb_ltdb_header *)data.dptr;
245 if (hdr->dmaster != ctdb->pnn) {
246 vdata->full_skipped++;
250 if (lmaster == ctdb->pnn) {
252 * We are both lmaster and dmaster, and the record is empty.
253 * So we should be able to delete it.
255 res = add_record_to_delete_list(vdata, key, hdr);
259 vdata->full_added_to_delete_list++;
263 * We are not lmaster.
264 * Add the record to the blob ready to send to the nodes.
266 res = add_record_to_vacuum_fetch_list(vdata, key);
270 vdata->full_added_to_vacuum_fetch_list++;
278 * traverse the tree of records to delete and marshall them into
281 static int delete_marshall_traverse(void *param, void *data)
283 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
284 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
285 struct ctdb_rec_data *rec;
288 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
290 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
294 old_size = talloc_get_size(recs->records);
295 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
296 if (recs->records == NULL) {
297 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
300 recs->records->count++;
301 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
306 * traverse function for the traversal of the delete_queue,
307 * the fast-path vacuuming list.
309 * - If the record has been migrated off the node
310 * or has been revived (filled with data) on the node,
311 * then skip the record.
313 * - If the current node is the record's lmaster and it is
314 * a record that has never been migrated with data, then
315 * delete the record from the local tdb.
317 * - If the current node is the record's lmaster and it has
318 * been migrated with data, then schedule it for the normal
319 * vacuuming procedure (i.e. add it to the delete_list).
321 * - If the current node is NOT the record's lmaster then
322 * add it to the list of records that are to be sent to
323 * the lmaster with the VACUUM_FETCH message.
325 static int delete_queue_traverse(void *param, void *data)
327 struct delete_record_data *dd =
328 talloc_get_type(data, struct delete_record_data);
329 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
330 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
331 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
333 struct ctdb_ltdb_header *header;
339 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
341 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
346 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
347 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
348 /* Does not exist or not a ctdb record. Skip. */
352 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
353 /* The record has been recycled (filled with data). Skip. */
357 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
359 if (header->dmaster != ctdb->pnn) {
360 /* The record has been migrated off the node. Skip. */
365 if (header->rsn != dd->hdr.rsn) {
367 * The record has been migrated off the node and back again.
368 * But not requeued for deletion. Skip it.
374 * We are dmaster, and the record has no data, and it has
375 * not been migrated after it has been queued for deletion.
377 * At this stage, the record could still have been revived locally
378 * and last been written with empty data. This can only be
379 * fixed with the addition of an active or delete flag. (TODO)
382 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
384 if (lmaster != ctdb->pnn) {
385 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
389 (__location__ " Error adding record to list "
390 "of records to send to lmaster.\n"));
393 vdata->fast_added_to_vacuum_fetch_list++;
398 /* use header->flags or dd->hdr.flags ?? */
399 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
400 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
404 (__location__ " Error adding record to list "
405 "of records for deletion on lmaster.\n"));
408 vdata->fast_added_to_delete_list++;
411 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
415 (__location__ " Error deleting record from local "
419 vdata->fast_deleted++;
426 vdata->fast_skipped++;
429 if (tdb_data.dptr != NULL) {
432 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
438 * Delete the records that we are lmaster and dmaster for and
439 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
442 static int delete_record_traverse(void *param, void *data)
444 struct delete_record_data *dd =
445 talloc_get_type(data, struct delete_record_data);
446 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
447 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
448 struct ctdb_context *ctdb = ctdb_db->ctdb;
450 struct ctdb_ltdb_header *header;
453 bool deleted = false;
455 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
457 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
458 vdata->delete_local_error++;
463 * Verify that the record is still empty, its RSN has not
464 * changed and that we are still its lmaster and dmaster.
467 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
468 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
469 /* Does not exist or not a ctdb record. Skip. */
470 vdata->delete_skipped++;
474 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
475 /* The record has been recycled (filled with data). Skip. */
476 vdata->delete_skipped++;
480 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
482 if (header->dmaster != ctdb->pnn) {
483 /* The record has been migrated off the node. Skip. */
484 vdata->delete_skipped++;
489 if (header->rsn != dd->hdr.rsn) {
491 * The record has been migrated off the node and back again.
492 * But not requeued for deletion. Skip it.
494 vdata->delete_skipped++;
498 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
500 if (lmaster != ctdb->pnn) {
501 /* we are not lmaster - strange */
502 vdata->delete_skipped++;
506 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
510 (__location__ " Error deleting record from local "
512 vdata->delete_local_error++;
519 if (tdb_data.dptr != NULL) {
523 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
527 * successfully deleted the record locally.
528 * remove it from the list and update statistics.
531 vdata->delete_deleted++;
532 vdata->delete_left--;
539 * Fast vacuuming run:
540 * Traverse the delete_queue.
541 * This fills the same lists as the database traverse.
543 static void ctdb_vacuum_db_fast(struct ctdb_db_context *ctdb_db,
544 struct vacuum_data *vdata)
546 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
548 if (vdata->fast_total > 0) {
551 " fast vacuuming delete_queue traverse statistics: "
560 (unsigned)vdata->fast_total,
561 (unsigned)vdata->fast_deleted,
562 (unsigned)vdata->fast_skipped,
563 (unsigned)vdata->fast_error,
564 (unsigned)vdata->fast_added_to_delete_list,
565 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
573 * read-only traverse of the database, looking for records that
574 * might be able to be vacuumed.
576 * This is not done each time but only every tunable
577 * VacuumFastPathCount times.
579 static int ctdb_vacuum_db_full(struct ctdb_db_context *ctdb_db,
580 struct vacuum_data *vdata,
581 bool full_vacuum_run)
585 if (!full_vacuum_run) {
589 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
590 if (ret == -1 || vdata->traverse_error) {
591 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
592 "'%s'\n", ctdb_db->db_name));
596 if (vdata->full_total > 0) {
599 " full vacuuming db traverse statistics: "
607 (unsigned)vdata->full_total,
608 (unsigned)vdata->full_skipped,
609 (unsigned)vdata->full_error,
610 (unsigned)vdata->full_added_to_delete_list,
611 (unsigned)vdata->full_added_to_vacuum_fetch_list));
619 * - Always do the fast vacuuming run, which traverses
620 * the in-memory delete queue: these records have been
621 * scheduled for deletion.
622 * - Only if explicitly requested, the database is traversed
623 * in order to use the traditional heuristics on empty records
624 * to trigger deletion.
625 * This is done only every VacuumFastPathCount'th vacuuming run.
627 * The traverse runs fill two lists:
630 * This is the list of empty records the current
631 * node is lmaster and dmaster for. These records are later
632 * deleted first on other nodes and then locally.
634 * The fast vacuuming run has a short cut for those records
635 * that have never been migrated with data: these records
636 * are immediately deleted locally, since they have left
637 * no trace on other nodes.
639 * - The vacuum_fetch lists
640 * (one for each other lmaster node):
641 * The records in this list are sent for deletion to
642 * their lmaster in a bulk VACUUM_FETCH message.
644 * The lmaster then migrates all these records to itelf
645 * so that they can be vacuumed there.
647 * This executes in the child context.
649 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
650 struct vacuum_data *vdata,
651 bool full_vacuum_run)
653 struct ctdb_context *ctdb = ctdb_db->ctdb;
654 const char *name = ctdb_db->db_name;
657 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
658 "%s db_id[0x%08x]\n",
659 full_vacuum_run ? "full" : "fast",
660 ctdb_db->db_name, ctdb_db->db_id));
662 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
664 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
668 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
670 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
676 vdata->fast_added_to_delete_list = 0;
677 vdata->fast_added_to_vacuum_fetch_list = 0;
678 vdata->fast_deleted = 0;
679 vdata->fast_skipped = 0;
680 vdata->fast_error = 0;
681 vdata->fast_total = 0;
682 vdata->full_added_to_delete_list = 0;
683 vdata->full_added_to_vacuum_fetch_list = 0;
684 vdata->full_skipped = 0;
685 vdata->full_error = 0;
686 vdata->full_total = 0;
687 vdata->delete_count = 0;
688 vdata->delete_left = 0;
689 vdata->delete_remote_error = 0;
690 vdata->delete_local_error = 0;
691 vdata->delete_skipped = 0;
692 vdata->delete_deleted = 0;
694 /* the list needs to be of length num_nodes */
695 vdata->vacuum_fetch_list = talloc_array(vdata,
696 struct ctdb_marshall_buffer *,
698 if (vdata->vacuum_fetch_list == NULL) {
699 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
702 for (i = 0; i < ctdb->num_nodes; i++) {
703 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
704 talloc_zero_size(vdata->vacuum_fetch_list,
705 offsetof(struct ctdb_marshall_buffer, data));
706 if (vdata->vacuum_fetch_list[i] == NULL) {
707 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
710 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
713 ctdb_vacuum_db_fast(ctdb_db, vdata);
715 ret = ctdb_vacuum_db_full(ctdb_db, vdata, full_vacuum_run);
721 * For records where we are not the lmaster,
722 * tell the lmaster to fetch the record.
724 for (i = 0; i < ctdb->num_nodes; i++) {
726 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
728 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
732 if (vfl->count == 0) {
736 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
737 vfl->count, ctdb->nodes[i]->pnn,
740 data.dsize = talloc_get_size(vfl);
741 data.dptr = (void *)vfl;
742 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
743 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
744 "fetch message to %u\n",
745 ctdb->nodes[i]->pnn));
750 /* Process all records we can delete (if any) */
751 if (vdata->delete_count > 0) {
752 struct delete_records_list *recs;
753 TDB_DATA indata, outdata;
755 struct ctdb_node_map *nodemap;
756 uint32_t *active_nodes;
757 int num_active_nodes;
759 vdata->delete_left = vdata->delete_count;
761 recs = talloc_zero(vdata, struct delete_records_list);
763 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
766 recs->records = (struct ctdb_marshall_buffer *)
767 talloc_zero_size(vdata,
768 offsetof(struct ctdb_marshall_buffer, data));
769 if (recs->records == NULL) {
770 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
773 recs->records->db_id = ctdb_db->db_id;
776 * traverse the tree of all records we want to delete and
777 * create a blob we can send to the other nodes.
779 trbt_traversearray32(vdata->delete_list, 1,
780 delete_marshall_traverse, recs);
782 indata.dsize = talloc_get_size(recs->records);
783 indata.dptr = (void *)recs->records;
786 * now tell all the active nodes to delete all these records
790 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
792 recs, /* talloc context */
795 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
799 active_nodes = list_of_active_nodes(ctdb, nodemap,
800 nodemap, /* talloc context */
801 false /* include self */);
803 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
805 for (i = 0; i < num_active_nodes; i++) {
806 struct ctdb_marshall_buffer *records;
807 struct ctdb_rec_data *rec;
809 ret = ctdb_control(ctdb, active_nodes[i], 0,
810 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
811 indata, recs, &outdata, &res,
813 if (ret != 0 || res != 0) {
814 DEBUG(DEBUG_ERR, ("Failed to delete records on "
815 "node %u: ret[%d] res[%d]\n",
816 active_nodes[i], ret, res));
821 * outdata contains the list of records coming back
822 * from the node: These are the records that the
823 * remote node could not delete.
825 records = (struct ctdb_marshall_buffer *)outdata.dptr;
826 rec = (struct ctdb_rec_data *)&records->data[0];
827 while (records->count-- > 1) {
828 TDB_DATA reckey, recdata;
829 struct ctdb_ltdb_header *rechdr;
830 struct delete_record_data *dd;
832 reckey.dptr = &rec->data[0];
833 reckey.dsize = rec->keylen;
834 recdata.dptr = &rec->data[reckey.dsize];
835 recdata.dsize = rec->datalen;
837 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
838 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
841 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
842 recdata.dptr += sizeof(*rechdr);
843 recdata.dsize -= sizeof(*rechdr);
845 dd = (struct delete_record_data *)trbt_lookup32(
850 * The other node could not delete the
851 * record and it is the first node that
852 * failed. So we should remove it from
853 * the tree and update statistics.
856 vdata->delete_remote_error++;
857 vdata->delete_left--;
860 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
864 /* free nodemap and active_nodes */
865 talloc_free(nodemap);
868 if (vdata->delete_left > 0) {
870 * The only records remaining in the tree are those
871 * records which all other nodes could successfully
872 * delete, so we can safely delete them on the
875 trbt_traversearray32(vdata->delete_list, 1,
876 delete_record_traverse, vdata);
879 /* this ensures we run our event queue */
880 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
887 * traverse function for repacking
889 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
891 struct vacuum_data *vdata = (struct vacuum_data *)private;
894 uint32_t hash = ctdb_hash(&key);
895 struct delete_record_data *kd;
897 * check if we can ignore this record because it's in the delete_list
899 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
901 * there might be hash collisions so we have to compare the keys here to be sure
903 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
904 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
906 * we have to check if the record hasn't changed in the meantime in order to
907 * savely remove it from the database
909 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
910 hdr->dmaster == kd->ctdb->pnn &&
911 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
912 kd->hdr.rsn == hdr->rsn) {
918 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
919 vdata->traverse_error = true;
929 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
931 struct tdb_context *tmp_db;
933 if (tdb_transaction_start(tdb) != 0) {
934 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
938 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
939 TDB_INTERNAL|TDB_DISALLOW_NESTING,
941 if (tmp_db == NULL) {
942 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
943 tdb_transaction_cancel(tdb);
947 vdata->traverse_error = false;
948 vdata->dest_db = tmp_db;
949 vdata->vacuum = true;
954 * repack and vacuum on-the-fly by not writing the records that are
957 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
958 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
959 tdb_transaction_cancel(tdb);
964 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
966 if (vdata->traverse_error) {
967 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
968 tdb_transaction_cancel(tdb);
973 if (tdb_wipe_all(tdb) != 0) {
974 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
975 tdb_transaction_cancel(tdb);
980 vdata->traverse_error = false;
981 vdata->dest_db = tdb;
982 vdata->vacuum = false;
985 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
986 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
987 tdb_transaction_cancel(tdb);
992 if (vdata->traverse_error) {
993 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
994 tdb_transaction_cancel(tdb);
1002 if (tdb_transaction_commit(tdb) != 0) {
1003 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1006 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1012 * repack and vaccum a db
1013 * called from the child context
1015 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1016 TALLOC_CTX *mem_ctx,
1017 bool full_vacuum_run)
1019 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1020 uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
1021 const char *name = ctdb_db->db_name;
1023 struct vacuum_data *vdata;
1025 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1026 if (freelist_size == -1) {
1027 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1031 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1032 if (vdata == NULL) {
1033 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1037 vdata->ctdb = ctdb_db->ctdb;
1038 vdata->vacuum_limit = vacuum_limit;
1039 vdata->repack_limit = repack_limit;
1040 vdata->delete_list = trbt_create(vdata, 0);
1041 vdata->ctdb_db = ctdb_db;
1042 if (vdata->delete_list == NULL) {
1043 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1048 vdata->start = timeval_current();
1051 * gather all records that can be deleted in vdata
1053 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1054 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1058 * decide if a repack is necessary
1060 if (freelist_size < repack_limit && vdata->delete_left < vacuum_limit)
1066 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
1067 name, freelist_size, vdata->delete_left));
1070 * repack and implicitely get rid of the records we can delete
1072 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1073 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1082 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1084 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1089 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1091 double l = timeval_elapsed(&child_ctx->start_time);
1092 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1093 struct ctdb_context *ctdb = ctdb_db->ctdb;
1095 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1097 if (child_ctx->child_pid != -1) {
1098 kill(child_ctx->child_pid, SIGKILL);
1100 /* Bump the number of successful fast-path runs. */
1101 child_ctx->vacuum_handle->fast_path_count++;
1104 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1106 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1107 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1108 ctdb_vacuum_event, child_ctx->vacuum_handle);
1114 * this event is generated when a vacuum child process times out
1116 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1117 struct timeval t, void *private_data)
1119 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1121 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1123 child_ctx->status = VACUUM_TIMEOUT;
1125 talloc_free(child_ctx);
1130 * this event is generated when a vacuum child process has completed
1132 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1133 uint16_t flags, void *private_data)
1135 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1139 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1140 child_ctx->child_pid = -1;
1142 ret = read(child_ctx->fd[0], &c, 1);
1143 if (ret != 1 || c != 0) {
1144 child_ctx->status = VACUUM_ERROR;
1145 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));
1147 child_ctx->status = VACUUM_OK;
1150 talloc_free(child_ctx);
1154 * this event is called every time we need to start a new vacuum process
1157 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1158 struct timeval t, void *private_data)
1160 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1161 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1162 struct ctdb_context *ctdb = ctdb_db->ctdb;
1163 struct ctdb_vacuum_child_context *child_ctx;
1164 struct tevent_fd *fde;
1167 /* we dont vacuum if we are in recovery mode, or db frozen */
1168 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1169 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1170 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1171 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1172 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1175 event_add_timed(ctdb->ev, vacuum_handle,
1176 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1177 ctdb_vacuum_event, vacuum_handle);
1181 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1182 if (child_ctx == NULL) {
1183 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1184 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1188 ret = pipe(child_ctx->fd);
1190 talloc_free(child_ctx);
1191 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1192 event_add_timed(ctdb->ev, vacuum_handle,
1193 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1194 ctdb_vacuum_event, vacuum_handle);
1198 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1199 vacuum_handle->fast_path_count = 0;
1202 child_ctx->child_pid = ctdb_fork(ctdb);
1203 if (child_ctx->child_pid == (pid_t)-1) {
1204 close(child_ctx->fd[0]);
1205 close(child_ctx->fd[1]);
1206 talloc_free(child_ctx);
1207 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1208 event_add_timed(ctdb->ev, vacuum_handle,
1209 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1210 ctdb_vacuum_event, vacuum_handle);
1215 if (child_ctx->child_pid == 0) {
1217 bool full_vacuum_run = false;
1218 close(child_ctx->fd[0]);
1220 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1222 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1223 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1230 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1231 (vacuum_handle->fast_path_count == 0))
1233 full_vacuum_run = true;
1235 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1238 write(child_ctx->fd[1], &cc, 1);
1242 set_close_on_exec(child_ctx->fd[0]);
1243 close(child_ctx->fd[1]);
1245 child_ctx->status = VACUUM_RUNNING;
1246 child_ctx->start_time = timeval_current();
1248 DLIST_ADD(ctdb->vacuumers, child_ctx);
1249 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1252 * Clear the fastpath vacuuming list in the parent.
1254 talloc_free(ctdb_db->delete_queue);
1255 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1256 if (ctdb_db->delete_queue == NULL) {
1257 /* fatal here? ... */
1258 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1259 "in parent context. Shutting down\n");
1262 event_add_timed(ctdb->ev, child_ctx,
1263 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1264 vacuum_child_timeout, child_ctx);
1266 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1268 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1269 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1270 tevent_fd_set_auto_close(fde);
1272 vacuum_handle->child_ctx = child_ctx;
1273 child_ctx->vacuum_handle = vacuum_handle;
1276 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1278 /* Simply free them all. */
1279 while (ctdb->vacuumers) {
1280 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1281 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1282 (int)ctdb->vacuumers->child_pid));
1283 /* vacuum_child_destructor kills it, removes from list */
1284 talloc_free(ctdb->vacuumers);
1288 /* this function initializes the vacuuming context for a database
1289 * starts the vacuuming events
1291 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1293 if (ctdb_db->persistent != 0) {
1294 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1298 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1299 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1301 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1302 ctdb_db->vacuum_handle->fast_path_count = 0;
1304 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1305 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1306 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1311 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1312 const struct ctdb_ltdb_header *hdr,
1315 struct delete_record_data *kd;
1318 hash = (uint32_t)ctdb_hash(&key);
1320 DEBUG(DEBUG_DEBUG, (__location__
1321 " remove_record_from_delete_queue: db[%s] "
1325 "migrated_with_data[%s]\n",
1326 ctdb_db->db_name, ctdb_db->db_id,
1328 ctdb_lmaster(ctdb_db->ctdb, &key),
1329 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1331 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1335 if (kd->key.dsize != key.dsize) {
1338 if (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0) {
1348 * Insert a record into the ctdb_db context's delete queue,
1349 * handling hash collisions.
1351 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1352 const struct ctdb_ltdb_header *hdr,
1355 struct delete_record_data *kd;
1359 hash = (uint32_t)ctdb_hash(&key);
1361 DEBUG(DEBUG_INFO, (__location__ " Schedule for deletion: db[%s] "
1365 "migrated_with_data[%s]\n",
1366 ctdb_db->db_name, ctdb_db->db_id,
1368 ctdb_lmaster(ctdb_db->ctdb, &key),
1369 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1371 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1373 if ((kd->key.dsize != key.dsize) ||
1374 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1377 ("schedule for deletion: Hash collision (0x%08x)."
1378 " Skipping the record.\n", hash));
1382 ("schedule for deletion: Overwriting entry for "
1383 "key with hash 0x%08x.\n", hash));
1387 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1388 ctdb_db->delete_queue,
1398 * Schedule a record for deletetion.
1399 * Called from the parent context.
1401 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1404 struct ctdb_control_schedule_for_deletion *dd;
1405 struct ctdb_db_context *ctdb_db;
1409 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1411 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1412 if (ctdb_db == NULL) {
1413 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1418 key.dsize = dd->keylen;
1421 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1426 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1427 const struct ctdb_ltdb_header *hdr,
1431 struct ctdb_control_schedule_for_deletion *dd;
1435 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1436 /* main daemon - directly queue */
1437 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1442 /* child process: send the main daemon a control */
1444 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1445 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1446 if (indata.dptr == NULL) {
1447 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1450 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1451 dd->db_id = ctdb_db->db_id;
1453 dd->keylen = key.dsize;
1454 memcpy(dd->key, key.dptr, key.dsize);
1456 ret = ctdb_control(ctdb_db->ctdb,
1459 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1460 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1465 NULL, /* timeout : NULL == wait forever */
1466 NULL); /* error message */
1468 talloc_free(indata.dptr);
1470 if (ret != 0 || status != 0) {
1471 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1472 "SCHEDULE_FOR_DELETION "
1482 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1483 const struct ctdb_ltdb_header *hdr,
1486 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1488 * Only remove the record from the delete queue if called
1489 * in the main daemon.
1494 remove_record_from_delete_queue(ctdb_db, hdr, key);