ctdb-vacuum: remove VacuumLimit criterion for triggering a repack
[Samba/wip.git] / ctdb / server / ctdb_vacuum.c
blob24d8cf9ecef421098ebf7c3d73ca3fa6610fe9dd
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 repack_limit;
57 struct ctdb_context *ctdb;
58 struct ctdb_db_context *ctdb_db;
59 struct tdb_context *dest_db;
60 trbt_tree_t *delete_list;
61 uint32_t delete_count;
62 struct ctdb_marshall_buffer **vacuum_fetch_list;
63 struct timeval start;
64 bool traverse_error;
65 bool vacuum;
66 uint32_t total;
67 uint32_t vacuumed;
68 uint32_t copied;
69 uint32_t fast_added_to_vacuum_fetch_list;
70 uint32_t fast_added_to_delete_list;
71 uint32_t fast_deleted;
72 uint32_t fast_skipped;
73 uint32_t fast_error;
74 uint32_t fast_total;
75 uint32_t full_scheduled;
76 uint32_t full_skipped;
77 uint32_t full_error;
78 uint32_t full_total;
79 uint32_t delete_left;
80 uint32_t delete_remote_error;
81 uint32_t delete_local_error;
82 uint32_t delete_deleted;
83 uint32_t delete_skipped;
86 /* this structure contains the information for one record to be deleted */
87 struct delete_record_data {
88 struct ctdb_context *ctdb;
89 struct ctdb_db_context *ctdb_db;
90 struct ctdb_ltdb_header hdr;
91 TDB_DATA key;
92 uint8_t keydata[1];
95 struct delete_records_list {
96 struct ctdb_marshall_buffer *records;
97 struct vacuum_data *vdata;
100 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
101 const struct ctdb_ltdb_header *hdr,
102 TDB_DATA key);
105 * Store key and header in a tree, indexed by the key hash.
107 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
108 struct ctdb_db_context *ctdb_db,
109 trbt_tree_t *tree,
110 const struct ctdb_ltdb_header *hdr,
111 TDB_DATA key)
113 struct delete_record_data *dd;
114 uint32_t hash;
115 size_t len;
117 len = offsetof(struct delete_record_data, keydata) + key.dsize;
119 dd = (struct delete_record_data *)talloc_size(tree, len);
120 if (dd == NULL) {
121 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
122 return -1;
124 talloc_set_name_const(dd, "struct delete_record_data");
126 dd->ctdb = ctdb;
127 dd->ctdb_db = ctdb_db;
128 dd->key.dsize = key.dsize;
129 dd->key.dptr = dd->keydata;
130 memcpy(dd->keydata, key.dptr, key.dsize);
132 dd->hdr = *hdr;
134 hash = ctdb_hash(&key);
136 trbt_insert32(tree, hash, dd);
138 return 0;
141 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
142 struct ctdb_ltdb_header *hdr)
144 struct ctdb_context *ctdb = vdata->ctdb;
145 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
146 uint32_t hash;
147 int ret;
149 hash = ctdb_hash(&key);
151 if (trbt_lookup32(vdata->delete_list, hash)) {
152 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
153 return 0;
156 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
157 vdata->delete_list,
158 hdr, key);
159 if (ret != 0) {
160 return -1;
163 vdata->delete_count++;
165 return 0;
169 * Add a record to the list of records to be sent
170 * to their lmaster with VACUUM_FETCH.
172 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
173 TDB_DATA key)
175 struct ctdb_context *ctdb = vdata->ctdb;
176 struct ctdb_rec_data *rec;
177 uint32_t lmaster;
178 size_t old_size;
179 struct ctdb_marshall_buffer *vfl;
181 lmaster = ctdb_lmaster(ctdb, &key);
183 vfl = vdata->vacuum_fetch_list[lmaster];
185 rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
186 if (rec == NULL) {
187 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
188 vdata->traverse_error = true;
189 return -1;
192 old_size = talloc_get_size(vfl);
193 vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
194 if (vfl == NULL) {
195 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
196 vdata->traverse_error = true;
197 return -1;
199 vdata->vacuum_fetch_list[lmaster] = vfl;
201 vfl->count++;
202 memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
203 talloc_free(rec);
205 vdata->total++;
207 return 0;
211 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
212 struct timeval t, void *private_data);
214 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
216 struct ctdb_ltdb_header *header =
217 (struct ctdb_ltdb_header *)private_data;
219 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
220 return -1;
223 *header = *(struct ctdb_ltdb_header *)data.dptr;
225 return 0;
229 * traverse function for gathering the records that can be deleted
231 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
232 void *private_data)
234 struct vacuum_data *vdata = talloc_get_type(private_data,
235 struct vacuum_data);
236 struct ctdb_context *ctdb = vdata->ctdb;
237 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
238 uint32_t lmaster;
239 struct ctdb_ltdb_header *hdr;
240 int res = 0;
242 vdata->full_total++;
244 lmaster = ctdb_lmaster(ctdb, &key);
245 if (lmaster >= ctdb->num_nodes) {
246 vdata->full_error++;
247 DEBUG(DEBUG_CRIT, (__location__
248 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
249 " with hash[%u]!\n",
250 (unsigned)lmaster,
251 (unsigned)ctdb->num_nodes,
252 (unsigned)ctdb_hash(&key)));
253 return -1;
256 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
257 /* it is not a deleted record */
258 vdata->full_skipped++;
259 return 0;
262 hdr = (struct ctdb_ltdb_header *)data.dptr;
264 if (hdr->dmaster != ctdb->pnn) {
265 vdata->full_skipped++;
266 return 0;
270 * Add the record to this process's delete_queue for processing
271 * in the subsequent traverse in the fast vacuum run.
273 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
274 if (res != 0) {
275 vdata->full_error++;
276 } else {
277 vdata->full_scheduled++;
280 return 0;
284 * traverse the tree of records to delete and marshall them into
285 * a blob
287 static int delete_marshall_traverse(void *param, void *data)
289 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
290 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
291 struct ctdb_rec_data *rec;
292 size_t old_size;
294 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
295 if (rec == NULL) {
296 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
297 return 0;
300 old_size = talloc_get_size(recs->records);
301 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
302 if (recs->records == NULL) {
303 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
304 return 0;
306 recs->records->count++;
307 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
308 return 0;
312 * Variant of delete_marshall_traverse() that bumps the
313 * RSN of each traversed record in the database.
315 * This is needed to ensure that when rolling out our
316 * empty record copy before remote deletion, we as the
317 * record's dmaster keep a higher RSN than the non-dmaster
318 * nodes. This is needed to prevent old copies from
319 * resurrection in recoveries.
321 static int delete_marshall_traverse_first(void *param, void *data)
323 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
324 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
325 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
326 struct ctdb_context *ctdb = ctdb_db->ctdb;
327 struct ctdb_ltdb_header *header;
328 TDB_DATA tdb_data, ctdb_data;
329 uint32_t lmaster;
330 uint32_t hash = ctdb_hash(&(dd->key));
331 int res;
333 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
334 if (res != 0) {
335 DEBUG(DEBUG_ERR,
336 (__location__ " Error getting chainlock on record with "
337 "key hash [0x%08x] on database db[%s].\n",
338 hash, ctdb_db->db_name));
339 recs->vdata->delete_skipped++;
340 talloc_free(dd);
341 return 0;
345 * Verify that the record is still empty, its RSN has not
346 * changed and that we are still its lmaster and dmaster.
349 tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
350 if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
351 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
352 "on database db[%s] does not exist or is not"
353 " a ctdb-record. skipping.\n",
354 hash, ctdb_db->db_name));
355 goto skip;
358 if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
359 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
360 "on database db[%s] has been recycled. "
361 "skipping.\n",
362 hash, ctdb_db->db_name));
363 goto skip;
366 header = (struct ctdb_ltdb_header *)tdb_data.dptr;
368 if (header->flags & CTDB_REC_RO_FLAGS) {
369 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
370 "on database db[%s] has read-only flags. "
371 "skipping.\n",
372 hash, ctdb_db->db_name));
373 goto skip;
376 if (header->dmaster != ctdb->pnn) {
377 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
378 "on database db[%s] has been migrated away. "
379 "skipping.\n",
380 hash, ctdb_db->db_name));
381 goto skip;
384 if (header->rsn != dd->hdr.rsn) {
385 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
386 "on database db[%s] seems to have been "
387 "migrated away and back again (with empty "
388 "data). skipping.\n",
389 hash, ctdb_db->db_name));
390 goto skip;
393 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
395 if (lmaster != ctdb->pnn) {
396 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
397 "delete list (key hash [0x%08x], db[%s]). "
398 "Strange! skipping.\n",
399 hash, ctdb_db->db_name));
400 goto skip;
404 * Increment the record's RSN to ensure the dmaster (i.e. the current
405 * node) has the highest RSN of the record in the cluster.
406 * This is to prevent old record copies from resurrecting in recoveries
407 * if something should fail during the deletion process.
408 * Note that ctdb_ltdb_store_server() increments the RSN if called
409 * on the record's dmaster.
412 ctdb_data.dptr = tdb_data.dptr + sizeof(struct ctdb_ltdb_header);
413 ctdb_data.dsize = tdb_data.dsize - sizeof(struct ctdb_ltdb_header);
415 res = ctdb_ltdb_store(ctdb_db, dd->key, header, ctdb_data);
416 if (res != 0) {
417 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
418 "key hash [0x%08x] on database db[%s].\n",
419 hash, ctdb_db->db_name));
420 goto skip;
423 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
425 goto done;
427 skip:
428 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
430 recs->vdata->delete_skipped++;
431 talloc_free(dd);
432 dd = NULL;
434 done:
435 if (tdb_data.dptr != NULL) {
436 free(tdb_data.dptr);
439 if (dd == NULL) {
440 return 0;
443 return delete_marshall_traverse(param, data);
447 * traverse function for the traversal of the delete_queue,
448 * the fast-path vacuuming list.
450 * - If the record has been migrated off the node
451 * or has been revived (filled with data) on the node,
452 * then skip the record.
454 * - If the current node is the record's lmaster and it is
455 * a record that has never been migrated with data, then
456 * delete the record from the local tdb.
458 * - If the current node is the record's lmaster and it has
459 * been migrated with data, then schedule it for the normal
460 * vacuuming procedure (i.e. add it to the delete_list).
462 * - If the current node is NOT the record's lmaster then
463 * add it to the list of records that are to be sent to
464 * the lmaster with the VACUUM_FETCH message.
466 static int delete_queue_traverse(void *param, void *data)
468 struct delete_record_data *dd =
469 talloc_get_type(data, struct delete_record_data);
470 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
471 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
472 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
473 int res;
474 struct ctdb_ltdb_header header;
475 uint32_t lmaster;
476 uint32_t hash = ctdb_hash(&(dd->key));
478 vdata->fast_total++;
480 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
481 if (res != 0) {
482 DEBUG(DEBUG_ERR,
483 (__location__ " Error getting chainlock on record with "
484 "key hash [0x%08x] on database db[%s].\n",
485 hash, ctdb_db->db_name));
486 vdata->fast_error++;
487 return 0;
490 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
491 vacuum_record_parser, &header);
492 if (res != 0) {
493 goto skipped;
496 if (header.dmaster != ctdb->pnn) {
497 /* The record has been migrated off the node. Skip. */
498 goto skipped;
501 if (header.rsn != dd->hdr.rsn) {
503 * The record has been migrated off the node and back again.
504 * But not requeued for deletion. Skip it.
506 goto skipped;
510 * We are dmaster, and the record has no data, and it has
511 * not been migrated after it has been queued for deletion.
513 * At this stage, the record could still have been revived locally
514 * and last been written with empty data. This can only be
515 * fixed with the addition of an active or delete flag. (TODO)
518 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
520 if (lmaster != ctdb->pnn) {
521 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
523 if (res != 0) {
524 DEBUG(DEBUG_ERR,
525 (__location__ " Error adding record to list "
526 "of records to send to lmaster.\n"));
527 vdata->fast_error++;
528 } else {
529 vdata->fast_added_to_vacuum_fetch_list++;
531 goto done;
534 /* use header->flags or dd->hdr.flags ?? */
535 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
536 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
538 if (res != 0) {
539 DEBUG(DEBUG_ERR,
540 (__location__ " Error adding record to list "
541 "of records for deletion on lmaster.\n"));
542 vdata->fast_error++;
543 } else {
544 vdata->fast_added_to_delete_list++;
546 } else {
547 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
549 if (res != 0) {
550 DEBUG(DEBUG_ERR,
551 (__location__ " Error deleting record with key "
552 "hash [0x%08x] from local data base db[%s].\n",
553 hash, ctdb_db->db_name));
554 vdata->fast_error++;
555 goto done;
558 DEBUG(DEBUG_DEBUG,
559 (__location__ " Deleted record with key hash "
560 "[0x%08x] from local data base db[%s].\n",
561 hash, ctdb_db->db_name));
562 vdata->fast_deleted++;
565 goto done;
567 skipped:
568 vdata->fast_skipped++;
570 done:
571 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
573 return 0;
577 * Delete the records that we are lmaster and dmaster for and
578 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
579 * control.
581 static int delete_record_traverse(void *param, void *data)
583 struct delete_record_data *dd =
584 talloc_get_type(data, struct delete_record_data);
585 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
586 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
587 struct ctdb_context *ctdb = ctdb_db->ctdb;
588 int res;
589 struct ctdb_ltdb_header header;
590 uint32_t lmaster;
591 uint32_t hash = ctdb_hash(&(dd->key));
593 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
594 if (res != 0) {
595 DEBUG(DEBUG_ERR,
596 (__location__ " Error getting chainlock on record with "
597 "key hash [0x%08x] on database db[%s].\n",
598 hash, ctdb_db->db_name));
599 vdata->delete_local_error++;
600 vdata->delete_left--;
601 talloc_free(dd);
602 return 0;
606 * Verify that the record is still empty, its RSN has not
607 * changed and that we are still its lmaster and dmaster.
610 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
611 vacuum_record_parser, &header);
612 if (res != 0) {
613 goto skip;
616 if (header.flags & CTDB_REC_RO_FLAGS) {
617 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
618 "on database db[%s] has read-only flags. "
619 "skipping.\n",
620 hash, ctdb_db->db_name));
621 goto skip;
624 if (header.dmaster != ctdb->pnn) {
625 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
626 "on database db[%s] has been migrated away. "
627 "skipping.\n",
628 hash, ctdb_db->db_name));
629 goto skip;
632 if (header.rsn != dd->hdr.rsn + 1) {
634 * The record has been migrated off the node and back again.
635 * But not requeued for deletion. Skip it.
636 * (Note that the first marshall traverse has bumped the RSN
637 * on disk.)
639 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
640 "on database db[%s] seems to have been "
641 "migrated away and back again (with empty "
642 "data). skipping.\n",
643 hash, ctdb_db->db_name));
644 goto skip;
647 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
649 if (lmaster != ctdb->pnn) {
650 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
651 "delete list (key hash [0x%08x], db[%s]). "
652 "Strange! skipping.\n",
653 hash, ctdb_db->db_name));
654 goto skip;
657 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
659 if (res != 0) {
660 DEBUG(DEBUG_ERR,
661 (__location__ " Error deleting record with key hash "
662 "[0x%08x] from local data base db[%s].\n",
663 hash, ctdb_db->db_name));
664 vdata->delete_local_error++;
665 goto done;
668 DEBUG(DEBUG_DEBUG,
669 (__location__ " Deleted record with key hash [0x%08x] from "
670 "local data base db[%s].\n", hash, ctdb_db->db_name));
672 vdata->delete_deleted++;
673 goto done;
675 skip:
676 vdata->delete_skipped++;
678 done:
679 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
681 talloc_free(dd);
682 vdata->delete_left--;
684 return 0;
688 * Traverse the delete_queue.
689 * Records are either deleted directly or filled
690 * into the delete list or the vacuum fetch lists
691 * for further processing.
693 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
694 struct vacuum_data *vdata)
696 uint32_t sum;
698 trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
700 sum = vdata->fast_deleted
701 + vdata->fast_skipped
702 + vdata->fast_error
703 + vdata->fast_added_to_delete_list
704 + vdata->fast_added_to_vacuum_fetch_list;
706 if (vdata->fast_total != sum) {
707 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
708 "counts for db[%s]: total[%u] != sum[%u]\n",
709 ctdb_db->db_name, (unsigned)vdata->fast_total,
710 (unsigned)sum));
713 if (vdata->fast_total > 0) {
714 DEBUG(DEBUG_INFO,
715 (__location__
716 " fast vacuuming delete_queue traverse statistics: "
717 "db[%s] "
718 "total[%u] "
719 "del[%u] "
720 "skp[%u] "
721 "err[%u] "
722 "adl[%u] "
723 "avf[%u]\n",
724 ctdb_db->db_name,
725 (unsigned)vdata->fast_total,
726 (unsigned)vdata->fast_deleted,
727 (unsigned)vdata->fast_skipped,
728 (unsigned)vdata->fast_error,
729 (unsigned)vdata->fast_added_to_delete_list,
730 (unsigned)vdata->fast_added_to_vacuum_fetch_list));
733 return;
737 * read-only traverse of the database, looking for records that
738 * might be able to be vacuumed.
740 * This is not done each time but only every tunable
741 * VacuumFastPathCount times.
743 static int ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
744 struct vacuum_data *vdata)
746 int ret;
748 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
749 if (ret == -1 || vdata->traverse_error) {
750 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
751 "'%s'\n", ctdb_db->db_name));
752 return -1;
755 if (vdata->full_total > 0) {
756 DEBUG(DEBUG_INFO,
757 (__location__
758 " full vacuuming db traverse statistics: "
759 "db[%s] "
760 "total[%u] "
761 "skp[%u] "
762 "err[%u] "
763 "sched[%u]\n",
764 ctdb_db->db_name,
765 (unsigned)vdata->full_total,
766 (unsigned)vdata->full_skipped,
767 (unsigned)vdata->full_error,
768 (unsigned)vdata->full_scheduled));
771 return 0;
775 * Process the vacuum fetch lists:
776 * For records for which we are not the lmaster, tell the lmaster to
777 * fetch the record.
779 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
780 struct vacuum_data *vdata)
782 int i;
783 struct ctdb_context *ctdb = ctdb_db->ctdb;
785 for (i = 0; i < ctdb->num_nodes; i++) {
786 TDB_DATA data;
787 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
789 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
790 continue;
793 if (vfl->count == 0) {
794 continue;
797 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
798 vfl->count, ctdb->nodes[i]->pnn,
799 ctdb_db->db_name));
801 data.dsize = talloc_get_size(vfl);
802 data.dptr = (void *)vfl;
803 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
804 CTDB_SRVID_VACUUM_FETCH,
805 data) != 0)
807 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
808 "fetch message to %u\n",
809 ctdb->nodes[i]->pnn));
810 return -1;
814 return 0;
818 * Process the delete list:
820 * This is the last step of vacuuming that consistently deletes
821 * those records that have been migrated with data and can hence
822 * not be deleted when leaving a node.
824 * In this step, the lmaster does the final deletion of those empty
825 * records that it is also dmaster for. It has ususally received
826 * at least some of these records previously from the former dmasters
827 * with the vacuum fetch message.
829 * This last step is implemented as a 3-phase process to protect from
830 * races leading to data corruption:
832 * 1) Send the lmaster's copy to all other active nodes with the
833 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
834 * 2) Send the records that could successfully be stored remotely
835 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
836 * control. The remote notes delete their local copy.
837 * 3) The lmaster locally deletes its copies of all records that
838 * could successfully be deleted remotely in step #2.
840 static int ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
841 struct vacuum_data *vdata)
843 int ret, i;
844 struct ctdb_context *ctdb = ctdb_db->ctdb;
845 struct delete_records_list *recs;
846 TDB_DATA indata;
847 struct ctdb_node_map *nodemap;
848 uint32_t *active_nodes;
849 int num_active_nodes;
850 TALLOC_CTX *tmp_ctx;
851 uint32_t sum;
853 if (vdata->delete_count == 0) {
854 return 0;
857 tmp_ctx = talloc_new(vdata);
858 if (tmp_ctx == NULL) {
859 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
860 return 0;
863 vdata->delete_left = vdata->delete_count;
866 * get the list of currently active nodes
869 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
870 CTDB_CURRENT_NODE,
871 tmp_ctx,
872 &nodemap);
873 if (ret != 0) {
874 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
875 ret = -1;
876 goto done;
879 active_nodes = list_of_active_nodes(ctdb, nodemap,
880 nodemap, /* talloc context */
881 false /* include self */);
882 /* yuck! ;-) */
883 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
886 * Now delete the records all active nodes in a three-phase process:
887 * 1) send all active remote nodes the current empty copy with this
888 * node as DMASTER
889 * 2) if all nodes could store the new copy,
890 * tell all the active remote nodes to delete all their copy
891 * 3) if all remote nodes deleted their record copy, delete it locally
895 * Step 1:
896 * Send currently empty record copy to all active nodes for storing.
899 recs = talloc_zero(tmp_ctx, struct delete_records_list);
900 if (recs == NULL) {
901 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
902 ret = -1;
903 goto done;
905 recs->records = (struct ctdb_marshall_buffer *)
906 talloc_zero_size(recs,
907 offsetof(struct ctdb_marshall_buffer, data));
908 if (recs->records == NULL) {
909 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
910 ret = -1;
911 goto done;
913 recs->records->db_id = ctdb_db->db_id;
914 recs->vdata = vdata;
917 * traverse the tree of all records we want to delete and
918 * create a blob we can send to the other nodes.
920 * We call delete_marshall_traverse_first() to bump the
921 * records' RSNs in the database, to ensure we (as dmaster)
922 * keep the highest RSN of the records in the cluster.
924 trbt_traversearray32(vdata->delete_list, 1,
925 delete_marshall_traverse_first, recs);
927 indata.dsize = talloc_get_size(recs->records);
928 indata.dptr = (void *)recs->records;
930 for (i = 0; i < num_active_nodes; i++) {
931 struct ctdb_marshall_buffer *records;
932 struct ctdb_rec_data *rec;
933 int32_t res;
934 TDB_DATA outdata;
936 ret = ctdb_control(ctdb, active_nodes[i], 0,
937 CTDB_CONTROL_RECEIVE_RECORDS, 0,
938 indata, recs, &outdata, &res,
939 NULL, NULL);
940 if (ret != 0 || res != 0) {
941 DEBUG(DEBUG_ERR, ("Error storing record copies on "
942 "node %u: ret[%d] res[%d]\n",
943 active_nodes[i], ret, res));
944 ret = -1;
945 goto done;
949 * outdata contains the list of records coming back
950 * from the node: These are the records that the
951 * remote node could not store. We remove these from
952 * the list to process further.
954 records = (struct ctdb_marshall_buffer *)outdata.dptr;
955 rec = (struct ctdb_rec_data *)&records->data[0];
956 while (records->count-- > 1) {
957 TDB_DATA reckey, recdata;
958 struct ctdb_ltdb_header *rechdr;
959 struct delete_record_data *dd;
961 reckey.dptr = &rec->data[0];
962 reckey.dsize = rec->keylen;
963 recdata.dptr = &rec->data[reckey.dsize];
964 recdata.dsize = rec->datalen;
966 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
967 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
968 ret = -1;
969 goto done;
971 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
972 recdata.dptr += sizeof(*rechdr);
973 recdata.dsize -= sizeof(*rechdr);
975 dd = (struct delete_record_data *)trbt_lookup32(
976 vdata->delete_list,
977 ctdb_hash(&reckey));
978 if (dd != NULL) {
980 * The other node could not store the record
981 * copy and it is the first node that failed.
982 * So we should remove it from the tree and
983 * update statistics.
985 talloc_free(dd);
986 vdata->delete_remote_error++;
987 vdata->delete_left--;
990 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
994 if (vdata->delete_left == 0) {
995 goto success;
999 * Step 2:
1000 * Send the remaining records to all active nodes for deletion.
1002 * The lmaster's (i.e. our) copies of these records have been stored
1003 * successfully on the other nodes.
1007 * Create a marshall blob from the remaining list of records to delete.
1010 talloc_free(recs->records);
1012 recs->records = (struct ctdb_marshall_buffer *)
1013 talloc_zero_size(recs,
1014 offsetof(struct ctdb_marshall_buffer, data));
1015 if (recs->records == NULL) {
1016 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1017 ret = -1;
1018 goto done;
1020 recs->records->db_id = ctdb_db->db_id;
1022 trbt_traversearray32(vdata->delete_list, 1,
1023 delete_marshall_traverse, recs);
1025 indata.dsize = talloc_get_size(recs->records);
1026 indata.dptr = (void *)recs->records;
1028 for (i = 0; i < num_active_nodes; i++) {
1029 struct ctdb_marshall_buffer *records;
1030 struct ctdb_rec_data *rec;
1031 int32_t res;
1032 TDB_DATA outdata;
1034 ret = ctdb_control(ctdb, active_nodes[i], 0,
1035 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1036 indata, recs, &outdata, &res,
1037 NULL, NULL);
1038 if (ret != 0 || res != 0) {
1039 DEBUG(DEBUG_ERR, ("Failed to delete records on "
1040 "node %u: ret[%d] res[%d]\n",
1041 active_nodes[i], ret, res));
1042 ret = -1;
1043 goto done;
1047 * outdata contains the list of records coming back
1048 * from the node: These are the records that the
1049 * remote node could not delete. We remove these from
1050 * the list to delete locally.
1052 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1053 rec = (struct ctdb_rec_data *)&records->data[0];
1054 while (records->count-- > 1) {
1055 TDB_DATA reckey, recdata;
1056 struct ctdb_ltdb_header *rechdr;
1057 struct delete_record_data *dd;
1059 reckey.dptr = &rec->data[0];
1060 reckey.dsize = rec->keylen;
1061 recdata.dptr = &rec->data[reckey.dsize];
1062 recdata.dsize = rec->datalen;
1064 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1065 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1066 ret = -1;
1067 goto done;
1069 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1070 recdata.dptr += sizeof(*rechdr);
1071 recdata.dsize -= sizeof(*rechdr);
1073 dd = (struct delete_record_data *)trbt_lookup32(
1074 vdata->delete_list,
1075 ctdb_hash(&reckey));
1076 if (dd != NULL) {
1078 * The other node could not delete the
1079 * record and it is the first node that
1080 * failed. So we should remove it from
1081 * the tree and update statistics.
1083 talloc_free(dd);
1084 vdata->delete_remote_error++;
1085 vdata->delete_left--;
1088 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1092 if (vdata->delete_left == 0) {
1093 goto success;
1097 * Step 3:
1098 * Delete the remaining records locally.
1100 * These records have successfully been deleted on all
1101 * active remote nodes.
1104 trbt_traversearray32(vdata->delete_list, 1,
1105 delete_record_traverse, vdata);
1107 success:
1109 if (vdata->delete_left != 0) {
1110 DEBUG(DEBUG_ERR, (__location__ " Vaccum db[%s] error: "
1111 "there are %u records left for deletion after "
1112 "processing delete list\n",
1113 ctdb_db->db_name,
1114 (unsigned)vdata->delete_left));
1117 sum = vdata->delete_deleted
1118 + vdata->delete_skipped
1119 + vdata->delete_remote_error
1120 + vdata->delete_local_error
1121 + vdata->delete_left;
1123 if (vdata->delete_count != sum) {
1124 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1125 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1126 ctdb_db->db_name, (unsigned)vdata->delete_count,
1127 (unsigned)sum));
1130 if (vdata->delete_count > 0) {
1131 DEBUG(DEBUG_INFO,
1132 (__location__
1133 " vacuum delete list statistics: "
1134 "db[%s] "
1135 "total[%u] "
1136 "del[%u] "
1137 "skip[%u] "
1138 "rem.err[%u] "
1139 "loc.err[%u] "
1140 "left[%u]\n",
1141 ctdb_db->db_name,
1142 (unsigned)vdata->delete_count,
1143 (unsigned)vdata->delete_deleted,
1144 (unsigned)vdata->delete_skipped,
1145 (unsigned)vdata->delete_remote_error,
1146 (unsigned)vdata->delete_local_error,
1147 (unsigned)vdata->delete_left));
1150 ret = 0;
1152 done:
1153 talloc_free(tmp_ctx);
1155 return ret;
1159 * initialize the vacuum_data
1161 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context *ctdb_db,
1162 struct vacuum_data *vdata)
1164 int i;
1165 struct ctdb_context *ctdb = ctdb_db->ctdb;
1167 vdata->fast_added_to_delete_list = 0;
1168 vdata->fast_added_to_vacuum_fetch_list = 0;
1169 vdata->fast_deleted = 0;
1170 vdata->fast_skipped = 0;
1171 vdata->fast_error = 0;
1172 vdata->fast_total = 0;
1173 vdata->full_scheduled = 0;
1174 vdata->full_skipped = 0;
1175 vdata->full_error = 0;
1176 vdata->full_total = 0;
1177 vdata->delete_count = 0;
1178 vdata->delete_left = 0;
1179 vdata->delete_remote_error = 0;
1180 vdata->delete_local_error = 0;
1181 vdata->delete_skipped = 0;
1182 vdata->delete_deleted = 0;
1184 /* the list needs to be of length num_nodes */
1185 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1186 struct ctdb_marshall_buffer *,
1187 ctdb->num_nodes);
1188 if (vdata->vacuum_fetch_list == NULL) {
1189 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1190 return -1;
1192 for (i = 0; i < ctdb->num_nodes; i++) {
1193 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1194 talloc_zero_size(vdata->vacuum_fetch_list,
1195 offsetof(struct ctdb_marshall_buffer, data));
1196 if (vdata->vacuum_fetch_list[i] == NULL) {
1197 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1198 return -1;
1200 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1203 return 0;
1207 * Vacuum a DB:
1208 * - Always do the fast vacuuming run, which traverses
1209 * the in-memory delete queue: these records have been
1210 * scheduled for deletion.
1211 * - Only if explicitly requested, the database is traversed
1212 * in order to use the traditional heuristics on empty records
1213 * to trigger deletion.
1214 * This is done only every VacuumFastPathCount'th vacuuming run.
1216 * The traverse runs fill two lists:
1218 * - The delete_list:
1219 * This is the list of empty records the current
1220 * node is lmaster and dmaster for. These records are later
1221 * deleted first on other nodes and then locally.
1223 * The fast vacuuming run has a short cut for those records
1224 * that have never been migrated with data: these records
1225 * are immediately deleted locally, since they have left
1226 * no trace on other nodes.
1228 * - The vacuum_fetch lists
1229 * (one for each other lmaster node):
1230 * The records in this list are sent for deletion to
1231 * their lmaster in a bulk VACUUM_FETCH message.
1233 * The lmaster then migrates all these records to itelf
1234 * so that they can be vacuumed there.
1236 * This executes in the child context.
1238 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1239 struct vacuum_data *vdata,
1240 bool full_vacuum_run)
1242 struct ctdb_context *ctdb = ctdb_db->ctdb;
1243 int ret, pnn;
1245 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1246 "%s db_id[0x%08x]\n",
1247 full_vacuum_run ? "full" : "fast",
1248 ctdb_db->db_name, ctdb_db->db_id));
1250 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1251 if (ret != 0) {
1252 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1253 return ret;
1256 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1257 if (pnn == -1) {
1258 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1259 return -1;
1262 ctdb->pnn = pnn;
1264 ret = ctdb_vacuum_init_vacuum_data(ctdb_db, vdata);
1265 if (ret != 0) {
1266 return ret;
1269 if (full_vacuum_run) {
1270 ret = ctdb_vacuum_traverse_db(ctdb_db, vdata);
1271 if (ret != 0) {
1272 return ret;
1276 ctdb_process_delete_queue(ctdb_db, vdata);
1278 ret = ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1279 if (ret != 0) {
1280 return ret;
1283 ret = ctdb_process_delete_list(ctdb_db, vdata);
1284 if (ret != 0) {
1285 return ret;
1288 /* this ensures we run our event queue */
1289 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1291 return 0;
1296 * traverse function for repacking
1298 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
1299 void *private_data)
1301 struct vacuum_data *vdata = (struct vacuum_data *)private_data;
1303 if (vdata->vacuum) {
1304 uint32_t hash = ctdb_hash(&key);
1305 struct delete_record_data *kd;
1307 * check if we can ignore this record because it's in the delete_list
1309 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
1311 * there might be hash collisions so we have to compare the keys here to be sure
1313 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
1314 struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
1316 * we have to check if the record hasn't changed in the meantime in order to
1317 * savely remove it from the database
1319 if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
1320 hdr->dmaster == kd->ctdb->pnn &&
1321 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
1322 kd->hdr.rsn == hdr->rsn) {
1323 vdata->vacuumed++;
1324 return 0;
1328 if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
1329 vdata->traverse_error = true;
1330 return -1;
1332 vdata->copied++;
1333 return 0;
1337 * repack a tdb
1339 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
1341 struct tdb_context *tmp_db;
1343 if (tdb_transaction_start(tdb) != 0) {
1344 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
1345 return -1;
1348 tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
1349 TDB_INTERNAL|TDB_DISALLOW_NESTING,
1350 O_RDWR|O_CREAT, 0);
1351 if (tmp_db == NULL) {
1352 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
1353 tdb_transaction_cancel(tdb);
1354 return -1;
1357 vdata->traverse_error = false;
1358 vdata->dest_db = tmp_db;
1359 vdata->vacuum = true;
1360 vdata->vacuumed = 0;
1361 vdata->copied = 0;
1364 * repack and vacuum on-the-fly by not writing the records that are
1365 * no longer needed
1367 if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
1368 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
1369 tdb_transaction_cancel(tdb);
1370 tdb_close(tmp_db);
1371 return -1;
1374 DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
1376 if (vdata->traverse_error) {
1377 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
1378 tdb_transaction_cancel(tdb);
1379 tdb_close(tmp_db);
1380 return -1;
1383 if (tdb_wipe_all(tdb) != 0) {
1384 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
1385 tdb_transaction_cancel(tdb);
1386 tdb_close(tmp_db);
1387 return -1;
1390 vdata->traverse_error = false;
1391 vdata->dest_db = tdb;
1392 vdata->vacuum = false;
1393 vdata->copied = 0;
1395 if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
1396 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
1397 tdb_transaction_cancel(tdb);
1398 tdb_close(tmp_db);
1399 return -1;
1402 if (vdata->traverse_error) {
1403 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
1404 tdb_transaction_cancel(tdb);
1405 tdb_close(tmp_db);
1406 return -1;
1409 tdb_close(tmp_db);
1412 if (tdb_transaction_commit(tdb) != 0) {
1413 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1414 return -1;
1416 DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1418 return 0;
1422 * repack and vaccum a db
1423 * called from the child context
1425 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1426 TALLOC_CTX *mem_ctx,
1427 bool full_vacuum_run)
1429 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1430 const char *name = ctdb_db->db_name;
1431 int freelist_size = 0;
1432 struct vacuum_data *vdata;
1434 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1435 if (vdata == NULL) {
1436 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1437 return -1;
1440 vdata->ctdb = ctdb_db->ctdb;
1441 vdata->repack_limit = repack_limit;
1442 vdata->delete_list = trbt_create(vdata, 0);
1443 vdata->ctdb_db = ctdb_db;
1444 if (vdata->delete_list == NULL) {
1445 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1446 talloc_free(vdata);
1447 return -1;
1450 vdata->start = timeval_current();
1453 * gather all records that can be deleted in vdata
1455 if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1456 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1459 if (repack_limit != 0) {
1460 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1461 if (freelist_size == -1) {
1462 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1463 talloc_free(vdata);
1464 return -1;
1469 * decide if a repack is necessary
1471 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1473 talloc_free(vdata);
1474 return 0;
1477 DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n",
1478 name, freelist_size, vdata->delete_left));
1481 * repack and implicitely get rid of the records we can delete
1483 if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1484 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1485 talloc_free(vdata);
1486 return -1;
1488 talloc_free(vdata);
1490 return 0;
1493 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1495 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1497 return interval;
1500 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1502 double l = timeval_elapsed(&child_ctx->start_time);
1503 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1504 struct ctdb_context *ctdb = ctdb_db->ctdb;
1506 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1508 if (child_ctx->child_pid != -1) {
1509 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1510 } else {
1511 /* Bump the number of successful fast-path runs. */
1512 child_ctx->vacuum_handle->fast_path_count++;
1515 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1517 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1518 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1519 ctdb_vacuum_event, child_ctx->vacuum_handle);
1521 return 0;
1525 * this event is generated when a vacuum child process times out
1527 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1528 struct timeval t, void *private_data)
1530 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1532 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1534 child_ctx->status = VACUUM_TIMEOUT;
1536 talloc_free(child_ctx);
1541 * this event is generated when a vacuum child process has completed
1543 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1544 uint16_t flags, void *private_data)
1546 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1547 char c = 0;
1548 int ret;
1550 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1551 child_ctx->child_pid = -1;
1553 ret = read(child_ctx->fd[0], &c, 1);
1554 if (ret != 1 || c != 0) {
1555 child_ctx->status = VACUUM_ERROR;
1556 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));
1557 } else {
1558 child_ctx->status = VACUUM_OK;
1561 talloc_free(child_ctx);
1565 * this event is called every time we need to start a new vacuum process
1567 static void
1568 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1569 struct timeval t, void *private_data)
1571 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1572 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1573 struct ctdb_context *ctdb = ctdb_db->ctdb;
1574 struct ctdb_vacuum_child_context *child_ctx;
1575 struct tevent_fd *fde;
1576 int ret;
1578 /* we dont vacuum if we are in recovery mode, or db frozen */
1579 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1580 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1581 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1582 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1583 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1584 ? "freeze pending"
1585 : "frozen"));
1586 event_add_timed(ctdb->ev, vacuum_handle,
1587 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1588 ctdb_vacuum_event, vacuum_handle);
1589 return;
1592 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1593 if (child_ctx == NULL) {
1594 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1595 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1599 ret = pipe(child_ctx->fd);
1600 if (ret != 0) {
1601 talloc_free(child_ctx);
1602 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1603 event_add_timed(ctdb->ev, vacuum_handle,
1604 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1605 ctdb_vacuum_event, vacuum_handle);
1606 return;
1609 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1610 vacuum_handle->fast_path_count = 0;
1613 child_ctx->child_pid = ctdb_fork(ctdb);
1614 if (child_ctx->child_pid == (pid_t)-1) {
1615 close(child_ctx->fd[0]);
1616 close(child_ctx->fd[1]);
1617 talloc_free(child_ctx);
1618 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1619 event_add_timed(ctdb->ev, vacuum_handle,
1620 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1621 ctdb_vacuum_event, vacuum_handle);
1622 return;
1626 if (child_ctx->child_pid == 0) {
1627 char cc = 0;
1628 bool full_vacuum_run = false;
1629 close(child_ctx->fd[0]);
1631 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1632 ctdb_set_process_name("ctdb_vacuum");
1633 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1634 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1635 _exit(1);
1639 * repack the db
1641 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1642 (vacuum_handle->fast_path_count == 0))
1644 full_vacuum_run = true;
1646 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1647 full_vacuum_run);
1649 write(child_ctx->fd[1], &cc, 1);
1650 _exit(0);
1653 set_close_on_exec(child_ctx->fd[0]);
1654 close(child_ctx->fd[1]);
1656 child_ctx->status = VACUUM_RUNNING;
1657 child_ctx->start_time = timeval_current();
1659 DLIST_ADD(ctdb->vacuumers, child_ctx);
1660 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1663 * Clear the fastpath vacuuming list in the parent.
1665 talloc_free(ctdb_db->delete_queue);
1666 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1667 if (ctdb_db->delete_queue == NULL) {
1668 /* fatal here? ... */
1669 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1670 "in parent context. Shutting down\n");
1673 event_add_timed(ctdb->ev, child_ctx,
1674 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1675 vacuum_child_timeout, child_ctx);
1677 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1679 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1680 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1681 tevent_fd_set_auto_close(fde);
1683 vacuum_handle->child_ctx = child_ctx;
1684 child_ctx->vacuum_handle = vacuum_handle;
1687 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1689 /* Simply free them all. */
1690 while (ctdb->vacuumers) {
1691 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1692 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1693 (int)ctdb->vacuumers->child_pid));
1694 /* vacuum_child_destructor kills it, removes from list */
1695 talloc_free(ctdb->vacuumers);
1699 /* this function initializes the vacuuming context for a database
1700 * starts the vacuuming events
1702 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1704 if (ctdb_db->persistent != 0) {
1705 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1706 return 0;
1709 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1710 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1712 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1713 ctdb_db->vacuum_handle->fast_path_count = 0;
1715 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1716 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1717 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1719 return 0;
1722 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1723 const struct ctdb_ltdb_header *hdr,
1724 const TDB_DATA key)
1726 struct delete_record_data *kd;
1727 uint32_t hash;
1729 hash = (uint32_t)ctdb_hash(&key);
1731 DEBUG(DEBUG_DEBUG, (__location__
1732 " remove_record_from_delete_queue: "
1733 "db[%s] "
1734 "db_id[0x%08x] "
1735 "key_hash[0x%08x] "
1736 "lmaster[%u] "
1737 "migrated_with_data[%s]\n",
1738 ctdb_db->db_name, ctdb_db->db_id,
1739 hash,
1740 ctdb_lmaster(ctdb_db->ctdb, &key),
1741 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1743 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1744 if (kd == NULL) {
1745 DEBUG(DEBUG_DEBUG, (__location__
1746 " remove_record_from_delete_queue: "
1747 "record not in queue (hash[0x%08x])\n.",
1748 hash));
1749 return;
1752 if ((kd->key.dsize != key.dsize) ||
1753 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1755 DEBUG(DEBUG_DEBUG, (__location__
1756 " remove_record_from_delete_queue: "
1757 "hash collision for key with hash[0x%08x] "
1758 "in db[%s] - skipping\n",
1759 hash, ctdb_db->db_name));
1760 return;
1763 DEBUG(DEBUG_DEBUG, (__location__
1764 " remove_record_from_delete_queue: "
1765 "removing key with hash[0x%08x]\n",
1766 hash));
1768 talloc_free(kd);
1770 return;
1774 * Insert a record into the ctdb_db context's delete queue,
1775 * handling hash collisions.
1777 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1778 const struct ctdb_ltdb_header *hdr,
1779 TDB_DATA key)
1781 struct delete_record_data *kd;
1782 uint32_t hash;
1783 int ret;
1785 hash = (uint32_t)ctdb_hash(&key);
1787 DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1788 "db_id[0x%08x] "
1789 "key_hash[0x%08x] "
1790 "lmaster[%u] "
1791 "migrated_with_data[%s]\n",
1792 ctdb_db->db_name, ctdb_db->db_id,
1793 hash,
1794 ctdb_lmaster(ctdb_db->ctdb, &key),
1795 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1797 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1798 if (kd != NULL) {
1799 if ((kd->key.dsize != key.dsize) ||
1800 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1802 DEBUG(DEBUG_INFO,
1803 (__location__ " schedule for deletion: "
1804 "hash collision for key hash [0x%08x]. "
1805 "Skipping the record.\n", hash));
1806 return 0;
1807 } else {
1808 DEBUG(DEBUG_DEBUG,
1809 (__location__ " schedule for deletion: "
1810 "updating entry for key with hash [0x%08x].\n",
1811 hash));
1815 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1816 ctdb_db->delete_queue,
1817 hdr, key);
1818 if (ret != 0) {
1819 DEBUG(DEBUG_INFO,
1820 (__location__ " schedule for deletion: error "
1821 "inserting key with hash [0x%08x] into delete queue\n",
1822 hash));
1823 return -1;
1826 return 0;
1830 * Schedule a record for deletetion.
1831 * Called from the parent context.
1833 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1834 TDB_DATA indata)
1836 struct ctdb_control_schedule_for_deletion *dd;
1837 struct ctdb_db_context *ctdb_db;
1838 int ret;
1839 TDB_DATA key;
1841 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1843 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1844 if (ctdb_db == NULL) {
1845 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1846 dd->db_id));
1847 return -1;
1850 key.dsize = dd->keylen;
1851 key.dptr = dd->key;
1853 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1855 return ret;
1858 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1859 const struct ctdb_ltdb_header *hdr,
1860 TDB_DATA key)
1862 int ret;
1863 struct ctdb_control_schedule_for_deletion *dd;
1864 TDB_DATA indata;
1865 int32_t status;
1867 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1868 /* main daemon - directly queue */
1869 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1871 return ret;
1874 /* if we dont have a connection to the daemon we can not send
1875 a control. For example sometimes from update_record control child
1876 process.
1878 if (!ctdb_db->ctdb->can_send_controls) {
1879 return -1;
1883 /* child process: send the main daemon a control */
1884 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1885 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1886 if (indata.dptr == NULL) {
1887 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1888 return -1;
1890 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1891 dd->db_id = ctdb_db->db_id;
1892 dd->hdr = *hdr;
1893 dd->keylen = key.dsize;
1894 memcpy(dd->key, key.dptr, key.dsize);
1896 ret = ctdb_control(ctdb_db->ctdb,
1897 CTDB_CURRENT_NODE,
1898 ctdb_db->db_id,
1899 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1900 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1901 indata,
1902 NULL, /* mem_ctx */
1903 NULL, /* outdata */
1904 &status,
1905 NULL, /* timeout : NULL == wait forever */
1906 NULL); /* error message */
1908 talloc_free(indata.dptr);
1910 if (ret != 0 || status != 0) {
1911 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1912 "SCHEDULE_FOR_DELETION "
1913 "control.\n"));
1914 if (status != 0) {
1915 ret = -1;
1919 return ret;
1922 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1923 const struct ctdb_ltdb_header *hdr,
1924 const TDB_DATA key)
1926 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1928 * Only remove the record from the delete queue if called
1929 * in the main daemon.
1931 return;
1934 remove_record_from_delete_queue(ctdb_db, hdr, key);
1936 return;