aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/updateoperationtest.cpp
blob: 9a2b529eb772895307d0837fe3bfaa0f78570bf4 (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
// 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 <cppunit/extensions/HelperMacros.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <tests/common/dummystoragelink.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/storageapi/message/state.h>
#include <vespa/storageapi/message/bucket.h>
#include <tests/distributor/distributortestutil.h>
#include <vespa/config/helper/configgetter.h>
#include <vespa/document/config/config-documenttypes.h>
#include <tests/distributor/messagesenderstub.h>
#include <vespa/storage/distributor/operations/external/updateoperation.h>
#include <vespa/vespalib/testkit/testapp.h>

using std::shared_ptr;
using namespace document;
using namespace storage;
using namespace storage::distributor;
using namespace storage::api;
using namespace std;
using namespace storage::lib;
using config::ConfigGetter;
using config::FileSpec;
using vespalib::string;

class UpdateOperation_Test : public CppUnit::TestFixture,
                             public DistributorTestUtil
{
    CPPUNIT_TEST_SUITE(UpdateOperation_Test);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testNotFound);
    CPPUNIT_TEST(testMultiNode);
    CPPUNIT_TEST(testMultiNodeInconsistentTimestamp);
    CPPUNIT_TEST_SUITE_END();

    DocumentTypeRepo::SP _repo;
    const DocumentType *_html_type;

protected:
    void testSimple();
    void testNotFound();
    void testMultiNode();
    void testMultiNodeInconsistentTimestamp();

public:
    void setUp() {
        _repo.reset(
                new DocumentTypeRepo(*ConfigGetter<DocumenttypesConfig>::
                                     getConfig("config-doctypes",
                                               FileSpec(vespalib::TestApp::GetSourceDirectory() + "config-doctypes.cfg"))));
        _html_type = _repo->getDocumentType("text/html");
        createLinks();
    }

    void tearDown() {
        close();
    }

    void replyToMessage(
            UpdateOperation& callback,
            MessageSenderStub& sender,
            uint32_t index,
            uint64_t oldTimestamp,
            api::BucketInfo info = api::BucketInfo(2,4,6));

    std::shared_ptr<UpdateOperation>
    sendUpdate(const std::string& bucketState);

    document::BucketId _bId;
};

CPPUNIT_TEST_SUITE_REGISTRATION(UpdateOperation_Test);

std::shared_ptr<UpdateOperation>
UpdateOperation_Test::sendUpdate(const std::string& bucketState)
{
    document::DocumentUpdate::SP update(
            new document::DocumentUpdate(
                    *_html_type,
                    document::DocumentId(document::DocIdString("test", "test"))));

    _bId = getExternalOperationHandler().getBucketId(update->getId());

    addNodesToBucketDB(_bId, bucketState);

    std::shared_ptr<api::UpdateCommand> msg(
            new api::UpdateCommand(document::BucketId(0),
                                   update,
                                   100));

    ExternalOperationHandler& handler = getExternalOperationHandler();
    return std::shared_ptr<UpdateOperation>(
            new UpdateOperation(handler,
                                msg,
                                getDistributor().getMetrics().updates[msg->getLoadType()]));
}


void
UpdateOperation_Test::replyToMessage(
        UpdateOperation& callback,
        MessageSenderStub& sender,
        uint32_t index,
        uint64_t oldTimestamp,
        api::BucketInfo info)
{
    std::shared_ptr<api::StorageMessage> msg2  = sender.commands[index];
    UpdateCommand* updatec = dynamic_cast<UpdateCommand*>(msg2.get());
    std::unique_ptr<api::StorageReply> reply(updatec->makeReply());
    UpdateReply* updateR = static_cast<api::UpdateReply*>(reply.get());
    updateR->setOldTimestamp(oldTimestamp);
    updateR->setBucketInfo(info);

    callback.onReceive(sender,
                       std::shared_ptr<StorageReply>(reply.release()));
}

void
UpdateOperation_Test::testSimple()
{
    setupDistributor(1, 1, "storage:1 distributor:1");

    std::shared_ptr<UpdateOperation> cb(sendUpdate("0=1/2/3"));
    MessageSenderStub sender;
    cb->start(sender, framework::MilliSecTime(0));

    CPPUNIT_ASSERT_EQUAL(
            std::string("Update => 0"),
            sender.getCommands(true));

    replyToMessage(*cb, sender, 0, 90);

    CPPUNIT_ASSERT_EQUAL(
            std::string("UpdateReply(doc:test:test, BucketId(0x0000000000000000), "
                        "timestamp 100, timestamp of updated doc: 90) ReturnCode(NONE)"),
            sender.getLastReply(true));
}

void
UpdateOperation_Test::testNotFound()
{
    setupDistributor(1, 1, "storage:1 distributor:1");

    std::shared_ptr<UpdateOperation> cb(sendUpdate("0=1/2/3"));
    MessageSenderStub sender;
    cb->start(sender, framework::MilliSecTime(0));

    CPPUNIT_ASSERT_EQUAL(
            std::string("Update => 0"),
            sender.getCommands(true));

    replyToMessage(*cb, sender, 0, 0);

    CPPUNIT_ASSERT_EQUAL(
            std::string("UpdateReply(doc:test:test, BucketId(0x0000000000000000), "
                        "timestamp 100, timestamp of updated doc: 0) ReturnCode(NONE)"),
            sender.getLastReply(true));
}

void
UpdateOperation_Test::testMultiNode()
{
    setupDistributor(2, 2, "distributor:1 storage:2");
    std::shared_ptr<UpdateOperation> cb(sendUpdate("0=1/2/3,1=1/2/3"));
    MessageSenderStub sender;
    cb->start(sender, framework::MilliSecTime(0));

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

    replyToMessage(*cb, sender, 0, 120);
    replyToMessage(*cb, sender, 1, 120);

    CPPUNIT_ASSERT_EQUAL(
            std::string("UpdateReply(doc:test:test, BucketId(0x0000000000000000), "
                        "timestamp 100, timestamp of updated doc: 120) ReturnCode(NONE)"),
            sender.getLastReply(true));

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    _bId.toString() + " : "
                    "node(idx=1,crc=0x2,docs=4/4,bytes=6/6,trusted=true,active=false), "
                    "node(idx=0,crc=0x2,docs=4/4,bytes=6/6,trusted=true,active=false)"),
            dumpBucket(_bId));
}

void
UpdateOperation_Test::testMultiNodeInconsistentTimestamp()
{
    setupDistributor(2, 2, "distributor:1 storage:2");
    std::shared_ptr<UpdateOperation> cb(sendUpdate("0=1/2/3,1=1/2/3"));
    MessageSenderStub sender;
    cb->start(sender, framework::MilliSecTime(0));

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

    replyToMessage(*cb, sender, 0, 119);
    replyToMessage(*cb, sender, 1, 120);

    CPPUNIT_ASSERT_EQUAL(
            std::string("UpdateReply(doc:test:test, BucketId(0x0000000000000000), "
                        "timestamp 100, timestamp of updated doc: 120 Was inconsistent "
                        "(best node 1)) ReturnCode(NONE)"),
            sender.getLastReply(true));
}