aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/tests/frtconnectionpool/frtconnectionpool.cpp
blob: 834b1797ed0ac4cdde9233653fc496abeded3b44 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/config/frt/frtconnectionpool.h>
#include <vespa/fnet/frt/error.h>
#include <vespa/fnet/transport.h>
#include <sstream>
#include <set>
#include <unistd.h>

using namespace config;

class Test : public vespalib::TestApp {
private:
    static ServerSpec::HostSpecList _sources;
    FNET_Transport    _transport;
    void verifyAllSourcesInRotation(FRTConnectionPool& sourcePool);
public:
    Test();
    ~Test() override;
    int Main() override;
    void testBasicRoundRobin();
    void testBasicHashBasedSelection();
    void testSetErrorRoundRobin();
    void testSetErrorAllRoundRobin();
    void testSetErrorHashBased();
    void testSetErrorAllHashBased();
    void testSuspensionTimeout();
    void testManySources();
};

Test::Test()
    : vespalib::TestApp(),
      _transport()
{
    _transport.Start();
}

Test::~Test() {
    _transport.ShutDown(true);
}

TEST_APPHOOK(Test);

ServerSpec::HostSpecList Test::_sources;
TimingValues timingValues;

int Test::Main() {
    TEST_INIT("frtconnectionpool_test");

    _sources.push_back("host0");
    _sources.push_back("host1");
    _sources.push_back("host2");

    testBasicRoundRobin();
    TEST_FLUSH();

    testBasicHashBasedSelection();
    TEST_FLUSH();

    testSetErrorRoundRobin();
    TEST_FLUSH();

    testSetErrorAllRoundRobin();
    TEST_FLUSH();

    testSetErrorHashBased();
    TEST_FLUSH();

    testSetErrorAllHashBased();
    TEST_FLUSH();

    testSuspensionTimeout();
    TEST_FLUSH();

    testManySources();
    TEST_FLUSH();

    TEST_DONE();
    return 0;
}

void Test::verifyAllSourcesInRotation(FRTConnectionPool& sourcePool) {
    std::set<std::string> completeSet(_sources.begin(), _sources.end());
    std::set<std::string> foundSet;
    for (int i = 0; i < (int)_sources.size(); i++) {
        foundSet.insert(sourcePool.getNextRoundRobin()->getAddress());
    }
    EXPECT_EQUAL(true, completeSet == foundSet);
}

/**
 * Tests that basic round robin selection through the list works.
 */
void Test::testBasicRoundRobin() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    for (int i = 0; i < 9; i++) {
        int j = i % _sources.size();
        std::stringstream s;
        s << "host" << j;
        EXPECT_EQUAL(s.str(), sourcePool.getNextRoundRobin()->getAddress());
    }
}

/**
 * Tests that hash-based selection through the list works.
 */
void Test::testBasicHashBasedSelection() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    sourcePool.setHostname("a.b.com");
    for (int i = 0; i < 9; i++) {
        EXPECT_EQUAL("host1", sourcePool.getNextHashBased()->getAddress());
    }
    sourcePool.setHostname("host98");
    for (int i = 0; i < 9; i++) {
        EXPECT_EQUAL("host0", sourcePool.getNextHashBased()->getAddress());
    }

    ServerSpec::HostSpecList hostnames;
    hostnames.push_back("sutter-01.example.yahoo.com");
    hostnames.push_back("stroustrup-02.example.yahoo.com");
    hostnames.push_back("alexandrescu-03.example.yahoo.com");
    const ServerSpec spec2(hostnames);
    FRTConnectionPool sourcePool2(_transport, spec2, timingValues);
    sourcePool2.setHostname("sutter-01.example.yahoo.com");
    EXPECT_EQUAL("stroustrup-02.example.yahoo.com", sourcePool2.getNextHashBased()->getAddress());
    sourcePool2.setHostname("stroustrup-02.example.yahoo.com");
    EXPECT_EQUAL("sutter-01.example.yahoo.com", sourcePool2.getNextHashBased()->getAddress());
    sourcePool2.setHostname("alexandrescu-03.example.yahoo.com");
    EXPECT_EQUAL("alexandrescu-03.example.yahoo.com", sourcePool2.getNextHashBased()->getAddress());
}

/**
 * Tests that a source is taken out of rotation when an error is reported,
 * and that it is taken back in when a success is reported.
 */
void Test::testSetErrorRoundRobin() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    FRTConnection* source = sourcePool.getNextRoundRobin();
    source->setError(FRTE_RPC_CONNECTION);
    for (int i = 0; i < 9; i++) {
        EXPECT_NOT_EQUAL(source->getAddress(), sourcePool.getCurrent()->getAddress());
    }
    source->setSuccess();
    verifyAllSourcesInRotation(sourcePool);
}

/**
 * Tests that all sources are in rotation when all sources have errors set.
 */
void Test::testSetErrorAllRoundRobin() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    for (int i = 0; i < (int)_sources.size(); i++) {
        FRTConnection* source = sourcePool.getNextRoundRobin();
        source->setError(FRTE_RPC_CONNECTION);
    }
    verifyAllSourcesInRotation(sourcePool);
}

/**
 * Tests that a source is not used when an error is reported,
 * and that the same source is used when a success is reported.
 */
void Test::testSetErrorHashBased() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    FRTConnection* source = sourcePool.getNextHashBased();
    source->setError(FRTE_RPC_CONNECTION);
    for (int i = 0; i < (int)_sources.size(); i++) {
        EXPECT_NOT_EQUAL(source->getAddress(), sourcePool.getNextHashBased()->getAddress());
    }
    source->setSuccess();
    EXPECT_EQUAL(source->getAddress(), sourcePool.getNextHashBased()->getAddress());
}

/**
 * Tests that the same source is used when all sources have errors set.
 */
void Test::testSetErrorAllHashBased() {
    const ServerSpec spec(_sources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);
    FRTConnection* firstSource = sourcePool.getNextHashBased();
    auto readySources = sourcePool.getReadySources();
    for (int i = 0; i < (int)readySources.size(); i++) {
        readySources[i]->setError(FRTE_RPC_CONNECTION);
    }
    EXPECT_EQUAL(sourcePool.getReadySources().size(), 0u);
    EXPECT_EQUAL(sourcePool.getSuspendedSources().size(), 3u);

    // should get the same source now, since all are suspended
    EXPECT_EQUAL(firstSource->getAddress(), sourcePool.getNextHashBased()->getAddress());

    // set all except firstSource to OK
    for (int i = 0; i < (int)readySources.size(); i++) {
        if (readySources[i]->getAddress() != firstSource->getAddress()) {
            readySources[i]->setSuccess();
        }
    }

    EXPECT_EQUAL(sourcePool.getReadySources().size(), 2u);
    EXPECT_EQUAL(sourcePool.getSuspendedSources().size(), 1u);

    // should not get the same source now, since original source is
    // suspended, while the rest are OK
    EXPECT_NOT_EQUAL(firstSource->getAddress(), sourcePool.getNextHashBased()->getAddress());
}

/**
 * Tests that the source is put back into rotation when the suspension times out.
 */
void Test::testSuspensionTimeout() {
    const ServerSpec spec(_sources);
    TimingValues short_transient_delay;
    short_transient_delay.transientDelay = 1s;
    FRTConnectionPool sourcePool(_transport, spec, short_transient_delay);
    FRTConnection* source = dynamic_cast<FRTConnection *>(sourcePool.getCurrent());
    source->setError(FRTE_RPC_CONNECTION);
    for (int i = 0; i < 9; i++) {
        EXPECT_NOT_EQUAL(source->getAddress(), sourcePool.getCurrent()->getAddress());
    }
    sleep(2);
    verifyAllSourcesInRotation(sourcePool);
}

/**
 * Tests that when there are two sources and several clients
 * the sources will be chosen with equal probability.
 */
void Test::testManySources() {
    std::vector<std::string> hostnames;
    for (int i = 0; i < 20; ++i) {
        hostnames.push_back("host-" + std::to_string(i) + ".example.yahoo.com");
    }

    std::map<std::string, int> timesUsed;
    ServerSpec::HostSpecList twoSources;

    twoSources.push_back("host0");
    twoSources.push_back("host1");

    const ServerSpec spec(twoSources);
    FRTConnectionPool sourcePool(_transport, spec, timingValues);

    for (int i = 0; i < (int)hostnames.size(); i++) {
        sourcePool.setHostname(hostnames[i]);
        std::string address = sourcePool.getNextHashBased()->getAddress();
        if (timesUsed.find(address) != timesUsed.end()) {
            int times = timesUsed[address];
            timesUsed[address] = times + 1;
        } else {
            timesUsed[address] = 1;
        }
    }
    EXPECT_EQUAL(timesUsed["host0"], (int)hostnames.size() / 2);
    EXPECT_EQUAL(timesUsed["host1"], (int)hostnames.size() / 2);
}