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/>.
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.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 */
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 */
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
;
66 uint32_t added_to_vacuum_fetch_list
;
67 uint32_t added_to_delete_list
;
81 uint32_t remote_error
;
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
;
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
,
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
,
118 const struct ctdb_ltdb_header
*hdr
,
121 struct delete_record_data
*dd
;
125 len
= offsetof(struct delete_record_data
, keydata
) + key
.dsize
;
127 dd
= (struct delete_record_data
*)talloc_size(tree
, len
);
129 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
132 talloc_set_name_const(dd
, "struct delete_record_data");
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
);
142 hash
= ctdb_hash(&key
);
144 trbt_insert32(tree
, hash
, dd
);
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
;
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"));
164 ret
= insert_delete_record_data_into_tree(ctdb
, ctdb_db
,
171 vdata
->count
.delete_list
.total
++;
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
,
183 struct ctdb_context
*ctdb
= vdata
->ctdb
;
185 struct ctdb_marshall_buffer
*vfl
;
187 lmaster
= ctdb_lmaster(ctdb
, &key
);
189 vfl
= vdata
->vacuum_fetch_list
[lmaster
];
191 vfl
= ctdb_marshall_add(ctdb
, vfl
, vfl
->db_id
, ctdb
->pnn
,
192 key
, NULL
, tdb_null
);
194 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
195 vdata
->traverse_error
= true;
199 vdata
->vacuum_fetch_list
[lmaster
] = vfl
;
205 static void ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
206 struct timeval t
, void *private_data
);
208 static int vacuum_record_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
210 struct ctdb_ltdb_header
*header
=
211 (struct ctdb_ltdb_header
*)private_data
;
213 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
217 *header
= *(struct ctdb_ltdb_header
*)data
.dptr
;
223 * traverse function for gathering the records that can be deleted
225 static int vacuum_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
,
228 struct vacuum_data
*vdata
= talloc_get_type(private_data
,
230 struct ctdb_context
*ctdb
= vdata
->ctdb
;
231 struct ctdb_db_context
*ctdb_db
= vdata
->ctdb_db
;
233 struct ctdb_ltdb_header
*hdr
;
236 vdata
->count
.db_traverse
.total
++;
238 lmaster
= ctdb_lmaster(ctdb
, &key
);
239 if (lmaster
>= ctdb
->num_nodes
) {
240 vdata
->count
.db_traverse
.error
++;
241 DEBUG(DEBUG_CRIT
, (__location__
242 " lmaster[%u] >= ctdb->num_nodes[%u] for key"
245 (unsigned)ctdb
->num_nodes
,
246 (unsigned)ctdb_hash(&key
)));
250 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
251 /* it is not a deleted record */
252 vdata
->count
.db_traverse
.skipped
++;
256 hdr
= (struct ctdb_ltdb_header
*)data
.dptr
;
258 if (hdr
->dmaster
!= ctdb
->pnn
) {
259 vdata
->count
.db_traverse
.skipped
++;
264 * Add the record to this process's delete_queue for processing
265 * in the subsequent traverse in the fast vacuum run.
267 res
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
269 vdata
->count
.db_traverse
.error
++;
271 vdata
->count
.db_traverse
.scheduled
++;
278 * traverse the tree of records to delete and marshall them into
281 static int delete_marshall_traverse(void *param
, void *data
)
283 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
284 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
285 struct ctdb_marshall_buffer
*m
;
287 m
= ctdb_marshall_add(recs
, recs
->records
, recs
->records
->db_id
,
288 recs
->records
->db_id
,
289 dd
->key
, &dd
->hdr
, tdb_null
);
291 DEBUG(DEBUG_ERR
, (__location__
" failed to marshall record\n"));
300 * Variant of delete_marshall_traverse() that bumps the
301 * RSN of each traversed record in the database.
303 * This is needed to ensure that when rolling out our
304 * empty record copy before remote deletion, we as the
305 * record's dmaster keep a higher RSN than the non-dmaster
306 * nodes. This is needed to prevent old copies from
307 * resurrection in recoveries.
309 static int delete_marshall_traverse_first(void *param
, void *data
)
311 struct delete_record_data
*dd
= talloc_get_type(data
, struct delete_record_data
);
312 struct delete_records_list
*recs
= talloc_get_type(param
, struct delete_records_list
);
313 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
314 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
315 struct ctdb_ltdb_header header
;
317 uint32_t hash
= ctdb_hash(&(dd
->key
));
320 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
323 (__location__
" Error getting chainlock on record with "
324 "key hash [0x%08x] on database db[%s].\n",
325 hash
, ctdb_db
->db_name
));
326 recs
->vdata
->count
.delete_list
.skipped
++;
327 recs
->vdata
->count
.delete_list
.left
--;
333 * Verify that the record is still empty, its RSN has not
334 * changed and that we are still its lmaster and dmaster.
337 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
338 vacuum_record_parser
, &header
);
343 if (header
.flags
& CTDB_REC_RO_FLAGS
) {
344 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
345 "on database db[%s] has read-only flags. "
347 hash
, ctdb_db
->db_name
));
351 if (header
.dmaster
!= ctdb
->pnn
) {
352 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
353 "on database db[%s] has been migrated away. "
355 hash
, ctdb_db
->db_name
));
359 if (header
.rsn
!= dd
->hdr
.rsn
) {
360 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
361 "on database db[%s] seems to have been "
362 "migrated away and back again (with empty "
363 "data). skipping.\n",
364 hash
, ctdb_db
->db_name
));
368 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
370 if (lmaster
!= ctdb
->pnn
) {
371 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
372 "delete list (key hash [0x%08x], db[%s]). "
373 "Strange! skipping.\n",
374 hash
, ctdb_db
->db_name
));
379 * Increment the record's RSN to ensure the dmaster (i.e. the current
380 * node) has the highest RSN of the record in the cluster.
381 * This is to prevent old record copies from resurrecting in recoveries
382 * if something should fail during the deletion process.
383 * Note that ctdb_ltdb_store_server() increments the RSN if called
384 * on the record's dmaster.
387 res
= ctdb_ltdb_store(ctdb_db
, dd
->key
, &header
, tdb_null
);
389 DEBUG(DEBUG_ERR
, (__location__
": Failed to store record with "
390 "key hash [0x%08x] on database db[%s].\n",
391 hash
, ctdb_db
->db_name
));
395 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
400 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
402 recs
->vdata
->count
.delete_list
.skipped
++;
403 recs
->vdata
->count
.delete_list
.left
--;
412 return delete_marshall_traverse(param
, data
);
416 * traverse function for the traversal of the delete_queue,
417 * the fast-path vacuuming list.
419 * - If the record has been migrated off the node
420 * or has been revived (filled with data) on the node,
421 * then skip the record.
423 * - If the current node is the record's lmaster and it is
424 * a record that has never been migrated with data, then
425 * delete the record from the local tdb.
427 * - If the current node is the record's lmaster and it has
428 * been migrated with data, then schedule it for the normal
429 * vacuuming procedure (i.e. add it to the delete_list).
431 * - If the current node is NOT the record's lmaster then
432 * add it to the list of records that are to be sent to
433 * the lmaster with the VACUUM_FETCH message.
435 static int delete_queue_traverse(void *param
, void *data
)
437 struct delete_record_data
*dd
=
438 talloc_get_type(data
, struct delete_record_data
);
439 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
440 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
441 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
; /* or dd->ctdb ??? */
443 struct ctdb_ltdb_header header
;
445 uint32_t hash
= ctdb_hash(&(dd
->key
));
447 vdata
->count
.delete_queue
.total
++;
449 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
452 (__location__
" Error getting chainlock on record with "
453 "key hash [0x%08x] on database db[%s].\n",
454 hash
, ctdb_db
->db_name
));
455 vdata
->count
.delete_queue
.error
++;
459 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
460 vacuum_record_parser
, &header
);
465 if (header
.dmaster
!= ctdb
->pnn
) {
466 /* The record has been migrated off the node. Skip. */
470 if (header
.rsn
!= dd
->hdr
.rsn
) {
472 * The record has been migrated off the node and back again.
473 * But not requeued for deletion. Skip it.
479 * We are dmaster, and the record has no data, and it has
480 * not been migrated after it has been queued for deletion.
482 * At this stage, the record could still have been revived locally
483 * and last been written with empty data. This can only be
484 * fixed with the addition of an active or delete flag. (TODO)
487 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
489 if (lmaster
!= ctdb
->pnn
) {
490 res
= add_record_to_vacuum_fetch_list(vdata
, dd
->key
);
494 (__location__
" Error adding record to list "
495 "of records to send to lmaster.\n"));
496 vdata
->count
.delete_queue
.error
++;
498 vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
++;
503 /* use header->flags or dd->hdr.flags ?? */
504 if (dd
->hdr
.flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
) {
505 res
= add_record_to_delete_list(vdata
, dd
->key
, &dd
->hdr
);
509 (__location__
" Error adding record to list "
510 "of records for deletion on lmaster.\n"));
511 vdata
->count
.delete_queue
.error
++;
513 vdata
->count
.delete_queue
.added_to_delete_list
++;
516 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
520 (__location__
" Error deleting record with key "
521 "hash [0x%08x] from local data base db[%s].\n",
522 hash
, ctdb_db
->db_name
));
523 vdata
->count
.delete_queue
.error
++;
528 (__location__
" Deleted record with key hash "
529 "[0x%08x] from local data base db[%s].\n",
530 hash
, ctdb_db
->db_name
));
531 vdata
->count
.delete_queue
.deleted
++;
537 vdata
->count
.delete_queue
.skipped
++;
540 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
546 * Delete the records that we are lmaster and dmaster for and
547 * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
550 static int delete_record_traverse(void *param
, void *data
)
552 struct delete_record_data
*dd
=
553 talloc_get_type(data
, struct delete_record_data
);
554 struct vacuum_data
*vdata
= talloc_get_type(param
, struct vacuum_data
);
555 struct ctdb_db_context
*ctdb_db
= dd
->ctdb_db
;
556 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
558 struct ctdb_ltdb_header header
;
560 uint32_t hash
= ctdb_hash(&(dd
->key
));
562 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
565 (__location__
" Error getting chainlock on record with "
566 "key hash [0x%08x] on database db[%s].\n",
567 hash
, ctdb_db
->db_name
));
568 vdata
->count
.delete_list
.local_error
++;
569 vdata
->count
.delete_list
.left
--;
575 * Verify that the record is still empty, its RSN has not
576 * changed and that we are still its lmaster and dmaster.
579 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
580 vacuum_record_parser
, &header
);
585 if (header
.flags
& CTDB_REC_RO_FLAGS
) {
586 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
587 "on database db[%s] has read-only flags. "
589 hash
, ctdb_db
->db_name
));
593 if (header
.dmaster
!= ctdb
->pnn
) {
594 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
595 "on database db[%s] has been migrated away. "
597 hash
, ctdb_db
->db_name
));
601 if (header
.rsn
!= dd
->hdr
.rsn
+ 1) {
603 * The record has been migrated off the node and back again.
604 * But not requeued for deletion. Skip it.
605 * (Note that the first marshall traverse has bumped the RSN
608 DEBUG(DEBUG_INFO
, (__location__
": record with hash [0x%08x] "
609 "on database db[%s] seems to have been "
610 "migrated away and back again (with empty "
611 "data). skipping.\n",
612 hash
, ctdb_db
->db_name
));
616 lmaster
= ctdb_lmaster(ctdb_db
->ctdb
, &dd
->key
);
618 if (lmaster
!= ctdb
->pnn
) {
619 DEBUG(DEBUG_INFO
, (__location__
": not lmaster for record in "
620 "delete list (key hash [0x%08x], db[%s]). "
621 "Strange! skipping.\n",
622 hash
, ctdb_db
->db_name
));
626 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
630 (__location__
" Error deleting record with key hash "
631 "[0x%08x] from local data base db[%s].\n",
632 hash
, ctdb_db
->db_name
));
633 vdata
->count
.delete_list
.local_error
++;
638 (__location__
" Deleted record with key hash [0x%08x] from "
639 "local data base db[%s].\n", hash
, ctdb_db
->db_name
));
641 vdata
->count
.delete_list
.deleted
++;
645 vdata
->count
.delete_list
.skipped
++;
648 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
651 vdata
->count
.delete_list
.left
--;
657 * Traverse the delete_queue.
658 * Records are either deleted directly or filled
659 * into the delete list or the vacuum fetch lists
660 * for further processing.
662 static void ctdb_process_delete_queue(struct ctdb_db_context
*ctdb_db
,
663 struct vacuum_data
*vdata
)
668 ret
= trbt_traversearray32(ctdb_db
->delete_queue
, 1,
669 delete_queue_traverse
, vdata
);
672 DEBUG(DEBUG_ERR
, (__location__
" Error traversing "
673 "the delete queue.\n"));
676 sum
= vdata
->count
.delete_queue
.deleted
677 + vdata
->count
.delete_queue
.skipped
678 + vdata
->count
.delete_queue
.error
679 + vdata
->count
.delete_queue
.added_to_delete_list
680 + vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
;
682 if (vdata
->count
.delete_queue
.total
!= sum
) {
683 DEBUG(DEBUG_ERR
, (__location__
" Inconsistency in fast vacuum "
684 "counts for db[%s]: total[%u] != sum[%u]\n",
686 (unsigned)vdata
->count
.delete_queue
.total
,
690 if (vdata
->count
.delete_queue
.total
> 0) {
693 " fast vacuuming delete_queue traverse statistics: "
702 (unsigned)vdata
->count
.delete_queue
.total
,
703 (unsigned)vdata
->count
.delete_queue
.deleted
,
704 (unsigned)vdata
->count
.delete_queue
.skipped
,
705 (unsigned)vdata
->count
.delete_queue
.error
,
706 (unsigned)vdata
->count
.delete_queue
.added_to_delete_list
,
707 (unsigned)vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
));
714 * read-only traverse of the database, looking for records that
715 * might be able to be vacuumed.
717 * This is not done each time but only every tunable
718 * VacuumFastPathCount times.
720 static void ctdb_vacuum_traverse_db(struct ctdb_db_context
*ctdb_db
,
721 struct vacuum_data
*vdata
)
725 ret
= tdb_traverse_read(ctdb_db
->ltdb
->tdb
, vacuum_traverse
, vdata
);
726 if (ret
== -1 || vdata
->traverse_error
) {
727 DEBUG(DEBUG_ERR
, (__location__
" Traverse error in vacuuming "
728 "'%s'\n", ctdb_db
->db_name
));
732 if (vdata
->count
.db_traverse
.total
> 0) {
735 " full vacuuming db traverse statistics: "
742 (unsigned)vdata
->count
.db_traverse
.total
,
743 (unsigned)vdata
->count
.db_traverse
.skipped
,
744 (unsigned)vdata
->count
.db_traverse
.error
,
745 (unsigned)vdata
->count
.db_traverse
.scheduled
));
752 * Process the vacuum fetch lists:
753 * For records for which we are not the lmaster, tell the lmaster to
756 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context
*ctdb_db
,
757 struct vacuum_data
*vdata
)
760 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
762 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
764 struct ctdb_marshall_buffer
*vfl
= vdata
->vacuum_fetch_list
[i
];
766 if (ctdb
->nodes
[i
]->pnn
== ctdb
->pnn
) {
770 if (vfl
->count
== 0) {
774 DEBUG(DEBUG_INFO
, ("Found %u records for lmaster %u in '%s'\n",
775 vfl
->count
, ctdb
->nodes
[i
]->pnn
,
778 data
= ctdb_marshall_finish(vfl
);
779 if (ctdb_client_send_message(ctdb
, ctdb
->nodes
[i
]->pnn
,
780 CTDB_SRVID_VACUUM_FETCH
,
783 DEBUG(DEBUG_ERR
, (__location__
" Failed to send vacuum "
784 "fetch message to %u\n",
785 ctdb
->nodes
[i
]->pnn
));
793 * Process the delete list:
795 * This is the last step of vacuuming that consistently deletes
796 * those records that have been migrated with data and can hence
797 * not be deleted when leaving a node.
799 * In this step, the lmaster does the final deletion of those empty
800 * records that it is also dmaster for. It has ususally received
801 * at least some of these records previously from the former dmasters
802 * with the vacuum fetch message.
804 * This last step is implemented as a 3-phase process to protect from
805 * races leading to data corruption:
807 * 1) Send the lmaster's copy to all other active nodes with the
808 * RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
809 * 2) Send the records that could successfully be stored remotely
810 * in step #1 to all active nodes with the TRY_DELETE_RECORDS
811 * control. The remote notes delete their local copy.
812 * 3) The lmaster locally deletes its copies of all records that
813 * could successfully be deleted remotely in step #2.
815 static void ctdb_process_delete_list(struct ctdb_db_context
*ctdb_db
,
816 struct vacuum_data
*vdata
)
819 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
820 struct delete_records_list
*recs
;
822 struct ctdb_node_map
*nodemap
;
823 uint32_t *active_nodes
;
824 int num_active_nodes
;
828 if (vdata
->count
.delete_list
.total
== 0) {
832 tmp_ctx
= talloc_new(vdata
);
833 if (tmp_ctx
== NULL
) {
834 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
838 vdata
->count
.delete_list
.left
= vdata
->count
.delete_list
.total
;
841 * get the list of currently active nodes
844 ret
= ctdb_ctrl_getnodemap(ctdb
, TIMELIMIT(),
849 DEBUG(DEBUG_ERR
,(__location__
" unable to get node map\n"));
853 active_nodes
= list_of_active_nodes(ctdb
, nodemap
,
854 nodemap
, /* talloc context */
855 false /* include self */);
857 num_active_nodes
= talloc_get_size(active_nodes
)/sizeof(*active_nodes
);
860 * Now delete the records all active nodes in a three-phase process:
861 * 1) send all active remote nodes the current empty copy with this
863 * 2) if all nodes could store the new copy,
864 * tell all the active remote nodes to delete all their copy
865 * 3) if all remote nodes deleted their record copy, delete it locally
870 * Send currently empty record copy to all active nodes for storing.
873 recs
= talloc_zero(tmp_ctx
, struct delete_records_list
);
875 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
878 recs
->records
= (struct ctdb_marshall_buffer
*)
879 talloc_zero_size(recs
,
880 offsetof(struct ctdb_marshall_buffer
, data
));
881 if (recs
->records
== NULL
) {
882 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
885 recs
->records
->db_id
= ctdb_db
->db_id
;
889 * traverse the tree of all records we want to delete and
890 * create a blob we can send to the other nodes.
892 * We call delete_marshall_traverse_first() to bump the
893 * records' RSNs in the database, to ensure we (as dmaster)
894 * keep the highest RSN of the records in the cluster.
896 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
897 delete_marshall_traverse_first
, recs
);
899 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
900 "delete list for first marshalling.\n"));
904 indata
= ctdb_marshall_finish(recs
->records
);
906 for (i
= 0; i
< num_active_nodes
; i
++) {
907 struct ctdb_marshall_buffer
*records
;
908 struct ctdb_rec_data
*rec
;
912 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
913 CTDB_CONTROL_RECEIVE_RECORDS
, 0,
914 indata
, recs
, &outdata
, &res
,
916 if (ret
!= 0 || res
!= 0) {
917 DEBUG(DEBUG_ERR
, ("Error storing record copies on "
918 "node %u: ret[%d] res[%d]\n",
919 active_nodes
[i
], ret
, res
));
924 * outdata contains the list of records coming back
925 * from the node: These are the records that the
926 * remote node could not store. We remove these from
927 * the list to process further.
929 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
930 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
931 while (records
->count
-- > 1) {
932 TDB_DATA reckey
, recdata
;
933 struct ctdb_ltdb_header
*rechdr
;
934 struct delete_record_data
*dd
;
936 reckey
.dptr
= &rec
->data
[0];
937 reckey
.dsize
= rec
->keylen
;
938 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
939 recdata
.dsize
= rec
->datalen
;
941 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
942 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
945 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
946 recdata
.dptr
+= sizeof(*rechdr
);
947 recdata
.dsize
-= sizeof(*rechdr
);
949 dd
= (struct delete_record_data
*)trbt_lookup32(
954 * The other node could not store the record
955 * copy and it is the first node that failed.
956 * So we should remove it from the tree and
960 vdata
->count
.delete_list
.remote_error
++;
961 vdata
->count
.delete_list
.left
--;
963 DEBUG(DEBUG_ERR
, (__location__
" Failed to "
964 "find record with hash 0x%08x coming "
965 "back from RECEIVE_RECORDS "
966 "control in delete list.\n",
967 ctdb_hash(&reckey
)));
968 vdata
->count
.delete_list
.local_error
++;
969 vdata
->count
.delete_list
.left
--;
972 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
976 if (vdata
->count
.delete_list
.left
== 0) {
982 * Send the remaining records to all active nodes for deletion.
984 * The lmaster's (i.e. our) copies of these records have been stored
985 * successfully on the other nodes.
989 * Create a marshall blob from the remaining list of records to delete.
992 talloc_free(recs
->records
);
994 recs
->records
= (struct ctdb_marshall_buffer
*)
995 talloc_zero_size(recs
,
996 offsetof(struct ctdb_marshall_buffer
, data
));
997 if (recs
->records
== NULL
) {
998 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1001 recs
->records
->db_id
= ctdb_db
->db_id
;
1003 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
1004 delete_marshall_traverse
, recs
);
1006 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
1007 "delete list for second marshalling.\n"));
1011 indata
= ctdb_marshall_finish(recs
->records
);
1013 for (i
= 0; i
< num_active_nodes
; i
++) {
1014 struct ctdb_marshall_buffer
*records
;
1015 struct ctdb_rec_data
*rec
;
1019 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
1020 CTDB_CONTROL_TRY_DELETE_RECORDS
, 0,
1021 indata
, recs
, &outdata
, &res
,
1023 if (ret
!= 0 || res
!= 0) {
1024 DEBUG(DEBUG_ERR
, ("Failed to delete records on "
1025 "node %u: ret[%d] res[%d]\n",
1026 active_nodes
[i
], ret
, res
));
1031 * outdata contains the list of records coming back
1032 * from the node: These are the records that the
1033 * remote node could not delete. We remove these from
1034 * the list to delete locally.
1036 records
= (struct ctdb_marshall_buffer
*)outdata
.dptr
;
1037 rec
= (struct ctdb_rec_data
*)&records
->data
[0];
1038 while (records
->count
-- > 1) {
1039 TDB_DATA reckey
, recdata
;
1040 struct ctdb_ltdb_header
*rechdr
;
1041 struct delete_record_data
*dd
;
1043 reckey
.dptr
= &rec
->data
[0];
1044 reckey
.dsize
= rec
->keylen
;
1045 recdata
.dptr
= &rec
->data
[reckey
.dsize
];
1046 recdata
.dsize
= rec
->datalen
;
1048 if (recdata
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1049 DEBUG(DEBUG_CRIT
,(__location__
" bad ltdb record\n"));
1052 rechdr
= (struct ctdb_ltdb_header
*)recdata
.dptr
;
1053 recdata
.dptr
+= sizeof(*rechdr
);
1054 recdata
.dsize
-= sizeof(*rechdr
);
1056 dd
= (struct delete_record_data
*)trbt_lookup32(
1058 ctdb_hash(&reckey
));
1061 * The other node could not delete the
1062 * record and it is the first node that
1063 * failed. So we should remove it from
1064 * the tree and update statistics.
1067 vdata
->count
.delete_list
.remote_error
++;
1068 vdata
->count
.delete_list
.left
--;
1070 DEBUG(DEBUG_ERR
, (__location__
" Failed to "
1071 "find record with hash 0x%08x coming "
1072 "back from TRY_DELETE_RECORDS "
1073 "control in delete list.\n",
1074 ctdb_hash(&reckey
)));
1075 vdata
->count
.delete_list
.local_error
++;
1076 vdata
->count
.delete_list
.left
--;
1079 rec
= (struct ctdb_rec_data
*)(rec
->length
+ (uint8_t *)rec
);
1083 if (vdata
->count
.delete_list
.left
== 0) {
1089 * Delete the remaining records locally.
1091 * These records have successfully been deleted on all
1092 * active remote nodes.
1095 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
1096 delete_record_traverse
, vdata
);
1098 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
1099 "delete list for deletion.\n"));
1104 if (vdata
->count
.delete_list
.left
!= 0) {
1105 DEBUG(DEBUG_ERR
, (__location__
" Vaccum db[%s] error: "
1106 "there are %u records left for deletion after "
1107 "processing delete list\n",
1109 (unsigned)vdata
->count
.delete_list
.left
));
1112 sum
= vdata
->count
.delete_list
.deleted
1113 + vdata
->count
.delete_list
.skipped
1114 + vdata
->count
.delete_list
.remote_error
1115 + vdata
->count
.delete_list
.local_error
1116 + vdata
->count
.delete_list
.left
;
1118 if (vdata
->count
.delete_list
.total
!= sum
) {
1119 DEBUG(DEBUG_ERR
, (__location__
" Inconsistency in vacuum "
1120 "delete list counts for db[%s]: total[%u] != sum[%u]\n",
1122 (unsigned)vdata
->count
.delete_list
.total
,
1126 if (vdata
->count
.delete_list
.total
> 0) {
1129 " vacuum delete list statistics: "
1138 (unsigned)vdata
->count
.delete_list
.total
,
1139 (unsigned)vdata
->count
.delete_list
.deleted
,
1140 (unsigned)vdata
->count
.delete_list
.skipped
,
1141 (unsigned)vdata
->count
.delete_list
.remote_error
,
1142 (unsigned)vdata
->count
.delete_list
.local_error
,
1143 (unsigned)vdata
->count
.delete_list
.left
));
1147 talloc_free(tmp_ctx
);
1153 * initialize the vacuum_data
1155 static struct vacuum_data
*ctdb_vacuum_init_vacuum_data(
1156 struct ctdb_db_context
*ctdb_db
,
1157 TALLOC_CTX
*mem_ctx
)
1160 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1161 struct vacuum_data
*vdata
;
1163 vdata
= talloc_zero(mem_ctx
, struct vacuum_data
);
1164 if (vdata
== NULL
) {
1165 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1169 vdata
->ctdb
= ctdb_db
->ctdb
;
1170 vdata
->ctdb_db
= ctdb_db
;
1171 vdata
->delete_list
= trbt_create(vdata
, 0);
1172 if (vdata
->delete_list
== NULL
) {
1173 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1177 vdata
->start
= timeval_current();
1179 vdata
->count
.delete_queue
.added_to_delete_list
= 0;
1180 vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
= 0;
1181 vdata
->count
.delete_queue
.deleted
= 0;
1182 vdata
->count
.delete_queue
.skipped
= 0;
1183 vdata
->count
.delete_queue
.error
= 0;
1184 vdata
->count
.delete_queue
.total
= 0;
1185 vdata
->count
.db_traverse
.scheduled
= 0;
1186 vdata
->count
.db_traverse
.skipped
= 0;
1187 vdata
->count
.db_traverse
.error
= 0;
1188 vdata
->count
.db_traverse
.total
= 0;
1189 vdata
->count
.delete_list
.total
= 0;
1190 vdata
->count
.delete_list
.left
= 0;
1191 vdata
->count
.delete_list
.remote_error
= 0;
1192 vdata
->count
.delete_list
.local_error
= 0;
1193 vdata
->count
.delete_list
.skipped
= 0;
1194 vdata
->count
.delete_list
.deleted
= 0;
1196 /* the list needs to be of length num_nodes */
1197 vdata
->vacuum_fetch_list
= talloc_zero_array(vdata
,
1198 struct ctdb_marshall_buffer
*,
1200 if (vdata
->vacuum_fetch_list
== NULL
) {
1201 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1204 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
1205 vdata
->vacuum_fetch_list
[i
] = (struct ctdb_marshall_buffer
*)
1206 talloc_zero_size(vdata
->vacuum_fetch_list
,
1207 offsetof(struct ctdb_marshall_buffer
, data
));
1208 if (vdata
->vacuum_fetch_list
[i
] == NULL
) {
1209 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
1213 vdata
->vacuum_fetch_list
[i
]->db_id
= ctdb_db
->db_id
;
1225 * - Always do the fast vacuuming run, which traverses
1226 * the in-memory delete queue: these records have been
1227 * scheduled for deletion.
1228 * - Only if explicitly requested, the database is traversed
1229 * in order to use the traditional heuristics on empty records
1230 * to trigger deletion.
1231 * This is done only every VacuumFastPathCount'th vacuuming run.
1233 * The traverse runs fill two lists:
1235 * - The delete_list:
1236 * This is the list of empty records the current
1237 * node is lmaster and dmaster for. These records are later
1238 * deleted first on other nodes and then locally.
1240 * The fast vacuuming run has a short cut for those records
1241 * that have never been migrated with data: these records
1242 * are immediately deleted locally, since they have left
1243 * no trace on other nodes.
1245 * - The vacuum_fetch lists
1246 * (one for each other lmaster node):
1247 * The records in this list are sent for deletion to
1248 * their lmaster in a bulk VACUUM_FETCH message.
1250 * The lmaster then migrates all these records to itelf
1251 * so that they can be vacuumed there.
1253 * This executes in the child context.
1255 static int ctdb_vacuum_db(struct ctdb_db_context
*ctdb_db
,
1256 bool full_vacuum_run
)
1258 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1260 struct vacuum_data
*vdata
;
1261 TALLOC_CTX
*tmp_ctx
;
1263 DEBUG(DEBUG_INFO
, (__location__
" Entering %s vacuum run for db "
1264 "%s db_id[0x%08x]\n",
1265 full_vacuum_run
? "full" : "fast",
1266 ctdb_db
->db_name
, ctdb_db
->db_id
));
1268 ret
= ctdb_ctrl_getvnnmap(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
, ctdb
, &ctdb
->vnn_map
);
1270 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from local node\n"));
1274 pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1276 DEBUG(DEBUG_ERR
, ("Unable to get pnn from local node\n"));
1282 tmp_ctx
= talloc_new(ctdb_db
);
1283 if (tmp_ctx
== NULL
) {
1284 DEBUG(DEBUG_ERR
, ("Out of memory!\n"));
1288 vdata
= ctdb_vacuum_init_vacuum_data(ctdb_db
, tmp_ctx
);
1289 if (vdata
== NULL
) {
1290 talloc_free(tmp_ctx
);
1294 if (full_vacuum_run
) {
1295 ctdb_vacuum_traverse_db(ctdb_db
, vdata
);
1298 ctdb_process_delete_queue(ctdb_db
, vdata
);
1300 ctdb_process_vacuum_fetch_lists(ctdb_db
, vdata
);
1302 ctdb_process_delete_list(ctdb_db
, vdata
);
1304 talloc_free(tmp_ctx
);
1306 /* this ensures we run our event queue */
1307 ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1313 * repack and vaccum a db
1314 * called from the child context
1316 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context
*ctdb_db
,
1317 bool full_vacuum_run
)
1319 uint32_t repack_limit
= ctdb_db
->ctdb
->tunable
.repack_limit
;
1320 const char *name
= ctdb_db
->db_name
;
1321 int freelist_size
= 0;
1324 if (ctdb_vacuum_db(ctdb_db
, full_vacuum_run
) != 0) {
1325 DEBUG(DEBUG_ERR
,(__location__
" Failed to vacuum '%s'\n", name
));
1328 freelist_size
= tdb_freelist_size(ctdb_db
->ltdb
->tdb
);
1329 if (freelist_size
== -1) {
1330 DEBUG(DEBUG_ERR
,(__location__
" Failed to get freelist size for '%s'\n", name
));
1335 * decide if a repack is necessary
1337 if ((repack_limit
== 0 || (uint32_t)freelist_size
< repack_limit
))
1342 DEBUG(DEBUG_INFO
, ("Repacking %s with %u freelist entries\n",
1343 name
, freelist_size
));
1345 ret
= tdb_repack(ctdb_db
->ltdb
->tdb
);
1347 DEBUG(DEBUG_ERR
,(__location__
" Failed to repack '%s'\n", name
));
1354 static uint32_t get_vacuum_interval(struct ctdb_db_context
*ctdb_db
)
1356 uint32_t interval
= ctdb_db
->ctdb
->tunable
.vacuum_interval
;
1361 static int vacuum_child_destructor(struct ctdb_vacuum_child_context
*child_ctx
)
1363 double l
= timeval_elapsed(&child_ctx
->start_time
);
1364 struct ctdb_db_context
*ctdb_db
= child_ctx
->vacuum_handle
->ctdb_db
;
1365 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1367 DEBUG(DEBUG_INFO
,("Vacuuming took %.3f seconds for database %s\n", l
, ctdb_db
->db_name
));
1369 if (child_ctx
->child_pid
!= -1) {
1370 ctdb_kill(ctdb
, child_ctx
->child_pid
, SIGKILL
);
1372 /* Bump the number of successful fast-path runs. */
1373 child_ctx
->vacuum_handle
->fast_path_count
++;
1376 DLIST_REMOVE(ctdb
->vacuumers
, child_ctx
);
1378 event_add_timed(ctdb
->ev
, child_ctx
->vacuum_handle
,
1379 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1380 ctdb_vacuum_event
, child_ctx
->vacuum_handle
);
1386 * this event is generated when a vacuum child process times out
1388 static void vacuum_child_timeout(struct event_context
*ev
, struct timed_event
*te
,
1389 struct timeval t
, void *private_data
)
1391 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1393 DEBUG(DEBUG_ERR
,("Vacuuming child process timed out for db %s\n", child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1395 child_ctx
->status
= VACUUM_TIMEOUT
;
1397 talloc_free(child_ctx
);
1402 * this event is generated when a vacuum child process has completed
1404 static void vacuum_child_handler(struct event_context
*ev
, struct fd_event
*fde
,
1405 uint16_t flags
, void *private_data
)
1407 struct ctdb_vacuum_child_context
*child_ctx
= talloc_get_type(private_data
, struct ctdb_vacuum_child_context
);
1411 DEBUG(DEBUG_INFO
,("Vacuuming child process %d finished for db %s\n", child_ctx
->child_pid
, child_ctx
->vacuum_handle
->ctdb_db
->db_name
));
1412 child_ctx
->child_pid
= -1;
1414 ret
= read(child_ctx
->fd
[0], &c
, 1);
1415 if (ret
!= 1 || c
!= 0) {
1416 child_ctx
->status
= VACUUM_ERROR
;
1417 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
));
1419 child_ctx
->status
= VACUUM_OK
;
1422 talloc_free(child_ctx
);
1426 * this event is called every time we need to start a new vacuum process
1429 ctdb_vacuum_event(struct event_context
*ev
, struct timed_event
*te
,
1430 struct timeval t
, void *private_data
)
1432 struct ctdb_vacuum_handle
*vacuum_handle
= talloc_get_type(private_data
, struct ctdb_vacuum_handle
);
1433 struct ctdb_db_context
*ctdb_db
= vacuum_handle
->ctdb_db
;
1434 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
1435 struct ctdb_vacuum_child_context
*child_ctx
;
1436 struct tevent_fd
*fde
;
1439 /* we dont vacuum if we are in recovery mode, or db frozen */
1440 if (ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
||
1441 ctdb
->freeze_mode
[ctdb_db
->priority
] != CTDB_FREEZE_NONE
) {
1442 DEBUG(DEBUG_INFO
, ("Not vacuuming %s (%s)\n", ctdb_db
->db_name
,
1443 ctdb
->recovery_mode
== CTDB_RECOVERY_ACTIVE
? "in recovery"
1444 : ctdb
->freeze_mode
[ctdb_db
->priority
] == CTDB_FREEZE_PENDING
1447 event_add_timed(ctdb
->ev
, vacuum_handle
,
1448 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1449 ctdb_vacuum_event
, vacuum_handle
);
1453 child_ctx
= talloc(vacuum_handle
, struct ctdb_vacuum_child_context
);
1454 if (child_ctx
== NULL
) {
1455 DEBUG(DEBUG_CRIT
, (__location__
" Failed to allocate child context for vacuuming of %s\n", ctdb_db
->db_name
));
1456 ctdb_fatal(ctdb
, "Out of memory when crating vacuum child context. Shutting down\n");
1460 ret
= pipe(child_ctx
->fd
);
1462 talloc_free(child_ctx
);
1463 DEBUG(DEBUG_ERR
, ("Failed to create pipe for vacuum child process.\n"));
1464 event_add_timed(ctdb
->ev
, vacuum_handle
,
1465 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1466 ctdb_vacuum_event
, vacuum_handle
);
1470 if (vacuum_handle
->fast_path_count
> ctdb
->tunable
.vacuum_fast_path_count
) {
1471 vacuum_handle
->fast_path_count
= 0;
1474 child_ctx
->child_pid
= ctdb_fork(ctdb
);
1475 if (child_ctx
->child_pid
== (pid_t
)-1) {
1476 close(child_ctx
->fd
[0]);
1477 close(child_ctx
->fd
[1]);
1478 talloc_free(child_ctx
);
1479 DEBUG(DEBUG_ERR
, ("Failed to fork vacuum child process.\n"));
1480 event_add_timed(ctdb
->ev
, vacuum_handle
,
1481 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1482 ctdb_vacuum_event
, vacuum_handle
);
1487 if (child_ctx
->child_pid
== 0) {
1489 bool full_vacuum_run
= false;
1490 close(child_ctx
->fd
[0]);
1492 DEBUG(DEBUG_INFO
,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db
->db_name
));
1493 ctdb_set_process_name("ctdb_vacuum");
1494 if (switch_from_server_to_client(ctdb
, "vacuum-%s", ctdb_db
->db_name
) != 0) {
1495 DEBUG(DEBUG_CRIT
, (__location__
"ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1499 if ((ctdb
->tunable
.vacuum_fast_path_count
> 0) &&
1500 (vacuum_handle
->fast_path_count
== 0))
1502 full_vacuum_run
= true;
1504 cc
= ctdb_vacuum_and_repack_db(ctdb_db
, full_vacuum_run
);
1506 write(child_ctx
->fd
[1], &cc
, 1);
1510 set_close_on_exec(child_ctx
->fd
[0]);
1511 close(child_ctx
->fd
[1]);
1513 child_ctx
->status
= VACUUM_RUNNING
;
1514 child_ctx
->start_time
= timeval_current();
1516 DLIST_ADD(ctdb
->vacuumers
, child_ctx
);
1517 talloc_set_destructor(child_ctx
, vacuum_child_destructor
);
1520 * Clear the fastpath vacuuming list in the parent.
1522 talloc_free(ctdb_db
->delete_queue
);
1523 ctdb_db
->delete_queue
= trbt_create(ctdb_db
, 0);
1524 if (ctdb_db
->delete_queue
== NULL
) {
1525 /* fatal here? ... */
1526 ctdb_fatal(ctdb
, "Out of memory when re-creating vacuum tree "
1527 "in parent context. Shutting down\n");
1530 event_add_timed(ctdb
->ev
, child_ctx
,
1531 timeval_current_ofs(ctdb
->tunable
.vacuum_max_run_time
, 0),
1532 vacuum_child_timeout
, child_ctx
);
1534 DEBUG(DEBUG_DEBUG
, (__location__
" Created PIPE FD:%d to child vacuum process\n", child_ctx
->fd
[0]));
1536 fde
= event_add_fd(ctdb
->ev
, child_ctx
, child_ctx
->fd
[0],
1537 EVENT_FD_READ
, vacuum_child_handler
, child_ctx
);
1538 tevent_fd_set_auto_close(fde
);
1540 vacuum_handle
->child_ctx
= child_ctx
;
1541 child_ctx
->vacuum_handle
= vacuum_handle
;
1544 void ctdb_stop_vacuuming(struct ctdb_context
*ctdb
)
1546 /* Simply free them all. */
1547 while (ctdb
->vacuumers
) {
1548 DEBUG(DEBUG_INFO
, ("Aborting vacuuming for %s (%i)\n",
1549 ctdb
->vacuumers
->vacuum_handle
->ctdb_db
->db_name
,
1550 (int)ctdb
->vacuumers
->child_pid
));
1551 /* vacuum_child_destructor kills it, removes from list */
1552 talloc_free(ctdb
->vacuumers
);
1556 /* this function initializes the vacuuming context for a database
1557 * starts the vacuuming events
1559 int ctdb_vacuum_init(struct ctdb_db_context
*ctdb_db
)
1561 if (ctdb_db
->persistent
!= 0) {
1562 DEBUG(DEBUG_ERR
,("Vacuuming is disabled for persistent database %s\n", ctdb_db
->db_name
));
1566 ctdb_db
->vacuum_handle
= talloc(ctdb_db
, struct ctdb_vacuum_handle
);
1567 CTDB_NO_MEMORY(ctdb_db
->ctdb
, ctdb_db
->vacuum_handle
);
1569 ctdb_db
->vacuum_handle
->ctdb_db
= ctdb_db
;
1570 ctdb_db
->vacuum_handle
->fast_path_count
= 0;
1572 event_add_timed(ctdb_db
->ctdb
->ev
, ctdb_db
->vacuum_handle
,
1573 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1574 ctdb_vacuum_event
, ctdb_db
->vacuum_handle
);
1579 static void remove_record_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1580 const struct ctdb_ltdb_header
*hdr
,
1583 struct delete_record_data
*kd
;
1586 hash
= (uint32_t)ctdb_hash(&key
);
1588 DEBUG(DEBUG_DEBUG
, (__location__
1589 " remove_record_from_delete_queue: "
1594 "migrated_with_data[%s]\n",
1595 ctdb_db
->db_name
, ctdb_db
->db_id
,
1597 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1598 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1600 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1602 DEBUG(DEBUG_DEBUG
, (__location__
1603 " remove_record_from_delete_queue: "
1604 "record not in queue (hash[0x%08x])\n.",
1609 if ((kd
->key
.dsize
!= key
.dsize
) ||
1610 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1612 DEBUG(DEBUG_DEBUG
, (__location__
1613 " remove_record_from_delete_queue: "
1614 "hash collision for key with hash[0x%08x] "
1615 "in db[%s] - skipping\n",
1616 hash
, ctdb_db
->db_name
));
1620 DEBUG(DEBUG_DEBUG
, (__location__
1621 " remove_record_from_delete_queue: "
1622 "removing key with hash[0x%08x]\n",
1631 * Insert a record into the ctdb_db context's delete queue,
1632 * handling hash collisions.
1634 static int insert_record_into_delete_queue(struct ctdb_db_context
*ctdb_db
,
1635 const struct ctdb_ltdb_header
*hdr
,
1638 struct delete_record_data
*kd
;
1642 hash
= (uint32_t)ctdb_hash(&key
);
1644 DEBUG(DEBUG_INFO
, (__location__
" schedule for deletion: db[%s] "
1648 "migrated_with_data[%s]\n",
1649 ctdb_db
->db_name
, ctdb_db
->db_id
,
1651 ctdb_lmaster(ctdb_db
->ctdb
, &key
),
1652 hdr
->flags
& CTDB_REC_FLAG_MIGRATED_WITH_DATA
? "yes" : "no"));
1654 kd
= (struct delete_record_data
*)trbt_lookup32(ctdb_db
->delete_queue
, hash
);
1656 if ((kd
->key
.dsize
!= key
.dsize
) ||
1657 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1660 (__location__
" schedule for deletion: "
1661 "hash collision for key hash [0x%08x]. "
1662 "Skipping the record.\n", hash
));
1666 (__location__
" schedule for deletion: "
1667 "updating entry for key with hash [0x%08x].\n",
1672 ret
= insert_delete_record_data_into_tree(ctdb_db
->ctdb
, ctdb_db
,
1673 ctdb_db
->delete_queue
,
1677 (__location__
" schedule for deletion: error "
1678 "inserting key with hash [0x%08x] into delete queue\n",
1687 * Schedule a record for deletetion.
1688 * Called from the parent context.
1690 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context
*ctdb
,
1693 struct ctdb_control_schedule_for_deletion
*dd
;
1694 struct ctdb_db_context
*ctdb_db
;
1698 dd
= (struct ctdb_control_schedule_for_deletion
*)indata
.dptr
;
1700 ctdb_db
= find_ctdb_db(ctdb
, dd
->db_id
);
1701 if (ctdb_db
== NULL
) {
1702 DEBUG(DEBUG_ERR
, (__location__
" Unknown db id 0x%08x\n",
1707 key
.dsize
= dd
->keylen
;
1710 ret
= insert_record_into_delete_queue(ctdb_db
, &dd
->hdr
, key
);
1715 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context
*ctdb_db
,
1716 const struct ctdb_ltdb_header
*hdr
,
1720 struct ctdb_control_schedule_for_deletion
*dd
;
1724 if (ctdb_db
->ctdb
->ctdbd_pid
== getpid()) {
1725 /* main daemon - directly queue */
1726 ret
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
1731 /* if we dont have a connection to the daemon we can not send
1732 a control. For example sometimes from update_record control child
1735 if (!ctdb_db
->ctdb
->can_send_controls
) {
1740 /* child process: send the main daemon a control */
1741 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + key
.dsize
;
1742 indata
.dptr
= talloc_zero_array(ctdb_db
, uint8_t, indata
.dsize
);
1743 if (indata
.dptr
== NULL
) {
1744 DEBUG(DEBUG_ERR
, (__location__
" out of memory\n"));
1747 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
1748 dd
->db_id
= ctdb_db
->db_id
;
1750 dd
->keylen
= key
.dsize
;
1751 memcpy(dd
->key
, key
.dptr
, key
.dsize
);
1753 ret
= ctdb_control(ctdb_db
->ctdb
,
1756 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
1757 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
1762 NULL
, /* timeout : NULL == wait forever */
1763 NULL
); /* error message */
1765 talloc_free(indata
.dptr
);
1767 if (ret
!= 0 || status
!= 0) {
1768 DEBUG(DEBUG_ERR
, (__location__
" Error sending "
1769 "SCHEDULE_FOR_DELETION "
1779 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1780 const struct ctdb_ltdb_header
*hdr
,
1783 if (ctdb_db
->ctdb
->ctdbd_pid
!= getpid()) {
1785 * Only remove the record from the delete queue if called
1786 * in the main daemon.
1791 remove_record_from_delete_queue(ctdb_db
, hdr
, key
);