aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/common/teststorageapp.cpp
blob: b2b82a46850ed3b03d4cb3affa71580b616f5768 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "teststorageapp.h"
#include <vespa/storage/common/content_bucket_db_options.h>
#include <vespa/storage/config/config-stor-server.h>
#include <vespa/storage/config/config-stor-distributormanager.h>
#include <vespa/storage/config/config-stor-visitordispatcher.h>
#include <vespa/config-stor-distribution.h>
#include <vespa/persistence/dummyimpl/dummypersistence.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/vdslib/state/cluster_state_bundle.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/config/subscription/configuri.h>
#include <vespa/config/helper/configgetter.hpp>
#include <thread>
#include <sstream>

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

using storage::framework::defaultimplementation::ComponentRegisterImpl;

namespace storage {

TestStorageApp::TestStorageApp(StorageComponentRegisterImpl::UP compReg,
                               const lib::NodeType& type, NodeIndex index,
                               const config::ConfigUri& config_uri)
    : TestComponentRegister(ComponentRegisterImpl::UP(std::move(compReg))),
      _compReg(dynamic_cast<StorageComponentRegisterImpl&>(TestComponentRegister::getComponentRegister())),
      _docMan(),
      _nodeStateUpdater(type),
      _configId(config_uri.getConfigId()),
      _node_identity("test_cluster", type, index),
      _initialized(false)
{
    // Use config to adjust values
    vespalib::string clusterName = "mycluster";
    uint32_t redundancy = 2;
    uint32_t nodeCount = 10;
    auto serverConfig = config::ConfigGetter<vespa::config::content::core::StorServerConfig>::getConfig(config_uri.getConfigId(), config_uri.getContext());
    clusterName = serverConfig->clusterName;
    if (index == 0xffff) {
        index = serverConfig->nodeIndex;
    }
    redundancy = config::ConfigGetter<vespa::config::content::StorDistributionConfig>::getConfig(config_uri.getConfigId(), config_uri.getContext())->redundancy;
    if (index >= nodeCount) {
        nodeCount = index + 1;
    }
    if (redundancy > nodeCount) {
        redundancy = nodeCount;
    }

    _compReg.setNodeInfo(clusterName, type, index);
    _compReg.setNodeStateUpdater(_nodeStateUpdater);
    _compReg.setDocumentTypeRepo(_docMan.getTypeRepoSP());
    _compReg.setBucketIdFactory(document::BucketIdFactory());
    auto distr = std::make_shared<lib::Distribution>(
            lib::Distribution::getDefaultDistributionConfig(redundancy, nodeCount));
    _compReg.setDistribution(distr);
    _nodeStateUpdater.patch_distribution(distr);
}

TestStorageApp::~TestStorageApp() = default;

void
TestStorageApp::setDistribution(Redundancy redundancy, NodeCount nodeCount)
{
    auto distr = std::make_shared<lib::Distribution>(
            lib::Distribution::getDefaultDistributionConfig(redundancy, nodeCount));
    _compReg.setDistribution(distr);
    _nodeStateUpdater.patch_distribution(distr);
}

void
TestStorageApp::setTypeRepo(std::shared_ptr<const document::DocumentTypeRepo> repo)
{
    _compReg.setDocumentTypeRepo(std::move(repo));
}

void
TestStorageApp::setClusterState(const lib::ClusterState& c)
{
    _nodeStateUpdater.setClusterState(std::make_shared<lib::ClusterState>(c));
}

void
TestStorageApp::set_cluster_state_bundle(std::shared_ptr<const lib::ClusterStateBundle> state_bundle)
{
    _nodeStateUpdater.setClusterStateBundle(std::move(state_bundle));
}

namespace {

NodeIndex node_index_from_config(const config::ConfigUri& uri) {
    return NodeIndex(config::ConfigGetter<vespa::config::content::core::StorServerConfig>::getConfig(uri.getConfigId(), uri.getContext())->nodeIndex);
}

VESPA_THREAD_STACK_TAG(test_executor)
}

TestServiceLayerApp::TestServiceLayerApp(NodeIndex index, const config::ConfigUri& config_uri)
    : TestStorageApp(std::make_unique<ServiceLayerComponentRegisterImpl>(ContentBucketDbOptions()),
                     lib::NodeType::STORAGE, index, config_uri),
      _compReg(dynamic_cast<ServiceLayerComponentRegisterImpl&>(TestStorageApp::getComponentRegister())),
      _persistenceProvider(),
      _executor(vespalib::SequencedTaskExecutor::create(test_executor, 1)),
      _host_info()
{
    lib::NodeState ns(*_nodeStateUpdater.getReportedNodeState());
    _nodeStateUpdater.setReportedNodeState(ns);
}

TestServiceLayerApp::TestServiceLayerApp(const config::ConfigUri& config_uri)
    : TestServiceLayerApp(node_index_from_config(config_uri), config_uri)
{
}

TestServiceLayerApp::~TestServiceLayerApp() = default;

void
TestServiceLayerApp::setupDummyPersistence()
{
    auto provider = std::make_unique<spi::dummy::DummyPersistence>(getTypeRepo());
    provider->initialize();
    setPersistenceProvider(std::move(provider));
}

void
TestServiceLayerApp::setPersistenceProvider(PersistenceProviderUP provider)
{
    _persistenceProvider = std::move(provider);
}

spi::PersistenceProvider&
TestServiceLayerApp::getPersistenceProvider()
{
    if ( ! _persistenceProvider) {
        throw vespalib::IllegalStateException("Persistence provider requested but not initialized.", VESPA_STRLOC);
    }
    return *_persistenceProvider;
}

namespace {

template<typename T>
[[nodiscard]] T get_config(const config::ConfigUri& uri) {
    return *config::ConfigGetter<T>::getConfig(uri.getConfigId(), uri.getContext());
}

}

void
TestDistributorApp::configure(const config::ConfigUri& config_uri)
{
    auto dc = get_config<vespa::config::content::core::StorDistributormanagerConfig>(config_uri);
    _compReg.setDistributorConfig(dc);
    auto vc = get_config<vespa::config::content::core::StorVisitordispatcherConfig>(config_uri);
    _compReg.setVisitorConfig(vc);
}

TestDistributorApp::TestDistributorApp(NodeIndex index, const config::ConfigUri& config_uri)
    : TestStorageApp(std::make_unique<DistributorComponentRegisterImpl>(),
                     lib::NodeType::DISTRIBUTOR, index, config_uri),
      _compReg(dynamic_cast<DistributorComponentRegisterImpl&>(TestStorageApp::getComponentRegister())),
      _lastUniqueTimestampRequested(0),
      _uniqueTimestampCounter(0)
{
    _compReg.setTimeCalculator(*this);
    configure(config_uri);
}

TestDistributorApp::TestDistributorApp(const config::ConfigUri& config_uri)
    : TestDistributorApp(node_index_from_config(config_uri), config_uri)
{
}

TestDistributorApp::~TestDistributorApp() = default;

api::Timestamp
TestDistributorApp::generate_unique_timestamp()
{
    std::lock_guard guard(_accessLock);
    uint64_t timeNow(vespalib::count_s(getClock().getSystemTime().time_since_epoch()));
    if (timeNow == _lastUniqueTimestampRequested) {
        ++_uniqueTimestampCounter;
    } else {
        if (timeNow < _lastUniqueTimestampRequested) {
            LOG(error, "Time has moved backwards, from %" PRIu64 " to %" PRIu64 ".",
                    _lastUniqueTimestampRequested, timeNow);
        }
        _lastUniqueTimestampRequested = timeNow;
        _uniqueTimestampCounter = 0;
    }

    return _lastUniqueTimestampRequested * 1000000ll + _uniqueTimestampCounter;
}

} // storage