aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/common/testnodestateupdater.cpp
blob: 5d1d7a085b9ba9b6dc6675084519fe8737c9aa20 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "testnodestateupdater.h"
#include <vespa/vdslib/state/cluster_state_bundle.h>
#include <vespa/vdslib/state/clusterstate.h>

namespace storage {

TestNodeStateUpdater::TestNodeStateUpdater(const lib::NodeType& type)
    : _mutex(),
      _reported(new lib::NodeState(type, lib::State::UP)),
      _current(new lib::NodeState(type, lib::State::UP)),
      _clusterStateBundle(std::make_shared<const lib::ClusterStateBundle>(lib::ClusterState())),
      _listeners(),
      _explicit_node_state_reply_send_invocations(0),
      _requested_almost_immediate_node_state_replies(0)
{ }

TestNodeStateUpdater::~TestNodeStateUpdater() = default;

std::shared_ptr<const lib::ClusterStateBundle>
TestNodeStateUpdater::getClusterStateBundle() const
{
    return _clusterStateBundle;
}

void
TestNodeStateUpdater::patch_distribution(std::shared_ptr<const lib::Distribution> distribution)
{
    _clusterStateBundle = _clusterStateBundle->clone_with_new_distribution(
            lib::DistributionConfigBundle::of(std::move(distribution)));
}

void
TestNodeStateUpdater::setClusterState(std::shared_ptr<const lib::ClusterState> c)
{
    setClusterStateBundle(std::make_shared<const lib::ClusterStateBundle>(std::move(c)));
}

void
TestNodeStateUpdater::setClusterStateBundle(std::shared_ptr<const lib::ClusterStateBundle> clusterStateBundle)
{
    auto existing_distr = _clusterStateBundle->distribution_config_bundle();
    _clusterStateBundle = std::move(clusterStateBundle);
    if (!_clusterStateBundle->has_distribution_config() && existing_distr) {
        _clusterStateBundle = _clusterStateBundle->clone_with_new_distribution(existing_distr);
    }
    for (auto* listener : _listeners) {
        listener->handleNewState();
    }
}

}