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/tevent/tevent.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/tevent/tevent.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;
54 /* a list of records to possibly delete */
56 uint32_t vacuum_limit;
57 uint32_t repack_limit;
58 struct ctdb_context *ctdb;
59 struct ctdb_db_context *ctdb_db;
60 struct tdb_context *dest_db;
61 trbt_tree_t *delete_tree;
62 uint32_t delete_count;
63 struct ctdb_marshall_buffer **list;
72 /* tuning information stored for every db */
73 struct vacuum_tuning_data {
74 uint32_t last_num_repack;
75 uint32_t last_num_empty;
76 uint32_t last_interval;
77 uint32_t new_interval;
78 struct timeval last_start;
82 /* this structure contains the information for one record to be deleted */
83 struct delete_record_data {
84 struct ctdb_context *ctdb;
85 struct ctdb_db_context *ctdb_db;
86 struct ctdb_ltdb_header hdr;
90 struct delete_records_list {
91 struct ctdb_marshall_buffer *records;
95 static int add_record_to_delete_tree(struct vacuum_data *vdata, TDB_DATA key,
96 struct ctdb_ltdb_header *hdr)
98 struct ctdb_context *ctdb = vdata->ctdb;
99 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
101 struct delete_record_data *dd;
103 hash = ctdb_hash(&key);
105 if (trbt_lookup32(vdata->delete_tree, hash)) {
106 DEBUG(DEBUG_DEBUG, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
110 /* store key and header indexed by the key hash */
111 dd = talloc_zero(vdata->delete_tree, struct delete_record_data);
113 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
117 dd->ctdb_db = ctdb_db;
118 dd->key.dsize = key.dsize;
119 dd->key.dptr = talloc_memdup(dd, key.dptr, key.dsize);
120 if (dd->key.dptr == NULL) {
121 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
127 trbt_insert32(vdata->delete_tree, hash, dd);
129 vdata->delete_count++;
135 * Add a record to the list of records to be sent
136 * to their lmaster with VACUUM_FETCH.
138 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
141 struct ctdb_context *ctdb = vdata->ctdb;
142 struct ctdb_rec_data *rec;
146 lmaster = ctdb_lmaster(ctdb, &key);
148 rec = ctdb_marshall_record(vdata->list[lmaster], ctdb->pnn, key, NULL, tdb_null);
150 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
151 vdata->traverse_error = true;
155 old_size = talloc_get_size(vdata->list[lmaster]);
156 vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster],
157 old_size + rec->length);
158 if (vdata->list[lmaster] == NULL) {
159 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
160 vdata->traverse_error = true;
164 vdata->list[lmaster]->count++;
165 memcpy(old_size+(uint8_t *)vdata->list[lmaster], rec, rec->length);
174 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
175 struct timeval t, void *private_data);
179 * traverse function for gathering the records that can be deleted
181 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
183 struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
184 struct ctdb_context *ctdb = vdata->ctdb;
186 struct ctdb_ltdb_header *hdr;
189 lmaster = ctdb_lmaster(ctdb, &key);
190 if (lmaster >= ctdb->num_nodes) {
191 DEBUG(DEBUG_CRIT, (__location__
192 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
195 (unsigned)ctdb->num_nodes,
196 (unsigned)ctdb_hash(&key)));
200 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
201 /* its not a deleted record */
205 hdr = (struct ctdb_ltdb_header *)data.dptr;
207 if (hdr->dmaster != ctdb->pnn) {
211 if (lmaster == ctdb->pnn) {
213 * We are both lmaster and dmaster, and the record * is empty.
214 * So we should be able to delete it.
216 res = add_record_to_delete_tree(vdata, key, hdr);
219 * We are not lmaster.
220 * Add the record to the blob ready to send to the nodes.
222 res = add_record_to_vacuum_fetch_list(vdata, key);
229 * traverse the tree of records to delete and marshall them into
232 static void delete_traverse(void *param, void *data)
234 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
235 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
236 struct ctdb_rec_data *rec;
239 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
241 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
245 old_size = talloc_get_size(recs->records);
246 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
247 if (recs->records == NULL) {
248 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
251 recs->records->count++;
252 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
256 * traverse function for the traversal of the delete_queue,
257 * the fast-path vacuuming list.
259 * - If the record has been migrated off the node
260 * or has been revived (filled with data) on the node,
261 * then skip the record.
263 * - If the current node is the record's lmaster and it is
264 * a record that has never been migrated with data, then
265 * delete the record from the local tdb.
267 * - If the current node is the record's lmaster and it has
268 * been migrated with data, then schedule it for the normal
269 * vacuuming procedure (i.e. add it to the delete_list).
271 * - If the current node is NOT the record's lmaster then
272 * add it to the list of records that are to be sent to
273 * the lmaster with the VACUUM_FETCH message.
275 static void delete_queue_traverse(void *param, void *data)
277 struct delete_record_data *dd =
278 talloc_get_type(data, struct delete_record_data);
279 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
280 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
281 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
283 struct ctdb_ltdb_header *header;
287 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
289 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
293 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
294 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
295 /* Does not exist or not a ctdb record. Skip. */
299 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
300 /* The record has been recycled (filled with data). Skip. */
304 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
306 if (header->dmaster != ctdb->pnn) {
307 /* The record has been migrated off the node. Skip. */
312 if (header->rsn != dd->hdr.rsn) {
314 * The record has been migrated off the node and back again.
315 * But not requeued for deletion. Skip it.
321 * We are dmaster, and the record has no data, and it has
322 * not been migrated after it has been queued for deletion.
324 * At this stage, the record could still have been revived locally
325 * and last been written with empty data. This can only be
326 * fixed with the addition of an active or delete flag. (TODO)
329 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
331 if (lmaster != ctdb->pnn) {
332 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
336 (__location__ " Error adding record to list "
337 "of records to send to lmaster.\n"));
343 /* use header->flags or dd->hdr.flags ?? */
344 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
345 res = add_record_to_delete_tree(vdata, dd->key, &dd->hdr);
349 (__location__ " Error adding record to list "
350 "of records for deletion on lmaster.\n"));
353 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
357 (__location__ " Error deleting record from local "
363 if (tdb_data.dptr != NULL) {
366 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
372 * read-only traverse the database in order to find
373 * records that can be deleted and try to delete these
374 * records on the other nodes
375 * this executes in the child context
377 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *vdata)
379 struct ctdb_context *ctdb = ctdb_db->ctdb;
380 const char *name = ctdb_db->db_name;
383 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
385 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
389 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
391 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
396 /* the list needs to be of length num_nodes */
397 vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->num_nodes);
398 if (vdata->list == NULL) {
399 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
402 for (i = 0; i < ctdb->num_nodes; i++) {
403 vdata->list[i] = (struct ctdb_marshall_buffer *)
404 talloc_zero_size(vdata->list,
405 offsetof(struct ctdb_marshall_buffer, data));
406 if (vdata->list[i] == NULL) {
407 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
410 vdata->list[i]->db_id = ctdb_db->db_id;
414 * Traverse the delete_queue.
415 * This builds the same lists as the db traverse.
417 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
419 /* read-only traverse, looking for records that might be able to be vacuumed */
420 if (tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata) == -1 ||
421 vdata->traverse_error) {
422 DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
427 * For records where we are not the lmaster,
428 * tell the lmaster to fetch the record.
430 for (i = 0; i < ctdb->num_nodes; i++) {
433 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
437 if (vdata->list[i]->count == 0) {
441 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
442 vdata->list[i]->count, ctdb->nodes[i]->pnn,
445 data.dsize = talloc_get_size(vdata->list[i]);
446 data.dptr = (void *)vdata->list[i];
447 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
448 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
449 "fetch message to %u\n",
450 ctdb->nodes[i]->pnn));
455 /* Process all records we can delete (if any) */
456 if (vdata->delete_count > 0) {
457 struct delete_records_list *recs;
458 TDB_DATA indata, outdata;
460 struct ctdb_node_map *nodemap;
461 uint32_t *active_nodes;
462 int num_active_nodes;
464 recs = talloc_zero(vdata, struct delete_records_list);
466 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
469 recs->records = (struct ctdb_marshall_buffer *)
470 talloc_zero_size(vdata,
471 offsetof(struct ctdb_marshall_buffer, data));
472 if (recs->records == NULL) {
473 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
476 recs->records->db_id = ctdb_db->db_id;
479 * traverse the tree of all records we want to delete and
480 * create a blob we can send to the other nodes.
482 trbt_traversearray32(vdata->delete_tree, 1, delete_traverse, recs);
484 indata.dsize = talloc_get_size(recs->records);
485 indata.dptr = (void *)recs->records;
488 * now tell all the active nodes to delete all these records
492 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
494 recs, /* talloc context */
497 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
501 active_nodes = list_of_active_nodes(ctdb, nodemap,
502 nodemap, /* talloc context */
503 false /* include self */);
505 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
507 for (i = 0; i < num_active_nodes; i++) {
508 struct ctdb_marshall_buffer *records;
509 struct ctdb_rec_data *rec;
511 ret = ctdb_control(ctdb, active_nodes[i], 0,
512 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
513 indata, recs, &outdata, &res,
515 if (ret != 0 || res != 0) {
516 DEBUG(DEBUG_ERR, ("Failed to delete records on "
517 "node %u: ret[%d] res[%d]\n",
518 active_nodes[i], ret, res));
523 * outdata countains the list of records coming back
524 * from the node which the node could not delete
526 records = (struct ctdb_marshall_buffer *)outdata.dptr;
527 rec = (struct ctdb_rec_data *)&records->data[0];
528 while (records->count-- > 1) {
529 TDB_DATA reckey, recdata;
530 struct ctdb_ltdb_header *rechdr;
532 reckey.dptr = &rec->data[0];
533 reckey.dsize = rec->keylen;
534 recdata.dptr = &rec->data[reckey.dsize];
535 recdata.dsize = rec->datalen;
537 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
538 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
541 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
542 recdata.dptr += sizeof(*rechdr);
543 recdata.dsize -= sizeof(*rechdr);
546 * that other node couldnt delete the record
547 * so we should delete it and thereby remove it from the tree
549 talloc_free(trbt_lookup32(vdata->delete_tree, ctdb_hash(&reckey)));
551 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
555 /* free nodemap and active_nodes */
556 talloc_free(nodemap);
559 * The only records remaining in the tree would be those
560 * records where all other nodes could successfully
561 * delete them, so we can safely delete them on the
562 * lmaster as well. Deletion implictely happens while
563 * we repack the database. The repack algorithm revisits
564 * the tree in order to find the records that don't need
565 * to be copied / repacked.
569 /* this ensures we run our event queue */
570 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
577 * traverse function for repacking
579 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
581 struct vacuum_data *vdata = (struct vacuum_data *)private;
584 uint32_t hash = ctdb_hash(&key);
585 struct delete_record_data *kd;
587 * check if we can ignore this record because it's in the delete_tree
589 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_tree, hash);
591 * there might be hash collisions so we have to compare the keys here to be sure
593 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
594 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
596 * we have to check if the record hasn't changed in the meantime in order to
597 * savely remove it from the database
599 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
600 hdr->dmaster == kd->ctdb->pnn &&
601 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
602 kd->hdr.rsn == hdr->rsn) {
608 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
609 vdata->traverse_error = true;
619 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
621 struct tdb_context *tmp_db;
623 if (tdb_transaction_start(tdb) != 0) {
624 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
628 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
629 TDB_INTERNAL|TDB_DISALLOW_NESTING,
631 if (tmp_db == NULL) {
632 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
633 tdb_transaction_cancel(tdb);
637 vdata->traverse_error = false;
638 vdata->dest_db = tmp_db;
639 vdata->vacuum = true;
644 * repack and vacuum on-the-fly by not writing the records that are
647 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
648 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
649 tdb_transaction_cancel(tdb);
654 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
656 if (vdata->traverse_error) {
657 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
658 tdb_transaction_cancel(tdb);
663 if (tdb_wipe_all(tdb) != 0) {
664 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
665 tdb_transaction_cancel(tdb);
670 vdata->traverse_error = false;
671 vdata->dest_db = tdb;
672 vdata->vacuum = false;
675 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
676 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
677 tdb_transaction_cancel(tdb);
682 if (vdata->traverse_error) {
683 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
684 tdb_transaction_cancel(tdb);
692 if (tdb_transaction_commit(tdb) != 0) {
693 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
696 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
701 static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *vdata, uint32_t freelist)
703 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
704 TDB_CONTEXT *tune_tdb;
706 struct vacuum_tuning_data tdata;
707 struct vacuum_tuning_data *tptr;
711 vac_dbname = talloc_asprintf(tmp_ctx, "%s/%s.%u",
712 ctdb_db->ctdb->db_directory_state,
713 TUNINGDBNAME, ctdb_db->ctdb->pnn);
714 if (vac_dbname == NULL) {
715 DEBUG(DEBUG_CRIT,(__location__ " Out of memory error while allocating '%s'\n", vac_dbname));
716 talloc_free(tmp_ctx);
720 flags = ctdb_db->ctdb->valgrinding ? TDB_NOMMAP : 0;
721 flags |= TDB_DISALLOW_NESTING;
722 tune_tdb = tdb_open(vac_dbname, 0,
724 O_RDWR|O_CREAT, 0600);
725 if (tune_tdb == NULL) {
726 DEBUG(DEBUG_ERR,(__location__ " Failed to create/open %s\n", TUNINGDBNAME));
727 talloc_free(tmp_ctx);
731 if (tdb_transaction_start(tune_tdb) != 0) {
732 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
736 key.dptr = discard_const(ctdb_db->db_name);
737 key.dsize = strlen(ctdb_db->db_name);
738 value = tdb_fetch(tune_tdb, key);
740 if (value.dptr != NULL && value.dsize == sizeof(struct vacuum_tuning_data)) {
741 tptr = (struct vacuum_tuning_data *)value.dptr;
745 * re-calc new vacuum interval:
746 * in case no limit was reached we continously increase the interval
747 * until vacuum_max_interval is reached
748 * in case a limit was reached we divide the current interval by 2
749 * unless vacuum_min_interval is reached
751 if (freelist < vdata->repack_limit &&
752 vdata->delete_count < vdata->vacuum_limit) {
753 if (tdata.last_interval < ctdb_db->ctdb->tunable.vacuum_max_interval) {
754 tdata.new_interval = tdata.last_interval * 110 / 100;
755 DEBUG(DEBUG_INFO,("Increasing vacuum interval %u -> %u for %s\n",
756 tdata.last_interval, tdata.new_interval, ctdb_db->db_name));
759 tdata.new_interval = tdata.last_interval / 2;
760 if (tdata.new_interval < ctdb_db->ctdb->tunable.vacuum_min_interval ||
761 tdata.new_interval > ctdb_db->ctdb->tunable.vacuum_max_interval) {
762 tdata.new_interval = ctdb_db->ctdb->tunable.vacuum_min_interval;
764 DEBUG(DEBUG_INFO,("Decreasing vacuum interval %u -> %u for %s\n",
765 tdata.last_interval, tdata.new_interval, ctdb_db->db_name));
767 tdata.last_interval = tdata.new_interval;
769 DEBUG(DEBUG_DEBUG,(__location__ " Cannot find tunedb record for %s. Using default interval\n", ctdb_db->db_name));
770 tdata.last_num_repack = freelist;
771 tdata.last_num_empty = vdata->delete_count;
772 tdata.last_interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
775 if (value.dptr != NULL) {
779 tdata.last_start = vdata->start;
780 tdata.last_duration = timeval_elapsed(&vdata->start);
782 value.dptr = (unsigned char *)&tdata;
783 value.dsize = sizeof(tdata);
785 if (tdb_store(tune_tdb, key, value, 0) != 0) {
786 DEBUG(DEBUG_ERR,(__location__ " Unable to store tundb record for %s\n", ctdb_db->db_name));
787 tdb_transaction_cancel(tune_tdb);
789 talloc_free(tmp_ctx);
792 tdb_transaction_commit(tune_tdb);
794 talloc_free(tmp_ctx);
800 * repack and vaccum a db
801 * called from the child context
803 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
806 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
807 uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
808 const char *name = ctdb_db->db_name;
810 struct vacuum_data *vdata;
812 size = tdb_freelist_size(ctdb_db->ltdb->tdb);
814 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
818 vdata = talloc_zero(mem_ctx, struct vacuum_data);
820 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
824 vdata->ctdb = ctdb_db->ctdb;
825 vdata->vacuum_limit = vacuum_limit;
826 vdata->repack_limit = repack_limit;
827 vdata->delete_tree = trbt_create(vdata, 0);
828 vdata->ctdb_db = ctdb_db;
829 if (vdata->delete_tree == NULL) {
830 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
835 vdata->start = timeval_current();
838 * gather all records that can be deleted in vdata
840 if (ctdb_vacuum_db(ctdb_db, vdata) != 0) {
841 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
845 * decide if a repack is necessary
847 if (size < repack_limit && vdata->delete_count < vacuum_limit) {
848 update_tuning_db(ctdb_db, vdata, size);
853 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
854 name, size, vdata->delete_count));
857 * repack and implicitely get rid of the records we can delete
859 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
860 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
861 update_tuning_db(ctdb_db, vdata, size);
865 update_tuning_db(ctdb_db, vdata, size);
871 static int get_vacuum_interval(struct ctdb_db_context *ctdb_db)
873 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
877 uint interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
878 struct ctdb_context *ctdb = ctdb_db->ctdb;
881 vac_dbname = talloc_asprintf(tmp_ctx, "%s/%s.%u", ctdb->db_directory, TUNINGDBNAME, ctdb->pnn);
882 if (vac_dbname == NULL) {
883 DEBUG(DEBUG_CRIT,(__location__ " Out of memory error while allocating '%s'\n", vac_dbname));
884 talloc_free(tmp_ctx);
888 flags = ctdb_db->ctdb->valgrinding ? TDB_NOMMAP : 0;
889 flags |= TDB_DISALLOW_NESTING;
890 tdb = tdb_open(vac_dbname, 0,
892 O_RDWR|O_CREAT, 0600);
894 DEBUG(DEBUG_ERR,("Unable to open/create database %s using default interval. Errno : %s (%d)\n", vac_dbname, strerror(errno), errno));
895 talloc_free(tmp_ctx);
899 key.dptr = discard_const(ctdb_db->db_name);
900 key.dsize = strlen(ctdb_db->db_name);
902 value = tdb_fetch(tdb, key);
904 if (value.dptr != NULL) {
905 if (value.dsize == sizeof(struct vacuum_tuning_data)) {
906 struct vacuum_tuning_data *tptr = (struct vacuum_tuning_data *)value.dptr;
908 interval = tptr->new_interval;
910 if (interval < ctdb->tunable.vacuum_min_interval) {
911 interval = ctdb->tunable.vacuum_min_interval;
913 if (interval > ctdb->tunable.vacuum_max_interval) {
914 interval = ctdb->tunable.vacuum_max_interval;
921 talloc_free(tmp_ctx);
926 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
928 double l = timeval_elapsed(&child_ctx->start_time);
929 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
930 struct ctdb_context *ctdb = ctdb_db->ctdb;
932 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
934 if (child_ctx->child_pid != -1) {
935 kill(child_ctx->child_pid, SIGKILL);
938 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
940 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
941 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
942 ctdb_vacuum_event, child_ctx->vacuum_handle);
948 * this event is generated when a vacuum child process times out
950 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
951 struct timeval t, void *private_data)
953 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
955 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
957 child_ctx->status = VACUUM_TIMEOUT;
959 talloc_free(child_ctx);
964 * this event is generated when a vacuum child process has completed
966 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
967 uint16_t flags, void *private_data)
969 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
973 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
974 child_ctx->child_pid = -1;
976 ret = read(child_ctx->fd[0], &c, 1);
977 if (ret != 1 || c != 0) {
978 child_ctx->status = VACUUM_ERROR;
979 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));
981 child_ctx->status = VACUUM_OK;
984 talloc_free(child_ctx);
988 * this event is called every time we need to start a new vacuum process
991 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
992 struct timeval t, void *private_data)
994 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
995 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
996 struct ctdb_context *ctdb = ctdb_db->ctdb;
997 struct ctdb_vacuum_child_context *child_ctx;
998 struct tevent_fd *fde;
1001 /* we dont vacuum if we are in recovery mode, or db frozen */
1002 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1003 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1004 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1005 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1006 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1009 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1013 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1014 if (child_ctx == NULL) {
1015 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1016 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1020 ret = pipe(child_ctx->fd);
1022 talloc_free(child_ctx);
1023 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1024 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1028 child_ctx->child_pid = ctdb_fork(ctdb);
1029 if (child_ctx->child_pid == (pid_t)-1) {
1030 close(child_ctx->fd[0]);
1031 close(child_ctx->fd[1]);
1032 talloc_free(child_ctx);
1033 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1034 event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
1039 if (child_ctx->child_pid == 0) {
1041 close(child_ctx->fd[0]);
1043 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1045 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1046 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1053 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx);
1055 write(child_ctx->fd[1], &cc, 1);
1059 set_close_on_exec(child_ctx->fd[0]);
1060 close(child_ctx->fd[1]);
1062 child_ctx->status = VACUUM_RUNNING;
1063 child_ctx->start_time = timeval_current();
1065 DLIST_ADD(ctdb->vacuumers, child_ctx);
1066 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1069 * Clear the fastpath vacuuming list in the parent.
1071 talloc_free(ctdb_db->delete_queue);
1072 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1073 if (ctdb_db->delete_queue == NULL) {
1074 /* fatal here? ... */
1075 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1076 "in parent context. Shutting down\n");
1079 event_add_timed(ctdb->ev, child_ctx,
1080 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1081 vacuum_child_timeout, child_ctx);
1083 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1085 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1086 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1087 tevent_fd_set_auto_close(fde);
1089 vacuum_handle->child_ctx = child_ctx;
1090 child_ctx->vacuum_handle = vacuum_handle;
1093 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1095 /* Simply free them all. */
1096 while (ctdb->vacuumers) {
1097 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1098 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1099 (int)ctdb->vacuumers->child_pid));
1100 /* vacuum_child_destructor kills it, removes from list */
1101 talloc_free(ctdb->vacuumers);
1105 /* this function initializes the vacuuming context for a database
1106 * starts the vacuuming events
1108 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1110 if (ctdb_db->persistent != 0) {
1111 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1115 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1116 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1118 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1120 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1121 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1122 ctdb_vacuum_event, ctdb_db->vacuum_handle);