aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/tests/distribution/idealnodecalculatorcachetest.cpp
blob: 8195d6dd070a30df5910448d867099b06d8ea938 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdslib/distribution/idealnodecalculatorcache.h>
#include <vespa/vdslib/distribution/idealnodecalculatorimpl.h>
#include <vespa/vdstestlib/cppunit/macros.h>

namespace storage {
namespace lib {

struct IdealNodeCalculatorCacheTest : public CppUnit::TestFixture {

    /** Test that you get a correct result forwarded through simple. */
    void testSimple();
    /** Test that similar buckets use different cache slots. */
    void testLocalityCached();
    /** Test that buckets using same cache slot invalidates each other. */
    void testBucketsSameCacheSlot();
    /** Test that cache is invalidated on changes. */
    void testCacheInvalidationOnChanges();
    /** Test that values for different upstates are kept for themselves. */
    void testDifferentUpStates();
    /** Test that values for different node types are kept for themselves. */
    void testDifferentNodeTypes();
    /**
     * Do a performance test, verifying that cache actually significantly
     * increase performance.
     */
    void testPerformance();

    CPPUNIT_TEST_SUITE(IdealNodeCalculatorCacheTest);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testLocalityCached);
    CPPUNIT_TEST(testBucketsSameCacheSlot);
    CPPUNIT_TEST(testCacheInvalidationOnChanges);
    CPPUNIT_TEST(testDifferentUpStates);
    CPPUNIT_TEST(testDifferentNodeTypes);
    CPPUNIT_TEST(testPerformance);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(IdealNodeCalculatorCacheTest);

void
IdealNodeCalculatorCacheTest::testSimple()
{
    ClusterState state("storage:10");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 4);

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

    std::string expected("[storage.8, storage.9, storage.6]");
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());
}

void
IdealNodeCalculatorCacheTest::testLocalityCached()
{
    ClusterState state("bits:6 storage:10");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 1024);

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

    std::vector<document::BucketId> local;
    local.push_back(document::BucketId(15, 134));
    local.push_back(document::BucketId(16, 134));
    local.push_back(document::BucketId(17, 134));
    local.push_back(document::BucketId(17, 134 | (1 << 16)));

    for (uint32_t i=0; i<local.size(); ++i) {
        calc.getIdealStorageNodes(local[i]);
    }

    CPPUNIT_ASSERT_EQUAL(4u, cache.getMissCount());
    CPPUNIT_ASSERT_EQUAL(0u, cache.getHitCount());

    for (uint32_t i=0; i<local.size(); ++i) {
        calc.getIdealStorageNodes(local[i]);
    }

    CPPUNIT_ASSERT_EQUAL(4u, cache.getMissCount());
    CPPUNIT_ASSERT_EQUAL(4u, cache.getHitCount());
}

void
IdealNodeCalculatorCacheTest::testBucketsSameCacheSlot()
{
    ClusterState state("bits:6 storage:10");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 1); // Only one slot available

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

        // See that you don't get same result as last one
    std::string expected("[storage.8, storage.9, storage.6]");
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());
    expected = "[storage.8, storage.6, storage.1]";
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 6)).toString());
}

void
IdealNodeCalculatorCacheTest::testCacheInvalidationOnChanges()
{
    ClusterState state("bits:6 storage:10");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 1); // Only one slot available

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

        // See that you don't get same result as last one
    std::string expected("[storage.8, storage.9, storage.6]");
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());

    CPPUNIT_ASSERT_EQUAL(1u, cache.getMissCount());
    CPPUNIT_ASSERT_EQUAL(0u, cache.getHitCount());

    configurable.setClusterState(state);

    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());

    CPPUNIT_ASSERT_EQUAL(2u, cache.getMissCount());
    CPPUNIT_ASSERT_EQUAL(0u, cache.getHitCount());

    configurable.setDistribution(distr);

    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());

    CPPUNIT_ASSERT_EQUAL(3u, cache.getMissCount());
    CPPUNIT_ASSERT_EQUAL(0u, cache.getHitCount());
}

void
IdealNodeCalculatorCacheTest::testDifferentUpStates()
{
    ClusterState state("bits:6 storage:10 .6.s:m .8.s:r");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 4);

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

    std::string expected("[storage.9, storage.4, storage.1]");
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5),
                                      IdealNodeCalculator::UpInit).toString());

    expected = "[storage.9, storage.6, storage.4]";
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(
                    document::BucketId(16, 5),
                    IdealNodeCalculator::UpInitMaintenance).toString());
}

void
IdealNodeCalculatorCacheTest::testDifferentNodeTypes()
{
    ClusterState state("bits:6 distributor:10 storage:10 .6.s:m .8.s:r");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 10));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    IdealNodeCalculatorCache cache(impl, 4);

    IdealNodeCalculatorConfigurable& configurable(cache);
    IdealNodeCalculator& calc(cache);
    configurable.setDistribution(distr);
    configurable.setClusterState(state);

    std::string expected("[storage.9, storage.4, storage.1]");
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealStorageNodes(document::BucketId(16, 5)).toString());

    expected = "[distributor.8]";
    CPPUNIT_ASSERT_EQUAL(
            expected,
            calc.getIdealDistributorNodes(
                    document::BucketId(16, 5)).toString());
}

namespace {

    uint64_t getCurrentTimeInMicros() {
        struct timeval mytime;
        gettimeofday(&mytime, 0);
        return mytime.tv_sec * 1000000llu + mytime.tv_usec;
    }

    void addBucketTree(std::vector<document::BucketId>& v,
                       uint64_t location,
                       uint32_t currentUsedBits,
                       uint32_t maxUsedBits)
    {
        document::BucketId id(currentUsedBits, location);
        v.push_back(id);
        if (currentUsedBits < maxUsedBits) {
            addBucketTree(v, location,
                          currentUsedBits + 1, maxUsedBits);
            addBucketTree(v, location | (uint64_t(1) << currentUsedBits),
                          currentUsedBits + 1, maxUsedBits);
        }
    }

    uint64_t runPerformanceTest(IdealNodeCalculator& calc) {
        std::vector<document::BucketId> buckets;

            //  Addvarious location split levels for a user
        addBucketTree(buckets, 123, 20, 22);
            // Add various gid bit split levels for a user
        addBucketTree(buckets, 123, 40, 42);
            
        {
            std::set<document::BucketId> uniqueBuckets;
            for (uint32_t i=0; i<buckets.size(); ++i) {
                uniqueBuckets.insert(buckets[i]);
                calc.getIdealStorageNodes(buckets[i]);
            }
            CPPUNIT_ASSERT_EQUAL(buckets.size(), uniqueBuckets.size());
            CPPUNIT_ASSERT_EQUAL(size_t(14), buckets.size());
        }
        IdealNodeCalculatorCache* cache(dynamic_cast<IdealNodeCalculatorCache*>(
                &calc));
        if (cache != 0) cache->clearCounts();
        uint32_t value;
        uint64_t start = getCurrentTimeInMicros();
        for (uint32_t j=0; j<1024; ++j) {
            for (uint32_t i=0; i<buckets.size(); ++i) {
                IdealNodeList result(calc.getIdealStorageNodes(buckets[i]));
                value += (result[0].getIndex() + result[1].getIndex())
                         / result[2].getIndex();
            }
        }
        uint64_t stop = getCurrentTimeInMicros();
        return (stop - start);
    }

    struct MapIdealNodeCalculator : public IdealNodeCalculator {
        mutable std::map<document::BucketId, IdealNodeList> values;
        const IdealNodeCalculator& calc;

        MapIdealNodeCalculator(const IdealNodeCalculator& c) : calc(c) {}

        virtual IdealNodeList getIdealNodes(const NodeType& nodeType,
                                            const document::BucketId& bucketId,
                                            UpStates upStates) const
        {
            std::map<document::BucketId, IdealNodeList>::const_iterator it(
                    values.find(bucketId));
            if (it != values.end()) return it->second;
            IdealNodeList result(
                    calc.getIdealNodes(nodeType, bucketId, upStates));
            values[bucketId] = result;
            return result;
        }
    };
}

void
IdealNodeCalculatorCacheTest::testPerformance()
{
    ClusterState state("bits:18 distributor:100 storage:100 .6.s:m .8.s:r");
    Distribution distr(Distribution::getDefaultDistributionConfig(3, 100));
    IdealNodeCalculatorImpl::SP impl(new IdealNodeCalculatorImpl);
    impl->setDistribution(distr);
    impl->setClusterState(state);

    uint64_t rawPerformance = runPerformanceTest(*impl);

    IdealNodeCalculatorCache cache(impl, 14);

    uint64_t cachePerformance = runPerformanceTest(cache);
    double hitrate = (100.0 * cache.getHitCount()
                      / (cache.getHitCount() + cache.getMissCount()));
    CPPUNIT_ASSERT(hitrate > 99.99);

    MapIdealNodeCalculator mapCalc(*impl);

    uint64_t mapPerformance = runPerformanceTest(mapCalc);

    IdealNodeCalculatorCache cache2(impl, 13);
    uint64_t cacheMissPerformance = runPerformanceTest(cache2);
    double hitrate2 = (100.0 * cache2.getHitCount()
                       / (cache2.getHitCount() + cache2.getMissCount()));
    CPPUNIT_ASSERT(hitrate2 < 0.01);

    std::cerr << "\n"
              << "  Cache is "
              << (static_cast<double>(rawPerformance) / cachePerformance)
              << " x faster than skipping cache with 100% hitrate\n"
              << "  Cache is "
              << (static_cast<double>(mapPerformance) / cachePerformance)
              << " x faster than std::map cache with all data\n"
              << "  Cache is "
              << (static_cast<double>(rawPerformance) / cacheMissPerformance)
              << " x faster than skipping cache with 0% hitrate\n";
}

} // lib
} // storage