aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/storageserver/storagenode.h
blob: a96f6b52a66ccd2d5fd691c65a2e62b19a38af4e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * Main storage server class.
 *
 * This class sets up the entire storage server.
 *
 * @author Håkon Humberset
 */

#pragma once

#include <vespa/config-bucketspaces.h>
#include <vespa/config-stor-distribution.h>
#include <vespa/config/helper/ifetchercallback.h>
#include <vespa/config/subscription/configuri.h>
#include <vespa/document/config/config-documenttypes.h>
#include <vespa/storage/common/doneinitializehandler.h>
#include <vespa/storage/config/config-stor-bouncer.h>
#include <vespa/storage/config/config-stor-communicationmanager.h>
#include <vespa/storage/config/config-stor-server.h>
#include <vespa/storage/storageutil/resumeguard.h>
#include <vespa/storageframework/defaultimplementation/component/componentregisterimpl.h>
#include <vespa/storageframework/generic/metric/metricupdatehook.h>
#include <atomic>
#include <mutex>

namespace document { class DocumentTypeRepo; }
namespace config { class ConfigFetcher; }

namespace storage {

class ApplicationGenerationFetcher;
class CommunicationManager;
class FileStorManager;
class HostInfo;
class IStorageChainBuilder;
class NodeIdentity;
class StateManager;
class StateReporter;
class StatusMetricConsumer;
class StatusWebServer;
class StorageComponent;
class StorageLink;
class NodeStateReporter;
struct DeadLockDetector;
struct StorageMetricSet;
struct StorageNodeContext;

namespace lib { class NodeType; }


class StorageNode : private framework::MetricUpdateHook,
                    private DoneInitializeHandler,
                    private framework::defaultimplementation::ShutdownListener
{
public:
    using BucketspacesConfig         = vespa::config::content::core::BucketspacesConfig;
    using CommunicationManagerConfig = vespa::config::content::core::StorCommunicationmanagerConfig;
    using StorBouncerConfig          = vespa::config::content::core::StorBouncerConfig;
    using StorDistributionConfig     = vespa::config::content::StorDistributionConfig;
    using StorServerConfig           = vespa::config::content::core::StorServerConfig;

    enum RunMode { NORMAL, SINGLE_THREADED_TEST_MODE };

    StorageNode(const StorageNode &) = delete;
    StorageNode & operator = (const StorageNode &) = delete;

    struct BootstrapConfigs {
        std::unique_ptr<StorBouncerConfig>          bouncer_cfg;
        std::unique_ptr<BucketspacesConfig>         bucket_spaces_cfg;
        std::unique_ptr<CommunicationManagerConfig> comm_mgr_cfg;
        std::unique_ptr<StorDistributionConfig>     distribution_cfg;
        std::unique_ptr<StorServerConfig>           server_cfg;

        BootstrapConfigs();
        ~BootstrapConfigs();
        BootstrapConfigs(BootstrapConfigs&&) noexcept;
        BootstrapConfigs& operator=(BootstrapConfigs&&) noexcept;
    };

    StorageNode(const config::ConfigUri& configUri,
                StorageNodeContext& context,
                BootstrapConfigs bootstrap_configs,
                ApplicationGenerationFetcher& generationFetcher,
                std::unique_ptr<HostInfo> hostInfo,
                RunMode = NORMAL);
    ~StorageNode() override;

    virtual const lib::NodeType& getNodeType() const = 0;
    [[nodiscard]] bool attemptedStopped() const;
    void notifyDoneInitializing() override;
    void waitUntilInitialized(vespalib::duration timeout = 15s);
    void updateMetrics(const MetricLockGuard & guard) override;

    /** Updates the document type repo. */
    void setNewDocumentRepo(const std::shared_ptr<const document::DocumentTypeRepo>& repo);

    /**
     * Pauses the persistence processing. While the returned ResumeGuard
     * is alive, no calls will be made towards the persistence provider.
     */
    virtual ResumeGuard pause() = 0;
    void requestShutdown(vespalib::stringref reason) override;
    DoneInitializeHandler& getDoneInitializeHandler() { return *this; }

    void configure(std::unique_ptr<StorServerConfig> config);
    void configure(std::unique_ptr<StorDistributionConfig> config);
    void configure(std::unique_ptr<BucketspacesConfig>);
    void configure(std::unique_ptr<CommunicationManagerConfig> config);
    void configure(std::unique_ptr<StorBouncerConfig> config);

    // For testing
    StorageLink* getChain() { return _chain.get(); }
    virtual void initializeStatusWebServer();
private:
    bool _singleThreadedDebugMode;

    std::unique_ptr<HostInfo> _hostInfo;

    StorageNodeContext& _context;
    ApplicationGenerationFetcher& _generationFetcher;
    vespalib::string _rootFolder;
    std::atomic<bool> _attemptedStopped;
    vespalib::string _pidFile;

    // First components that doesn't depend on others
    std::unique_ptr<StatusWebServer>           _statusWebServer;
    std::shared_ptr<StorageMetricSet>          _metrics;
    std::unique_ptr<metrics::MetricManager>    _metricManager;

    // Depends on bucket databases and stop() functionality
    std::unique_ptr<DeadLockDetector>          _deadLockDetector;
    // Depends on metric manager
    std::unique_ptr<StatusMetricConsumer>      _statusMetrics;
    // Depends on metric manager
    std::unique_ptr<StateReporter>             _stateReporter;
    std::unique_ptr<StateManager>              _stateManager;

    // The storage chain can depend on anything.
    std::unique_ptr<StorageLink>               _chain;

    template <typename ConfigT>
    struct ConfigWrapper {
        std::unique_ptr<ConfigT> staging;
        std::unique_ptr<ConfigT> active;

        ConfigWrapper() noexcept;
        explicit ConfigWrapper(std::unique_ptr<ConfigT> initial_active) noexcept;
        ~ConfigWrapper();

        void promote_staging_to_active() noexcept;
    };

    template <typename ConfigT>
    void stage_config_change(ConfigWrapper<ConfigT>& my_cfg, std::unique_ptr<ConfigT> new_cfg);

protected:
    // Lock taken while doing configuration of the server.
    std::mutex _configLock;
    std::mutex _initial_config_mutex; // TODO can probably be removed
    using InitialGuard = std::lock_guard<std::mutex>;

    ConfigWrapper<StorBouncerConfig>          _bouncer_config;
    ConfigWrapper<BucketspacesConfig>         _bucket_spaces_config;
    ConfigWrapper<CommunicationManagerConfig> _comm_mgr_config;
    ConfigWrapper<StorDistributionConfig>     _distribution_config;
    ConfigWrapper<StorServerConfig>           _server_config;

    [[nodiscard]] const StorBouncerConfig& bouncer_config() const noexcept {
        return *_bouncer_config.active;
    }
    [[nodiscard]] const BucketspacesConfig& bucket_spaces_config() const noexcept {
        return *_bucket_spaces_config.active;
    }
    [[nodiscard]] const CommunicationManagerConfig& communication_manager_config() const noexcept {
        return *_comm_mgr_config.active;
    }
    [[nodiscard]] const StorDistributionConfig& distribution_config() const noexcept {
        return *_distribution_config.active;
    }
    [[nodiscard]] const StorServerConfig& server_config() const noexcept {
        return *_server_config.active;
    }

    std::unique_ptr<StorageComponent> _component;
    std::unique_ptr<NodeIdentity> _node_identity;
    config::ConfigUri _configUri;
    CommunicationManager* _communicationManager;
private:
    std::unique_ptr<IStorageChainBuilder>      _chain_builder;
protected:

    /**
     * Node subclasses currently need to explicitly acquire ownership of state
     * manager so that they can add it to the end of their processing chains,
     * which this method allows for.
     * Any component releasing the state manager must ensure it lives for as
     * long as the node instance itself lives.
     */
    std::unique_ptr<StateManager> releaseStateManager();

    void initialize(const NodeStateReporter & reporter);
    virtual void initializeNodeSpecific() = 0;
    virtual void perform_post_chain_creation_init_steps() = 0;
    virtual void createChain(IStorageChainBuilder &builder) = 0;
    virtual void handleLiveConfigUpdate(const InitialGuard & initGuard);
    void shutdown();

    virtual void on_bouncer_config_changed() { /* no-op by default */ }
public:
    void set_storage_chain_builder(std::unique_ptr<IStorageChainBuilder> builder);
};

} // storage