aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/bucketdb/bucketinfo.cpp
blob: 7ce64ced6f6a8b4a61caef8a03784f9b011e4f95 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "bucketinfo.h"
#include "bucketinfo.hpp"
#include <vespa/storage/storageutil/utils.h>
#include <algorithm>

namespace storage {

template class BucketInfoBase<std::vector<BucketCopy>>;
template class BucketInfoBase<vespalib::ConstArrayRef<BucketCopy>>;

BucketInfo::BucketInfo() noexcept : BucketInfoBase() {}

BucketInfo::BucketInfo(uint32_t lastGarbageCollection, std::vector<BucketCopy> nodes) noexcept
    : BucketInfoBase(lastGarbageCollection, std::move(nodes))
{}

BucketInfo::~BucketInfo() = default;

BucketInfo::BucketInfo(const BucketInfo&) = default;
BucketInfo& BucketInfo::operator=(const BucketInfo&) = default;
BucketInfo::BucketInfo(BucketInfo&&) noexcept = default;
BucketInfo& BucketInfo::operator=(BucketInfo&&) noexcept = default;

void
BucketInfo::updateTrusted() noexcept {
    if (validAndConsistent()) {
        for (uint32_t i = 0; i < _nodes.size(); i++) {
            _nodes[i].setTrusted();
        }
    }

    int trusted = -1;
    for (uint32_t i = 0; i < _nodes.size(); i++) {
        if (_nodes[i].trusted()) {
            trusted = i;
            break;
        }
    }

    if (trusted != -1) {
        for (uint32_t i = 0; i < _nodes.size(); i++) {
            if (_nodes[i].consistentWith(_nodes[trusted])) {
                _nodes[i].setTrusted();
            } else if (_nodes[i].trusted()) {
                resetTrusted();
                return;
            }
        }
    }
}

void
BucketInfo::resetTrusted() noexcept {
    for (uint32_t i = 0; i < _nodes.size(); i++) {
        _nodes[i].clearTrusted();
    }
    updateTrusted();
}

namespace {

struct Sorter {
    const std::vector<uint16_t>& _order;

    Sorter(const std::vector<uint16_t>& recommendedOrder) noexcept :
        _order(recommendedOrder) {}

    bool operator() (const BucketCopy& a, const BucketCopy& b) noexcept {
        int order_a = -1;
        for (uint32_t i = 0; i < _order.size(); i++) {
            if (_order[i] == a.getNode()) {
                order_a = i;
                break;
            }
        }
        int order_b = -1;
        for (uint32_t i = 0; i < _order.size(); i++) {
            if (_order[i] == b.getNode()) {
                order_b = i;
                break;
            }
        }

        if (order_b == -1 && order_a == -1) {
            return a.getNode() < b.getNode();
        }
        if (order_b == -1) {
            return true;
        }
        if (order_a == -1) {
            return false;
        }

        return order_a < order_b;
    }
};

}

void
BucketInfo::updateNode(const BucketCopy& newCopy)
{
    BucketCopy* found = getNodeInternal(newCopy.getNode());

    if (found) {
        *found = newCopy;
        updateTrusted();
    }
}

void
BucketInfo::addNodes(const std::vector<BucketCopy>& newCopies,
                     const std::vector<uint16_t>& recommendedOrder,
                     TrustedUpdate update)
{
    for (uint32_t i = 0; i < newCopies.size(); ++i) {
        BucketCopy* found = getNodeInternal(newCopies[i].getNode());

        if (found) {
            if (found->getTimestamp() < newCopies[i].getTimestamp()) {
                found->setBucketInfo(newCopies[i].getTimestamp(), newCopies[i].getBucketInfo());
            }
        } else {
            _nodes.push_back(newCopies[i]);
        }
    }

    std::sort(_nodes.begin(), _nodes.end(), Sorter(recommendedOrder));

    if (update == TrustedUpdate::UPDATE) {
        updateTrusted();
    }
}

void
BucketInfo::addNode(const BucketCopy& newCopy, const std::vector<uint16_t>& recommendedOrder)
{
    addNodes(toVector<BucketCopy>(newCopy), recommendedOrder);
}

bool
BucketInfo::removeNode(unsigned short node, TrustedUpdate update)
{
    for (auto iter = _nodes.begin(); iter != _nodes.end(); ++iter) {
        if (iter->getNode() == node) {
            _nodes.erase(iter);
            if (update == TrustedUpdate::UPDATE) {
                updateTrusted();
            }
            return true;
        }
    }
    return false;
}

BucketCopy*
BucketInfo::getNodeInternal(uint16_t node)
{
    for (BucketCopy & copy : _nodes) {
        if (copy.getNode() == node) {
            return &copy;
        }
    }
    return 0;
}

}