2011-08-31 18:08:22 +05:30
|
|
|
#ifndef BTREE_CHECKER_H
|
|
|
|
#define BTREE_CHECKER_H
|
2011-08-24 15:15:39 +05:30
|
|
|
|
|
|
|
#include "btree.h"
|
|
|
|
|
2011-09-16 14:36:37 +05:30
|
|
|
#include "checksum.h"
|
2011-08-24 18:57:45 +05:30
|
|
|
#include "error_set.h"
|
2011-09-16 14:36:37 +05:30
|
|
|
#include "hex_dump.h"
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
#include <sstream>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Little helper class that keeps track of how many times blocks
|
|
|
|
// are referenced.
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
class block_counter {
|
|
|
|
public:
|
2011-08-25 15:24:43 +05:30
|
|
|
typedef std::map<block_address, unsigned> count_map;
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
void inc(block_address b) {
|
2011-08-25 15:24:43 +05:30
|
|
|
count_map::iterator it = counts_.find(b);
|
2011-08-24 15:15:39 +05:30
|
|
|
if (it == counts_.end())
|
|
|
|
counts_.insert(make_pair(b, 1));
|
|
|
|
else
|
|
|
|
it->second++;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get_count(block_address b) const {
|
2011-08-24 18:57:45 +05:30
|
|
|
count_map::const_iterator it = counts_.find(b);
|
2011-08-24 15:15:39 +05:30
|
|
|
return (it == counts_.end()) ? 0 : it->second;
|
|
|
|
}
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
count_map const &get_counts() const {
|
2011-08-24 15:15:39 +05:30
|
|
|
return counts_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-08-24 18:57:45 +05:30
|
|
|
count_map counts_;
|
2011-08-24 15:15:39 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// This class implements consistency checking for the btrees in
|
|
|
|
// general. Derive from this if you want some additional checks.
|
|
|
|
// It's worth summarising what is checked:
|
|
|
|
//
|
|
|
|
// Implemented
|
|
|
|
// -----------
|
|
|
|
//
|
|
|
|
// - block_nr
|
|
|
|
// - nr_entries < max_entries
|
|
|
|
// - max_entries fits in block
|
|
|
|
// - max_entries is divisible by 3
|
|
|
|
// - nr_entries > minimum (except for root nodes)
|
|
|
|
//
|
|
|
|
// Not implemented
|
|
|
|
// ---------------
|
|
|
|
//
|
|
|
|
// - checksum
|
|
|
|
// - leaf | internal flags (this can be inferred from siblings)
|
|
|
|
//----------------------------------------------------------------
|
2011-08-31 17:18:41 +05:30
|
|
|
template <uint32_t Levels, typename ValueTraits>
|
2011-08-31 18:08:22 +05:30
|
|
|
class btree_checker : public btree<Levels, ValueTraits>::visitor {
|
2011-08-24 15:15:39 +05:30
|
|
|
public:
|
2011-08-31 18:08:22 +05:30
|
|
|
btree_checker(block_counter &counter)
|
2011-08-24 18:57:45 +05:30
|
|
|
: counter_(counter),
|
|
|
|
errs_(new error_set("btree errors")) {
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
bool visit_internal(unsigned level,
|
|
|
|
optional<uint64_t> key,
|
2011-08-31 17:18:41 +05:30
|
|
|
btree_detail::node_ref<uint64_traits> const &n) {
|
2011-08-26 15:43:13 +05:30
|
|
|
if (already_visited(n))
|
|
|
|
return false;
|
|
|
|
|
2011-09-16 14:36:37 +05:30
|
|
|
check_sum(n);
|
|
|
|
|
2011-09-02 18:58:42 +05:30
|
|
|
if (!key)
|
|
|
|
new_root(level);
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
check_block_nr(n);
|
|
|
|
check_max_entries(n);
|
2011-09-02 15:56:42 +05:30
|
|
|
check_nr_entries(n, !key);
|
|
|
|
check_ordered_keys(n);
|
|
|
|
check_parent_key(key, n);
|
2011-08-26 15:43:13 +05:30
|
|
|
return true;
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
bool visit_internal_leaf(unsigned level,
|
|
|
|
optional<uint64_t> key,
|
2011-08-31 17:18:41 +05:30
|
|
|
btree_detail::node_ref<uint64_traits> const &n) {
|
2011-08-26 15:43:13 +05:30
|
|
|
if (already_visited(n))
|
|
|
|
return false;
|
|
|
|
|
2011-09-16 14:36:37 +05:30
|
|
|
check_sum(n);
|
|
|
|
|
2011-09-02 18:58:42 +05:30
|
|
|
if (!key)
|
|
|
|
new_root(level);
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
check_block_nr(n);
|
|
|
|
check_max_entries(n);
|
2011-09-02 15:56:42 +05:30
|
|
|
check_nr_entries(n, !key);
|
|
|
|
check_ordered_keys(n);
|
|
|
|
check_parent_key(key, n);
|
|
|
|
check_leaf_key(level, n);
|
2011-09-16 14:36:37 +05:30
|
|
|
|
2011-08-26 15:43:13 +05:30
|
|
|
return true;
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
bool visit_leaf(unsigned level,
|
|
|
|
optional<uint64_t> key,
|
2011-08-31 17:18:41 +05:30
|
|
|
btree_detail::node_ref<ValueTraits> const &n) {
|
2011-08-26 15:43:13 +05:30
|
|
|
if (already_visited(n))
|
|
|
|
return false;
|
|
|
|
|
2011-09-16 14:36:37 +05:30
|
|
|
check_sum(n);
|
|
|
|
|
2011-09-02 18:58:42 +05:30
|
|
|
if (!key)
|
|
|
|
new_root(level);
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
check_block_nr(n);
|
|
|
|
check_max_entries(n);
|
2011-09-02 15:56:42 +05:30
|
|
|
check_nr_entries(n, !key);
|
|
|
|
check_ordered_keys(n);
|
|
|
|
check_parent_key(key, n);
|
|
|
|
check_leaf_key(level, n);
|
2011-08-26 15:43:13 +05:30
|
|
|
return true;
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
boost::optional<error_set::ptr> get_errors() const {
|
|
|
|
return errs_;
|
|
|
|
}
|
|
|
|
|
2011-08-26 15:43:13 +05:30
|
|
|
protected:
|
|
|
|
block_counter &get_counter() {
|
|
|
|
return counter_;
|
|
|
|
}
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
private:
|
2011-08-26 15:43:13 +05:30
|
|
|
template <typename node>
|
|
|
|
bool already_visited(node const &n) {
|
|
|
|
block_address b = n.get_location();
|
|
|
|
|
|
|
|
counter_.inc(b);
|
|
|
|
|
|
|
|
if (seen_.count(b) > 0)
|
|
|
|
return true;
|
2011-08-24 15:15:39 +05:30
|
|
|
|
|
|
|
seen_.insert(b);
|
2011-08-26 15:43:13 +05:30
|
|
|
return false;
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
2011-09-16 14:36:37 +05:30
|
|
|
template <typename node>
|
|
|
|
void check_sum(node const &n) const {
|
|
|
|
crc32c sum(BTREE_CSUM_XOR);
|
|
|
|
|
|
|
|
disk_node const *data = n.raw();
|
|
|
|
sum.append(&data->header.flags, MD_BLOCK_SIZE - sizeof(uint32_t));
|
|
|
|
if (sum.get_sum() != n.get_checksum()) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "checksum error for block " << n.get_block_nr()
|
|
|
|
<< ", sum was " << sum.get_sum()
|
|
|
|
<< ", expected " << n.get_checksum();
|
|
|
|
errs_->add_child(out.str());
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
template <typename node>
|
|
|
|
void check_block_nr(node const &n) const {
|
|
|
|
if (n.get_location() != n.get_block_nr()) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "block number mismatch: actually "
|
|
|
|
<< n.get_location()
|
|
|
|
<< ", claims " << n.get_block_nr();
|
2011-08-24 18:57:45 +05:30
|
|
|
errs_->add_child(out.str());
|
|
|
|
throw runtime_error(out.str());
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename node>
|
|
|
|
void check_max_entries(node const &n) const {
|
|
|
|
size_t elt_size = sizeof(uint64_t) + n.get_value_size();
|
2011-08-31 17:18:41 +05:30
|
|
|
if (elt_size * n.get_max_entries() + sizeof(node_header) > MD_BLOCK_SIZE) {
|
2011-08-24 15:15:39 +05:30
|
|
|
std::ostringstream out;
|
|
|
|
out << "max entries too large: " << n.get_max_entries();
|
2011-08-24 18:57:45 +05:30
|
|
|
errs_->add_child(out.str());
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (n.get_max_entries() % 3) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "max entries is not divisible by 3: " << n.get_max_entries();
|
2011-08-24 18:57:45 +05:30
|
|
|
errs_->add_child(out.str());
|
|
|
|
throw runtime_error(out.str());
|
2011-08-24 15:15:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename node>
|
|
|
|
void check_nr_entries(node const &n, bool is_root) const {
|
|
|
|
if (n.get_nr_entries() > n.get_max_entries()) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "bad nr_entries: "
|
|
|
|
<< n.get_nr_entries() << " < "
|
|
|
|
<< n.get_max_entries();
|
2011-08-24 18:57:45 +05:30
|
|
|
errs_->add_child(out.str());
|
2011-08-24 15:15:39 +05:30
|
|
|
throw std::runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
block_address min = n.get_max_entries() / 3;
|
|
|
|
if (!is_root && (n.get_nr_entries() < min)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "too few entries in btree: "
|
|
|
|
<< n.get_nr_entries()
|
|
|
|
<< ", expected at least "
|
|
|
|
<< min;
|
2011-08-24 18:57:45 +05:30
|
|
|
errs_->add_child(out.str());
|
2011-08-24 15:15:39 +05:30
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 15:56:42 +05:30
|
|
|
template <typename node>
|
|
|
|
void check_ordered_keys(node const &n) const {
|
|
|
|
unsigned nr_entries = n.get_nr_entries();
|
|
|
|
|
|
|
|
if (nr_entries == 0)
|
|
|
|
return; // can only happen if a root node
|
|
|
|
|
|
|
|
uint64_t last_key = n.key_at(0);
|
|
|
|
|
|
|
|
for (unsigned i = 1; i < nr_entries; i++) {
|
|
|
|
uint64_t k = n.key_at(i);
|
|
|
|
if (k <= last_key) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "keys are out of order, " << k << " <= " << last_key;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
last_key = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename node>
|
|
|
|
void check_parent_key(boost::optional<uint64_t> key, node const &n) const {
|
|
|
|
if (!key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*key > n.key_at(0)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "parent key mismatch: parent was " << *key
|
|
|
|
<< ", but lowest in node was " << n.key_at(0);
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename node>
|
|
|
|
void check_leaf_key(unsigned level, node const &n) {
|
|
|
|
if (n.get_nr_entries() == 0)
|
|
|
|
return; // can only happen if a root node
|
|
|
|
|
|
|
|
if (last_leaf_key_[level] && *last_leaf_key_[level] >= n.key_at(0)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "the last key of the previous leaf was " << *last_leaf_key_[level]
|
|
|
|
<< " and the first key of this leaf is " << n.key_at(0);
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
last_leaf_key_[level] = n.key_at(n.get_nr_entries() - 1);
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:58:42 +05:30
|
|
|
void new_root(unsigned level) {
|
|
|
|
// we're starting a new subtree, so should
|
|
|
|
// reset the last_leaf value.
|
|
|
|
last_leaf_key_[level] = boost::optional<uint64_t>();
|
|
|
|
}
|
|
|
|
|
2011-08-24 15:15:39 +05:30
|
|
|
block_counter &counter_;
|
|
|
|
std::set<block_address> seen_;
|
2011-08-24 18:57:45 +05:30
|
|
|
error_set::ptr errs_;
|
2011-09-02 15:56:42 +05:30
|
|
|
boost::optional<uint64_t> last_leaf_key_[Levels];
|
2011-08-24 15:15:39 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|