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

#include "prepare_restart_flush_strategy.h"
#include "flush_target_candidates.h"
#include "flush_target_candidate.h"
#include "tls_stats_map.h"
#include <sstream>
#include <algorithm>

#include <vespa/log/log.h>
LOG_SETUP(".proton.flushengine.prepare_restart_flush_strategy");

namespace proton {

using search::SerialNum;
using searchcorespi::IFlushTarget;

using Config = PrepareRestartFlushStrategy::Config;
using FlushContextsMap = std::map<vespalib::string, FlushContext::List>;
using FlushTargetCandidatesList = std::vector<FlushTargetCandidates::UP>;

PrepareRestartFlushStrategy::Config::Config(double tlsReplayByteCost_,
                                            double tlsReplayOperationCost_,
                                            double flushTargetWriteCost_)
    : tlsReplayByteCost(tlsReplayByteCost_),
      tlsReplayOperationCost(tlsReplayOperationCost_),
      flushTargetWriteCost(flushTargetWriteCost_)
{
}

PrepareRestartFlushStrategy::PrepareRestartFlushStrategy(const Config &cfg)
    : _cfg(cfg)
{
}

namespace {

FlushContext::List
removeGCFlushTargets(const FlushContext::List &flushContexts)
{
    FlushContext::List result;
    for (const auto &flushContext : flushContexts) {
        if (flushContext->getTarget()->getType() != IFlushTarget::Type::GC) {
            result.push_back(flushContext);
        }
    }
    return result;
}

FlushContextsMap
groupByFlushHandler(const FlushContext::List &flushContexts)
{
    FlushContextsMap result;
    for (const auto &flushContext : flushContexts) {
        const vespalib::string &handlerName = flushContext->getHandler()->getName();
        result[handlerName].push_back(flushContext);
    }
    return result;
}

FlushContext::List
flatten(const FlushContextsMap &flushContextsPerHandler)
{
    FlushContext::List result;
    for (const auto &entry : flushContextsPerHandler) {
        for (const auto &flushContext : entry.second) {
            result.push_back(flushContext);
        }
    }
    return result;
}

void
sortByOldestFlushedSerialNumber(std::vector<FlushTargetCandidate>& candidates)
{
    std::sort(candidates.begin(), candidates.end(),
              [](const auto &lhs, const auto &rhs) {
                  if (lhs.get_flushed_serial() == rhs.get_flushed_serial()) {
                      return lhs.get_flush_context()->getName() < rhs.get_flush_context()->getName();
                  }
                  return lhs.get_flushed_serial() < rhs.get_flushed_serial();
              });
}

vespalib::string
toString(const FlushContext::List &flushContexts)
{
    std::ostringstream oss;
    bool first = true;
    for (const auto &flushContext : flushContexts) {
        if (!first) {
            oss << ",";
        }
        oss << "'" << flushContext->getName() << "'";
        first = false;
    }
    return oss.str();
}

FlushContext::List
findBestTargetsToFlush(const FlushContext::List &unsortedFlushContexts,
                       const flushengine::TlsStats &tlsStats,
                       const Config &cfg)
{
    std::vector<FlushTargetCandidate> candidates;
    candidates.reserve(unsortedFlushContexts.size());
    for (const auto &flush_context : unsortedFlushContexts) {
        candidates.emplace_back(flush_context, tlsStats.getLastSerial(), cfg);
    }
    sortByOldestFlushedSerialNumber(candidates);

    FlushTargetCandidates bestSet(candidates, 0, tlsStats, cfg);
    for (size_t numCandidates = 1; numCandidates <= candidates.size(); ++numCandidates) {
        FlushTargetCandidates nextSet(candidates, numCandidates, tlsStats, cfg);
        LOG(debug, "findBestTargetsToFlush(): Created candidate set: "
                "flushTargets=[%s], tlsReplayBytesCost=%f, tlsReplayOperationsCost=%f, flushTargetsWriteCost=%f, totalCost=%f",
                toString(nextSet.getCandidates()).c_str(),
                nextSet.getTlsReplayCost().bytesCost,
                nextSet.getTlsReplayCost().operationsCost,
                nextSet.getFlushTargetsWriteCost(),
                nextSet.getTotalCost());
        if (nextSet.getTotalCost() < bestSet.getTotalCost()) {
            bestSet = nextSet;
        }
    }
    LOG(info, "findBestTargetsToFlush(): Best candidate set: "
            "flushTargets=[%s], tlsReplayBytesCost=%f, tlsReplayOperationsCost=%f, flushTargetsWriteCost=%f, totalCost=%f",
            toString(bestSet.getCandidates()).c_str(),
            bestSet.getTlsReplayCost().bytesCost,
            bestSet.getTlsReplayCost().operationsCost,
            bestSet.getFlushTargetsWriteCost(),
            bestSet.getTotalCost());
    return bestSet.getCandidates();
}

FlushContextsMap
findBestTargetsToFlushPerHandler(const FlushContextsMap &flushContextsPerHandler,
                                 const Config &cfg,
                                 const flushengine::TlsStatsMap &tlsStatsMap)
{
    FlushContextsMap result;
    for (const auto &entry : flushContextsPerHandler) {
        const auto &handlerName = entry.first;
        const auto &flushContexts = entry.second;
        const auto &tlsStats = tlsStatsMap.getTlsStats(handlerName);
        result.insert(std::make_pair(handlerName,
                findBestTargetsToFlush(flushContexts, tlsStats, cfg)));
    }
    return result;
}

}

FlushContext::List
PrepareRestartFlushStrategy::getFlushTargets(const FlushContext::List &targetList,
                                             const flushengine::TlsStatsMap &tlsStatsMap,
                                             const flushengine::ActiveFlushStats&) const
{
    return flatten(findBestTargetsToFlushPerHandler(
            groupByFlushHandler(removeGCFlushTargets(targetList)),
            _cfg, tlsStatsMap));
}

} // namespace proton