summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/server/disk_mem_usage_filter/disk_mem_usage_filter_test.cpp
blob: 4dd8c43d9b28a9f32c9244a122c9d923b3363d9b (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchcore/proton/common/hw_info.h>
#include <vespa/searchcore/proton/server/disk_mem_usage_filter.h>

using proton::DiskMemUsageFilter;
using proton::HwInfo;

namespace fs = std::experimental::filesystem;

namespace {

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

    Fixture()
        : _filter(HwInfo(HwInfo::Disk(100, false, false), HwInfo::Memory(64 * 1024 * 1024), HwInfo::Cpu(0)))
    {
        _filter.setDiskUsedSize(0);
        _filter.setMemoryStats(vespalib::ProcessMemoryStats(10000000,
                                                            10000001,
                                                            10000002,
                                                            10000003,
                                                            42));
    }

    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 triggerDiskLimit() {
        _filter.setDiskUsedSize(90);
    }

    void triggerMemoryLimit()
    {
        _filter.setMemoryStats(vespalib::ProcessMemoryStats(58720259,
                                                            58720258,
                                                            58720257,
                                                            58720256,
                                                            43));
    }
};

}

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

TEST_F("Check that stats are wired through", Fixture)
{
    EXPECT_EQUAL(42u, f._filter.getMemoryStats().getMappingsCount());
    f.triggerMemoryLimit();
    EXPECT_EQUAL(43u, f._filter.getMemoryStats().getMappingsCount());
}

TEST_F("Check that disk limit can be reached", Fixture)
{
    f._filter.setConfig(Fixture::Config(1.0, 0.8));
    f.triggerDiskLimit();
    f.testWrite("diskLimitReached: { "
                "action: \"add more content nodes\", "
                "reason: \"disk used (0.9) > disk limit (0.8)\", "
                "stats: { "
                "capacity: 100, used: 90, diskUsed: 0.9, diskLimit: 0.8}}");
}

TEST_F("Check that memory limit can be reached", Fixture)
{
    f._filter.setConfig(Fixture::Config(0.8, 1.0));
    f.triggerMemoryLimit();
    f.testWrite("memoryLimitReached: { "
                "action: \"add more content nodes\", "
                "reason: \"memory used (0.875) > memory limit (0.8)\", "
                "stats: { "
                "mapped: { virt: 58720259, rss: 58720258}, "
                "anonymous: { virt: 58720257, rss: 58720256}, "
                "physicalMemory: 67108864, memoryUsed: 0.875, memoryLimit: 0.8}}");
}

TEST_F("Check that both disk limit and memory limit can be reached", Fixture)
{
    f._filter.setConfig(Fixture::Config(0.8, 0.8));
    f.triggerMemoryLimit();
    f.triggerDiskLimit();
    f.testWrite("memoryLimitReached: { "
                "action: \"add more content nodes\", "
                "reason: \"memory used (0.875) > memory limit (0.8)\", "
                "stats: { "
                "mapped: { virt: 58720259, rss: 58720258}, "
                "anonymous: { virt: 58720257, rss: 58720256}, "
                "physicalMemory: 67108864, memoryUsed: 0.875, memoryLimit: 0.8}}, "
                "diskLimitReached: { "
                "action: \"add more content nodes\", "
                "reason: \"disk used (0.9) > disk limit (0.8)\", "
                "stats: { "
                "capacity: 100, used: 90, diskUsed: 0.9, diskLimit: 0.8}}");
}

TEST_MAIN() { TEST_RUN_ALL(); }