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"
35 #include "ctdb_private.h"
36 #include "ctdb_client.h"
37 #include "ctdb_logging.h"
39 #include "common/rb_tree.h"
40 #include "common/system.h"
41 #include "common/common.h"
43 #define TIMELIMIT() timeval_current_ofs(10, 0)
45 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
47 struct ctdb_vacuum_child_context {
48 struct ctdb_vacuum_child_context *next, *prev;
49 struct ctdb_vacuum_handle *vacuum_handle;
50 /* fd child writes status to */
53 enum vacuum_child_status status;
54 struct timeval start_time;
57 struct ctdb_vacuum_handle {
58 struct ctdb_db_context *ctdb_db;
59 struct ctdb_vacuum_child_context *child_ctx;
60 uint32_t fast_path_count;
64 /* a list of records to possibly delete */
66 struct ctdb_context *ctdb;
67 struct ctdb_db_context *ctdb_db;
68 struct tdb_context *dest_db;
69 trbt_tree_t *delete_list;
70 struct ctdb_marshall_buffer **vacuum_fetch_list;
76 uint32_t added_to_vacuum_fetch_list;
77 uint32_t added_to_delete_list;
91 uint32_t remote_error;
104 /* this structure contains the information for one record to be deleted */
105 struct delete_record_data {
106 struct ctdb_context *ctdb;
107 struct ctdb_db_context *ctdb_db;
108 struct ctdb_ltdb_header hdr;
113 struct delete_records_list {
114 struct ctdb_marshall_buffer *records;
115 struct vacuum_data *vdata;
118 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
119 const struct ctdb_ltdb_header *hdr,
123 * Store key and header in a tree, indexed by the key hash.
125 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
126 struct ctdb_db_context *ctdb_db,
128 const struct ctdb_ltdb_header *hdr,
131 struct delete_record_data *dd;
135 len = offsetof(struct delete_record_data, keydata) + key.dsize;
137 dd = (struct delete_record_data *)talloc_size(tree, len);
139 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
142 talloc_set_name_const(dd, "struct delete_record_data");
145 dd->ctdb_db = ctdb_db;
146 dd->key.dsize = key.dsize;
147 dd->key.dptr = dd->keydata;
148 memcpy(dd->keydata, key.dptr, key.dsize);
152 hash = ctdb_hash(&key);
154 trbt_insert32(tree, hash, dd);
159 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
160 struct ctdb_ltdb_header *hdr)
162 struct ctdb_context *ctdb = vdata->ctdb;
163 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
167 hash = ctdb_hash(&key);
169 if (trbt_lookup32(vdata->delete_list, hash)) {
170 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
174 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
181 vdata->count.delete_list.total++;
187 * Add a record to the list of records to be sent
188 * to their lmaster with VACUUM_FETCH.
190 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
193 struct ctdb_context *ctdb = vdata->ctdb;
195 struct ctdb_marshall_buffer *vfl;
197 lmaster = ctdb_lmaster(ctdb, &key);
199 vfl = vdata->vacuum_fetch_list[lmaster];
201 vfl = ctdb_marshall_add(ctdb, vfl, vfl->db_id, ctdb->pnn,
202 key, NULL, tdb_null);
204 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
205 vdata->traverse_error = true;
209 vdata->vacuum_fetch_list[lmaster] = vfl;
215 static void ctdb_vacuum_event(struct tevent_context *ev,
216 struct tevent_timer *te,
217 struct timeval t, void *private_data);
219 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
221 struct ctdb_ltdb_header *header =
222 (struct ctdb_ltdb_header *)private_data;
224 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
228 *header = *(struct ctdb_ltdb_header *)data.dptr;
234 * traverse function for gathering the records that can be deleted
236 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
239 struct vacuum_data *vdata = talloc_get_type(private_data,
241 struct ctdb_context *ctdb = vdata->ctdb;
242 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
244 struct ctdb_ltdb_header *hdr;
247 vdata->count.db_traverse.total++;
249 lmaster = ctdb_lmaster(ctdb, &key);
250 if (lmaster >= ctdb->num_nodes) {
251 vdata->count.db_traverse.error++;
252 DEBUG(DEBUG_CRIT, (__location__
253 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
256 (unsigned)ctdb->num_nodes,
257 (unsigned)ctdb_hash(&key)));
261 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
262 /* it is not a deleted record */
263 vdata->count.db_traverse.skipped++;
267 hdr = (struct ctdb_ltdb_header *)data.dptr;
269 if (hdr->dmaster != ctdb->pnn) {
270 vdata->count.db_traverse.skipped++;
275 * Add the record to this process's delete_queue for processing
276 * in the subsequent traverse in the fast vacuum run.
278 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
280 vdata->count.db_traverse.error++;
282 vdata->count.db_traverse.scheduled++;
289 * traverse the tree of records to delete and marshall them into
292 static int delete_marshall_traverse(void *param, void *data)
294 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
295 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
296 struct ctdb_marshall_buffer *m;
298 m = ctdb_marshall_add(recs, recs->records, recs->records->db_id,
299 recs->records->db_id,
300 dd->key, &dd->hdr, tdb_null);
302 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
311 * Variant of delete_marshall_traverse() that bumps the
312 * RSN of each traversed record in the database.
314 * This is needed to ensure that when rolling out our
315 * empty record copy before remote deletion, we as the
316 * record's dmaster keep a higher RSN than the non-dmaster
317 * nodes. This is needed to prevent old copies from
318 * resurrection in recoveries.
320 static int delete_marshall_traverse_first(void *param, void *data)
322 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
323 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
324 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
325 struct ctdb_context *ctdb = ctdb_db->ctdb;
326 struct ctdb_ltdb_header header;
328 uint32_t hash = ctdb_hash(&(dd->key));
331 res = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, dd->key);
333 recs->vdata->count.delete_list.skipped++;
334 recs->vdata->count.delete_list.left--;
340 * Verify that the record is still empty, its RSN has not
341 * changed and that we are still its lmaster and dmaster.
344 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
345 vacuum_record_parser, &header);
350 if (header.flags & CTDB_REC_RO_FLAGS) {
351 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
352 "on database db[%s] has read-only flags. "
354 hash, ctdb_db->db_name));
358 if (header.dmaster != ctdb->pnn) {
359 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
360 "on database db[%s] has been migrated away. "
362 hash, ctdb_db->db_name));
366 if (header.rsn != dd->hdr.rsn) {
367 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
368 "on database db[%s] seems to have been "
369 "migrated away and back again (with empty "
370 "data). skipping.\n",
371 hash, ctdb_db->db_name));
375 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
377 if (lmaster != ctdb->pnn) {
378 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
379 "delete list (key hash [0x%08x], db[%s]). "
380 "Strange! skipping.\n",
381 hash, ctdb_db->db_name));
386 * Increment the record's RSN to ensure the dmaster (i.e. the current
387 * node) has the highest RSN of the record in the cluster.
388 * This is to prevent old record copies from resurrecting in recoveries
389 * if something should fail during the deletion process.
390 * Note that ctdb_ltdb_store_server() increments the RSN if called
391 * on the record's dmaster.
394 res = ctdb_ltdb_store(ctdb_db, dd->key, &header, tdb_null);
396 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
397 "key hash [0x%08x] on database db[%s].\n",
398 hash, ctdb_db->db_name));
402 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
407 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
409 recs->vdata->count.delete_list.skipped++;
410 recs->vdata->count.delete_list.left--;
419 return delete_marshall_traverse(param, data);
423 * traverse function for the traversal of the delete_queue,
424 * the fast-path vacuuming list.
426 * - If the record has been migrated off the node
427 * or has been revived (filled with data) on the node,
428 * then skip the record.
430 * - If the current node is the record's lmaster and it is
431 * a record that has never been migrated with data, then
432 * delete the record from the local tdb.
434 * - If the current node is the record's lmaster and it has
435 * been migrated with data, then schedule it for the normal
436 * vacuuming procedure (i.e. add it to the delete_list).
438 * - If the current node is NOT the record's lmaster then
439 * add it to the list of records that are to be sent to
440 * the lmaster with the VACUUM_FETCH message.
442 static int delete_queue_traverse(void *param, void *data)
444 struct delete_record_data *dd =
445 talloc_get_type(data, struct delete_record_data);
446 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
447 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
448 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
450 struct ctdb_ltdb_header header;
452 uint32_t hash = ctdb_hash(&(dd->key));
454 vdata->count.delete_queue.total++;
456 res = tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, dd->key);
458 vdata->count.delete_queue.error++;
462 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
463 vacuum_record_parser, &header);
468 if (header.dmaster != ctdb->pnn) {
469 /* The record has been migrated off the node. Skip. */
473 if (header.rsn != dd->hdr.rsn) {
475 * The record has been migrated off the node and back again.
476 * But not requeued for deletion. Skip it.
482 * We are dmaster, and the record has no data, and it has
483 * not been migrated after it has been queued for deletion.
485 * At this stage, the record could still have been revived locally
486 * and last been written with empty data. This can only be
487 * fixed with the addition of an active or delete flag. (TODO)
490 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
492 if (lmaster != ctdb->pnn) {
493 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
497 (__location__ " Error adding record to list "
498 "of records to send to lmaster.\n"));
499 vdata->count.delete_queue.error++;
501 vdata->count.delete_queue.added_to_vacuum_fetch_list++;
506 /* use header->flags or dd->hdr.flags ?? */
507 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
508 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
512 (__location__ " Error adding record to list "
513 "of records for deletion on lmaster.\n"));
514 vdata->count.delete_queue.error++;
516 vdata->count.delete_queue.added_to_delete_list++;
519 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
523 (__location__ " Error deleting record with key "
524 "hash [0x%08x] from local data base db[%s].\n",
525 hash, ctdb_db->db_name));
526 vdata->count.delete_queue.error++;
531 (__location__ " Deleted record with key hash "
532 "[0x%08x] from local data base db[%s].\n",
533 hash, ctdb_db->db_name));
534 vdata->count.delete_queue.deleted++;
540 vdata->count.delete_queue.skipped++;
543 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
549 * Delete the records that we are lmaster and dmaster for and
550 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
553 static int delete_record_traverse(void *param, void *data)
555 struct delete_record_data *dd =
556 talloc_get_type(data, struct delete_record_data);
557 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
558 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
559 struct ctdb_context *ctdb = ctdb_db->ctdb;
561 struct ctdb_ltdb_header header;
563 uint32_t hash = ctdb_hash(&(dd->key));
565 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
568 (__location__ " Error getting chainlock on record with "
569 "key hash [0x%08x] on database db[%s].\n",
570 hash, ctdb_db->db_name));
571 vdata->count.delete_list.local_error++;
572 vdata->count.delete_list.left--;
578 * Verify that the record is still empty, its RSN has not
579 * changed and that we are still its lmaster and dmaster.
582 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
583 vacuum_record_parser, &header);
588 if (header.flags & CTDB_REC_RO_FLAGS) {
589 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
590 "on database db[%s] has read-only flags. "
592 hash, ctdb_db->db_name));
596 if (header.dmaster != ctdb->pnn) {
597 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
598 "on database db[%s] has been migrated away. "
600 hash, ctdb_db->db_name));
604 if (header.rsn != dd->hdr.rsn + 1) {
606 * The record has been migrated off the node and back again.
607 * But not requeued for deletion. Skip it.
608 * (Note that the first marshall traverse has bumped the RSN
611 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
612 "on database db[%s] seems to have been "
613 "migrated away and back again (with empty "
614 "data). skipping.\n",
615 hash, ctdb_db->db_name));
619 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
621 if (lmaster != ctdb->pnn) {
622 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
623 "delete list (key hash [0x%08x], db[%s]). "
624 "Strange! skipping.\n",
625 hash, ctdb_db->db_name));
629 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
633 (__location__ " Error deleting record with key hash "
634 "[0x%08x] from local data base db[%s].\n",
635 hash, ctdb_db->db_name));
636 vdata->count.delete_list.local_error++;
641 (__location__ " Deleted record with key hash [0x%08x] from "
642 "local data base db[%s].\n", hash, ctdb_db->db_name));
644 vdata->count.delete_list.deleted++;
648 vdata->count.delete_list.skipped++;
651 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
654 vdata->count.delete_list.left--;
660 * Traverse the delete_queue.
661 * Records are either deleted directly or filled
662 * into the delete list or the vacuum fetch lists
663 * for further processing.
665 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
666 struct vacuum_data *vdata)
671 ret = trbt_traversearray32(ctdb_db->delete_queue, 1,
672 delete_queue_traverse, vdata);
675 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
676 "the delete queue.\n"));
679 sum = vdata->count.delete_queue.deleted
680 + vdata->count.delete_queue.skipped
681 + vdata->count.delete_queue.error
682 + vdata->count.delete_queue.added_to_delete_list
683 + vdata->count.delete_queue.added_to_vacuum_fetch_list;
685 if (vdata->count.delete_queue.total != sum) {
686 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
687 "counts for db[%s]: total[%u] != sum[%u]\n",
689 (unsigned)vdata->count.delete_queue.total,
693 if (vdata->count.delete_queue.total > 0) {
696 " fast vacuuming delete_queue traverse statistics: "
705 (unsigned)vdata->count.delete_queue.total,
706 (unsigned)vdata->count.delete_queue.deleted,
707 (unsigned)vdata->count.delete_queue.skipped,
708 (unsigned)vdata->count.delete_queue.error,
709 (unsigned)vdata->count.delete_queue.added_to_delete_list,
710 (unsigned)vdata->count.delete_queue.added_to_vacuum_fetch_list));
717 * read-only traverse of the database, looking for records that
718 * might be able to be vacuumed.
720 * This is not done each time but only every tunable
721 * VacuumFastPathCount times.
723 static void ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
724 struct vacuum_data *vdata)
728 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
729 if (ret == -1 || vdata->traverse_error) {
730 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
731 "'%s'\n", ctdb_db->db_name));
735 if (vdata->count.db_traverse.total > 0) {
738 " full vacuuming db traverse statistics: "
745 (unsigned)vdata->count.db_traverse.total,
746 (unsigned)vdata->count.db_traverse.skipped,
747 (unsigned)vdata->count.db_traverse.error,
748 (unsigned)vdata->count.db_traverse.scheduled));
755 * Process the vacuum fetch lists:
756 * For records for which we are not the lmaster, tell the lmaster to
759 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
760 struct vacuum_data *vdata)
763 struct ctdb_context *ctdb = ctdb_db->ctdb;
765 for (i = 0; i < ctdb->num_nodes; i++) {
767 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
769 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
773 if (vfl->count == 0) {
777 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
778 vfl->count, ctdb->nodes[i]->pnn,
781 data = ctdb_marshall_finish(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));
796 * Process the delete list:
798 * This is the last step of vacuuming that consistently deletes
799 * those records that have been migrated with data and can hence
800 * not be deleted when leaving a node.
802 * In this step, the lmaster does the final deletion of those empty
803 * records that it is also dmaster for. It has ususally received
804 * at least some of these records previously from the former dmasters
805 * with the vacuum fetch message.
807 * This last step is implemented as a 3-phase process to protect from
808 * races leading to data corruption:
810 * 1) Send the lmaster's copy to all other active nodes with the
811 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
812 * 2) Send the records that could successfully be stored remotely
813 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
814 * control. The remote notes delete their local copy.
815 * 3) The lmaster locally deletes its copies of all records that
816 * could successfully be deleted remotely in step #2.
818 static void ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
819 struct vacuum_data *vdata)
822 struct ctdb_context *ctdb = ctdb_db->ctdb;
823 struct delete_records_list *recs;
825 struct ctdb_node_map_old *nodemap;
826 uint32_t *active_nodes;
827 int num_active_nodes;
831 if (vdata->count.delete_list.total == 0) {
835 tmp_ctx = talloc_new(vdata);
836 if (tmp_ctx == NULL) {
837 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
841 vdata->count.delete_list.left = vdata->count.delete_list.total;
844 * get the list of currently active nodes
847 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
852 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
856 active_nodes = list_of_active_nodes(ctdb, nodemap,
857 nodemap, /* talloc context */
858 false /* include self */);
860 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
863 * Now delete the records all active nodes in a three-phase process:
864 * 1) send all active remote nodes the current empty copy with this
866 * 2) if all nodes could store the new copy,
867 * tell all the active remote nodes to delete all their copy
868 * 3) if all remote nodes deleted their record copy, delete it locally
873 * Send currently empty record copy to all active nodes for storing.
876 recs = talloc_zero(tmp_ctx, struct delete_records_list);
878 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
881 recs->records = (struct ctdb_marshall_buffer *)
882 talloc_zero_size(recs,
883 offsetof(struct ctdb_marshall_buffer, data));
884 if (recs->records == NULL) {
885 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
888 recs->records->db_id = ctdb_db->db_id;
892 * traverse the tree of all records we want to delete and
893 * create a blob we can send to the other nodes.
895 * We call delete_marshall_traverse_first() to bump the
896 * records' RSNs in the database, to ensure we (as dmaster)
897 * keep the highest RSN of the records in the cluster.
899 ret = trbt_traversearray32(vdata->delete_list, 1,
900 delete_marshall_traverse_first, recs);
902 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
903 "delete list for first marshalling.\n"));
907 indata = ctdb_marshall_finish(recs->records);
909 for (i = 0; i < num_active_nodes; i++) {
910 struct ctdb_marshall_buffer *records;
911 struct ctdb_rec_data_old *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));
927 * outdata contains the list of records coming back
928 * from the node: These are the records that the
929 * remote node could not store. We remove these from
930 * the list to process further.
932 records = (struct ctdb_marshall_buffer *)outdata.dptr;
933 rec = (struct ctdb_rec_data_old *)&records->data[0];
934 while (records->count-- > 1) {
935 TDB_DATA reckey, recdata;
936 struct ctdb_ltdb_header *rechdr;
937 struct delete_record_data *dd;
939 reckey.dptr = &rec->data[0];
940 reckey.dsize = rec->keylen;
941 recdata.dptr = &rec->data[reckey.dsize];
942 recdata.dsize = rec->datalen;
944 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
945 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
948 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
949 recdata.dptr += sizeof(*rechdr);
950 recdata.dsize -= sizeof(*rechdr);
952 dd = (struct delete_record_data *)trbt_lookup32(
957 * The other node could not store the record
958 * copy and it is the first node that failed.
959 * So we should remove it from the tree and
963 vdata->count.delete_list.remote_error++;
964 vdata->count.delete_list.left--;
966 DEBUG(DEBUG_ERR, (__location__ " Failed to "
967 "find record with hash 0x%08x coming "
968 "back from RECEIVE_RECORDS "
969 "control in delete list.\n",
970 ctdb_hash(&reckey)));
971 vdata->count.delete_list.local_error++;
972 vdata->count.delete_list.left--;
975 rec = (struct ctdb_rec_data_old *)(rec->length + (uint8_t *)rec);
979 if (vdata->count.delete_list.left == 0) {
985 * Send the remaining records to all active nodes for deletion.
987 * The lmaster's (i.e. our) copies of these records have been stored
988 * successfully on the other nodes.
992 * Create a marshall blob from the remaining list of records to delete.
995 talloc_free(recs->records);
997 recs->records = (struct ctdb_marshall_buffer *)
998 talloc_zero_size(recs,
999 offsetof(struct ctdb_marshall_buffer, data));
1000 if (recs->records == NULL) {
1001 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1004 recs->records->db_id = ctdb_db->db_id;
1006 ret = trbt_traversearray32(vdata->delete_list, 1,
1007 delete_marshall_traverse, recs);
1009 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1010 "delete list for second marshalling.\n"));
1014 indata = ctdb_marshall_finish(recs->records);
1016 for (i = 0; i < num_active_nodes; i++) {
1017 struct ctdb_marshall_buffer *records;
1018 struct ctdb_rec_data_old *rec;
1022 ret = ctdb_control(ctdb, active_nodes[i], 0,
1023 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1024 indata, recs, &outdata, &res,
1026 if (ret != 0 || res != 0) {
1027 DEBUG(DEBUG_ERR, ("Failed to delete records on "
1028 "node %u: ret[%d] res[%d]\n",
1029 active_nodes[i], ret, res));
1034 * outdata contains the list of records coming back
1035 * from the node: These are the records that the
1036 * remote node could not delete. We remove these from
1037 * the list to delete locally.
1039 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1040 rec = (struct ctdb_rec_data_old *)&records->data[0];
1041 while (records->count-- > 1) {
1042 TDB_DATA reckey, recdata;
1043 struct ctdb_ltdb_header *rechdr;
1044 struct delete_record_data *dd;
1046 reckey.dptr = &rec->data[0];
1047 reckey.dsize = rec->keylen;
1048 recdata.dptr = &rec->data[reckey.dsize];
1049 recdata.dsize = rec->datalen;
1051 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1052 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1055 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1056 recdata.dptr += sizeof(*rechdr);
1057 recdata.dsize -= sizeof(*rechdr);
1059 dd = (struct delete_record_data *)trbt_lookup32(
1061 ctdb_hash(&reckey));
1064 * The other node could not delete the
1065 * record and it is the first node that
1066 * failed. So we should remove it from
1067 * the tree and update statistics.
1070 vdata->count.delete_list.remote_error++;
1071 vdata->count.delete_list.left--;
1073 DEBUG(DEBUG_ERR, (__location__ " Failed to "
1074 "find record with hash 0x%08x coming "
1075 "back from TRY_DELETE_RECORDS "
1076 "control in delete list.\n",
1077 ctdb_hash(&reckey)));
1078 vdata->count.delete_list.local_error++;
1079 vdata->count.delete_list.left--;
1082 rec = (struct ctdb_rec_data_old *)(rec->length + (uint8_t *)rec);
1086 if (vdata->count.delete_list.left == 0) {
1092 * Delete the remaining records locally.
1094 * These records have successfully been deleted on all
1095 * active remote nodes.
1098 ret = trbt_traversearray32(vdata->delete_list, 1,
1099 delete_record_traverse, vdata);
1101 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1102 "delete list for deletion.\n"));
1107 if (vdata->count.delete_list.left != 0) {
1108 DEBUG(DEBUG_ERR, (__location__ " Vaccum db[%s] error: "
1109 "there are %u records left for deletion after "
1110 "processing delete list\n",
1112 (unsigned)vdata->count.delete_list.left));
1115 sum = vdata->count.delete_list.deleted
1116 + vdata->count.delete_list.skipped
1117 + vdata->count.delete_list.remote_error
1118 + vdata->count.delete_list.local_error
1119 + vdata->count.delete_list.left;
1121 if (vdata->count.delete_list.total != sum) {
1122 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1123 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1125 (unsigned)vdata->count.delete_list.total,
1129 if (vdata->count.delete_list.total > 0) {
1132 " vacuum delete list statistics: "
1141 (unsigned)vdata->count.delete_list.total,
1142 (unsigned)vdata->count.delete_list.deleted,
1143 (unsigned)vdata->count.delete_list.skipped,
1144 (unsigned)vdata->count.delete_list.remote_error,
1145 (unsigned)vdata->count.delete_list.local_error,
1146 (unsigned)vdata->count.delete_list.left));
1150 talloc_free(tmp_ctx);
1156 * initialize the vacuum_data
1158 static struct vacuum_data *ctdb_vacuum_init_vacuum_data(
1159 struct ctdb_db_context *ctdb_db,
1160 TALLOC_CTX *mem_ctx)
1163 struct ctdb_context *ctdb = ctdb_db->ctdb;
1164 struct vacuum_data *vdata;
1166 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1167 if (vdata == NULL) {
1168 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1172 vdata->ctdb = ctdb_db->ctdb;
1173 vdata->ctdb_db = ctdb_db;
1174 vdata->delete_list = trbt_create(vdata, 0);
1175 if (vdata->delete_list == NULL) {
1176 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1180 vdata->start = timeval_current();
1182 vdata->count.delete_queue.added_to_delete_list = 0;
1183 vdata->count.delete_queue.added_to_vacuum_fetch_list = 0;
1184 vdata->count.delete_queue.deleted = 0;
1185 vdata->count.delete_queue.skipped = 0;
1186 vdata->count.delete_queue.error = 0;
1187 vdata->count.delete_queue.total = 0;
1188 vdata->count.db_traverse.scheduled = 0;
1189 vdata->count.db_traverse.skipped = 0;
1190 vdata->count.db_traverse.error = 0;
1191 vdata->count.db_traverse.total = 0;
1192 vdata->count.delete_list.total = 0;
1193 vdata->count.delete_list.left = 0;
1194 vdata->count.delete_list.remote_error = 0;
1195 vdata->count.delete_list.local_error = 0;
1196 vdata->count.delete_list.skipped = 0;
1197 vdata->count.delete_list.deleted = 0;
1199 /* the list needs to be of length num_nodes */
1200 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1201 struct ctdb_marshall_buffer *,
1203 if (vdata->vacuum_fetch_list == NULL) {
1204 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1207 for (i = 0; i < ctdb->num_nodes; i++) {
1208 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1209 talloc_zero_size(vdata->vacuum_fetch_list,
1210 offsetof(struct ctdb_marshall_buffer, data));
1211 if (vdata->vacuum_fetch_list[i] == NULL) {
1212 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1216 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1228 * - Always do the fast vacuuming run, which traverses
1229 * the in-memory delete queue: these records have been
1230 * scheduled for deletion.
1231 * - Only if explicitly requested, the database is traversed
1232 * in order to use the traditional heuristics on empty records
1233 * to trigger deletion.
1234 * This is done only every VacuumFastPathCount'th vacuuming run.
1236 * The traverse runs fill two lists:
1238 * - The delete_list:
1239 * This is the list of empty records the current
1240 * node is lmaster and dmaster for. These records are later
1241 * deleted first on other nodes and then locally.
1243 * The fast vacuuming run has a short cut for those records
1244 * that have never been migrated with data: these records
1245 * are immediately deleted locally, since they have left
1246 * no trace on other nodes.
1248 * - The vacuum_fetch lists
1249 * (one for each other lmaster node):
1250 * The records in this list are sent for deletion to
1251 * their lmaster in a bulk VACUUM_FETCH message.
1253 * The lmaster then migrates all these records to itelf
1254 * so that they can be vacuumed there.
1256 * This executes in the child context.
1258 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1259 bool full_vacuum_run)
1261 struct ctdb_context *ctdb = ctdb_db->ctdb;
1263 struct vacuum_data *vdata;
1264 TALLOC_CTX *tmp_ctx;
1266 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1267 "%s db_id[0x%08x]\n",
1268 full_vacuum_run ? "full" : "fast",
1269 ctdb_db->db_name, ctdb_db->db_id));
1271 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1273 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1277 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1279 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1285 tmp_ctx = talloc_new(ctdb_db);
1286 if (tmp_ctx == NULL) {
1287 DEBUG(DEBUG_ERR, ("Out of memory!\n"));
1291 vdata = ctdb_vacuum_init_vacuum_data(ctdb_db, tmp_ctx);
1292 if (vdata == NULL) {
1293 talloc_free(tmp_ctx);
1297 if (full_vacuum_run) {
1298 ctdb_vacuum_traverse_db(ctdb_db, vdata);
1301 ctdb_process_delete_queue(ctdb_db, vdata);
1303 ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1305 ctdb_process_delete_list(ctdb_db, vdata);
1307 talloc_free(tmp_ctx);
1309 /* this ensures we run our event queue */
1310 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1316 * repack and vaccum a db
1317 * called from the child context
1319 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1320 bool full_vacuum_run)
1322 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1323 const char *name = ctdb_db->db_name;
1324 int freelist_size = 0;
1327 if (ctdb_vacuum_db(ctdb_db, full_vacuum_run) != 0) {
1328 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1331 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1332 if (freelist_size == -1) {
1333 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1338 * decide if a repack is necessary
1340 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1345 DEBUG(DEBUG_INFO, ("Repacking %s with %u freelist entries\n",
1346 name, freelist_size));
1348 ret = tdb_repack(ctdb_db->ltdb->tdb);
1350 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1357 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1359 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1364 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1366 double l = timeval_elapsed(&child_ctx->start_time);
1367 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1368 struct ctdb_context *ctdb = ctdb_db->ctdb;
1370 CTDB_UPDATE_DB_LATENCY(ctdb_db, "vacuum", vacuum.latency, l);
1371 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1373 if (child_ctx->child_pid != -1) {
1374 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1376 /* Bump the number of successful fast-path runs. */
1377 child_ctx->vacuum_handle->fast_path_count++;
1380 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1382 tevent_add_timer(ctdb->ev, child_ctx->vacuum_handle,
1383 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1384 ctdb_vacuum_event, child_ctx->vacuum_handle);
1390 * this event is generated when a vacuum child process times out
1392 static void vacuum_child_timeout(struct tevent_context *ev,
1393 struct tevent_timer *te,
1394 struct timeval t, void *private_data)
1396 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1398 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1400 child_ctx->status = VACUUM_TIMEOUT;
1402 talloc_free(child_ctx);
1407 * this event is generated when a vacuum child process has completed
1409 static void vacuum_child_handler(struct tevent_context *ev,
1410 struct tevent_fd *fde,
1411 uint16_t flags, void *private_data)
1413 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1417 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1418 child_ctx->child_pid = -1;
1420 ret = sys_read(child_ctx->fd[0], &c, 1);
1421 if (ret != 1 || c != 0) {
1422 child_ctx->status = VACUUM_ERROR;
1423 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));
1425 child_ctx->status = VACUUM_OK;
1428 talloc_free(child_ctx);
1432 * this event is called every time we need to start a new vacuum process
1434 static void ctdb_vacuum_event(struct tevent_context *ev,
1435 struct tevent_timer *te,
1436 struct timeval t, void *private_data)
1438 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1439 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1440 struct ctdb_context *ctdb = ctdb_db->ctdb;
1441 struct ctdb_vacuum_child_context *child_ctx;
1442 struct tevent_fd *fde;
1445 /* we dont vacuum if we are in recovery mode, or db frozen */
1446 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1447 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1448 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1449 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1450 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1453 tevent_add_timer(ctdb->ev, vacuum_handle,
1454 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1455 ctdb_vacuum_event, vacuum_handle);
1459 /* Do not allow multiple vacuuming child processes to be active at the
1460 * same time. If there is vacuuming child process active, delay
1461 * new vacuuming event to stagger vacuuming events.
1463 if (ctdb->vacuumers != NULL) {
1464 tevent_add_timer(ctdb->ev, vacuum_handle,
1465 timeval_current_ofs(0, 500*1000),
1466 ctdb_vacuum_event, vacuum_handle);
1470 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1471 if (child_ctx == NULL) {
1472 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1473 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1477 ret = pipe(child_ctx->fd);
1479 talloc_free(child_ctx);
1480 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1481 tevent_add_timer(ctdb->ev, vacuum_handle,
1482 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1483 ctdb_vacuum_event, vacuum_handle);
1487 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1488 vacuum_handle->fast_path_count = 0;
1491 child_ctx->child_pid = ctdb_fork(ctdb);
1492 if (child_ctx->child_pid == (pid_t)-1) {
1493 close(child_ctx->fd[0]);
1494 close(child_ctx->fd[1]);
1495 talloc_free(child_ctx);
1496 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1497 tevent_add_timer(ctdb->ev, vacuum_handle,
1498 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1499 ctdb_vacuum_event, vacuum_handle);
1504 if (child_ctx->child_pid == 0) {
1506 bool full_vacuum_run = false;
1507 close(child_ctx->fd[0]);
1509 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1510 ctdb_set_process_name("ctdb_vacuum");
1511 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1512 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1516 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1517 (vacuum_handle->fast_path_count == 0))
1519 full_vacuum_run = true;
1521 cc = ctdb_vacuum_and_repack_db(ctdb_db, full_vacuum_run);
1523 sys_write(child_ctx->fd[1], &cc, 1);
1527 set_close_on_exec(child_ctx->fd[0]);
1528 close(child_ctx->fd[1]);
1530 child_ctx->status = VACUUM_RUNNING;
1531 child_ctx->start_time = timeval_current();
1533 DLIST_ADD(ctdb->vacuumers, child_ctx);
1534 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1537 * Clear the fastpath vacuuming list in the parent.
1539 talloc_free(ctdb_db->delete_queue);
1540 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1541 if (ctdb_db->delete_queue == NULL) {
1542 /* fatal here? ... */
1543 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1544 "in parent context. Shutting down\n");
1547 tevent_add_timer(ctdb->ev, child_ctx,
1548 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1549 vacuum_child_timeout, child_ctx);
1551 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1553 fde = tevent_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1554 TEVENT_FD_READ, vacuum_child_handler, child_ctx);
1555 tevent_fd_set_auto_close(fde);
1557 vacuum_handle->child_ctx = child_ctx;
1558 child_ctx->vacuum_handle = vacuum_handle;
1561 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1563 /* Simply free them all. */
1564 while (ctdb->vacuumers) {
1565 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1566 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1567 (int)ctdb->vacuumers->child_pid));
1568 /* vacuum_child_destructor kills it, removes from list */
1569 talloc_free(ctdb->vacuumers);
1573 /* this function initializes the vacuuming context for a database
1574 * starts the vacuuming events
1576 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1578 if (ctdb_db->persistent != 0) {
1579 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1583 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1584 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1586 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1587 ctdb_db->vacuum_handle->fast_path_count = 0;
1589 tevent_add_timer(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1590 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1591 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1596 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1597 const struct ctdb_ltdb_header *hdr,
1600 struct delete_record_data *kd;
1603 hash = (uint32_t)ctdb_hash(&key);
1605 DEBUG(DEBUG_DEBUG, (__location__
1606 " remove_record_from_delete_queue: "
1611 "migrated_with_data[%s]\n",
1612 ctdb_db->db_name, ctdb_db->db_id,
1614 ctdb_lmaster(ctdb_db->ctdb, &key),
1615 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1617 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1619 DEBUG(DEBUG_DEBUG, (__location__
1620 " remove_record_from_delete_queue: "
1621 "record not in queue (hash[0x%08x])\n.",
1626 if ((kd->key.dsize != key.dsize) ||
1627 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1629 DEBUG(DEBUG_DEBUG, (__location__
1630 " remove_record_from_delete_queue: "
1631 "hash collision for key with hash[0x%08x] "
1632 "in db[%s] - skipping\n",
1633 hash, ctdb_db->db_name));
1637 DEBUG(DEBUG_DEBUG, (__location__
1638 " remove_record_from_delete_queue: "
1639 "removing key with hash[0x%08x]\n",
1648 * Insert a record into the ctdb_db context's delete queue,
1649 * handling hash collisions.
1651 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1652 const struct ctdb_ltdb_header *hdr,
1655 struct delete_record_data *kd;
1659 hash = (uint32_t)ctdb_hash(&key);
1661 DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1665 "migrated_with_data[%s]\n",
1666 ctdb_db->db_name, ctdb_db->db_id,
1668 ctdb_lmaster(ctdb_db->ctdb, &key),
1669 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1671 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1673 if ((kd->key.dsize != key.dsize) ||
1674 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1677 (__location__ " schedule for deletion: "
1678 "hash collision for key hash [0x%08x]. "
1679 "Skipping the record.\n", hash));
1683 (__location__ " schedule for deletion: "
1684 "updating entry for key with hash [0x%08x].\n",
1689 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1690 ctdb_db->delete_queue,
1694 (__location__ " schedule for deletion: error "
1695 "inserting key with hash [0x%08x] into delete queue\n",
1704 * Schedule a record for deletetion.
1705 * Called from the parent context.
1707 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1710 struct ctdb_control_schedule_for_deletion *dd;
1711 struct ctdb_db_context *ctdb_db;
1715 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1717 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1718 if (ctdb_db == NULL) {
1719 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1724 key.dsize = dd->keylen;
1727 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1732 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1733 const struct ctdb_ltdb_header *hdr,
1737 struct ctdb_control_schedule_for_deletion *dd;
1741 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1742 /* main daemon - directly queue */
1743 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1748 /* if we dont have a connection to the daemon we can not send
1749 a control. For example sometimes from update_record control child
1752 if (!ctdb_db->ctdb->can_send_controls) {
1757 /* child process: send the main daemon a control */
1758 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1759 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1760 if (indata.dptr == NULL) {
1761 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1764 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1765 dd->db_id = ctdb_db->db_id;
1767 dd->keylen = key.dsize;
1768 memcpy(dd->key, key.dptr, key.dsize);
1770 ret = ctdb_control(ctdb_db->ctdb,
1773 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1774 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1779 NULL, /* timeout : NULL == wait forever */
1780 NULL); /* error message */
1782 talloc_free(indata.dptr);
1784 if (ret != 0 || status != 0) {
1785 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1786 "SCHEDULE_FOR_DELETION "
1796 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1797 const struct ctdb_ltdb_header *hdr,
1800 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1802 * Only remove the record from the delete queue if called
1803 * in the main daemon.
1808 remove_record_from_delete_queue(ctdb_db, hdr, key);