aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/removebucketoperationtest.cpp
blob: f289d374509d4d0cc6eaab68c0d9d86037b3a53f (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
145
146
147
148
149
150
151
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <cppunit/extensions/HelperMacros.h>
#include <tests/common/dummystoragelink.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/storageapi/message/bucket.h>
#include <vespa/storage/distributor/operations/idealstate/removebucketoperation.h>
#include <vespa/storage/distributor/idealstatemanager.h>
#include <vespa/storage/distributor/distributor.h>
#include <tests/distributor/distributortestutil.h>
#include <vespa/document/test/make_document_bucket.h>

using document::test::makeDocumentBucket;

namespace storage {
namespace distributor {

class RemoveBucketOperationTest : public CppUnit::TestFixture,
                                  public DistributorTestUtil
{
    CPPUNIT_TEST_SUITE(RemoveBucketOperationTest);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testBucketInfoMismatchFailure);
    CPPUNIT_TEST(testFailWithInvalidBucketInfo);
    CPPUNIT_TEST_SUITE_END();

protected:
    void testSimple();
    void testBucketInfoMismatchFailure();
    void testFailWithInvalidBucketInfo();

public:
    void setUp() override {
        createLinks();
    };

    void tearDown() override {
        close();
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(RemoveBucketOperationTest);

void
RemoveBucketOperationTest::testSimple()
{
    addNodesToBucketDB(document::BucketId(16, 1),
                       "0=10/100/1/t,"
                       "1=10/100/1/t,"
                       "2=10/100/1/t");
    setRedundancy(1);
    _distributor->enableClusterState(lib::ClusterState("distributor:1 storage:3"));

    RemoveBucketOperation op("storage",
                             BucketAndNodes(makeDocumentBucket(document::BucketId(16, 1)),
                                     toVector<uint16_t>(1,2)));
    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender, framework::MilliSecTime(0));


    CPPUNIT_ASSERT_EQUAL(std::string("Delete bucket => 1,"
                                     "Delete bucket => 2"),
                         _sender.getCommands(true));

    sendReply(op, 0);
    sendReply(op, 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x4000000000000001) : "
                    "node(idx=0,crc=0xa,docs=100/100,bytes=1/1,trusted=true,active=false,ready=false)"),
            dumpBucket(document::BucketId(16, 1)));
}

/**
 * Test that receiving a DeleteBucket failure from a storage node that sends
 * back actual bucket info reinserts that bucket info into the distributor
 * bucket database.
 */
void
RemoveBucketOperationTest::testBucketInfoMismatchFailure()
{
    addNodesToBucketDB(document::BucketId(16, 1), "1=0/0/0/t");

    getComponentRegisterImpl().setDistribution(std::shared_ptr<lib::Distribution>(
            new lib::Distribution(
                lib::Distribution::getDefaultDistributionConfig(1, 10))));

    _distributor->enableClusterState(lib::ClusterState("distributor:1 storage:2"));

    RemoveBucketOperation op("storage",
                             BucketAndNodes(makeDocumentBucket(document::BucketId(16, 1)),
                                     toVector<uint16_t>(1)));
    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender, framework::MilliSecTime(0));

    CPPUNIT_ASSERT_EQUAL(std::string("Delete bucket => 1"),
                         _sender.getCommands(true));

    CPPUNIT_ASSERT_EQUAL((size_t) 1, _sender.commands.size());
    std::shared_ptr<api::StorageCommand> msg2  = _sender.commands[0];
    std::shared_ptr<api::StorageReply> reply(msg2->makeReply().release());
    dynamic_cast<api::DeleteBucketReply&>(*reply).setBucketInfo(
            api::BucketInfo(10, 100, 1));
    reply->setResult(api::ReturnCode::REJECTED);
    op.receive(_sender, reply);

    // RemoveBucketOperation should reinsert bucketinfo into database
    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x4000000000000001) : "
                    "node(idx=1,crc=0xa,docs=100/100,bytes=1/1,trusted=true,active=false,ready=false)"),
            dumpBucket(document::BucketId(16, 1)));
}

/**
 * Test that receiving a DeleteBucket failure from a storage node that does
 * not include valid BucketInfo in its reply does not reinsert the bucket
 * into the distributor.
 */
void
RemoveBucketOperationTest::testFailWithInvalidBucketInfo()
{
    addNodesToBucketDB(document::BucketId(16, 1), "1=0/0/0/t");

    getComponentRegisterImpl().setDistribution(std::shared_ptr<lib::Distribution>(
            new lib::Distribution(
                lib::Distribution::getDefaultDistributionConfig(1, 10))));

    _distributor->enableClusterState(lib::ClusterState("distributor:1 storage:2"));

    RemoveBucketOperation op("storage",
                             BucketAndNodes(makeDocumentBucket(document::BucketId(16, 1)),
                                     toVector<uint16_t>(1)));
    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender, framework::MilliSecTime(0));

    CPPUNIT_ASSERT_EQUAL(std::string("Delete bucket => 1"),
                         _sender.getCommands(true));

    CPPUNIT_ASSERT_EQUAL((size_t) 1, _sender.commands.size());
    std::shared_ptr<api::StorageCommand> msg2  = _sender.commands[0];
    std::shared_ptr<api::StorageReply> reply(msg2->makeReply().release());
    reply->setResult(api::ReturnCode::ABORTED);
    op.receive(_sender, reply);

    CPPUNIT_ASSERT_EQUAL(std::string("NONEXISTING"),
                         dumpBucket(document::BucketId(16, 1)));
}

} // distributor
} // storage