aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/distribution/idealnodecalculatorimpl.cpp
blob: 86123f47d6f111109887250efd27cfd86844ca2c (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "idealnodecalculatorimpl.h"
#include "distribution.h"
#include <vespa/vespalib/util/exceptions.h>
#include <ostream>
#include <cassert>

namespace storage::lib {

IdealNodeList::IdealNodeList() noexcept = default;
IdealNodeList::~IdealNodeList() = default;

void
IdealNodeList::print(std::ostream& out, bool , const std::string &) const
{
    out << "[";
    for (uint32_t i=0; i<_idealNodes.size(); ++i) {
        if (i != 0) out << ", ";
        out << _idealNodes[i];
    }
    out << "]";
}

IdealNodeCalculatorImpl::IdealNodeCalculatorImpl()
    : _distribution(0),
      _clusterState(0)
{
    initUpStateMapping();
}

IdealNodeCalculatorImpl::~IdealNodeCalculatorImpl() = default;

void
IdealNodeCalculatorImpl::setDistribution(const Distribution& d) {
    _distribution = &d;
}
void
IdealNodeCalculatorImpl::setClusterState(const ClusterState& cs) {
    _clusterState = &cs;
}

IdealNodeList
IdealNodeCalculatorImpl::getIdealNodes(const NodeType& nodeType,
                                       const document::BucketId& bucket,
                                       UpStates upStates) const
{
    assert(_clusterState != 0);
    assert(_distribution != 0);
    std::vector<uint16_t> nodes;
    _distribution->getIdealNodes(nodeType, *_clusterState, bucket, nodes, _upStates[upStates]);
    IdealNodeList list;
    for (uint32_t i=0; i<nodes.size(); ++i) {
        list.push_back(Node(nodeType, nodes[i]));
    }
    return list;
}

void
IdealNodeCalculatorImpl::initUpStateMapping() {
    _upStates.clear();
    _upStates.resize(UP_STATE_COUNT);
    _upStates[UpInit] = "ui";
    _upStates[UpInitMaintenance] = "uim";
    for (uint32_t i=0; i<_upStates.size(); ++i) {
        if (_upStates[i] == 0) {
            throw vespalib::IllegalStateException("Failed to initialize up state. Code likely not updated "
                                                  "after another upstate was added.", VESPA_STRLOC);
        }
    }
}

}