summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/filestorage/filestormodifiedbucketstest.cpp
blob: 856add700e7756c88e4cef8ff228c42785af8775 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/storageapi/message/bucket.h>
#include <vespa/storage/persistence/filestorage/modifiedbucketchecker.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>

using storage::spi::test::makeBucket;

namespace storage {

/**
 * Effectively an integration test between the ModifiedBucketChecker storage
 * link and the behavior of the filestor component.
 */
class FileStorModifiedBucketsTest : public FileStorTestFixture
{
public:
    void modifiedBucketsSendNotifyBucketChange();
    void fileStorRepliesToRecheckBucketCommands();
    
    void modifyBuckets(uint32_t first, uint32_t count);

    spi::dummy::DummyPersistence& getDummyPersistence() {
        return dynamic_cast<spi::dummy::DummyPersistence&>(_node->getPersistenceProvider());
    }

    CPPUNIT_TEST_SUITE(FileStorModifiedBucketsTest);
    CPPUNIT_TEST(modifiedBucketsSendNotifyBucketChange);
    CPPUNIT_TEST(fileStorRepliesToRecheckBucketCommands);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(FileStorModifiedBucketsTest);

namespace {

struct BucketCheckerInjector : FileStorTestFixture::StorageLinkInjector
{
    TestServiceLayerApp& _node;
    FileStorTestFixture& _fixture;
    BucketCheckerInjector(TestServiceLayerApp& node,
                          FileStorTestFixture& fixture)
        : _node(node),
          _fixture(fixture)
    {}
    void inject(DummyStorageLink& link) const override {
       link.push_back(std::unique_ptr<ModifiedBucketChecker>(
            new ModifiedBucketChecker(_node.getComponentRegister(),
                                      _node.getPersistenceProvider(),
                                      _fixture._config->getConfigId())));
    }
};

void
assertIsNotifyCommandWithActiveBucket(api::StorageMessage& msg)
{
    api::NotifyBucketChangeCommand& cmd(
            dynamic_cast<api::NotifyBucketChangeCommand&>(msg));
    CPPUNIT_ASSERT(cmd.getBucketInfo().isActive());
    CPPUNIT_ASSERT_EQUAL(
            vespalib::string("StorageMessageAddress(Storage protocol, "
                             "cluster storage, nodetype distributor, index 0)"),
            cmd.getAddress()->toString());
}

}

void
FileStorModifiedBucketsTest::modifyBuckets(uint32_t first, uint32_t count)
{
    spi::BucketIdListResult::List buckets;
    for (uint32_t i = 0; i < count; ++i) {
        buckets.push_back(document::BucketId(16, first + i));
        _node->getPersistenceProvider().setActiveState(
                makeBucket(buckets[i]),
                spi::BucketInfo::ACTIVE);
    }

    getDummyPersistence().setModifiedBuckets(buckets); 
}

void
FileStorModifiedBucketsTest::modifiedBucketsSendNotifyBucketChange()
{
    BucketCheckerInjector bcj(*_node, *this);
    TestFileStorComponents c(*this, "modifiedBucketsSendNotifyBucketChange", bcj);
    setClusterState("storage:1 distributor:1");

    uint32_t numBuckets = 10;

    for (uint32_t i = 0; i < numBuckets; ++i) {
        document::BucketId bucket(16, i);
        createBucket(makeBucket(bucket));
        c.sendPut(bucket, DocumentIndex(0), PutTimestamp(1000));
    }
    c.top.waitForMessages(numBuckets, MSG_WAIT_TIME);
    c.top.reset();

    modifyBuckets(0, numBuckets);
    c.top.waitForMessages(numBuckets, MSG_WAIT_TIME);

    for (uint32_t i = 0; i < 10; ++i) {
        assertIsNotifyCommandWithActiveBucket(*c.top.getReply(i));

        StorBucketDatabase::WrappedEntry entry(
                _node->getStorageBucketDatabase().get(
                        document::BucketId(16, i), "foo"));

        CPPUNIT_ASSERT(entry->info.isActive());
    }
}

void
FileStorModifiedBucketsTest::fileStorRepliesToRecheckBucketCommands()
{
    BucketCheckerInjector bcj(*_node, *this);
    TestFileStorComponents c(*this, "fileStorRepliesToRecheckBucketCommands", bcj);
    setClusterState("storage:1 distributor:1");

    document::BucketId bucket(16, 0);
    createBucket(makeBucket(bucket));
    c.sendPut(bucket, DocumentIndex(0), PutTimestamp(1000));
    c.top.waitForMessages(1, MSG_WAIT_TIME);
    c.top.reset();

    modifyBuckets(0, 1);
    c.top.waitForMessages(1, MSG_WAIT_TIME);
    assertIsNotifyCommandWithActiveBucket(*c.top.getReply(0));

    // If we don't reply to the recheck bucket commands, we won't trigger
    // a new round of getModifiedBuckets and recheck commands.
    c.top.reset();
    createBucket(makeBucket(document::BucketId(16, 1)));
    modifyBuckets(1, 1);
    c.top.waitForMessages(1, MSG_WAIT_TIME);
    assertIsNotifyCommandWithActiveBucket(*c.top.getReply(0));
}

} // storage