aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/pendingclusterstate.h
blob: 9a001aa07932b80309cfc25d0b71d0e26a23a1b2 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "node_supported_features.h"
#include "pending_bucket_space_db_transition_entry.h"
#include "clusterinformation.h"
#include <vespa/storageapi/message/bucket.h>
#include <vespa/storageapi/message/state.h>
#include <vespa/vdslib/state/cluster_state_bundle.h>
#include <vespa/vespalib/util/xmlserializable.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include "outdated_nodes_map.h"
#include <unordered_map>
#include <deque>

namespace storage::framework { struct Clock; }

namespace storage::distributor {

class BucketSpaceStateMap;
class DistributorMessageSender;
class PendingBucketSpaceDbTransition;
class StripeAccessGuard;

/**
 * Class used by TopLevelBucketDBUpdater to track request bucket info
 * messages sent to the storage nodes.
 */
class PendingClusterState : public vespalib::XmlSerializable {
public:
    using OutdatedNodes = dbtransition::OutdatedNodes;
    using OutdatedNodesMap = dbtransition::OutdatedNodesMap;
    struct Summary {
        Summary(std::string prevClusterState, std::string newClusterState, vespalib::duration processingTime);
        Summary(const Summary &);
        Summary & operator = (const Summary &);
        Summary(Summary &&) noexcept = default;
        Summary & operator = (Summary &&) noexcept = default;
        ~Summary();

        std::string _prevClusterState;
        std::string _newClusterState;
        vespalib::duration _processingTime;
    };

    static std::unique_ptr<PendingClusterState> createForClusterStateChange(
            const framework::Clock& clock,
            const ClusterInformation::CSP& clusterInfo,
            DistributorMessageSender& sender,
            const BucketSpaceStateMap& bucket_space_states,
            const std::shared_ptr<api::SetSystemStateCommand>& newStateCmd,
            const OutdatedNodesMap& outdatedNodesMap,
            api::Timestamp creationTimestamp)
    {
        // Naked new due to private constructor
        return std::unique_ptr<PendingClusterState>(new PendingClusterState(
                clock, clusterInfo, sender, bucket_space_states,
                newStateCmd, outdatedNodesMap, creationTimestamp));
    }

    /**
     * Distribution changes always need to ask all storage nodes, so no
     * need to do an union of existing outdated nodes; implicit complete set.
     */
    static std::unique_ptr<PendingClusterState> createForDistributionChange(
            const framework::Clock& clock,
            const ClusterInformation::CSP& clusterInfo,
            DistributorMessageSender& sender,
            const BucketSpaceStateMap& bucket_space_states,
            api::Timestamp creationTimestamp)
    {
        // Naked new due to private constructor
        return std::unique_ptr<PendingClusterState>(new PendingClusterState(
                clock, clusterInfo, sender, bucket_space_states, creationTimestamp));
    }

    PendingClusterState(const PendingClusterState &) = delete;
    PendingClusterState & operator = (const PendingClusterState &) = delete;
    ~PendingClusterState() override;

    /**
     * Adds the info from the reply to our list of information.
     * Returns true if the reply was accepted by this object, false if not.
     */
    bool onRequestBucketInfoReply(const std::shared_ptr<api::RequestBucketInfoReply>& reply);

    /**
     * Tags the given node as having replied to at least one of the
     * request bucket info commands. Only used for debug logging.
     */
    void setNodeReplied(uint16_t nodeIdx) {
        _requestedNodes[nodeIdx] = true;
    }

    /** Called to resend delayed resends due to failures. */
    void resendDelayedMessages();

    /**
     * Returns true if all the nodes we requested have replied to
     * the request bucket info commands.
     */
    [[nodiscard]] bool done() const noexcept {
        return _sentMessages.empty() && _delayedRequests.empty();
    }

    bool hasBucketOwnershipTransfer() const noexcept {
        return _bucketOwnershipTransfer;
    }

    bool hasCommand() const noexcept {
        return (_cmd.get() != nullptr);
    }

    std::shared_ptr<api::SetSystemStateCommand> getCommand() const noexcept {
        return _cmd;
    }

    bool isVersionedTransition() const noexcept {
        return _isVersionedTransition;
    }

    uint32_t clusterStateVersion() const noexcept {
        return _clusterStateVersion;
    }

    bool isDeferred() const noexcept {
        return (isVersionedTransition()
                && _newClusterStateBundle.deferredActivation());
    }

    void clearCommand() noexcept {
        _cmd.reset();
    }

    const lib::ClusterStateBundle& getNewClusterStateBundle() const {
        return _newClusterStateBundle;
    }

    /**
     * Returns the union set of the outdated node set provided at construction
     * time and the set of nodes that the pending cluster state figured out
     * were outdated based on the cluster state diff. If the pending cluster
     * state was constructed for a distribution config change, this set will
     * be equal to the set of all available storage nodes.
     */
    OutdatedNodesMap getOutdatedNodesMap() const;

    /**
     * Merges all the results with the corresponding bucket databases.
     */
    void merge_into_bucket_databases(StripeAccessGuard& guard);

    // Get pending transition for a specific bucket space. Only used by unit test.
    PendingBucketSpaceDbTransition& getPendingBucketSpaceDbTransition(document::BucketSpace bucketSpace);

    // May be a subset of the nodes in the cluster, depending on how many nodes were consulted
    // as part of the pending cluster state. Caller must take care to aggregate features.
    const vespalib::hash_map<uint16_t, NodeSupportedFeatures>& gathered_node_supported_features() const noexcept {
        return _node_features;
    }

    void printXml(vespalib::XmlOutputStream&) const override;
    Summary getSummary() const;

private:
    // With 100ms resend timeout, this requires a particular node to have failed
    // for _at least_ threshold/10 seconds before a log warning is emitted.
    constexpr static size_t RequestFailureWarningEdgeTriggerThreshold = 200;

    /**
     * Creates a pending cluster state that represents
     * a set system state command from the cluster controller.
     */
    PendingClusterState(
            const framework::Clock&,
            const ClusterInformation::CSP& clusterInfo,
            DistributorMessageSender& sender,
            const BucketSpaceStateMap& bucket_space_states,
            const std::shared_ptr<api::SetSystemStateCommand>& newStateCmd,
            const OutdatedNodesMap& outdatedNodesMap,
            api::Timestamp creationTimestamp);

    /**
     * Creates a pending cluster state that represents a distribution
     * change.
     */
    PendingClusterState(
            const framework::Clock&,
            const ClusterInformation::CSP& clusterInfo,
            DistributorMessageSender& sender,
            const BucketSpaceStateMap& bucket_space_states,
            api::Timestamp creationTimestamp);

    struct BucketSpaceAndNode {
        document::BucketSpace bucketSpace;
        uint16_t              node;
        BucketSpaceAndNode(document::BucketSpace bucketSpace_, uint16_t node_)
            : bucketSpace(bucketSpace_),
              node(node_)
        {
        }
    };

    void initializeBucketSpaceTransitions(bool distributionChanged, const OutdatedNodesMap& outdatedNodesMap);
    void logConstructionInformation() const;
    void requestNode(BucketSpaceAndNode bucketSpaceAndNode);
    void requestNodes();
    void requestBucketInfoFromStorageNodesWithChangedState();

    bool shouldRequestBucketInfo() const;
    bool clusterIsDown() const;
    bool iAmDown() const;

    bool storageNodeUpInNewState(document::BucketSpace bucketSpace, uint16_t node) const;
    std::string getNewClusterStateBundleString() const;
    std::string getPrevClusterStateBundleString() const;
    void update_reply_failure_statistics(const api::ReturnCode& result, const BucketSpaceAndNode& source);
    void update_node_supported_features_from_reply(uint16_t node, const api::RequestBucketInfoReply& reply);

    using SentMessages       = std::map<uint64_t, BucketSpaceAndNode>;
    using DelayedRequests    = std::deque<std::pair<vespalib::steady_time , BucketSpaceAndNode>>;
    using PendingTransitions = std::unordered_map<document::BucketSpace, std::unique_ptr<PendingBucketSpaceDbTransition>, document::BucketSpace::hash>;
    using NodeFeatures       = vespalib::hash_map<uint16_t, NodeSupportedFeatures>;

    std::shared_ptr<api::SetSystemStateCommand> _cmd;

    SentMessages               _sentMessages;
    std::vector<bool>          _requestedNodes;
    DelayedRequests            _delayedRequests;

    lib::ClusterStateBundle    _prevClusterStateBundle;
    lib::ClusterStateBundle    _newClusterStateBundle;

    const framework::Clock&    _clock;
    ClusterInformation::CSP    _clusterInfo;
    api::Timestamp             _creationTimestamp;
    DistributorMessageSender&  _sender;
    const BucketSpaceStateMap& _bucket_space_states;
    uint32_t                   _clusterStateVersion;
    bool                       _isVersionedTransition;
    bool                       _bucketOwnershipTransfer;
    PendingTransitions         _pendingTransitions;
    NodeFeatures               _node_features;
};

}