summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/common/filestortestfixture.cpp
blob: 835b8ef10442f6b7c1eb6ee5260f2d88ffdbe3ea (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storage/persistence/messages.h>
#include <vespa/storage/persistence/filestorage/filestormanager.h>
#include <vespa/persistence/dummyimpl/dummypersistence.h>
#include <tests/persistence/common/filestortestfixture.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/persistence/spi/test.h>
#include <sstream>

using storage::spi::test::makeSpiBucket;
using document::test::makeDocumentBucket;

namespace storage {

spi::LoadType FileStorTestFixture::defaultLoadType = spi::LoadType(0, "default");
const uint32_t FileStorTestFixture::MSG_WAIT_TIME;

void
FileStorTestFixture::setupPersistenceThreads(uint32_t threads)
{
    std::string rootOfRoot = "todo-make-unique-filestorefixture";
    _config = std::make_unique<vdstestlib::DirConfig>(getStandardConfig(true, rootOfRoot));
    _config->getConfig("stor-server").set("root_folder", (rootOfRoot + "-vdsroot.2"));
    _config->getConfig("stor-devices").set("root_folder", (rootOfRoot + "-vdsroot.2"));
    _config->getConfig("stor-server").set("node_index", "1");
    _config->getConfig("stor-filestor").set("num_threads", std::to_string(threads));

    _node = std::make_unique<TestServiceLayerApp>(
            DiskCount(1), NodeIndex(1), _config->getConfigId());
    _testdoctype1 = _node->getTypeRepo()->getDocumentType("testdoctype1");
}

// Default provider setup which should work out of the box for most tests.
void
FileStorTestFixture::setUp()
{
    setupPersistenceThreads(1);
    _node->setPersistenceProvider(
            std::make_unique<spi::dummy::DummyPersistence>(_node->getTypeRepo(), 1));
}

void
FileStorTestFixture::tearDown()
{
    _node.reset();
}

void
FileStorTestFixture::createBucket(const document::BucketId& bid)
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    _node->getPersistenceProvider().createBucket(
            makeSpiBucket(bid), context);

    StorBucketDatabase::WrappedEntry entry(
            _node->getStorageBucketDatabase().get(bid, "foo",
                    StorBucketDatabase::CREATE_IF_NONEXISTING));
    entry->disk = 0;
    entry->info = api::BucketInfo(0, 0, 0, 0, 0, true, false);
    entry.write();
}

bool
FileStorTestFixture::bucketExistsInDb(const document::BucketId& bucket) const
{
    StorBucketDatabase::WrappedEntry entry(
            _node->getStorageBucketDatabase().get(bucket, "bucketExistsInDb"));
    return entry.exist();
}

FileStorTestFixture::TestFileStorComponents::TestFileStorComponents(
        FileStorTestFixture& fixture,
        const char* testName,
        const StorageLinkInjector& injector)
    : _testName(testName),
      _fixture(fixture),
      manager(new FileStorManager(fixture._config->getConfigId(),
                                  fixture._node->getPartitions(),
                                  fixture._node->getPersistenceProvider(),
                                  fixture._node->getComponentRegister()))
{
    injector.inject(top);
    top.push_back(StorageLink::UP(manager));
    top.open();
}

api::StorageMessageAddress
FileStorTestFixture::makeSelfAddress() {
    return api::StorageMessageAddress("storage", lib::NodeType::STORAGE, 0);
}

void
FileStorTestFixture::TestFileStorComponents::sendDummyGet(
        const document::BucketId& bid)
{
    std::ostringstream id;
    id << "id:foo:testdoctype1:n=" << bid.getId() << ":0";
    std::shared_ptr<api::GetCommand> cmd(
            new api::GetCommand(makeDocumentBucket(bid), document::DocumentId(id.str()), "[all]"));
    cmd->setAddress(makeSelfAddress());
    cmd->setPriority(255);
    top.sendDown(cmd);
}

void
FileStorTestFixture::TestFileStorComponents::sendDummyGetDiff(
        const document::BucketId& bid)
{
    std::vector<api::GetBucketDiffCommand::Node> nodes;
    nodes.push_back(0);
    nodes.push_back(1);
    std::shared_ptr<api::GetBucketDiffCommand> cmd(
            new api::GetBucketDiffCommand(makeDocumentBucket(bid), nodes, 12345));
    cmd->setAddress(makeSelfAddress());
    cmd->setPriority(255);
    top.sendDown(cmd);
}

void
FileStorTestFixture::TestFileStorComponents::sendPut(
        const document::BucketId& bid,
        uint32_t docIdx,
        uint64_t timestamp)
{
    std::ostringstream id;
    id << "id:foo:testdoctype1:n=" << bid.getId() << ":" << docIdx;
    document::Document::SP doc(
            _fixture._node->getTestDocMan().createDocument("foobar", id.str()));
    std::shared_ptr<api::PutCommand> cmd(
            new api::PutCommand(makeDocumentBucket(bid), doc, timestamp));
    cmd->setAddress(makeSelfAddress());
    top.sendDown(cmd);
}

void 
FileStorTestFixture::setClusterState(const std::string& state)
{
    _node->getStateUpdater().setClusterState(
            lib::ClusterState::CSP(new lib::ClusterState(state)));
}


} // ns storage