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 "common/rb_tree.h"
41 #include "common/common.h"
42 #include "common/logging.h"
44 #include "protocol/protocol_api.h"
46 #define TIMELIMIT() timeval_current_ofs(10, 0)
48 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
50 struct ctdb_vacuum_child_context {
51 struct ctdb_vacuum_handle *vacuum_handle;
52 /* fd child writes status to */
55 enum vacuum_child_status status;
56 struct timeval start_time;
59 struct ctdb_vacuum_handle {
60 struct ctdb_db_context *ctdb_db;
61 uint32_t fast_path_count;
65 /* a list of records to possibly delete */
67 struct ctdb_context *ctdb;
68 struct ctdb_db_context *ctdb_db;
69 struct tdb_context *dest_db;
70 trbt_tree_t *delete_list;
71 struct ctdb_marshall_buffer **vacuum_fetch_list;
77 uint32_t added_to_vacuum_fetch_list;
78 uint32_t added_to_delete_list;
92 uint32_t remote_error;
105 /* this structure contains the information for one record to be deleted */
106 struct delete_record_data {
107 struct ctdb_context *ctdb;
108 struct ctdb_db_context *ctdb_db;
109 struct ctdb_ltdb_header hdr;
110 uint32_t remote_fail_count;
115 struct delete_records_list {
116 struct ctdb_marshall_buffer *records;
117 struct vacuum_data *vdata;
120 struct fetch_record_data {
125 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
126 const struct ctdb_ltdb_header *hdr,
130 * Store key and header in a tree, indexed by the key hash.
132 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
133 struct ctdb_db_context *ctdb_db,
135 const struct ctdb_ltdb_header *hdr,
138 struct delete_record_data *dd;
142 len = offsetof(struct delete_record_data, keydata) + key.dsize;
144 dd = (struct delete_record_data *)talloc_size(tree, len);
146 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
149 talloc_set_name_const(dd, "struct delete_record_data");
152 dd->ctdb_db = ctdb_db;
153 dd->key.dsize = key.dsize;
154 dd->key.dptr = dd->keydata;
155 memcpy(dd->keydata, key.dptr, key.dsize);
158 dd->remote_fail_count = 0;
160 hash = ctdb_hash(&key);
162 trbt_insert32(tree, hash, dd);
167 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
168 struct ctdb_ltdb_header *hdr)
170 struct ctdb_context *ctdb = vdata->ctdb;
171 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
175 hash = ctdb_hash(&key);
177 if (trbt_lookup32(vdata->delete_list, hash)) {
178 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
182 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
189 vdata->count.delete_list.total++;
195 * Add a record to the list of records to be sent
196 * to their lmaster with VACUUM_FETCH.
198 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
201 struct ctdb_context *ctdb = vdata->ctdb;
203 struct ctdb_marshall_buffer *vfl;
205 lmaster = ctdb_lmaster(ctdb, &key);
207 vfl = vdata->vacuum_fetch_list[lmaster];
209 vfl = ctdb_marshall_add(ctdb, vfl, vfl->db_id, ctdb->pnn,
210 key, NULL, tdb_null);
212 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
213 vdata->traverse_error = true;
217 vdata->vacuum_fetch_list[lmaster] = vfl;
223 static void ctdb_vacuum_event(struct tevent_context *ev,
224 struct tevent_timer *te,
225 struct timeval t, void *private_data);
227 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
229 struct ctdb_ltdb_header *header =
230 (struct ctdb_ltdb_header *)private_data;
232 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
236 *header = *(struct ctdb_ltdb_header *)data.dptr;
242 * traverse function for gathering the records that can be deleted
244 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
247 struct vacuum_data *vdata = talloc_get_type(private_data,
249 struct ctdb_context *ctdb = vdata->ctdb;
250 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
252 struct ctdb_ltdb_header *hdr;
255 vdata->count.db_traverse.total++;
257 lmaster = ctdb_lmaster(ctdb, &key);
258 if (lmaster >= ctdb->num_nodes) {
259 vdata->count.db_traverse.error++;
260 DEBUG(DEBUG_CRIT, (__location__
261 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
264 (unsigned)ctdb->num_nodes,
265 (unsigned)ctdb_hash(&key)));
269 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
270 /* it is not a deleted record */
271 vdata->count.db_traverse.skipped++;
275 hdr = (struct ctdb_ltdb_header *)data.dptr;
277 if (hdr->dmaster != ctdb->pnn) {
278 vdata->count.db_traverse.skipped++;
283 * Add the record to this process's delete_queue for processing
284 * in the subsequent traverse in the fast vacuum run.
286 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
288 vdata->count.db_traverse.error++;
290 vdata->count.db_traverse.scheduled++;
297 * traverse the tree of records to delete and marshall them into
300 static int delete_marshall_traverse(void *param, void *data)
302 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
303 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
304 struct ctdb_marshall_buffer *m;
306 m = ctdb_marshall_add(recs, recs->records, recs->records->db_id,
307 recs->records->db_id,
308 dd->key, &dd->hdr, tdb_null);
310 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
318 struct fetch_queue_state {
319 struct ctdb_db_context *ctdb_db;
323 struct fetch_record_migrate_state {
324 struct fetch_queue_state *fetch_queue;
328 static void fetch_record_migrate_callback(struct ctdb_client_call_state *state)
330 struct fetch_record_migrate_state *fetch = talloc_get_type_abort(
331 state->async.private_data, struct fetch_record_migrate_state);
332 struct fetch_queue_state *fetch_queue = fetch->fetch_queue;
333 struct ctdb_ltdb_header hdr;
334 struct ctdb_call call = { 0 };
337 ret = ctdb_call_recv(state, &call);
338 fetch_queue->count--;
340 D_ERR("Failed to migrate record for vacuuming\n");
344 ret = tdb_chainlock_nonblock(fetch_queue->ctdb_db->ltdb->tdb,
350 ret = tdb_parse_record(fetch_queue->ctdb_db->ltdb->tdb,
352 vacuum_record_parser,
355 tdb_chainunlock(fetch_queue->ctdb_db->ltdb->tdb, fetch->key);
361 D_INFO("Vacuum Fetch record, key=%.*s\n",
362 (int)fetch->key.dsize,
365 (void) ctdb_local_schedule_for_deletion(fetch_queue->ctdb_db,
373 static int fetch_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
375 struct ctdb_ltdb_header *header =
376 (struct ctdb_ltdb_header *)private_data;
378 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
382 memcpy(header, data.dptr, sizeof(*header));
387 * traverse function for the traversal of the fetch_queue.
389 * Send a record migration request.
391 static int fetch_queue_traverse(void *param, void *data)
393 struct fetch_record_data *rd = talloc_get_type_abort(
394 data, struct fetch_record_data);
395 struct fetch_queue_state *fetch_queue =
396 (struct fetch_queue_state *)param;
397 struct ctdb_db_context *ctdb_db = fetch_queue->ctdb_db;
398 struct ctdb_client_call_state *state;
399 struct fetch_record_migrate_state *fetch;
400 struct ctdb_call call = { 0 };
401 struct ctdb_ltdb_header header;
404 ret = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, rd->key);
409 ret = tdb_parse_record(ctdb_db->ltdb->tdb,
414 tdb_chainunlock(ctdb_db->ltdb->tdb, rd->key);
420 if (header.dmaster == ctdb_db->ctdb->pnn) {
421 /* If the record is already migrated, skip */
425 fetch = talloc_zero(ctdb_db, struct fetch_record_migrate_state);
427 D_ERR("Failed to setup fetch record migrate state\n");
431 fetch->fetch_queue = fetch_queue;
433 fetch->key.dsize = rd->key.dsize;
434 fetch->key.dptr = talloc_memdup(fetch, rd->key.dptr, rd->key.dsize);
435 if (fetch->key.dptr == NULL) {
436 D_ERR("Memory error in fetch_queue_traverse\n");
441 call.call_id = CTDB_NULL_FUNC;
442 call.flags = CTDB_IMMEDIATE_MIGRATION |
443 CTDB_CALL_FLAG_VACUUM_MIGRATION;
444 call.key = fetch->key;
446 state = ctdb_call_send(ctdb_db, &call);
448 DEBUG(DEBUG_ERR, ("Failed to setup vacuum fetch call\n"));
453 state->async.fn = fetch_record_migrate_callback;
454 state->async.private_data = fetch;
456 fetch_queue->count++;
461 D_INFO("Skipped Fetch record, key=%.*s\n",
468 * Traverse the fetch.
469 * Records are migrated to the local node and
470 * added to delete queue for further processing.
472 static void ctdb_process_fetch_queue(struct ctdb_db_context *ctdb_db)
474 struct fetch_queue_state state;
477 state.ctdb_db = ctdb_db;
480 ret = trbt_traversearray32(ctdb_db->fetch_queue, 1,
481 fetch_queue_traverse, &state);
483 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
484 "the fetch queue.\n"));
487 /* Wait for all migrations to complete */
488 while (state.count > 0) {
489 tevent_loop_once(ctdb_db->ctdb->ev);
494 * traverse function for the traversal of the delete_queue,
495 * the fast-path vacuuming list.
497 * - If the record has been migrated off the node
498 * or has been revived (filled with data) on the node,
499 * then skip the record.
501 * - If the current node is the record's lmaster and it is
502 * a record that has never been migrated with data, then
503 * delete the record from the local tdb.
505 * - If the current node is the record's lmaster and it has
506 * been migrated with data, then schedule it for the normal
507 * vacuuming procedure (i.e. add it to the delete_list).
509 * - If the current node is NOT the record's lmaster then
510 * add it to the list of records that are to be sent to
511 * the lmaster with the VACUUM_FETCH message.
513 static int delete_queue_traverse(void *param, void *data)
515 struct delete_record_data *dd =
516 talloc_get_type(data, struct delete_record_data);
517 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
518 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
519 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
521 struct ctdb_ltdb_header header;
523 uint32_t hash = ctdb_hash(&(dd->key));
525 vdata->count.delete_queue.total++;
527 res = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, dd->key);
529 vdata->count.delete_queue.error++;
533 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
534 vacuum_record_parser, &header);
539 if (header.dmaster != ctdb->pnn) {
540 /* The record has been migrated off the node. Skip. */
544 if (header.rsn != dd->hdr.rsn) {
546 * The record has been migrated off the node and back again.
547 * But not requeued for deletion. Skip it.
553 * We are dmaster, and the record has no data, and it has
554 * not been migrated after it has been queued for deletion.
556 * At this stage, the record could still have been revived locally
557 * and last been written with empty data. This can only be
558 * fixed with the addition of an active or delete flag. (TODO)
561 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
563 if (lmaster != ctdb->pnn) {
564 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
568 (__location__ " Error adding record to list "
569 "of records to send to lmaster.\n"));
570 vdata->count.delete_queue.error++;
572 vdata->count.delete_queue.added_to_vacuum_fetch_list++;
577 /* use header->flags or dd->hdr.flags ?? */
578 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
579 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
583 (__location__ " Error adding record to list "
584 "of records for deletion on lmaster.\n"));
585 vdata->count.delete_queue.error++;
587 vdata->count.delete_queue.added_to_delete_list++;
590 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
594 (__location__ " Error deleting record with key "
595 "hash [0x%08x] from local data base db[%s].\n",
596 hash, ctdb_db->db_name));
597 vdata->count.delete_queue.error++;
602 (__location__ " Deleted record with key hash "
603 "[0x%08x] from local data base db[%s].\n",
604 hash, ctdb_db->db_name));
605 vdata->count.delete_queue.deleted++;
611 vdata->count.delete_queue.skipped++;
614 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
620 * Delete the records that we are lmaster and dmaster for and
621 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
624 static int delete_record_traverse(void *param, void *data)
626 struct delete_record_data *dd =
627 talloc_get_type(data, struct delete_record_data);
628 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
629 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
630 struct ctdb_context *ctdb = ctdb_db->ctdb;
632 struct ctdb_ltdb_header header;
634 uint32_t hash = ctdb_hash(&(dd->key));
636 if (dd->remote_fail_count > 0) {
637 vdata->count.delete_list.remote_error++;
638 vdata->count.delete_list.left--;
643 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
646 (__location__ " Error getting chainlock on record with "
647 "key hash [0x%08x] on database db[%s].\n",
648 hash, ctdb_db->db_name));
649 vdata->count.delete_list.local_error++;
650 vdata->count.delete_list.left--;
656 * Verify that the record is still empty, its RSN has not
657 * changed and that we are still its lmaster and dmaster.
660 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
661 vacuum_record_parser, &header);
666 if (header.flags & CTDB_REC_RO_FLAGS) {
667 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
668 "on database db[%s] has read-only flags. "
670 hash, ctdb_db->db_name));
674 if (header.dmaster != ctdb->pnn) {
675 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
676 "on database db[%s] has been migrated away. "
678 hash, ctdb_db->db_name));
682 if (header.rsn != dd->hdr.rsn) {
684 * The record has been migrated off the node and back again.
685 * But not requeued for deletion. Skip it.
687 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
688 "on database db[%s] seems to have been "
689 "migrated away and back again (with empty "
690 "data). skipping.\n",
691 hash, ctdb_db->db_name));
695 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
697 if (lmaster != ctdb->pnn) {
698 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
699 "delete list (key hash [0x%08x], db[%s]). "
700 "Strange! skipping.\n",
701 hash, ctdb_db->db_name));
705 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
709 (__location__ " Error deleting record with key hash "
710 "[0x%08x] from local data base db[%s].\n",
711 hash, ctdb_db->db_name));
712 vdata->count.delete_list.local_error++;
717 (__location__ " Deleted record with key hash [0x%08x] from "
718 "local data base db[%s].\n", hash, ctdb_db->db_name));
720 vdata->count.delete_list.deleted++;
724 vdata->count.delete_list.skipped++;
727 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
730 vdata->count.delete_list.left--;
736 * Traverse the delete_queue.
737 * Records are either deleted directly or filled
738 * into the delete list or the vacuum fetch lists
739 * for further processing.
741 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
742 struct vacuum_data *vdata)
747 ret = trbt_traversearray32(ctdb_db->delete_queue, 1,
748 delete_queue_traverse, vdata);
751 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
752 "the delete queue.\n"));
755 sum = vdata->count.delete_queue.deleted
756 + vdata->count.delete_queue.skipped
757 + vdata->count.delete_queue.error
758 + vdata->count.delete_queue.added_to_delete_list
759 + vdata->count.delete_queue.added_to_vacuum_fetch_list;
761 if (vdata->count.delete_queue.total != sum) {
762 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
763 "counts for db[%s]: total[%u] != sum[%u]\n",
765 (unsigned)vdata->count.delete_queue.total,
769 if (vdata->count.delete_queue.total > 0) {
772 " fast vacuuming delete_queue traverse statistics: "
781 (unsigned)vdata->count.delete_queue.total,
782 (unsigned)vdata->count.delete_queue.deleted,
783 (unsigned)vdata->count.delete_queue.skipped,
784 (unsigned)vdata->count.delete_queue.error,
785 (unsigned)vdata->count.delete_queue.added_to_delete_list,
786 (unsigned)vdata->count.delete_queue.added_to_vacuum_fetch_list));
793 * read-only traverse of the database, looking for records that
794 * might be able to be vacuumed.
796 * This is not done each time but only every tunable
797 * VacuumFastPathCount times.
799 static void ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
800 struct vacuum_data *vdata)
804 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
805 if (ret == -1 || vdata->traverse_error) {
806 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
807 "'%s'\n", ctdb_db->db_name));
811 if (vdata->count.db_traverse.total > 0) {
814 " full vacuuming db traverse statistics: "
821 (unsigned)vdata->count.db_traverse.total,
822 (unsigned)vdata->count.db_traverse.skipped,
823 (unsigned)vdata->count.db_traverse.error,
824 (unsigned)vdata->count.db_traverse.scheduled));
831 * Process the vacuum fetch lists:
832 * For records for which we are not the lmaster, tell the lmaster to
835 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
836 struct vacuum_data *vdata)
839 struct ctdb_context *ctdb = ctdb_db->ctdb;
842 for (i = 0; i < ctdb->num_nodes; i++) {
844 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
846 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
850 if (vfl->count == 0) {
854 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
855 vfl->count, ctdb->nodes[i]->pnn,
858 data = ctdb_marshall_finish(vfl);
860 ret = ctdb_control(ctdb, ctdb->nodes[i]->pnn, 0,
861 CTDB_CONTROL_VACUUM_FETCH, 0,
862 data, NULL, NULL, &res, NULL, NULL);
863 if (ret != 0 || res != 0) {
864 DEBUG(DEBUG_ERR, ("Failed to send vacuum "
865 "fetch control to node %u\n",
866 ctdb->nodes[i]->pnn));
872 * Process the delete list:
874 * This is the last step of vacuuming that consistently deletes
875 * those records that have been migrated with data and can hence
876 * not be deleted when leaving a node.
878 * In this step, the lmaster does the final deletion of those empty
879 * records that it is also dmaster for. It has ususally received
880 * at least some of these records previously from the former dmasters
881 * with the vacuum fetch message.
883 * 1) Send the records to all active nodes with the TRY_DELETE_RECORDS
884 * control. The remote notes delete their local copy.
885 * 2) The lmaster locally deletes its copies of all records that
886 * could successfully be deleted remotely in step #2.
888 static void ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
889 struct vacuum_data *vdata)
892 struct ctdb_context *ctdb = ctdb_db->ctdb;
893 struct delete_records_list *recs;
895 struct ctdb_node_map_old *nodemap;
896 uint32_t *active_nodes;
897 int num_active_nodes;
901 if (vdata->count.delete_list.total == 0) {
905 tmp_ctx = talloc_new(vdata);
906 if (tmp_ctx == NULL) {
907 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
911 vdata->count.delete_list.left = vdata->count.delete_list.total;
914 * get the list of currently active nodes
917 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
922 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
926 active_nodes = list_of_active_nodes(ctdb, nodemap,
927 nodemap, /* talloc context */
928 false /* include self */);
930 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
933 * Now delete the records all active nodes in a two-phase process:
934 * 1) tell all active remote nodes to delete all their copy
935 * 2) if all remote nodes deleted their record copy, delete it locally
938 recs = talloc_zero(tmp_ctx, struct delete_records_list);
940 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
946 * Send all records to all active nodes for deletion.
950 * Create a marshall blob from the remaining list of records to delete.
953 recs->records = (struct ctdb_marshall_buffer *)
954 talloc_zero_size(recs,
955 offsetof(struct ctdb_marshall_buffer, data));
956 if (recs->records == NULL) {
957 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
960 recs->records->db_id = ctdb_db->db_id;
962 ret = trbt_traversearray32(vdata->delete_list, 1,
963 delete_marshall_traverse, recs);
965 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
966 "delete list for second marshalling.\n"));
970 indata = ctdb_marshall_finish(recs->records);
972 for (i = 0; i < num_active_nodes; i++) {
973 struct ctdb_marshall_buffer *records;
974 struct ctdb_rec_data_old *rec;
978 ret = ctdb_control(ctdb, active_nodes[i], 0,
979 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
980 indata, recs, &outdata, &res,
982 if (ret != 0 || res != 0) {
983 DEBUG(DEBUG_ERR, ("Failed to delete records on "
984 "node %u: ret[%d] res[%d]\n",
985 active_nodes[i], ret, res));
990 * outdata contains the list of records coming back
991 * from the node: These are the records that the
992 * remote node could not delete. We remove these from
993 * the list to delete locally.
995 records = (struct ctdb_marshall_buffer *)outdata.dptr;
996 rec = (struct ctdb_rec_data_old *)&records->data[0];
997 while (records->count-- > 0) {
998 TDB_DATA reckey, recdata;
999 struct ctdb_ltdb_header *rechdr;
1000 struct delete_record_data *dd;
1002 reckey.dptr = &rec->data[0];
1003 reckey.dsize = rec->keylen;
1004 recdata.dptr = &rec->data[reckey.dsize];
1005 recdata.dsize = rec->datalen;
1007 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1008 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1011 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1012 recdata.dptr += sizeof(*rechdr);
1013 recdata.dsize -= sizeof(*rechdr);
1015 dd = (struct delete_record_data *)trbt_lookup32(
1017 ctdb_hash(&reckey));
1020 * The remote node could not delete the
1021 * record. Since other remote nodes can
1022 * also fail, we just mark the record.
1024 dd->remote_fail_count++;
1026 DEBUG(DEBUG_ERR, (__location__ " Failed to "
1027 "find record with hash 0x%08x coming "
1028 "back from TRY_DELETE_RECORDS "
1029 "control in delete list.\n",
1030 ctdb_hash(&reckey)));
1033 rec = (struct ctdb_rec_data_old *)(rec->length + (uint8_t *)rec);
1039 * Delete the remaining records locally.
1041 * These records have successfully been deleted on all
1042 * active remote nodes.
1045 ret = trbt_traversearray32(vdata->delete_list, 1,
1046 delete_record_traverse, vdata);
1048 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1049 "delete list for deletion.\n"));
1052 if (vdata->count.delete_list.left != 0) {
1053 DEBUG(DEBUG_ERR, (__location__ " Vaccum db[%s] error: "
1054 "there are %u records left for deletion after "
1055 "processing delete list\n",
1057 (unsigned)vdata->count.delete_list.left));
1060 sum = vdata->count.delete_list.deleted
1061 + vdata->count.delete_list.skipped
1062 + vdata->count.delete_list.remote_error
1063 + vdata->count.delete_list.local_error
1064 + vdata->count.delete_list.left;
1066 if (vdata->count.delete_list.total != sum) {
1067 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1068 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1070 (unsigned)vdata->count.delete_list.total,
1074 if (vdata->count.delete_list.total > 0) {
1077 " vacuum delete list statistics: "
1086 (unsigned)vdata->count.delete_list.total,
1087 (unsigned)vdata->count.delete_list.deleted,
1088 (unsigned)vdata->count.delete_list.skipped,
1089 (unsigned)vdata->count.delete_list.remote_error,
1090 (unsigned)vdata->count.delete_list.local_error,
1091 (unsigned)vdata->count.delete_list.left));
1095 talloc_free(tmp_ctx);
1101 * initialize the vacuum_data
1103 static struct vacuum_data *ctdb_vacuum_init_vacuum_data(
1104 struct ctdb_db_context *ctdb_db,
1105 TALLOC_CTX *mem_ctx)
1108 struct ctdb_context *ctdb = ctdb_db->ctdb;
1109 struct vacuum_data *vdata;
1111 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1112 if (vdata == NULL) {
1113 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1117 vdata->ctdb = ctdb_db->ctdb;
1118 vdata->ctdb_db = ctdb_db;
1119 vdata->delete_list = trbt_create(vdata, 0);
1120 if (vdata->delete_list == NULL) {
1121 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1125 vdata->start = timeval_current();
1127 vdata->count.delete_queue.added_to_delete_list = 0;
1128 vdata->count.delete_queue.added_to_vacuum_fetch_list = 0;
1129 vdata->count.delete_queue.deleted = 0;
1130 vdata->count.delete_queue.skipped = 0;
1131 vdata->count.delete_queue.error = 0;
1132 vdata->count.delete_queue.total = 0;
1133 vdata->count.db_traverse.scheduled = 0;
1134 vdata->count.db_traverse.skipped = 0;
1135 vdata->count.db_traverse.error = 0;
1136 vdata->count.db_traverse.total = 0;
1137 vdata->count.delete_list.total = 0;
1138 vdata->count.delete_list.left = 0;
1139 vdata->count.delete_list.remote_error = 0;
1140 vdata->count.delete_list.local_error = 0;
1141 vdata->count.delete_list.skipped = 0;
1142 vdata->count.delete_list.deleted = 0;
1144 /* the list needs to be of length num_nodes */
1145 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1146 struct ctdb_marshall_buffer *,
1148 if (vdata->vacuum_fetch_list == NULL) {
1149 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1152 for (i = 0; i < ctdb->num_nodes; i++) {
1153 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1154 talloc_zero_size(vdata->vacuum_fetch_list,
1155 offsetof(struct ctdb_marshall_buffer, data));
1156 if (vdata->vacuum_fetch_list[i] == NULL) {
1157 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1161 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1173 * - Always do the fast vacuuming run, which traverses
1174 * - the in-memory fetch queue: these records have been
1175 * scheduled for migration
1176 * - the in-memory delete queue: these records have been
1177 * scheduled for deletion.
1178 * - Only if explicitly requested, the database is traversed
1179 * in order to use the traditional heuristics on empty records
1180 * to trigger deletion.
1181 * This is done only every VacuumFastPathCount'th vacuuming run.
1183 * The traverse runs fill two lists:
1185 * - The delete_list:
1186 * This is the list of empty records the current
1187 * node is lmaster and dmaster for. These records are later
1188 * deleted first on other nodes and then locally.
1190 * The fast vacuuming run has a short cut for those records
1191 * that have never been migrated with data: these records
1192 * are immediately deleted locally, since they have left
1193 * no trace on other nodes.
1195 * - The vacuum_fetch lists
1196 * (one for each other lmaster node):
1197 * The records in this list are sent for deletion to
1198 * their lmaster in a bulk VACUUM_FETCH control.
1200 * The lmaster then migrates all these records to itelf
1201 * so that they can be vacuumed there.
1203 * This executes in the child context.
1205 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1206 bool full_vacuum_run)
1208 struct ctdb_context *ctdb = ctdb_db->ctdb;
1210 struct vacuum_data *vdata;
1211 TALLOC_CTX *tmp_ctx;
1213 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1214 "%s db_id[0x%08x]\n",
1215 full_vacuum_run ? "full" : "fast",
1216 ctdb_db->db_name, ctdb_db->db_id));
1218 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1220 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1224 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1226 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1232 tmp_ctx = talloc_new(ctdb_db);
1233 if (tmp_ctx == NULL) {
1234 DEBUG(DEBUG_ERR, ("Out of memory!\n"));
1238 vdata = ctdb_vacuum_init_vacuum_data(ctdb_db, tmp_ctx);
1239 if (vdata == NULL) {
1240 talloc_free(tmp_ctx);
1244 if (full_vacuum_run) {
1245 ctdb_vacuum_traverse_db(ctdb_db, vdata);
1248 ctdb_process_fetch_queue(ctdb_db);
1250 ctdb_process_delete_queue(ctdb_db, vdata);
1252 ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1254 ctdb_process_delete_list(ctdb_db, vdata);
1256 talloc_free(tmp_ctx);
1262 * repack and vaccum a db
1263 * called from the child context
1265 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1266 bool full_vacuum_run)
1268 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1269 const char *name = ctdb_db->db_name;
1270 int freelist_size = 0;
1273 if (ctdb_vacuum_db(ctdb_db, full_vacuum_run) != 0) {
1274 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1277 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1278 if (freelist_size == -1) {
1279 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1284 * decide if a repack is necessary
1286 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1291 D_NOTICE("Repacking %s with %u freelist entries\n",
1295 ret = tdb_repack(ctdb_db->ltdb->tdb);
1297 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1304 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1306 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1311 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1313 double l = timeval_elapsed(&child_ctx->start_time);
1314 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1315 struct ctdb_context *ctdb = ctdb_db->ctdb;
1317 CTDB_UPDATE_DB_LATENCY(ctdb_db, "vacuum", vacuum.latency, l);
1318 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1320 if (child_ctx->child_pid != -1) {
1321 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1323 /* Bump the number of successful fast-path runs. */
1324 child_ctx->vacuum_handle->fast_path_count++;
1327 ctdb->vacuumer = NULL;
1329 tevent_add_timer(ctdb->ev, child_ctx->vacuum_handle,
1330 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1331 ctdb_vacuum_event, child_ctx->vacuum_handle);
1337 * this event is generated when a vacuum child process times out
1339 static void vacuum_child_timeout(struct tevent_context *ev,
1340 struct tevent_timer *te,
1341 struct timeval t, void *private_data)
1343 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1345 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1347 child_ctx->status = VACUUM_TIMEOUT;
1349 talloc_free(child_ctx);
1354 * this event is generated when a vacuum child process has completed
1356 static void vacuum_child_handler(struct tevent_context *ev,
1357 struct tevent_fd *fde,
1358 uint16_t flags, void *private_data)
1360 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1364 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1365 child_ctx->child_pid = -1;
1367 ret = sys_read(child_ctx->fd[0], &c, 1);
1368 if (ret != 1 || c != 0) {
1369 child_ctx->status = VACUUM_ERROR;
1370 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));
1372 child_ctx->status = VACUUM_OK;
1375 talloc_free(child_ctx);
1379 * this event is called every time we need to start a new vacuum process
1381 static int vacuum_db_child(TALLOC_CTX *mem_ctx,
1382 struct ctdb_db_context *ctdb_db,
1383 bool full_vacuum_run,
1384 struct ctdb_vacuum_child_context **out)
1386 struct ctdb_context *ctdb = ctdb_db->ctdb;
1387 struct ctdb_vacuum_child_context *child_ctx;
1388 struct tevent_fd *fde;
1391 /* we don't vacuum if we are in recovery mode, or db frozen */
1392 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1393 ctdb_db_frozen(ctdb_db)) {
1394 D_INFO("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1395 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ?
1396 "in recovery" : "frozen");
1400 /* Do not allow multiple vacuuming child processes to be active at the
1401 * same time. If there is vacuuming child process active, delay
1402 * new vacuuming event to stagger vacuuming events.
1404 if (ctdb->vacuumer != NULL) {
1408 child_ctx = talloc_zero(mem_ctx, struct ctdb_vacuum_child_context);
1409 if (child_ctx == NULL) {
1410 DBG_ERR("Failed to allocate child context for vacuuming of %s\n",
1416 ret = pipe(child_ctx->fd);
1418 talloc_free(child_ctx);
1419 D_ERR("Failed to create pipe for vacuum child process.\n");
1423 child_ctx->child_pid = ctdb_fork(ctdb);
1424 if (child_ctx->child_pid == (pid_t)-1) {
1425 close(child_ctx->fd[0]);
1426 close(child_ctx->fd[1]);
1427 talloc_free(child_ctx);
1428 D_ERR("Failed to fork vacuum child process.\n");
1433 if (child_ctx->child_pid == 0) {
1435 close(child_ctx->fd[0]);
1437 D_INFO("Vacuuming child process %d for db %s started\n",
1440 prctl_set_comment("ctdb_vacuum");
1441 ret = switch_from_server_to_client(ctdb);
1443 DBG_ERR("ERROR: failed to switch vacuum daemon "
1444 "into client mode.\n");
1448 cc = ctdb_vacuum_and_repack_db(ctdb_db, full_vacuum_run);
1450 sys_write(child_ctx->fd[1], &cc, 1);
1454 set_close_on_exec(child_ctx->fd[0]);
1455 close(child_ctx->fd[1]);
1457 child_ctx->status = VACUUM_RUNNING;
1458 child_ctx->start_time = timeval_current();
1460 ctdb->vacuumer = child_ctx;
1461 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1464 * Clear the fastpath vacuuming list in the parent.
1466 talloc_free(ctdb_db->delete_queue);
1467 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1468 if (ctdb_db->delete_queue == NULL) {
1469 DBG_ERR("Out of memory when re-creating vacuum tree\n");
1473 talloc_free(ctdb_db->fetch_queue);
1474 ctdb_db->fetch_queue = trbt_create(ctdb_db, 0);
1475 if (ctdb_db->fetch_queue == NULL) {
1476 ctdb_fatal(ctdb, "Out of memory when re-create fetch queue "
1477 " in parent context. Shutting down\n");
1480 tevent_add_timer(ctdb->ev, child_ctx,
1481 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time,
1483 vacuum_child_timeout, child_ctx);
1485 DBG_DEBUG(" Created PIPE FD:%d to child vacuum process\n",
1488 fde = tevent_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1489 TEVENT_FD_READ, vacuum_child_handler, child_ctx);
1490 tevent_fd_set_auto_close(fde);
1492 child_ctx->vacuum_handle = ctdb_db->vacuum_handle;
1498 static void ctdb_vacuum_event(struct tevent_context *ev,
1499 struct tevent_timer *te,
1500 struct timeval t, void *private_data)
1502 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(
1503 private_data, struct ctdb_vacuum_handle);
1504 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1505 struct ctdb_context *ctdb = ctdb_db->ctdb;
1506 struct ctdb_vacuum_child_context *child_ctx = NULL;
1507 uint32_t fast_path_max = ctdb->tunable.vacuum_fast_path_count;
1508 bool full_vacuum_run = false;
1511 if (vacuum_handle->fast_path_count >= fast_path_max) {
1512 if (fast_path_max > 0) {
1513 full_vacuum_run = true;
1515 vacuum_handle->fast_path_count = 0;
1518 ret = vacuum_db_child(vacuum_handle,
1530 tevent_add_timer(ctdb->ev,
1532 timeval_current_ofs(0, 500*1000),
1538 /* Temporary failure, schedule next attempt */
1539 tevent_add_timer(ctdb->ev,
1541 timeval_current_ofs(
1542 get_vacuum_interval(ctdb_db), 0),
1549 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1551 if (ctdb->vacuumer != NULL) {
1552 D_INFO("Aborting vacuuming for %s (%i)\n",
1553 ctdb->vacuumer->vacuum_handle->ctdb_db->db_name,
1554 (int)ctdb->vacuumer->child_pid);
1555 /* vacuum_child_destructor kills it, removes from list */
1556 talloc_free(ctdb->vacuumer);
1560 /* this function initializes the vacuuming context for a database
1561 * starts the vacuuming events
1563 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1565 if (! ctdb_db_volatile(ctdb_db)) {
1567 ("Vacuuming is disabled for non-volatile database %s\n",
1572 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1573 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1575 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1576 ctdb_db->vacuum_handle->fast_path_count = 0;
1578 tevent_add_timer(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1579 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1580 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1585 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1586 const struct ctdb_ltdb_header *hdr,
1589 struct delete_record_data *kd;
1592 hash = (uint32_t)ctdb_hash(&key);
1594 DEBUG(DEBUG_DEBUG, (__location__
1595 " remove_record_from_delete_queue: "
1600 "migrated_with_data[%s]\n",
1601 ctdb_db->db_name, ctdb_db->db_id,
1603 ctdb_lmaster(ctdb_db->ctdb, &key),
1604 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1606 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1608 DEBUG(DEBUG_DEBUG, (__location__
1609 " remove_record_from_delete_queue: "
1610 "record not in queue (hash[0x%08x])\n.",
1615 if ((kd->key.dsize != key.dsize) ||
1616 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1618 DEBUG(DEBUG_DEBUG, (__location__
1619 " remove_record_from_delete_queue: "
1620 "hash collision for key with hash[0x%08x] "
1621 "in db[%s] - skipping\n",
1622 hash, ctdb_db->db_name));
1626 DEBUG(DEBUG_DEBUG, (__location__
1627 " remove_record_from_delete_queue: "
1628 "removing key with hash[0x%08x]\n",
1637 * Insert a record into the ctdb_db context's delete queue,
1638 * handling hash collisions.
1640 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1641 const struct ctdb_ltdb_header *hdr,
1644 struct delete_record_data *kd;
1648 hash = (uint32_t)ctdb_hash(&key);
1650 DEBUG(DEBUG_DEBUG, (__location__ " schedule for deletion: db[%s] "
1654 "migrated_with_data[%s]\n",
1655 ctdb_db->db_name, ctdb_db->db_id,
1657 ctdb_lmaster(ctdb_db->ctdb, &key),
1658 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1660 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1662 if ((kd->key.dsize != key.dsize) ||
1663 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1666 (__location__ " schedule for deletion: "
1667 "hash collision for key hash [0x%08x]. "
1668 "Skipping the record.\n", hash));
1672 (__location__ " schedule for deletion: "
1673 "updating entry for key with hash [0x%08x].\n",
1678 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1679 ctdb_db->delete_queue,
1683 (__location__ " schedule for deletion: error "
1684 "inserting key with hash [0x%08x] into delete queue\n",
1693 * Schedule a record for deletetion.
1694 * Called from the parent context.
1696 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1699 struct ctdb_control_schedule_for_deletion *dd;
1700 struct ctdb_db_context *ctdb_db;
1704 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1706 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1707 if (ctdb_db == NULL) {
1708 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1713 key.dsize = dd->keylen;
1716 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1721 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1722 const struct ctdb_ltdb_header *hdr,
1726 struct ctdb_control_schedule_for_deletion *dd;
1730 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1731 /* main daemon - directly queue */
1732 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1737 /* if we don't have a connection to the daemon we can not send
1738 a control. For example sometimes from update_record control child
1741 if (!ctdb_db->ctdb->can_send_controls) {
1746 /* child process: send the main daemon a control */
1747 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1748 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1749 if (indata.dptr == NULL) {
1750 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1753 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1754 dd->db_id = ctdb_db->db_id;
1756 dd->keylen = key.dsize;
1757 memcpy(dd->key, key.dptr, key.dsize);
1759 ret = ctdb_control(ctdb_db->ctdb,
1762 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1763 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1768 NULL, /* timeout : NULL == wait forever */
1769 NULL); /* error message */
1771 talloc_free(indata.dptr);
1773 if (ret != 0 || status != 0) {
1774 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1775 "SCHEDULE_FOR_DELETION "
1785 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1786 const struct ctdb_ltdb_header *hdr,
1789 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1791 * Only remove the record from the delete queue if called
1792 * in the main daemon.
1797 remove_record_from_delete_queue(ctdb_db, hdr, key);
1802 static int vacuum_fetch_parser(uint32_t reqid,
1803 struct ctdb_ltdb_header *header,
1804 TDB_DATA key, TDB_DATA data,
1807 struct ctdb_db_context *ctdb_db = talloc_get_type_abort(
1808 private_data, struct ctdb_db_context);
1809 struct fetch_record_data *rd;
1813 len = offsetof(struct fetch_record_data, keydata) + key.dsize;
1815 rd = (struct fetch_record_data *)talloc_size(ctdb_db->fetch_queue,
1818 DEBUG(DEBUG_ERR, (__location__ " Memory error\n"));
1821 talloc_set_name_const(rd, "struct fetch_record_data");
1823 rd->key.dsize = key.dsize;
1824 rd->key.dptr = rd->keydata;
1825 memcpy(rd->keydata, key.dptr, key.dsize);
1827 hash = ctdb_hash(&key);
1829 trbt_insert32(ctdb_db->fetch_queue, hash, rd);
1834 int32_t ctdb_control_vacuum_fetch(struct ctdb_context *ctdb, TDB_DATA indata)
1836 struct ctdb_rec_buffer *recbuf;
1837 struct ctdb_db_context *ctdb_db;
1841 ret = ctdb_rec_buffer_pull(indata.dptr, indata.dsize, ctdb, &recbuf,
1844 DEBUG(DEBUG_ERR, ("Invalid data in vacuum_fetch\n"));
1848 ctdb_db = find_ctdb_db(ctdb, recbuf->db_id);
1849 if (ctdb_db == NULL) {
1850 talloc_free(recbuf);
1851 DEBUG(DEBUG_ERR, (__location__ " Unknown db 0x%08x\n",
1856 ret = ctdb_rec_buffer_traverse(recbuf, vacuum_fetch_parser, ctdb_db);
1857 talloc_free(recbuf);