summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/mergelimitertest.cpp
blob: d6508ee82d27ee1fa17250b7257c8a41d36ee39f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storage/distributor/operations/idealstate/mergelimiter.h>
#include <vespa/vdstestlib/cppunit/macros.h>

namespace storage::distributor {

struct MergeLimiterTest : public CppUnit::TestFixture
{
    void testKeepsAllBelowLimit();
    void testLessThanMaxUntrusted();
    void testMoreThanMaxUntrusted();
    void testAllUntrustedLessThanMaxVariants();
    void testAllUntrustedMoreThanMaxVariants();
    void testSourceOnlyLast();
    void limited_set_cannot_be_just_source_only();
    void non_source_only_replica_chosen_from_in_sync_group();
    void non_source_only_replicas_preferred_when_replicas_not_in_sync();
    void at_least_one_non_source_only_replica_chosen_when_all_trusted();

    CPPUNIT_TEST_SUITE(MergeLimiterTest);
    CPPUNIT_TEST(testKeepsAllBelowLimit);
    CPPUNIT_TEST(testLessThanMaxUntrusted);
    CPPUNIT_TEST(testMoreThanMaxUntrusted);
    CPPUNIT_TEST(testAllUntrustedLessThanMaxVariants);
    CPPUNIT_TEST(testAllUntrustedMoreThanMaxVariants);
    CPPUNIT_TEST(testSourceOnlyLast);
    CPPUNIT_TEST(limited_set_cannot_be_just_source_only);
    CPPUNIT_TEST(non_source_only_replica_chosen_from_in_sync_group);
    CPPUNIT_TEST(non_source_only_replicas_preferred_when_replicas_not_in_sync);
    CPPUNIT_TEST(at_least_one_non_source_only_replica_chosen_when_all_trusted);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(MergeLimiterTest);

namespace {
    using BucketCopyPtr = std::unique_ptr<BucketCopy>;
    std::vector<BucketCopyPtr> _bucketDatabase;

    struct NodeFactory {
        std::vector<MergeMetaData> _nodes;

        NodeFactory& add(int index, int crc) {
            _bucketDatabase.push_back(BucketCopyPtr(
                    new BucketCopy(0, index, api::BucketInfo(crc, 5, 10))));
            _nodes.push_back(MergeMetaData(index, *_bucketDatabase.back()));
            return *this;
        }
        NodeFactory& addTrusted(int index, int crc) {
            add(index, crc);
            _bucketDatabase.back()->setTrusted(true);
            return *this;
        }
        NodeFactory& setSourceOnly() {
            _nodes.back()._sourceOnly = true;
            return *this;
        }

        operator const MergeLimiter::NodeArray&() const { return _nodes; }
    };

    #define ASSERT_LIMIT(maxNodes, nodes, result) \
    { \
        MergeLimiter limiter(maxNodes); \
        auto nodesCopy = nodes; \
        limiter.limitMergeToMaxNodes(nodesCopy); \
        std::ostringstream actual; \
        for (uint32_t i = 0; i < nodesCopy.size(); ++i) { \
            if (i != 0) actual << ","; \
            actual << nodesCopy[i]._nodeIndex; \
            if (nodesCopy[i]._sourceOnly) actual << 's'; \
        } \
        CPPUNIT_ASSERT_EQUAL(std::string(result), actual.str()); \
    }
}

// If there is <= max nodes, then none should be removed.
void
MergeLimiterTest::testKeepsAllBelowLimit()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(3, 0x4)
        .addTrusted(5, 0x4)
        .add(9, 0x6)
        .add(2, 0x6)
        .add(4, 0x5));

    ASSERT_LIMIT(8, nodes, "3,5,9,2,4");
}
// If less than max nodes is untrusted, merge all untrusted copies with a
// trusted one. (Optionally with extra trusted copies if there is space)
void
MergeLimiterTest::testLessThanMaxUntrusted()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(3, 0x4)
        .addTrusted(5, 0x4)
        .add(9, 0x6)
        .add(2, 0x6)
        .add(4, 0x5));
    ASSERT_LIMIT(4, nodes, "2,4,9,5");
}
// With more than max untrusted, just merge one trusted with as many untrusted
// that fits.
void
MergeLimiterTest::testMoreThanMaxUntrusted()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(3, 0x4)
        .addTrusted(5, 0x4)
        .add(9, 0x6)
        .add(2, 0x6)
        .add(13, 0x9)
        .add(1, 0x7)
        .add(4, 0x5));
    ASSERT_LIMIT(4, nodes, "2,13,1,5");
}
// With nothing trusted. If there is <= max different variants (checksums),
// merge one of each variant. After this merge, all these nodes can be set
// trusted. (Except for any source only ones)
void
MergeLimiterTest::testAllUntrustedLessThanMaxVariants()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .add(3, 0x4)
        .add(5, 0x4)
        .add(9, 0x6)
        .add(2, 0x6)
        .add(13, 0x3)
        .add(1, 0x3)
        .add(4, 0x3));
    ASSERT_LIMIT(4, nodes, "5,2,4,3");
}
// With nothing trusted and more than max variants, we just have to merge one
// of each variant until we end up with less than max variants.
void
MergeLimiterTest::testAllUntrustedMoreThanMaxVariants()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .add(3, 0x4)
        .add(5, 0x5)
        .add(9, 0x6)
        .add(2, 0x6)
        .add(13, 0x3)
        .add(1, 0x9)
        .add(4, 0x8));
    ASSERT_LIMIT(4, nodes, "3,5,2,13");
}

// With more than max untrusted, just merge one trusted with as many untrusted
// that fits.
void
MergeLimiterTest::testSourceOnlyLast()
{
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(3, 0x4)
        .addTrusted(5, 0x4).setSourceOnly()
        .add(9, 0x6)
        .add(2, 0x6).setSourceOnly()
        .add(13, 0x9)
        .add(1, 0x7)
        .add(4, 0x5));
    ASSERT_LIMIT(4, nodes, "9,3,5s,2s");
}

void MergeLimiterTest::limited_set_cannot_be_just_source_only() {
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(9, 0x6)
        .addTrusted(2, 0x6)
        .addTrusted(13, 0x6).setSourceOnly()
        .add(1, 0x7).setSourceOnly());
    ASSERT_LIMIT(2, nodes, "2,13s");
    ASSERT_LIMIT(3, nodes, "2,13s,1s");
}

void MergeLimiterTest::non_source_only_replica_chosen_from_in_sync_group() {
    // nodes 9, 2, 13 are all in sync. Merge limiter will currently by default
    // pop the _last_ node of an in-sync replica "group" when outputting a limited
    // set. Unless we special-case source-only replicas here, we'd end up with an
    // output set of "13s,1s", i.e. all source-only.
    MergeLimiter::NodeArray nodes(NodeFactory()
        .add(9, 0x6)
        .add(2, 0x6)
        .add(13, 0x6).setSourceOnly()
        .add(1, 0x7).setSourceOnly());
    ASSERT_LIMIT(2, nodes, "2,13s");
    ASSERT_LIMIT(3, nodes, "2,13s,1s");
}

void MergeLimiterTest::non_source_only_replicas_preferred_when_replicas_not_in_sync() {
    MergeLimiter::NodeArray nodes(NodeFactory()
        .add(9, 0x4)
        .add(2, 0x5)
        .add(13, 0x6).setSourceOnly()
        .add(1, 0x7).setSourceOnly());
    ASSERT_LIMIT(2, nodes, "9,2");
    ASSERT_LIMIT(3, nodes, "9,2,13s");
}

void MergeLimiterTest::at_least_one_non_source_only_replica_chosen_when_all_trusted() {
    MergeLimiter::NodeArray nodes(NodeFactory()
        .addTrusted(9, 0x6)
        .addTrusted(2, 0x6)
        .addTrusted(13, 0x6).setSourceOnly()
        .addTrusted(1, 0x6).setSourceOnly());
    ASSERT_LIMIT(2, nodes, "2,13s");
    ASSERT_LIMIT(3, nodes, "2,13s,1s");
}

} // storage::distributor