summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/server/memory_flush_config_updater/memory_flush_config_updater_test.cpp
blob: ff64d6141691167b180e9db5f06793d461941274 (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
// 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/server/memory_flush_config_updater.h>

using namespace proton;
using vespa::config::search::core::ProtonConfig;

ProtonConfig::Flush::Memory
getConfig(int64_t maxMemory, int64_t eachMaxMemory, int64_t maxTlsSize,
          double conservativeMemoryLimitFactor = 0.5,
          double conservativeDiskLimitFactor = 0.6,
          double lowWatermarkFactor = 0.8)
{
    ProtonConfig::Flush::Memory result;
    result.maxmemory = maxMemory;
    result.each.maxmemory = eachMaxMemory;
    result.maxtlssize = maxTlsSize;
    result.conservative.memorylimitfactor = conservativeMemoryLimitFactor;
    result.conservative.disklimitfactor = conservativeDiskLimitFactor;
    result.conservative.lowwatermarkfactor = lowWatermarkFactor;
    return result;
}

ProtonConfig::Flush::Memory
getDefaultConfig()
{
    return getConfig(4, 1, 20);
}

ResourceUsageState
aboveLimit()
{
    return ResourceUsageState(0.7, 0.8);
}

ResourceUsageState
belowLimit()
{
    // This is still over default low watermark of 0.56 (0.7 * 0.8)
    return ResourceUsageState(0.7, 0.6);
}

const HwInfo::Memory defaultMemory(8ul * 1024ul * 1024ul * 1024ul);
constexpr size_t ONE_G = 1024ul * 1024ul * 1024ul;;

struct Fixture
{
    MemoryFlush::SP strategy;
    MemoryFlushConfigUpdater updater;
    Fixture()
        : strategy(std::make_shared<MemoryFlush>(MemoryFlushConfigUpdater::convertConfig(getDefaultConfig(), defaultMemory))),
          updater(strategy, getDefaultConfig(), defaultMemory)
    {}
    void assertStrategyConfig(uint64_t expMaxGlobalMemory, int64_t expMaxEachMemory, uint64_t expMaxGlobalTlsSize) {
        EXPECT_EQUAL(expMaxGlobalMemory, strategy->getConfig().maxGlobalMemory);
        EXPECT_EQUAL(expMaxEachMemory, strategy->getConfig().maxMemoryGain);
        EXPECT_EQUAL(expMaxGlobalTlsSize, strategy->getConfig().maxGlobalTlsSize);
    }
    void assertStrategyDiskConfig(double expGlobalDiskBloatFactor, double expDiskBloatFactor) {
        EXPECT_APPROX(expGlobalDiskBloatFactor, strategy->getConfig().globalDiskBloatFactor, 0.00001);
        EXPECT_APPROX(expDiskBloatFactor, strategy->getConfig().diskBloatFactor, 0.00001);
    }
    void notifyDiskMemUsage(const ResourceUsageState &diskState, const ResourceUsageState &memoryState) {
        updater.notifyDiskMemUsage(DiskMemUsageState(diskState, memoryState));
    }
    void setNodeRetired(bool nodeRetired) {
        updater.setNodeRetired(nodeRetired);
    }
};

TEST_F("require that strategy is updated when setting new config", Fixture)
{
    f.updater.setConfig(getConfig(6, 3, 30));
    TEST_DO(f.assertStrategyConfig(6, 3, 30));
}

TEST("require that we use configured memory limits") {
    auto cfg = MemoryFlushConfigUpdater::convertConfig(getConfig(6, 3, 30), defaultMemory);
    EXPECT_EQUAL(cfg.maxGlobalMemory, 6u);
    EXPECT_EQUAL(cfg.maxMemoryGain, 3);
}

TEST("require that we cap configured limits based on available memory") {
    const uint64_t LIMIT = defaultMemory.sizeBytes()/4;
    constexpr uint64_t MEM_4G = 4 * ONE_G;
    auto cfg = MemoryFlushConfigUpdater::convertConfig(getConfig(MEM_4G, MEM_4G, 30), defaultMemory);
    EXPECT_EQUAL(cfg.maxGlobalMemory, LIMIT);
    EXPECT_EQUAL(uint64_t(cfg.maxMemoryGain), LIMIT);
}

TEST("require that we cap configured limits based on the absolute 16G limit") {
    constexpr uint64_t LIMIT_16G = 16 * ONE_G;
    constexpr uint64_t MEM_64G = 64 * ONE_G;
    const HwInfo::Memory largeMemory(512 * ONE_G);
    auto cfg = MemoryFlushConfigUpdater::convertConfig(getConfig(MEM_64G, MEM_64G, 30), largeMemory);
    EXPECT_EQUAL(cfg.maxGlobalMemory, LIMIT_16G);
    EXPECT_EQUAL(uint64_t(cfg.maxMemoryGain), LIMIT_16G);
}

TEST_F("require that strategy is updated with normal values if no limits are reached", Fixture)
{
    f.updater.notifyDiskMemUsage(DiskMemUsageState());
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
}

TEST_F("require that strategy is updated with conservative max tls size value if disk limit is reached", Fixture)
{
    f.notifyDiskMemUsage(aboveLimit(), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 12));
}

TEST_F("require that strategy is updated with conservative max memory value if memory limit is reached", Fixture)
{
    f.notifyDiskMemUsage(belowLimit(), aboveLimit());
    TEST_DO(f.assertStrategyConfig(2, 0, 20));
}

TEST_F("require that strategy is updated with all conservative values if both limits are reached", Fixture)
{
    f.notifyDiskMemUsage(aboveLimit(), aboveLimit());
    TEST_DO(f.assertStrategyConfig(2, 0, 12));
}

TEST_F("require that last disk/memory usage state is remembered when setting new config", Fixture)
{
    f.notifyDiskMemUsage(aboveLimit(), belowLimit());
    f.updater.setConfig(getConfig(6, 3, 30));
    TEST_DO(f.assertStrategyConfig(6, 3, 18));
}

TEST_F("require that last config if remembered when setting new disk/memory usage state", Fixture)
{
    f.updater.setConfig(getConfig(6, 3, 30));
    f.notifyDiskMemUsage(aboveLimit(), belowLimit());
    TEST_DO(f.assertStrategyConfig(6, 3, 18));
}

TEST_F("require that we must go below low watermark for disk usage before using normal tls size value again", Fixture)
{
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.8), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 12));
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.7), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 12));
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.56), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 12));
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.55), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.6), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
}

TEST_F("require that we must go below low watermark for memory usage before using normal max memory value again", Fixture)
{
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.7, 0.8));
    TEST_DO(f.assertStrategyConfig(2, 0, 20));
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.7, 0.7));
    TEST_DO(f.assertStrategyConfig(2, 0, 20));
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.7, 0.56));
    TEST_DO(f.assertStrategyConfig(2, 0, 20));
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.7, 0.55));
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.7, 0.6));
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
}

TEST_F("require that more disk bloat is allowed while node state is retired", Fixture)
{
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.3), belowLimit());
    TEST_DO(f.assertStrategyDiskConfig(0.2, 0.2));
    f.setNodeRetired(true);
    TEST_DO(f.assertStrategyDiskConfig((0.8 - 0.3 / 0.7) * 0.8, 1.0));
    f.notifyDiskMemUsage(belowLimit(), belowLimit());
    TEST_DO(f.assertStrategyDiskConfig(0.2, 0.2));
}

TEST_MAIN() { TEST_RUN_ALL(); }