4 Copyright (C) Ronnie Sahlberg 2009
5 Copyright (C) Michael Adam 2010-2013
6 Copyright (C) Stefan Metzmacher 2010-2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.h"
29 #include "lib/util/dlinklist.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
35 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
37 struct ctdb_vacuum_child_context {
38 struct ctdb_vacuum_child_context *next, *prev;
39 struct ctdb_vacuum_handle *vacuum_handle;
40 /* fd child writes status to */
43 enum vacuum_child_status status;
44 struct timeval start_time;
47 struct ctdb_vacuum_handle {
48 struct ctdb_db_context *ctdb_db;
49 struct ctdb_vacuum_child_context *child_ctx;
50 uint32_t fast_path_count;
54 /* a list of records to possibly delete */
56 uint32_t repack_limit;
57 struct ctdb_context *ctdb;
58 struct ctdb_db_context *ctdb_db;
59 struct tdb_context *dest_db;
60 trbt_tree_t *delete_list;
61 uint32_t delete_count;
62 struct ctdb_marshall_buffer **vacuum_fetch_list;
69 uint32_t fast_added_to_vacuum_fetch_list;
70 uint32_t fast_added_to_delete_list;
71 uint32_t fast_deleted;
72 uint32_t fast_skipped;
75 uint32_t full_scheduled;
76 uint32_t full_skipped;
80 uint32_t delete_remote_error;
81 uint32_t delete_local_error;
82 uint32_t delete_deleted;
83 uint32_t delete_skipped;
86 /* this structure contains the information for one record to be deleted */
87 struct delete_record_data {
88 struct ctdb_context *ctdb;
89 struct ctdb_db_context *ctdb_db;
90 struct ctdb_ltdb_header hdr;
95 struct delete_records_list {
96 struct ctdb_marshall_buffer *records;
97 struct vacuum_data *vdata;
100 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
101 const struct ctdb_ltdb_header *hdr,
105 * Store key and header in a tree, indexed by the key hash.
107 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
108 struct ctdb_db_context *ctdb_db,
110 const struct ctdb_ltdb_header *hdr,
113 struct delete_record_data *dd;
117 len = offsetof(struct delete_record_data, keydata) + key.dsize;
119 dd = (struct delete_record_data *)talloc_size(tree, len);
121 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
124 talloc_set_name_const(dd, "struct delete_record_data");
127 dd->ctdb_db = ctdb_db;
128 dd->key.dsize = key.dsize;
129 dd->key.dptr = dd->keydata;
130 memcpy(dd->keydata, key.dptr, key.dsize);
134 hash = ctdb_hash(&key);
136 trbt_insert32(tree, hash, dd);
141 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
142 struct ctdb_ltdb_header *hdr)
144 struct ctdb_context *ctdb = vdata->ctdb;
145 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
149 hash = ctdb_hash(&key);
151 if (trbt_lookup32(vdata->delete_list, hash)) {
152 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
156 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
163 vdata->delete_count++;
169 * Add a record to the list of records to be sent
170 * to their lmaster with VACUUM_FETCH.
172 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
175 struct ctdb_context *ctdb = vdata->ctdb;
176 struct ctdb_rec_data *rec;
179 struct ctdb_marshall_buffer *vfl;
181 lmaster = ctdb_lmaster(ctdb, &key);
183 vfl = vdata->vacuum_fetch_list[lmaster];
185 rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
187 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
188 vdata->traverse_error = true;
192 old_size = talloc_get_size(vfl);
193 vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
195 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
196 vdata->traverse_error = true;
199 vdata->vacuum_fetch_list[lmaster] = vfl;
202 memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
211 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
212 struct timeval t, void *private_data);
214 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
216 struct ctdb_ltdb_header *header =
217 (struct ctdb_ltdb_header *)private_data;
219 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
223 *header = *(struct ctdb_ltdb_header *)data.dptr;
229 * traverse function for gathering the records that can be deleted
231 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
234 struct vacuum_data *vdata = talloc_get_type(private_data,
236 struct ctdb_context *ctdb = vdata->ctdb;
237 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
239 struct ctdb_ltdb_header *hdr;
244 lmaster = ctdb_lmaster(ctdb, &key);
245 if (lmaster >= ctdb->num_nodes) {
247 DEBUG(DEBUG_CRIT, (__location__
248 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
251 (unsigned)ctdb->num_nodes,
252 (unsigned)ctdb_hash(&key)));
256 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
257 /* it is not a deleted record */
258 vdata->full_skipped++;
262 hdr = (struct ctdb_ltdb_header *)data.dptr;
264 if (hdr->dmaster != ctdb->pnn) {
265 vdata->full_skipped++;
270 * Add the record to this process's delete_queue for processing
271 * in the subsequent traverse in the fast vacuum run.
273 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
277 vdata->full_scheduled++;
284 * traverse the tree of records to delete and marshall them into
287 static int delete_marshall_traverse(void *param, void *data)
289 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
290 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
291 struct ctdb_rec_data *rec;
294 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
296 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
300 old_size = talloc_get_size(recs->records);
301 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
302 if (recs->records == NULL) {
303 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
306 recs->records->count++;
307 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
312 * Variant of delete_marshall_traverse() that bumps the
313 * RSN of each traversed record in the database.
315 * This is needed to ensure that when rolling out our
316 * empty record copy before remote deletion, we as the
317 * record's dmaster keep a higher RSN than the non-dmaster
318 * nodes. This is needed to prevent old copies from
319 * resurrection in recoveries.
321 static int delete_marshall_traverse_first(void *param, void *data)
323 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
324 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
325 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
326 struct ctdb_context *ctdb = ctdb_db->ctdb;
327 struct ctdb_ltdb_header header;
329 uint32_t hash = ctdb_hash(&(dd->key));
332 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
335 (__location__ " Error getting chainlock on record with "
336 "key hash [0x%08x] on database db[%s].\n",
337 hash, ctdb_db->db_name));
338 recs->vdata->delete_skipped++;
344 * Verify that the record is still empty, its RSN has not
345 * changed and that we are still its lmaster and dmaster.
348 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
349 vacuum_record_parser, &header);
354 if (header.flags & CTDB_REC_RO_FLAGS) {
355 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
356 "on database db[%s] has read-only flags. "
358 hash, ctdb_db->db_name));
362 if (header.dmaster != ctdb->pnn) {
363 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
364 "on database db[%s] has been migrated away. "
366 hash, ctdb_db->db_name));
370 if (header.rsn != dd->hdr.rsn) {
371 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
372 "on database db[%s] seems to have been "
373 "migrated away and back again (with empty "
374 "data). skipping.\n",
375 hash, ctdb_db->db_name));
379 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
381 if (lmaster != ctdb->pnn) {
382 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
383 "delete list (key hash [0x%08x], db[%s]). "
384 "Strange! skipping.\n",
385 hash, ctdb_db->db_name));
390 * Increment the record's RSN to ensure the dmaster (i.e. the current
391 * node) has the highest RSN of the record in the cluster.
392 * This is to prevent old record copies from resurrecting in recoveries
393 * if something should fail during the deletion process.
394 * Note that ctdb_ltdb_store_server() increments the RSN if called
395 * on the record's dmaster.
398 res = ctdb_ltdb_store(ctdb_db, dd->key, &header, tdb_null);
400 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
401 "key hash [0x%08x] on database db[%s].\n",
402 hash, ctdb_db->db_name));
406 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
411 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
413 recs->vdata->delete_skipped++;
422 return delete_marshall_traverse(param, data);
426 * traverse function for the traversal of the delete_queue,
427 * the fast-path vacuuming list.
429 * - If the record has been migrated off the node
430 * or has been revived (filled with data) on the node,
431 * then skip the record.
433 * - If the current node is the record's lmaster and it is
434 * a record that has never been migrated with data, then
435 * delete the record from the local tdb.
437 * - If the current node is the record's lmaster and it has
438 * been migrated with data, then schedule it for the normal
439 * vacuuming procedure (i.e. add it to the delete_list).
441 * - If the current node is NOT the record's lmaster then
442 * add it to the list of records that are to be sent to
443 * the lmaster with the VACUUM_FETCH message.
445 static int delete_queue_traverse(void *param, void *data)
447 struct delete_record_data *dd =
448 talloc_get_type(data, struct delete_record_data);
449 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
450 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
451 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
453 struct ctdb_ltdb_header header;
455 uint32_t hash = ctdb_hash(&(dd->key));
459 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
462 (__location__ " Error getting chainlock on record with "
463 "key hash [0x%08x] on database db[%s].\n",
464 hash, ctdb_db->db_name));
469 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
470 vacuum_record_parser, &header);
475 if (header.dmaster != ctdb->pnn) {
476 /* The record has been migrated off the node. Skip. */
480 if (header.rsn != dd->hdr.rsn) {
482 * The record has been migrated off the node and back again.
483 * But not requeued for deletion. Skip it.
489 * We are dmaster, and the record has no data, and it has
490 * not been migrated after it has been queued for deletion.
492 * At this stage, the record could still have been revived locally
493 * and last been written with empty data. This can only be
494 * fixed with the addition of an active or delete flag. (TODO)
497 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
499 if (lmaster != ctdb->pnn) {
500 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
504 (__location__ " Error adding record to list "
505 "of records to send to lmaster.\n"));
508 vdata->fast_added_to_vacuum_fetch_list++;
513 /* use header->flags or dd->hdr.flags ?? */
514 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
515 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
519 (__location__ " Error adding record to list "
520 "of records for deletion on lmaster.\n"));
523 vdata->fast_added_to_delete_list++;
526 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
530 (__location__ " Error deleting record with key "
531 "hash [0x%08x] from local data base db[%s].\n",
532 hash, ctdb_db->db_name));
538 (__location__ " Deleted record with key hash "
539 "[0x%08x] from local data base db[%s].\n",
540 hash, ctdb_db->db_name));
541 vdata->fast_deleted++;
547 vdata->fast_skipped++;
550 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
556 * Delete the records that we are lmaster and dmaster for and
557 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
560 static int delete_record_traverse(void *param, void *data)
562 struct delete_record_data *dd =
563 talloc_get_type(data, struct delete_record_data);
564 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
565 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
566 struct ctdb_context *ctdb = ctdb_db->ctdb;
568 struct ctdb_ltdb_header header;
570 uint32_t hash = ctdb_hash(&(dd->key));
572 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
575 (__location__ " Error getting chainlock on record with "
576 "key hash [0x%08x] on database db[%s].\n",
577 hash, ctdb_db->db_name));
578 vdata->delete_local_error++;
579 vdata->delete_left--;
585 * Verify that the record is still empty, its RSN has not
586 * changed and that we are still its lmaster and dmaster.
589 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
590 vacuum_record_parser, &header);
595 if (header.flags & CTDB_REC_RO_FLAGS) {
596 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
597 "on database db[%s] has read-only flags. "
599 hash, ctdb_db->db_name));
603 if (header.dmaster != ctdb->pnn) {
604 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
605 "on database db[%s] has been migrated away. "
607 hash, ctdb_db->db_name));
611 if (header.rsn != dd->hdr.rsn + 1) {
613 * The record has been migrated off the node and back again.
614 * But not requeued for deletion. Skip it.
615 * (Note that the first marshall traverse has bumped the RSN
618 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
619 "on database db[%s] seems to have been "
620 "migrated away and back again (with empty "
621 "data). skipping.\n",
622 hash, ctdb_db->db_name));
626 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
628 if (lmaster != ctdb->pnn) {
629 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
630 "delete list (key hash [0x%08x], db[%s]). "
631 "Strange! skipping.\n",
632 hash, ctdb_db->db_name));
636 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
640 (__location__ " Error deleting record with key hash "
641 "[0x%08x] from local data base db[%s].\n",
642 hash, ctdb_db->db_name));
643 vdata->delete_local_error++;
648 (__location__ " Deleted record with key hash [0x%08x] from "
649 "local data base db[%s].\n", hash, ctdb_db->db_name));
651 vdata->delete_deleted++;
655 vdata->delete_skipped++;
658 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
661 vdata->delete_left--;
667 * Traverse the delete_queue.
668 * Records are either deleted directly or filled
669 * into the delete list or the vacuum fetch lists
670 * for further processing.
672 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
673 struct vacuum_data *vdata)
677 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
679 sum = vdata->fast_deleted
680 + vdata->fast_skipped
682 + vdata->fast_added_to_delete_list
683 + vdata->fast_added_to_vacuum_fetch_list;
685 if (vdata->fast_total != sum) {
686 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
687 "counts for db[%s]: total[%u] != sum[%u]\n",
688 ctdb_db->db_name, (unsigned)vdata->fast_total,
692 if (vdata->fast_total > 0) {
695 " fast vacuuming delete_queue traverse statistics: "
704 (unsigned)vdata->fast_total,
705 (unsigned)vdata->fast_deleted,
706 (unsigned)vdata->fast_skipped,
707 (unsigned)vdata->fast_error,
708 (unsigned)vdata->fast_added_to_delete_list,
709 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
716 * read-only traverse of the database, looking for records that
717 * might be able to be vacuumed.
719 * This is not done each time but only every tunable
720 * VacuumFastPathCount times.
722 static int ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
723 struct vacuum_data *vdata)
727 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
728 if (ret == -1 || vdata->traverse_error) {
729 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
730 "'%s'\n", ctdb_db->db_name));
734 if (vdata->full_total > 0) {
737 " full vacuuming db traverse statistics: "
744 (unsigned)vdata->full_total,
745 (unsigned)vdata->full_skipped,
746 (unsigned)vdata->full_error,
747 (unsigned)vdata->full_scheduled));
754 * Process the vacuum fetch lists:
755 * For records for which we are not the lmaster, tell the lmaster to
758 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
759 struct vacuum_data *vdata)
762 struct ctdb_context *ctdb = ctdb_db->ctdb;
764 for (i = 0; i < ctdb->num_nodes; i++) {
766 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
768 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
772 if (vfl->count == 0) {
776 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
777 vfl->count, ctdb->nodes[i]->pnn,
780 data.dsize = talloc_get_size(vfl);
781 data.dptr = (void *)vfl;
782 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
783 CTDB_SRVID_VACUUM_FETCH,
786 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
787 "fetch message to %u\n",
788 ctdb->nodes[i]->pnn));
797 * Process the delete list:
799 * This is the last step of vacuuming that consistently deletes
800 * those records that have been migrated with data and can hence
801 * not be deleted when leaving a node.
803 * In this step, the lmaster does the final deletion of those empty
804 * records that it is also dmaster for. It has ususally received
805 * at least some of these records previously from the former dmasters
806 * with the vacuum fetch message.
808 * This last step is implemented as a 3-phase process to protect from
809 * races leading to data corruption:
811 * 1) Send the lmaster's copy to all other active nodes with the
812 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
813 * 2) Send the records that could successfully be stored remotely
814 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
815 * control. The remote notes delete their local copy.
816 * 3) The lmaster locally deletes its copies of all records that
817 * could successfully be deleted remotely in step #2.
819 static int ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
820 struct vacuum_data *vdata)
823 struct ctdb_context *ctdb = ctdb_db->ctdb;
824 struct delete_records_list *recs;
826 struct ctdb_node_map *nodemap;
827 uint32_t *active_nodes;
828 int num_active_nodes;
832 if (vdata->delete_count == 0) {
836 tmp_ctx = talloc_new(vdata);
837 if (tmp_ctx == NULL) {
838 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
842 vdata->delete_left = vdata->delete_count;
845 * get the list of currently active nodes
848 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
853 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
858 active_nodes = list_of_active_nodes(ctdb, nodemap,
859 nodemap, /* talloc context */
860 false /* include self */);
862 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
865 * Now delete the records all active nodes in a three-phase process:
866 * 1) send all active remote nodes the current empty copy with this
868 * 2) if all nodes could store the new copy,
869 * tell all the active remote nodes to delete all their copy
870 * 3) if all remote nodes deleted their record copy, delete it locally
875 * Send currently empty record copy to all active nodes for storing.
878 recs = talloc_zero(tmp_ctx, struct delete_records_list);
880 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
884 recs->records = (struct ctdb_marshall_buffer *)
885 talloc_zero_size(recs,
886 offsetof(struct ctdb_marshall_buffer, data));
887 if (recs->records == NULL) {
888 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
892 recs->records->db_id = ctdb_db->db_id;
896 * traverse the tree of all records we want to delete and
897 * create a blob we can send to the other nodes.
899 * We call delete_marshall_traverse_first() to bump the
900 * records' RSNs in the database, to ensure we (as dmaster)
901 * keep the highest RSN of the records in the cluster.
903 trbt_traversearray32(vdata->delete_list, 1,
904 delete_marshall_traverse_first, recs);
906 indata.dsize = talloc_get_size(recs->records);
907 indata.dptr = (void *)recs->records;
909 for (i = 0; i < num_active_nodes; i++) {
910 struct ctdb_marshall_buffer *records;
911 struct ctdb_rec_data *rec;
915 ret = ctdb_control(ctdb, active_nodes[i], 0,
916 CTDB_CONTROL_RECEIVE_RECORDS, 0,
917 indata, recs, &outdata, &res,
919 if (ret != 0 || res != 0) {
920 DEBUG(DEBUG_ERR, ("Error storing record copies on "
921 "node %u: ret[%d] res[%d]\n",
922 active_nodes[i], ret, res));
928 * outdata contains the list of records coming back
929 * from the node: These are the records that the
930 * remote node could not store. We remove these from
931 * the list to process further.
933 records = (struct ctdb_marshall_buffer *)outdata.dptr;
934 rec = (struct ctdb_rec_data *)&records->data[0];
935 while (records->count-- > 1) {
936 TDB_DATA reckey, recdata;
937 struct ctdb_ltdb_header *rechdr;
938 struct delete_record_data *dd;
940 reckey.dptr = &rec->data[0];
941 reckey.dsize = rec->keylen;
942 recdata.dptr = &rec->data[reckey.dsize];
943 recdata.dsize = rec->datalen;
945 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
946 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
950 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
951 recdata.dptr += sizeof(*rechdr);
952 recdata.dsize -= sizeof(*rechdr);
954 dd = (struct delete_record_data *)trbt_lookup32(
959 * The other node could not store the record
960 * copy and it is the first node that failed.
961 * So we should remove it from the tree and
965 vdata->delete_remote_error++;
966 vdata->delete_left--;
969 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
973 if (vdata->delete_left == 0) {
979 * Send the remaining records to all active nodes for deletion.
981 * The lmaster's (i.e. our) copies of these records have been stored
982 * successfully on the other nodes.
986 * Create a marshall blob from the remaining list of records to delete.
989 talloc_free(recs->records);
991 recs->records = (struct ctdb_marshall_buffer *)
992 talloc_zero_size(recs,
993 offsetof(struct ctdb_marshall_buffer, data));
994 if (recs->records == NULL) {
995 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
999 recs->records->db_id = ctdb_db->db_id;
1001 trbt_traversearray32(vdata->delete_list, 1,
1002 delete_marshall_traverse, recs);
1004 indata.dsize = talloc_get_size(recs->records);
1005 indata.dptr = (void *)recs->records;
1007 for (i = 0; i < num_active_nodes; i++) {
1008 struct ctdb_marshall_buffer *records;
1009 struct ctdb_rec_data *rec;
1013 ret = ctdb_control(ctdb, active_nodes[i], 0,
1014 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1015 indata, recs, &outdata, &res,
1017 if (ret != 0 || res != 0) {
1018 DEBUG(DEBUG_ERR, ("Failed to delete records on "
1019 "node %u: ret[%d] res[%d]\n",
1020 active_nodes[i], ret, res));
1026 * outdata contains the list of records coming back
1027 * from the node: These are the records that the
1028 * remote node could not delete. We remove these from
1029 * the list to delete locally.
1031 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1032 rec = (struct ctdb_rec_data *)&records->data[0];
1033 while (records->count-- > 1) {
1034 TDB_DATA reckey, recdata;
1035 struct ctdb_ltdb_header *rechdr;
1036 struct delete_record_data *dd;
1038 reckey.dptr = &rec->data[0];
1039 reckey.dsize = rec->keylen;
1040 recdata.dptr = &rec->data[reckey.dsize];
1041 recdata.dsize = rec->datalen;
1043 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1044 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1048 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1049 recdata.dptr += sizeof(*rechdr);
1050 recdata.dsize -= sizeof(*rechdr);
1052 dd = (struct delete_record_data *)trbt_lookup32(
1054 ctdb_hash(&reckey));
1057 * The other node could not delete the
1058 * record and it is the first node that
1059 * failed. So we should remove it from
1060 * the tree and update statistics.
1063 vdata->delete_remote_error++;
1064 vdata->delete_left--;
1067 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1071 if (vdata->delete_left == 0) {
1077 * Delete the remaining records locally.
1079 * These records have successfully been deleted on all
1080 * active remote nodes.
1083 trbt_traversearray32(vdata->delete_list, 1,
1084 delete_record_traverse, vdata);
1088 if (vdata->delete_left != 0) {
1089 DEBUG(DEBUG_ERR, (__location__ " Vaccum db[%s] error: "
1090 "there are %u records left for deletion after "
1091 "processing delete list\n",
1093 (unsigned)vdata->delete_left));
1096 sum = vdata->delete_deleted
1097 + vdata->delete_skipped
1098 + vdata->delete_remote_error
1099 + vdata->delete_local_error
1100 + vdata->delete_left;
1102 if (vdata->delete_count != sum) {
1103 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1104 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1105 ctdb_db->db_name, (unsigned)vdata->delete_count,
1109 if (vdata->delete_count > 0) {
1112 " vacuum delete list statistics: "
1121 (unsigned)vdata->delete_count,
1122 (unsigned)vdata->delete_deleted,
1123 (unsigned)vdata->delete_skipped,
1124 (unsigned)vdata->delete_remote_error,
1125 (unsigned)vdata->delete_local_error,
1126 (unsigned)vdata->delete_left));
1132 talloc_free(tmp_ctx);
1138 * initialize the vacuum_data
1140 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context *ctdb_db,
1141 struct vacuum_data *vdata)
1144 struct ctdb_context *ctdb = ctdb_db->ctdb;
1146 vdata->fast_added_to_delete_list = 0;
1147 vdata->fast_added_to_vacuum_fetch_list = 0;
1148 vdata->fast_deleted = 0;
1149 vdata->fast_skipped = 0;
1150 vdata->fast_error = 0;
1151 vdata->fast_total = 0;
1152 vdata->full_scheduled = 0;
1153 vdata->full_skipped = 0;
1154 vdata->full_error = 0;
1155 vdata->full_total = 0;
1156 vdata->delete_count = 0;
1157 vdata->delete_left = 0;
1158 vdata->delete_remote_error = 0;
1159 vdata->delete_local_error = 0;
1160 vdata->delete_skipped = 0;
1161 vdata->delete_deleted = 0;
1163 /* the list needs to be of length num_nodes */
1164 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1165 struct ctdb_marshall_buffer *,
1167 if (vdata->vacuum_fetch_list == NULL) {
1168 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1171 for (i = 0; i < ctdb->num_nodes; i++) {
1172 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1173 talloc_zero_size(vdata->vacuum_fetch_list,
1174 offsetof(struct ctdb_marshall_buffer, data));
1175 if (vdata->vacuum_fetch_list[i] == NULL) {
1176 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1179 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1187 * - Always do the fast vacuuming run, which traverses
1188 * the in-memory delete queue: these records have been
1189 * scheduled for deletion.
1190 * - Only if explicitly requested, the database is traversed
1191 * in order to use the traditional heuristics on empty records
1192 * to trigger deletion.
1193 * This is done only every VacuumFastPathCount'th vacuuming run.
1195 * The traverse runs fill two lists:
1197 * - The delete_list:
1198 * This is the list of empty records the current
1199 * node is lmaster and dmaster for. These records are later
1200 * deleted first on other nodes and then locally.
1202 * The fast vacuuming run has a short cut for those records
1203 * that have never been migrated with data: these records
1204 * are immediately deleted locally, since they have left
1205 * no trace on other nodes.
1207 * - The vacuum_fetch lists
1208 * (one for each other lmaster node):
1209 * The records in this list are sent for deletion to
1210 * their lmaster in a bulk VACUUM_FETCH message.
1212 * The lmaster then migrates all these records to itelf
1213 * so that they can be vacuumed there.
1215 * This executes in the child context.
1217 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1218 struct vacuum_data *vdata,
1219 bool full_vacuum_run)
1221 struct ctdb_context *ctdb = ctdb_db->ctdb;
1224 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1225 "%s db_id[0x%08x]\n",
1226 full_vacuum_run ? "full" : "fast",
1227 ctdb_db->db_name, ctdb_db->db_id));
1229 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1231 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1235 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1237 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1243 ret = ctdb_vacuum_init_vacuum_data(ctdb_db, vdata);
1248 if (full_vacuum_run) {
1249 ret = ctdb_vacuum_traverse_db(ctdb_db, vdata);
1255 ctdb_process_delete_queue(ctdb_db, vdata);
1257 ret = ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1262 ret = ctdb_process_delete_list(ctdb_db, vdata);
1267 /* this ensures we run our event queue */
1268 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1275 * traverse function for repacking
1277 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
1280 struct vacuum_data *vdata = (struct vacuum_data *)private_data;
1282 if (vdata->vacuum) {
1283 uint32_t hash = ctdb_hash(&key);
1284 struct delete_record_data *kd;
1286 * check if we can ignore this record because it's in the delete_list
1288 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
1290 * there might be hash collisions so we have to compare the keys here to be sure
1292 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
1293 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
1295 * we have to check if the record hasn't changed in the meantime in order to
1296 * savely remove it from the database
1298 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
1299 hdr->dmaster == kd->ctdb->pnn &&
1300 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
1301 kd->hdr.rsn == hdr->rsn) {
1307 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
1308 vdata->traverse_error = true;
1318 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
1320 struct tdb_context *tmp_db;
1322 if (tdb_transaction_start(tdb) != 0) {
1323 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
1327 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
1328 TDB_INTERNAL|TDB_DISALLOW_NESTING,
1330 if (tmp_db == NULL) {
1331 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
1332 tdb_transaction_cancel(tdb);
1336 vdata->traverse_error = false;
1337 vdata->dest_db = tmp_db;
1338 vdata->vacuum = true;
1339 vdata->vacuumed = 0;
1343 * repack and vacuum on-the-fly by not writing the records that are
1346 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
1347 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
1348 tdb_transaction_cancel(tdb);
1353 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
1355 if (vdata->traverse_error) {
1356 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
1357 tdb_transaction_cancel(tdb);
1362 if (tdb_wipe_all(tdb) != 0) {
1363 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
1364 tdb_transaction_cancel(tdb);
1369 vdata->traverse_error = false;
1370 vdata->dest_db = tdb;
1371 vdata->vacuum = false;
1374 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
1375 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
1376 tdb_transaction_cancel(tdb);
1381 if (vdata->traverse_error) {
1382 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
1383 tdb_transaction_cancel(tdb);
1391 if (tdb_transaction_commit(tdb) != 0) {
1392 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1395 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1401 * repack and vaccum a db
1402 * called from the child context
1404 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1405 TALLOC_CTX *mem_ctx,
1406 bool full_vacuum_run)
1408 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1409 const char *name = ctdb_db->db_name;
1410 int freelist_size = 0;
1411 struct vacuum_data *vdata;
1413 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1414 if (vdata == NULL) {
1415 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1419 vdata->ctdb = ctdb_db->ctdb;
1420 vdata->repack_limit = repack_limit;
1421 vdata->delete_list = trbt_create(vdata, 0);
1422 vdata->ctdb_db = ctdb_db;
1423 if (vdata->delete_list == NULL) {
1424 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1429 vdata->start = timeval_current();
1432 * gather all records that can be deleted in vdata
1434 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1435 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1438 if (repack_limit != 0) {
1439 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1440 if (freelist_size == -1) {
1441 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1448 * decide if a repack is necessary
1450 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1456 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
1457 name, freelist_size, vdata->delete_left));
1460 * repack and implicitely get rid of the records we can delete
1462 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1463 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1472 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1474 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1479 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1481 double l = timeval_elapsed(&child_ctx->start_time);
1482 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1483 struct ctdb_context *ctdb = ctdb_db->ctdb;
1485 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1487 if (child_ctx->child_pid != -1) {
1488 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1490 /* Bump the number of successful fast-path runs. */
1491 child_ctx->vacuum_handle->fast_path_count++;
1494 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1496 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1497 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1498 ctdb_vacuum_event, child_ctx->vacuum_handle);
1504 * this event is generated when a vacuum child process times out
1506 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1507 struct timeval t, void *private_data)
1509 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1511 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1513 child_ctx->status = VACUUM_TIMEOUT;
1515 talloc_free(child_ctx);
1520 * this event is generated when a vacuum child process has completed
1522 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1523 uint16_t flags, void *private_data)
1525 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1529 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1530 child_ctx->child_pid = -1;
1532 ret = read(child_ctx->fd[0], &c, 1);
1533 if (ret != 1 || c != 0) {
1534 child_ctx->status = VACUUM_ERROR;
1535 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));
1537 child_ctx->status = VACUUM_OK;
1540 talloc_free(child_ctx);
1544 * this event is called every time we need to start a new vacuum process
1547 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1548 struct timeval t, void *private_data)
1550 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1551 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1552 struct ctdb_context *ctdb = ctdb_db->ctdb;
1553 struct ctdb_vacuum_child_context *child_ctx;
1554 struct tevent_fd *fde;
1557 /* we dont vacuum if we are in recovery mode, or db frozen */
1558 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1559 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1560 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1561 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1562 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1565 event_add_timed(ctdb->ev, vacuum_handle,
1566 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1567 ctdb_vacuum_event, vacuum_handle);
1571 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1572 if (child_ctx == NULL) {
1573 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1574 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1578 ret = pipe(child_ctx->fd);
1580 talloc_free(child_ctx);
1581 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1582 event_add_timed(ctdb->ev, vacuum_handle,
1583 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1584 ctdb_vacuum_event, vacuum_handle);
1588 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1589 vacuum_handle->fast_path_count = 0;
1592 child_ctx->child_pid = ctdb_fork(ctdb);
1593 if (child_ctx->child_pid == (pid_t)-1) {
1594 close(child_ctx->fd[0]);
1595 close(child_ctx->fd[1]);
1596 talloc_free(child_ctx);
1597 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1598 event_add_timed(ctdb->ev, vacuum_handle,
1599 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1600 ctdb_vacuum_event, vacuum_handle);
1605 if (child_ctx->child_pid == 0) {
1607 bool full_vacuum_run = false;
1608 close(child_ctx->fd[0]);
1610 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1611 ctdb_set_process_name("ctdb_vacuum");
1612 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1613 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1620 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1621 (vacuum_handle->fast_path_count == 0))
1623 full_vacuum_run = true;
1625 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1628 write(child_ctx->fd[1], &cc, 1);
1632 set_close_on_exec(child_ctx->fd[0]);
1633 close(child_ctx->fd[1]);
1635 child_ctx->status = VACUUM_RUNNING;
1636 child_ctx->start_time = timeval_current();
1638 DLIST_ADD(ctdb->vacuumers, child_ctx);
1639 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1642 * Clear the fastpath vacuuming list in the parent.
1644 talloc_free(ctdb_db->delete_queue);
1645 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1646 if (ctdb_db->delete_queue == NULL) {
1647 /* fatal here? ... */
1648 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1649 "in parent context. Shutting down\n");
1652 event_add_timed(ctdb->ev, child_ctx,
1653 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1654 vacuum_child_timeout, child_ctx);
1656 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1658 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1659 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1660 tevent_fd_set_auto_close(fde);
1662 vacuum_handle->child_ctx = child_ctx;
1663 child_ctx->vacuum_handle = vacuum_handle;
1666 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1668 /* Simply free them all. */
1669 while (ctdb->vacuumers) {
1670 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1671 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1672 (int)ctdb->vacuumers->child_pid));
1673 /* vacuum_child_destructor kills it, removes from list */
1674 talloc_free(ctdb->vacuumers);
1678 /* this function initializes the vacuuming context for a database
1679 * starts the vacuuming events
1681 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1683 if (ctdb_db->persistent != 0) {
1684 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1688 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1689 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1691 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1692 ctdb_db->vacuum_handle->fast_path_count = 0;
1694 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1695 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1696 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1701 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1702 const struct ctdb_ltdb_header *hdr,
1705 struct delete_record_data *kd;
1708 hash = (uint32_t)ctdb_hash(&key);
1710 DEBUG(DEBUG_DEBUG, (__location__
1711 " remove_record_from_delete_queue: "
1716 "migrated_with_data[%s]\n",
1717 ctdb_db->db_name, ctdb_db->db_id,
1719 ctdb_lmaster(ctdb_db->ctdb, &key),
1720 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1722 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1724 DEBUG(DEBUG_DEBUG, (__location__
1725 " remove_record_from_delete_queue: "
1726 "record not in queue (hash[0x%08x])\n.",
1731 if ((kd->key.dsize != key.dsize) ||
1732 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1734 DEBUG(DEBUG_DEBUG, (__location__
1735 " remove_record_from_delete_queue: "
1736 "hash collision for key with hash[0x%08x] "
1737 "in db[%s] - skipping\n",
1738 hash, ctdb_db->db_name));
1742 DEBUG(DEBUG_DEBUG, (__location__
1743 " remove_record_from_delete_queue: "
1744 "removing key with hash[0x%08x]\n",
1753 * Insert a record into the ctdb_db context's delete queue,
1754 * handling hash collisions.
1756 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1757 const struct ctdb_ltdb_header *hdr,
1760 struct delete_record_data *kd;
1764 hash = (uint32_t)ctdb_hash(&key);
1766 DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1770 "migrated_with_data[%s]\n",
1771 ctdb_db->db_name, ctdb_db->db_id,
1773 ctdb_lmaster(ctdb_db->ctdb, &key),
1774 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1776 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1778 if ((kd->key.dsize != key.dsize) ||
1779 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1782 (__location__ " schedule for deletion: "
1783 "hash collision for key hash [0x%08x]. "
1784 "Skipping the record.\n", hash));
1788 (__location__ " schedule for deletion: "
1789 "updating entry for key with hash [0x%08x].\n",
1794 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1795 ctdb_db->delete_queue,
1799 (__location__ " schedule for deletion: error "
1800 "inserting key with hash [0x%08x] into delete queue\n",
1809 * Schedule a record for deletetion.
1810 * Called from the parent context.
1812 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1815 struct ctdb_control_schedule_for_deletion *dd;
1816 struct ctdb_db_context *ctdb_db;
1820 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1822 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1823 if (ctdb_db == NULL) {
1824 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1829 key.dsize = dd->keylen;
1832 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1837 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1838 const struct ctdb_ltdb_header *hdr,
1842 struct ctdb_control_schedule_for_deletion *dd;
1846 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1847 /* main daemon - directly queue */
1848 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1853 /* if we dont have a connection to the daemon we can not send
1854 a control. For example sometimes from update_record control child
1857 if (!ctdb_db->ctdb->can_send_controls) {
1862 /* child process: send the main daemon a control */
1863 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1864 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1865 if (indata.dptr == NULL) {
1866 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1869 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1870 dd->db_id = ctdb_db->db_id;
1872 dd->keylen = key.dsize;
1873 memcpy(dd->key, key.dptr, key.dsize);
1875 ret = ctdb_control(ctdb_db->ctdb,
1878 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1879 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1884 NULL, /* timeout : NULL == wait forever */
1885 NULL); /* error message */
1887 talloc_free(indata.dptr);
1889 if (ret != 0 || status != 0) {
1890 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1891 "SCHEDULE_FOR_DELETION "
1901 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1902 const struct ctdb_ltdb_header *hdr,
1905 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1907 * Only remove the record from the delete queue if called
1908 * in the main daemon.
1913 remove_record_from_delete_queue(ctdb_db, hdr, key);