aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/server/memory_flush_config_updater/memory_flush_config_updater_test.cpp
blob: 4b2ad4b005c1f1b6880bcd6e5f86ebd24df6a941 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright Vespa.ai. 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>
#include <vespa/vespalib/util/size_literals.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 high_watermark_factor = 0.9,
          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.highwatermarkfactor = high_watermark_factor;
    result.conservative.lowwatermarkfactor = lowWatermarkFactor;
    return result;
}

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

ResourceUsageState
aboveLimit()
{
    // The high watermark limit is 0.63 (0.7 * 0.9 (factor)).
    return ResourceUsageState(0.7, 0.64);
}

ResourceUsageState
belowLimit()
{
    // The high watermark limit is 0.63 (0.7 * 0.9 (factor)).
    // This is still over the low watermark limit of 0.56 (0.7 * 0.8 (factor)).
    return ResourceUsageState(0.7, 0.62);
}

const HwInfo::Memory defaultMemory(8_Gi);

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));
}

void
expectEqual(const MemoryFlush::Config & a, const MemoryFlush::Config & b) {
    EXPECT_TRUE(a.equal(b));
    EXPECT_TRUE(a == b);
    EXPECT_FALSE( a != b);
    EXPECT_TRUE(b.equal(a));
    EXPECT_TRUE(b == a);
    EXPECT_FALSE( b != a);
}

void
expectNotEqual(const MemoryFlush::Config & a, const MemoryFlush::Config & b) {
    EXPECT_FALSE(a.equal(b));
    EXPECT_FALSE(a == b);
    EXPECT_TRUE( a != b);
    EXPECT_FALSE(b.equal(a));
    EXPECT_FALSE(b == a);
    EXPECT_TRUE( b != a);
}

TEST("require that MemoryFlush::Config equal is correct") {
    MemoryFlush::Config a, b;
    expectEqual(a, b);
    a.maxGlobalMemory = 7;
    expectNotEqual(a, b);
    b.maxGlobalMemory = 7;
    expectEqual(a, b);
    a.maxMemoryGain = 8;
    expectNotEqual(a, b);
    b.maxMemoryGain = 8;
    expectEqual(a, b);
    a.maxGlobalTlsSize = 9;
    expectNotEqual(a, b);
    b.maxGlobalTlsSize = 9;
    expectEqual(a, b);
    a.maxTimeGain = 10us;
    expectNotEqual(a, b);
    b.maxTimeGain = 10us;
    expectEqual(a, b);
    a.globalDiskBloatFactor = 11;
    expectNotEqual(a, b);
    b.globalDiskBloatFactor = 11;
    expectEqual(a, b);
    a.diskBloatFactor = 12;
    expectNotEqual(a, b);
    b.diskBloatFactor = 12;
    expectEqual(a, b);
}

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;
    auto cfg = MemoryFlushConfigUpdater::convertConfig(getConfig(4_Gi, 4_Gi, 30), defaultMemory);
    EXPECT_EQUAL(cfg.maxGlobalMemory, LIMIT);
    EXPECT_EQUAL(uint64_t(cfg.maxMemoryGain), LIMIT);
}

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("Use conservative settings when above high watermark for disk usage", Fixture)
{
    // The high watermark limit is 0.63 (0.7 * 0.9 (factor)).
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.62), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.64), belowLimit());
    TEST_DO(f.assertStrategyConfig(4, 1, 12));
}

TEST_F("Use conservative settings when above high watermark for memory usage", Fixture)
{
    // The high watermark limit is 0.54 (0.6 * 0.9 (factor)).
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.6, 0.53));
    TEST_DO(f.assertStrategyConfig(4, 1, 20));
    f.notifyDiskMemUsage(belowLimit(), ResourceUsageState(0.6, 0.55));
    TEST_DO(f.assertStrategyConfig(2, 0, 20));
}

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)
{
    constexpr double DEFAULT_DISK_BLOAT = 0.25;
    f.notifyDiskMemUsage(ResourceUsageState(0.7, 0.3), belowLimit());
    TEST_DO(f.assertStrategyDiskConfig(DEFAULT_DISK_BLOAT, DEFAULT_DISK_BLOAT));
    f.setNodeRetired(true);
    TEST_DO(f.assertStrategyDiskConfig((0.8 - ((0.3/0.7)*(1 - DEFAULT_DISK_BLOAT))) / 0.8, 1.0));
    f.notifyDiskMemUsage(belowLimit(), belowLimit());
    TEST_DO(f.assertStrategyDiskConfig(DEFAULT_DISK_BLOAT, DEFAULT_DISK_BLOAT));
}

TEST_MAIN() { TEST_RUN_ALL(); }