4 Copyright (C) Ronnie Sahlberg 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "lib/events/events.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "system/dir.h"
26 #include "../include/ctdb_private.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/events/events.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
34 #define TUNINGDBNAME "vactune.tdb"
36 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
38 struct ctdb_vacuum_child_context {
39 struct ctdb_vacuum_child_context *next, *prev;
40 struct ctdb_vacuum_handle *vacuum_handle;
41 /* fd child writes status to */
44 enum vacuum_child_status status;
45 struct timeval start_time;
48 struct ctdb_vacuum_handle {
49 struct ctdb_db_context *ctdb_db;
50 struct ctdb_vacuum_child_context *child_ctx;
51 uint32_t fast_path_count;
55 /* a list of records to possibly delete */
57 uint32_t vacuum_limit;
58 uint32_t repack_limit;
59 struct ctdb_context *ctdb;
60 struct ctdb_db_context *ctdb_db;
61 struct tdb_context *dest_db;
62 trbt_tree_t *delete_tree;
63 uint32_t delete_count;
64 struct ctdb_marshall_buffer **list;
71 uint32_t fast_added_to_vacuum_fetch_list;
72 uint32_t fast_added_to_delete_tree;
73 uint32_t fast_deleted;
74 uint32_t fast_skipped;
77 uint32_t full_added_to_vacuum_fetch_list;
78 uint32_t full_added_to_delete_tree;
79 uint32_t full_skipped;
84 /* tuning information stored for every db */
85 struct vacuum_tuning_data {
86 uint32_t last_num_repack;
87 uint32_t last_num_empty;
88 uint32_t last_interval;
89 uint32_t new_interval;
90 struct timeval last_start;
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;
102 struct delete_records_list {
103 struct ctdb_marshall_buffer *records;
107 * Store key and header in a tree, indexed by the key hash.
109 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
110 struct ctdb_db_context *ctdb_db,
112 const struct ctdb_ltdb_header *hdr,
115 struct delete_record_data *dd;
118 dd = talloc_zero(tree, struct delete_record_data);
120 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
125 dd->ctdb_db = ctdb_db;
126 dd->key.dsize = key.dsize;
127 dd->key.dptr = talloc_memdup(dd, key.dptr, key.dsize);
128 if (dd->key.dptr == NULL) {
129 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
135 hash = (uint32_t)tdb_jenkins_hash(&key);
137 trbt_insert32(tree, hash, dd);
142 static int add_record_to_delete_tree(struct vacuum_data *vdata, TDB_DATA key,
143 struct ctdb_ltdb_header *hdr)
145 struct ctdb_context *ctdb = vdata->ctdb;
146 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
150 hash = (uint32_t)tdb_jenkins_hash(&key);
152 if (trbt_lookup32(vdata->delete_tree, hash)) {
153 DEBUG(DEBUG_INFO, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
157 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
164 vdata->delete_count++;
170 * Add a record to the list of records to be sent
171 * to their lmaster with VACUUM_FETCH.
173 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
176 struct ctdb_context *ctdb = vdata->ctdb;
177 struct ctdb_rec_data *rec;
181 lmaster = ctdb_lmaster(ctdb, &key);
183 rec = ctdb_marshall_record(vdata->list[lmaster], ctdb->pnn, key, NULL, tdb_null);
185 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
186 vdata->traverse_error = true;
190 old_size = talloc_get_size(vdata->list[lmaster]);
191 vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster],
192 old_size + rec->length);
193 if (vdata->list[lmaster] == NULL) {
194 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
195 vdata->traverse_error = true;
199 vdata->list[lmaster]->count++;
200 memcpy(old_size+(uint8_t *)vdata->list[lmaster], rec, rec->length);
209 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
210 struct timeval t, void *private_data);
214 * traverse function for gathering the records that can be deleted
216 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
218 struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
219 struct ctdb_context *ctdb = vdata->ctdb;
221 struct ctdb_ltdb_header *hdr;
226 lmaster = ctdb_lmaster(ctdb, &key);
227 if (lmaster >= ctdb->num_nodes) {
229 DEBUG(DEBUG_CRIT, (__location__
230 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
233 (unsigned)ctdb->num_nodes,
234 (unsigned)ctdb_hash(&key)));
238 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
239 /* it is not a deleted record */
240 vdata->full_skipped++;
244 hdr = (struct ctdb_ltdb_header *)data.dptr;
246 if (hdr->dmaster != ctdb->pnn) {
247 vdata->full_skipped++;
251 if (lmaster == ctdb->pnn) {
253 * We are both lmaster and dmaster, and the record * is empty.
254 * So we should be able to delete it.
256 res = add_record_to_delete_tree(vdata, key, hdr);
260 vdata->full_added_to_delete_tree++;
264 * We are not lmaster.
265 * Add the record to the blob ready to send to the nodes.
267 res = add_record_to_vacuum_fetch_list(vdata, key);
271 vdata->full_added_to_vacuum_fetch_list++;
279 * traverse the tree of records to delete and marshall them into
282 static int delete_traverse(void *param, void *data)
284 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
285 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
286 struct ctdb_rec_data *rec;
289 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
291 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
295 old_size = talloc_get_size(recs->records);
296 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
297 if (recs->records == NULL) {
298 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
301 recs->records->count++;
302 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
307 * traverse function for the traversal of the delete_queue,
308 * the fast-path vacuuming list.
310 * - If the record has been migrated off the node
311 * or has been revived (filled with data) on the node,
312 * then skip the record.
314 * - If the current node is the record's lmaster and it is
315 * a record that has never been migrated with data, then
316 * delete the record from the local tdb.
318 * - If the current node is the record's lmaster and it has
319 * been migrated with data, then schedule it for the normal
320 * vacuuming procedure (i.e. add it to the delete_list).
322 * - If the current node is NOT the record's lmaster then
323 * add it to the list of records that are to be sent to
324 * the lmaster with the VACUUM_FETCH message.
326 static int delete_queue_traverse(void *param, void *data)
328 struct delete_record_data *dd =
329 talloc_get_type(data, struct delete_record_data);
330 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
331 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
332 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
334 struct ctdb_ltdb_header *header;
340 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
342 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
347 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
348 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
349 /* Does not exist or not a ctdb record. Skip. */
353 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
354 /* The record has been recycled (filled with data). Skip. */
358 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
360 if (header->dmaster != ctdb->pnn) {
361 /* The record has been migrated off the node. Skip. */
366 if (header->rsn != dd->hdr.rsn) {
368 * The record has been migrated off the node and back again.
369 * But not requeued for deletion. Skip it.
375 * We are dmaster, and the record has no data, and it has
376 * not been migrated after it has been queued for deletion.
378 * At this stage, the record could still have been revived locally
379 * and last been written with empty data. This can only be
380 * fixed with the addition of an active or delete flag. (TODO)
383 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
385 if (lmaster != ctdb->pnn) {
386 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
390 (__location__ " Error adding record to list "
391 "of records to send to lmaster.\n"));
394 vdata->fast_added_to_vacuum_fetch_list++;
399 /* use header->flags or dd->hdr.flags ?? */
400 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
401 res = add_record_to_delete_tree(vdata, dd->key, &dd->hdr);
405 (__location__ " Error adding record to list "
406 "of records for deletion on lmaster.\n"));
409 vdata->fast_added_to_delete_tree++;
412 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
416 (__location__ " Error deleting record from local "
420 vdata->fast_deleted++;
427 vdata->fast_skipped++;
430 if (tdb_data.dptr != NULL) {
433 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
439 * read-only traverse the database in order to find
440 * records that can be deleted and try to delete these
441 * records on the other nodes
442 * this executes in the child context
444 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
445 struct vacuum_data *vdata,
446 bool full_vacuum_run)
448 struct ctdb_context *ctdb = ctdb_db->ctdb;
449 const char *name = ctdb_db->db_name;
452 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
453 "%s db_id[0x%08x]\n",
454 full_vacuum_run ? "full" : "fast",
455 ctdb_db->db_name, ctdb_db->db_id));
457 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
459 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
463 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
465 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
471 vdata->fast_added_to_delete_tree = 0;
472 vdata->fast_added_to_vacuum_fetch_list = 0;
473 vdata->fast_deleted = 0;
474 vdata->fast_skipped = 0;
475 vdata->fast_error = 0;
476 vdata->fast_total = 0;
477 vdata->full_added_to_delete_tree = 0;
478 vdata->full_added_to_vacuum_fetch_list = 0;
479 vdata->full_skipped = 0;
480 vdata->full_error = 0;
481 vdata->full_total = 0;
483 /* the list needs to be of length num_nodes */
484 vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->num_nodes);
485 if (vdata->list == NULL) {
486 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
489 for (i = 0; i < ctdb->num_nodes; i++) {
490 vdata->list[i] = (struct ctdb_marshall_buffer *)
491 talloc_zero_size(vdata->list,
492 offsetof(struct ctdb_marshall_buffer, data));
493 if (vdata->list[i] == NULL) {
494 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
497 vdata->list[i]->db_id = ctdb_db->db_id;
501 * Traverse the delete_queue.
502 * This builds the same lists as the db traverse.
504 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
506 if (vdata->fast_total > 0) {
509 " fast vacuuming delete_queue traverse statistics: "
518 (unsigned)vdata->fast_total,
519 (unsigned)vdata->fast_deleted,
520 (unsigned)vdata->fast_skipped,
521 (unsigned)vdata->fast_error,
522 (unsigned)vdata->fast_added_to_delete_tree,
523 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
527 * read-only traverse of the database, looking for records that
528 * might be able to be vacuumed.
530 * This is not done each time but only every tunable
531 * VacuumFastPathCount times.
533 if (full_vacuum_run) {
534 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
535 if (ret == -1 || vdata->traverse_error) {
536 DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
539 if (vdata->full_total > 0) {
542 " full vacuuming db traverse statistics: "
550 (unsigned)vdata->full_total,
551 (unsigned)vdata->full_skipped,
552 (unsigned)vdata->full_error,
553 (unsigned)vdata->full_added_to_delete_tree,
554 (unsigned)vdata->full_added_to_vacuum_fetch_list));
559 * For records where we are not the lmaster,
560 * tell the lmaster to fetch the record.
562 for (i = 0; i < ctdb->num_nodes; i++) {
565 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
569 if (vdata->list[i]->count == 0) {
573 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
574 vdata->list[i]->count, ctdb->nodes[i]->pnn,
577 data.dsize = talloc_get_size(vdata->list[i]);
578 data.dptr = (void *)vdata->list[i];
579 if (ctdb_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
580 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
581 "fetch message to %u\n",
582 ctdb->nodes[i]->pnn));
587 /* Process all records we can delete (if any) */
588 if (vdata->delete_count > 0) {
589 struct delete_records_list *recs;
590 TDB_DATA indata, outdata;
592 struct ctdb_node_map *nodemap;
593 uint32_t *active_nodes;
594 int num_active_nodes;
596 recs = talloc_zero(vdata, struct delete_records_list);
598 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
601 recs->records = (struct ctdb_marshall_buffer *)
602 talloc_zero_size(vdata,
603 offsetof(struct ctdb_marshall_buffer, data));
604 if (recs->records == NULL) {
605 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
608 recs->records->db_id = ctdb_db->db_id;
611 * traverse the tree of all records we want to delete and
612 * create a blob we can send to the other nodes.
614 trbt_traversearray32(vdata->delete_tree, 1, delete_traverse, recs);
616 indata.dsize = talloc_get_size(recs->records);
617 indata.dptr = (void *)recs->records;
620 * now tell all the active nodes to delete all these records
624 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
626 recs, /* talloc context */
629 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
633 active_nodes = list_of_active_nodes(ctdb, nodemap,
634 nodemap, /* talloc context */
635 false /* include self */);
637 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
639 for (i = 0; i < num_active_nodes; i++) {
640 struct ctdb_marshall_buffer *records;
641 struct ctdb_rec_data *rec;
643 ret = ctdb_control(ctdb, active_nodes[i], 0,
644 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
645 indata, recs, &outdata, &res,
647 if (ret != 0 || res != 0) {
648 DEBUG(DEBUG_ERR, ("Failed to delete records on "
649 "node %u: ret[%d] res[%d]\n",
650 active_nodes[i], ret, res));
655 * outdata countains the list of records coming back
656 * from the node which the node could not delete
658 records = (struct ctdb_marshall_buffer *)outdata.dptr;
659 rec = (struct ctdb_rec_data *)&records->data[0];
660 while (records->count-- > 1) {
661 TDB_DATA reckey, recdata;
662 struct ctdb_ltdb_header *rechdr;
664 reckey.dptr = &rec->data[0];
665 reckey.dsize = rec->keylen;
666 recdata.dptr = &rec->data[reckey.dsize];
667 recdata.dsize = rec->datalen;
669 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
670 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
673 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
674 recdata.dptr += sizeof(*rechdr);
675 recdata.dsize -= sizeof(*rechdr);
678 * that other node couldnt delete the record
679 * so we should delete it and thereby remove it from the tree
681 talloc_free(trbt_lookup32(vdata->delete_tree,
682 (uint32_t)tdb_jenkins_hash(&reckey)));
684 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
688 /* free nodemap and active_nodes */
689 talloc_free(nodemap);
692 * The only records remaining in the tree would be those
693 * records where all other nodes could successfully
694 * delete them, so we can safely delete them on the
695 * lmaster as well. Deletion implictely happens while
696 * we repack the database. The repack algorithm revisits
697 * the tree in order to find the records that don't need
698 * to be copied / repacked.
702 /* this ensures we run our event queue */
703 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
710 * traverse function for repacking
712 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
714 struct vacuum_data *vdata = (struct vacuum_data *)private;
717 uint32_t hash = (uint32_t)tdb_jenkins_hash(&key);
718 struct delete_record_data *kd;
720 * check if we can ignore this record because it's in the delete_tree
722 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_tree, hash);
724 * there might be hash collisions so we have to compare the keys here to be sure
726 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
727 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
729 * we have to check if the record hasn't changed in the meantime in order to
730 * savely remove it from the database
732 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
733 hdr->dmaster == kd->ctdb->pnn &&
734 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
735 kd->hdr.rsn == hdr->rsn) {
741 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
742 vdata->traverse_error = true;
752 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
754 struct tdb_context *tmp_db;
756 if (tdb_transaction_start(tdb) != 0) {
757 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
761 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
762 TDB_INTERNAL|TDB_DISALLOW_NESTING,
764 if (tmp_db == NULL) {
765 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
766 tdb_transaction_cancel(tdb);
770 vdata->traverse_error = false;
771 vdata->dest_db = tmp_db;
772 vdata->vacuum = true;
777 * repack and vacuum on-the-fly by not writing the records that are
780 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
781 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
782 tdb_transaction_cancel(tdb);
787 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
789 if (vdata->traverse_error) {
790 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
791 tdb_transaction_cancel(tdb);
796 if (tdb_wipe_all(tdb) != 0) {
797 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
798 tdb_transaction_cancel(tdb);
803 vdata->traverse_error = false;
804 vdata->dest_db = tdb;
805 vdata->vacuum = false;
808 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
809 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
810 tdb_transaction_cancel(tdb);
815 if (vdata->traverse_error) {
816 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
817 tdb_transaction_cancel(tdb);
825 if (tdb_transaction_commit(tdb) != 0) {
826 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
829 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
834 static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *vdata, uint32_t freelist)
836 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
837 TDB_CONTEXT *tune_tdb;
839 struct vacuum_tuning_data tdata;
840 struct vacuum_tuning_data *tptr;
844 vac_dbname = talloc_asprintf(tmp_ctx, "%s/%s.%u",
845 ctdb_db->ctdb->db_directory_state,
846 TUNINGDBNAME, ctdb_db->ctdb->pnn);
847 if (vac_dbname == NULL) {
848 DEBUG(DEBUG_CRIT,(__location__ " Out of memory error while allocating '%s'\n", vac_dbname));
849 talloc_free(tmp_ctx);
853 flags = ctdb_db->ctdb->valgrinding ? TDB_NOMMAP : 0;
854 flags |= TDB_DISALLOW_NESTING;
855 tune_tdb = tdb_open(vac_dbname, 0,
857 O_RDWR|O_CREAT, 0600);
858 if (tune_tdb == NULL) {
859 DEBUG(DEBUG_ERR,(__location__ " Failed to create/open %s\n", TUNINGDBNAME));
860 talloc_free(tmp_ctx);
864 if (tdb_transaction_start(tune_tdb) != 0) {
865 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
869 key.dptr = discard_const(ctdb_db->db_name);
870 key.dsize = strlen(ctdb_db->db_name);
871 value = tdb_fetch(tune_tdb, key);
873 if (value.dptr != NULL && value.dsize == sizeof(struct vacuum_tuning_data)) {
874 tptr = (struct vacuum_tuning_data *)value.dptr;
878 * re-calc new vacuum interval:
879 * in case no limit was reached we continuously increase the interval
880 * until vacuum_max_interval is reached
881 * in case a limit was reached we divide the current interval by 2
882 * unless vacuum_min_interval is reached
884 if (freelist < vdata->repack_limit &&
885 vdata->delete_count < vdata->vacuum_limit) {
886 if (tdata.last_interval < ctdb_db->ctdb->tunable.vacuum_max_interval) {
887 tdata.new_interval = tdata.last_interval * 110 / 100;
888 DEBUG(DEBUG_INFO,("Increasing vacuum interval %u -> %u for %s\n",
889 tdata.last_interval, tdata.new_interval, ctdb_db->db_name));
892 tdata.new_interval = tdata.last_interval / 2;
893 if (tdata.new_interval < ctdb_db->ctdb->tunable.vacuum_min_interval ||
894 tdata.new_interval > ctdb_db->ctdb->tunable.vacuum_max_interval) {
895 tdata.new_interval = ctdb_db->ctdb->tunable.vacuum_min_interval;
897 DEBUG(DEBUG_INFO,("Decreasing vacuum interval %u -> %u for %s\n",
898 tdata.last_interval, tdata.new_interval, ctdb_db->db_name));
900 tdata.last_interval = tdata.new_interval;
902 DEBUG(DEBUG_ERR,(__location__ " Cannot find tunedb record for %s. Using default interval\n", ctdb_db->db_name));
903 tdata.last_num_repack = freelist;
904 tdata.last_num_empty = vdata->delete_count;
905 tdata.last_interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
908 if (value.dptr != NULL) {
912 tdata.last_start = vdata->start;
913 tdata.last_duration = timeval_elapsed(&vdata->start);
915 value.dptr = (unsigned char *)&tdata;
916 value.dsize = sizeof(tdata);
918 if (tdb_store(tune_tdb, key, value, 0) != 0) {
919 DEBUG(DEBUG_ERR,(__location__ " Unable to store tundb record for %s\n", ctdb_db->db_name));
920 tdb_transaction_cancel(tune_tdb);
922 talloc_free(tmp_ctx);
925 tdb_transaction_commit(tune_tdb);
927 talloc_free(tmp_ctx);
933 * repack and vaccum a db
934 * called from the child context
936 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
938 bool full_vacuum_run)
940 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
941 uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
942 const char *name = ctdb_db->db_name;
944 struct vacuum_data *vdata;
946 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
947 if (freelist_size == -1) {
948 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
952 vdata = talloc_zero(mem_ctx, struct vacuum_data);
954 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
958 vdata->ctdb = ctdb_db->ctdb;
959 vdata->vacuum_limit = vacuum_limit;
960 vdata->repack_limit = repack_limit;
961 vdata->delete_tree = trbt_create(vdata, 0);
962 vdata->ctdb_db = ctdb_db;
963 if (vdata->delete_tree == NULL) {
964 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
969 vdata->start = timeval_current();
972 * gather all records that can be deleted in vdata
974 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
975 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
979 * decide if a repack is necessary
981 if (freelist_size < repack_limit && vdata->delete_count < vacuum_limit)
983 update_tuning_db(ctdb_db, vdata, freelist_size);
988 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
989 name, freelist_size, vdata->delete_count));
992 * repack and implicitely get rid of the records we can delete
994 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
995 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
996 update_tuning_db(ctdb_db, vdata, freelist_size);
1000 update_tuning_db(ctdb_db, vdata, freelist_size);
1006 static int get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1008 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1010 TDB_DATA key, value;
1012 uint interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
1013 struct ctdb_context *ctdb = ctdb_db->ctdb;
1016 vac_dbname = talloc_asprintf(tmp_ctx, "%s/%s.%u", ctdb->db_directory, TUNINGDBNAME, ctdb->pnn);
1017 if (vac_dbname == NULL) {
1018 DEBUG(DEBUG_CRIT,(__location__ " Out of memory error while allocating '%s'\n", vac_dbname));
1019 talloc_free(tmp_ctx);
1023 flags = ctdb_db->ctdb->valgrinding ? TDB_NOMMAP : 0;
1024 flags |= TDB_DISALLOW_NESTING;
1025 tdb = tdb_open(vac_dbname, 0,
1027 O_RDWR|O_CREAT, 0600);
1029 DEBUG(DEBUG_ERR,("Unable to open/create database %s using default interval\n", vac_dbname));
1030 talloc_free(tmp_ctx);
1034 key.dptr = discard_const(ctdb_db->db_name);
1035 key.dsize = strlen(ctdb_db->db_name);
1037 value = tdb_fetch(tdb, key);
1039 if (value.dptr != NULL) {
1040 if (value.dsize == sizeof(struct vacuum_tuning_data)) {
1041 struct vacuum_tuning_data *tptr = (struct vacuum_tuning_data *)value.dptr;
1043 interval = tptr->new_interval;
1045 if (interval < ctdb->tunable.vacuum_min_interval) {
1046 interval = ctdb->tunable.vacuum_min_interval;
1048 if (interval > ctdb->tunable.vacuum_max_interval) {
1049 interval = ctdb->tunable.vacuum_max_interval;
1056 talloc_free(tmp_ctx);
1061 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1063 double l = timeval_elapsed(&child_ctx->start_time);
1064 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1065 struct ctdb_context *ctdb = ctdb_db->ctdb;
1067 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1069 if (child_ctx->child_pid != -1) {
1070 kill(child_ctx->child_pid, SIGKILL);
1072 /* Bump the number of successful fast-path runs. */
1073 child_ctx->vacuum_handle->fast_path_count++;
1076 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1078 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1079 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1080 ctdb_vacuum_event, child_ctx->vacuum_handle);
1086 * this event is generated when a vacuum child process times out
1088 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1089 struct timeval t, void *private_data)
1091 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1093 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1095 child_ctx->status = VACUUM_TIMEOUT;
1097 talloc_free(child_ctx);
1102 * this event is generated when a vacuum child process has completed
1104 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1105 uint16_t flags, void *private_data)
1107 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1111 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1112 child_ctx->child_pid = -1;
1114 ret = read(child_ctx->fd[0], &c, 1);
1115 if (ret != 1 || c != 0) {
1116 child_ctx->status = VACUUM_ERROR;
1117 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));
1119 child_ctx->status = VACUUM_OK;
1122 talloc_free(child_ctx);
1126 * this event is called every time we need to start a new vacuum process
1129 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1130 struct timeval t, void *private_data)
1132 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1133 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1134 struct ctdb_context *ctdb = ctdb_db->ctdb;
1135 struct ctdb_vacuum_child_context *child_ctx;
1138 /* we dont vacuum if we are in recovery mode, or db frozen */
1139 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1140 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1141 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1142 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1143 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1146 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1150 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1151 if (child_ctx == NULL) {
1152 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1153 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1157 ret = pipe(child_ctx->fd);
1159 talloc_free(child_ctx);
1160 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1161 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1165 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1166 vacuum_handle->fast_path_count = 0;
1169 child_ctx->child_pid = fork();
1170 if (child_ctx->child_pid == (pid_t)-1) {
1171 close(child_ctx->fd[0]);
1172 close(child_ctx->fd[1]);
1173 talloc_free(child_ctx);
1174 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1175 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1180 if (child_ctx->child_pid == 0) {
1182 bool full_vacuum_run = false;
1183 close(child_ctx->fd[0]);
1185 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1187 if (switch_from_server_to_client(ctdb) != 0) {
1188 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1195 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1196 (vacuum_handle->fast_path_count == 0))
1198 full_vacuum_run = true;
1200 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1203 write(child_ctx->fd[1], &cc, 1);
1207 set_close_on_exec(child_ctx->fd[0]);
1208 close(child_ctx->fd[1]);
1210 child_ctx->status = VACUUM_RUNNING;
1211 child_ctx->start_time = timeval_current();
1213 DLIST_ADD(ctdb->vacuumers, child_ctx);
1214 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1217 * Clear the fastpath vacuuming list in the parent.
1219 talloc_free(ctdb_db->delete_queue);
1220 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1221 if (ctdb_db->delete_queue == NULL) {
1222 /* fatal here? ... */
1223 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1224 "in parent context. Shutting down\n");
1227 event_add_timed(ctdb->ev, child_ctx,
1228 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1229 vacuum_child_timeout, child_ctx);
1231 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1233 event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1234 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
1235 vacuum_child_handler,
1238 vacuum_handle->child_ctx = child_ctx;
1239 child_ctx->vacuum_handle = vacuum_handle;
1242 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1244 /* Simply free them all. */
1245 while (ctdb->vacuumers) {
1246 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1247 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1248 (int)ctdb->vacuumers->child_pid));
1249 /* vacuum_child_destructor kills it, removes from list */
1250 talloc_free(ctdb->vacuumers);
1254 /* this function initializes the vacuuming context for a database
1255 * starts the vacuuming events
1257 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1259 if (ctdb_db->persistent != 0) {
1260 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1264 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1265 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1267 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1268 ctdb_db->vacuum_handle->fast_path_count = 0;
1270 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1271 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1272 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1278 * Insert a record into the ctdb_db context's delete queue,
1279 * handling hash collisions.
1281 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1282 const struct ctdb_ltdb_header *hdr,
1285 struct delete_record_data *kd;
1289 hash = (uint32_t)tdb_jenkins_hash(&key);
1291 DEBUG(DEBUG_INFO, (__location__ " Schedule for deletion: db[%s] "
1295 "migrated_with_data[%s]\n",
1296 ctdb_db->db_name, ctdb_db->db_id,
1298 ctdb_lmaster(ctdb_db->ctdb, &key),
1299 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1301 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1303 if ((kd->key.dsize != key.dsize) ||
1304 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1307 ("schedule for deletion: Hash collision (0x%08x)."
1308 " Skipping the record.\n", hash));
1312 ("schedule for deletion: Overwriting entry for "
1313 "key with hash 0x%08x.\n", hash));
1317 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1318 ctdb_db->delete_queue,
1328 * Schedule a record for deletetion.
1329 * Called from the parent context.
1331 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1334 struct ctdb_control_schedule_for_deletion *dd;
1335 struct ctdb_db_context *ctdb_db;
1339 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1341 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1342 if (ctdb_db == NULL) {
1343 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1348 key.dsize = dd->keylen;
1351 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1356 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1357 const struct ctdb_ltdb_header *hdr,
1361 struct ctdb_control_schedule_for_deletion *dd;
1365 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1366 /* main daemon - directly queue */
1367 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1372 /* child process: send the main daemon a control */
1374 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1375 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1376 if (indata.dptr == NULL) {
1377 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1380 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1381 dd->db_id = ctdb_db->db_id;
1383 dd->keylen = key.dsize;
1384 memcpy(dd->key, key.dptr, key.dsize);
1386 ret = ctdb_control(ctdb_db->ctdb,
1389 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1390 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1395 NULL, /* timeout : NULL == wait forever */
1396 NULL); /* error message */
1398 talloc_free(indata.dptr);
1400 if (ret != 0 || status != 0) {
1401 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1402 "SCHEDULE_FOR_DELETION "