smbd: Simplify strict_lock_default with early returns
[Samba.git] / ctdb / server / ctdb_vacuum.c
blobce3c600bb3b3005820c0f4b071c119efcd4d4d05
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 struct ctdb_context *ctdb;
57 struct ctdb_db_context *ctdb_db;
58 struct tdb_context *dest_db;
59 trbt_tree_t *delete_list;
60 struct ctdb_marshall_buffer **vacuum_fetch_list;
61 struct timeval start;
62 bool traverse_error;
63 bool vacuum;
64 struct {
65 struct {
66 uint32_t added_to_vacuum_fetch_list;
67 uint32_t added_to_delete_list;
68 uint32_t deleted;
69 uint32_t skipped;
70 uint32_t error;
71 uint32_t total;
72 } delete_queue;
73 struct {
74 uint32_t scheduled;
75 uint32_t skipped;
76 uint32_t error;
77 uint32_t total;
78 } db_traverse;
79 struct {
80 uint32_t total;
81 uint32_t remote_error;
82 uint32_t local_error;
83 uint32_t deleted;
84 uint32_t skipped;
85 uint32_t left;
86 } delete_list;
87 struct {
88 uint32_t vacuumed;
89 uint32_t copied;
90 } repack;
91 } count;
94 /* this structure contains the information for one record to be deleted */
95 struct delete_record_data {
96 struct ctdb_context *ctdb;
97 struct ctdb_db_context *ctdb_db;
98 struct ctdb_ltdb_header hdr;
99 TDB_DATA key;
100 uint8_t keydata[1];
103 struct delete_records_list {
104 struct ctdb_marshall_buffer *records;
105 struct vacuum_data *vdata;
108 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
109 const struct ctdb_ltdb_header *hdr,
110 TDB_DATA key);
113 * Store key and header in a tree, indexed by the key hash.
115 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
116 struct ctdb_db_context *ctdb_db,
117 trbt_tree_t *tree,
118 const struct ctdb_ltdb_header *hdr,
119 TDB_DATA key)
121 struct delete_record_data *dd;
122 uint32_t hash;
123 size_t len;
125 len = offsetof(struct delete_record_data, keydata) + key.dsize;
127 dd = (struct delete_record_data *)talloc_size(tree, len);
128 if (dd == NULL) {
129 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
130 return -1;
132 talloc_set_name_const(dd, "struct delete_record_data");
134 dd->ctdb = ctdb;
135 dd->ctdb_db = ctdb_db;
136 dd->key.dsize = key.dsize;
137 dd->key.dptr = dd->keydata;
138 memcpy(dd->keydata, key.dptr, key.dsize);
140 dd->hdr = *hdr;
142 hash = ctdb_hash(&key);
144 trbt_insert32(tree, hash, dd);
146 return 0;
149 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
150 struct ctdb_ltdb_header *hdr)
152 struct ctdb_context *ctdb = vdata->ctdb;
153 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
154 uint32_t hash;
155 int ret;
157 hash = ctdb_hash(&key);
159 if (trbt_lookup32(vdata->delete_list, hash)) {
160 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
161 return 0;
164 ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
165 vdata->delete_list,
166 hdr, key);
167 if (ret != 0) {
168 return -1;
171 vdata->count.delete_list.total++;
173 return 0;
177 * Add a record to the list of records to be sent
178 * to their lmaster with VACUUM_FETCH.
180 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
181 TDB_DATA key)
183 struct ctdb_context *ctdb = vdata->ctdb;
184 struct ctdb_rec_data *rec;
185 uint32_t lmaster;
186 size_t old_size;
187 struct ctdb_marshall_buffer *vfl;
189 lmaster = ctdb_lmaster(ctdb, &key);
191 vfl = vdata->vacuum_fetch_list[lmaster];
193 rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
194 if (rec == NULL) {
195 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
196 vdata->traverse_error = true;
197 return -1;
200 old_size = talloc_get_size(vfl);
201 vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
202 if (vfl == NULL) {
203 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
204 vdata->traverse_error = true;
205 return -1;
207 vdata->vacuum_fetch_list[lmaster] = vfl;
209 vfl->count++;
210 memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
211 talloc_free(rec);
213 return 0;
217 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
218 struct timeval t, void *private_data);
220 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
222 struct ctdb_ltdb_header *header =
223 (struct ctdb_ltdb_header *)private_data;
225 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
226 return -1;
229 *header = *(struct ctdb_ltdb_header *)data.dptr;
231 return 0;
235 * traverse function for gathering the records that can be deleted
237 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
238 void *private_data)
240 struct vacuum_data *vdata = talloc_get_type(private_data,
241 struct vacuum_data);
242 struct ctdb_context *ctdb = vdata->ctdb;
243 struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
244 uint32_t lmaster;
245 struct ctdb_ltdb_header *hdr;
246 int res = 0;
248 vdata->count.db_traverse.total++;
250 lmaster = ctdb_lmaster(ctdb, &key);
251 if (lmaster >= ctdb->num_nodes) {
252 vdata->count.db_traverse.error++;
253 DEBUG(DEBUG_CRIT, (__location__
254 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
255 " with hash[%u]!\n",
256 (unsigned)lmaster,
257 (unsigned)ctdb->num_nodes,
258 (unsigned)ctdb_hash(&key)));
259 return -1;
262 if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
263 /* it is not a deleted record */
264 vdata->count.db_traverse.skipped++;
265 return 0;
268 hdr = (struct ctdb_ltdb_header *)data.dptr;
270 if (hdr->dmaster != ctdb->pnn) {
271 vdata->count.db_traverse.skipped++;
272 return 0;
276 * Add the record to this process's delete_queue for processing
277 * in the subsequent traverse in the fast vacuum run.
279 res = insert_record_into_delete_queue(ctdb_db, hdr, key);
280 if (res != 0) {
281 vdata->count.db_traverse.error++;
282 } else {
283 vdata->count.db_traverse.scheduled++;
286 return 0;
290 * traverse the tree of records to delete and marshall them into
291 * a blob
293 static int delete_marshall_traverse(void *param, void *data)
295 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
296 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
297 struct ctdb_rec_data *rec;
298 size_t old_size;
300 rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
301 if (rec == NULL) {
302 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
303 return 0;
306 old_size = talloc_get_size(recs->records);
307 recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
308 if (recs->records == NULL) {
309 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
310 return 0;
312 recs->records->count++;
313 memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
314 return 0;
318 * Variant of delete_marshall_traverse() that bumps the
319 * RSN of each traversed record in the database.
321 * This is needed to ensure that when rolling out our
322 * empty record copy before remote deletion, we as the
323 * record's dmaster keep a higher RSN than the non-dmaster
324 * nodes. This is needed to prevent old copies from
325 * resurrection in recoveries.
327 static int delete_marshall_traverse_first(void *param, void *data)
329 struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
330 struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
331 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
332 struct ctdb_context *ctdb = ctdb_db->ctdb;
333 struct ctdb_ltdb_header header;
334 uint32_t lmaster;
335 uint32_t hash = ctdb_hash(&(dd->key));
336 int res;
338 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
339 if (res != 0) {
340 DEBUG(DEBUG_ERR,
341 (__location__ " Error getting chainlock on record with "
342 "key hash [0x%08x] on database db[%s].\n",
343 hash, ctdb_db->db_name));
344 recs->vdata->count.delete_list.skipped++;
345 recs->vdata->count.delete_list.left--;
346 talloc_free(dd);
347 return 0;
351 * Verify that the record is still empty, its RSN has not
352 * changed and that we are still its lmaster and dmaster.
355 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
356 vacuum_record_parser, &header);
357 if (res != 0) {
358 goto skip;
361 if (header.flags & CTDB_REC_RO_FLAGS) {
362 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
363 "on database db[%s] has read-only flags. "
364 "skipping.\n",
365 hash, ctdb_db->db_name));
366 goto skip;
369 if (header.dmaster != ctdb->pnn) {
370 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
371 "on database db[%s] has been migrated away. "
372 "skipping.\n",
373 hash, ctdb_db->db_name));
374 goto skip;
377 if (header.rsn != dd->hdr.rsn) {
378 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
379 "on database db[%s] seems to have been "
380 "migrated away and back again (with empty "
381 "data). skipping.\n",
382 hash, ctdb_db->db_name));
383 goto skip;
386 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
388 if (lmaster != ctdb->pnn) {
389 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
390 "delete list (key hash [0x%08x], db[%s]). "
391 "Strange! skipping.\n",
392 hash, ctdb_db->db_name));
393 goto skip;
397 * Increment the record's RSN to ensure the dmaster (i.e. the current
398 * node) has the highest RSN of the record in the cluster.
399 * This is to prevent old record copies from resurrecting in recoveries
400 * if something should fail during the deletion process.
401 * Note that ctdb_ltdb_store_server() increments the RSN if called
402 * on the record's dmaster.
405 res = ctdb_ltdb_store(ctdb_db, dd->key, &header, tdb_null);
406 if (res != 0) {
407 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
408 "key hash [0x%08x] on database db[%s].\n",
409 hash, ctdb_db->db_name));
410 goto skip;
413 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
415 goto done;
417 skip:
418 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
420 recs->vdata->count.delete_list.skipped++;
421 recs->vdata->count.delete_list.left--;
422 talloc_free(dd);
423 dd = NULL;
425 done:
426 if (dd == NULL) {
427 return 0;
430 return delete_marshall_traverse(param, data);
434 * traverse function for the traversal of the delete_queue,
435 * the fast-path vacuuming list.
437 * - If the record has been migrated off the node
438 * or has been revived (filled with data) on the node,
439 * then skip the record.
441 * - If the current node is the record's lmaster and it is
442 * a record that has never been migrated with data, then
443 * delete the record from the local tdb.
445 * - If the current node is the record's lmaster and it has
446 * been migrated with data, then schedule it for the normal
447 * vacuuming procedure (i.e. add it to the delete_list).
449 * - If the current node is NOT the record's lmaster then
450 * add it to the list of records that are to be sent to
451 * the lmaster with the VACUUM_FETCH message.
453 static int delete_queue_traverse(void *param, void *data)
455 struct delete_record_data *dd =
456 talloc_get_type(data, struct delete_record_data);
457 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
458 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
459 struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
460 int res;
461 struct ctdb_ltdb_header header;
462 uint32_t lmaster;
463 uint32_t hash = ctdb_hash(&(dd->key));
465 vdata->count.delete_queue.total++;
467 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
468 if (res != 0) {
469 DEBUG(DEBUG_ERR,
470 (__location__ " Error getting chainlock on record with "
471 "key hash [0x%08x] on database db[%s].\n",
472 hash, ctdb_db->db_name));
473 vdata->count.delete_queue.error++;
474 return 0;
477 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
478 vacuum_record_parser, &header);
479 if (res != 0) {
480 goto skipped;
483 if (header.dmaster != ctdb->pnn) {
484 /* The record has been migrated off the node. Skip. */
485 goto skipped;
488 if (header.rsn != dd->hdr.rsn) {
490 * The record has been migrated off the node and back again.
491 * But not requeued for deletion. Skip it.
493 goto skipped;
497 * We are dmaster, and the record has no data, and it has
498 * not been migrated after it has been queued for deletion.
500 * At this stage, the record could still have been revived locally
501 * and last been written with empty data. This can only be
502 * fixed with the addition of an active or delete flag. (TODO)
505 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
507 if (lmaster != ctdb->pnn) {
508 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
510 if (res != 0) {
511 DEBUG(DEBUG_ERR,
512 (__location__ " Error adding record to list "
513 "of records to send to lmaster.\n"));
514 vdata->count.delete_queue.error++;
515 } else {
516 vdata->count.delete_queue.added_to_vacuum_fetch_list++;
518 goto done;
521 /* use header->flags or dd->hdr.flags ?? */
522 if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
523 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
525 if (res != 0) {
526 DEBUG(DEBUG_ERR,
527 (__location__ " Error adding record to list "
528 "of records for deletion on lmaster.\n"));
529 vdata->count.delete_queue.error++;
530 } else {
531 vdata->count.delete_queue.added_to_delete_list++;
533 } else {
534 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
536 if (res != 0) {
537 DEBUG(DEBUG_ERR,
538 (__location__ " Error deleting record with key "
539 "hash [0x%08x] from local data base db[%s].\n",
540 hash, ctdb_db->db_name));
541 vdata->count.delete_queue.error++;
542 goto done;
545 DEBUG(DEBUG_DEBUG,
546 (__location__ " Deleted record with key hash "
547 "[0x%08x] from local data base db[%s].\n",
548 hash, ctdb_db->db_name));
549 vdata->count.delete_queue.deleted++;
552 goto done;
554 skipped:
555 vdata->count.delete_queue.skipped++;
557 done:
558 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
560 return 0;
564 * Delete the records that we are lmaster and dmaster for and
565 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
566 * control.
568 static int delete_record_traverse(void *param, void *data)
570 struct delete_record_data *dd =
571 talloc_get_type(data, struct delete_record_data);
572 struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
573 struct ctdb_db_context *ctdb_db = dd->ctdb_db;
574 struct ctdb_context *ctdb = ctdb_db->ctdb;
575 int res;
576 struct ctdb_ltdb_header header;
577 uint32_t lmaster;
578 uint32_t hash = ctdb_hash(&(dd->key));
580 res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
581 if (res != 0) {
582 DEBUG(DEBUG_ERR,
583 (__location__ " Error getting chainlock on record with "
584 "key hash [0x%08x] on database db[%s].\n",
585 hash, ctdb_db->db_name));
586 vdata->count.delete_list.local_error++;
587 vdata->count.delete_list.left--;
588 talloc_free(dd);
589 return 0;
593 * Verify that the record is still empty, its RSN has not
594 * changed and that we are still its lmaster and dmaster.
597 res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
598 vacuum_record_parser, &header);
599 if (res != 0) {
600 goto skip;
603 if (header.flags & CTDB_REC_RO_FLAGS) {
604 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
605 "on database db[%s] has read-only flags. "
606 "skipping.\n",
607 hash, ctdb_db->db_name));
608 goto skip;
611 if (header.dmaster != ctdb->pnn) {
612 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
613 "on database db[%s] has been migrated away. "
614 "skipping.\n",
615 hash, ctdb_db->db_name));
616 goto skip;
619 if (header.rsn != dd->hdr.rsn + 1) {
621 * The record has been migrated off the node and back again.
622 * But not requeued for deletion. Skip it.
623 * (Note that the first marshall traverse has bumped the RSN
624 * on disk.)
626 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
627 "on database db[%s] seems to have been "
628 "migrated away and back again (with empty "
629 "data). skipping.\n",
630 hash, ctdb_db->db_name));
631 goto skip;
634 lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
636 if (lmaster != ctdb->pnn) {
637 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
638 "delete list (key hash [0x%08x], db[%s]). "
639 "Strange! skipping.\n",
640 hash, ctdb_db->db_name));
641 goto skip;
644 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
646 if (res != 0) {
647 DEBUG(DEBUG_ERR,
648 (__location__ " Error deleting record with key hash "
649 "[0x%08x] from local data base db[%s].\n",
650 hash, ctdb_db->db_name));
651 vdata->count.delete_list.local_error++;
652 goto done;
655 DEBUG(DEBUG_DEBUG,
656 (__location__ " Deleted record with key hash [0x%08x] from "
657 "local data base db[%s].\n", hash, ctdb_db->db_name));
659 vdata->count.delete_list.deleted++;
660 goto done;
662 skip:
663 vdata->count.delete_list.skipped++;
665 done:
666 tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
668 talloc_free(dd);
669 vdata->count.delete_list.left--;
671 return 0;
675 * Traverse the delete_queue.
676 * Records are either deleted directly or filled
677 * into the delete list or the vacuum fetch lists
678 * for further processing.
680 static void ctdb_process_delete_queue(struct ctdb_db_context *ctdb_db,
681 struct vacuum_data *vdata)
683 uint32_t sum;
684 int ret;
686 ret = trbt_traversearray32(ctdb_db->delete_queue, 1,
687 delete_queue_traverse, vdata);
689 if (ret != 0) {
690 DEBUG(DEBUG_ERR, (__location__ " Error traversing "
691 "the delete queue.\n"));
694 sum = vdata->count.delete_queue.deleted
695 + vdata->count.delete_queue.skipped
696 + vdata->count.delete_queue.error
697 + vdata->count.delete_queue.added_to_delete_list
698 + vdata->count.delete_queue.added_to_vacuum_fetch_list;
700 if (vdata->count.delete_queue.total != sum) {
701 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
702 "counts for db[%s]: total[%u] != sum[%u]\n",
703 ctdb_db->db_name,
704 (unsigned)vdata->count.delete_queue.total,
705 (unsigned)sum));
708 if (vdata->count.delete_queue.total > 0) {
709 DEBUG(DEBUG_INFO,
710 (__location__
711 " fast vacuuming delete_queue traverse statistics: "
712 "db[%s] "
713 "total[%u] "
714 "del[%u] "
715 "skp[%u] "
716 "err[%u] "
717 "adl[%u] "
718 "avf[%u]\n",
719 ctdb_db->db_name,
720 (unsigned)vdata->count.delete_queue.total,
721 (unsigned)vdata->count.delete_queue.deleted,
722 (unsigned)vdata->count.delete_queue.skipped,
723 (unsigned)vdata->count.delete_queue.error,
724 (unsigned)vdata->count.delete_queue.added_to_delete_list,
725 (unsigned)vdata->count.delete_queue.added_to_vacuum_fetch_list));
728 return;
732 * read-only traverse of the database, looking for records that
733 * might be able to be vacuumed.
735 * This is not done each time but only every tunable
736 * VacuumFastPathCount times.
738 static void ctdb_vacuum_traverse_db(struct ctdb_db_context *ctdb_db,
739 struct vacuum_data *vdata)
741 int ret;
743 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
744 if (ret == -1 || vdata->traverse_error) {
745 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
746 "'%s'\n", ctdb_db->db_name));
747 return;
750 if (vdata->count.db_traverse.total > 0) {
751 DEBUG(DEBUG_INFO,
752 (__location__
753 " full vacuuming db traverse statistics: "
754 "db[%s] "
755 "total[%u] "
756 "skp[%u] "
757 "err[%u] "
758 "sched[%u]\n",
759 ctdb_db->db_name,
760 (unsigned)vdata->count.db_traverse.total,
761 (unsigned)vdata->count.db_traverse.skipped,
762 (unsigned)vdata->count.db_traverse.error,
763 (unsigned)vdata->count.db_traverse.scheduled));
766 return;
770 * Process the vacuum fetch lists:
771 * For records for which we are not the lmaster, tell the lmaster to
772 * fetch the record.
774 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
775 struct vacuum_data *vdata)
777 int i;
778 struct ctdb_context *ctdb = ctdb_db->ctdb;
780 for (i = 0; i < ctdb->num_nodes; i++) {
781 TDB_DATA data;
782 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
784 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
785 continue;
788 if (vfl->count == 0) {
789 continue;
792 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
793 vfl->count, ctdb->nodes[i]->pnn,
794 ctdb_db->db_name));
796 data.dsize = talloc_get_size(vfl);
797 data.dptr = (void *)vfl;
798 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
799 CTDB_SRVID_VACUUM_FETCH,
800 data) != 0)
802 DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
803 "fetch message to %u\n",
804 ctdb->nodes[i]->pnn));
808 return;
812 * Process the delete list:
814 * This is the last step of vacuuming that consistently deletes
815 * those records that have been migrated with data and can hence
816 * not be deleted when leaving a node.
818 * In this step, the lmaster does the final deletion of those empty
819 * records that it is also dmaster for. It has ususally received
820 * at least some of these records previously from the former dmasters
821 * with the vacuum fetch message.
823 * This last step is implemented as a 3-phase process to protect from
824 * races leading to data corruption:
826 * 1) Send the lmaster's copy to all other active nodes with the
827 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
828 * 2) Send the records that could successfully be stored remotely
829 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
830 * control. The remote notes delete their local copy.
831 * 3) The lmaster locally deletes its copies of all records that
832 * could successfully be deleted remotely in step #2.
834 static void ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
835 struct vacuum_data *vdata)
837 int ret, i;
838 struct ctdb_context *ctdb = ctdb_db->ctdb;
839 struct delete_records_list *recs;
840 TDB_DATA indata;
841 struct ctdb_node_map *nodemap;
842 uint32_t *active_nodes;
843 int num_active_nodes;
844 TALLOC_CTX *tmp_ctx;
845 uint32_t sum;
847 if (vdata->count.delete_list.total == 0) {
848 return;
851 tmp_ctx = talloc_new(vdata);
852 if (tmp_ctx == NULL) {
853 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
854 return;
857 vdata->count.delete_list.left = vdata->count.delete_list.total;
860 * get the list of currently active nodes
863 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
864 CTDB_CURRENT_NODE,
865 tmp_ctx,
866 &nodemap);
867 if (ret != 0) {
868 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
869 goto done;
872 active_nodes = list_of_active_nodes(ctdb, nodemap,
873 nodemap, /* talloc context */
874 false /* include self */);
875 /* yuck! ;-) */
876 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
879 * Now delete the records all active nodes in a three-phase process:
880 * 1) send all active remote nodes the current empty copy with this
881 * node as DMASTER
882 * 2) if all nodes could store the new copy,
883 * tell all the active remote nodes to delete all their copy
884 * 3) if all remote nodes deleted their record copy, delete it locally
888 * Step 1:
889 * Send currently empty record copy to all active nodes for storing.
892 recs = talloc_zero(tmp_ctx, struct delete_records_list);
893 if (recs == NULL) {
894 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
895 goto done;
897 recs->records = (struct ctdb_marshall_buffer *)
898 talloc_zero_size(recs,
899 offsetof(struct ctdb_marshall_buffer, data));
900 if (recs->records == NULL) {
901 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
902 goto done;
904 recs->records->db_id = ctdb_db->db_id;
905 recs->vdata = vdata;
908 * traverse the tree of all records we want to delete and
909 * create a blob we can send to the other nodes.
911 * We call delete_marshall_traverse_first() to bump the
912 * records' RSNs in the database, to ensure we (as dmaster)
913 * keep the highest RSN of the records in the cluster.
915 ret = trbt_traversearray32(vdata->delete_list, 1,
916 delete_marshall_traverse_first, recs);
917 if (ret != 0) {
918 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
919 "delete list for first marshalling.\n"));
922 indata.dsize = talloc_get_size(recs->records);
923 indata.dptr = (void *)recs->records;
925 for (i = 0; i < num_active_nodes; i++) {
926 struct ctdb_marshall_buffer *records;
927 struct ctdb_rec_data *rec;
928 int32_t res;
929 TDB_DATA outdata;
931 ret = ctdb_control(ctdb, active_nodes[i], 0,
932 CTDB_CONTROL_RECEIVE_RECORDS, 0,
933 indata, recs, &outdata, &res,
934 NULL, NULL);
935 if (ret != 0 || res != 0) {
936 DEBUG(DEBUG_ERR, ("Error storing record copies on "
937 "node %u: ret[%d] res[%d]\n",
938 active_nodes[i], ret, res));
939 goto done;
943 * outdata contains the list of records coming back
944 * from the node: These are the records that the
945 * remote node could not store. We remove these from
946 * the list to process further.
948 records = (struct ctdb_marshall_buffer *)outdata.dptr;
949 rec = (struct ctdb_rec_data *)&records->data[0];
950 while (records->count-- > 1) {
951 TDB_DATA reckey, recdata;
952 struct ctdb_ltdb_header *rechdr;
953 struct delete_record_data *dd;
955 reckey.dptr = &rec->data[0];
956 reckey.dsize = rec->keylen;
957 recdata.dptr = &rec->data[reckey.dsize];
958 recdata.dsize = rec->datalen;
960 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
961 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
962 goto done;
964 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
965 recdata.dptr += sizeof(*rechdr);
966 recdata.dsize -= sizeof(*rechdr);
968 dd = (struct delete_record_data *)trbt_lookup32(
969 vdata->delete_list,
970 ctdb_hash(&reckey));
971 if (dd != NULL) {
973 * The other node could not store the record
974 * copy and it is the first node that failed.
975 * So we should remove it from the tree and
976 * update statistics.
978 talloc_free(dd);
979 vdata->count.delete_list.remote_error++;
980 vdata->count.delete_list.left--;
981 } else {
982 DEBUG(DEBUG_ERR, (__location__ " Failed to "
983 "find record with hash 0x%08x coming "
984 "back from RECEIVE_RECORDS "
985 "control in delete list.\n",
986 ctdb_hash(&reckey)));
987 vdata->count.delete_list.local_error++;
988 vdata->count.delete_list.left--;
991 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
995 if (vdata->count.delete_list.left == 0) {
996 goto success;
1000 * Step 2:
1001 * Send the remaining records to all active nodes for deletion.
1003 * The lmaster's (i.e. our) copies of these records have been stored
1004 * successfully on the other nodes.
1008 * Create a marshall blob from the remaining list of records to delete.
1011 talloc_free(recs->records);
1013 recs->records = (struct ctdb_marshall_buffer *)
1014 talloc_zero_size(recs,
1015 offsetof(struct ctdb_marshall_buffer, data));
1016 if (recs->records == NULL) {
1017 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1018 goto done;
1020 recs->records->db_id = ctdb_db->db_id;
1022 ret = trbt_traversearray32(vdata->delete_list, 1,
1023 delete_marshall_traverse, recs);
1024 if (ret != 0) {
1025 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1026 "delete list for second marshalling.\n"));
1029 indata.dsize = talloc_get_size(recs->records);
1030 indata.dptr = (void *)recs->records;
1032 for (i = 0; i < num_active_nodes; i++) {
1033 struct ctdb_marshall_buffer *records;
1034 struct ctdb_rec_data *rec;
1035 int32_t res;
1036 TDB_DATA outdata;
1038 ret = ctdb_control(ctdb, active_nodes[i], 0,
1039 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1040 indata, recs, &outdata, &res,
1041 NULL, NULL);
1042 if (ret != 0 || res != 0) {
1043 DEBUG(DEBUG_ERR, ("Failed to delete records on "
1044 "node %u: ret[%d] res[%d]\n",
1045 active_nodes[i], ret, res));
1046 goto done;
1050 * outdata contains the list of records coming back
1051 * from the node: These are the records that the
1052 * remote node could not delete. We remove these from
1053 * the list to delete locally.
1055 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1056 rec = (struct ctdb_rec_data *)&records->data[0];
1057 while (records->count-- > 1) {
1058 TDB_DATA reckey, recdata;
1059 struct ctdb_ltdb_header *rechdr;
1060 struct delete_record_data *dd;
1062 reckey.dptr = &rec->data[0];
1063 reckey.dsize = rec->keylen;
1064 recdata.dptr = &rec->data[reckey.dsize];
1065 recdata.dsize = rec->datalen;
1067 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1068 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1069 goto done;
1071 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1072 recdata.dptr += sizeof(*rechdr);
1073 recdata.dsize -= sizeof(*rechdr);
1075 dd = (struct delete_record_data *)trbt_lookup32(
1076 vdata->delete_list,
1077 ctdb_hash(&reckey));
1078 if (dd != NULL) {
1080 * The other node could not delete the
1081 * record and it is the first node that
1082 * failed. So we should remove it from
1083 * the tree and update statistics.
1085 talloc_free(dd);
1086 vdata->count.delete_list.remote_error++;
1087 vdata->count.delete_list.left--;
1088 } else {
1089 DEBUG(DEBUG_ERR, (__location__ " Failed to "
1090 "find record with hash 0x%08x coming "
1091 "back from TRY_DELETE_RECORDS "
1092 "control in delete list.\n",
1093 ctdb_hash(&reckey)));
1094 vdata->count.delete_list.local_error++;
1095 vdata->count.delete_list.left--;
1098 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1102 if (vdata->count.delete_list.left == 0) {
1103 goto success;
1107 * Step 3:
1108 * Delete the remaining records locally.
1110 * These records have successfully been deleted on all
1111 * active remote nodes.
1114 ret = trbt_traversearray32(vdata->delete_list, 1,
1115 delete_record_traverse, vdata);
1116 if (ret != 0) {
1117 DEBUG(DEBUG_ERR, (__location__ " Error traversing the "
1118 "delete list for deletion.\n"));
1121 success:
1123 if (vdata->count.delete_list.left != 0) {
1124 DEBUG(DEBUG_ERR, (__location__ " Vaccum db[%s] error: "
1125 "there are %u records left for deletion after "
1126 "processing delete list\n",
1127 ctdb_db->db_name,
1128 (unsigned)vdata->count.delete_list.left));
1131 sum = vdata->count.delete_list.deleted
1132 + vdata->count.delete_list.skipped
1133 + vdata->count.delete_list.remote_error
1134 + vdata->count.delete_list.local_error
1135 + vdata->count.delete_list.left;
1137 if (vdata->count.delete_list.total != sum) {
1138 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in vacuum "
1139 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1140 ctdb_db->db_name,
1141 (unsigned)vdata->count.delete_list.total,
1142 (unsigned)sum));
1145 if (vdata->count.delete_list.total > 0) {
1146 DEBUG(DEBUG_INFO,
1147 (__location__
1148 " vacuum delete list statistics: "
1149 "db[%s] "
1150 "total[%u] "
1151 "del[%u] "
1152 "skip[%u] "
1153 "rem.err[%u] "
1154 "loc.err[%u] "
1155 "left[%u]\n",
1156 ctdb_db->db_name,
1157 (unsigned)vdata->count.delete_list.total,
1158 (unsigned)vdata->count.delete_list.deleted,
1159 (unsigned)vdata->count.delete_list.skipped,
1160 (unsigned)vdata->count.delete_list.remote_error,
1161 (unsigned)vdata->count.delete_list.local_error,
1162 (unsigned)vdata->count.delete_list.left));
1165 done:
1166 talloc_free(tmp_ctx);
1168 return;
1172 * initialize the vacuum_data
1174 static struct vacuum_data *ctdb_vacuum_init_vacuum_data(
1175 struct ctdb_db_context *ctdb_db,
1176 TALLOC_CTX *mem_ctx)
1178 int i;
1179 struct ctdb_context *ctdb = ctdb_db->ctdb;
1180 struct vacuum_data *vdata;
1182 vdata = talloc_zero(mem_ctx, struct vacuum_data);
1183 if (vdata == NULL) {
1184 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1185 return NULL;
1188 vdata->ctdb = ctdb_db->ctdb;
1189 vdata->ctdb_db = ctdb_db;
1190 vdata->delete_list = trbt_create(vdata, 0);
1191 if (vdata->delete_list == NULL) {
1192 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1193 goto fail;
1196 vdata->start = timeval_current();
1198 vdata->count.delete_queue.added_to_delete_list = 0;
1199 vdata->count.delete_queue.added_to_vacuum_fetch_list = 0;
1200 vdata->count.delete_queue.deleted = 0;
1201 vdata->count.delete_queue.skipped = 0;
1202 vdata->count.delete_queue.error = 0;
1203 vdata->count.delete_queue.total = 0;
1204 vdata->count.db_traverse.scheduled = 0;
1205 vdata->count.db_traverse.skipped = 0;
1206 vdata->count.db_traverse.error = 0;
1207 vdata->count.db_traverse.total = 0;
1208 vdata->count.delete_list.total = 0;
1209 vdata->count.delete_list.left = 0;
1210 vdata->count.delete_list.remote_error = 0;
1211 vdata->count.delete_list.local_error = 0;
1212 vdata->count.delete_list.skipped = 0;
1213 vdata->count.delete_list.deleted = 0;
1215 /* the list needs to be of length num_nodes */
1216 vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1217 struct ctdb_marshall_buffer *,
1218 ctdb->num_nodes);
1219 if (vdata->vacuum_fetch_list == NULL) {
1220 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1221 goto fail;
1223 for (i = 0; i < ctdb->num_nodes; i++) {
1224 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1225 talloc_zero_size(vdata->vacuum_fetch_list,
1226 offsetof(struct ctdb_marshall_buffer, data));
1227 if (vdata->vacuum_fetch_list[i] == NULL) {
1228 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1229 talloc_free(vdata);
1230 return NULL;
1232 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1235 return vdata;
1237 fail:
1238 talloc_free(vdata);
1239 return NULL;
1243 * Vacuum a DB:
1244 * - Always do the fast vacuuming run, which traverses
1245 * the in-memory delete queue: these records have been
1246 * scheduled for deletion.
1247 * - Only if explicitly requested, the database is traversed
1248 * in order to use the traditional heuristics on empty records
1249 * to trigger deletion.
1250 * This is done only every VacuumFastPathCount'th vacuuming run.
1252 * The traverse runs fill two lists:
1254 * - The delete_list:
1255 * This is the list of empty records the current
1256 * node is lmaster and dmaster for. These records are later
1257 * deleted first on other nodes and then locally.
1259 * The fast vacuuming run has a short cut for those records
1260 * that have never been migrated with data: these records
1261 * are immediately deleted locally, since they have left
1262 * no trace on other nodes.
1264 * - The vacuum_fetch lists
1265 * (one for each other lmaster node):
1266 * The records in this list are sent for deletion to
1267 * their lmaster in a bulk VACUUM_FETCH message.
1269 * The lmaster then migrates all these records to itelf
1270 * so that they can be vacuumed there.
1272 * This executes in the child context.
1274 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1275 bool full_vacuum_run)
1277 struct ctdb_context *ctdb = ctdb_db->ctdb;
1278 int ret, pnn;
1279 struct vacuum_data *vdata;
1280 TALLOC_CTX *tmp_ctx;
1282 DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1283 "%s db_id[0x%08x]\n",
1284 full_vacuum_run ? "full" : "fast",
1285 ctdb_db->db_name, ctdb_db->db_id));
1287 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1288 if (ret != 0) {
1289 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1290 return ret;
1293 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1294 if (pnn == -1) {
1295 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1296 return -1;
1299 ctdb->pnn = pnn;
1301 tmp_ctx = talloc_new(ctdb_db);
1302 if (tmp_ctx == NULL) {
1303 DEBUG(DEBUG_ERR, ("Out of memory!\n"));
1304 return -1;
1307 vdata = ctdb_vacuum_init_vacuum_data(ctdb_db, tmp_ctx);
1308 if (vdata == NULL) {
1309 talloc_free(tmp_ctx);
1310 return -1;
1313 if (full_vacuum_run) {
1314 ctdb_vacuum_traverse_db(ctdb_db, vdata);
1317 ctdb_process_delete_queue(ctdb_db, vdata);
1319 ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1321 ctdb_process_delete_list(ctdb_db, vdata);
1323 talloc_free(tmp_ctx);
1325 /* this ensures we run our event queue */
1326 ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1328 return 0;
1332 * repack and vaccum a db
1333 * called from the child context
1335 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1336 bool full_vacuum_run)
1338 uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1339 const char *name = ctdb_db->db_name;
1340 int freelist_size = 0;
1341 int ret;
1343 if (ctdb_vacuum_db(ctdb_db, full_vacuum_run) != 0) {
1344 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1347 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1348 if (freelist_size == -1) {
1349 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1350 return -1;
1354 * decide if a repack is necessary
1356 if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit))
1358 return 0;
1361 DEBUG(DEBUG_INFO, ("Repacking %s with %u freelist entries\n",
1362 name, freelist_size));
1364 ret = tdb_repack(ctdb_db->ltdb->tdb);
1365 if (ret != 0) {
1366 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1367 return -1;
1370 return 0;
1373 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1375 uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1377 return interval;
1380 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1382 double l = timeval_elapsed(&child_ctx->start_time);
1383 struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1384 struct ctdb_context *ctdb = ctdb_db->ctdb;
1386 DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1388 if (child_ctx->child_pid != -1) {
1389 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1390 } else {
1391 /* Bump the number of successful fast-path runs. */
1392 child_ctx->vacuum_handle->fast_path_count++;
1395 DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1397 event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1398 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1399 ctdb_vacuum_event, child_ctx->vacuum_handle);
1401 return 0;
1405 * this event is generated when a vacuum child process times out
1407 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1408 struct timeval t, void *private_data)
1410 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1412 DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1414 child_ctx->status = VACUUM_TIMEOUT;
1416 talloc_free(child_ctx);
1421 * this event is generated when a vacuum child process has completed
1423 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1424 uint16_t flags, void *private_data)
1426 struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1427 char c = 0;
1428 int ret;
1430 DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1431 child_ctx->child_pid = -1;
1433 ret = read(child_ctx->fd[0], &c, 1);
1434 if (ret != 1 || c != 0) {
1435 child_ctx->status = VACUUM_ERROR;
1436 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));
1437 } else {
1438 child_ctx->status = VACUUM_OK;
1441 talloc_free(child_ctx);
1445 * this event is called every time we need to start a new vacuum process
1447 static void
1448 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1449 struct timeval t, void *private_data)
1451 struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1452 struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1453 struct ctdb_context *ctdb = ctdb_db->ctdb;
1454 struct ctdb_vacuum_child_context *child_ctx;
1455 struct tevent_fd *fde;
1456 int ret;
1458 /* we dont vacuum if we are in recovery mode, or db frozen */
1459 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1460 ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1461 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1462 ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1463 : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1464 ? "freeze pending"
1465 : "frozen"));
1466 event_add_timed(ctdb->ev, vacuum_handle,
1467 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1468 ctdb_vacuum_event, vacuum_handle);
1469 return;
1472 child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1473 if (child_ctx == NULL) {
1474 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1475 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1479 ret = pipe(child_ctx->fd);
1480 if (ret != 0) {
1481 talloc_free(child_ctx);
1482 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1483 event_add_timed(ctdb->ev, vacuum_handle,
1484 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1485 ctdb_vacuum_event, vacuum_handle);
1486 return;
1489 if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1490 vacuum_handle->fast_path_count = 0;
1493 child_ctx->child_pid = ctdb_fork(ctdb);
1494 if (child_ctx->child_pid == (pid_t)-1) {
1495 close(child_ctx->fd[0]);
1496 close(child_ctx->fd[1]);
1497 talloc_free(child_ctx);
1498 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1499 event_add_timed(ctdb->ev, vacuum_handle,
1500 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1501 ctdb_vacuum_event, vacuum_handle);
1502 return;
1506 if (child_ctx->child_pid == 0) {
1507 char cc = 0;
1508 bool full_vacuum_run = false;
1509 close(child_ctx->fd[0]);
1511 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1512 ctdb_set_process_name("ctdb_vacuum");
1513 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1514 DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1515 _exit(1);
1518 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1519 (vacuum_handle->fast_path_count == 0))
1521 full_vacuum_run = true;
1523 cc = ctdb_vacuum_and_repack_db(ctdb_db, full_vacuum_run);
1525 write(child_ctx->fd[1], &cc, 1);
1526 _exit(0);
1529 set_close_on_exec(child_ctx->fd[0]);
1530 close(child_ctx->fd[1]);
1532 child_ctx->status = VACUUM_RUNNING;
1533 child_ctx->start_time = timeval_current();
1535 DLIST_ADD(ctdb->vacuumers, child_ctx);
1536 talloc_set_destructor(child_ctx, vacuum_child_destructor);
1539 * Clear the fastpath vacuuming list in the parent.
1541 talloc_free(ctdb_db->delete_queue);
1542 ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1543 if (ctdb_db->delete_queue == NULL) {
1544 /* fatal here? ... */
1545 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1546 "in parent context. Shutting down\n");
1549 event_add_timed(ctdb->ev, child_ctx,
1550 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1551 vacuum_child_timeout, child_ctx);
1553 DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1555 fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1556 EVENT_FD_READ, vacuum_child_handler, child_ctx);
1557 tevent_fd_set_auto_close(fde);
1559 vacuum_handle->child_ctx = child_ctx;
1560 child_ctx->vacuum_handle = vacuum_handle;
1563 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1565 /* Simply free them all. */
1566 while (ctdb->vacuumers) {
1567 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1568 ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1569 (int)ctdb->vacuumers->child_pid));
1570 /* vacuum_child_destructor kills it, removes from list */
1571 talloc_free(ctdb->vacuumers);
1575 /* this function initializes the vacuuming context for a database
1576 * starts the vacuuming events
1578 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1580 if (ctdb_db->persistent != 0) {
1581 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1582 return 0;
1585 ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1586 CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1588 ctdb_db->vacuum_handle->ctdb_db = ctdb_db;
1589 ctdb_db->vacuum_handle->fast_path_count = 0;
1591 event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle,
1592 timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1593 ctdb_vacuum_event, ctdb_db->vacuum_handle);
1595 return 0;
1598 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1599 const struct ctdb_ltdb_header *hdr,
1600 const TDB_DATA key)
1602 struct delete_record_data *kd;
1603 uint32_t hash;
1605 hash = (uint32_t)ctdb_hash(&key);
1607 DEBUG(DEBUG_DEBUG, (__location__
1608 " remove_record_from_delete_queue: "
1609 "db[%s] "
1610 "db_id[0x%08x] "
1611 "key_hash[0x%08x] "
1612 "lmaster[%u] "
1613 "migrated_with_data[%s]\n",
1614 ctdb_db->db_name, ctdb_db->db_id,
1615 hash,
1616 ctdb_lmaster(ctdb_db->ctdb, &key),
1617 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1619 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1620 if (kd == NULL) {
1621 DEBUG(DEBUG_DEBUG, (__location__
1622 " remove_record_from_delete_queue: "
1623 "record not in queue (hash[0x%08x])\n.",
1624 hash));
1625 return;
1628 if ((kd->key.dsize != key.dsize) ||
1629 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1631 DEBUG(DEBUG_DEBUG, (__location__
1632 " remove_record_from_delete_queue: "
1633 "hash collision for key with hash[0x%08x] "
1634 "in db[%s] - skipping\n",
1635 hash, ctdb_db->db_name));
1636 return;
1639 DEBUG(DEBUG_DEBUG, (__location__
1640 " remove_record_from_delete_queue: "
1641 "removing key with hash[0x%08x]\n",
1642 hash));
1644 talloc_free(kd);
1646 return;
1650 * Insert a record into the ctdb_db context's delete queue,
1651 * handling hash collisions.
1653 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1654 const struct ctdb_ltdb_header *hdr,
1655 TDB_DATA key)
1657 struct delete_record_data *kd;
1658 uint32_t hash;
1659 int ret;
1661 hash = (uint32_t)ctdb_hash(&key);
1663 DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1664 "db_id[0x%08x] "
1665 "key_hash[0x%08x] "
1666 "lmaster[%u] "
1667 "migrated_with_data[%s]\n",
1668 ctdb_db->db_name, ctdb_db->db_id,
1669 hash,
1670 ctdb_lmaster(ctdb_db->ctdb, &key),
1671 hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1673 kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1674 if (kd != NULL) {
1675 if ((kd->key.dsize != key.dsize) ||
1676 (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1678 DEBUG(DEBUG_INFO,
1679 (__location__ " schedule for deletion: "
1680 "hash collision for key hash [0x%08x]. "
1681 "Skipping the record.\n", hash));
1682 return 0;
1683 } else {
1684 DEBUG(DEBUG_DEBUG,
1685 (__location__ " schedule for deletion: "
1686 "updating entry for key with hash [0x%08x].\n",
1687 hash));
1691 ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1692 ctdb_db->delete_queue,
1693 hdr, key);
1694 if (ret != 0) {
1695 DEBUG(DEBUG_INFO,
1696 (__location__ " schedule for deletion: error "
1697 "inserting key with hash [0x%08x] into delete queue\n",
1698 hash));
1699 return -1;
1702 return 0;
1706 * Schedule a record for deletetion.
1707 * Called from the parent context.
1709 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1710 TDB_DATA indata)
1712 struct ctdb_control_schedule_for_deletion *dd;
1713 struct ctdb_db_context *ctdb_db;
1714 int ret;
1715 TDB_DATA key;
1717 dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1719 ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1720 if (ctdb_db == NULL) {
1721 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1722 dd->db_id));
1723 return -1;
1726 key.dsize = dd->keylen;
1727 key.dptr = dd->key;
1729 ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1731 return ret;
1734 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1735 const struct ctdb_ltdb_header *hdr,
1736 TDB_DATA key)
1738 int ret;
1739 struct ctdb_control_schedule_for_deletion *dd;
1740 TDB_DATA indata;
1741 int32_t status;
1743 if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1744 /* main daemon - directly queue */
1745 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1747 return ret;
1750 /* if we dont have a connection to the daemon we can not send
1751 a control. For example sometimes from update_record control child
1752 process.
1754 if (!ctdb_db->ctdb->can_send_controls) {
1755 return -1;
1759 /* child process: send the main daemon a control */
1760 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1761 indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1762 if (indata.dptr == NULL) {
1763 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1764 return -1;
1766 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1767 dd->db_id = ctdb_db->db_id;
1768 dd->hdr = *hdr;
1769 dd->keylen = key.dsize;
1770 memcpy(dd->key, key.dptr, key.dsize);
1772 ret = ctdb_control(ctdb_db->ctdb,
1773 CTDB_CURRENT_NODE,
1774 ctdb_db->db_id,
1775 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1776 CTDB_CTRL_FLAG_NOREPLY, /* flags */
1777 indata,
1778 NULL, /* mem_ctx */
1779 NULL, /* outdata */
1780 &status,
1781 NULL, /* timeout : NULL == wait forever */
1782 NULL); /* error message */
1784 talloc_free(indata.dptr);
1786 if (ret != 0 || status != 0) {
1787 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1788 "SCHEDULE_FOR_DELETION "
1789 "control.\n"));
1790 if (status != 0) {
1791 ret = -1;
1795 return ret;
1798 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1799 const struct ctdb_ltdb_header *hdr,
1800 const TDB_DATA key)
1802 if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1804 * Only remove the record from the delete queue if called
1805 * in the main daemon.
1807 return;
1810 remove_record_from_delete_queue(ctdb_db, hdr, key);
1812 return;