summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/getoperationtest.cpp
blob: d9013ca2c22815076ad5a8e85c62fd200d8553fc (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>

#include <vespa/config/helper/configgetter.h>
#include <vespa/document/config/config-documenttypes.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/storage/distributor/externaloperationhandler.h>
#include <vespa/storage/distributor/distributormetricsset.h>
#include <vespa/storageapi/message/persistence.h>
#include <tests/distributor/distributortestutil.h>
#include <tests/common/dummystoragelink.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <vespa/storage/distributor/operations/external/getoperation.h>

using std::shared_ptr;
using config::ConfigGetter;
using document::DocumenttypesConfig;
using config::FileSpec;

namespace storage {
namespace distributor {

class GetOperationTest : public CppUnit::TestFixture, public DistributorTestUtil {
    CPPUNIT_TEST_SUITE(GetOperationTest);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testNotFound);
    CPPUNIT_TEST(testResendOnStorageFailure);
    CPPUNIT_TEST(testResendOnStorageFailureAllFail);
    CPPUNIT_TEST(testSendToIdealCopyIfBucketInSync);
    CPPUNIT_TEST(testReturnNotFoundWhenBucketNotInDb);
    CPPUNIT_TEST(testAskAllNodesIfBucketIsInconsistent);
    CPPUNIT_TEST(testSendToAllInvalidNodesWhenInconsistent);
    CPPUNIT_TEST(testAskTrustedNodeIfBucketIsInconsistent);
    CPPUNIT_TEST(testInconsistentSplit); // Test that we ask all nodes if a bucket is inconsistent.
    CPPUNIT_TEST(testSendToAllInvalidCopies);
    CPPUNIT_TEST(testMultiInconsistentBucket);
    CPPUNIT_TEST(testMultiInconsistentBucketFail);
    CPPUNIT_TEST(testMultiInconsistentBucketNotFound);
    CPPUNIT_TEST(testMultiInconsistentBucketNotFoundDeleted);
    CPPUNIT_TEST(testMultipleCopiesWithFailureOnLocalNode);
    CPPUNIT_TEST(canGetDocumentsWhenAllReplicaNodesRetired);
    CPPUNIT_TEST_SUITE_END();

    document::DocumentTypeRepo::SP _repo;

public:
    document::DocumentId docId;
    document::BucketId bucketId;
    std::unique_ptr<Operation> op;

    void setUp() {
        _repo.reset(
                new document::DocumentTypeRepo(*ConfigGetter<DocumenttypesConfig>::
                        getConfig("config-doctypes",
                                  FileSpec(TEST_PATH("config-doctypes.cfg")))));
        createLinks();

        docId = document::DocumentId(document::DocIdString("test", "uri"));
        bucketId = getExternalOperationHandler().getBucketId(docId);
    };

    void tearDown() {
        close();
        op.reset();
    }

    void sendGet() {
        std::shared_ptr<api::GetCommand> msg(
                new api::GetCommand(document::BucketId(0), docId, "[all]"));

        op.reset(new GetOperation(getExternalOperationHandler(),
                                  msg,
                                  getDistributor().getMetrics().
                                  gets[msg->getLoadType()]));
        op->start(_sender, framework::MilliSecTime(0));
    }

    void sendReply(uint32_t idx,
               api::ReturnCode::Result result,
               std::string authorVal, uint32_t timestamp)
    {
        if (idx == (uint32_t)-1) {
            idx = _sender.commands.size() - 1;
        }

        std::shared_ptr<api::StorageCommand> msg2 = _sender.commands[idx];
        CPPUNIT_ASSERT_EQUAL(api::MessageType::GET, msg2->getType());

        api::GetCommand* tmp = static_cast<api::GetCommand*>(msg2.get());
        document::Document::SP doc;

        if (authorVal.length()) {
            const document::DocumentType* type(_repo->getDocumentType("text/html"));
            doc = document::Document::SP(
                    new document::Document(*type, docId));

            doc->setValue(doc->getField("author"),
                          document::StringFieldValue(authorVal));
        }

        api::GetReply* reply = new api::GetReply(*tmp, doc, timestamp);
        reply->setResult(result);

        op->receive(_sender, std::shared_ptr<api::StorageReply>(reply));
    }

    void replyWithFailure() {
        sendReply(-1, api::ReturnCode::IO_FAILURE, "", 0);
    }

    void replyWithNotFound() {
        sendReply(-1, api::ReturnCode::OK, "", 0);
    }

    void replyWithDocument() {
        sendReply(-1, api::ReturnCode::OK, "foo", 100);
    }

    std::string getLastReplyAuthor() {
        api::StorageMessage& msg = *_sender.replies[_sender.replies.size() - 1];

        if (msg.getType() == api::MessageType::GET_REPLY) {
            document::Document::SP doc(
                    dynamic_cast<api::GetReply&>(msg).getDocument());

            return doc->getValue(doc->getField("author"))->toString();
        } else {
            std::ostringstream ost;
            ost << "Last reply was not a GET reply, but " << msg;
            return ost.str();
        }
    }

    void setClusterState(const std::string& clusterState) {
        _distributor->enableClusterState(lib::ClusterState(clusterState));
    }

    void testSimple();
    void testReturnNotFoundWhenBucketNotInDb();
    void testNotFound();
    void testResendOnStorageFailure();
    void testResendOnStorageFailureAllFail();
    void testSendToIdealCopyIfBucketInSync();
    void testAskAllNodesIfBucketIsInconsistent();
    void testSendToAllInvalidNodesWhenInconsistent();
    void testAskTrustedNodeIfBucketIsInconsistent();
    void testInconsistentSplit();
    void testMultiInconsistentBucket();
    void testMultiInconsistentBucketFail();
    void testMultiInconsistentBucketNotFound();
    void testMultiInconsistentBucketNotFoundDeleted();
    void testSendToAllInvalidCopies();
    void testMultipleCopiesWithFailureOnLocalNode();
    void canGetDocumentsWhenAllReplicaNodesRetired();
};

CPPUNIT_TEST_SUITE_REGISTRATION(GetOperationTest);

void
GetOperationTest::testSimple()
{
    setClusterState("distributor:1 storage:2");

    addNodesToBucketDB(bucketId, "0=4,1=4");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0"),
            _sender.getCommands(true));

    replyWithDocument();

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 100) ReturnCode(NONE)"),
            _sender.getLastReply());
}

void
GetOperationTest::testAskTrustedNodeIfBucketIsInconsistent()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100/3/10,1=200/4/12/t");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(std::string("Get => 1"),
                         _sender.getCommands(true));

    replyWithDocument();

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 100) ReturnCode(NONE)"),
            _sender.getLastReply());
}

void
GetOperationTest::testAskAllNodesIfBucketIsInconsistent()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100/3/10,1=200/4/12");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "oldauthor", 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newauthor"), getLastReplyAuthor());
}


void
GetOperationTest::testSendToAllInvalidCopies()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "2=0/0/1,3=0/0/1");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 2,Get => 3"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "oldauthor", 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newauthor"), getLastReplyAuthor());
}

void
GetOperationTest::testSendToAllInvalidNodesWhenInconsistent()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100,1=200,2=0/0/1,3=0/0/1");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 2,Get => 3,Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "oldauthor", 1);
    sendReply(2, api::ReturnCode::OK, "oldauthor", 1);
    sendReply(3, api::ReturnCode::OK, "oldauthor", 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newauthor"), getLastReplyAuthor());
}

void
GetOperationTest::testInconsistentSplit()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(document::BucketId(16, 0x2a52), "0=100");
    addNodesToBucketDB(document::BucketId(17, 0x2a52), "1=200");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "oldauthor", 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newauthor"), getLastReplyAuthor());
}


void
GetOperationTest::testMultiInconsistentBucketNotFound()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100,2=100,1=200,3=200");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "", 0);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());
}

void
GetOperationTest::testMultiInconsistentBucketNotFoundDeleted()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100,2=100,1=200,3=200");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    // This signifies that the latest change was that the document was deleted
    // at timestamp 3.
    sendReply(1, api::ReturnCode::OK, "", 3);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 3) ReturnCode(NONE)"),
            _sender.getLastReply());
}

void
GetOperationTest::testMultiInconsistentBucket()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100,2=100,1=200,3=200");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 2);
    sendReply(1, api::ReturnCode::OK, "oldauthor", 1);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 2) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newauthor"), getLastReplyAuthor());
}

void
GetOperationTest::testMultiInconsistentBucketFail()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "0=100,2=100,1=200,3=200");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 1"),
            _sender.getCommands(true));

    sendReply(0, api::ReturnCode::OK, "newauthor", 1);
    sendReply(1, api::ReturnCode::DISK_FAILURE, "", 0);

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 3"),
            _sender.getLastCommand());

    replyWithDocument();

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 100) ReturnCode(NONE)"),
            _sender.getLastReply());
}


void
GetOperationTest::testReturnNotFoundWhenBucketNotInDb()
{
    setClusterState("distributor:1 storage:1");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 0) ReturnCode(NONE)"),
            _sender.getLastReply());
}

void
GetOperationTest::testNotFound()
{
    setClusterState("distributor:1 storage:1");

    addNodesToBucketDB(bucketId, "0=100");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 0"),
            _sender.getLastCommand());

    replyWithNotFound();

   CPPUNIT_ASSERT_EQUAL(
           std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                       "timestamp 0) ReturnCode(NONE)"),
           _sender.getLastReply());

   CPPUNIT_ASSERT_EQUAL(1, (int)(getDistributor().
                                 getMetrics().gets[documentapi::LoadType::DEFAULT].
                                 failures.notfound.getValue()));
}

void
GetOperationTest::testResendOnStorageFailure()
{
    setClusterState("distributor:1 storage:3");

    // Add two nodes that are not trusted. GET should retry each one of them
    // if one fails.
    addNodesToBucketDB(bucketId, "1=100,2=100");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 1"),
            _sender.getLastCommand());

    replyWithFailure();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 2"),
            _sender.getLastCommand());

    replyWithDocument();

   CPPUNIT_ASSERT_EQUAL(
           std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                       "timestamp 100) ReturnCode(NONE)"),
           _sender.getLastReply());
}

void
GetOperationTest::testResendOnStorageFailureAllFail()
{
    setClusterState("distributor:1 storage:3");

    // Add two nodes that are not trusted. GET should retry each one of them
    // if one fails.
    addNodesToBucketDB(bucketId, "1=100,2=100");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 1"),
            _sender.getLastCommand());

    replyWithFailure();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 2"),
            _sender.getLastCommand());

    replyWithFailure();

   CPPUNIT_ASSERT_EQUAL(
           std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                       "timestamp 0) ReturnCode(IO_FAILURE)"),
           _sender.getLastReply());
}

void
GetOperationTest::testSendToIdealCopyIfBucketInSync()
{
    setClusterState("distributor:1 storage:4");

    addNodesToBucketDB(bucketId, "1=100,2=100,3=100");

    sendGet();

    // Should always send to node 1 (follow bucket db order)
    CPPUNIT_ASSERT_EQUAL(
            std::string("Get(BucketId(0x4000000000002a52), doc:test:uri) => 1"),
            _sender.getLastCommand());

    replyWithDocument();

   CPPUNIT_ASSERT_EQUAL(
           std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                       "timestamp 100) ReturnCode(NONE)"),
           _sender.getLastReply());
}

void
GetOperationTest::testMultipleCopiesWithFailureOnLocalNode()
{
    setClusterState("distributor:1 storage:4");

    // Node 0 is local copy to distributor 0 and will be preferred when
    // sending initially.
    addNodesToBucketDB(document::BucketId(16, 0x2a52), "2=100,0=100");

    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0"),
            _sender.getCommands(true));

    // Fail local node; no reply must be sent yet since we've got more nodes
    // to try.
    sendReply(0, api::ReturnCode::TIMEOUT, "", 0);

    // Retry with remaining copy on node 2.
    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0,Get => 2"),
            _sender.getCommands(true));

    sendReply(1, api::ReturnCode::OK, "newestauthor", 3);

    CPPUNIT_ASSERT_EQUAL(
            std::string("GetReply(BucketId(0x0000000000000000), doc:test:uri, "
                        "timestamp 3) ReturnCode(NONE)"),
            _sender.getLastReply());

    CPPUNIT_ASSERT_EQUAL(std::string("newestauthor"), getLastReplyAuthor());
}

void
GetOperationTest::canGetDocumentsWhenAllReplicaNodesRetired()
{
    setClusterState("distributor:1 storage:2 .0.s:r .1.s:r");
    addNodesToBucketDB(bucketId, "0=4,1=4");
    sendGet();

    CPPUNIT_ASSERT_EQUAL(
            std::string("Get => 0"),
            _sender.getCommands(true));
}

} // distributor
} // storage