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
;
184 struct ctdb_rec_data
*rec
;
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
);
195 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
196 vdata
->traverse_error
= true;
200 old_size
= talloc_get_size(vfl
);
201 vfl
= talloc_realloc_size(NULL
, vfl
, old_size
+ rec
->length
);
203 DEBUG(DEBUG_ERR
,(__location__
" Failed to expand\n"));
204 vdata
->traverse_error
= true;
207 vdata
->vacuum_fetch_list
[lmaster
] = vfl
;
210 memcpy(old_size
+(uint8_t *)vfl
, rec
, rec
->length
);
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
)) {
229 *header
= *(struct ctdb_ltdb_header
*)data
.dptr
;
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
,
240 struct vacuum_data
*vdata
= talloc_get_type(private_data
,
242 struct ctdb_context
*ctdb
= vdata
->ctdb
;
243 struct ctdb_db_context
*ctdb_db
= vdata
->ctdb_db
;
245 struct ctdb_ltdb_header
*hdr
;
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"
257 (unsigned)ctdb
->num_nodes
,
258 (unsigned)ctdb_hash(&key
)));
262 if (data
.dsize
!= sizeof(struct ctdb_ltdb_header
)) {
263 /* it is not a deleted record */
264 vdata
->count
.db_traverse
.skipped
++;
268 hdr
= (struct ctdb_ltdb_header
*)data
.dptr
;
270 if (hdr
->dmaster
!= ctdb
->pnn
) {
271 vdata
->count
.db_traverse
.skipped
++;
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
);
281 vdata
->count
.db_traverse
.error
++;
283 vdata
->count
.db_traverse
.scheduled
++;
290 * traverse the tree of records to delete and marshall them into
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
;
300 rec
= ctdb_marshall_record(dd
, recs
->records
->db_id
, dd
->key
, &dd
->hdr
, tdb_null
);
302 DEBUG(DEBUG_ERR
, (__location__
" failed to marshall record\n"));
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"));
312 recs
->records
->count
++;
313 memcpy(old_size
+(uint8_t *)(recs
->records
), rec
, rec
->length
);
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
;
335 uint32_t hash
= ctdb_hash(&(dd
->key
));
338 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
--;
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
);
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. "
365 hash
, ctdb_db
->db_name
));
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. "
373 hash
, ctdb_db
->db_name
));
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
));
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
));
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
);
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
));
413 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
418 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
420 recs
->vdata
->count
.delete_list
.skipped
++;
421 recs
->vdata
->count
.delete_list
.left
--;
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 ??? */
461 struct ctdb_ltdb_header header
;
463 uint32_t hash
= ctdb_hash(&(dd
->key
));
465 vdata
->count
.delete_queue
.total
++;
467 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
++;
477 res
= tdb_parse_record(ctdb_db
->ltdb
->tdb
, dd
->key
,
478 vacuum_record_parser
, &header
);
483 if (header
.dmaster
!= ctdb
->pnn
) {
484 /* The record has been migrated off the node. Skip. */
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.
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
);
512 (__location__
" Error adding record to list "
513 "of records to send to lmaster.\n"));
514 vdata
->count
.delete_queue
.error
++;
516 vdata
->count
.delete_queue
.added_to_vacuum_fetch_list
++;
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
);
527 (__location__
" Error adding record to list "
528 "of records for deletion on lmaster.\n"));
529 vdata
->count
.delete_queue
.error
++;
531 vdata
->count
.delete_queue
.added_to_delete_list
++;
534 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
++;
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
++;
555 vdata
->count
.delete_queue
.skipped
++;
558 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
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
;
576 struct ctdb_ltdb_header header
;
578 uint32_t hash
= ctdb_hash(&(dd
->key
));
580 res
= tdb_chainlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
--;
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
);
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. "
607 hash
, ctdb_db
->db_name
));
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. "
615 hash
, ctdb_db
->db_name
));
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
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
));
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
));
644 res
= tdb_delete(ctdb_db
->ltdb
->tdb
, dd
->key
);
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
++;
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
++;
663 vdata
->count
.delete_list
.skipped
++;
666 tdb_chainunlock(ctdb_db
->ltdb
->tdb
, dd
->key
);
669 vdata
->count
.delete_list
.left
--;
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
)
686 ret
= trbt_traversearray32(ctdb_db
->delete_queue
, 1,
687 delete_queue_traverse
, vdata
);
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",
704 (unsigned)vdata
->count
.delete_queue
.total
,
708 if (vdata
->count
.delete_queue
.total
> 0) {
711 " fast vacuuming delete_queue traverse statistics: "
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
));
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
)
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
));
750 if (vdata
->count
.db_traverse
.total
> 0) {
753 " full vacuuming db traverse statistics: "
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
));
770 * Process the vacuum fetch lists:
771 * For records for which we are not the lmaster, tell the lmaster to
774 static void ctdb_process_vacuum_fetch_lists(struct ctdb_db_context
*ctdb_db
,
775 struct vacuum_data
*vdata
)
778 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
780 for (i
= 0; i
< ctdb
->num_nodes
; i
++) {
782 struct ctdb_marshall_buffer
*vfl
= vdata
->vacuum_fetch_list
[i
];
784 if (ctdb
->nodes
[i
]->pnn
== ctdb
->pnn
) {
788 if (vfl
->count
== 0) {
792 DEBUG(DEBUG_INFO
, ("Found %u records for lmaster %u in '%s'\n",
793 vfl
->count
, ctdb
->nodes
[i
]->pnn
,
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
,
802 DEBUG(DEBUG_ERR
, (__location__
" Failed to send vacuum "
803 "fetch message to %u\n",
804 ctdb
->nodes
[i
]->pnn
));
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
)
838 struct ctdb_context
*ctdb
= ctdb_db
->ctdb
;
839 struct delete_records_list
*recs
;
841 struct ctdb_node_map
*nodemap
;
842 uint32_t *active_nodes
;
843 int num_active_nodes
;
847 if (vdata
->count
.delete_list
.total
== 0) {
851 tmp_ctx
= talloc_new(vdata
);
852 if (tmp_ctx
== NULL
) {
853 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
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(),
868 DEBUG(DEBUG_ERR
,(__location__
" unable to get node map\n"));
872 active_nodes
= list_of_active_nodes(ctdb
, nodemap
,
873 nodemap
, /* talloc context */
874 false /* include self */);
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
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
889 * Send currently empty record copy to all active nodes for storing.
892 recs
= talloc_zero(tmp_ctx
, struct delete_records_list
);
894 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
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"));
904 recs
->records
->db_id
= ctdb_db
->db_id
;
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
);
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
;
931 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
932 CTDB_CONTROL_RECEIVE_RECORDS
, 0,
933 indata
, recs
, &outdata
, &res
,
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
));
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"));
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(
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
979 vdata
->count
.delete_list
.remote_error
++;
980 vdata
->count
.delete_list
.left
--;
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) {
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"));
1020 recs
->records
->db_id
= ctdb_db
->db_id
;
1022 ret
= trbt_traversearray32(vdata
->delete_list
, 1,
1023 delete_marshall_traverse
, recs
);
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
;
1038 ret
= ctdb_control(ctdb
, active_nodes
[i
], 0,
1039 CTDB_CONTROL_TRY_DELETE_RECORDS
, 0,
1040 indata
, recs
, &outdata
, &res
,
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
));
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"));
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(
1077 ctdb_hash(&reckey
));
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.
1086 vdata
->count
.delete_list
.remote_error
++;
1087 vdata
->count
.delete_list
.left
--;
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) {
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
);
1117 DEBUG(DEBUG_ERR
, (__location__
" Error traversing the "
1118 "delete list for deletion.\n"));
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",
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",
1141 (unsigned)vdata
->count
.delete_list
.total
,
1145 if (vdata
->count
.delete_list
.total
> 0) {
1148 " vacuum delete list statistics: "
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
));
1166 talloc_free(tmp_ctx
);
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
)
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"));
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"));
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
*,
1219 if (vdata
->vacuum_fetch_list
== NULL
) {
1220 DEBUG(DEBUG_ERR
,(__location__
" Out of memory\n"));
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"));
1232 vdata
->vacuum_fetch_list
[i
]->db_id
= ctdb_db
->db_id
;
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
;
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
);
1289 DEBUG(DEBUG_ERR
, ("Unable to get vnnmap from local node\n"));
1293 pnn
= ctdb_ctrl_getpnn(ctdb
, TIMELIMIT(), CTDB_CURRENT_NODE
);
1295 DEBUG(DEBUG_ERR
, ("Unable to get pnn from local node\n"));
1301 tmp_ctx
= talloc_new(ctdb_db
);
1302 if (tmp_ctx
== NULL
) {
1303 DEBUG(DEBUG_ERR
, ("Out of memory!\n"));
1307 vdata
= ctdb_vacuum_init_vacuum_data(ctdb_db
, tmp_ctx
);
1308 if (vdata
== NULL
) {
1309 talloc_free(tmp_ctx
);
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
);
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;
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
));
1354 * decide if a repack is necessary
1356 if ((repack_limit
== 0 || (uint32_t)freelist_size
< repack_limit
))
1361 DEBUG(DEBUG_INFO
, ("Repacking %s with %u freelist entries\n",
1362 name
, freelist_size
));
1364 ret
= tdb_repack(ctdb_db
->ltdb
->tdb
);
1366 DEBUG(DEBUG_ERR
,(__location__
" Failed to repack '%s'\n", name
));
1373 static uint32_t get_vacuum_interval(struct ctdb_db_context
*ctdb_db
)
1375 uint32_t interval
= ctdb_db
->ctdb
->tunable
.vacuum_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
);
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
);
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
);
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
));
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
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
;
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
1466 event_add_timed(ctdb
->ev
, vacuum_handle
,
1467 timeval_current_ofs(get_vacuum_interval(ctdb_db
), 0),
1468 ctdb_vacuum_event
, vacuum_handle
);
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
);
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
);
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
);
1506 if (child_ctx
->child_pid
== 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"));
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);
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
));
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
);
1598 static void remove_record_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1599 const struct ctdb_ltdb_header
*hdr
,
1602 struct delete_record_data
*kd
;
1605 hash
= (uint32_t)ctdb_hash(&key
);
1607 DEBUG(DEBUG_DEBUG
, (__location__
1608 " remove_record_from_delete_queue: "
1613 "migrated_with_data[%s]\n",
1614 ctdb_db
->db_name
, ctdb_db
->db_id
,
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
);
1621 DEBUG(DEBUG_DEBUG
, (__location__
1622 " remove_record_from_delete_queue: "
1623 "record not in queue (hash[0x%08x])\n.",
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
));
1639 DEBUG(DEBUG_DEBUG
, (__location__
1640 " remove_record_from_delete_queue: "
1641 "removing key with hash[0x%08x]\n",
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
,
1657 struct delete_record_data
*kd
;
1661 hash
= (uint32_t)ctdb_hash(&key
);
1663 DEBUG(DEBUG_INFO
, (__location__
" schedule for deletion: db[%s] "
1667 "migrated_with_data[%s]\n",
1668 ctdb_db
->db_name
, ctdb_db
->db_id
,
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
);
1675 if ((kd
->key
.dsize
!= key
.dsize
) ||
1676 (memcmp(kd
->key
.dptr
, key
.dptr
, key
.dsize
) != 0))
1679 (__location__
" schedule for deletion: "
1680 "hash collision for key hash [0x%08x]. "
1681 "Skipping the record.\n", hash
));
1685 (__location__
" schedule for deletion: "
1686 "updating entry for key with hash [0x%08x].\n",
1691 ret
= insert_delete_record_data_into_tree(ctdb_db
->ctdb
, ctdb_db
,
1692 ctdb_db
->delete_queue
,
1696 (__location__
" schedule for deletion: error "
1697 "inserting key with hash [0x%08x] into delete queue\n",
1706 * Schedule a record for deletetion.
1707 * Called from the parent context.
1709 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context
*ctdb
,
1712 struct ctdb_control_schedule_for_deletion
*dd
;
1713 struct ctdb_db_context
*ctdb_db
;
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",
1726 key
.dsize
= dd
->keylen
;
1729 ret
= insert_record_into_delete_queue(ctdb_db
, &dd
->hdr
, key
);
1734 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context
*ctdb_db
,
1735 const struct ctdb_ltdb_header
*hdr
,
1739 struct ctdb_control_schedule_for_deletion
*dd
;
1743 if (ctdb_db
->ctdb
->ctdbd_pid
== getpid()) {
1744 /* main daemon - directly queue */
1745 ret
= insert_record_into_delete_queue(ctdb_db
, hdr
, key
);
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
1754 if (!ctdb_db
->ctdb
->can_send_controls
) {
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"));
1766 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
1767 dd
->db_id
= ctdb_db
->db_id
;
1769 dd
->keylen
= key
.dsize
;
1770 memcpy(dd
->key
, key
.dptr
, key
.dsize
);
1772 ret
= ctdb_control(ctdb_db
->ctdb
,
1775 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
1776 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
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 "
1798 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context
*ctdb_db
,
1799 const struct ctdb_ltdb_header
*hdr
,
1802 if (ctdb_db
->ctdb
->ctdbd_pid
!= getpid()) {
1804 * Only remove the record from the delete queue if called
1805 * in the main daemon.
1810 remove_record_from_delete_queue(ctdb_db
, hdr
, key
);