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

#include "flush_target_candidates.h"
#include "flush_target_candidate.h"
#include "tls_stats.h"

namespace proton {

using search::SerialNum;

using Config = PrepareRestartFlushStrategy::Config;
using TlsReplayCost = FlushTargetCandidates::TlsReplayCost;

namespace {

SerialNum
calculateReplayStartSerial(vespalib::ConstArrayRef<FlushTargetCandidate> candidates,
                           size_t num_candidates,
                           const flushengine::TlsStats &tlsStats)
{
    if (num_candidates == 0) {
        return tlsStats.getFirstSerial();
    }
    if (num_candidates == candidates.size()) {
        return tlsStats.getLastSerial() + 1;
    }
    return candidates[num_candidates].get_flushed_serial() + 1;
}

TlsReplayCost
calculateTlsReplayCost(const flushengine::TlsStats &tlsStats,
                       const Config &cfg,
                       SerialNum replayStartSerial)
{
    SerialNum replayEndSerial = tlsStats.getLastSerial();
    SerialNum numTotalOperations = replayEndSerial - tlsStats.getFirstSerial() + 1;
    if (numTotalOperations == 0) {
        return TlsReplayCost(0.0, 0.0);
    }
    double numBytesPerOperation =
        (double)tlsStats.getNumBytes() / (double)numTotalOperations;
    SerialNum numOperationsToReplay = replayEndSerial + 1 - replayStartSerial;
    double numBytesToReplay = numBytesPerOperation * numOperationsToReplay;
    return TlsReplayCost((numBytesToReplay * cfg.tlsReplayByteCost), (numOperationsToReplay * cfg.tlsReplayOperationCost));
}

double
calculateFlushTargetsWriteCost(vespalib::ConstArrayRef<FlushTargetCandidate> candidates,
                               size_t num_candidates)
{
    double result = 0;
    for (size_t i = 0; i < num_candidates; ++i) {
        result += candidates[i].get_write_cost();
    }
    return result;
}

}

FlushTargetCandidates::FlushTargetCandidates(vespalib::ConstArrayRef<FlushTargetCandidate> candidates,
                                             size_t num_candidates,
                                             const flushengine::TlsStats &tlsStats,
                                             const Config &cfg)
    : _candidates(candidates),
      _num_candidates(std::min(num_candidates, _candidates.size())),
      _tlsReplayCost(calculateTlsReplayCost(tlsStats,
                                            cfg,
                                            calculateReplayStartSerial(_candidates,
                                                                       _num_candidates,
                                                                       tlsStats))),
      _flushTargetsWriteCost(calculateFlushTargetsWriteCost(_candidates,
                                                            _num_candidates))
{
}

FlushContext::List
FlushTargetCandidates::getCandidates() const
{
    FlushContext::List result;
    result.reserve(_num_candidates);
    for (const auto &candidate : _candidates) {
        if (result.size() < _num_candidates || candidate.get_always_flush()) {
            result.emplace_back(candidate.get_flush_context());
        }
    }
    return result;
}

} // namespace proton