aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/distribution/idealnodecalculator.h
blob: 4eb8f7e04ae575c41585c110111445621cc4c7ba (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * An interface to implement for a calculator calcuting ideal state. It should
 * be easy to wrap this calculator in a cache. Thus options that seldom change,
 * are taken in as set parameters, such that existing cache can be invalidated.
 */
#pragma once

#include <vespa/document/bucket/bucketid.h>
#include <vespa/document/util/printable.h>
#include <vespa/vdslib/state/node.h>
#include <vector>
#include <memory>

namespace storage::lib {

class Distribution;
class ClusterState;

/**
 * A list of ideal nodes, sorted in preferred order. Wraps a vector to hide
 * unneeded details, and make it easily printable.
 */
class IdealNodeList : public document::Printable {
public:
    IdealNodeList() noexcept;
    ~IdealNodeList();

    void push_back(const Node& node) {
        _idealNodes.push_back(node);
    }

    const Node& operator[](uint32_t i) const noexcept { return _idealNodes[i]; }
    uint32_t size() const noexcept { return _idealNodes.size(); }
    bool contains(const Node& n) const noexcept {
        return indexOf(n) != 0xffff;
    }
    uint16_t indexOf(const Node& n) const noexcept {
        for (uint16_t i=0; i<_idealNodes.size(); ++i) {
            if (n == _idealNodes[i]) return i;
        }
        return 0xffff;
    }

    void print(std::ostream& out, bool, const std::string &) const override;
private:
    std::vector<Node> _idealNodes;
};

/**
 * Simple interface to use for those who needs to calculate ideal nodes.
 */
class IdealNodeCalculator {
public:
    using SP = std::shared_ptr<IdealNodeCalculator>;
    enum UpStates {
        UpInit,
        UpInitMaintenance,
        UP_STATE_COUNT
    };

    virtual ~IdealNodeCalculator() = default;

    virtual IdealNodeList getIdealNodes(const NodeType&, const document::BucketId&, UpStates upStates = UpInit) const = 0;

    // Wrapper functions to make prettier call if nodetype is given.
    IdealNodeList getIdealDistributorNodes(const document::BucketId& bucket, UpStates upStates = UpInit) const {
        return getIdealNodes(NodeType::DISTRIBUTOR, bucket, upStates);
    }
    IdealNodeList getIdealStorageNodes(const document::BucketId& bucket, UpStates upStates = UpInit) const {
        return getIdealNodes(NodeType::STORAGE, bucket, upStates);
    }
};


/**
 * More complex interface that provides a way to alter needed settings not
 * provided in the function call itself.
 */
class IdealNodeCalculatorConfigurable : public IdealNodeCalculator
{
public:
    using SP = std::shared_ptr<IdealNodeCalculatorConfigurable>;

    virtual void setDistribution(const Distribution&) = 0;
    virtual void setClusterState(const ClusterState&) = 0;
};

}