aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/prepare_restart_handler.cpp
blob: 0ec0e6a1147b882c83b49711314f114ffb17908b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "prepare_restart_handler.h"
#include <vespa/searchcore/proton/flushengine/flushengine.h>
#include <vespa/searchcore/proton/flushengine/prepare_restart_flush_strategy.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.server.prepare_restart_handler");

namespace proton {

PrepareRestartHandler::PrepareRestartHandler(FlushEngine &flushEngine)
    : _flushEngine(flushEngine),
      _mutex(),
      _cond(),
      _running(false)
{
}

bool
PrepareRestartHandler::prepareRestart(const ProtonConfig &protonCfg)
{
    std::unique_lock lock(_mutex);
    if (!_flushEngine.has_thread()) {
        return false;
    }
    if (!_running) {
        performPrepareRestart(protonCfg, lock);
    } else {
        _cond.wait(lock, [this] { return !_running; });
        LOG(info, "prepareRestart(): Waited for another thread performing prepareRestart()");
    }
    return true;
}

namespace {

PrepareRestartFlushStrategy::Config
createPrepareRestartConfig(const PrepareRestartHandler::ProtonConfig &protonCfg)
{
    return PrepareRestartFlushStrategy::Config(protonCfg.flush.preparerestart.replaycost,
                                               protonCfg.flush.preparerestart.replayoperationcost,
                                               protonCfg.flush.preparerestart.writecost);
}

}

void
PrepareRestartHandler::performPrepareRestart(const ProtonConfig &protonCfg, std::unique_lock<std::mutex> &lock)
{
    _running = true;
    lock.unlock();
    _flushEngine.setStrategy(std::make_shared<PrepareRestartFlushStrategy>(createPrepareRestartConfig(protonCfg)));
    lock.lock();
    _running = false;
    _cond.notify_all();
}

}