summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/idealstatemanagertest.cpp
blob: 0a36e5cd0e5d97d40921a34bdd9dd20563e2e0fa (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <tests/common/dummystoragelink.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/storage/distributor/bucketdbupdater.h>
#include <vespa/storage/distributor/distributor.h>
#include <vespa/storage/distributor/distributor_stripe.h>
#include <vespa/storage/distributor/operations/idealstate/mergeoperation.h>
#include <vespa/storage/distributor/operation_sequencer.h>
#include <vespa/storageapi/message/stat.h>
#include <vespa/storageapi/message/visitor.h>
#include <vespa/storageapi/message/bucketsplitting.h>
#include <tests/distributor/distributortestutil.h>
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/document/test/make_bucket_space.h>
#include <vespa/vespalib/gtest/gtest.h>
#include "dummy_cluster_context.h"

using document::test::makeDocumentBucket;
using document::test::makeBucketSpace;
using document::FixedBucketSpaces;
using namespace ::testing;

namespace storage::distributor {

struct IdealStateManagerTest : Test, DistributorTestUtil {
    IdealStateManagerTest()
        : Test(),
          DistributorTestUtil(),
          _bucketSpaces()
    {}
    void SetUp() override {
        createLinks();
        _bucketSpaces = getBucketSpaces();
    };

    void TearDown() override {
        close();
    }

    bool checkBlock(const IdealStateOperation& op,
                    const document::Bucket& bucket,
                    const PendingMessageTracker& tracker,
                    const OperationSequencer& op_seq) const
    {
        return op.checkBlock(bucket, tracker, op_seq);
    }

    bool checkBlockForAllNodes(const IdealStateOperation& op,
                               const document::Bucket& bucket,
                               const PendingMessageTracker& tracker,
                               const OperationSequencer& op_seq) const
    {
        return op.checkBlockForAllNodes(bucket, tracker, op_seq);
    }

    std::vector<document::BucketSpace> _bucketSpaces;
    std::string makeBucketStatusString(const std::string &defaultSpaceBucketStatus);
};

TEST_F(IdealStateManagerTest, sibling) {
    EXPECT_EQ(document::BucketId(1,1),
              getIdealStateManager().operation_context()
              .get_sibling(document::BucketId(1, 0)));
    EXPECT_EQ(document::BucketId(1,0),
              getIdealStateManager().operation_context()
              .get_sibling(document::BucketId(1, 1)));
    EXPECT_EQ(document::BucketId(2,3),
              getIdealStateManager().operation_context()
              .get_sibling(document::BucketId(2, 1)));
    EXPECT_EQ(document::BucketId(2,1),
              getIdealStateManager().operation_context()
              .get_sibling(document::BucketId(2, 3)));
}

TEST_F(IdealStateManagerTest, status_page) {
    close();
    getDirConfig().getConfig("stor-distributormanager").set("splitsize", "100");
    getDirConfig().getConfig("stor-distributormanager").set("splitcount", "1000000");
    getDirConfig().getConfig("stor-distributormanager").set("joinsize", "0");
    getDirConfig().getConfig("stor-distributormanager").set("joincount", "0");
    createLinks();
    setupDistributor(1, 1, "distributor:1 storage:1");

    insertBucketInfo(document::BucketId(16, 5), 0, 0xff, 100, 200, true, true);
    insertBucketInfo(document::BucketId(16, 2), 0, 0xff, 10, 10, true, true);

    std::ostringstream ost;
    getIdealStateManager().getBucketStatus(ost);

    EXPECT_EQ(makeBucketStatusString("BucketId(0x4000000000000002) : [node(idx=0,crc=0xff,docs=10/10,bytes=10/10,trusted=true,active=true,ready=false)]<br>\n"
                                     "<b>BucketId(0x4000000000000005):</b> <i> : split: [Splitting bucket because its maximum size (200 b, 100 docs, 100 meta, 200 b total) is "
                                     "higher than the configured limit of (100, 1000000)]</i> [node(idx=0,crc=0xff,docs=100/100,bytes=200/200,trusted=true,"
                                     "active=true,ready=false)]<br>\n"),
              ost.str());
}

TEST_F(IdealStateManagerTest, disabled_state_checker) {
    setupDistributor(1, 1, "distributor:1 storage:1");

    getConfig().setSplitSize(100);
    getConfig().setSplitCount(1000000);
    getConfig().disableStateChecker("SplitBucket");

    insertBucketInfo(document::BucketId(16, 5), 0, 0xff, 100, 200, true, true);
    insertBucketInfo(document::BucketId(16, 2), 0, 0xff, 10, 10, true, true);

    std::ostringstream ost;
    getIdealStateManager().getBucketStatus(ost);

    EXPECT_EQ(makeBucketStatusString(
        "BucketId(0x4000000000000002) : [node(idx=0,crc=0xff,docs=10/10,bytes=10/10,trusted=true,active=true,ready=false)]<br>\n"
         "<b>BucketId(0x4000000000000005):</b> <i> : split: [Splitting bucket because its maximum size (200 b, 100 docs, 100 meta, 200 b total) is "
         "higher than the configured limit of (100, 1000000)]</i> [node(idx=0,crc=0xff,docs=100/100,bytes=200/200,trusted=true,"
         "active=true,ready=false)]<br>\n"),
         ost.str());

    tick();
    EXPECT_EQ("", active_ideal_state_operations());

}

TEST_F(IdealStateManagerTest, clear_active_on_node_down) {
    setSystemState(lib::ClusterState("distributor:1 storage:3"));
    for (int i = 1; i < 4; i++) {
        insertBucketInfo(document::BucketId(16, i), 0, 0xff, 100, 200);
        insertBucketInfo(document::BucketId(16, i), 1, 0xffe, 1020, 2300);
        insertBucketInfo(document::BucketId(16, i), 2, 0xfff, 1030, 2400);
    }

    tick();

    // Start all three operations.
    for (uint32_t i = 0; i < 3; ++i) {
        tick();
    }

    // Node 2 gets activated for each bucket as it has the most documents.
    EXPECT_EQ("setbucketstate to [2] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000001)) (pri 100)\n"
              "setbucketstate to [2] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000002)) (pri 100)\n"
              "setbucketstate to [2] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000003)) (pri 100)\n",
              active_ideal_state_operations());

    setSystemState(lib::ClusterState("distributor:1 storage:3 .2.s:d"));

    EXPECT_EQ("", active_ideal_state_operations());
    EXPECT_EQ(0, pending_message_tracker().getNodeInfo().getPendingCount(0));
}

TEST_F(IdealStateManagerTest, recheck_when_active) {
    for (uint32_t j = 0; j < 3; j++) {
        insertBucketInfo(document::BucketId(16, 1), j, 0xff - j, 100, 200);
    }

    setSystemState(lib::ClusterState("distributor:1 storage:3"));

    tick();

    EXPECT_EQ("setbucketstate to [0] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000001)) (pri 100)\n",
              active_ideal_state_operations());

    tick();

    EXPECT_EQ("setbucketstate to [0] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000001)) (pri 100)\n",
              active_ideal_state_operations());

    tick();

    EXPECT_EQ("setbucketstate to [0] Bucket(BucketSpace(0x0000000000000001), BucketId(0x4000000000000001)) (pri 100)\n",
              active_ideal_state_operations());
}

TEST_F(IdealStateManagerTest, block_ideal_state_ops_on_full_request_bucket_info) {

    setupDistributor(2, 10, "distributor:1 storage:2");

    framework::defaultimplementation::FakeClock clock;
    PendingMessageTracker tracker(_node->getComponentRegister());
    OperationSequencer op_seq;

    document::BucketId bid(16, 1234);
    std::vector<document::BucketId> buckets;

    // RequestBucketInfoCommand does not have a specific bucketid since it's
    // sent to the entire node. It will then use a null bucketid.
    {
        auto msg = std::make_shared<api::RequestBucketInfoCommand>(makeBucketSpace(), buckets);
        msg->setAddress(api::StorageMessageAddress::create(dummy_cluster_context.cluster_name_ptr(), lib::NodeType::STORAGE, 4));
        tracker.insert(msg);
    }

    {
        RemoveBucketOperation op(dummy_cluster_context,
                                 BucketAndNodes(makeDocumentBucket(bid), toVector<uint16_t>(3, 4)));
        EXPECT_TRUE(op.isBlocked(tracker, op_seq));
    }

    {
        // Don't trigger on requests to other nodes.
        RemoveBucketOperation op(dummy_cluster_context,
                                 BucketAndNodes(makeDocumentBucket(bid), toVector<uint16_t>(3, 5)));
        EXPECT_FALSE(op.isBlocked(tracker, op_seq));
    }

    // Don't block on null-bucket messages that aren't RequestBucketInfo.
    {
        auto msg = std::make_shared<api::CreateVisitorCommand>(makeBucketSpace(), "foo", "bar", "baz");
        msg->setAddress(api::StorageMessageAddress::create(dummy_cluster_context.cluster_name_ptr(), lib::NodeType::STORAGE, 7));
        tracker.insert(msg);
    }

    {
        RemoveBucketOperation op(dummy_cluster_context,
                                 BucketAndNodes(makeDocumentBucket(bid), toVector<uint16_t>(7)));
        EXPECT_FALSE(op.isBlocked(tracker, op_seq));
    }
}

TEST_F(IdealStateManagerTest, block_check_for_all_operations_to_specific_bucket) {
    setupDistributor(2, 10, "distributor:1 storage:2");
    framework::defaultimplementation::FakeClock clock;
    PendingMessageTracker tracker(_node->getComponentRegister());
    OperationSequencer op_seq;
    document::BucketId bid(16, 1234);

    {
        auto msg = std::make_shared<api::JoinBucketsCommand>(makeDocumentBucket(bid));
        msg->setAddress(api::StorageMessageAddress::create(dummy_cluster_context.cluster_name_ptr(), lib::NodeType::STORAGE, 4));
        tracker.insert(msg);
    }
    {
        RemoveBucketOperation op(dummy_cluster_context,
                                 BucketAndNodes(makeDocumentBucket(bid), toVector<uint16_t>(7)));
        // Not blocked for exact node match.
        EXPECT_FALSE(checkBlock(op, makeDocumentBucket(bid), tracker, op_seq));
        // But blocked for bucket match!
        EXPECT_TRUE(checkBlockForAllNodes(op, makeDocumentBucket(bid), tracker, op_seq));
    }
}

TEST_F(IdealStateManagerTest, block_operations_with_locked_buckets) {
    setupDistributor(2, 10, "distributor:1 storage:2");
    framework::defaultimplementation::FakeClock clock;
    PendingMessageTracker tracker(_node->getComponentRegister());
    OperationSequencer op_seq;
    const auto bucket = makeDocumentBucket(document::BucketId(16, 1234));

    {
        auto msg = std::make_shared<api::JoinBucketsCommand>(bucket);
        msg->setAddress(api::StorageMessageAddress::create(dummy_cluster_context.cluster_name_ptr(), lib::NodeType::STORAGE, 1));
        tracker.insert(msg);
    }
    auto token = op_seq.try_acquire(bucket, "foo");
    EXPECT_TRUE(token.valid());
    {
        RemoveBucketOperation op(dummy_cluster_context, BucketAndNodes(bucket, toVector<uint16_t>(0)));
        EXPECT_TRUE(checkBlock(op, bucket, tracker, op_seq));
        EXPECT_TRUE(checkBlockForAllNodes(op, bucket, tracker, op_seq));
    }
}

std::string
IdealStateManagerTest::makeBucketStatusString(const std::string &defaultSpaceBucketStatus)
{
    std::ostringstream ost;
    for (const auto &bucketSpace : _bucketSpaces) {
        ost << "<h2>" << FixedBucketSpaces::to_string(bucketSpace) << " - " << bucketSpace << "</h2>\n";
        if (bucketSpace == FixedBucketSpaces::default_space()) {
            ost << defaultSpaceBucketStatus;
        }
    }
    return ost.str();
}

} // storage::distributor