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

#include "summarycompacttarget.h"
#include <vespa/vespalib/util/lambdatask.h>
#include <future>

using search::IDocumentStore;
using search::SerialNum;
using vespalib::makeLambdaTask;
using searchcorespi::FlushStats;
using searchcorespi::IFlushTarget;
using searchcorespi::FlushTask;

namespace proton {

namespace {

class Compacter : public FlushTask {
private:
    IDocumentStore & _docStore;
    FlushStats     & _stats;
    SerialNum        _currSerial;
    virtual void compact(IDocumentStore & docStore, SerialNum currSerial) const = 0;
public:
    Compacter(IDocumentStore & docStore, FlushStats & stats, SerialNum currSerial)
        : _docStore(docStore),
          _stats(stats),
          _currSerial(currSerial)
    {}
    void run() override {
        compact(_docStore, _currSerial);
        updateStats();
    }
    void updateStats() {
        // the target must live until this task is done (handled by flush engine).
        _stats.setPath(_docStore.getBaseDir());
    }

    SerialNum getFlushSerial() const override {
        return 0u; // Zero means no sync of transaction log is needed
    }
};

class CompactBloat : public Compacter {
public:
    CompactBloat(IDocumentStore & docStore, FlushStats & stats, SerialNum currSerial)
        : Compacter(docStore, stats, currSerial)
    {}
private:
    void compact(IDocumentStore & docStore, SerialNum currSerial) const override {
        docStore.compactBloat(currSerial);
    }
};

class CompactSpread : public Compacter {
public:
    CompactSpread(IDocumentStore & docStore, FlushStats & stats, SerialNum currSerial)
        : Compacter(docStore, stats, currSerial)
    {}
private:
    void compact(IDocumentStore & docStore, SerialNum currSerial) const override {
        docStore.compactSpread(currSerial);
    }
};

}

SummaryGCTarget::SummaryGCTarget(const vespalib::string & name, vespalib::Executor & summaryService, IDocumentStore & docStore)
    : LeafFlushTarget(name, Type::GC, Component::DOCUMENT_STORE),
      _summaryService(summaryService),
      _docStore(docStore),
      _lastStats()
{
    _lastStats.setPathElementsToLog(6);
}

IFlushTarget::MemoryGain
SummaryGCTarget::getApproxMemoryGain() const
{
    return MemoryGain::noGain(_docStore.memoryUsed());
}

IFlushTarget::DiskGain
SummaryGCTarget::getApproxDiskGain() const
{
    size_t total(_docStore.getDiskFootprint());
    return DiskGain(total, total - std::min(total, getBloat(_docStore)));
}

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

SerialNum
SummaryGCTarget::getFlushedSerialNum() const
{
    return _docStore.tentativeLastSyncToken();
}

IFlushTarget::Task::UP
SummaryGCTarget::initFlush(SerialNum currentSerial, std::shared_ptr<search::IFlushToken>)
{
    std::promise<Task::UP> promise;
    std::future<Task::UP> future = promise.get_future();
    _summaryService.execute(makeLambdaTask([this, &promise,currentSerial]() {
        promise.set_value(create(_docStore, _lastStats, currentSerial));
    }));
    return future.get();
}

SummaryCompactBloatTarget::SummaryCompactBloatTarget(vespalib::Executor & summaryService, IDocumentStore & docStore)
    : SummaryGCTarget("summary.compact_bloat", summaryService, docStore)
{
}

size_t
SummaryCompactBloatTarget::getBloat(const search::IDocumentStore & docStore) const {
    return docStore.getDiskBloat();
}

FlushTask::UP
SummaryCompactBloatTarget::create(IDocumentStore & docStore, FlushStats & stats, SerialNum currSerial) {
    return std::make_unique<CompactBloat>(docStore, stats, currSerial);
}

SummaryCompactSpreadTarget::SummaryCompactSpreadTarget(vespalib::Executor & summaryService, IDocumentStore & docStore)
    : SummaryGCTarget("summary.compact_spread", summaryService, docStore)
{
}

size_t
SummaryCompactSpreadTarget::getBloat(const search::IDocumentStore & docStore) const {
    return docStore.getMaxSpreadAsBloat();
}

FlushTask::UP
SummaryCompactSpreadTarget::create(IDocumentStore & docStore, FlushStats & stats, SerialNum currSerial) {
    return std::make_unique<CompactSpread>(docStore, stats, currSerial);
}

} // namespace proton