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

#pragma once

#include <vespa/config-stor-distribution.h>
#include <vespa/config-upgrading.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/config-bucketspaces.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 config::IFetcherCallback<vespa::config::content::core::StorServerConfig>,
                    private config::IFetcherCallback<vespa::config::content::StorDistributionConfig>,
                    private config::IFetcherCallback<vespa::config::content::UpgradingConfig>,
                    private config::IFetcherCallback<vespa::config::content::core::BucketspacesConfig>,
                    private framework::MetricUpdateHook,
                    private DoneInitializeHandler,
                    private framework::defaultimplementation::ShutdownListener
{
public:
    enum RunMode { NORMAL, SINGLE_THREADED_TEST_MODE };

    StorageNode(const StorageNode &) = delete;
    StorageNode & operator = (const StorageNode &) = delete;
    /**
     * @param excludeStorageChain With this option set, no chain will be set
     * up. This can be useful in unit testing if you need a storage server
     * instance, but you want to have full control over the components yourself.
     */
    StorageNode(const config::ConfigUri & configUri,
                StorageNodeContext& context,
                ApplicationGenerationFetcher& generationFetcher,
                std::unique_ptr<HostInfo> hostInfo,
                RunMode = NORMAL);
    ~StorageNode() override;

    virtual const lib::NodeType& getNodeType() const = 0;
    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; }

    // For testing
    StorageLink* getChain() { return _chain.get(); }
    virtual void initializeStatusWebServer();
protected:
    using StorServerConfig = vespa::config::content::core::StorServerConfig;
    using UpgradingConfig = vespa::config::content::UpgradingConfig;
    using StorDistributionConfig = vespa::config::content::StorDistributionConfig;
    using BucketspacesConfig = vespa::config::content::core::BucketspacesConfig;
private:
    bool _singleThreadedDebugMode;
        // Subscriptions to config
    std::unique_ptr<config::ConfigFetcher> _configFetcher;

    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;

    /** Implementation of config callbacks. */
    void configure(std::unique_ptr<StorServerConfig> config) override;
    void configure(std::unique_ptr<UpgradingConfig> config) override;
    void configure(std::unique_ptr<StorDistributionConfig> config) override;
    virtual void configure(std::unique_ptr<document::config::DocumenttypesConfig> config,
                           bool hasChanged, int64_t generation);
    void configure(std::unique_ptr<BucketspacesConfig>) override;

protected:
    // Lock taken while doing configuration of the server.
    std::mutex _configLock;
    std::mutex _initial_config_mutex;
    using InitialGuard = std::lock_guard<std::mutex>;
    // Current running config. Kept, such that we can see what has been
    // changed in live config updates.
    std::unique_ptr<StorServerConfig> _serverConfig;
    std::unique_ptr<UpgradingConfig> _clusterConfig;
    std::unique_ptr<StorDistributionConfig> _distributionConfig;
    std::unique_ptr<document::config::DocumenttypesConfig> _doctypesConfig;
    std::unique_ptr<BucketspacesConfig> _bucketSpacesConfig;
    // New configs gotten that has yet to have been handled
    std::unique_ptr<StorServerConfig> _newServerConfig;
    std::unique_ptr<UpgradingConfig> _newClusterConfig;
    std::unique_ptr<StorDistributionConfig> _newDistributionConfig;
    std::unique_ptr<document::config::DocumenttypesConfig> _newDoctypesConfig;
    std::unique_ptr<BucketspacesConfig> _newBucketSpacesConfig;
    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 subscribeToConfigs();
    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 removeConfigSubscriptions();
public:
    void set_storage_chain_builder(std::unique_ptr<IStorageChainBuilder> builder);
};

} // storage