aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/server/memoryflush/memoryflush_test.cpp
blob: 8c017184a7b552b89149b68fa7cb4d69b2f5bdd3 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcore/proton/flushengine/active_flush_stats.h>
#include <vespa/searchcore/proton/flushengine/flushcontext.h>
#include <vespa/searchcore/proton/flushengine/tls_stats_map.h>
#include <vespa/searchcore/proton/server/memoryflush.h>
#include <vespa/searchcore/proton/test/dummy_flush_target.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/size_literals.h>

using vespalib::system_time;
using search::SerialNum;
using namespace proton;
using namespace searchcorespi;

using MemoryGain = IFlushTarget::MemoryGain;
using DiskGain = IFlushTarget::DiskGain;

class MyFlushHandler : public IFlushHandler {
public:
    MyFlushHandler(const vespalib::string &name) noexcept : IFlushHandler(name) {}
    std::vector<IFlushTarget::SP> getFlushTargets() override {
        return std::vector<IFlushTarget::SP>();
    }
    SerialNum getCurrentSerialNumber() const override { return 0; }
    void flushDone(SerialNum oldestSerial) override { (void) oldestSerial; }
    void syncTls(search::SerialNum syncTo) override {(void) syncTo;}
};

class MyFlushTarget : public test::DummyFlushTarget {
private:
    MemoryGain    _memoryGain;
    DiskGain      _diskGain;
    SerialNum     _flushedSerial;
    system_time  _lastFlushTime;
    bool          _urgentFlush;
public:
    MyFlushTarget(const vespalib::string &name, MemoryGain memoryGain,
                  DiskGain diskGain, SerialNum flushedSerial,
                  system_time lastFlushTime, bool urgentFlush) noexcept :
        test::DummyFlushTarget(name),
        _memoryGain(memoryGain),
        _diskGain(diskGain),
        _flushedSerial(flushedSerial),
        _lastFlushTime(lastFlushTime),
        _urgentFlush(urgentFlush)
    {
    }
    MemoryGain getApproxMemoryGain() const override { return _memoryGain; }
    DiskGain getApproxDiskGain() const override { return _diskGain; }
    SerialNum getFlushedSerialNum() const override { return _flushedSerial; }
    system_time getLastFlushTime() const override { return _lastFlushTime; }
    bool needUrgentFlush() const override { return _urgentFlush; }
};

using StringList = std::vector<vespalib::string>;

class ContextBuilder {
private:
    FlushContext::List _list;
    IFlushHandler::SP  _handler;
    flushengine::TlsStatsMap::Map _map;
    flushengine::ActiveFlushStats _active_flushes;

    void
    fixupMap(const vespalib::string &name, SerialNum lastSerial)
    {
        flushengine::TlsStats oldStats = _map[name];
        if (oldStats.getLastSerial() < lastSerial) {
            _map[name] =
                flushengine::TlsStats(oldStats.getNumBytes(),
                                      oldStats.getFirstSerial(),
                                      lastSerial);
        }
    }
public:
    ContextBuilder();
    ~ContextBuilder();
    void addTls(const vespalib::string &name,
                const flushengine::TlsStats &tlsStats) {
        _map[name] = tlsStats;
    }
    ContextBuilder &add(const FlushContext::SP &context) {
        _list.push_back(context);
        fixupMap(_handler->getName(), context->getLastSerial());
        return *this;
    }
    ContextBuilder &add(const IFlushTarget::SP &target, SerialNum lastSerial = 0) {
        FlushContext::SP ctx(new FlushContext(_handler, target, lastSerial));
        return add(ctx);
    }
    ContextBuilder& active_flush(const vespalib::string& handler_name, vespalib::system_time start_time) {
        _active_flushes.set_start_time(handler_name, start_time);
        return *this;
    }
    const FlushContext::List &list() const { return _list; }
    flushengine::TlsStatsMap tlsStats() const {
        flushengine::TlsStatsMap::Map map(_map);
        return flushengine::TlsStatsMap(std::move(map));
    }
    FlushContext::List flush_targets(const IFlushStrategy& strategy) const {
        return strategy.getFlushTargets(list(), tlsStats(), _active_flushes);
    }
};

using minutes = std::chrono::minutes;
using seconds = std::chrono::seconds;

ContextBuilder::ContextBuilder()
    : _list(), _handler(new MyFlushHandler("myhandler")), _map(), _active_flushes()
{}
ContextBuilder::~ContextBuilder() = default;

MyFlushTarget::SP
createTargetM(const vespalib::string &name, MemoryGain memoryGain)
{
    return std::make_shared<MyFlushTarget>(name, memoryGain, DiskGain(), SerialNum(), system_time(), false);
}

MyFlushTarget::SP
createTargetD(const vespalib::string &name, DiskGain diskGain, SerialNum serial = 0)
{
    return std::make_shared<MyFlushTarget>(name, MemoryGain(), diskGain, serial, system_time(), false);
}

MyFlushTarget::SP
createTargetS(const vespalib::string &name, SerialNum serial, system_time timeStamp = system_time())
{
    return std::make_shared<MyFlushTarget>(name, MemoryGain(), DiskGain(), serial, timeStamp, false);
}

MyFlushTarget::SP
createTargetT(const vespalib::string &name, system_time lastFlushTime, SerialNum serial = 0)
{
    return std::make_shared<MyFlushTarget>(name, MemoryGain(), DiskGain(), serial, lastFlushTime, false);
}

MyFlushTarget::SP
createTargetF(const vespalib::string &name, bool urgentFlush)
{
    return std::make_shared<MyFlushTarget>(name, MemoryGain(), DiskGain(), SerialNum(), system_time(), urgentFlush);
}

void
assertOrder(const StringList &exp, const FlushContext::List &act)
{
    ASSERT_EQ(exp.size(), act.size());
    for (size_t i = 0; i < exp.size(); ++i) {
        EXPECT_EQ(exp[i], act[i]->getTarget()->getName());
    }
}

TEST(MemoryFlushTest, can_order_by_memory_gain)
{
    ContextBuilder cb;
    cb.add(createTargetM("t2", MemoryGain(10, 0)))
      .add(createTargetM("t1", MemoryGain(5, 0)))
      .add(createTargetM("t4", MemoryGain(20, 0)))
      .add(createTargetM("t3", MemoryGain(15, 0)));
    { // target t4 has memoryGain >= maxMemoryGain
        MemoryFlush flush({1000, 20_Gi, 1.0, 20, 1.0, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
    { // trigger totalMemoryGain >= globalMaxMemory
        MemoryFlush flush({50, 20_Gi, 1.0, 1000, 1.0, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
}

int64_t milli = 1000000;

TEST(MemoryFlushTest, can_order_by_disk_gain_with_large_values)
{
    ContextBuilder cb;
    int64_t before = 100 * milli;
    cb.add(createTargetD("t2", DiskGain(before, 70 * milli)))  // gain 30M
      .add(createTargetD("t1", DiskGain(before, 75 * milli)))  // gain 25M
      .add(createTargetD("t4", DiskGain(before, 45 * milli)))  // gain 55M
      .add(createTargetD("t3", DiskGain(before, 50 * milli))); // gain 50M
    { // target t4 has diskGain > bloatValue
        // t4 gain: 55M / 100M = 0.55 -> bloat factor 0.54 to trigger
        MemoryFlush flush({1000, 20_Gi, 10.0, 1000, 0.54, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
    { // trigger totalDiskGain > totalBloatValue
        // total gain: 160M / 4 * 100M = 0.4 -> bloat factor 0.39 to trigger
        MemoryFlush flush({1000, 20_Gi, 0.39, 1000, 10.0, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
}

TEST(MemoryFlushTest, can_order_by_disk_gain_with_small_values)
{
    ContextBuilder cb;
    cb.add(createTargetD("t2", DiskGain(100, 70)))  // gain 30
      .add(createTargetD("t1", DiskGain(100, 75)))  // gain 25
      .add(createTargetD("t4", DiskGain(100, 45)))  // gain 55
      .add(createTargetD("t3", DiskGain(100, 50))); // gain 50
    // total disk bloat value calculation uses min 100M disk size
    // target bloat value calculation uses min 100M disk size
    { // target t4 has diskGain > bloatValue
        // t4 gain: 55 / 100M = 0.0000055 -> bloat factor 0.0000054 to trigger
        MemoryFlush flush({1000, 20_Gi, 10.0, 1000, 0.00000054, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
    { // trigger totalDiskGain > totalBloatValue
        // total gain: 160 / 100M = 0.0000016 -> bloat factor 0.0000015 to trigger
        MemoryFlush flush({1000, 20_Gi, 0.0000015, 1000, 10.0, minutes(1)});
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
}

TEST(MemoryFlushTest, can_order_by_age)
{
    system_time now(vespalib::system_clock::now());
    system_time start(now - seconds(20));
    ContextBuilder cb;
    cb.add(createTargetT("t2", now - seconds(10)))
      .add(createTargetT("t1", now - seconds(5)))
      .add(createTargetT("t4", system_time()))
      .add(createTargetT("t3", now - seconds(15)));

    { // all targets have timeDiff >= maxTimeGain
        MemoryFlush flush({1000, 20_Gi, 1.0, 1000, 1.0, seconds(2)}, start);
        assertOrder({"t4", "t3", "t2", "t1"}, cb.flush_targets(flush));
    }
    { // no targets have timeDiff >= maxTimeGain
        MemoryFlush flush({1000, 20_Gi, 1.0, 1000, 1.0, seconds(30)}, start);
        assertOrder({}, cb.flush_targets(flush));
    }
}

TEST(MemoryFlushTest, can_order_by_tls_size)
{
    system_time now(vespalib::system_clock::now());
    system_time start = now - seconds(20);
    ContextBuilder cb;
    auto handler1 = std::make_shared<MyFlushHandler>("handler1");
    auto handler2 = std::make_shared<MyFlushHandler>("handler2");
    cb.addTls("handler1", {20_Gi, 1001, 2000 });
    cb.addTls("handler2", { 5_Gi, 1001, 2000 });
    cb.add(std::make_shared<FlushContext>(handler1, createTargetT("t2", now - seconds(10), 1900), 2000)).
        add(std::make_shared<FlushContext>(handler2, createTargetT("t1", now - seconds(5), 1000), 2000)).
        add(std::make_shared<FlushContext>(handler1, createTargetT("t4", system_time(), 1000), 2000)).
        add(std::make_shared<FlushContext>(handler2, createTargetT("t3", now - seconds(15), 1900), 2000));
    { // sum of tls sizes above limit, trigger sort order based on tls size
        MemoryFlush flush({1000, 3_Gi, 1.0, 1000, 1.0, seconds(2)}, start);
        assertOrder({"t4", "t1", "t2", "t3"}, cb.flush_targets(flush));
    }
    { // sum of tls sizes below limit
        MemoryFlush flush({1000, 30_Gi, 1.0, 1000, 1.0, seconds(30)}, start);
        assertOrder({}, cb.flush_targets(flush));
    }
}

TEST(MemoryFlushTest, order_by_tls_size_not_always_selected_when_active_flushes)
{
    system_time now = vespalib::system_clock::now();
    system_time start = now - seconds(20);
    ContextBuilder cb;
    auto h1 = std::make_shared<MyFlushHandler>("h1");
    constexpr uint64_t first_serial = 1001;
    constexpr uint64_t last_serial = 2000;
    cb.addTls("h1", {5_Gi, first_serial, last_serial});
    cb.add(std::make_shared<FlushContext>(h1, createTargetT("t1", now - seconds(10), 1300), last_serial)).
       add(std::make_shared<FlushContext>(h1, createTargetT("t2", now - seconds(5), 1500), last_serial));
    MemoryFlush flush({1000, 4_Gi, 1.0, 1000, 1.0, seconds(60)}, start);

    // Last flush time of both t1 and t2 are older than start of active flush -> triggers TLSSIZE.
    cb.active_flush("h1", now - seconds(4));
    assertOrder({"t1", "t2"}, cb.flush_targets(flush));

    // Last flush time of only t1 is older than start of active flush -> triggers TLSSIZE.
    cb.active_flush("h1", now - seconds(9));
    assertOrder({"t1", "t2"}, cb.flush_targets(flush));

    // Last flush time both t1 and t2 is newer than start of active flush -> don't use TLSSIZE.
    cb.active_flush("h1", now - seconds(11));
    assertOrder({}, cb.flush_targets(flush));
}

TEST(MemoryFlushTest, can_handle_large_serial_numbers_when_ordering_by_tls_size)
{
    uint64_t uint32_max = std::numeric_limits<uint32_t>::max();
    ContextBuilder cb;
    SerialNum firstSerial = 10;
    SerialNum lastSerial = uint32_max + 10;
    cb.addTls("myhandler", {uint32_max, firstSerial, lastSerial});
    cb.add(createTargetT("t1", system_time(), uint32_max + 5), lastSerial);
    cb.add(createTargetT("t2", system_time(), uint32_max - 5), lastSerial);
    uint64_t maxMemoryGain = 10;
    MemoryFlush flush({maxMemoryGain, 1000, 0, maxMemoryGain, 0, vespalib::duration(0)}, system_time());
    assertOrder({"t2", "t1"}, cb.flush_targets(flush));
}

TEST(MemoryFlushTest, order_type_is_preserved)
{
    system_time now(vespalib::system_clock::now());
    system_time ts2 = now - seconds(20);

    { // MAXAGE VS DISKBLOAT
        ContextBuilder cb;
        cb.add(createTargetT("t2", ts2, 5), 14)
          .add(createTargetD("t1", DiskGain(100 * milli, 80 * milli), 5));
        MemoryFlush flush({1000, 20_Gi, 1.0, 1000, 0.19, seconds(30)});
        assertOrder({"t1", "t2"}, cb.flush_targets(flush));
    }
    { // DISKBLOAT VS MEMORY
        ContextBuilder cb;
        cb.add(createTargetD("t2", DiskGain(100 * milli, 80 * milli)))
          .add(createTargetM("t1", MemoryGain(100, 80)));
        MemoryFlush flush({1000, 20_Gi, 1.0, 20, 0.19, seconds(30)});
        assertOrder({"t1", "t2"}, cb.flush_targets(flush));
    }
    { // urgent flush
        ContextBuilder cb;
        cb.add(createTargetF("t2", false))
          .add(createTargetF("t1", true));
        MemoryFlush flush({1000, 20_Gi, 1.0, 1000, 1.0, seconds(30)});
        assertOrder({"t1", "t2"}, cb.flush_targets(flush));
    }
}

GTEST_MAIN_RUN_ALL_TESTS()