summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/statoperationtest.cpp
blob: e9364b06085e82e402421427848d3c5fb93e6fb7 (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
// 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/stat.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <tests/distributor/distributortestutil.h>
#include <tests/common/make_document_bucket.h>
#include <vespa/storage/distributor/operations/external/statbucketoperation.h>
#include <vespa/storage/distributor/operations/external/statbucketlistoperation.h>
#include <vespa/storage/distributor/distributor.h>

using storage::test::makeDocumentBucket;

namespace storage {
namespace distributor {

struct StatOperationTest : public CppUnit::TestFixture,
                           public DistributorTestUtil
{
    void setUp() override {
        createLinks();
    };

    void tearDown() override {
        close();
    }

    void testBucketInfo();
    void testBucketList();

    CPPUNIT_TEST_SUITE(StatOperationTest);
    CPPUNIT_TEST(testBucketInfo);
    CPPUNIT_TEST(testBucketList);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(StatOperationTest);

void
StatOperationTest::testBucketInfo()
{
    _distributor->enableClusterState(lib::ClusterState("distributor:1 storage:2"));

    addNodesToBucketDB(document::BucketId(16, 5),
                       "0=4/2/100,1=4/2/100");

    StatBucketOperation op(
            getExternalOperationHandler(),
            std::shared_ptr<api::StatBucketCommand>(
                    new api::StatBucketCommand(makeDocumentBucket(document::BucketId(16, 5)), "")));

    op.start(_sender, framework::MilliSecTime(0));

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

    {
        api::StatBucketCommand* tmp(
                static_cast<api::StatBucketCommand*>(_sender.commands[0].get()));
        api::StatBucketReply* reply = new api::StatBucketReply(*tmp, "foo");
        op.receive(_sender, std::shared_ptr<api::StorageReply>(reply));
    }

    {
        api::StatBucketCommand* tmp(
                static_cast<api::StatBucketCommand*>(_sender.commands[1].get()));
        api::StatBucketReply* reply = new api::StatBucketReply(*tmp, "bar");
        op.receive(_sender, std::shared_ptr<api::StorageReply>(reply));
    }

    api::StatBucketReply* replyback(
            static_cast<api::StatBucketReply*>(_sender.replies.back().get()));
    CPPUNIT_ASSERT_CONTAIN("foo", replyback->getResults());
    CPPUNIT_ASSERT_CONTAIN("bar", replyback->getResults());
}

void
StatOperationTest::testBucketList() {
    setupDistributor(2, 2, "distributor:1 storage:2");

    getConfig().setSplitCount(10);
    getConfig().setSplitSize(100);

    for (uint32_t i = 0; i < 2; ++i) {
        insertBucketInfo(document::BucketId(16, 5), i,
                         0xff, 100, 200, true, (i == 1));
    }

    std::shared_ptr<api::GetBucketListCommand> msg(
            new api::GetBucketListCommand(makeDocumentBucket(document::BucketId(16, 5))));

    StatBucketListOperation op(
            getExternalOperationHandler().getBucketDatabase(),
            getIdealStateManager(),
            getExternalOperationHandler().getIndex(),
            msg);
    op.start(_sender, framework::MilliSecTime(0));

    CPPUNIT_ASSERT_EQUAL(1, (int)_sender.replies.size());

    api::GetBucketListReply* repl(
            dynamic_cast<api::GetBucketListReply*>(_sender.replies[0].get()));

    CPPUNIT_ASSERT_EQUAL(1, (int)repl->getBuckets().size());
    CPPUNIT_ASSERT_EQUAL(document::BucketId(16, 5),
                         repl->getBuckets()[0]._bucket);
    CPPUNIT_ASSERT_EQUAL(
            vespalib::string(
                    "[distributor:0] split: "
                    "[Splitting bucket because its maximum size (200 b, 100 docs, 100 meta, 200 b total) "
                    "is higher than the configured limit of (100, 10)] "
                    "[node(idx=0,crc=0xff,docs=100/100,bytes=200/200,trusted=true,active=false,ready=false), "
                    "node(idx=1,crc=0xff,docs=100/100,bytes=200/200,trusted=true,active=true,ready=false)]"),
            repl->getBuckets()[0]._bucketInformation);
}

} // distributor
} // storage