aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/tests/failover/failover.cpp
blob: 80d89f41c16e1b17a67f0969dba12d6607e480b6 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/config/frt/protocol.h>
#include <vespa/config/common/configcontext.h>
#include <vespa/config/subscription/configsubscriber.hpp>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/rpcrequest.h>

#include "config-my.h"
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/data/simple_buffer.h>

#include <vespa/log/log.h>
LOG_SETUP("failover");

using namespace config;
using vespalib::Barrier;
using namespace config::protocol::v2;
using namespace vespalib::slime;
using namespace vespalib;

namespace {

int get_port(const vespalib::string &spec) {
    const char *port = (spec.data() + spec.size());
    while ((port > spec.data()) && (port[-1] >= '0') && (port[-1] <= '9')) {
        --port;
    }
    return atoi(port);
}

const vespalib::string requestTypes = "s";
const vespalib::string responseTypes = "sx";

struct RPCServer : public FRT_Invokable {
    vespalib::Barrier barrier;
    int64_t gen;

    RPCServer() : barrier(2), gen(1) { }

    void init(FRT_Supervisor * s) {
        FRT_ReflectionBuilder rb(s);
        rb.DefineMethod("config.v3.getConfig", requestTypes.c_str(), responseTypes.c_str(),
                        FRT_METHOD(RPCServer::getConfig), this);
    }

    void getConfig(FRT_RPCRequest * req)
    {
        Slime slime;
        Cursor & root(slime.setObject());
        root.setLong(RESPONSE_VERSION, 3);
        root.setString(RESPONSE_DEF_NAME, Memory(MyConfig::CONFIG_DEF_NAME));
        root.setString(RESPONSE_DEF_NAMESPACE, Memory(MyConfig::CONFIG_DEF_NAMESPACE));
        root.setString(RESPONSE_DEF_MD5, Memory(MyConfig::CONFIG_DEF_MD5));
        Cursor &info = root.setObject("compressionInfo");
        info.setString("compressionType", "UNCOMPRESSED");
        info.setString("uncompressedSize", "0");
        root.setString(RESPONSE_CONFIGID, "myId");
        root.setString(RESPONSE_CLIENT_HOSTNAME, "myhost");
        root.setString(RESPONSE_CONFIG_XXHASH64, "xxhash64");
        root.setLong(RESPONSE_CONFIG_GENERATION, gen);
        root.setObject(RESPONSE_TRACE);
        Slime payload;
        payload.setObject().setString("myField", "myval");

        FRT_Values & ret = *req->GetReturn();
        SimpleBuffer buf;
        JsonFormat::encode(slime, buf, false);
        ret.AddString(buf.get().make_string().c_str());

        SimpleBuffer pbuf;
        JsonFormat::encode(payload, pbuf, false);
        vespalib::string d = pbuf.get().make_string();
        ret.AddData(d.c_str(), d.size());
        LOG(info, "Answering...");
    }
    void wait() {
        barrier.await();
    }
    void reload() { gen++; }
};


void verifyConfig(std::unique_ptr<MyConfig> config)
{
    ASSERT_TRUE(config);
    ASSERT_EQUAL("myval", config->myField);
}

struct ServerFixture {
    using UP = std::unique_ptr<ServerFixture>;
    std::unique_ptr<fnet::frt::StandaloneFRT> frt;
    RPCServer server;
    Barrier b;
    const vespalib::string listenSpec;
    ServerFixture(const vespalib::string & ls)
        : frt(),
          server(),
          b(2),
          listenSpec(ls)
    {
    }

    void wait()
    {
        b.await();
    }

    void start()
    {
        frt = std::make_unique<fnet::frt::StandaloneFRT>();
        server.init(&frt->supervisor());
        frt->supervisor().Listen(get_port(listenSpec));
        wait(); // Wait until test runner signals we can start
        wait(); // Signalling that we have shut down
        wait(); // Wait for signal saying that supervisor is deleted
    }

    void stop()
    {
        if (frt) {
            frt.reset();
            wait(); // Wait for supervisor to shut down
            wait(); // Signal that we are done and start can return.
        }
    }

    ~ServerFixture() { stop(); }
};

struct NetworkFixture {
    std::vector<ServerFixture::UP> serverList;
    ServerSpec spec;
    bool running;
    NetworkFixture(const std::vector<vespalib::string> & serverSpecs)
        : spec(serverSpecs), running(true)
    {
        for (size_t i = 0; i < serverSpecs.size(); i++) {
            serverList.push_back(std::make_unique<ServerFixture>(serverSpecs[i]));
        }
    }
    void start(size_t i) {
        serverList[i]->start();
    }
    void wait(size_t i) {
        serverList[i]->wait();
    }
    void waitAll() {
        for (size_t i = 0; i < serverList.size(); i++) {
            serverList[i]->wait();
        }
    }
    void run(size_t i) {
        while (running) {
            serverList[i]->start();
        }
    }
    void stopAll() {
        running = false;
        for (size_t i = 0; i < serverList.size(); i++) {
            serverList[i]->stop();
        }
    }
    void stop(size_t i) {
        serverList[i]->stop();
    }
    void reload() {
        for (size_t i = 0; i < serverList.size(); i++) {
            serverList[i]->server.reload();
        }
    }
};


TimingValues testTimingValues(
    500ms,  // successTimeout
    500ms,  // errorTimeout
    500ms,   // initialTimeout
    400ms,  // unsubscribeTimeout
    0ms,     // fixedDelay
    250ms,   // successDelay
    250ms,   // unconfiguredDelay
    500ms,   // configuredErrorDelay
    1,      // maxDelayMultiplier
    600ms,  // transientDelay
    1200ms); // fatalDelay

struct ConfigCheckFixture {
    std::shared_ptr<IConfigContext> ctx;
    NetworkFixture & nf;

    ConfigCheckFixture(NetworkFixture & f2)
        : ctx(std::make_shared<ConfigContext>(testTimingValues, f2.spec)),
          nf(f2)
    {
    }
    void checkSubscribe()
    {
        ConfigSubscriber s(ctx);
        ConfigHandle<MyConfig>::UP handle = s.subscribe<MyConfig>("myId");
        ASSERT_TRUE(s.nextConfig());
    }
    void verifySubscribeFailover(size_t index)
    {
        nf.stop(index);
        checkSubscribe();
        nf.wait(index);
    }

    void verifySubscribeFailover(size_t indexA, size_t indexB)
    {
        nf.stop(indexA);
        nf.stop(indexB);
        checkSubscribe();
        nf.wait(indexA);
        nf.wait(indexB);
    }
};

struct ConfigReloadFixture {
    std::shared_ptr<IConfigContext> ctx;
    NetworkFixture & nf;
    ConfigSubscriber s;
    ConfigHandle<MyConfig>::UP handle;

    ConfigReloadFixture(NetworkFixture & f2)
        : ctx(std::make_shared<ConfigContext>(testTimingValues, f2.spec)),
          nf(f2),
          s(ctx),
          handle(s.subscribe<MyConfig>("myId"))
    {
    }

    void verifyReload()
    {
        nf.reload();
        ASSERT_TRUE(s.nextGeneration());
        verifyConfig(handle->getConfig());
    }

    void verifyReloadFailover(size_t index)
    {
        nf.stop(index);
        verifyReload();
        nf.wait(index);
    }

    void verifyReloadFailover(size_t indexA, size_t indexB)
    {
        nf.stop(indexA);
        nf.stop(indexB);
        verifyReload();
        nf.wait(indexA);
        nf.wait(indexB);
    }
};

struct ThreeServersFixture {
    std::vector<vespalib::string> specs;
    ThreeServersFixture() : specs() {
        specs.push_back("tcp/localhost:18590");
        specs.push_back("tcp/localhost:18592");
        specs.push_back("tcp/localhost:18594");
    }
};

struct OneServerFixture {
    std::vector<vespalib::string> specs;
    OneServerFixture() : specs() {
        specs.push_back("tcp/localhost:18590");
    }
};

}

TEST_MT_FF("require that any node can be down when subscribing",
             4,
             ThreeServersFixture(),
             NetworkFixture(f1.specs))
{
    if (thread_id == 0) {
        ConfigCheckFixture ccf(f2);
        f2.waitAll();
        ccf.checkSubscribe();
        ccf.verifySubscribeFailover(0);
        ccf.verifySubscribeFailover(1);
        ccf.verifySubscribeFailover(2);
        f2.stopAll();
        TEST_BARRIER();
    } else {
        f2.run(thread_id - 1);
        TEST_BARRIER();
    }
}
/*
TEST_MT_FF("require that two out of three nodes can be down when subscribing",
             4,
             ThreeServersFixture(),
             NetworkFixture(f1.specs))
{
    if (thread_id == 0) {
        ConfigCheckFixture ccf(f2);
        f2.waitAll();
        ccf.checkSubscribe();
        ccf.verifySubscribeFailover(0, 1);
        ccf.verifySubscribeFailover(1, 2);
        ccf.verifySubscribeFailover(0, 2);
        f2.stopAll();
        TEST_BARRIER();
    } else {
        f2.run(thread_id - 1);
        TEST_BARRIER();
    }
}

TEST_MT_FF("require that any node can be down when waiting for next generation",
             4,
             ThreeServersFixture(),
             NetworkFixture(f1.specs))
{
    if (thread_id == 0) {
        f2.waitAll();
        ConfigReloadFixture crf(f2);
        crf.verifyReload();
        crf.verifyReloadFailover(0);
        crf.verifyReloadFailover(1);
        crf.verifyReloadFailover(2);
        f2.stopAll();
        TEST_BARRIER();
    } else { f2.run(thread_id - 1); TEST_BARRIER();
    }
}

TEST_MT_FF("require that two out of three nodes can be down when waiting for next generation",
             4,
             ThreeServersFixture(),
             NetworkFixture(f1.specs))
{
    if (thread_id == 0) {
        f2.waitAll();
        ConfigReloadFixture crf(f2);
        crf.verifyReload();
        crf.verifyReloadFailover(0, 1);
        crf.verifyReloadFailover(1, 2);
        crf.verifyReloadFailover(0, 2);
        f2.stopAll();
        TEST_BARRIER();
    } else {
        f2.run(thread_id - 1);
        TEST_BARRIER();
    }
}
*/

TEST_MAIN() { TEST_RUN_ALL(); }