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

#include "bucketgctimecalculator.h"
#include "ideal_service_layer_nodes_bundle.h"
#include <vespa/storage/distributor/maintenance/maintenancepriority.h>
#include <vespa/storage/distributor/operations/idealstate/idealstateoperation.h>
#include <vespa/storage/common/storagecomponent.h>
#include <vespa/storage/bucketdb/bucketdatabase.h>
#include <vespa/vespalib/stllike/hash_set.h>
#include <map>
#include <set>

namespace storage::lib {
    class ClusterState;
}
namespace storage { class DistributorConfiguration; }

namespace storage::distributor {

class DistributorBucketSpace;
class DistributorNodeContext;
class DistributorStripeOperationContext;
class NodeMaintenanceStatsTracker;

/**
 * This class is used by IdealStateManager to generate ideal state operations.
 * Every time IdealStateManager wants to verify that a bucket is in its ideal
 * state, it calls a list of StateCheckers' generateOperations() methods.
 * This generates a list of operations to run.
 *
 * Each statechecker also keeps a queue of operations that have been previously
 * generated. IdealStateManager adds to this queue, and also calls
 * startOperations() to fetch operations to perform.
 *
 * The statechecker can also be used to generate metrics on what needs to be
 * done to reach the ideal state - using the generateMetrics() method.
 */
class StateChecker {
public:
    using SP = std::shared_ptr<StateChecker>;

    /**
     * Context object used when generating operations and metrics for a
     * bucket.
     */
    class Context
    {
    public:
        Context(const DistributorNodeContext& node_ctx_in,
                const DistributorStripeOperationContext& op_ctx_in,
                const DistributorBucketSpace &distributorBucketSpace,
                NodeMaintenanceStatsTracker&,
                const document::Bucket& bucket_);
        ~Context();
        Context(const Context&) = delete;
        Context& operator=(const Context&) = delete;


        // Per bucket
        document::Bucket                   bucket;
        document::BucketId                 siblingBucket;
        BucketDatabase::Entry              siblingEntry;
        std::vector<BucketDatabase::Entry> entries;

        // Common
        const lib::ClusterState                 & systemState;
        const lib::ClusterState                 * pending_cluster_state; // nullptr if no state is pending.
        const DistributorConfiguration          & distributorConfig;
        const lib::Distribution                 & distribution;
        BucketGcTimeCalculator                    gcTimeCalculator;
        const IdealServiceLayerNodesBundle      & idealStateBundle;
        const DistributorNodeContext            & node_ctx;
        const DistributorStripeOperationContext & op_ctx;
        const BucketDatabase                    & db;
        NodeMaintenanceStatsTracker             & stats;
        const bool                                merges_inhibited_in_bucket_space;

        const BucketDatabase::Entry& getSiblingEntry() const noexcept { return siblingEntry; }
        IdealServiceLayerNodesBundle::ConstNodesRef idealState() const noexcept {
            return idealStateBundle.available_nonretired_or_maintenance_nodes();
        }

        document::Bucket getBucket() const noexcept { return bucket; }
        document::BucketId getBucketId() const noexcept { return bucket.getBucketId(); }
        document::BucketSpace getBucketSpace() const noexcept { return bucket.getBucketSpace(); }

        std::string toString() const;
        void fillParentAndChildBuckets();
        void fillSiblingBucket();
        const BucketDatabase::Entry* getEntryForPrimaryBucket() const;
        const BucketDatabase::Entry & entry() const noexcept { return _entry; }

        void set_entry(const BucketDatabase::Entry & e) { _entry = e; }
    private:
        BucketDatabase::Entry  _entry;
    };

    class ResultImpl
    {
    public:
        virtual ~ResultImpl() = default;
        virtual IdealStateOperation::UP createOperation() = 0;
        virtual MaintenancePriority getPriority() const = 0;
        virtual MaintenanceOperation::Type getType() const = 0;
    };

    class Result
    {
        std::unique_ptr<ResultImpl> _impl;
    public:
        IdealStateOperation::UP createOperation() {
            return (_impl ? _impl->createOperation() : IdealStateOperation::UP());
        }

        MaintenancePriority getPriority() const {
            return (_impl ? _impl->getPriority() : MaintenancePriority());
        }

        MaintenanceOperation::Type getType() const {
            return (_impl ? _impl->getType() : MaintenanceOperation::OPERATION_COUNT);
        }

        static Result noMaintenanceNeeded();
        static Result createStoredResult(IdealStateOperation::UP operation, MaintenancePriority::Priority priority);
    private:
        explicit Result(std::unique_ptr<ResultImpl> impl)
            : _impl(std::move(impl))
        {}
    };

    StateChecker() noexcept = default;
    virtual ~StateChecker() = default;

    /**
     * Calculates if operations need to be scheduled to rectify any issues
     * this state checker is checking for.
     *
     * @return Returns an operation to perform for the given bucket.
     */
    virtual Result check(const Context &c) const = 0;

    /**
     * Returns the name of this state checker.
     */
    virtual const char* getName() const noexcept = 0;
};

}