aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/distribution/group.cpp
blob: 254a20e105280015806ffe1044b9548ba2026327 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "group.h"

#include <vespa/vdslib/state/random.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <ostream>
#include <cassert>
#include <algorithm>

namespace storage::lib {

Group::Group(uint16_t index, vespalib::stringref name) noexcept
    : _name(name),
      _index(index),
      _distributionHash(0),
      _capacity(1.0),
      _subGroups(),
      _nodes()
{
}

Group::Group(uint16_t index, vespalib::stringref name,
             const Distribution& d, uint16_t redundancy)
    : _name(name),
      _index(index),
      _distributionHash(0),
      _distributionSpec(d),
      _preCalculated(redundancy + 1),
      _capacity(1.0),
      _subGroups(),
      _nodes()
{
    for (uint32_t i=0; i<_preCalculated.size(); ++i) {
        _preCalculated[i] = Distribution(d, i);
    }
}

Group::~Group()
{
    for (auto & subGroup : _subGroups) {
        delete subGroup.second;
        subGroup.second = nullptr;
    }
}

bool
Group::operator==(const Group& other) const noexcept
{
    return (_name == other._name &&
            _index == other._index &&
            _distributionSpec == other._distributionSpec &&
            _preCalculated.size() == other._preCalculated.size() &&
            _capacity == other._capacity &&
            _subGroups == other._subGroups &&
            _nodes == other._nodes);
}

void
Group::print(std::ostream& out, bool verbose,
             const std::string& indent) const {
    out << "Group(";
    if (!_name.empty()) {
        out << "name: " << _name << ", ";
    }
    out << "index: " << _index;
    if (_distributionSpec.size() > 0) {
        out << ", distribution: " << _distributionSpec;
    }
    if (_capacity != 1.0) {
        out << ", capacity: " << _capacity;
    }
    if (_distributionSpec.size() == 0) {
        out << ", nodes( ";
        for (auto node : _nodes) {
            out << node << " ";
        }
        out << ")";
    }

    if (_subGroups.size()>0) {
        out << ", subgroups: " << _subGroups.size();
    }

    out << ") {";

    if (_subGroups.size()>0) {
        for (const auto & subGroup : _subGroups) {
            out  << "\n" << indent << "  ";
            subGroup.second->print(out, verbose, indent + "  ");
        }
    }

    out << "\n" << indent << "}";
}

void
Group::addSubGroup(Group::UP group)
{
    if (_distributionSpec.size() == 0) {
        throw vespalib::IllegalStateException(
                "Cannot add sub groups to a group without a valid distribution",
                VESPA_STRLOC);
    }
    if (!group) {
        throw vespalib::IllegalArgumentException(
                "Cannot add null group.", VESPA_STRLOC);
    }
    auto it =_subGroups.find(group->getIndex());
    if (it != _subGroups.end()) {
        throw vespalib::IllegalArgumentException(
                "Another subgroup with same index is already added.",
                VESPA_STRLOC);
    }
    auto index = group->getIndex();
    _subGroups[index] = group.release();
}

void
Group::setCapacity(vespalib::Double capacity)
{
    if (capacity <= 0) {
        vespalib::asciistream ost;
        ost << "Illegal capacity '" << capacity << "'. Capacity "
            "must be a positive floating point number";
        throw vespalib::IllegalArgumentException(ost.str(), VESPA_STRLOC);
    }
    _capacity = capacity;
}

void
Group::setNodes(const std::vector<uint16_t>& nodes)
{
    assert(_distributionSpec.size() == 0);
    _originalNodes = nodes;
    _nodes = nodes;
    // Maintain ordering invariant. Required to ensure node score computations
    // finish in linear time. Failure to maintain invariant may result in
    // quadratic worst case behavior.
    std::sort(_nodes.begin(), _nodes.end());
}

const Group*
Group::getGroupForNode(uint16_t nodeIdx) const
{
    for (auto node : _nodes) {
        if (node == nodeIdx) {
            return this;
        }
    }

    for (const auto & subGroup : _subGroups) {
        const Group* g = subGroup.second->getGroupForNode(nodeIdx);
        if (g != nullptr) {
            return g;
        }
    }

    return nullptr;
}

void
Group::calculateDistributionHashValues(uint32_t parentHash)
{
    _distributionHash = parentHash ^ (1664525L * _index + 1013904223L);
    for (const auto & subGroup : _subGroups) {
        subGroup.second->calculateDistributionHashValues(_distributionHash);
    }
}

void
Group::getConfigHash(vespalib::asciistream& out) const
{
    out << '(' << _index;
    if (_capacity != 1.0) {
        out << 'c' << _capacity;
    }
    if (isLeafGroup()) {
        for (uint16_t node : _originalNodes) {
            out << ';' << node;
        }
    } else {
        out << 'd' << _distributionSpec.toString();
        for (const auto& subGroup : _subGroups) {
            subGroup.second->getConfigHash(out);
        }
    }
    out << ')';
}

vespalib::string
Group::getDistributionConfigHash() const {
    vespalib::asciistream ost;
    getConfigHash(ost);
    return ost.str();
}

}