aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/storageserver/changedbucketownershiphandler.h
blob: 801534385f768913361fa9a63ca459e2841cefbc (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/document/bucket/bucketid.h>
#include <vespa/storage/common/storagelink.h>
#include <vespa/config-persistence.h>
#include <vespa/config/helper/ifetchercallback.h>
#include <vespa/storage/common/servicelayercomponent.h>
#include <vespa/storage/persistence/messages.h>
#include <vespa/metrics/valuemetric.h>
#include <vespa/metrics/countmetric.h>
#include <vespa/metrics/metricset.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <atomic>
#include <vector>
#include <unordered_map>

namespace config {
    class ConfigUri;
    class ConfigFetcher;
}
namespace storage {

namespace lib {
    class ClusterState;
    class ClusterStateBundle;
    class Distribution;
}

/**
 * The changed bucket ownership handler is a storage link that synchronously
 * intercepts attempts to change the state on the node and ensure any
 * operations to buckets whose ownership is changed are aborted.
 *
 * If default config is used, all mutating ideal state operations for buckets
 * that--upon time of checking in this handler--belong to a different
 * distributor than the one specified as the sender will be aborted.
 *
 * We consider the following operations as mutating ideal state ops:
 *  - SplitBucketCommand
 *  - JoinBucketsCommand
 *  - MergeBucketsCommand (already blocked by throttler, but let's not
 *    let that stop us)
 *  - RemoveLocationCommand (technically an external load op, but is used by
 *    the GC functionality and must therefore be included here)
 *  - SetBucketStateCommand
 *  - DeleteBucketCommand
 *  - CreateBucketCommand
 *
 *  If default config is used, all mutating external operations with altered
 *  bucket owneship will also be aborted.
 *
 *  We consider the following external operations as mutating:
 *   - PutCommand
 *   - UpdateCommand
 *   - RemoveCommand
 *   - RevertCommand
 */
class ChangedBucketOwnershipHandler : public StorageLink {
public:
    class Metrics : public metrics::MetricSet {
    public:
        metrics::LongAverageMetric averageAbortProcessingTime;
        metrics::LongCountMetric idealStateOpsAborted;
        metrics::LongCountMetric externalLoadOpsAborted;

        explicit Metrics(metrics::MetricSet* owner = nullptr);
        ~Metrics() override;
    };

    /**
     * Wrapper around the distribution & state pairs that decides how to
     * compute the owner distributor for a bucket. It's possible to have
     * an ownership state with a nullptr cluster state when the node
     * initially starts up, which is why no ownership state must be used unless
     * invoking valid() on it returns true.
     */
    class OwnershipState {
        using BucketSpace = document::BucketSpace;
        std::unordered_map<BucketSpace, std::shared_ptr<const lib::Distribution>, BucketSpace::hash> _distributions;
        std::shared_ptr<const lib::ClusterStateBundle> _state;
    public:
        using SP = std::shared_ptr<OwnershipState>;
        using CSP = std::shared_ptr<const OwnershipState>;

        OwnershipState(const ContentBucketSpaceRepo &contentBucketSpaceRepo,
                       std::shared_ptr<const lib::ClusterStateBundle> state);
        ~OwnershipState();

        static const uint16_t FAILED_TO_RESOLVE = 0xffff;

        [[nodiscard]] bool valid() const noexcept {
            return (!_distributions.empty() && _state);
        }

        /**
         * Precondition: valid() == true.
         */
        const lib::ClusterState& getBaselineState() const;

        uint16_t ownerOf(const document::Bucket& bucket) const;
        bool storageNodeUp(document::BucketSpace bucketSpace, uint16_t nodeIndex) const;
    };

    /**
     * For unit testing only; trigger a reload of the cluster state from the
     * component registry, since tests may want to set the cluster state
     * explicitly without sending a message through the chain.
     */
    void reloadClusterState();

private:
    class ClusterStateSyncAndApplyTask;

    using PersistenceConfig = vespa::config::content::PersistenceConfig;
    using ClusterStateBundleCSP = std::shared_ptr<const lib::ClusterStateBundle>;

    ServiceLayerComponent         _component;
    Metrics                       _metrics;
    vespalib::ThreadStackExecutor _state_sync_executor;
    mutable std::mutex            _stateLock;
    ClusterStateBundleCSP         _currentState;
    OwnershipState::CSP           _currentOwnership;
    std::atomic<bool>             _abortQueuedAndPendingOnStateChange;
    std::atomic<bool>             _abortMutatingIdealStateOps;
    std::atomic<bool>             _abortMutatingExternalLoadOps;

    std::unique_ptr<AbortBucketOperationsCommand::AbortPredicate>
    makeLazyAbortPredicate(
            const OwnershipState::CSP& oldOwnership,
            const OwnershipState::CSP& newOwnership) const;

    void logTransition(const lib::ClusterState& currentState,
                       const lib::ClusterState& newState) const;

    /**
     * Creates a new immutable OwnershipState based on the current distribution
     * and the provided cluster state and assigns it to _currentOwnership.
     */
    void setCurrentOwnershipWithStateNoLock(const lib::ClusterStateBundle&);

    /**
     * Grabs _stateLock and returns a shared_ptr to the current ownership
     * state, which may or may not be valid().
     */
    OwnershipState::CSP getCurrentOwnershipState() const;

    bool isMutatingCommandAndNeedsChecking(const api::StorageMessage&) const;

    bool isMutatingIdealStateOperation(const api::StorageMessage&) const;

    bool isMutatingExternalOperation(const api::StorageMessage&) const;
    /**
     * Returns whether the operation in cmd has a bucket whose ownership in
     * the current cluster state does not match the distributor marked as
     * being the sender in the message itself.
     *
     * Precondition: cmd is an instance of a message type containing a bucket
     *     identifier.
     */
    bool sendingDistributorOwnsBucketInCurrentState(
            const api::StorageCommand& cmd) const;
    /**
     * Creates a reply for cmd, assigns an ABORTED return code and sends the
     * reply back up the storage chain.
     */
    void abortOperation(api::StorageCommand& cmd);

    /**
     * Returns whether aborting queued, changed ops and waiting for pending
     * changed ops is enabled through config.
     */
    bool enabledOperationAbortingOnStateChange() const;

    /**
     * Returns whether aborting outdated ideal state operations has been enabled
     * through config.
     */
    bool enabledIdealStateAborting() const;

    bool enabledExternalLoadAborting() const;

public:
    ChangedBucketOwnershipHandler(const PersistenceConfig& bootstrap_config,
                                  ServiceLayerComponentRegister& compReg);
    ~ChangedBucketOwnershipHandler() override;

    bool onSetSystemState(const std::shared_ptr<api::SetSystemStateCommand>&) override;
    bool onDown(const std::shared_ptr<api::StorageMessage>&) override;
    bool onInternalReply(const std::shared_ptr<api::InternalReply>& reply) override;
    void onClose() override;

    void on_configure(const PersistenceConfig&);

    /**
     * We want to ensure distribution config changes are thread safe wrt. our
     * own state, so we make sure to get notified when these happen so we can
     * do explicit locked updates.
     */
    void storageDistributionChanged() override;

    const Metrics& getMetrics() const { return _metrics; }
};

}