summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/distributor_host_info_reporter_test.cpp
blob: 3e4e1d6da88d0121ec32d40f57152cbf4913e853 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/storage/distributor/distributor_host_info_reporter.h>
#include <vespa/storage/distributor/latency_statistics_provider.h>
#include <vespa/storage/distributor/min_replica_provider.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <tests/common/hostreporter/util.h>
#include <vespa/vespalib/stllike/asciistream.h>

namespace storage {
namespace distributor {

using End = vespalib::JsonStream::End;
using File = vespalib::File;
using Object = vespalib::JsonStream::Object;

class DistributorHostInfoReporterTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(DistributorHostInfoReporterTest);
    CPPUNIT_TEST(hostInfoWithPutLatenciesOnly);
    CPPUNIT_TEST(hostInfoAllInfo);
    CPPUNIT_TEST(generateExampleJson);
    CPPUNIT_TEST(noReportGeneratedIfDisabled);
    CPPUNIT_TEST_SUITE_END();

    void hostInfoWithPutLatenciesOnly();
    void hostInfoAllInfo();
    void verifyReportedNodeLatencies(
            const vespalib::Slime& root,
            uint16_t node,
            int64_t latencySum,
            int64_t count);
    void generateExampleJson();
    void noReportGeneratedIfDisabled();
};

CPPUNIT_TEST_SUITE_REGISTRATION(DistributorHostInfoReporterTest);

using ms = std::chrono::milliseconds;

namespace {

OperationStats
makeOpStats(std::chrono::milliseconds totalLatency, uint64_t numRequests)
{
    OperationStats stats;
    stats.totalLatency = totalLatency;
    stats.numRequests = numRequests;
    return stats;
}

// My kingdom for GoogleMock!
struct MockedLatencyStatisticsProvider : LatencyStatisticsProvider
{
    NodeStatsSnapshot returnedSnapshot;

    NodeStatsSnapshot doGetLatencyStatistics() const override {
        return returnedSnapshot;
    }
};

struct MockedMinReplicaProvider : MinReplicaProvider
{
    std::unordered_map<uint16_t, uint32_t> minReplica;

    std::unordered_map<uint16_t, uint32_t> getMinReplica() const override {
        return minReplica;
    }
};

const vespalib::slime::Inspector&
getNode(const vespalib::Slime& root, uint16_t nodeIndex)
{
    auto& storage_nodes = root.get()["distributor"]["storage-nodes"];
    const size_t n = storage_nodes.entries();
    for (size_t i = 0; i < n; ++i) {
        if (storage_nodes[i]["node-index"].asLong() == nodeIndex) {
            return storage_nodes[i];
        }
    }
    throw std::runtime_error("No node found with index "
                             + std::to_string(nodeIndex));
}

int
getMinReplica(const vespalib::Slime& root, uint16_t nodeIndex)
{
    return getNode(root, nodeIndex)["min-current-replication-factor"].asLong();
}

const vespalib::slime::Inspector&
getLatenciesForNode(const vespalib::Slime& root, uint16_t nodeIndex)
{
    return getNode(root, nodeIndex)["ops-latency"];
}

} // anon ns

void
DistributorHostInfoReporterTest::verifyReportedNodeLatencies(
        const vespalib::Slime& root,
        uint16_t node,
        int64_t latencySum,
        int64_t count)
{
    auto& latencies = getLatenciesForNode(root, node);
    CPPUNIT_ASSERT_EQUAL(latencySum,
                         latencies["put"]["latency-ms-sum"].asLong());
    CPPUNIT_ASSERT_EQUAL(count, latencies["put"]["count"].asLong());
}

void
DistributorHostInfoReporterTest::hostInfoWithPutLatenciesOnly()
{
    MockedLatencyStatisticsProvider latencyStatsProvider;
    MockedMinReplicaProvider minReplicaProvider;
    DistributorHostInfoReporter reporter(latencyStatsProvider,
                                         minReplicaProvider);

    NodeStatsSnapshot snapshot;
    snapshot.nodeToStats[0] = { makeOpStats(ms(10000), 3) };
    snapshot.nodeToStats[5] = { makeOpStats(ms(25000), 7) };

    latencyStatsProvider.returnedSnapshot = snapshot;

    vespalib::Slime root;
    util::reporterToSlime(reporter, root);
    verifyReportedNodeLatencies(root, 0, 10000, 3);
    verifyReportedNodeLatencies(root, 5, 25000, 7);
}

void
DistributorHostInfoReporterTest::hostInfoAllInfo()
{
    MockedLatencyStatisticsProvider latencyStatsProvider;
    MockedMinReplicaProvider minReplicaProvider;
    DistributorHostInfoReporter reporter(latencyStatsProvider,
                                         minReplicaProvider);

    NodeStatsSnapshot latencySnapshot;
    latencySnapshot.nodeToStats[0] = { makeOpStats(ms(10000), 3) };
    latencySnapshot.nodeToStats[5] = { makeOpStats(ms(25000), 7) };
    latencyStatsProvider.returnedSnapshot = latencySnapshot;

    std::unordered_map<uint16_t, uint32_t> minReplica;
    minReplica[0] = 2;
    minReplica[5] = 9;
    minReplicaProvider.minReplica = minReplica;

    vespalib::Slime root;
    util::reporterToSlime(reporter, root);
    verifyReportedNodeLatencies(root, 0, 10000, 3);
    verifyReportedNodeLatencies(root, 5, 25000, 7);

    CPPUNIT_ASSERT_EQUAL(2, getMinReplica(root, 0));
    CPPUNIT_ASSERT_EQUAL(9, getMinReplica(root, 5));
}

void
DistributorHostInfoReporterTest::generateExampleJson()
{
    MockedLatencyStatisticsProvider latencyStatsProvider;
    MockedMinReplicaProvider minReplicaProvider;
    DistributorHostInfoReporter reporter(latencyStatsProvider,
                                         minReplicaProvider);

    NodeStatsSnapshot snapshot;
    snapshot.nodeToStats[0] = { makeOpStats(ms(10000), 3) };
    snapshot.nodeToStats[5] = { makeOpStats(ms(25000), 7) };
    latencyStatsProvider.returnedSnapshot = snapshot;

    std::unordered_map<uint16_t, uint32_t> minReplica;
    minReplica[0] = 2;
    minReplica[5] = 9;
    minReplicaProvider.minReplica = minReplica;

    vespalib::asciistream json;
    vespalib::JsonStream stream(json, true);

    stream << Object();
    reporter.report(stream);
    stream << End();
    stream.finalize();

    std::string jsonString = json.str();

    std::string path = TEST_PATH("../../../protocols/getnodestate/distributor.json");
    std::string goldenString = File::readAll(path);

    vespalib::Memory goldenMemory(goldenString);
    vespalib::Slime goldenSlime;
    vespalib::slime::JsonFormat::decode(goldenMemory, goldenSlime);

    vespalib::Memory jsonMemory(jsonString);
    vespalib::Slime jsonSlime;
    vespalib::slime::JsonFormat::decode(jsonMemory, jsonSlime);

    CPPUNIT_ASSERT_EQUAL(goldenSlime, jsonSlime);
}

void
DistributorHostInfoReporterTest::noReportGeneratedIfDisabled()
{
    MockedLatencyStatisticsProvider latencyStatsProvider;
    MockedMinReplicaProvider minReplicaProvider;
    DistributorHostInfoReporter reporter(latencyStatsProvider,
                                         minReplicaProvider);
    reporter.enableReporting(false);

    NodeStatsSnapshot snapshot;
    snapshot.nodeToStats[0] = { makeOpStats(ms(10000), 3) };
    snapshot.nodeToStats[5] = { makeOpStats(ms(25000), 7) };

    latencyStatsProvider.returnedSnapshot = snapshot;

    vespalib::Slime root;
    util::reporterToSlime(reporter, root);
    CPPUNIT_ASSERT_EQUAL(size_t(0), root.get().children());
}

} // distributor
} // storage