summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/flushengine/flush_target_candidates.cpp
blob: e83b0adc3f5f8c86e9108bd955ad5bcfdf9da12c (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 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP(".proton.flushengine.flush_target_candidates");

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

namespace proton {

using search::SerialNum;

using Config = PrepareRestartFlushStrategy::Config;

namespace {

SerialNum
calculateReplayStartSerial(const FlushContext::List &sortedFlushContexts,
                           size_t numCandidates,
                           const flushengine::TlsStats &tlsStats)
{
    if (numCandidates == 0) {
        return tlsStats.getFirstSerial();
    }
    if (numCandidates == sortedFlushContexts.size()) {
        return tlsStats.getLastSerial() + 1;
    }
    return sortedFlushContexts[numCandidates]->getTarget()->getFlushedSerialNum() + 1;
}

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

double
calculateFlushTargetsWriteCost(const FlushContext::List &sortedFlushContexts,
                               size_t numCandidates,
                               const Config &cfg)
{
    double result = 0;
    for (size_t i = 0; i < numCandidates; ++i) {
        const auto &flushContext = sortedFlushContexts[i];
        result += (flushContext->getTarget()->getApproxBytesToWriteToDisk() *
                cfg.flushTargetWriteCost);
    }
    return result;
}

}

FlushTargetCandidates::FlushTargetCandidates(const FlushContext::List &sortedFlushContexts,
                                             size_t numCandidates,
                                             const flushengine::TlsStats &tlsStats,
                                             const Config &cfg)
    : _sortedFlushContexts(&sortedFlushContexts),
      _numCandidates(numCandidates),
      _tlsReplayCost(calculateTlsReplayCost(tlsStats,
                                            cfg,
                                            calculateReplayStartSerial(sortedFlushContexts,
                                                                       numCandidates,
                                                                       tlsStats))),
      _flushTargetsWriteCost(calculateFlushTargetsWriteCost(sortedFlushContexts,
                                                            numCandidates,
                                                            cfg))
{
}

FlushContext::List
FlushTargetCandidates::getCandidates() const
{
    FlushContext::List result(_sortedFlushContexts->begin(),
            _sortedFlushContexts->begin() + _numCandidates);
    return result;
}

} // namespace proton