thin-provisioning-tools/unit-tests/btree_damage_visitor_t.cc

648 lines
14 KiB
C++
Raw Normal View History

2013-05-08 16:30:24 +05:30
#include "gmock/gmock.h"
#include "test_utils.h"
#include "persistent-data/data-structures/btree_damage_visitor.h"
2013-05-08 21:08:38 +05:30
#include "persistent-data/endian_utils.h"
#include "persistent-data/space-maps/core.h"
#include "persistent-data/transaction_manager.h"
2013-05-08 16:30:24 +05:30
using namespace std;
using namespace persistent_data;
using namespace test;
using namespace testing;
//----------------------------------------------------------------
namespace {
block_address const BLOCK_SIZE = 4096;
block_address const NR_BLOCKS = 102400;
2013-05-09 18:01:04 +05:30
block_address const SUPERBLOCK = 0;
2013-05-08 16:30:24 +05:30
2013-05-08 21:08:38 +05:30
struct thing {
thing(uint32_t x_ = 0, uint64_t y_ = 0)
: x(x_),
2013-05-09 18:01:04 +05:30
y(y_) {
}
bool operator ==(thing const &rhs) const {
return (x == rhs.x) && (y == rhs.y);
2013-05-09 18:01:04 +05:30
}
uint32_t x;
2013-05-08 21:08:38 +05:30
uint64_t y;
};
ostream &operator <<(ostream &out, thing const &t) {
return out << "thing [" << t.x << ", " << t.y << "]";
}
2013-05-08 21:08:38 +05:30
struct thing_disk {
le32 x;
2013-05-08 21:08:38 +05:30
le64 y;
2013-05-14 15:02:28 +05:30
// To ensure we have fewer entries per leaf, and thus more internal nodes.
char padding[200];
2013-05-08 21:08:38 +05:30
};
struct thing_traits {
typedef thing_disk disk_type;
typedef thing value_type;
typedef persistent_data::no_op_ref_counter<value_type> ref_counter;
static void unpack(thing_disk const &disk, thing &value) {
value.x = to_cpu<uint32_t>(disk.x);
2013-05-08 21:08:38 +05:30
value.y = to_cpu<uint64_t>(disk.y);
}
static void pack(thing const &value, thing_disk &disk) {
disk.x = to_disk<le32>(value.x);
2013-05-08 21:08:38 +05:30
disk.y = to_disk<le64>(value.y);
}
};
2013-05-14 15:31:39 +05:30
//--------------------------------
struct node_info {
typedef boost::shared_ptr<node_info> ptr;
btree_detail::btree_path path;
2013-05-14 15:31:39 +05:30
bool leaf;
unsigned depth;
block_address b;
range<uint64_t> keys;
};
ostream &operator <<(ostream &out, node_info const &ni) {
out << "node_info [leaf = " << ni.leaf
<< ", depth " << ni.depth
<< ", path [";
bool first = true;
for (auto k : ni.path) {
if (first)
first = false;
else
out << ", ";
out << k;
}
out << "], b " << ni.b
<< ", keys " << ni.keys
<< "]";
return out;
}
2013-05-15 18:06:56 +05:30
bool is_leaf(node_info const &n) {
return n.leaf;
}
bool is_internal(node_info const &n) {
return !n.leaf;
}
2013-05-14 17:37:04 +05:30
typedef vector<node_info> node_array;
typedef vector<node_info::ptr> node_ptr_array;
class btree_layout {
public:
btree_layout(vector<node_info::ptr> const &ns)
2013-05-14 20:08:20 +05:30
: nodes_(ns.size(), node_info()) {
for (unsigned i = 0; i < ns.size(); i++)
nodes_[i] = *ns[i];
2013-05-14 17:37:04 +05:30
}
template <typename Predicate>
2013-05-14 20:08:20 +05:30
unsigned get_nr_nodes(Predicate const &pred) const {
unsigned nr = 0;
for (auto n : nodes_)
if (pred(n))
nr++;
return nr;
}
2013-05-14 17:37:04 +05:30
2013-05-14 20:08:20 +05:30
template <typename Predicate>
node_info get_node(unsigned target, Predicate const &pred) const {
unsigned i = 0;
for (auto n : nodes_) {
if (pred(n)) {
2013-05-14 17:37:04 +05:30
if (!target)
break;
else
target--;
}
2013-05-14 20:08:20 +05:30
i++;
}
2013-05-14 17:37:04 +05:30
if (target)
throw runtime_error("not that many nodes");
2013-05-14 20:08:20 +05:30
return nodes_[i];
2013-05-14 17:37:04 +05:30
}
template <typename Predicate>
2013-05-14 20:08:20 +05:30
node_info random_node(Predicate const &pred) const {
2013-05-14 17:37:04 +05:30
unsigned nr = get_nr_nodes(pred);
unsigned target = random() % nr;
2013-05-14 20:08:20 +05:30
return get_node(target, pred);
2013-05-14 17:37:04 +05:30
}
template <typename Predicate>
2013-05-14 20:08:20 +05:30
node_array get_random_nodes(unsigned count, Predicate const &pred) const {
2013-05-14 17:37:04 +05:30
unsigned nr = get_nr_nodes(pred);
2013-05-14 20:08:20 +05:30
unsigned target = count + random() % (nr - count);
node_array v;
2013-05-14 17:37:04 +05:30
2013-05-14 20:08:20 +05:30
for (auto n : nodes_) {
if (!target)
break;
2013-05-14 17:37:04 +05:30
2013-05-14 20:08:20 +05:30
if (pred(n)) {
if (target <= count)
v.push_back(n);
target--;
2013-05-14 17:37:04 +05:30
}
}
return v;
}
private:
2013-05-14 20:08:20 +05:30
node_array nodes_;
2013-05-14 17:37:04 +05:30
};
//--------------------------------
2013-05-14 15:31:39 +05:30
template <uint32_t Levels, typename ValueTraits>
2013-05-14 17:37:04 +05:30
class btree_layout_visitor : public btree<Levels, ValueTraits>::visitor {
public:
typedef btree_detail::node_location node_location;
typedef btree<Levels, ValueTraits> tree;
2013-05-14 17:37:04 +05:30
typedef boost::shared_ptr<btree_layout_visitor> ptr;
virtual bool visit_internal(node_location const &loc,
typename tree::internal_node const &n) {
record_node(false, loc, n);
return true;
}
virtual bool visit_internal_leaf(node_location const &loc,
typename tree::internal_node const &n) {
record_node(true, loc, n);
return true;
}
virtual bool visit_leaf(node_location const &loc,
typename tree::leaf_node const &n) {
record_node(true, loc, n);
return true;
}
virtual void visit_complete() {
}
2013-05-14 17:37:04 +05:30
btree_layout get_layout() {
return btree_layout(nodes_);
}
private:
// We rely on the visit order being depth first, lowest to highest.
template <typename N>
void record_node(bool leaf, node_location const &loc, N const &n) {
2013-05-14 15:31:39 +05:30
node_info::ptr ni(new node_info);
ni->path = loc.path;
ni->leaf = leaf;
ni->depth = loc.depth;
ni->b = n.get_location();
if (n.get_nr_entries())
ni->keys = range<uint64_t>(n.key_at(0));
else {
if (loc.key)
ni->keys = range<uint64_t>(*loc.key);
else
ni->keys = range<uint64_t>();
}
if (last_node_at_depth_.size() > loc.depth) {
2013-05-14 15:31:39 +05:30
node_info::ptr &last = last_node_at_depth_[loc.depth];
last->keys.end_ = ni->keys.begin_;
last_node_at_depth_[loc.depth] = ni;
} else
last_node_at_depth_.push_back(ni);
nodes_.push_back(ni);
}
2013-05-14 17:37:04 +05:30
node_ptr_array nodes_;
node_ptr_array last_node_at_depth_;
};
2013-05-14 15:31:39 +05:30
//----------------------------------
MATCHER_P(DamagedKeys, keys, "") {
return arg.lost_keys_ == keys;
}
MATCHER(EmptyPath, "") {
return arg == btree_path();
}
2013-05-08 21:08:38 +05:30
class value_visitor_mock {
public:
MOCK_METHOD2(visit, void(btree_path const &, thing const &));
2013-05-08 21:08:38 +05:30
};
class damage_visitor_mock {
public:
MOCK_METHOD2(visit, void(btree_path const &, btree_detail::damage const &));
2013-05-08 21:08:38 +05:30
};
2013-05-15 18:07:30 +05:30
class DamageTests : public Test {
2013-05-08 16:30:24 +05:30
public:
2013-05-15 18:07:30 +05:30
DamageTests()
2013-05-08 21:08:38 +05:30
: bm_(create_bm<BLOCK_SIZE>(NR_BLOCKS)),
2013-05-09 18:01:04 +05:30
sm_(setup_core_map()),
2013-05-15 18:07:30 +05:30
tm_(new transaction_manager(bm_, sm_)) {
2013-05-09 18:01:04 +05:30
}
2013-05-15 18:07:30 +05:30
virtual ~DamageTests() {}
2013-05-09 18:01:04 +05:30
2013-05-14 17:37:04 +05:30
void tree_complete() {
commit();
discover_layout();
2013-05-08 21:08:38 +05:30
}
2013-05-15 18:07:30 +05:30
void run() {
commit();
run_();
}
2013-05-09 18:01:04 +05:30
void trash_block(block_address b) {
2013-05-08 21:08:38 +05:30
::test::zero_block(bm_, b);
2013-05-08 16:30:24 +05:30
}
2013-05-15 18:07:30 +05:30
//--------------------------------
void expect_no_values() {
EXPECT_CALL(value_visitor_, visit(_, _)).Times(0);
2013-05-15 18:07:30 +05:30
}
void expect_no_damage() {
EXPECT_CALL(damage_visitor_, visit(_, _)).Times(0);
2013-05-15 18:07:30 +05:30
}
//--------------------------------
with_temp_directory dir_;
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager::ptr tm_;
thing_traits::ref_counter rc_;
optional<btree_layout> layout_;
value_visitor_mock value_visitor_;
damage_visitor_mock damage_visitor_;
private:
space_map::ptr setup_core_map() {
space_map::ptr sm(new core_map(NR_BLOCKS));
sm->inc(SUPERBLOCK);
return sm;
}
void commit() {
block_manager<>::write_ref superblock(bm_->superblock(SUPERBLOCK));
}
virtual void discover_layout() = 0;
virtual void run_() = 0;
};
//--------------------------------
class BTreeDamageVisitorTests : public DamageTests {
public:
BTreeDamageVisitorTests()
: tree_(new btree<1, thing_traits>(tm_, rc_)) {
}
2013-05-09 18:01:04 +05:30
void insert_values(unsigned nr) {
for (unsigned i = 0; i < nr; i++) {
uint64_t key[1] = {i};
thing value(i, i + 1234);
2013-05-09 18:01:04 +05:30
tree_->insert(key, value);
}
}
void expect_value_range(uint64_t begin, uint64_t end) {
while (begin < end) {
btree_path path;
path.push_back(begin);
EXPECT_CALL(value_visitor_, visit(Eq(path), Eq(thing(begin, begin + 1234)))).Times(1);
begin++;
}
}
2013-05-09 18:01:04 +05:30
void expect_nr_values(unsigned nr) {
expect_value_range(0, nr);
2013-05-09 18:01:04 +05:30
}
void expect_value(uint64_t n) {
btree_path path;
path.push_back(n);
EXPECT_CALL(value_visitor_, visit(Eq(path), Eq(thing(n, n + 1234)))).Times(1);
2013-05-09 18:01:04 +05:30
}
void expect_damage(range<uint64_t> keys) {
EXPECT_CALL(damage_visitor_, visit(EmptyPath(), DamagedKeys(keys))).Times(1);
}
2013-05-15 18:07:30 +05:30
btree<1, thing_traits>::ptr tree_;
2013-05-09 18:01:04 +05:30
2013-05-15 18:07:30 +05:30
private:
virtual void discover_layout() {
btree_layout_visitor<1, thing_traits> visitor;
tree_->visit_depth_first(visitor);
layout_ = visitor.get_layout();
2013-05-09 18:01:04 +05:30
}
2013-05-15 18:07:30 +05:30
virtual void run_() {
2013-05-09 18:01:04 +05:30
block_counter counter;
btree_visit_values(*tree_, counter, value_visitor_, damage_visitor_);
2013-05-09 18:01:04 +05:30
}
2013-05-15 18:07:30 +05:30
};
2013-05-09 18:01:04 +05:30
2013-05-15 18:07:30 +05:30
//--------------------------------
2013-05-08 21:08:38 +05:30
2013-05-15 18:07:30 +05:30
// 2 level btree
class BTreeDamageVisitor2Tests : public DamageTests {
public:
BTreeDamageVisitor2Tests()
: tree_(new btree<2, thing_traits>(tm_, rc_)) {
}
2013-05-08 21:08:38 +05:30
2013-05-15 18:07:30 +05:30
void insert_values(unsigned nr_sub_trees, unsigned nr_values) {
for (unsigned i = 0; i < nr_sub_trees; i++)
insert_sub_tree_values(i, nr_values);
}
2013-05-15 18:07:30 +05:30
void insert_sub_tree_values(unsigned sub_tree, unsigned nr_values) {
for (unsigned i = 0; i < nr_values; i++) {
uint64_t key[2] = {sub_tree, i};
thing value(key_to_value(key));
tree_->insert(key, value);
}
}
void expect_values(unsigned nr_sub_trees, unsigned nr_values) {
for (unsigned i = 0; i < nr_sub_trees; i++)
expect_sub_tree_values(i, nr_values);
}
void expect_sub_tree_values(unsigned sub_tree, unsigned nr_values) {
for (unsigned i = 0; i < nr_values; i++) {
uint64_t key[2] = {sub_tree, i};
btree_path path;
path.push_back(sub_tree);
path.push_back(i);
EXPECT_CALL(value_visitor_, visit(Eq(path),
Eq(key_to_value(key))));
2013-05-15 18:07:30 +05:30
}
}
2013-05-20 17:01:47 +05:30
void expect_values_except(unsigned nr_sub_trees, unsigned nr_values,
btree_path const &path, range<uint64_t> keys) {
for (unsigned i = 0; i < nr_sub_trees; i++)
expect_sub_tree_values_except(i, nr_values, path, keys);
}
void expect_sub_tree_values_except(unsigned sub_tree, unsigned nr_values,
btree_path const &path, range<uint64_t> keys) {
for (unsigned i = 0; i < nr_values; i++) {
uint64_t key[2] = {sub_tree, i};
if (sub_tree == path[0] && keys.contains(i))
continue;
btree_path p2;
p2.push_back(sub_tree);
p2.push_back(i);
2013-05-20 17:01:47 +05:30
EXPECT_CALL(value_visitor_, visit(Eq(p2), Eq(key_to_value(key))));
}
}
void expect_damage(btree_path path, range<uint64_t> keys) {
EXPECT_CALL(damage_visitor_, visit(Eq(path), DamagedKeys(keys))).Times(1);
}
2013-05-15 18:07:30 +05:30
btree<2, thing_traits>::ptr tree_;
2013-05-14 17:37:04 +05:30
private:
2013-05-15 18:07:30 +05:30
thing key_to_value(uint64_t key[2]) {
return thing(key[0] * 1000000 + key[1], key[1] + 1234);
2013-05-14 17:37:04 +05:30
}
2013-05-15 18:07:30 +05:30
virtual void discover_layout() {
btree_layout_visitor<2, thing_traits> visitor;
2013-05-14 17:37:04 +05:30
tree_->visit_depth_first(visitor);
layout_ = visitor.get_layout();
}
2013-05-15 18:07:30 +05:30
virtual void run_() {
block_counter counter;
btree_visit_values(*tree_, counter, value_visitor_, damage_visitor_);
2013-05-15 18:07:30 +05:30
}
2013-05-08 16:30:24 +05:30
};
}
//----------------------------------------------------------------
TEST_F(BTreeDamageVisitorTests, an_empty_tree)
2013-05-08 16:30:24 +05:30
{
2013-05-09 18:01:04 +05:30
expect_no_values();
expect_no_damage();
2013-05-08 21:08:38 +05:30
2013-05-09 18:01:04 +05:30
run();
2013-05-08 21:08:38 +05:30
}
TEST_F(BTreeDamageVisitorTests, tree_with_a_trashed_root)
2013-05-08 21:08:38 +05:30
{
2013-05-09 18:01:04 +05:30
trash_block(tree_->get_root());
2013-05-08 21:08:38 +05:30
2013-05-09 18:01:04 +05:30
expect_no_values();
expect_damage(range<uint64_t>(0ull));
2013-05-08 21:08:38 +05:30
2013-05-09 18:01:04 +05:30
run();
2013-05-08 16:30:24 +05:30
}
TEST_F(BTreeDamageVisitorTests, populated_tree_with_no_damage)
2013-05-09 18:01:04 +05:30
{
insert_values(10000);
2013-05-09 18:01:04 +05:30
expect_nr_values(10000);
2013-05-09 18:01:04 +05:30
expect_no_damage();
run();
}
2013-05-08 21:08:38 +05:30
TEST_F(BTreeDamageVisitorTests, populated_tree_with_a_damaged_leaf_node)
{
insert_values(10000);
2013-05-14 17:37:04 +05:30
tree_complete();
2013-05-15 18:06:56 +05:30
node_info n = layout_->random_node(is_leaf);
2013-05-14 20:08:20 +05:30
trash_block(n.b);
expect_value_range(0, *n.keys.begin_);
expect_value_range(*n.keys.end_, 10000);
expect_damage(n.keys);
run();
}
TEST_F(BTreeDamageVisitorTests, populated_tree_with_a_sequence_of_damaged_leaf_nodes)
{
insert_values(10000);
2013-05-14 17:37:04 +05:30
tree_complete();
unsigned const COUNT = 5;
2013-05-15 18:06:56 +05:30
node_array nodes = layout_->get_random_nodes(COUNT, is_leaf);
2013-05-14 20:08:20 +05:30
for (auto n : nodes)
trash_block(n.b);
2013-05-14 15:02:28 +05:30
2013-05-14 20:08:20 +05:30
block_address begin = *nodes[0].keys.begin_;
block_address end = *nodes[COUNT - 1].keys.end_;
2013-05-14 20:08:20 +05:30
expect_value_range(0, *nodes[0].keys.begin_);
expect_value_range(*nodes[COUNT - 1].keys.end_, 10000);
expect_damage(range<block_address>(begin, end));
run();
}
TEST_F(BTreeDamageVisitorTests, damaged_first_leaf)
{
insert_values(10000);
2013-05-14 17:37:04 +05:30
tree_complete();
2013-05-15 18:06:56 +05:30
node_info n = layout_->get_node(0, is_leaf);
2013-05-14 20:08:20 +05:30
block_address end = *n.keys.end_;
trash_block(n.b);
expect_damage(range<block_address>(0ull, end));
expect_value_range(end, 10000);
run();
}
TEST_F(BTreeDamageVisitorTests, damaged_last_leaf)
{
insert_values(10000);
2013-05-14 17:37:04 +05:30
tree_complete();
2013-05-14 20:08:20 +05:30
node_info n = layout_->get_node(
2013-05-15 18:06:56 +05:30
layout_->get_nr_nodes(is_leaf) - 1,
is_leaf);
2013-05-14 20:08:20 +05:30
block_address begin = *n.keys.begin_;
trash_block(n.b);
2013-05-14 15:02:28 +05:30
expect_value_range(0, begin);
expect_damage(range<block_address>(begin));
2013-05-14 15:02:28 +05:30
run();
}
TEST_F(BTreeDamageVisitorTests, damaged_internal)
{
insert_values(10000);
2013-05-14 17:37:04 +05:30
tree_complete();
2013-05-15 18:06:56 +05:30
node_info n = layout_->random_node(is_internal);
2013-05-14 20:08:20 +05:30
optional<block_address> begin = n.keys.begin_;
optional<block_address> end = n.keys.end_;
2013-05-14 15:31:39 +05:30
2013-05-14 20:08:20 +05:30
trash_block(n.b);
2013-05-14 15:31:39 +05:30
expect_value_range(0, *begin);
expect_damage(range<block_address>(begin, end));
2013-05-14 15:31:39 +05:30
if (end)
expect_value_range(*end, 10000);
run();
}
2013-05-08 16:30:24 +05:30
//----------------------------------------------------------------
2013-05-15 18:07:30 +05:30
TEST_F(BTreeDamageVisitor2Tests, empty_tree)
{
tree_complete();
2013-05-15 18:07:30 +05:30
expect_no_damage();
expect_no_values();
run();
}
TEST_F(BTreeDamageVisitor2Tests, tree_with_a_trashed_root)
{
tree_complete();
2013-05-15 18:07:30 +05:30
trash_block(tree_->get_root());
expect_no_values();
btree_path path;
expect_damage(path, range<uint64_t>(0ull));
2013-05-15 18:07:30 +05:30
run();
}
TEST_F(BTreeDamageVisitor2Tests, populated_tree_with_no_damage)
{
insert_values(10, 10);
tree_complete();
2013-05-15 18:07:30 +05:30
expect_values(10, 10);
expect_no_damage();
run();
}
2013-05-20 17:01:47 +05:30
TEST_F(BTreeDamageVisitor2Tests, damaged_leaf)
{
insert_values(10, 1000);
tree_complete();
auto leaf1 = [] (node_info const &n) {
return (n.leaf && n.path.size() == 1 && n.path[0] == 1);
};
node_info n = layout_->random_node(leaf1);
cerr << "node: " << n << endl;
trash_block(n.b);
expect_damage(n.path, n.keys);
expect_values_except(10, 1000, n.path, n.keys);
run();
}
2013-05-15 18:07:30 +05:30
//----------------------------------------------------------------