aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/filestorage/deactivatebucketstest.cpp
blob: fa4a537bdb02fd352da73bf4414918ff1af2004a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storageapi/message/bucket.h>
#include <vespa/storageapi/message/state.h>
#include <vespa/persistence/spi/test.h>
#include <tests/persistence/common/persistenceproviderwrapper.h>
#include <vespa/persistence/dummyimpl/dummypersistence.h>
#include <tests/persistence/common/filestortestfixture.h>
#include <vespa/vdslib/state/clusterstate.h>

using storage::spi::test::makeSpiBucket;
using namespace ::testing;

namespace storage {

struct DeactivateBucketsTest : FileStorTestFixture {
    std::unique_ptr<TestFileStorComponents> _c;

    [[nodiscard]] bool is_active(const document::BucketId&) const;

    void SetUp() override {
        FileStorTestFixture::SetUp();
        _c = std::make_unique<TestFileStorComponents>(*this);

        std::string up_state("storage:2 distributor:2");
        _node->getStateUpdater().setClusterState(
                std::make_shared<const lib::ClusterState>(up_state));

        createBucket(test_bucket());

        api::BucketInfo serviceLayerInfo(1, 2, 3, 4, 5, true, true);
        {
            StorBucketDatabase::WrappedEntry entry(
                    _node->getStorageBucketDatabase().get(test_bucket(), "foo",
                                                          StorBucketDatabase::CREATE_IF_NONEXISTING));
            entry->info = serviceLayerInfo;
            entry.write();
        }
    }

    void TearDown() override {
        _c.reset();
        FileStorTestFixture::TearDown();
    }

    static document::BucketId test_bucket() noexcept {
        return {8, 123};
    }

    static std::shared_ptr<const lib::ClusterState> state_of(const char* str) {
        return std::make_shared<const lib::ClusterState>(str);
    }
};

bool
DeactivateBucketsTest::is_active(const document::BucketId& bucket) const
{
    StorBucketDatabase::WrappedEntry entry(
            _node->getStorageBucketDatabase().get(bucket, "foo"));
    assert(entry.exists());
    return entry->info.isActive();
}

TEST_F(DeactivateBucketsTest, buckets_deactivated_when_node_marked_down)
{
    EXPECT_TRUE(is_active(test_bucket()));
    _node->getStateUpdater().setClusterState(state_of("storage:2 .1.s:d distributor:2"));
    // Buckets should have been deactivated in content layer
    EXPECT_FALSE(is_active(test_bucket()));
}

TEST_F(DeactivateBucketsTest, buckets_not_deactivated_when_node_marked_maintenance)
{
    EXPECT_TRUE(is_active(test_bucket()));
    _node->getStateUpdater().setClusterState(state_of("storage:2 .1.s:m distributor:2"));
    EXPECT_TRUE(is_active(test_bucket()));
}

TEST_F(DeactivateBucketsTest, buckets_deactivated_when_node_goes_from_maintenance_to_up)
{
    EXPECT_TRUE(is_active(test_bucket()));
    _node->getStateUpdater().setClusterState(state_of("storage:2 .1.s:m distributor:2"));
    _node->getStateUpdater().setClusterState(state_of("storage:2 distributor:2"));
    EXPECT_FALSE(is_active(test_bucket()));
}

TEST_F(DeactivateBucketsTest, buckets_deactivated_when_node_goes_from_maintenance_to_down)
{
    EXPECT_TRUE(is_active(test_bucket()));
    _node->getStateUpdater().setClusterState(state_of("storage:2 .1.s:m distributor:2"));
    _node->getStateUpdater().setClusterState(state_of("storage:2 .1.s:d distributor:2"));
    EXPECT_FALSE(is_active(test_bucket()));
}

// If we only have a subset of the bucket spaces in maintenance mode (i.e. global
// bucket merge enforcement), we treat this as the node being down from the perspective
// of default space bucket deactivation.
TEST_F(DeactivateBucketsTest, bucket_space_subset_in_maintenance_deactivates_buckets)
{
    EXPECT_TRUE(is_active(test_bucket()));
    auto derived = lib::ClusterStateBundle::BucketSpaceStateMapping({
        {document::FixedBucketSpaces::default_space(), state_of("storage:2 .1.s:m distributor:2")},
        {document::FixedBucketSpaces::global_space(),  state_of("storage:2 distributor:2")}
    });
    _node->getStateUpdater().setClusterStateBundle(
            std::make_shared<const lib::ClusterStateBundle>(*state_of("storage:2 .1.s:m distributor:2"),
                                                            std::move(derived)));
    EXPECT_FALSE(is_active(test_bucket()));
}

// TODO should also test SPI interaction

} // namespace storage