aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/bucketownershipnotifiertest.cpp
blob: 9699e0907e30baf8d4c5e98ea2a3f3de0485ba4a (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
// 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 <tests/distributor/messagesenderstub.h>
#include <tests/common/teststorageapp.h>
#include <vespa/storage/persistence/bucketownershipnotifier.h>
#include <vespa/document/test/make_document_bucket.h>

using document::test::makeDocumentBucket;

namespace storage {

class BucketOwnershipNotifierTest : public CppUnit::TestFixture
{
    std::unique_ptr<TestServiceLayerApp> _app;
    lib::ClusterState _clusterState;
public:

    BucketOwnershipNotifierTest()
        : _app(),
          _clusterState("distributor:2 storage:1")
    {}

    void setUp() override;

    CPPUNIT_TEST_SUITE(BucketOwnershipNotifierTest);
    CPPUNIT_TEST(testSendNotifyBucketChangeIfOwningDistributorChanged);
    CPPUNIT_TEST(testDoNotSendNotifyBucketChangeIfBucketOwnedByInitialSender);
    CPPUNIT_TEST(testIgnoreIdealStateCalculationExceptions);
    CPPUNIT_TEST(testGuardNotifyAlways);
    CPPUNIT_TEST_SUITE_END();

    bool ownsBucket(uint16_t distributorIndex,
                    const document::BucketId& bucket) const
    {
        uint16_t distributor = _app->getDistribution()->getIdealDistributorNode(
                _clusterState, bucket);
        return distributor == distributorIndex;
    }
    
    document::Bucket getFirstNonOwnedBucket() {
        for (int i = 0; i < 1000; ++i) {
            if (!ownsBucket(0, document::BucketId(16, i))) {
                return makeDocumentBucket(document::BucketId(16, i));
            }
        }
        return makeDocumentBucket(document::BucketId(0));
    }
        
    document::Bucket getFirstOwnedBucket() {
        for (int i = 0; i < 1000; ++i) {
            if (ownsBucket(0, document::BucketId(16, i))) {
                return makeDocumentBucket(document::BucketId(16, i));
            }
        }
        return makeDocumentBucket(document::BucketId(0));
    }


    void testSendNotifyBucketChangeIfOwningDistributorChanged();
    void testDoNotSendNotifyBucketChangeIfBucketOwnedByInitialSender();
    void testIgnoreIdealStateCalculationExceptions();
    void testGuardNotifyAlways();

    void doTestNotification(const document::Bucket &bucket,
                            const api::BucketInfo& info,
                            const std::string& wantedSend);
};

CPPUNIT_TEST_SUITE_REGISTRATION(BucketOwnershipNotifierTest);

void
BucketOwnershipNotifierTest::setUp()
{
    _app.reset(new TestServiceLayerApp);
    _app->setDistribution(Redundancy(1), NodeCount(2));
    _app->setClusterState(_clusterState);
}

void
BucketOwnershipNotifierTest::doTestNotification(const document::Bucket &bucket,
                                                const api::BucketInfo& info,
                                                const std::string& wantedSend)
{
    ServiceLayerComponent component(_app->getComponentRegister(), "dummy");
    MessageSenderStub sender;

    BucketOwnershipNotifier notifier(component, sender);

    notifier.notifyIfOwnershipChanged(bucket, 0, info);

    CPPUNIT_ASSERT_EQUAL(wantedSend, sender.getCommands(true, true));
}

void
BucketOwnershipNotifierTest::testSendNotifyBucketChangeIfOwningDistributorChanged()
{
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstNonOwnedBucket());
    CPPUNIT_ASSERT(bucket.getBucketId().getRawId() != 0);

    std::ostringstream wanted;
    wanted << "NotifyBucketChangeCommand("
           << bucket.getBucketId()
           << ", " << info
           << ") => 1";

    doTestNotification(bucket, info, wanted.str());
}

void
BucketOwnershipNotifierTest::testDoNotSendNotifyBucketChangeIfBucketOwnedByInitialSender()
{
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstOwnedBucket());
    CPPUNIT_ASSERT(bucket.getBucketId().getRawId() != 0);

    doTestNotification(bucket, info, "");
}

void
BucketOwnershipNotifierTest::testIgnoreIdealStateCalculationExceptions()
{
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstNonOwnedBucket());
    CPPUNIT_ASSERT(bucket.getBucketId().getRawId() != 0);

    _app->setClusterState(lib::ClusterState("distributor:0 storage:1"));

    doTestNotification(bucket, info, "");
}

void
BucketOwnershipNotifierTest::testGuardNotifyAlways()
{
    ServiceLayerComponent component(_app->getComponentRegister(), "dummy");
    MessageSenderStub sender;
    BucketOwnershipNotifier notifier(component, sender);
    std::ostringstream wanted;
    {
        NotificationGuard guard(notifier);

        api::BucketInfo info(0x1, 2, 3);
        document::Bucket bucket1(getFirstOwnedBucket());
        guard.notifyAlways(bucket1, info);

        document::Bucket bucket2(getFirstNonOwnedBucket());
        guard.notifyAlways(bucket2, info);

        wanted << "NotifyBucketChangeCommand("
               << bucket1.getBucketId()
               << ", " << info
               << ") => 0,"
               << "NotifyBucketChangeCommand("
               << bucket2.getBucketId()
               << ", " << info
               << ") => 1";
    }

    CPPUNIT_ASSERT_EQUAL(wanted.str(), sender.getCommands(true, true));
}

} // storage