ctdb-vacuum: change full db traverse vacuuming to fill delete queue
[Samba.git] / ctdb / server / ctdb_vacuum.c
blob0f9b62bef39d0fa25d31c220f188e20fb02ca0b6
1 /*
2 ctdb vacuuming events
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/>.
22 #include "includes.h"
23 #include "tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.h"
28 #include "db_wrap.h"
29 #include "lib/util/dlinklist.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
35 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
37 struct ctdb_vacuum_child_context {
38 struct ctdb_vacuum_child_context *next, *prev;
39 struct ctdb_vacuum_handle *vacuum_handle;
40 /* fd child writes status to */
41 int fd[2];
42 pid_t child_pid;
43 enum vacuum_child_status status;
44 struct timeval start_time;
47 struct ctdb_vacuum_handle {
48 struct ctdb_db_context *ctdb_db;
49 struct ctdb_vacuum_child_context *child_ctx;
50 uint32_t fast_path_count;
54 /* a list of records to possibly delete */
55 struct vacuum_data {
56 uint32_t vacuum_limit;
57 uint32_t repack_limit;
58 struct ctdb_context *ctdb;
59 struct ctdb_db_context *ctdb_db;
60 struct tdb_context *dest_db;
61 trbt_tree_t *delete_list;
62 uint32_t delete_count;
63 struct ctdb_marshall_buffer **vacuum_fetch_list;
64 struct timeval start;
65 bool traverse_error;
66 bool vacuum;
67 uint32_t total;
68 uint32_t vacuumed;
69 uint32_t copied;
70 uint32_t fast_added_to_vacuum_fetch_list;
71 uint32_t fast_added_to_delete_list;
72 uint32_t fast_deleted;
73 uint32_t fast_skipped;
74 uint32_t fast_error;
75 uint32_t fast_total;
76 uint32_t full_scheduled;
77 uint32_t full_skipped;
78 uint32_t full_error;
79 uint32_t full_total;
80 uint32_t delete_left;
81 uint32_t delete_remote_error;
82 uint32_t delete_local_error;
83 uint32_t delete_deleted;
84 uint32_t delete_skipped;
87 /* this structure contains the information for one record to be deleted */
88 struct delete_record_data {
89 struct ctdb_context *ctdb;
90 struct ctdb_db_context *ctdb_db;
91 struct ctdb_ltdb_header hdr;
92 TDB_DATA key;
93 uint8_t keydata[1];
96 struct delete_records_list {
97 struct ctdb_marshall_buffer *records;
98 struct vacuum_data *vdata;
101 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
102 const struct ctdb_ltdb_header *hdr,
103 TDB_DATA key);
106 * Store key and header in a tree, indexed by the key hash.
108 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
109 struct ctdb_db_context *ctdb_db,
110 trbt_tree_t *tree,
111 const struct ctdb_ltdb_header *hdr,
112 TDB_DATA key)
114 struct delete_record_data *dd;
115 uint32_t hash;
116 size_t len;
118 len = offsetof(struct delete_record_data, keydata) + key.dsize;
120 dd = (struct delete_record_data *)talloc_size(tree, len);
121 if (dd == NULL) {
122 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
123 return -1;
125 talloc_set_name_const(dd, "struct delete_record_data");
127 dd->ctdb = ctdb;
128 dd->ctdb_db = ctdb_db;
129 dd->key.dsize = key.dsize;
130 dd->key.dptr = dd->keydata;
131 memcpy(dd->keydata, key.dptr, key.dsize);
133 dd->hdr = *hdr;
135 hash = ctdb_hash(&key);
137 trbt_insert32(tree, hash, dd);
139 return 0;
142 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
143 struct ctdb_ltdb_header *hdr)
145 struct ctdb_context *ctdb = vdata->ctdb;
146 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
147 uint32_t hash;
148 int ret;
150 hash = ctdb_hash(&key);
152 if (trbt_lookup32(vdata->delete_list, hash)) {
153 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
154 return 0;
157 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
158 vdata->delete_list,
159 hdr, key);
160 if (ret != 0) {
161 return -1;
164 vdata->delete_count++;
166 return 0;
170 * Add a record to the list of records to be sent
171 * to their lmaster with VACUUM_FETCH.
173 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
174 TDB_DATA key)
176 struct ctdb_context *ctdb = vdata->ctdb;
177 struct ctdb_rec_data *rec;
178 uint32_t lmaster;
179 size_t old_size;
180 struct ctdb_marshall_buffer *vfl;
182 lmaster = ctdb_lmaster(ctdb, &key);
184 vfl = vdata->vacuum_fetch_list[lmaster];
186 rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
187 if (rec == NULL) {
188 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
189 vdata->traverse_error = true;
190 return -1;
193 old_size = talloc_get_size(vfl);
194 vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
195 if (vfl == NULL) {
196 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
197 vdata->traverse_error = true;
198 return -1;
200 vdata->vacuum_fetch_list[lmaster] = vfl;
202 vfl->count++;
203 memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
204 talloc_free(rec);
206 vdata->total++;
208 return 0;
212 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
213 struct timeval t, void *private_data);
215 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
217 struct ctdb_ltdb_header *header =
218 (struct ctdb_ltdb_header *)private_data;
220 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
221 return -1;
224 *header = *(struct ctdb_ltdb_header *)data.dptr;
226 return 0;
230 * traverse function for gathering the records that can be deleted
232 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
233 void *private_data)
235 struct vacuum_data *vdata = talloc_get_type(private_data,
236 struct vacuum_data);
237 struct ctdb_context *ctdb = vdata->ctdb;
238 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
239 uint32_t lmaster;
240 struct ctdb_ltdb_header *hdr;
241 int res = 0;
243 vdata->full_total++;
245 lmaster = ctdb_lmaster(ctdb, &key);
246 if (lmaster >= ctdb->num_nodes) {
247 vdata->full_error++;
248 DEBUG(DEBUG_CRIT, (__location__
249 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
250 " with hash[%u]!\n",
251 (unsigned)lmaster,
252 (unsigned)ctdb->num_nodes,
253 (unsigned)ctdb_hash(&key)));
254 return -1;
257 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
258 /* it is not a deleted record */
259 vdata->full_skipped++;
260 return 0;
263 hdr = (struct ctdb_ltdb_header *)data.dptr;
265 if (hdr->dmaster != ctdb->pnn) {
266 vdata->full_skipped++;
267 return 0;
271 * Add the record to this process's delete_queue for processing
272 * in the subsequent traverse in the fast vacuum run.
274 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
275 if (res != 0) {
276 vdata->full_error++;
277 } else {
278 vdata->full_scheduled++;
281 return 0;
285 * traverse the tree of records to delete and marshall them into
286 * a blob
288 static int delete_marshall_traverse(void *param, void *data)
290 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
291 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
292 struct ctdb_rec_data *rec;
293 size_t old_size;
295 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
296 if (rec == NULL) {
297 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
298 return 0;
301 old_size = talloc_get_size(recs->records);
302 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
303 if (recs->records == NULL) {
304 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
305 return 0;
307 recs->records->count++;
308 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
309 return 0;
313 * Variant of delete_marshall_traverse() that bumps the
314 * RSN of each traversed record in the database.
316 * This is needed to ensure that when rolling out our
317 * empty record copy before remote deletion, we as the
318 * record's dmaster keep a higher RSN than the non-dmaster
319 * nodes. This is needed to prevent old copies from
320 * resurrection in recoveries.
322 static int delete_marshall_traverse_first(void *param, void *data)
324 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
325 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
326 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
327 struct ctdb_context *ctdb = ctdb_db->ctdb;
328 struct ctdb_ltdb_header *header;
329 TDB_DATA tdb_data, ctdb_data;
330 uint32_t lmaster;
331 uint32_t hash = ctdb_hash(&(dd->key));
332 int res;
334 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
335 if (res != 0) {
336 DEBUG(DEBUG_ERR,
337 (__location__ " Error getting chainlock on record with "
338 "key hash [0x%08x] on database db[%s].\n",
339 hash, ctdb_db->db_name));
340 recs->vdata->delete_skipped++;
341 talloc_free(dd);
342 return 0;
346 * Verify that the record is still empty, its RSN has not
347 * changed and that we are still its lmaster and dmaster.
350 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
351 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
352 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
353 "on database db[%s] does not exist or is not"
354 " a ctdb-record. skipping.\n",
355 hash, ctdb_db->db_name));
356 goto skip;
359 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
360 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
361 "on database db[%s] has been recycled. "
362 "skipping.\n",
363 hash, ctdb_db->db_name));
364 goto skip;
367 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
369 if (header->flags & CTDB_REC_RO_FLAGS) {
370 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
371 "on database db[%s] has read-only flags. "
372 "skipping.\n",
373 hash, ctdb_db->db_name));
374 goto skip;
377 if (header->dmaster != ctdb->pnn) {
378 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
379 "on database db[%s] has been migrated away. "
380 "skipping.\n",
381 hash, ctdb_db->db_name));
382 goto skip;
385 if (header->rsn != dd->hdr.rsn) {
386 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
387 "on database db[%s] seems to have been "
388 "migrated away and back again (with empty "
389 "data). skipping.\n",
390 hash, ctdb_db->db_name));
391 goto skip;
394 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
396 if (lmaster != ctdb->pnn) {
397 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
398 "delete list (key hash [0x%08x], db[%s]). "
399 "Strange! skipping.\n",
400 hash, ctdb_db->db_name));
401 goto skip;
405 * Increment the record's RSN to ensure the dmaster (i.e. the current
406 * node) has the highest RSN of the record in the cluster.
407 * This is to prevent old record copies from resurrecting in recoveries
408 * if something should fail during the deletion process.
409 * Note that ctdb_ltdb_store_server() increments the RSN if called
410 * on the record's dmaster.
413 ctdb_data.dptr = tdb_data.dptr + sizeof(struct ctdb_ltdb_header);
414 ctdb_data.dsize = tdb_data.dsize - sizeof(struct ctdb_ltdb_header);
416 res = ctdb_ltdb_store(ctdb_db, dd->key, header, ctdb_data);
417 if (res != 0) {
418 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
419 "key hash [0x%08x] on database db[%s].\n",
420 hash, ctdb_db->db_name));
421 goto skip;
424 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
426 goto done;
428 skip:
429 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
431 recs->vdata->delete_skipped++;
432 talloc_free(dd);
433 dd = NULL;
435 done:
436 if (tdb_data.dptr != NULL) {
437 free(tdb_data.dptr);
440 if (dd == NULL) {
441 return 0;
444 return delete_marshall_traverse(param, data);
448 * traverse function for the traversal of the delete_queue,
449 * the fast-path vacuuming list.
451 * - If the record has been migrated off the node
452 * or has been revived (filled with data) on the node,
453 * then skip the record.
455 * - If the current node is the record's lmaster and it is
456 * a record that has never been migrated with data, then
457 * delete the record from the local tdb.
459 * - If the current node is the record's lmaster and it has
460 * been migrated with data, then schedule it for the normal
461 * vacuuming procedure (i.e. add it to the delete_list).
463 * - If the current node is NOT the record's lmaster then
464 * add it to the list of records that are to be sent to
465 * the lmaster with the VACUUM_FETCH message.
467 static int delete_queue_traverse(void *param, void *data)
469 struct delete_record_data *dd =
470 talloc_get_type(data, struct delete_record_data);
471 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
472 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
473 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
474 int res;
475 struct ctdb_ltdb_header header;
476 uint32_t lmaster;
477 uint32_t hash = ctdb_hash(&(dd->key));
479 vdata->fast_total++;
481 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
482 if (res != 0) {
483 DEBUG(DEBUG_ERR,
484 (__location__ " Error getting chainlock on record with "
485 "key hash [0x%08x] on database db[%s].\n",
486 hash, ctdb_db->db_name));
487 vdata->fast_error++;
488 return 0;
491 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
492 vacuum_record_parser, &header);
493 if (res != 0) {
494 goto skipped;
497 if (header.dmaster != ctdb->pnn) {
498 /* The record has been migrated off the node. Skip. */
499 goto skipped;
502 if (header.rsn != dd->hdr.rsn) {
504 * The record has been migrated off the node and back again.
505 * But not requeued for deletion. Skip it.
507 goto skipped;
511 * We are dmaster, and the record has no data, and it has
512 * not been migrated after it has been queued for deletion.
514 * At this stage, the record could still have been revived locally
515 * and last been written with empty data. This can only be
516 * fixed with the addition of an active or delete flag. (TODO)
519 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
521 if (lmaster != ctdb->pnn) {
522 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
524 if (res != 0) {
525 DEBUG(DEBUG_ERR,
526 (__location__ " Error adding record to list "
527 "of records to send to lmaster.\n"));
528 vdata->fast_error++;
529 } else {
530 vdata->fast_added_to_vacuum_fetch_list++;
532 goto done;
535 /* use header->flags or dd->hdr.flags ?? */
536 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
537 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
539 if (res != 0) {
540 DEBUG(DEBUG_ERR,
541 (__location__ " Error adding record to list "
542 "of records for deletion on lmaster.\n"));
543 vdata->fast_error++;
544 } else {
545 vdata->fast_added_to_delete_list++;
547 } else {
548 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
550 if (res != 0) {
551 DEBUG(DEBUG_ERR,
552 (__location__ " Error deleting record with key "
553 "hash [0x%08x] from local data base db[%s].\n",
554 hash, ctdb_db->db_name));
555 vdata->fast_error++;
556 goto done;
559 DEBUG(DEBUG_DEBUG,
560 (__location__ " Deleted record with key hash "
561 "[0x%08x] from local data base db[%s].\n",
562 hash, ctdb_db->db_name));
563 vdata->fast_deleted++;
566 goto done;
568 skipped:
569 vdata->fast_skipped++;
571 done:
572 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
574 return 0;
578 * Delete the records that we are lmaster and dmaster for and
579 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
580 * control.
582 static int delete_record_traverse(void *param, void *data)
584 struct delete_record_data *dd =
585 talloc_get_type(data, struct delete_record_data);
586 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
587 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
588 struct ctdb_context *ctdb = ctdb_db->ctdb;
589 int res;
590 struct ctdb_ltdb_header *header;
591 TDB_DATA tdb_data;
592 uint32_t lmaster;
593 uint32_t hash = ctdb_hash(&(dd->key));
595 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
596 if (res != 0) {
597 DEBUG(DEBUG_ERR,
598 (__location__ " Error getting chainlock on record with "
599 "key hash [0x%08x] on database db[%s].\n",
600 hash, ctdb_db->db_name));
601 vdata->delete_local_error++;
602 vdata->delete_left--;
603 talloc_free(dd);
604 return 0;
608 * Verify that the record is still empty, its RSN has not
609 * changed and that we are still its lmaster and dmaster.
612 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
613 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
614 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
615 "on database db[%s] does not exist or is not"
616 " a ctdb-record. skipping.\n",
617 hash, ctdb_db->db_name));
618 goto skip;
621 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
622 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
623 "on database db[%s] has been recycled. "
624 "skipping.\n",
625 hash, ctdb_db->db_name));
626 goto skip;
629 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
631 if (header->flags & CTDB_REC_RO_FLAGS) {
632 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
633 "on database db[%s] has read-only flags. "
634 "skipping.\n",
635 hash, ctdb_db->db_name));
636 goto skip;
639 if (header->dmaster != ctdb->pnn) {
640 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
641 "on database db[%s] has been migrated away. "
642 "skipping.\n",
643 hash, ctdb_db->db_name));
644 goto skip;
647 if (header->rsn != dd->hdr.rsn + 1) {
649 * The record has been migrated off the node and back again.
650 * But not requeued for deletion. Skip it.
651 * (Note that the first marshall traverse has bumped the RSN
652 * on disk.)
654 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
655 "on database db[%s] seems to have been "
656 "migrated away and back again (with empty "
657 "data). skipping.\n",
658 hash, ctdb_db->db_name));
659 goto skip;
662 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
664 if (lmaster != ctdb->pnn) {
665 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
666 "delete list (key hash [0x%08x], db[%s]). "
667 "Strange! skipping.\n",
668 hash, ctdb_db->db_name));
669 goto skip;
672 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
674 if (res != 0) {
675 DEBUG(DEBUG_ERR,
676 (__location__ " Error deleting record with key hash "
677 "[0x%08x] from local data base db[%s].\n",
678 hash, ctdb_db->db_name));
679 vdata->delete_local_error++;
680 goto done;
683 DEBUG(DEBUG_DEBUG,
684 (__location__ " Deleted record with key hash [0x%08x] from "
685 "local data base db[%s].\n", hash, ctdb_db->db_name));
687 vdata->delete_deleted++;
688 goto done;
690 skip:
691 vdata->delete_skipped++;
693 done:
694 free(tdb_data.dptr);
696 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
698 talloc_free(dd);
699 vdata->delete_left--;
701 return 0;
705 * Fast vacuuming run:
706 * Traverse the delete_queue.
707 * This fills the same lists as the database traverse.
709 static void ctdb_vacuum_db_fast(struct ctdb_db_context *ctdb_db,
710 struct vacuum_data *vdata)
712 uint32_t sum;
714 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
716 sum = vdata->fast_deleted
717 + vdata->fast_skipped
718 + vdata->fast_error
719 + vdata->fast_added_to_delete_list
720 + vdata->fast_added_to_vacuum_fetch_list;
722 if (vdata->fast_total != sum) {
723 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
724 "counts for db[%s]: total[%u] != sum[%u]\n",
725 ctdb_db->db_name, (unsigned)vdata->fast_total,
726 (unsigned)sum));
729 if (vdata->fast_total > 0) {
730 DEBUG(DEBUG_INFO,
731 (__location__
732 " fast vacuuming delete_queue traverse statistics: "
733 "db[%s] "
734 "total[%u] "
735 "del[%u] "
736 "skp[%u] "
737 "err[%u] "
738 "adl[%u] "
739 "avf[%u]\n",
740 ctdb_db->db_name,
741 (unsigned)vdata->fast_total,
742 (unsigned)vdata->fast_deleted,
743 (unsigned)vdata->fast_skipped,
744 (unsigned)vdata->fast_error,
745 (unsigned)vdata->fast_added_to_delete_list,
746 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
749 return;
753 * Full vacuum run:
754 * read-only traverse of the database, looking for records that
755 * might be able to be vacuumed.
757 * This is not done each time but only every tunable
758 * VacuumFastPathCount times.
760 static int ctdb_vacuum_db_full(struct ctdb_db_context *ctdb_db,
761 struct vacuum_data *vdata)
763 int ret;
765 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
766 if (ret == -1 || vdata->traverse_error) {
767 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
768 "'%s'\n", ctdb_db->db_name));
769 return -1;
772 if (vdata->full_total > 0) {
773 DEBUG(DEBUG_INFO,
774 (__location__
775 " full vacuuming db traverse statistics: "
776 "db[%s] "
777 "total[%u] "
778 "skp[%u] "
779 "err[%u] "
780 "sched[%u]\n",
781 ctdb_db->db_name,
782 (unsigned)vdata->full_total,
783 (unsigned)vdata->full_skipped,
784 (unsigned)vdata->full_error,
785 (unsigned)vdata->full_scheduled));
788 return 0;
792 * Process the vacuum fetch lists:
793 * For records for which we are not the lmaster, tell the lmaster to
794 * fetch the record.
796 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
797 struct vacuum_data *vdata)
799 int i;
800 struct ctdb_context *ctdb = ctdb_db->ctdb;
802 for (i = 0; i < ctdb->num_nodes; i++) {
803 TDB_DATA data;
804 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
806 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
807 continue;
810 if (vfl->count == 0) {
811 continue;
814 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
815 vfl->count, ctdb->nodes[i]->pnn,
816 ctdb_db->db_name));
818 data.dsize = talloc_get_size(vfl);
819 data.dptr = (void *)vfl;
820 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
821 CTDB_SRVID_VACUUM_FETCH,
822 data) != 0)
824 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
825 "fetch message to %u\n",
826 ctdb->nodes[i]->pnn));
827 return -1;
831 return 0;
835 * Process the delete list:
837 * This is the last step of vacuuming that consistently deletes
838 * those records that have been migrated with data and can hence
839 * not be deleted when leaving a node.
841 * In this step, the lmaster does the final deletion of those empty
842 * records that it is also dmaster for. It has ususally received
843 * at least some of these records previously from the former dmasters
844 * with the vacuum fetch message.
846 * This last step is implemented as a 3-phase process to protect from
847 * races leading to data corruption:
849 * 1) Send the lmaster's copy to all other active nodes with the
850 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
851 * 2) Send the records that could successfully be stored remotely
852 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
853 * control. The remote notes delete their local copy.
854 * 3) The lmaster locally deletes its copies of all records that
855 * could successfully be deleted remotely in step #2.
857 static int ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
858 struct vacuum_data *vdata)
860 int ret, i;
861 struct ctdb_context *ctdb = ctdb_db->ctdb;
862 struct delete_records_list *recs;
863 TDB_DATA indata;
864 struct ctdb_node_map *nodemap;
865 uint32_t *active_nodes;
866 int num_active_nodes;
867 TALLOC_CTX *tmp_ctx;
869 if (vdata->delete_count == 0) {
870 return 0;
873 tmp_ctx = talloc_new(vdata);
874 if (tmp_ctx == NULL) {
875 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
876 return 0;
879 vdata->delete_left = vdata->delete_count;
882 * get the list of currently active nodes
885 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
886 CTDB_CURRENT_NODE,
887 tmp_ctx,
888 &nodemap);
889 if (ret != 0) {
890 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
891 ret = -1;
892 goto done;
895 active_nodes = list_of_active_nodes(ctdb, nodemap,
896 nodemap, /* talloc context */
897 false /* include self */);
898 /* yuck! ;-) */
899 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
902 * Now delete the records all active nodes in a three-phase process:
903 * 1) send all active remote nodes the current empty copy with this
904 * node as DMASTER
905 * 2) if all nodes could store the new copy,
906 * tell all the active remote nodes to delete all their copy
907 * 3) if all remote nodes deleted their record copy, delete it locally
911 * Step 1:
912 * Send currently empty record copy to all active nodes for storing.
915 recs = talloc_zero(tmp_ctx, struct delete_records_list);
916 if (recs == NULL) {
917 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
918 ret = -1;
919 goto done;
921 recs->records = (struct ctdb_marshall_buffer *)
922 talloc_zero_size(recs,
923 offsetof(struct ctdb_marshall_buffer, data));
924 if (recs->records == NULL) {
925 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
926 ret = -1;
927 goto done;
929 recs->records->db_id = ctdb_db->db_id;
930 recs->vdata = vdata;
933 * traverse the tree of all records we want to delete and
934 * create a blob we can send to the other nodes.
936 * We call delete_marshall_traverse_first() to bump the
937 * records' RSNs in the database, to ensure we (as dmaster)
938 * keep the highest RSN of the records in the cluster.
940 trbt_traversearray32(vdata->delete_list, 1,
941 delete_marshall_traverse_first, recs);
943 indata.dsize = talloc_get_size(recs->records);
944 indata.dptr = (void *)recs->records;
946 for (i = 0; i < num_active_nodes; i++) {
947 struct ctdb_marshall_buffer *records;
948 struct ctdb_rec_data *rec;
949 int32_t res;
950 TDB_DATA outdata;
952 ret = ctdb_control(ctdb, active_nodes[i], 0,
953 CTDB_CONTROL_RECEIVE_RECORDS, 0,
954 indata, recs, &outdata, &res,
955 NULL, NULL);
956 if (ret != 0 || res != 0) {
957 DEBUG(DEBUG_ERR, ("Error storing record copies on "
958 "node %u: ret[%d] res[%d]\n",
959 active_nodes[i], ret, res));
960 ret = -1;
961 goto done;
965 * outdata contains the list of records coming back
966 * from the node: These are the records that the
967 * remote node could not store. We remove these from
968 * the list to process further.
970 records = (struct ctdb_marshall_buffer *)outdata.dptr;
971 rec = (struct ctdb_rec_data *)&records->data[0];
972 while (records->count-- > 1) {
973 TDB_DATA reckey, recdata;
974 struct ctdb_ltdb_header *rechdr;
975 struct delete_record_data *dd;
977 reckey.dptr = &rec->data[0];
978 reckey.dsize = rec->keylen;
979 recdata.dptr = &rec->data[reckey.dsize];
980 recdata.dsize = rec->datalen;
982 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
983 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
984 ret = -1;
985 goto done;
987 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
988 recdata.dptr += sizeof(*rechdr);
989 recdata.dsize -= sizeof(*rechdr);
991 dd = (struct delete_record_data *)trbt_lookup32(
992 vdata->delete_list,
993 ctdb_hash(&reckey));
994 if (dd != NULL) {
996 * The other node could not store the record
997 * copy and it is the first node that failed.
998 * So we should remove it from the tree and
999 * update statistics.
1001 talloc_free(dd);
1002 vdata->delete_remote_error++;
1003 vdata->delete_left--;
1006 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1010 if (vdata->delete_left == 0) {
1011 goto success;
1015 * Step 2:
1016 * Send the remaining records to all active nodes for deletion.
1018 * The lmaster's (i.e. our) copies of these records have been stored
1019 * successfully on the other nodes.
1023 * Create a marshall blob from the remaining list of records to delete.
1026 talloc_free(recs->records);
1028 recs->records = (struct ctdb_marshall_buffer *)
1029 talloc_zero_size(recs,
1030 offsetof(struct ctdb_marshall_buffer, data));
1031 if (recs->records == NULL) {
1032 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1033 ret = -1;
1034 goto done;
1036 recs->records->db_id = ctdb_db->db_id;
1038 trbt_traversearray32(vdata->delete_list, 1,
1039 delete_marshall_traverse, recs);
1041 indata.dsize = talloc_get_size(recs->records);
1042 indata.dptr = (void *)recs->records;
1044 for (i = 0; i < num_active_nodes; i++) {
1045 struct ctdb_marshall_buffer *records;
1046 struct ctdb_rec_data *rec;
1047 int32_t res;
1048 TDB_DATA outdata;
1050 ret = ctdb_control(ctdb, active_nodes[i], 0,
1051 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1052 indata, recs, &outdata, &res,
1053 NULL, NULL);
1054 if (ret != 0 || res != 0) {
1055 DEBUG(DEBUG_ERR, ("Failed to delete records on "
1056 "node %u: ret[%d] res[%d]\n",
1057 active_nodes[i], ret, res));
1058 ret = -1;
1059 goto done;
1063 * outdata contains the list of records coming back
1064 * from the node: These are the records that the
1065 * remote node could not delete. We remove these from
1066 * the list to delete locally.
1068 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1069 rec = (struct ctdb_rec_data *)&records->data[0];
1070 while (records->count-- > 1) {
1071 TDB_DATA reckey, recdata;
1072 struct ctdb_ltdb_header *rechdr;
1073 struct delete_record_data *dd;
1075 reckey.dptr = &rec->data[0];
1076 reckey.dsize = rec->keylen;
1077 recdata.dptr = &rec->data[reckey.dsize];
1078 recdata.dsize = rec->datalen;
1080 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1081 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1082 ret = -1;
1083 goto done;
1085 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1086 recdata.dptr += sizeof(*rechdr);
1087 recdata.dsize -= sizeof(*rechdr);
1089 dd = (struct delete_record_data *)trbt_lookup32(
1090 vdata->delete_list,
1091 ctdb_hash(&reckey));
1092 if (dd != NULL) {
1094 * The other node could not delete the
1095 * record and it is the first node that
1096 * failed. So we should remove it from
1097 * the tree and update statistics.
1099 talloc_free(dd);
1100 vdata->delete_remote_error++;
1101 vdata->delete_left--;
1104 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1108 if (vdata->delete_left == 0) {
1109 goto success;
1113 * Step 3:
1114 * Delete the remaining records locally.
1116 * These records have successfully been deleted on all
1117 * active remote nodes.
1120 trbt_traversearray32(vdata->delete_list, 1,
1121 delete_record_traverse, vdata);
1123 success:
1125 if (vdata->delete_count > 0) {
1126 DEBUG(DEBUG_INFO,
1127 (__location__
1128 " vacuum delete list statistics: "
1129 "db[%s] "
1130 "total[%u] "
1131 "del[%u] "
1132 "skip[%u] "
1133 "rem.err[%u] "
1134 "loc.err[%u] "
1135 "left[%u]\n",
1136 ctdb_db->db_name,
1137 (unsigned)vdata->delete_count,
1138 (unsigned)vdata->delete_deleted,
1139 (unsigned)vdata->delete_skipped,
1140 (unsigned)vdata->delete_remote_error,
1141 (unsigned)vdata->delete_local_error,
1142 (unsigned)vdata->delete_left));
1145 ret = 0;
1147 done:
1148 talloc_free(tmp_ctx);
1150 return ret;
1154 * initialize the vacuum_data
1156 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context *ctdb_db,
1157 struct vacuum_data *vdata)
1159 int i;
1160 struct ctdb_context *ctdb = ctdb_db->ctdb;
1162 vdata->fast_added_to_delete_list = 0;
1163 vdata->fast_added_to_vacuum_fetch_list = 0;
1164 vdata->fast_deleted = 0;
1165 vdata->fast_skipped = 0;
1166 vdata->fast_error = 0;
1167 vdata->fast_total = 0;
1168 vdata->full_scheduled = 0;
1169 vdata->full_skipped = 0;
1170 vdata->full_error = 0;
1171 vdata->full_total = 0;
1172 vdata->delete_count = 0;
1173 vdata->delete_left = 0;
1174 vdata->delete_remote_error = 0;
1175 vdata->delete_local_error = 0;
1176 vdata->delete_skipped = 0;
1177 vdata->delete_deleted = 0;
1179 /* the list needs to be of length num_nodes */
1180 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1181 struct ctdb_marshall_buffer *,
1182 ctdb->num_nodes);
1183 if (vdata->vacuum_fetch_list == NULL) {
1184 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1185 return -1;
1187 for (i = 0; i < ctdb->num_nodes; i++) {
1188 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1189 talloc_zero_size(vdata->vacuum_fetch_list,
1190 offsetof(struct ctdb_marshall_buffer, data));
1191 if (vdata->vacuum_fetch_list[i] == NULL) {
1192 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1193 return -1;
1195 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1198 return 0;
1202 * Vacuum a DB:
1203 * - Always do the fast vacuuming run, which traverses
1204 * the in-memory delete queue: these records have been
1205 * scheduled for deletion.
1206 * - Only if explicitly requested, the database is traversed
1207 * in order to use the traditional heuristics on empty records
1208 * to trigger deletion.
1209 * This is done only every VacuumFastPathCount'th vacuuming run.
1211 * The traverse runs fill two lists:
1213 * - The delete_list:
1214 * This is the list of empty records the current
1215 * node is lmaster and dmaster for. These records are later
1216 * deleted first on other nodes and then locally.
1218 * The fast vacuuming run has a short cut for those records
1219 * that have never been migrated with data: these records
1220 * are immediately deleted locally, since they have left
1221 * no trace on other nodes.
1223 * - The vacuum_fetch lists
1224 * (one for each other lmaster node):
1225 * The records in this list are sent for deletion to
1226 * their lmaster in a bulk VACUUM_FETCH message.
1228 * The lmaster then migrates all these records to itelf
1229 * so that they can be vacuumed there.
1231 * This executes in the child context.
1233 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1234 struct vacuum_data *vdata,
1235 bool full_vacuum_run)
1237 struct ctdb_context *ctdb = ctdb_db->ctdb;
1238 int ret, pnn;
1240 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1241 "%s db_id[0x%08x]\n",
1242 full_vacuum_run ? "full" : "fast",
1243 ctdb_db->db_name, ctdb_db->db_id));
1245 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1246 if (ret != 0) {
1247 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1248 return ret;
1251 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1252 if (pnn == -1) {
1253 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1254 return -1;
1257 ctdb->pnn = pnn;
1259 ret = ctdb_vacuum_init_vacuum_data(ctdb_db, vdata);
1260 if (ret != 0) {
1261 return ret;
1264 if (full_vacuum_run) {
1265 ret = ctdb_vacuum_db_full(ctdb_db, vdata);
1266 if (ret != 0) {
1267 return ret;
1271 ctdb_vacuum_db_fast(ctdb_db, vdata);
1273 ret = ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1274 if (ret != 0) {
1275 return ret;
1278 ret = ctdb_process_delete_list(ctdb_db, vdata);
1279 if (ret != 0) {
1280 return ret;
1283 /* this ensures we run our event queue */
1284 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1286 return 0;
1291 * traverse function for repacking
1293 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
1294 void *private_data)
1296 struct vacuum_data *vdata = (struct vacuum_data *)private_data;
1298 if (vdata->vacuum) {
1299 uint32_t hash = ctdb_hash(&key);
1300 struct delete_record_data *kd;
1302 * check if we can ignore this record because it's in the delete_list
1304 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
1306 * there might be hash collisions so we have to compare the keys here to be sure
1308 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
1309 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
1311 * we have to check if the record hasn't changed in the meantime in order to
1312 * savely remove it from the database
1314 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
1315 hdr->dmaster == kd->ctdb->pnn &&
1316 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
1317 kd->hdr.rsn == hdr->rsn) {
1318 vdata->vacuumed++;
1319 return 0;
1323 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
1324 vdata->traverse_error = true;
1325 return -1;
1327 vdata->copied++;
1328 return 0;
1332 * repack a tdb
1334 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
1336 struct tdb_context *tmp_db;
1338 if (tdb_transaction_start(tdb) != 0) {
1339 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
1340 return -1;
1343 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
1344 TDB_INTERNAL|TDB_DISALLOW_NESTING,
1345 O_RDWR|O_CREAT, 0);
1346 if (tmp_db == NULL) {
1347 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
1348 tdb_transaction_cancel(tdb);
1349 return -1;
1352 vdata->traverse_error = false;
1353 vdata->dest_db = tmp_db;
1354 vdata->vacuum = true;
1355 vdata->vacuumed = 0;
1356 vdata->copied = 0;
1359 * repack and vacuum on-the-fly by not writing the records that are
1360 * no longer needed
1362 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
1363 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
1364 tdb_transaction_cancel(tdb);
1365 tdb_close(tmp_db);
1366 return -1;
1369 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
1371 if (vdata->traverse_error) {
1372 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
1373 tdb_transaction_cancel(tdb);
1374 tdb_close(tmp_db);
1375 return -1;
1378 if (tdb_wipe_all(tdb) != 0) {
1379 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
1380 tdb_transaction_cancel(tdb);
1381 tdb_close(tmp_db);
1382 return -1;
1385 vdata->traverse_error = false;
1386 vdata->dest_db = tdb;
1387 vdata->vacuum = false;
1388 vdata->copied = 0;
1390 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
1391 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
1392 tdb_transaction_cancel(tdb);
1393 tdb_close(tmp_db);
1394 return -1;
1397 if (vdata->traverse_error) {
1398 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
1399 tdb_transaction_cancel(tdb);
1400 tdb_close(tmp_db);
1401 return -1;
1404 tdb_close(tmp_db);
1407 if (tdb_transaction_commit(tdb) != 0) {
1408 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1409 return -1;
1411 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1413 return 0;
1417 * repack and vaccum a db
1418 * called from the child context
1420 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1421 TALLOC_CTX *mem_ctx,
1422 bool full_vacuum_run)
1424 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1425 uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
1426 const char *name = ctdb_db->db_name;
1427 int freelist_size = 0;
1428 struct vacuum_data *vdata;
1430 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1431 if (vdata == NULL) {
1432 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1433 return -1;
1436 vdata->ctdb = ctdb_db->ctdb;
1437 vdata->vacuum_limit = vacuum_limit;
1438 vdata->repack_limit = repack_limit;
1439 vdata->delete_list = trbt_create(vdata, 0);
1440 vdata->ctdb_db = ctdb_db;
1441 if (vdata->delete_list == NULL) {
1442 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1443 talloc_free(vdata);
1444 return -1;
1447 vdata->start = timeval_current();
1450 * gather all records that can be deleted in vdata
1452 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1453 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1456 if (repack_limit != 0) {
1457 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1458 if (freelist_size == -1) {
1459 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1460 talloc_free(vdata);
1461 return -1;
1466 * decide if a repack is necessary
1468 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit) &&
1469 vdata->delete_left < vacuum_limit)
1471 talloc_free(vdata);
1472 return 0;
1475 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
1476 name, freelist_size, vdata->delete_left));
1479 * repack and implicitely get rid of the records we can delete
1481 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1482 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1483 talloc_free(vdata);
1484 return -1;
1486 talloc_free(vdata);
1488 return 0;
1491 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1493 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1495 return interval;
1498 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1500 double l = timeval_elapsed(&child_ctx->start_time);
1501 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1502 struct ctdb_context *ctdb = ctdb_db->ctdb;
1504 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1506 if (child_ctx->child_pid != -1) {
1507 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1508 } else {
1509 /* Bump the number of successful fast-path runs. */
1510 child_ctx->vacuum_handle->fast_path_count++;
1513 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1515 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1516 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1517 ctdb_vacuum_event, child_ctx->vacuum_handle);
1519 return 0;
1523 * this event is generated when a vacuum child process times out
1525 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1526 struct timeval t, void *private_data)
1528 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1530 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1532 child_ctx->status = VACUUM_TIMEOUT;
1534 talloc_free(child_ctx);
1539 * this event is generated when a vacuum child process has completed
1541 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1542 uint16_t flags, void *private_data)
1544 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1545 char c = 0;
1546 int ret;
1548 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1549 child_ctx->child_pid = -1;
1551 ret = read(child_ctx->fd[0], &c, 1);
1552 if (ret != 1 || c != 0) {
1553 child_ctx->status = VACUUM_ERROR;
1554 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));
1555 } else {
1556 child_ctx->status = VACUUM_OK;
1559 talloc_free(child_ctx);
1563 * this event is called every time we need to start a new vacuum process
1565 static void
1566 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1567 struct timeval t, void *private_data)
1569 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1570 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1571 struct ctdb_context *ctdb = ctdb_db->ctdb;
1572 struct ctdb_vacuum_child_context *child_ctx;
1573 struct tevent_fd *fde;
1574 int ret;
1576 /* we dont vacuum if we are in recovery mode, or db frozen */
1577 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1578 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1579 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1580 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1581 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1582 ? "freeze pending"
1583 : "frozen"));
1584 event_add_timed(ctdb->ev, vacuum_handle,
1585 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1586 ctdb_vacuum_event, vacuum_handle);
1587 return;
1590 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1591 if (child_ctx == NULL) {
1592 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1593 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1597 ret = pipe(child_ctx->fd);
1598 if (ret != 0) {
1599 talloc_free(child_ctx);
1600 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1601 event_add_timed(ctdb->ev, vacuum_handle,
1602 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1603 ctdb_vacuum_event, vacuum_handle);
1604 return;
1607 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1608 vacuum_handle->fast_path_count = 0;
1611 child_ctx->child_pid = ctdb_fork(ctdb);
1612 if (child_ctx->child_pid == (pid_t)-1) {
1613 close(child_ctx->fd[0]);
1614 close(child_ctx->fd[1]);
1615 talloc_free(child_ctx);
1616 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1617 event_add_timed(ctdb->ev, vacuum_handle,
1618 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1619 ctdb_vacuum_event, vacuum_handle);
1620 return;
1624 if (child_ctx->child_pid == 0) {
1625 char cc = 0;
1626 bool full_vacuum_run = false;
1627 close(child_ctx->fd[0]);
1629 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1630 ctdb_set_process_name("ctdb_vacuum");
1631 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1632 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1633 _exit(1);
1637 * repack the db
1639 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1640 (vacuum_handle->fast_path_count == 0))
1642 full_vacuum_run = true;
1644 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1645 full_vacuum_run);
1647 write(child_ctx->fd[1], &cc, 1);
1648 _exit(0);
1651 set_close_on_exec(child_ctx->fd[0]);
1652 close(child_ctx->fd[1]);
1654 child_ctx->status = VACUUM_RUNNING;
1655 child_ctx->start_time = timeval_current();
1657 DLIST_ADD(ctdb->vacuumers, child_ctx);
1658 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1661 * Clear the fastpath vacuuming list in the parent.
1663 talloc_free(ctdb_db->delete_queue);
1664 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1665 if (ctdb_db->delete_queue == NULL) {
1666 /* fatal here? ... */
1667 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1668 "in parent context. Shutting down\n");
1671 event_add_timed(ctdb->ev, child_ctx,
1672 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1673 vacuum_child_timeout, child_ctx);
1675 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1677 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1678 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1679 tevent_fd_set_auto_close(fde);
1681 vacuum_handle->child_ctx = child_ctx;
1682 child_ctx->vacuum_handle = vacuum_handle;
1685 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1687 /* Simply free them all. */
1688 while (ctdb->vacuumers) {
1689 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1690 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1691 (int)ctdb->vacuumers->child_pid));
1692 /* vacuum_child_destructor kills it, removes from list */
1693 talloc_free(ctdb->vacuumers);
1697 /* this function initializes the vacuuming context for a database
1698 * starts the vacuuming events
1700 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1702 if (ctdb_db->persistent != 0) {
1703 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1704 return 0;
1707 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1708 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1710 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1711 ctdb_db->vacuum_handle->fast_path_count = 0;
1713 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1714 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1715 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1717 return 0;
1720 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1721 const struct ctdb_ltdb_header *hdr,
1722 const TDB_DATA key)
1724 struct delete_record_data *kd;
1725 uint32_t hash;
1727 hash = (uint32_t)ctdb_hash(&key);
1729 DEBUG(DEBUG_DEBUG, (__location__
1730 " remove_record_from_delete_queue: "
1731 "db[%s] "
1732 "db_id[0x%08x] "
1733 "key_hash[0x%08x] "
1734 "lmaster[%u] "
1735 "migrated_with_data[%s]\n",
1736 ctdb_db->db_name, ctdb_db->db_id,
1737 hash,
1738 ctdb_lmaster(ctdb_db->ctdb, &key),
1739 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1741 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1742 if (kd == NULL) {
1743 DEBUG(DEBUG_DEBUG, (__location__
1744 " remove_record_from_delete_queue: "
1745 "record not in queue (hash[0x%08x])\n.",
1746 hash));
1747 return;
1750 if ((kd->key.dsize != key.dsize) ||
1751 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1753 DEBUG(DEBUG_DEBUG, (__location__
1754 " remove_record_from_delete_queue: "
1755 "hash collision for key with hash[0x%08x] "
1756 "in db[%s] - skipping\n",
1757 hash, ctdb_db->db_name));
1758 return;
1761 DEBUG(DEBUG_DEBUG, (__location__
1762 " remove_record_from_delete_queue: "
1763 "removing key with hash[0x%08x]\n",
1764 hash));
1766 talloc_free(kd);
1768 return;
1772 * Insert a record into the ctdb_db context's delete queue,
1773 * handling hash collisions.
1775 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1776 const struct ctdb_ltdb_header *hdr,
1777 TDB_DATA key)
1779 struct delete_record_data *kd;
1780 uint32_t hash;
1781 int ret;
1783 hash = (uint32_t)ctdb_hash(&key);
1785 DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1786 "db_id[0x%08x] "
1787 "key_hash[0x%08x] "
1788 "lmaster[%u] "
1789 "migrated_with_data[%s]\n",
1790 ctdb_db->db_name, ctdb_db->db_id,
1791 hash,
1792 ctdb_lmaster(ctdb_db->ctdb, &key),
1793 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1795 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1796 if (kd != NULL) {
1797 if ((kd->key.dsize != key.dsize) ||
1798 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1800 DEBUG(DEBUG_INFO,
1801 (__location__ " schedule for deletion: "
1802 "hash collision for key hash [0x%08x]. "
1803 "Skipping the record.\n", hash));
1804 return 0;
1805 } else {
1806 DEBUG(DEBUG_DEBUG,
1807 (__location__ " schedule for deletion: "
1808 "updating entry for key with hash [0x%08x].\n",
1809 hash));
1813 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1814 ctdb_db->delete_queue,
1815 hdr, key);
1816 if (ret != 0) {
1817 DEBUG(DEBUG_INFO,
1818 (__location__ " schedule for deletion: error "
1819 "inserting key with hash [0x%08x] into delete queue\n",
1820 hash));
1821 return -1;
1824 return 0;
1828 * Schedule a record for deletetion.
1829 * Called from the parent context.
1831 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1832 TDB_DATA indata)
1834 struct ctdb_control_schedule_for_deletion *dd;
1835 struct ctdb_db_context *ctdb_db;
1836 int ret;
1837 TDB_DATA key;
1839 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1841 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1842 if (ctdb_db == NULL) {
1843 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1844 dd->db_id));
1845 return -1;
1848 key.dsize = dd->keylen;
1849 key.dptr = dd->key;
1851 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1853 return ret;
1856 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1857 const struct ctdb_ltdb_header *hdr,
1858 TDB_DATA key)
1860 int ret;
1861 struct ctdb_control_schedule_for_deletion *dd;
1862 TDB_DATA indata;
1863 int32_t status;
1865 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1866 /* main daemon - directly queue */
1867 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1869 return ret;
1872 /* if we dont have a connection to the daemon we can not send
1873 a control. For example sometimes from update_record control child
1874 process.
1876 if (!ctdb_db->ctdb->can_send_controls) {
1877 return -1;
1881 /* child process: send the main daemon a control */
1882 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1883 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1884 if (indata.dptr == NULL) {
1885 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1886 return -1;
1888 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1889 dd->db_id = ctdb_db->db_id;
1890 dd->hdr = *hdr;
1891 dd->keylen = key.dsize;
1892 memcpy(dd->key, key.dptr, key.dsize);
1894 ret = ctdb_control(ctdb_db->ctdb,
1895 CTDB_CURRENT_NODE,
1896 ctdb_db->db_id,
1897 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1898 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1899 indata,
1900 NULL, /* mem_ctx */
1901 NULL, /* outdata */
1902 &status,
1903 NULL, /* timeout : NULL == wait forever */
1904 NULL); /* error message */
1906 talloc_free(indata.dptr);
1908 if (ret != 0 || status != 0) {
1909 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1910 "SCHEDULE_FOR_DELETION "
1911 "control.\n"));
1912 if (status != 0) {
1913 ret = -1;
1917 return ret;
1920 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1921 const struct ctdb_ltdb_header *hdr,
1922 const TDB_DATA key)
1924 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1926 * Only remove the record from the delete queue if called
1927 * in the main daemon.
1929 return;
1932 remove_record_from_delete_queue(ctdb_db, hdr, key);
1934 return;