aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/attribute_usage_filter.cpp
blob: 494a47fe65a957d6c6b7fdf6daf55a61f731759e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "attribute_usage_filter.h"
#include "i_attribute_usage_listener.h"
#include <sstream>

namespace proton {

namespace {

void makeAddressSpaceMessage(std::ostream &os,
                              const AddressSpaceUsageStats &usage)
{
    os << "{ used: " <<
        usage.getUsage().used() << ", dead: " <<
        usage.getUsage().dead() << ", limit: " <<
        usage.getUsage().limit() << "}, " <<
        "attributeName: \"" << usage.getAttributeName() << "\", " <<
        "componentName: \"" << usage.get_component_name() << "\", " <<
        "subdb: \"" << usage.getSubDbName() << "\"}";
}

void make_error_message(std::ostream &os,
                        double used, double limit,
                        const AddressSpaceUsageStats &usage)
{
    os << "addressSpaceLimitReached: { "
        "action: \""
        "add more content nodes"
        "\", "
        "reason: \""
        "max address space in attribute vector components used (" << used << ") > "
        "limit (" << limit << ")"
        "\", addressSpace: ";
    makeAddressSpaceMessage(os, usage);
}

}

void
AttributeUsageFilter::recalcState(const Guard &guard)
{
    (void) guard;
    bool hasMessage = false;
    std::ostringstream message;
    const auto &max_usage = _attributeStats.max_address_space_usage();
    double used = max_usage.getUsage().usage();
    if (used > _config._address_space_limit) {
        hasMessage = true;
        make_error_message(message, used, _config._address_space_limit, max_usage);
    }
    if (hasMessage) {
        _state = State(false, message.str());
        _acceptWrite = false;
    } else {
        _state = State();
        _acceptWrite = true;
    }
}

AttributeUsageFilter::AttributeUsageFilter()
    : _lock(),
      _attributeStats(),
      _config(),
      _state(),
      _acceptWrite(true),
      _listener()
{
}

AttributeUsageFilter::~AttributeUsageFilter() = default;

void
AttributeUsageFilter::setAttributeStats(AttributeUsageStats attributeStats_in)
{
    Guard guard(_lock);
    _attributeStats = attributeStats_in;
    recalcState(guard);
    if (_listener) {
        _listener->notify_attribute_usage(_attributeStats);
    }
}

AttributeUsageStats
AttributeUsageFilter::getAttributeUsageStats() const
{
    Guard guard(_lock);
    return _attributeStats;
}

void
AttributeUsageFilter::setConfig(Config config_in)
{
    Guard guard(_lock);
    _config = config_in;
    recalcState(guard);
}

void
AttributeUsageFilter::set_listener(std::unique_ptr<IAttributeUsageListener> listener)
{
    Guard guard(_lock);
    _listener = std::move(listener);
}

bool
AttributeUsageFilter::acceptWriteOperation() const
{
    return _acceptWrite;
}

AttributeUsageFilter::State
AttributeUsageFilter::getAcceptState() const
{
    Guard guard(_lock);
    return _state;
}

} // namespace proton