[everything] Fix circular shared pointer references.

We had a cycle from transaction_manager <-> space_map, and also from
the ref_counters back up to the tm.

This prevented objects being destroyed when various programs exited.

From now on we'll try and only use a shared ptr if ownership is
implied.  Otherwise a reference will be used (eg, for up pointers).
This commit is contained in:
Joe Thornber
2014-08-26 11:14:49 +01:00
parent 930cc9d412
commit a7c96c0e1e
31 changed files with 391 additions and 406 deletions

View File

@@ -37,7 +37,9 @@ namespace {
class ArrayTests : public Test {
public:
ArrayTests()
: tm_(create_tm()) {
: bm_(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE)),
sm_(new core_map(NR_BLOCKS)),
tm_(bm_, sm_) {
}
void
@@ -76,15 +78,9 @@ namespace {
array64::ptr a_;
private:
static transaction_manager::ptr
create_tm() {
block_manager<>::ptr bm(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE));
space_map::ptr sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;
}
transaction_manager::ptr tm_;
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager tm_;
};
class value_visitor {

View File

@@ -32,34 +32,40 @@ using namespace testing;
namespace {
block_address const NR_BLOCKS = 102400;
transaction_manager::ptr
create_tm() {
block_manager<>::ptr bm(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE));
space_map::ptr sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;
}
class BitsetTests : public Test {
public:
BitsetTests()
: bm_(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE)),
sm_(new core_map(NR_BLOCKS)),
tm_(bm_, sm_) {
}
bitset::ptr
create_bitset() {
return bitset::ptr(new bitset(create_tm()));
}
bitset::ptr
create_bitset() {
return bitset::ptr(new bitset(tm_));
}
bitset::ptr
open_bitset(block_address root, unsigned count) {
return bitset::ptr(new bitset(create_tm(), root, count));
}
bitset::ptr
open_bitset(block_address root, unsigned count) {
return bitset::ptr(new bitset(tm_, root, count));
}
private:
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager tm_;
};
}
//----------------------------------------------------------------
TEST(BitsetTests, create_empty_bitset)
TEST_F(BitsetTests, create_empty_bitset)
{
bitset::ptr bs = create_bitset();
ASSERT_THROW(bs->get(0), runtime_error);
}
TEST(BitsetTests, grow_default_false)
TEST_F(BitsetTests, grow_default_false)
{
unsigned const COUNT = 100000;
@@ -70,7 +76,7 @@ TEST(BitsetTests, grow_default_false)
ASSERT_FALSE(bs->get(i));
}
TEST(BitsetTests, grow_default_true)
TEST_F(BitsetTests, grow_default_true)
{
unsigned const COUNT = 100000;
@@ -81,7 +87,7 @@ TEST(BitsetTests, grow_default_true)
ASSERT_TRUE(bs->get(i));
}
TEST(BitsetTests, grow_throws_if_actualy_asked_to_shrink)
TEST_F(BitsetTests, grow_throws_if_actualy_asked_to_shrink)
{
unsigned const COUNT = 100000;
@@ -90,7 +96,7 @@ TEST(BitsetTests, grow_throws_if_actualy_asked_to_shrink)
ASSERT_THROW(bs->grow(COUNT / 2, false), runtime_error);
}
TEST(BitsetTests, multiple_grow_calls)
TEST_F(BitsetTests, multiple_grow_calls)
{
unsigned const COUNT = 100000;
unsigned const STEP = 37;
@@ -121,7 +127,7 @@ TEST(BitsetTests, multiple_grow_calls)
}
}
TEST(BitsetTests, set_out_of_bounds_throws)
TEST_F(BitsetTests, set_out_of_bounds_throws)
{
unsigned const COUNT = 100000;
bitset::ptr bs = create_bitset();
@@ -131,7 +137,7 @@ TEST(BitsetTests, set_out_of_bounds_throws)
ASSERT_THROW(bs->set(COUNT, true), runtime_error);
}
TEST(BitsetTests, set_works)
TEST_F(BitsetTests, set_works)
{
unsigned const COUNT = 100000;
bitset::ptr bs = create_bitset();
@@ -144,7 +150,7 @@ TEST(BitsetTests, set_works)
ASSERT_THAT(bs->get(i), Eq(i % 7 ? true : false));
}
TEST(BitsetTests, reopen_works)
TEST_F(BitsetTests, reopen_works)
{
unsigned const COUNT = 100000;
block_address root;

View File

@@ -31,7 +31,7 @@ namespace {
BloomFilterTests()
: bm_(create_bm<BLOCK_SIZE>(NR_BLOCKS)),
sm_(setup_core_map()),
tm_(new transaction_manager(bm_, sm_)) {
tm_(bm_, sm_) {
}
set<block_address> generate_random_blocks(unsigned count,
@@ -73,7 +73,7 @@ namespace {
with_temp_directory dir_;
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager::ptr tm_;
transaction_manager tm_;
boost::random::mt19937 rng_;
};

View File

@@ -25,7 +25,7 @@ namespace {
BTreeCounterTests()
: bm_(create_bm<BLOCK_SIZE>(NR_BLOCKS)),
sm_(setup_core_map()),
tm_(new transaction_manager(bm_, sm_)) {
tm_(bm_, sm_) {
}
void check_nr_metadata_blocks_is_ge(unsigned n) {
@@ -38,7 +38,7 @@ namespace {
with_temp_directory dir_;
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager::ptr tm_;
transaction_manager tm_;
uint64_traits::ref_counter rc_;
btree<1, uint64_traits>::ptr tree_;

View File

@@ -281,7 +281,7 @@ namespace {
DamageTests()
: bm_(create_bm<BLOCK_SIZE>(NR_BLOCKS)),
sm_(setup_core_map()),
tm_(new transaction_manager(bm_, sm_)) {
tm_(bm_, sm_) {
}
virtual ~DamageTests() {}
@@ -315,7 +315,7 @@ namespace {
with_temp_directory dir_;
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager::ptr tm_;
transaction_manager tm_;
thing_traits::ref_counter rc_;
boost::optional<btree_layout> layout_;

View File

@@ -31,21 +31,28 @@ using namespace testing;
namespace {
block_address const NR_BLOCKS = 102400;
transaction_manager::ptr
create_tm() {
block_manager<>::ptr bm(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE));
space_map::ptr sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;
}
class BtreeTests : public Test {
public:
BtreeTests()
: bm_(new block_manager<>("./test.data", NR_BLOCKS, 4, block_manager<>::READ_WRITE)),
sm_(new core_map(NR_BLOCKS)),
tm_(bm_, sm_) {
}
btree<1, uint64_traits>::ptr
create_btree() {
uint64_traits::ref_counter rc;
btree<1, uint64_traits>::ptr
create_btree() {
uint64_traits::ref_counter rc;
return btree<1, uint64_traits>::ptr(
new btree<1, uint64_traits>(tm_, rc));
}
private:
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager tm_;
};
return btree<1, uint64_traits>::ptr(
new btree<1, uint64_traits>(create_tm(), rc));
}
// Checks that a btree is well formed.
//
@@ -99,7 +106,7 @@ namespace {
//----------------------------------------------------------------
TEST(BtreeTests, empty_btree_contains_nothing)
TEST_F(BtreeTests, empty_btree_contains_nothing)
{
btree<1, uint64_traits>::ptr tree = create_btree();
check_constraints(tree);
@@ -110,7 +117,7 @@ TEST(BtreeTests, empty_btree_contains_nothing)
}
}
TEST(BtreeTests, insert_works)
TEST_F(BtreeTests, insert_works)
{
unsigned const COUNT = 100000;
@@ -129,7 +136,7 @@ TEST(BtreeTests, insert_works)
check_constraints(tree);
}
TEST(BtreeTests, insert_does_not_insert_imaginary_values)
TEST_F(BtreeTests, insert_does_not_insert_imaginary_values)
{
btree<1, uint64_traits>::ptr tree = create_btree();
uint64_t key[1] = {0};
@@ -156,7 +163,7 @@ TEST(BtreeTests, insert_does_not_insert_imaginary_values)
check_constraints(tree);
}
TEST(BtreeTests, clone)
TEST_F(BtreeTests, clone)
{
typedef btree<1, uint64_traits> tree64;

View File

@@ -33,278 +33,258 @@ namespace {
block_address const SUPERBLOCK = 0;
block_address const MAX_LOCKS = 8;
transaction_manager::ptr
create_tm() {
block_manager<>::ptr bm(
new block_manager<>("./test.data", NR_BLOCKS, MAX_LOCKS, block_manager<>::READ_WRITE));
space_map::ptr sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(
new transaction_manager(bm, sm));
return tm;
}
struct sm_core_creator {
static space_map::ptr
create() {
return space_map::ptr(new persistent_data::core_map(NR_BLOCKS));
}
};
struct sm_careful_alloc_creator {
static space_map::ptr
create() {
return create_careful_alloc_sm(
checked_space_map::ptr(
new core_map(NR_BLOCKS)));
}
};
struct sm_recursive_creator {
static checked_space_map::ptr
create() {
return create_recursive_sm(
checked_space_map::ptr(
new core_map(NR_BLOCKS)));
}
};
struct sm_disk_creator {
static persistent_space_map::ptr
create() {
transaction_manager::ptr tm = create_tm();
return persistent_data::create_disk_sm(tm, NR_BLOCKS);
class SpaceMapTests : public Test {
public:
SpaceMapTests()
: bm_(new block_manager<>("./test.data", NR_BLOCKS, MAX_LOCKS, block_manager<>::READ_WRITE)),
sm_(new core_map(NR_BLOCKS)),
tm_(bm_, sm_) {
}
static persistent_space_map::ptr
open(void *root) {
transaction_manager::ptr tm = create_tm();
return persistent_data::open_disk_sm(tm, root);
}
};
struct sm_core_creator {
static space_map::ptr
create(transaction_manager &tm) {
return space_map::ptr(new persistent_data::core_map(NR_BLOCKS));
}
};
struct sm_metadata_creator {
static persistent_space_map::ptr
create() {
transaction_manager::ptr tm = create_tm();
return persistent_data::create_metadata_sm(tm, NR_BLOCKS);
struct sm_careful_alloc_creator {
static space_map::ptr
create(transaction_manager &tm) {
return create_careful_alloc_sm(
checked_space_map::ptr(
new core_map(NR_BLOCKS)));
}
};
struct sm_recursive_creator {
static checked_space_map::ptr
create(transaction_manager &tm) {
return create_recursive_sm(
checked_space_map::ptr(
new core_map(NR_BLOCKS)));
}
};
struct sm_disk_creator {
static persistent_space_map::ptr
create(transaction_manager &tm) {
return persistent_data::create_disk_sm(tm, NR_BLOCKS);
}
static persistent_space_map::ptr
open(transaction_manager &tm, void *root) {
return persistent_data::open_disk_sm(tm, root);
}
};
struct sm_metadata_creator {
static persistent_space_map::ptr
create(transaction_manager &tm) {
return persistent_data::create_metadata_sm(tm, NR_BLOCKS);
}
static persistent_space_map::ptr
open(transaction_manager &tm, void *root) {
return persistent_data::open_metadata_sm(tm, root);
}
};
//--------------------------------
void test_get_nr_blocks(space_map::ptr sm) {
ASSERT_THAT(sm->get_nr_blocks(), Eq(NR_BLOCKS));
}
static persistent_space_map::ptr
open(void *root) {
transaction_manager::ptr tm = create_tm();
return persistent_data::open_metadata_sm(tm, root);
void test_get_nr_free(space_map::ptr sm) {
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS));
for (unsigned i = 0; i < NR_BLOCKS; i++) {
boost::optional<block_address> mb = sm->new_block();
ASSERT_TRUE(mb);
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS - i - 1));
}
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->dec(i);
ASSERT_THAT(sm->get_nr_free(), Eq(i + 1));
}
}
};
//--------------------------------
void test_runs_out_of_space(space_map::ptr sm) {
boost::optional<block_address> mb;
void test_get_nr_blocks(space_map::ptr sm)
{
ASSERT_THAT(sm->get_nr_blocks(), Eq(NR_BLOCKS));
}
for (unsigned i = 0; i < NR_BLOCKS; i++)
mb = sm->new_block();
void test_get_nr_free(space_map::ptr sm)
{
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS));
mb = sm->new_block();
ASSERT_FALSE(mb);
}
for (unsigned i = 0; i < NR_BLOCKS; i++) {
void test_inc_and_dec(space_map::ptr sm) {
block_address b = 63;
for (unsigned i = 0; i < 50; i++) {
ASSERT_THAT(sm->get_count(b), Eq(i));
sm->inc(b);
}
for (unsigned i = 50; i > 0; i--) {
ASSERT_THAT(sm->get_count(b), Eq(i));
sm->dec(b);
}
}
void test_not_allocated_twice(space_map::ptr sm) {
boost::optional<block_address> mb = sm->new_block();
ASSERT_TRUE(mb);
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS - i - 1));
for (;;) {
boost::optional<block_address> b = sm->new_block();
if (!b)
break;
if (b)
ASSERT_TRUE(*b != *mb);
}
}
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->dec(i);
ASSERT_THAT(sm->get_nr_free(), Eq(i + 1));
}
}
void test_runs_out_of_space(space_map::ptr sm)
{
boost::optional<block_address> mb;
for (unsigned i = 0; i < NR_BLOCKS; i++)
mb = sm->new_block();
mb = sm->new_block();
ASSERT_FALSE(mb);
}
void test_inc_and_dec(space_map::ptr sm)
{
block_address b = 63;
for (unsigned i = 0; i < 50; i++) {
ASSERT_THAT(sm->get_count(b), Eq(i));
sm->inc(b);
void test_set_count(space_map::ptr sm) {
sm->set_count(43, 5);
ASSERT_THAT(sm->get_count(43), Eq(5u));
}
for (unsigned i = 50; i > 0; i--) {
ASSERT_THAT(sm->get_count(b), Eq(i));
sm->dec(b);
}
}
void test_set_affects_nr_allocated(space_map::ptr sm) {
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->set_count(i, 1);
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS - i - 1));
}
void test_not_allocated_twice(space_map::ptr sm)
{
boost::optional<block_address> mb = sm->new_block();
ASSERT_TRUE(mb);
for (;;) {
boost::optional<block_address> b = sm->new_block();
if (!b)
break;
if (b)
ASSERT_TRUE(*b != *mb);
}
}
void test_set_count(space_map::ptr sm)
{
sm->set_count(43, 5);
ASSERT_THAT(sm->get_count(43), Eq(5u));
}
void test_set_affects_nr_allocated(space_map::ptr sm)
{
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->set_count(i, 1);
ASSERT_THAT(sm->get_nr_free(), Eq(NR_BLOCKS - i - 1));
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->set_count(i, 0);
ASSERT_THAT(sm->get_nr_free(), Eq(i + 1));
}
}
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->set_count(i, 0);
ASSERT_THAT(sm->get_nr_free(), Eq(i + 1));
}
}
// Ref counts below 3 gets stored as bitmaps, above 3 they go into
// a btree with uint32_t values. Worth checking this thoroughly,
// especially for the metadata format which may have complications
// due to recursion.
void test_high_ref_counts(space_map::ptr sm)
{
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
sm->set_count(i, rand() % 6789);
sm->commit();
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->inc(i);
sm->inc(i);
if (i % 1000)
sm->commit();
}
sm->commit();
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
ASSERT_THAT(sm->get_count(i), Eq((rand() % 6789u) + 2u));
for (unsigned i = 0; i < NR_BLOCKS; i++)
sm->dec(i);
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
ASSERT_THAT(sm->get_count(i), Eq((rand() % 6789u) + 1u));
}
template <typename SMCreator>
void test_sm_reopen()
{
unsigned char buffer[128];
{
persistent_space_map::ptr sm = SMCreator::create();
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
sm->inc(i);
// Ref counts below 3 gets stored as bitmaps, above 3 they go into
// a btree with uint32_t values. Worth checking this thoroughly,
// especially for the metadata format which may have complications
// due to recursion.
void test_high_ref_counts(space_map::ptr sm) {
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
sm->set_count(i, rand() % 6789);
sm->commit();
ASSERT_THAT(sm->root_size(), Le(sizeof(buffer)));
sm->copy_root(buffer, sizeof(buffer));
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->inc(i);
sm->inc(i);
if (i % 1000)
sm->commit();
}
sm->commit();
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
ASSERT_THAT(sm->get_count(i), Eq((rand() % 6789u) + 2u));
for (unsigned i = 0; i < NR_BLOCKS; i++)
sm->dec(i);
srand(1234);
for (unsigned i = 0; i < NR_BLOCKS; i++)
ASSERT_THAT(sm->get_count(i), Eq((rand() % 6789u) + 1u));
}
{
persistent_space_map::ptr sm = SMCreator::open(buffer);
template <typename SMCreator>
void test_sm_reopen() {
unsigned char buffer[128];
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
ASSERT_THAT(sm->get_count(i), Eq(1u));
{
persistent_space_map::ptr sm = SMCreator::create(tm_);
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
sm->inc(i);
sm->commit();
ASSERT_THAT(sm->root_size(), Le(sizeof(buffer)));
sm->copy_root(buffer, sizeof(buffer));
}
{
persistent_space_map::ptr sm = SMCreator::open(tm_, buffer);
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
ASSERT_THAT(sm->get_count(i), Eq(1u));
}
}
}
typedef void (*sm_test)(space_map::ptr);
template <typename SMCreator, unsigned NTests>
void do_tests(sm_test (&tests)[NTests])
{
for (unsigned t = 0; t < NTests; t++) {
space_map::ptr sm = SMCreator::create();
tests[t](sm);
template <typename SMCreator>
void do_tests() {
test_get_nr_blocks(SMCreator::create(tm_));
test_get_nr_free(SMCreator::create(tm_));
test_runs_out_of_space(SMCreator::create(tm_));
test_inc_and_dec(SMCreator::create(tm_));
test_not_allocated_twice(SMCreator::create(tm_));
test_set_count(SMCreator::create(tm_));
test_set_affects_nr_allocated(SMCreator::create(tm_));
test_high_ref_counts(SMCreator::create(tm_));
}
}
sm_test space_map_tests[] = {
test_get_nr_blocks,
test_get_nr_free,
test_runs_out_of_space,
test_inc_and_dec,
test_not_allocated_twice,
test_set_count,
test_set_affects_nr_allocated,
test_high_ref_counts
void
copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) {
for (block_address b = 0; b < rhs->get_nr_blocks(); b++) {
uint32_t count = rhs->get_count(b);
if (count > 0)
lhs->set_count(b, rhs->get_count(b));
}
}
block_manager<>::ptr bm_;
space_map::ptr sm_;
transaction_manager tm_;
};
void
copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) {
for (block_address b = 0; b < rhs->get_nr_blocks(); b++) {
uint32_t count = rhs->get_count(b);
if (count > 0)
lhs->set_count(b, rhs->get_count(b));
}
}
}
//----------------------------------------------------------------
TEST(SpaceMapTests, test_sm_core)
TEST_F(SpaceMapTests, test_sm_core)
{
do_tests<sm_core_creator>(space_map_tests);
do_tests<sm_core_creator>();
}
TEST(SpaceMapTests, test_sm_careful_alloc)
TEST_F(SpaceMapTests, test_sm_careful_alloc)
{
do_tests<sm_careful_alloc_creator>(space_map_tests);
do_tests<sm_careful_alloc_creator>();
}
TEST(SpaceMapTests, test_sm_recursive)
TEST_F(SpaceMapTests, test_sm_recursive)
{
do_tests<sm_recursive_creator>(space_map_tests);
do_tests<sm_recursive_creator>();
}
TEST(SpaceMapTests, test_sm_disk)
TEST_F(SpaceMapTests, test_sm_disk)
{
do_tests<sm_disk_creator>(space_map_tests);
do_tests<sm_disk_creator>();
test_sm_reopen<sm_disk_creator>();
}
TEST(SpaceMapTests, test_sm_metadata)
TEST_F(SpaceMapTests, test_sm_metadata)
{
do_tests<sm_metadata_creator>(space_map_tests);
do_tests<sm_metadata_creator>();
test_sm_reopen<sm_metadata_creator>();
}
TEST(SpaceMapTests, test_metadata_and_disk)
TEST_F(SpaceMapTests, test_metadata_and_disk)
{
block_manager<>::ptr bm(
new block_manager<>("./test.data", NR_BLOCKS, MAX_LOCKS, block_manager<>::READ_WRITE));
space_map::ptr core_sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(new transaction_manager(bm, core_sm));
persistent_space_map::ptr metadata_sm = persistent_data::create_metadata_sm(tm, NR_BLOCKS);
persistent_space_map::ptr metadata_sm = persistent_data::create_metadata_sm(*tm, NR_BLOCKS);
copy_space_maps(metadata_sm, core_sm);
tm->set_sm(metadata_sm);
persistent_space_map::ptr data_sm_ = create_disk_sm(tm, NR_BLOCKS * 2);
persistent_space_map::ptr data_sm_ = create_disk_sm(*tm, NR_BLOCKS * 2);
}
//----------------------------------------------------------------