aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/bucketownershipnotifiertest.cpp
blob: 129cac34c6859c2156e9ef8ffbd097478d761786 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <tests/common/message_sender_stub.h>
#include <tests/common/teststorageapp.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/storage/persistence/bucketownershipnotifier.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/vespalib/gtest/gtest.h>

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

namespace storage {

struct BucketOwnershipNotifierTest : public Test {
    std::unique_ptr<TestServiceLayerApp> _app;
    lib::ClusterState _clusterState;

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

    void SetUp() override;

    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 doTestNotification(const document::Bucket &bucket,
                            const api::BucketInfo& info,
                            const std::string& wantedSend);
};

void
BucketOwnershipNotifierTest::SetUp()
{
    _app = std::make_unique<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);

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

TEST_F(BucketOwnershipNotifierTest, send_notify_bucket_change_if_owning_distributor_changed) {
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstNonOwnedBucket());
    ASSERT_NE(bucket.getBucketId().getRawId(), 0ULL);

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

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

TEST_F(BucketOwnershipNotifierTest, do_not_send_notify_bucket_change_if_bucket_owned_by_initial_sender) {
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstOwnedBucket());
    ASSERT_NE(bucket.getBucketId().getRawId(), 0ULL);

    doTestNotification(bucket, info, "");
}

TEST_F(BucketOwnershipNotifierTest, ignore_ideal_state_calculation_exceptions) {
    api::BucketInfo info(0x1, 2, 3);
    document::Bucket bucket(getFirstNonOwnedBucket());
    ASSERT_NE(bucket.getBucketId().getRawId(), 0ULL);

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

    doTestNotification(bucket, info, "");
}

TEST_F(BucketOwnershipNotifierTest, guard_notify_always) {
    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";
    }

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

} // storage