aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/pendingclusterstate.h
blob: babcebea69d47e5b57062304da88374c07d2c5b2 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "pending_bucket_space_db_transition_entry.h"
#include "clusterinformation.h"
#include <vespa/storage/common/storagelink.h>
#include <vespa/storageapi/message/bucket.h>
#include <vespa/storageapi/message/state.h>
#include <vespa/storageframework/generic/clock/clock.h>
#include <vespa/vdslib/state/cluster_state_bundle.h>
#include <vespa/vespalib/util/xmlserializable.h>
#include "outdated_nodes_map.h"
#include <unordered_map>
#include <deque>

namespace storage::distributor {

class DistributorMessageSender;
class PendingBucketSpaceDbTransition;
class DistributorBucketSpaceRepo;
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(const std::string& prevClusterState, const std::string& newClusterState, uint32_t processingTime);
        Summary(const Summary &);
        Summary & operator = (const Summary &);
        Summary(Summary &&) = default;
        Summary & operator = (Summary &&) = default;
        ~Summary();

        std::string _prevClusterState;
        std::string _newClusterState;
        uint32_t _processingTime;
    };

    static std::unique_ptr<PendingClusterState> createForClusterStateChange(
            const framework::Clock& clock,
            const ClusterInformation::CSP& clusterInfo,
            DistributorMessageSender& sender,
            DistributorBucketSpaceRepo& bucketSpaceRepo,
            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, bucketSpaceRepo,
                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,
            DistributorBucketSpaceRepo& bucketSpaceRepo,
            api::Timestamp creationTimestamp)
    {
        // Naked new due to private constructor
        return std::unique_ptr<PendingClusterState>(new PendingClusterState(
                clock, clusterInfo, sender, bucketSpaceRepo, 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.
     */
    bool done() {
        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() {
        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() {
        _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 mergeIntoBucketDatabases();
    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);

    void printXml(vespalib::XmlOutputStream&) const override;
    Summary getSummary() const;
    std::string requestNodesToString() 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,
            DistributorBucketSpaceRepo& bucketSpaceRepo,
            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,
            DistributorBucketSpaceRepo& bucketSpaceRepo,
            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();

    /**
     * Number of nodes with node type 'storage' in _newClusterState.
     */
    uint16_t newStateStorageNodeCount() const;
    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);

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

    std::map<uint64_t, BucketSpaceAndNode> _sentMessages;
    std::vector<bool> _requestedNodes;
    std::deque<std::pair<framework::MilliSecTime, BucketSpaceAndNode> > _delayedRequests;

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

    const framework::Clock& _clock;
    ClusterInformation::CSP _clusterInfo;
    api::Timestamp _creationTimestamp;

    DistributorMessageSender& _sender;
    DistributorBucketSpaceRepo& _bucketSpaceRepo;
    uint32_t _clusterStateVersion;
    bool _isVersionedTransition;
    bool _bucketOwnershipTransfer;
    std::unordered_map<document::BucketSpace, std::unique_ptr<PendingBucketSpaceDbTransition>, document::BucketSpace::hash> _pendingTransitions;
};

}