aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/common/filestortestfixture.cpp
blob: 55a1b5d435b97afb806c8041a66da9bc5c348fd3 (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
// Copyright Yahoo. 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/storageapi/message/bucket.h>
#include <vespa/persistence/dummyimpl/dummypersistence.h>
#include <tests/persistence/common/filestortestfixture.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/fieldset/fieldsets.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/persistence/spi/test.h>
#include <sstream>

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

namespace storage {

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>(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()));
    _node->getPersistenceProvider().initialize();
}

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

void
FileStorTestFixture::createBucket(const document::BucketId& bid)
{
    _node->getPersistenceProvider().createBucket(makeSpiBucket(bid));
    StorBucketDatabase::WrappedEntry entry(
            _node->getStorageBucketDatabase().get(bid, "foo", StorBucketDatabase::CREATE_IF_NONEXISTING));
    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.exists();
}

FileStorTestFixture::TestFileStorComponents::TestFileStorComponents(
        FileStorTestFixture& fixture,
        const StorageLinkInjector& injector)
    : _fixture(fixture),
      top(),
      manager(nullptr)
{
    injector.inject(top);
    auto fsm = std::make_unique<FileStorManager>(config::ConfigUri(fixture._config->getConfigId()), fixture._node->getPersistenceProvider(),
                                                 fixture._node->getComponentRegister(), *fixture._node, fixture._node->get_host_info());
    manager = fsm.get();
    top.push_back(std::move(fsm));
    top.open();
}

vespalib::string _Storage("storage");

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";
    auto cmd = std::make_shared<api::GetCommand>(makeDocumentBucket(bid), document::DocumentId(id.str()), document::AllFields::NAME);
    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);
    auto cmd = std::make_shared<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()));
    auto cmd = std::make_shared<api::PutCommand>(makeDocumentBucket(bid), doc, timestamp);
    cmd->setAddress(makeSelfAddress());
    top.sendDown(cmd);
}

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

} // ns storage