aboutsummaryrefslogtreecommitdiffstats
path: root/persistence/src/vespa/persistence/spi/clusterstate.cpp
blob: 7d106e93f57fab7ec5936ec88e4dde50fffc7b1e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "clusterstate.h"
#include "bucket.h"
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>

using vespalib::Trinary;

namespace storage::spi {

ClusterState::ClusterState(const lib::ClusterState& state,
                           uint16_t nodeIndex,
                           const lib::Distribution& distribution,
                           bool maintenanceInAllSpaces)
    : _state(std::make_unique<lib::ClusterState>(state)),
      _distribution(std::make_unique<lib::Distribution>(distribution.serialize())),
      _nodeIndex(nodeIndex),
      _maintenanceInAllSpaces(maintenanceInAllSpaces)
{
}

void ClusterState::deserialize(vespalib::nbostream& i) {
    vespalib::string clusterState;
    vespalib::string distribution;

    i >> clusterState;
    i >> _nodeIndex;
    i >> distribution;

    _state = std::make_unique<lib::ClusterState>(clusterState);
    _distribution = std::make_unique<lib::Distribution>(distribution);
}

ClusterState::ClusterState(const ClusterState& other) {
    vespalib::nbostream o;
    other.serialize(o);
    deserialize(o);
    _maintenanceInAllSpaces = other._maintenanceInAllSpaces;
}

ClusterState::~ClusterState() = default;

Trinary
ClusterState::shouldBeReady(const Bucket& b) const {
    assert(_distribution);
    assert(_state);

    if (b.getBucketId().getUsedBits() < _state->getDistributionBitCount()) {
        return Trinary::Undefined;
    }

    if (_distribution->getReadyCopies() >= _distribution->getRedundancy()) {
        return Trinary::True; // all copies should be ready
    }

    std::vector<uint16_t> idealNodes;
    _distribution->getIdealNodes(lib::NodeType::STORAGE, *_state,
                                 b.getBucketId(), idealNodes,
                                 "uim", _distribution->getReadyCopies());
    for (uint16_t node : idealNodes) {
        if (node == _nodeIndex) return Trinary::True;
    }
    return Trinary::False;
}

bool ClusterState::clusterUp() const noexcept {
    return _state && _state->getClusterState() == lib::State::UP;
}

bool ClusterState::nodeHasStateOneOf(const char* states) const noexcept {
    return _state &&
           _state->getNodeState(lib::Node(lib::NodeType::STORAGE, _nodeIndex)).
                   getState().oneOf(states);
}

bool ClusterState::nodeUp() const noexcept {
    return nodeHasStateOneOf("uir");
}

bool ClusterState::nodeInitializing() const noexcept {
    return nodeHasStateOneOf("i");
}

bool ClusterState::nodeRetired() const noexcept {
    return nodeHasStateOneOf("r");
}

bool ClusterState::nodeMaintenance() const noexcept {
    return _maintenanceInAllSpaces;
}

void ClusterState::serialize(vespalib::nbostream& o) const {
    assert(_distribution);
    assert(_state);
    vespalib::asciistream tmp;
    _state->serialize(tmp);
    o << tmp.str() << _nodeIndex;
    o << _distribution->serialize();
}

}