aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/nodeinfo.cpp
blob: 3e645f57393b679eb634a8dfd92dbc39352a8209 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "nodeinfo.h"
#include <vespa/storageframework/generic/clock/clock.h>

namespace storage::distributor {

NodeInfo::NodeInfo(const framework::Clock& clock) noexcept
    : _clock(clock) {}

uint32_t
NodeInfo::getPendingCount(uint16_t idx) const {
    return getNode(idx)._pending;
}

bool
NodeInfo::isBusy(uint16_t idx) const {
    const SingleNodeInfo& info = getNode(idx);
    if (info._busyUntilTime.time_since_epoch().count() != 0) {
        if (_clock.getMonotonicTime() > info._busyUntilTime) {
            info._busyUntilTime = vespalib::steady_time();
        } else {
            return true;
        }
    }

    return false;
}

void
NodeInfo::setBusy(uint16_t idx, vespalib::duration for_duration) {
    getNode(idx)._busyUntilTime = _clock.getMonotonicTime() + for_duration;
}

void
NodeInfo::incPending(uint16_t idx) {
    getNode(idx)._pending++;
}

void
NodeInfo::decPending(uint16_t idx) {
    SingleNodeInfo& info = getNode(idx);

    if (info._pending > 0) {
        getNode(idx)._pending--;
    }
}

void
NodeInfo::clearPending(uint16_t idx) {
    SingleNodeInfo& info = getNode(idx);
    info._pending = 0;
}

NodeInfo::SingleNodeInfo&
NodeInfo::getNode(uint16_t idx) {
    const auto index_lbound = static_cast<size_t>(idx) + 1;
    while (_nodes.size() < index_lbound) {
        _nodes.emplace_back();
    }

    return _nodes[idx];
}

const NodeInfo::SingleNodeInfo&
NodeInfo::getNode(uint16_t idx) const {
    const auto index_lbound = static_cast<size_t>(idx) + 1;
    while (_nodes.size() < index_lbound) {
        _nodes.emplace_back();
    }

    return _nodes[idx];
}

}