aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/documentdb/storeonlyfeedview/storeonlyfeedview_test.cpp
blob: 4dd6c454540d4faef8fcb610ae7b3cc8941e4989 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for storeonlyfeedview.

#include <vespa/document/base/documentid.h>
#include <vespa/document/base/globalid.h>
#include <vespa/document/bucket/bucketid.h>
#include <vespa/document/datatype/datatype.h>
#include <vespa/searchcommon/common/schema.h>
#include <vespa/searchcore/proton/metrics/feed_metrics.h>
#include <vespa/searchcore/proton/server/executorthreadingservice.h>
#include <vespa/searchcore/proton/server/storeonlyfeedview.h>
#include <vespa/searchcore/proton/documentmetastore/lidreusedelayer.h>
#include <vespa/searchcore/proton/test/mock_summary_adapter.h>
#include <vespa/searchcore/proton/test/thread_utils.h>
#include <vespa/searchcore/proton/common/commit_time_tracker.h>
#include <vespa/searchlib/index/docbuilder.h>
#include <vespa/searchlib/common/serialnum.h>
#include <vespa/vespalib/testkit/testapp.h>

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

using document::BucketId;
using document::DataType;
using document::Document;
using document::DocumentId;
using document::DocumentTypeRepo;
using document::DocumentUpdate;
using document::GlobalId;
using search::DocumentIdT;
using search::index::DocBuilder;
using search::index::Schema;
using search::SerialNum;
using storage::spi::Timestamp;
using vespalib::make_string;
using namespace proton;

namespace {

class MySummaryAdapter : public test::MockSummaryAdapter {
    int &_rm_count;
    int &_put_count;
    int &_heartbeat_count;

public:
    MySummaryAdapter(int &remove_count, int &put_count, int &heartbeat_count)
        : _rm_count(remove_count),
          _put_count(put_count),
          _heartbeat_count(heartbeat_count) {
    }
    virtual void put(SerialNum, const Document &, DocumentIdT) override { ++ _put_count; }
    virtual void remove(SerialNum, DocumentIdT) override { ++_rm_count; }
    virtual void heartBeat(SerialNum) override { ++_heartbeat_count; }
};

DocumentTypeRepo::SP myGetDocumentTypeRepo() {
    Schema schema;
    DocBuilder builder(schema);
    DocumentTypeRepo::SP repo = builder.getDocumentTypeRepo();
    ASSERT_TRUE(repo.get());
    return repo;
}

struct MyMinimalFeedView : StoreOnlyFeedView {
    typedef std::unique_ptr<MyMinimalFeedView> UP;

    int removeAttributes_count;
    int removeIndexedFields_count;
    int heartBeatAttributes_count;
    int heartBeatIndexedFields_count;

    MyMinimalFeedView(const ISummaryAdapter::SP &summary_adapter,
                      const DocumentMetaStore::SP &meta_store,
                      searchcorespi::index::IThreadingService &writeService,
                      documentmetastore::ILidReuseDelayer &lidReuseDelayer,
                      CommitTimeTracker &commitTimeTracker,
                      const PersistentParams &params) :
        StoreOnlyFeedView(StoreOnlyFeedView::Context(summary_adapter,
                        search::index::Schema::SP(),
                        DocumentMetaStoreContext::SP(
                                new DocumentMetaStoreContext(meta_store)),
                        myGetDocumentTypeRepo(),
                        writeService,
                        lidReuseDelayer,
                        commitTimeTracker),
                        params),
        removeAttributes_count(0),
        removeIndexedFields_count(0),
        heartBeatAttributes_count(0),
        heartBeatIndexedFields_count(0) {
    }
    virtual void removeAttributes(SerialNum s, const LidVector &l,
                                  bool immediateCommit, OnWriteDoneType onWriteDone) override {
        StoreOnlyFeedView::removeAttributes(s, l, immediateCommit, onWriteDone);
        ++removeAttributes_count;
    }
    virtual void removeIndexedFields(SerialNum s, const LidVector &l,
                                     bool immediateCommit,
                                     OnWriteDoneType onWriteDone) override {
        StoreOnlyFeedView::removeIndexedFields(s, l,
                                               immediateCommit, onWriteDone);
        ++removeIndexedFields_count;
    }
    virtual void heartBeatIndexedFields(SerialNum s) override {
        StoreOnlyFeedView::heartBeatIndexedFields(s);
        ++heartBeatIndexedFields_count;
    }
    virtual void heartBeatAttributes(SerialNum s) override {
        StoreOnlyFeedView::heartBeatAttributes(s);
        ++heartBeatAttributes_count;
    }
};

const uint32_t subdb_id = 0;

struct Fixture {
    int remove_count;
    int put_count;
    int heartbeat_count;
    DocumentMetaStore::SP meta_store;
    ExecutorThreadingService writeService;
    documentmetastore::LidReuseDelayer _lidReuseDelayer;
    CommitTimeTracker _commitTimeTracker;
    MyMinimalFeedView::UP feedview;
 
    Fixture(SubDbType subDbType = SubDbType::READY)
        : remove_count(0),
          put_count(0),
          heartbeat_count(0),
          meta_store(new DocumentMetaStore(std::make_shared<BucketDBOwner>(),
                                           DocumentMetaStore::getFixedName(),
                                           search::GrowStrategy(),
                                           DocumentMetaStore::IGidCompare::SP(
                                                   new DocumentMetaStore::
                                                   DefaultGidCompare),
                                           subDbType)),
          writeService(),
          _lidReuseDelayer(writeService, *meta_store),
          _commitTimeTracker(fastos::TimeStamp()),
          feedview() {
        PerDocTypeFeedMetrics metrics(0);
        StoreOnlyFeedView::PersistentParams
            params(0, 0, DocTypeName("foo"), metrics, subdb_id,
                   subDbType);
        meta_store->constructFreeList();
        ISummaryAdapter::SP adapter(new MySummaryAdapter(
                        remove_count, put_count, heartbeat_count));
        feedview.reset(new MyMinimalFeedView(adapter, meta_store, writeService,
                                             _lidReuseDelayer,
                                             _commitTimeTracker, params));
    }

    ~Fixture() {
        writeService.sync();
    }

    void addSingleDocToMetaStore(uint32_t expected_lid) {
        typedef DocumentMetaStore::Result Result;
        DocumentId id(make_string("groupdoc:test:foo:%d", expected_lid));
        Result inspect = meta_store->inspect(id.getGlobalId());
        uint32_t docSize = 1;
        EXPECT_EQUAL(expected_lid,
                     meta_store->put(id.getGlobalId(),
                                     id.getGlobalId().convertToBucketId(),
                                     Timestamp(10), docSize, inspect.getLid()).getLid());
    }

    void addDocsToMetaStore(int count) {
        for (int i = 1; i <= count; ++i) {
            addSingleDocToMetaStore(i);
            EXPECT_TRUE(meta_store->validLid(i));
        }
    }

    template <typename FunctionType>
    void runInMaster(FunctionType func) {
        test::runInMaster(writeService, func);
    }

};

TEST_F("require that prepareMove sets target db document id", Fixture)
{
    Document::SP doc(new Document);
    MoveOperation op(BucketId(20, 42), Timestamp(10), doc, 1, subdb_id + 1);
    f.runInMaster([&] () { f.feedview->prepareMove(op); });

    DbDocumentId target_id = op.getDbDocumentId();
    EXPECT_EQUAL(subdb_id, target_id.getSubDbId());
    EXPECT_EQUAL(1u, target_id.getLid());
}

TEST_F("require that handleMove adds document to target "
       "and removes it from source", Fixture)
{
    Document::SP doc(new Document);
    MoveOperation op(doc->getId().getGlobalId().convertToBucketId(),
                     Timestamp(10), doc,
                     DbDocumentId(subdb_id + 1, 1), subdb_id);
    op.setSerialNum(1);
    EXPECT_EQUAL(0, f.put_count);
    f.runInMaster([&] () { f.feedview->prepareMove(op); });
    f.runInMaster([&] () { f.feedview->handleMove(op); });
    EXPECT_EQUAL(1, f.put_count);
    uint32_t lid = op.getDbDocumentId().getLid();
    EXPECT_TRUE(f.meta_store->validLid(lid));

    // Change the MoveOperation so this is the source sub db.
    op.setDbDocumentId(DbDocumentId(subdb_id + 1, lid));
    op.setPrevDbDocumentId(DbDocumentId(subdb_id, lid));
    EXPECT_EQUAL(0, f.remove_count);
    f.runInMaster([&] () { f.feedview->handleMove(op); });
    EXPECT_FALSE(f.meta_store->validLid(lid));
    EXPECT_EQUAL(1, f.remove_count);
}


TEST_F("require that handleMove handles move within same subdb", Fixture)
{
    Document::SP doc(new Document);
    DocumentId doc1id("groupdoc:test:foo:1");
    uint32_t docSize = 1;
    f.runInMaster([&] () { f.meta_store->put(doc1id.getGlobalId(),
                      doc1id.getGlobalId().convertToBucketId(),
                      Timestamp(9), docSize, 1); });
    f.runInMaster([&] () { f.meta_store->put(doc->getId().getGlobalId(),
                      doc->getId().getGlobalId().convertToBucketId(),
                      Timestamp(10), docSize, 2); });
    f.runInMaster([&] () { f.meta_store->remove(1); });
    f.meta_store->removeComplete(1);
    MoveOperation op(doc->getId().getGlobalId().convertToBucketId(),
                     Timestamp(10), doc,
                     DbDocumentId(subdb_id, 2), subdb_id);
    op.setTargetLid(1);
    op.setSerialNum(1);
    EXPECT_EQUAL(0, f.put_count); 
    EXPECT_EQUAL(0, f.remove_count);
    f.runInMaster([&] () { f.feedview->handleMove(op); });
    EXPECT_EQUAL(1, f.put_count);
    EXPECT_EQUAL(1, f.remove_count);
    uint32_t lid = op.getDbDocumentId().getLid();
    EXPECT_TRUE(f.meta_store->validLid(lid));
}


TEST_F("require that prune removed documents removes documents",
       Fixture(SubDbType::REMOVED))
{
    f.addDocsToMetaStore(3);

    LidVectorContext::SP lids(new LidVectorContext(4));
    lids->addLid(1);
    lids->addLid(3);
    PruneRemovedDocumentsOperation op(lids->getDocIdLimit(), subdb_id);
    op.setLidsToRemove(lids);
    op.setSerialNum(1);  // allows use of meta store.
    f.runInMaster([&] () { f.feedview->handlePruneRemovedDocuments(op); });

    EXPECT_EQUAL(2, f.remove_count);
    EXPECT_FALSE(f.meta_store->validLid(1));
    EXPECT_TRUE(f.meta_store->validLid(2));
    EXPECT_FALSE(f.meta_store->validLid(3));
    EXPECT_EQUAL(0, f.feedview->removeAttributes_count);
    EXPECT_EQUAL(0, f.feedview->removeIndexedFields_count);
}

TEST_F("require that heartbeat propagates and commits meta store", Fixture)
{
    EXPECT_EQUAL(0u, f.meta_store->getStatus().getLastSyncToken());
    EXPECT_EQUAL(0, f.feedview->heartBeatIndexedFields_count);
    EXPECT_EQUAL(0, f.feedview->heartBeatAttributes_count);
    EXPECT_EQUAL(0, f.heartbeat_count);
    f.runInMaster([&] () { f.feedview->heartBeat(2); });
    EXPECT_EQUAL(2u, f.meta_store->getStatus().getLastSyncToken());
    EXPECT_EQUAL(1, f.feedview->heartBeatIndexedFields_count);
    EXPECT_EQUAL(1, f.feedview->heartBeatAttributes_count);
    EXPECT_EQUAL(1, f.heartbeat_count);
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }