aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcorespi/index/indexfusiontarget.cpp
blob: 562d49a4348e96fb481a8191164784a6ceead3cd (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "indexfusiontarget.h"
#include <cinttypes>

#include <vespa/log/log.h>
LOG_SETUP(".searchcorespi.index.indexfusiontarget");

namespace searchcorespi::index {

using search::SerialNum;
namespace {

class Fusioner : public FlushTask {
private:
    IndexMaintainer &_indexMaintainer;
    FlushStats      &_stats;
    SerialNum        _serialNum;
    std::shared_ptr<search::IFlushToken> _flush_token;
public:
    Fusioner(IndexMaintainer &indexMaintainer, FlushStats &stats, SerialNum serialNum, std::shared_ptr<search::IFlushToken> flush_token) :
        _indexMaintainer(indexMaintainer),
        _stats(stats),
        _serialNum(serialNum),
        _flush_token(std::move(flush_token))
    {}

    void run() override {
        vespalib::string outputFusionDir = _indexMaintainer.doFusion(_serialNum, _flush_token);
        // the target must live until this task is done (handled by flush engine).
        _stats.setPath(outputFusionDir);
    }

    SerialNum getFlushSerial() const override {
        return 0u; // Zero means that no tls syncing is needed
    }
};

}
IndexFusionTarget::IndexFusionTarget(IndexMaintainer &indexMaintainer)
    : LeafFlushTarget("memoryindex.fusion", Type::GC, Component::INDEX),
      _indexMaintainer(indexMaintainer),
      _fusionStats(indexMaintainer.getFusionStats()),
      _lastStats()
{
    _lastStats.setPathElementsToLog(7);
    LOG(debug, "New target, Num flushed: %d, Disk usage: %" PRIu64, _fusionStats.numUnfused, _fusionStats.diskUsage);
}

IndexFusionTarget::~IndexFusionTarget() = default;

IFlushTarget::MemoryGain
IndexFusionTarget::getApproxMemoryGain() const
{
    return MemoryGain(0, 0);
}

IFlushTarget::DiskGain
IndexFusionTarget::getApproxDiskGain() const
{
    uint64_t diskUsageBefore = _fusionStats.diskUsage;
    uint64_t diskUsageGain = static_cast<uint64_t>((0.1 * (diskUsageBefore * std::max(0,static_cast<int>(_fusionStats.numUnfused - 1)))));
    diskUsageGain = std::min(diskUsageGain, diskUsageBefore);
    if (!_fusionStats._canRunFusion)
        diskUsageGain = 0;
    return DiskGain(diskUsageBefore, diskUsageBefore - diskUsageGain);
}

bool
IndexFusionTarget::needUrgentFlush() const
{
    bool urgent = (_fusionStats.numUnfused > _fusionStats.maxFlushed) && (_fusionStats._canRunFusion);
    LOG(debug, "Num flushed: %d Urgent: %d", _fusionStats.numUnfused, urgent);
    return urgent;
}

IFlushTarget::Time
IndexFusionTarget::getLastFlushTime() const
{
    return vespalib::system_clock::now();
}

IFlushTarget::SerialNum
IndexFusionTarget::getFlushedSerialNum() const
{
    // Lack of fusion operation doesn't prevent transaction log
    // pruning.
    return _indexMaintainer.getCurrentSerialNum();
}

IFlushTarget::Task::UP
IndexFusionTarget::initFlush(SerialNum serialNum, std::shared_ptr<search::IFlushToken> flush_token)
{
    return std::make_unique<Fusioner>(_indexMaintainer, _lastStats, serialNum, std::move(flush_token));
}

uint64_t
IndexFusionTarget::getApproxBytesToWriteToDisk() const
{
    return _fusionStats.diskUsage;
}

}