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/>.
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "system/time.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "lib/util/dlinklist.h"
32 #include "lib/util/debug.h"
33 #include "lib/util/samba_util.h"
34 #include "lib/util/sys_rw.h"
35 #include "lib/util/util_process.h"
37 #include "ctdb_private.h"
38 #include "ctdb_client.h"
40 #include "protocol/protocol_private.h"
42 #include "common/rb_tree.h"
43 #include "common/common.h"
44 #include "common/logging.h"
46 #include "protocol/protocol_api.h"
48 #define TIMELIMIT() timeval_current_ofs(10, 0)
50 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
52 struct ctdb_vacuum_child_context {
53 struct ctdb_vacuum_handle *vacuum_handle;
54 /* fd child writes status to */
57 enum vacuum_child_status status;
58 struct timeval start_time;
62 struct ctdb_vacuum_handle {
63 struct ctdb_db_context *ctdb_db;
64 uint32_t fast_path_count;
65 uint32_t vacuum_interval;
69 /* a list of records to possibly delete */
71 struct ctdb_context *ctdb;
72 struct ctdb_db_context *ctdb_db;
73 struct tdb_context *dest_db;
74 trbt_tree_t *delete_list;
75 struct ctdb_marshall_buffer **vacuum_fetch_list;
81 uint32_t added_to_vacuum_fetch_list;
82 uint32_t added_to_delete_list;
96 uint32_t remote_error;
109 /* this structure contains the information for one record to be deleted */
110 struct delete_record_data {
111 struct ctdb_context *ctdb;
112 struct ctdb_db_context *ctdb_db;
113 struct ctdb_ltdb_header hdr;
114 uint32_t remote_fail_count;
119 struct delete_records_list {
120 struct ctdb_marshall_buffer *records;
121 struct vacuum_data *vdata;
124 struct fetch_record_data {
129 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
130 const struct ctdb_ltdb_header *hdr,
134 * Store key and header in a tree, indexed by the key hash.
136 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
137 struct ctdb_db_context *ctdb_db,
139 const struct ctdb_ltdb_header *hdr,
142 struct delete_record_data *dd;
146 len = offsetof(struct delete_record_data, keydata) + key.dsize;
148 dd = (struct delete_record_data *)talloc_size(tree, len);
150 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
153 talloc_set_name_const(dd, "struct delete_record_data");
156 dd->ctdb_db = ctdb_db;
157 dd->key.dsize = key.dsize;
158 dd->key.dptr = dd->keydata;
159 memcpy(dd->keydata, key.dptr, key.dsize);
162 dd->remote_fail_count = 0;
164 hash = ctdb_hash(&key);
166 trbt_insert32(tree, hash, dd);
171 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
172 struct ctdb_ltdb_header *hdr)
174 struct ctdb_context *ctdb = vdata->ctdb;
175 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
179 hash = ctdb_hash(&key);
181 if (trbt_lookup32(vdata->delete_list, hash)) {
182 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
186 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
193 vdata->count.delete_list.total++;
199 * Add a record to the list of records to be sent
200 * to their lmaster with VACUUM_FETCH.
202 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
205 struct ctdb_context *ctdb = vdata->ctdb;
207 struct ctdb_marshall_buffer *vfl;
209 lmaster = ctdb_lmaster(ctdb, &key);
211 vfl = vdata->vacuum_fetch_list[lmaster];
213 vfl = ctdb_marshall_add(ctdb, vfl, vfl->db_id, ctdb->pnn,
214 key, NULL, tdb_null);
216 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
217 vdata->traverse_error = true;
221 vdata->vacuum_fetch_list[lmaster] = vfl;
227 static void ctdb_vacuum_event(struct tevent_context *ev,
228 struct tevent_timer *te,
229 struct timeval t, void *private_data);
231 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
233 struct ctdb_ltdb_header *header =
234 (struct ctdb_ltdb_header *)private_data;
236 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
240 *header = *(struct ctdb_ltdb_header *)data.dptr;
246 * traverse function for gathering the records that can be deleted
248 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
251 struct vacuum_data *vdata = talloc_get_type(private_data,
253 struct ctdb_context *ctdb = vdata->ctdb;
254 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
256 struct ctdb_ltdb_header *hdr;
259 vdata->count.db_traverse.total++;
261 lmaster = ctdb_lmaster(ctdb, &key);
262 if (lmaster >= ctdb->num_nodes) {
263 vdata->count.db_traverse.error++;
264 DEBUG(DEBUG_CRIT, (__location__
265 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
268 (unsigned)ctdb->num_nodes,
269 (unsigned)ctdb_hash(&key)));
273 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
274 /* it is not a deleted record */
275 vdata->count.db_traverse.skipped++;
279 hdr = (struct ctdb_ltdb_header *)data.dptr;
281 if (hdr->dmaster != ctdb->pnn) {
282 vdata->count.db_traverse.skipped++;
287 * Add the record to this process's delete_queue for processing
288 * in the subsequent traverse in the fast vacuum run.
290 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
292 vdata->count.db_traverse.error++;
294 vdata->count.db_traverse.scheduled++;
301 * traverse the tree of records to delete and marshall them into
304 static int delete_marshall_traverse(void *param, void *data)
306 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
307 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
308 struct ctdb_marshall_buffer *m;
310 m = ctdb_marshall_add(recs, recs->records, recs->records->db_id,
311 recs->records->db_id,
312 dd->key, &dd->hdr, tdb_null);
314 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
322 struct fetch_queue_state {
323 struct ctdb_db_context *ctdb_db;
327 struct fetch_record_migrate_state {
328 struct fetch_queue_state *fetch_queue;
332 static void fetch_record_migrate_callback(struct ctdb_client_call_state *state)
334 struct fetch_record_migrate_state *fetch = talloc_get_type_abort(
335 state->async.private_data, struct fetch_record_migrate_state);
336 struct fetch_queue_state *fetch_queue = fetch->fetch_queue;
337 struct ctdb_ltdb_header hdr;
338 struct ctdb_call call = { 0 };
341 ret = ctdb_call_recv(state, &call);
342 fetch_queue->count--;
344 D_ERR("Failed to migrate record for vacuuming\n");
348 ret = tdb_chainlock_nonblock(fetch_queue->ctdb_db->ltdb->tdb,
354 ret = tdb_parse_record(fetch_queue->ctdb_db->ltdb->tdb,
356 vacuum_record_parser,
359 tdb_chainunlock(fetch_queue->ctdb_db->ltdb->tdb, fetch->key);
365 D_INFO("Vacuum Fetch record, key=%.*s\n",
366 (int)fetch->key.dsize,
369 (void) ctdb_local_schedule_for_deletion(fetch_queue->ctdb_db,
377 static int fetch_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
379 struct ctdb_ltdb_header *header =
380 (struct ctdb_ltdb_header *)private_data;
382 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
386 memcpy(header, data.dptr, sizeof(*header));
391 * traverse function for the traversal of the fetch_queue.
393 * Send a record migration request.
395 static int fetch_queue_traverse(void *param, void *data)
397 struct fetch_record_data *rd = talloc_get_type_abort(
398 data, struct fetch_record_data);
399 struct fetch_queue_state *fetch_queue =
400 (struct fetch_queue_state *)param;
401 struct ctdb_db_context *ctdb_db = fetch_queue->ctdb_db;
402 struct ctdb_client_call_state *state;
403 struct fetch_record_migrate_state *fetch;
404 struct ctdb_call call = { 0 };
405 struct ctdb_ltdb_header header;
408 ret = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, rd->key);
413 ret = tdb_parse_record(ctdb_db->ltdb->tdb,
418 tdb_chainunlock(ctdb_db->ltdb->tdb, rd->key);
424 if (header.dmaster == ctdb_db->ctdb->pnn) {
425 /* If the record is already migrated, skip */
429 fetch = talloc_zero(ctdb_db, struct fetch_record_migrate_state);
431 D_ERR("Failed to setup fetch record migrate state\n");
435 fetch->fetch_queue = fetch_queue;
437 fetch->key.dsize = rd->key.dsize;
438 fetch->key.dptr = talloc_memdup(fetch, rd->key.dptr, rd->key.dsize);
439 if (fetch->key.dptr == NULL) {
440 D_ERR("Memory error in fetch_queue_traverse\n");
445 call.call_id = CTDB_NULL_FUNC;
446 call.flags = CTDB_IMMEDIATE_MIGRATION |
447 CTDB_CALL_FLAG_VACUUM_MIGRATION;
448 call.key = fetch->key;
450 state = ctdb_call_send(ctdb_db, &call);
452 DEBUG(DEBUG_ERR, ("Failed to setup vacuum fetch call\n"));
457 state->async.fn = fetch_record_migrate_callback;
458 state->async.private_data = fetch;
460 fetch_queue->count++;
465 D_INFO("Skipped Fetch record, key=%.*s\n",
472 * Traverse the fetch.
473 * Records are migrated to the local node and
474 * added to delete queue for further processing.
476 static void ctdb_process_fetch_queue(struct ctdb_db_context *ctdb_db)
478 struct fetch_queue_state state;
481 state.ctdb_db = ctdb_db;
484 ret = trbt_traversearray32(ctdb_db->fetch_queue, 1,
485 fetch_queue_traverse, &state);
487 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
488 "the fetch queue.\n"));
491 /* Wait for all migrations to complete */
492 while (state.count > 0) {
493 tevent_loop_once(ctdb_db->ctdb->ev);
498 * traverse function for the traversal of the delete_queue,
499 * the fast-path vacuuming list.
501 * - If the record has been migrated off the node
502 * or has been revived (filled with data) on the node,
503 * then skip the record.
505 * - If the current node is the record's lmaster and it is
506 * a record that has never been migrated with data, then
507 * delete the record from the local tdb.
509 * - If the current node is the record's lmaster and it has
510 * been migrated with data, then schedule it for the normal
511 * vacuuming procedure (i.e. add it to the delete_list).
513 * - If the current node is NOT the record's lmaster then
514 * add it to the list of records that are to be sent to
515 * the lmaster with the VACUUM_FETCH message.
517 static int delete_queue_traverse(void *param, void *data)
519 struct delete_record_data *dd =
520 talloc_get_type(data, struct delete_record_data);
521 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
522 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
523 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
525 struct ctdb_ltdb_header header;
527 uint32_t hash = ctdb_hash(&(dd->key));
529 vdata->count.delete_queue.total++;
531 res = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, dd->key);
533 vdata->count.delete_queue.error++;
537 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
538 vacuum_record_parser, &header);
543 if (header.dmaster != ctdb->pnn) {
544 /* The record has been migrated off the node. Skip. */
548 if (header.rsn != dd->hdr.rsn) {
550 * The record has been migrated off the node and back again.
551 * But not requeued for deletion. Skip it.
557 * We are dmaster, and the record has no data, and it has
558 * not been migrated after it has been queued for deletion.
560 * At this stage, the record could still have been revived locally
561 * and last been written with empty data. This can only be
562 * fixed with the addition of an active or delete flag. (TODO)
565 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
567 if (lmaster != ctdb->pnn) {
568 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
572 (__location__ " Error adding record to list "
573 "of records to send to lmaster.\n"));
574 vdata->count.delete_queue.error++;
576 vdata->count.delete_queue.added_to_vacuum_fetch_list++;
581 /* use header->flags or dd->hdr.flags ?? */
582 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
583 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
587 (__location__ " Error adding record to list "
588 "of records for deletion on lmaster.\n"));
589 vdata->count.delete_queue.error++;
591 vdata->count.delete_queue.added_to_delete_list++;
594 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
598 (__location__ " Error deleting record with key "
599 "hash [0x%08x] from local data base db[%s].\n",
600 hash, ctdb_db->db_name));
601 vdata->count.delete_queue.error++;
606 (__location__ " Deleted record with key hash "
607 "[0x%08x] from local data base db[%s].\n",
608 hash, ctdb_db->db_name));
609 vdata->count.delete_queue.deleted++;
615 vdata->count.delete_queue.skipped++;
618 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
624 * Delete the records that we are lmaster and dmaster for and
625 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
628 static int delete_record_traverse(void *param, void *data)
630 struct delete_record_data *dd =
631 talloc_get_type(data, struct delete_record_data);
632 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
633 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
634 struct ctdb_context *ctdb = ctdb_db->ctdb;
636 struct ctdb_ltdb_header header;
638 uint32_t hash = ctdb_hash(&(dd->key));
640 if (dd->remote_fail_count > 0) {
641 vdata->count.delete_list.remote_error++;
642 vdata->count.delete_list.left--;
647 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
650 (__location__ " Error getting chainlock on record with "
651 "key hash [0x%08x] on database db[%s].\n",
652 hash, ctdb_db->db_name));
653 vdata->count.delete_list.local_error++;
654 vdata->count.delete_list.left--;
660 * Verify that the record is still empty, its RSN has not
661 * changed and that we are still its lmaster and dmaster.
664 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
665 vacuum_record_parser, &header);
670 if (header.flags & CTDB_REC_RO_FLAGS) {
671 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
672 "on database db[%s] has read-only flags. "
674 hash, ctdb_db->db_name));
678 if (header.dmaster != ctdb->pnn) {
679 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
680 "on database db[%s] has been migrated away. "
682 hash, ctdb_db->db_name));
686 if (header.rsn != dd->hdr.rsn) {
688 * The record has been migrated off the node and back again.
689 * But not requeued for deletion. Skip it.
691 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
692 "on database db[%s] seems to have been "
693 "migrated away and back again (with empty "
694 "data). skipping.\n",
695 hash, ctdb_db->db_name));
699 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
701 if (lmaster != ctdb->pnn) {
702 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
703 "delete list (key hash [0x%08x], db[%s]). "
704 "Strange! skipping.\n",
705 hash, ctdb_db->db_name));
709 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
713 (__location__ " Error deleting record with key hash "
714 "[0x%08x] from local data base db[%s].\n",
715 hash, ctdb_db->db_name));
716 vdata->count.delete_list.local_error++;
721 (__location__ " Deleted record with key hash [0x%08x] from "
722 "local data base db[%s].\n", hash, ctdb_db->db_name));
724 vdata->count.delete_list.deleted++;
728 vdata->count.delete_list.skipped++;
731 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
734 vdata->count.delete_list.left--;
740 * Traverse the delete_queue.
741 * Records are either deleted directly or filled
742 * into the delete list or the vacuum fetch lists
743 * for further processing.
745 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
746 struct vacuum_data *vdata)
751 ret = trbt_traversearray32(ctdb_db->delete_queue, 1,
752 delete_queue_traverse, vdata);
755 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
756 "the delete queue.\n"));
759 sum = vdata->count.delete_queue.deleted
760 + vdata->count.delete_queue.skipped
761 + vdata->count.delete_queue.error
762 + vdata->count.delete_queue.added_to_delete_list
763 + vdata->count.delete_queue.added_to_vacuum_fetch_list;
765 if (vdata->count.delete_queue.total != sum) {
766 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
767 "counts for db[%s]: total[%u] != sum[%u]\n",
769 (unsigned)vdata->count.delete_queue.total,
773 if (vdata->count.delete_queue.total > 0) {
776 " fast vacuuming delete_queue traverse statistics: "
785 (unsigned)vdata->count.delete_queue.total,
786 (unsigned)vdata->count.delete_queue.deleted,
787 (unsigned)vdata->count.delete_queue.skipped,
788 (unsigned)vdata->count.delete_queue.error,
789 (unsigned)vdata->count.delete_queue.added_to_delete_list,
790 (unsigned)vdata->count.delete_queue.added_to_vacuum_fetch_list));
797 * read-only traverse of the database, looking for records that
798 * might be able to be vacuumed.
800 * This is not done each time but only every tunable
801 * VacuumFastPathCount times.
803 static void ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
804 struct vacuum_data *vdata)
808 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
809 if (ret == -1 || vdata->traverse_error) {
810 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
811 "'%s'\n", ctdb_db->db_name));
815 if (vdata->count.db_traverse.total > 0) {
818 " full vacuuming db traverse statistics: "
825 (unsigned)vdata->count.db_traverse.total,
826 (unsigned)vdata->count.db_traverse.skipped,
827 (unsigned)vdata->count.db_traverse.error,
828 (unsigned)vdata->count.db_traverse.scheduled));
835 * Process the vacuum fetch lists:
836 * For records for which we are not the lmaster, tell the lmaster to
839 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
840 struct vacuum_data *vdata)
843 struct ctdb_context *ctdb = ctdb_db->ctdb;
846 for (i = 0; i < ctdb->num_nodes; i++) {
848 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
850 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
854 if (vfl->count == 0) {
858 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
859 vfl->count, ctdb->nodes[i]->pnn,
862 data = ctdb_marshall_finish(vfl);
864 ret = ctdb_control(ctdb, ctdb->nodes[i]->pnn, 0,
865 CTDB_CONTROL_VACUUM_FETCH, 0,
866 data, NULL, NULL, &res, NULL, NULL);
867 if (ret != 0 || res != 0) {
868 DEBUG(DEBUG_ERR, ("Failed to send vacuum "
869 "fetch control to node %u\n",
870 ctdb->nodes[i]->pnn));
876 * Process the delete list:
878 * This is the last step of vacuuming that consistently deletes
879 * those records that have been migrated with data and can hence
880 * not be deleted when leaving a node.
882 * In this step, the lmaster does the final deletion of those empty
883 * records that it is also dmaster for. It has usually received
884 * at least some of these records previously from the former dmasters
885 * with the vacuum fetch message.
887 * 1) Send the records to all active nodes with the TRY_DELETE_RECORDS
888 * control. The remote notes delete their local copy.
889 * 2) The lmaster locally deletes its copies of all records that
890 * could successfully be deleted remotely in step #2.
892 static void ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
893 struct vacuum_data *vdata)
896 struct ctdb_context *ctdb = ctdb_db->ctdb;
897 struct delete_records_list *recs;
899 struct ctdb_node_map_old *nodemap;
900 uint32_t *active_nodes;
901 int num_active_nodes;
905 if (vdata->count.delete_list.total == 0) {
909 tmp_ctx = talloc_new(vdata);
910 if (tmp_ctx == NULL) {
911 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
915 vdata->count.delete_list.left = vdata->count.delete_list.total;
918 * get the list of currently active nodes
921 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
926 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
930 active_nodes = list_of_active_nodes(ctdb, nodemap,
931 nodemap, /* talloc context */
932 false /* include self */);
934 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
937 * Now delete the records all active nodes in a two-phase process:
938 * 1) tell all active remote nodes to delete all their copy
939 * 2) if all remote nodes deleted their record copy, delete it locally
942 recs = talloc_zero(tmp_ctx, struct delete_records_list);
944 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
950 * Send all records to all active nodes for deletion.
954 * Create a marshall blob from the remaining list of records to delete.
957 recs->records = (struct ctdb_marshall_buffer *)
958 talloc_zero_size(recs,
959 offsetof(struct ctdb_marshall_buffer, data));
960 if (recs->records == NULL) {
961 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
964 recs->records->db_id = ctdb_db->db_id;
966 ret = trbt_traversearray32(vdata->delete_list, 1,
967 delete_marshall_traverse, recs);
969 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
970 "delete list for second marshalling.\n"));
974 indata = ctdb_marshall_finish(recs->records);
976 for (i = 0; i < num_active_nodes; i++) {
977 struct ctdb_marshall_buffer *records;
978 struct ctdb_rec_data_old *rec;
982 ret = ctdb_control(ctdb, active_nodes[i], 0,
983 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
984 indata, recs, &outdata, &res,
986 if (ret != 0 || res != 0) {
987 DEBUG(DEBUG_ERR, ("Failed to delete records on "
988 "node %u: ret[%d] res[%d]\n",
989 active_nodes[i], ret, res));
994 * outdata contains the list of records coming back
995 * from the node: These are the records that the
996 * remote node could not delete. We remove these from
997 * the list to delete locally.
999 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1000 rec = (struct ctdb_rec_data_old *)&records->data[0];
1001 while (records->count-- > 0) {
1002 TDB_DATA reckey, recdata;
1003 struct ctdb_ltdb_header *rechdr;
1004 struct delete_record_data *dd;
1006 reckey.dptr = &rec->data[0];
1007 reckey.dsize = rec->keylen;
1008 recdata.dptr = &rec->data[reckey.dsize];
1009 recdata.dsize = rec->datalen;
1011 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1012 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1015 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1016 recdata.dptr += sizeof(*rechdr);
1017 recdata.dsize -= sizeof(*rechdr);
1019 dd = (struct delete_record_data *)trbt_lookup32(
1021 ctdb_hash(&reckey));
1024 * The remote node could not delete the
1025 * record. Since other remote nodes can
1026 * also fail, we just mark the record.
1028 dd->remote_fail_count++;
1030 DEBUG(DEBUG_ERR, (__location__ " Failed to "
1031 "find record with hash 0x%08x coming "
1032 "back from TRY_DELETE_RECORDS "
1033 "control in delete list.\n",
1034 ctdb_hash(&reckey)));
1037 rec = (struct ctdb_rec_data_old *)(rec->length + (uint8_t *)rec);
1043 * Delete the remaining records locally.
1045 * These records have successfully been deleted on all
1046 * active remote nodes.
1049 ret = trbt_traversearray32(vdata->delete_list, 1,
1050 delete_record_traverse, vdata);
1052 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1053 "delete list for deletion.\n"));
1056 if (vdata->count.delete_list.left != 0) {
1057 DEBUG(DEBUG_ERR, (__location__ " Vacuum db[%s] error: "
1058 "there are %u records left for deletion after "
1059 "processing delete list\n",
1061 (unsigned)vdata->count.delete_list.left));
1064 sum = vdata->count.delete_list.deleted
1065 + vdata->count.delete_list.skipped
1066 + vdata->count.delete_list.remote_error
1067 + vdata->count.delete_list.local_error
1068 + vdata->count.delete_list.left;
1070 if (vdata->count.delete_list.total != sum) {
1071 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1072 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1074 (unsigned)vdata->count.delete_list.total,
1078 if (vdata->count.delete_list.total > 0) {
1081 " vacuum delete list statistics: "
1090 (unsigned)vdata->count.delete_list.total,
1091 (unsigned)vdata->count.delete_list.deleted,
1092 (unsigned)vdata->count.delete_list.skipped,
1093 (unsigned)vdata->count.delete_list.remote_error,
1094 (unsigned)vdata->count.delete_list.local_error,
1095 (unsigned)vdata->count.delete_list.left));
1099 talloc_free(tmp_ctx);
1105 * initialize the vacuum_data
1107 static struct vacuum_data *ctdb_vacuum_init_vacuum_data(
1108 struct ctdb_db_context *ctdb_db,
1109 TALLOC_CTX *mem_ctx)
1112 struct ctdb_context *ctdb = ctdb_db->ctdb;
1113 struct vacuum_data *vdata;
1115 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1116 if (vdata == NULL) {
1117 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1121 vdata->ctdb = ctdb_db->ctdb;
1122 vdata->ctdb_db = ctdb_db;
1123 vdata->delete_list = trbt_create(vdata, 0);
1124 if (vdata->delete_list == NULL) {
1125 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1129 vdata->start = timeval_current();
1131 vdata->count.delete_queue.added_to_delete_list = 0;
1132 vdata->count.delete_queue.added_to_vacuum_fetch_list = 0;
1133 vdata->count.delete_queue.deleted = 0;
1134 vdata->count.delete_queue.skipped = 0;
1135 vdata->count.delete_queue.error = 0;
1136 vdata->count.delete_queue.total = 0;
1137 vdata->count.db_traverse.scheduled = 0;
1138 vdata->count.db_traverse.skipped = 0;
1139 vdata->count.db_traverse.error = 0;
1140 vdata->count.db_traverse.total = 0;
1141 vdata->count.delete_list.total = 0;
1142 vdata->count.delete_list.left = 0;
1143 vdata->count.delete_list.remote_error = 0;
1144 vdata->count.delete_list.local_error = 0;
1145 vdata->count.delete_list.skipped = 0;
1146 vdata->count.delete_list.deleted = 0;
1148 /* the list needs to be of length num_nodes */
1149 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1150 struct ctdb_marshall_buffer *,
1152 if (vdata->vacuum_fetch_list == NULL) {
1153 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1156 for (i = 0; i < ctdb->num_nodes; i++) {
1157 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1158 talloc_zero_size(vdata->vacuum_fetch_list,
1159 offsetof(struct ctdb_marshall_buffer, data));
1160 if (vdata->vacuum_fetch_list[i] == NULL) {
1161 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1165 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1177 * - Always do the fast vacuuming run, which traverses
1178 * - the in-memory fetch queue: these records have been
1179 * scheduled for migration
1180 * - the in-memory delete queue: these records have been
1181 * scheduled for deletion.
1182 * - Only if explicitly requested, the database is traversed
1183 * in order to use the traditional heuristics on empty records
1184 * to trigger deletion.
1185 * This is done only every VacuumFastPathCount'th vacuuming run.
1187 * The traverse runs fill two lists:
1189 * - The delete_list:
1190 * This is the list of empty records the current
1191 * node is lmaster and dmaster for. These records are later
1192 * deleted first on other nodes and then locally.
1194 * The fast vacuuming run has a short cut for those records
1195 * that have never been migrated with data: these records
1196 * are immediately deleted locally, since they have left
1197 * no trace on other nodes.
1199 * - The vacuum_fetch lists
1200 * (one for each other lmaster node):
1201 * The records in this list are sent for deletion to
1202 * their lmaster in a bulk VACUUM_FETCH control.
1204 * The lmaster then migrates all these records to itelf
1205 * so that they can be vacuumed there.
1207 * This executes in the child context.
1209 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1210 bool full_vacuum_run)
1212 struct ctdb_context *ctdb = ctdb_db->ctdb;
1214 struct vacuum_data *vdata;
1215 TALLOC_CTX *tmp_ctx;
1217 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1218 "%s db_id[0x%08x]\n",
1219 full_vacuum_run ? "full" : "fast",
1220 ctdb_db->db_name, ctdb_db->db_id));
1222 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1224 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1228 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1230 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1236 tmp_ctx = talloc_new(ctdb_db);
1237 if (tmp_ctx == NULL) {
1238 DEBUG(DEBUG_ERR, ("Out of memory!\n"));
1242 vdata = ctdb_vacuum_init_vacuum_data(ctdb_db, tmp_ctx);
1243 if (vdata == NULL) {
1244 talloc_free(tmp_ctx);
1248 if (full_vacuum_run) {
1249 ctdb_vacuum_traverse_db(ctdb_db, vdata);
1252 ctdb_process_fetch_queue(ctdb_db);
1254 ctdb_process_delete_queue(ctdb_db, vdata);
1256 ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1258 ctdb_process_delete_list(ctdb_db, vdata);
1260 talloc_free(tmp_ctx);
1266 * repack and vacuum a db
1267 * called from the child context
1269 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1270 bool full_vacuum_run)
1272 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1273 const char *name = ctdb_db->db_name;
1274 int freelist_size = 0;
1277 if (ctdb_vacuum_db(ctdb_db, full_vacuum_run) != 0) {
1278 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1281 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1282 if (freelist_size == -1) {
1283 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1288 * decide if a repack is necessary
1290 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1295 D_NOTICE("Repacking %s with %u freelist entries\n",
1299 ret = tdb_repack(ctdb_db->ltdb->tdb);
1301 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1308 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1310 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1315 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1317 double l = timeval_elapsed(&child_ctx->start_time);
1318 struct ctdb_vacuum_handle *vacuum_handle = child_ctx->vacuum_handle;
1319 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1320 struct ctdb_context *ctdb = ctdb_db->ctdb;
1322 CTDB_UPDATE_DB_LATENCY(ctdb_db, "vacuum", vacuum.latency, l);
1323 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1325 if (child_ctx->child_pid != -1) {
1326 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1328 /* Bump the number of successful fast-path runs. */
1329 vacuum_handle->fast_path_count++;
1332 ctdb->vacuumer = NULL;
1334 if (child_ctx->scheduled) {
1335 vacuum_handle->vacuum_interval = get_vacuum_interval(ctdb_db);
1340 timeval_current_ofs(vacuum_handle->vacuum_interval, 0),
1349 * this event is generated when a vacuum child process times out
1351 static void vacuum_child_timeout(struct tevent_context *ev,
1352 struct tevent_timer *te,
1353 struct timeval t, void *private_data)
1355 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1357 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1359 child_ctx->status = VACUUM_TIMEOUT;
1361 talloc_free(child_ctx);
1366 * this event is generated when a vacuum child process has completed
1368 static void vacuum_child_handler(struct tevent_context *ev,
1369 struct tevent_fd *fde,
1370 uint16_t flags, void *private_data)
1372 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1376 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1377 child_ctx->child_pid = -1;
1379 ret = sys_read(child_ctx->fd[0], &c, 1);
1380 if (ret != 1 || c != 0) {
1381 child_ctx->status = VACUUM_ERROR;
1382 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));
1384 child_ctx->status = VACUUM_OK;
1387 talloc_free(child_ctx);
1391 * this event is called every time we need to start a new vacuum process
1393 static int vacuum_db_child(TALLOC_CTX *mem_ctx,
1394 struct ctdb_db_context *ctdb_db,
1396 bool full_vacuum_run,
1397 struct ctdb_vacuum_child_context **out)
1399 struct ctdb_context *ctdb = ctdb_db->ctdb;
1400 struct ctdb_vacuum_child_context *child_ctx;
1401 struct tevent_fd *fde;
1404 /* we don't vacuum if we are in recovery mode, or db frozen */
1405 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1406 ctdb_db_frozen(ctdb_db)) {
1407 D_INFO("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1408 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ?
1409 "in recovery" : "frozen");
1413 /* Do not allow multiple vacuuming child processes to be active at the
1414 * same time. If there is vacuuming child process active, delay
1415 * new vacuuming event to stagger vacuuming events.
1417 if (ctdb->vacuumer != NULL) {
1421 child_ctx = talloc_zero(mem_ctx, struct ctdb_vacuum_child_context);
1422 if (child_ctx == NULL) {
1423 DBG_ERR("Failed to allocate child context for vacuuming of %s\n",
1429 ret = pipe(child_ctx->fd);
1431 talloc_free(child_ctx);
1432 D_ERR("Failed to create pipe for vacuum child process.\n");
1436 child_ctx->child_pid = ctdb_fork(ctdb);
1437 if (child_ctx->child_pid == (pid_t)-1) {
1438 close(child_ctx->fd[0]);
1439 close(child_ctx->fd[1]);
1440 talloc_free(child_ctx);
1441 D_ERR("Failed to fork vacuum child process.\n");
1446 if (child_ctx->child_pid == 0) {
1448 close(child_ctx->fd[0]);
1450 D_INFO("Vacuuming child process %d for db %s started\n",
1453 prctl_set_comment("ctdb_vacuum");
1454 ret = switch_from_server_to_client(ctdb);
1456 DBG_ERR("ERROR: failed to switch vacuum daemon "
1457 "into client mode.\n");
1461 cc = ctdb_vacuum_and_repack_db(ctdb_db, full_vacuum_run);
1463 sys_write(child_ctx->fd[1], &cc, 1);
1467 set_close_on_exec(child_ctx->fd[0]);
1468 close(child_ctx->fd[1]);
1470 child_ctx->status = VACUUM_RUNNING;
1471 child_ctx->scheduled = scheduled;
1472 child_ctx->start_time = timeval_current();
1474 ctdb->vacuumer = child_ctx;
1475 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1478 * Clear the fastpath vacuuming list in the parent.
1480 talloc_free(ctdb_db->delete_queue);
1481 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1482 if (ctdb_db->delete_queue == NULL) {
1483 DBG_ERR("Out of memory when re-creating vacuum tree\n");
1487 talloc_free(ctdb_db->fetch_queue);
1488 ctdb_db->fetch_queue = trbt_create(ctdb_db, 0);
1489 if (ctdb_db->fetch_queue == NULL) {
1490 ctdb_fatal(ctdb, "Out of memory when re-create fetch queue "
1491 " in parent context. Shutting down\n");
1494 tevent_add_timer(ctdb->ev, child_ctx,
1495 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time,
1497 vacuum_child_timeout, child_ctx);
1499 DBG_DEBUG(" Created PIPE FD:%d to child vacuum process\n",
1502 fde = tevent_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1503 TEVENT_FD_READ, vacuum_child_handler, child_ctx);
1504 tevent_fd_set_auto_close(fde);
1506 child_ctx->vacuum_handle = ctdb_db->vacuum_handle;
1512 static void ctdb_vacuum_event(struct tevent_context *ev,
1513 struct tevent_timer *te,
1514 struct timeval t, void *private_data)
1516 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(
1517 private_data, struct ctdb_vacuum_handle);
1518 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1519 struct ctdb_context *ctdb = ctdb_db->ctdb;
1520 struct ctdb_vacuum_child_context *child_ctx = NULL;
1521 uint32_t fast_path_max = ctdb->tunable.vacuum_fast_path_count;
1522 uint32_t vacuum_interval = get_vacuum_interval(ctdb_db);
1523 bool full_vacuum_run = false;
1526 if (vacuum_interval > vacuum_handle->vacuum_interval) {
1527 uint32_t d = vacuum_interval - vacuum_handle->vacuum_interval;
1529 DBG_INFO("Vacuum interval increased from "
1530 "%"PRIu32" to %"PRIu32", rescheduling\n",
1531 vacuum_handle->vacuum_interval,
1533 vacuum_handle->vacuum_interval = vacuum_interval;
1534 tevent_add_timer(ctdb->ev,
1536 timeval_current_ofs(d, 0),
1542 vacuum_handle->vacuum_interval = vacuum_interval;
1544 if (vacuum_handle->fast_path_count >= fast_path_max) {
1545 if (fast_path_max > 0) {
1546 full_vacuum_run = true;
1548 vacuum_handle->fast_path_count = 0;
1551 ret = vacuum_db_child(vacuum_handle,
1564 tevent_add_timer(ctdb->ev,
1566 timeval_current_ofs(0, 500*1000),
1572 /* Temporary failure, schedule next attempt */
1573 tevent_add_timer(ctdb->ev,
1575 timeval_current_ofs(
1576 vacuum_handle->vacuum_interval, 0),
1583 struct vacuum_control_state {
1584 struct ctdb_vacuum_child_context *child_ctx;
1585 struct ctdb_req_control_old *c;
1586 struct ctdb_context *ctdb;
1589 static int vacuum_control_state_destructor(struct vacuum_control_state *state)
1591 struct ctdb_vacuum_child_context *child_ctx = state->child_ctx;
1594 status = (child_ctx->status == VACUUM_OK ? 0 : -1);
1595 ctdb_request_control_reply(state->ctdb, state->c, NULL, status, NULL);
1600 int32_t ctdb_control_db_vacuum(struct ctdb_context *ctdb,
1601 struct ctdb_req_control_old *c,
1605 struct ctdb_db_context *ctdb_db;
1606 struct ctdb_vacuum_child_context *child_ctx = NULL;
1607 struct ctdb_db_vacuum *db_vacuum;
1608 struct vacuum_control_state *state;
1612 ret = ctdb_db_vacuum_pull(indata.dptr,
1618 DBG_ERR("Invalid data\n");
1622 ctdb_db = find_ctdb_db(ctdb, db_vacuum->db_id);
1623 if (ctdb_db == NULL) {
1624 DBG_ERR("Unknown db id 0x%08x\n", db_vacuum->db_id);
1625 talloc_free(db_vacuum);
1629 state = talloc(ctdb, struct vacuum_control_state);
1630 if (state == NULL) {
1631 DBG_ERR("Memory allocation error\n");
1635 ret = vacuum_db_child(ctdb_db,
1638 db_vacuum->full_vacuum_run,
1641 talloc_free(db_vacuum);
1644 (void) talloc_steal(child_ctx, state);
1646 state->child_ctx = child_ctx;
1647 state->c = talloc_steal(state, c);
1650 talloc_set_destructor(state, vacuum_control_state_destructor);
1652 *async_reply = true;
1660 DBG_WARNING("Vacuuming collision\n");
1664 DBG_ERR("Temporary vacuuming failure, ret=%d\n", ret);
1670 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1672 if (ctdb->vacuumer != NULL) {
1673 D_INFO("Aborting vacuuming for %s (%i)\n",
1674 ctdb->vacuumer->vacuum_handle->ctdb_db->db_name,
1675 (int)ctdb->vacuumer->child_pid);
1676 /* vacuum_child_destructor kills it, removes from list */
1677 talloc_free(ctdb->vacuumer);
1681 /* this function initializes the vacuuming context for a database
1682 * starts the vacuuming events
1684 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1686 struct ctdb_vacuum_handle *vacuum_handle;
1688 if (! ctdb_db_volatile(ctdb_db)) {
1690 ("Vacuuming is disabled for non-volatile database %s\n",
1695 vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1696 if (vacuum_handle == NULL) {
1697 DBG_ERR("Memory allocation error\n");
1701 vacuum_handle->ctdb_db = ctdb_db;
1702 vacuum_handle->fast_path_count = 0;
1703 vacuum_handle->vacuum_interval = get_vacuum_interval(ctdb_db);
1705 ctdb_db->vacuum_handle = vacuum_handle;
1707 tevent_add_timer(ctdb_db->ctdb->ev,
1709 timeval_current_ofs(vacuum_handle->vacuum_interval, 0),
1716 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1717 const struct ctdb_ltdb_header *hdr,
1720 struct delete_record_data *kd;
1723 hash = (uint32_t)ctdb_hash(&key);
1725 DEBUG(DEBUG_DEBUG, (__location__
1726 " remove_record_from_delete_queue: "
1731 "migrated_with_data[%s]\n",
1732 ctdb_db->db_name, ctdb_db->db_id,
1734 ctdb_lmaster(ctdb_db->ctdb, &key),
1735 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1737 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1739 DEBUG(DEBUG_DEBUG, (__location__
1740 " remove_record_from_delete_queue: "
1741 "record not in queue (hash[0x%08x])\n.",
1746 if ((kd->key.dsize != key.dsize) ||
1747 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1749 DEBUG(DEBUG_DEBUG, (__location__
1750 " remove_record_from_delete_queue: "
1751 "hash collision for key with hash[0x%08x] "
1752 "in db[%s] - skipping\n",
1753 hash, ctdb_db->db_name));
1757 DEBUG(DEBUG_DEBUG, (__location__
1758 " remove_record_from_delete_queue: "
1759 "removing key with hash[0x%08x]\n",
1768 * Insert a record into the ctdb_db context's delete queue,
1769 * handling hash collisions.
1771 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1772 const struct ctdb_ltdb_header *hdr,
1775 struct delete_record_data *kd;
1779 hash = (uint32_t)ctdb_hash(&key);
1781 DEBUG(DEBUG_DEBUG, (__location__ " schedule for deletion: db[%s] "
1785 "migrated_with_data[%s]\n",
1786 ctdb_db->db_name, ctdb_db->db_id,
1788 ctdb_lmaster(ctdb_db->ctdb, &key),
1789 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1791 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1793 if ((kd->key.dsize != key.dsize) ||
1794 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1797 (__location__ " schedule for deletion: "
1798 "hash collision for key hash [0x%08x]. "
1799 "Skipping the record.\n", hash));
1803 (__location__ " schedule for deletion: "
1804 "updating entry for key with hash [0x%08x].\n",
1809 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1810 ctdb_db->delete_queue,
1814 (__location__ " schedule for deletion: error "
1815 "inserting key with hash [0x%08x] into delete queue\n",
1824 * Schedule a record for deletetion.
1825 * Called from the parent context.
1827 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1830 struct ctdb_control_schedule_for_deletion *dd;
1831 struct ctdb_db_context *ctdb_db;
1835 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1837 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1838 if (ctdb_db == NULL) {
1839 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1844 key.dsize = dd->keylen;
1847 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1852 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1853 const struct ctdb_ltdb_header *hdr,
1857 struct ctdb_control_schedule_for_deletion *dd;
1861 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1862 /* main daemon - directly queue */
1863 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1868 /* if we don't have a connection to the daemon we can not send
1869 a control. For example sometimes from update_record control child
1872 if (!ctdb_db->ctdb->can_send_controls) {
1877 /* child process: send the main daemon a control */
1878 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1879 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1880 if (indata.dptr == NULL) {
1881 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1884 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1885 dd->db_id = ctdb_db->db_id;
1887 dd->keylen = key.dsize;
1888 memcpy(dd->key, key.dptr, key.dsize);
1890 ret = ctdb_control(ctdb_db->ctdb,
1893 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1894 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1899 NULL, /* timeout : NULL == wait forever */
1900 NULL); /* error message */
1902 talloc_free(indata.dptr);
1904 if (ret != 0 || status != 0) {
1905 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1906 "SCHEDULE_FOR_DELETION "
1916 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1917 const struct ctdb_ltdb_header *hdr,
1920 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1922 * Only remove the record from the delete queue if called
1923 * in the main daemon.
1928 remove_record_from_delete_queue(ctdb_db, hdr, key);
1933 static int vacuum_fetch_parser(uint32_t reqid,
1934 struct ctdb_ltdb_header *header,
1935 TDB_DATA key, TDB_DATA data,
1938 struct ctdb_db_context *ctdb_db = talloc_get_type_abort(
1939 private_data, struct ctdb_db_context);
1940 struct fetch_record_data *rd;
1944 len = offsetof(struct fetch_record_data, keydata) + key.dsize;
1946 rd = (struct fetch_record_data *)talloc_size(ctdb_db->fetch_queue,
1949 DEBUG(DEBUG_ERR, (__location__ " Memory error\n"));
1952 talloc_set_name_const(rd, "struct fetch_record_data");
1954 rd->key.dsize = key.dsize;
1955 rd->key.dptr = rd->keydata;
1956 memcpy(rd->keydata, key.dptr, key.dsize);
1958 hash = ctdb_hash(&key);
1960 trbt_insert32(ctdb_db->fetch_queue, hash, rd);
1965 int32_t ctdb_control_vacuum_fetch(struct ctdb_context *ctdb, TDB_DATA indata)
1967 struct ctdb_rec_buffer *recbuf;
1968 struct ctdb_db_context *ctdb_db;
1972 ret = ctdb_rec_buffer_pull(indata.dptr, indata.dsize, ctdb, &recbuf,
1975 DEBUG(DEBUG_ERR, ("Invalid data in vacuum_fetch\n"));
1979 ctdb_db = find_ctdb_db(ctdb, recbuf->db_id);
1980 if (ctdb_db == NULL) {
1981 talloc_free(recbuf);
1982 DEBUG(DEBUG_ERR, (__location__ " Unknown db 0x%08x\n",
1987 ret = ctdb_rec_buffer_traverse(recbuf, vacuum_fetch_parser, ctdb_db);
1988 talloc_free(recbuf);