summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/attribute/attribute_usage_filter/attribute_usage_filter_test.cpp
blob: 20497f262cb38bbda0eb25bf2a7bd845b2b67c78 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("attribute_usage_filter_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchcore/proton/attribute/attribute_usage_filter.h>

using proton::AttributeUsageFilter;
using proton::AttributeUsageStats;

namespace
{

search::AddressSpace enumStoreOverLoad(30 * 1024 * 1024 * UINT64_C(1024),
                                       0,
                                       32 * 1024 * 1024 * UINT64_C(1024));

search::AddressSpace multiValueOverLoad(127 * 1024 * 1024,
                                        0,
                                        128 * 1024 * 1024);



class MyAttributeStats : public AttributeUsageStats
{
public:
    void triggerEnumStoreLimit() {
        merge({        enumStoreOverLoad,
                       search::AddressSpaceUsage::defaultMultiValueUsage() },
              "enumeratedName",
              "ready");
    }

    void triggerMultiValueLimit() {
        merge({         search::AddressSpaceUsage::defaultEnumStoreUsage(),
                       multiValueOverLoad },
              "multiValueName",
              "ready");
    }
};

struct Fixture
{
    AttributeUsageFilter _filter;
    using State = AttributeUsageFilter::State;
    using Config = AttributeUsageFilter::Config;

    Fixture()
        : _filter()
    {
    }

    void testWrite(const vespalib::string &exp) {
        if (exp.empty()) {
            EXPECT_TRUE(_filter.acceptWriteOperation());
            State state = _filter.getAcceptState();
            EXPECT_TRUE(state.acceptWriteOperation());
            EXPECT_EQUAL(exp, state.message());
        } else {
            EXPECT_FALSE(_filter.acceptWriteOperation());
            State state = _filter.getAcceptState();
            EXPECT_FALSE(state.acceptWriteOperation());
            EXPECT_EQUAL(exp, state.message());
        }
    }

    void setAttributeStats(const AttributeUsageStats &stats) {
        _filter.setAttributeStats(stats);
    }
};

}

TEST_F("Check that default filter allows write", Fixture)
{
    f.testWrite("");
}


TEST_F("Check that enum store limit can be reached", Fixture)
{
    f._filter.setConfig(Fixture::Config(0.8, 1.0));
    MyAttributeStats stats;
    stats.triggerEnumStoreLimit();
    f.setAttributeStats(stats);
    f.testWrite("enumStoreLimitReached: { "
                "action: \""
                "add more content nodes"
                "\", "
                "reason: \""
                "enum store address space used (0.9375) > limit (0.8)"
                "\", "
                "enumStore: { used: 32212254720, dead: 0, limit: 34359738368}, "
                "attributeName: \"enumeratedName\", subdb: \"ready\"}");
}

TEST_F("Check that multivalue limit can be reached", Fixture)
{
    f._filter.setConfig(Fixture::Config(1.0, 0.8));
    MyAttributeStats stats;
    stats.triggerMultiValueLimit();
    f.setAttributeStats(stats);
    f.testWrite("multiValueLimitReached: { "
                "action: \""
                "add more content nodes"
                "\", "
                "reason: \""
                "multiValue address space used (0.992188) > limit (0.8)"
                "\", "
                "multiValue: { used: 133169152, dead: 0, limit: 134217728}, "
                "attributeName: \"multiValueName\", subdb: \"ready\"}");
}

TEST_F("Check that both enumstore limit and multivalue limit can be reached",
       Fixture)
{
    f._filter.setConfig(Fixture::Config(0.8, 0.8));
    MyAttributeStats stats;
    stats.triggerEnumStoreLimit();
    stats.triggerMultiValueLimit();
    f.setAttributeStats(stats);
    f.testWrite("enumStoreLimitReached: { "
                "action: \""
                "add more content nodes"
                "\", "
                "reason: \""
                "enum store address space used (0.9375) > limit (0.8)"
                "\", "
                "enumStore: { used: 32212254720, dead: 0, limit: 34359738368}, "
                "attributeName: \"enumeratedName\", subdb: \"ready\"}"
                ", "
                "multiValueLimitReached: { "
                "action: \""
                "add more content nodes"
                "\", "
                "reason: \""
                "multiValue address space used (0.992188) > limit (0.8)"
                "\", "
                "multiValue: { used: 133169152, dead: 0, limit: 134217728}, "
                "attributeName: \"multiValueName\", subdb: \"ready\"}");
}

TEST_MAIN() { TEST_RUN_ALL(); }