aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/docsummary/summarymanager.cpp
blob: 8a27c89cb653dbea386e971afed5a951ad04782f (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "summarymanager.h"
#include "documentstoreadapter.h"
#include "summarycompacttarget.h"
#include "summaryflushtarget.h"
#include <vespa/config/print/ostreamconfigwriter.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/juniper/rpinterface.h>
#include <vespa/searchcore/proton/flushengine/shrink_lid_space_flush_target.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/searchsummary/docsummary/docsum_field_writer_factory.h>
#include <vespa/searchsummary/docsummary/i_query_term_filter.h>
#include <vespa/searchsummary/docsummary/query_term_filter_factory.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/fastlib/text/normwordfolder.h>
#include <vespa/config-summary.h>

#include <sstream>

#include <vespa/log/log.h>
LOG_SETUP(".proton.docsummary.summarymanager");

using namespace config;
using namespace document;
using namespace search::docsummary;
using vespalib::make_string;
using vespalib::IllegalArgumentException;
using vespalib::compression::CompressionConfig;

using search::DocumentStore;
using search::IDocumentStore;
using search::LogDocumentStore;
using search::WriteableFileChunk;
using vespalib::makeLambdaTask;

using search::TuneFileSummary;
using search::common::FileHeaderContext;
using searchcorespi::IFlushTarget;

namespace proton {

namespace {

class ShrinkSummaryLidSpaceFlushTarget : public ShrinkLidSpaceFlushTarget
{
    using ICompactableLidSpace = search::common::ICompactableLidSpace;
    vespalib::Executor & _summaryService;

public:
    ShrinkSummaryLidSpaceFlushTarget(const vespalib::string &name, Type type, Component component,
                                     SerialNum flushedSerialNum, vespalib::system_time lastFlushTime,
                                     vespalib::Executor & summaryService,
                                     std::shared_ptr<ICompactableLidSpace> target);
    ~ShrinkSummaryLidSpaceFlushTarget() override;
    Task::UP initFlush(SerialNum currentSerial, std::shared_ptr<search::IFlushToken> flush_token) override;
};

ShrinkSummaryLidSpaceFlushTarget::
ShrinkSummaryLidSpaceFlushTarget(const vespalib::string &name, Type type, Component component,
                                 SerialNum flushedSerialNum, vespalib::system_time lastFlushTime,
                                 vespalib::Executor & summaryService,
                                 std::shared_ptr<ICompactableLidSpace> target)
    : ShrinkLidSpaceFlushTarget(name, type, component, flushedSerialNum, lastFlushTime, std::move(target)),
      _summaryService(summaryService)
{
}

ShrinkSummaryLidSpaceFlushTarget::~ShrinkSummaryLidSpaceFlushTarget() = default;

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

}

SummaryManager::SummarySetup::
SummarySetup(const vespalib::string & baseDir, const SummaryConfig & summaryCfg,
             const JuniperrcConfig & juniperCfg,
             search::IAttributeManager::SP attributeMgr, search::IDocumentStore::SP docStore,
             std::shared_ptr<const DocumentTypeRepo> repo,
             const search::index::Schema& schema)
    : _docsumWriter(),
      _wordFolder(std::make_unique<Fast_NormalizeWordFolder>()),
      _juniperProps(juniperCfg),
      _juniperConfig(),
      _attributeMgr(std::move(attributeMgr)),
      _docStore(std::move(docStore)),
      _repo(std::move(repo))
{
    _juniperConfig = std::make_unique<juniper::Juniper>(&_juniperProps, _wordFolder.get());
    auto resultConfig = std::make_unique<ResultConfig>();
    std::unique_ptr<IQueryTermFilterFactory> query_term_filter_factory = std::make_unique<QueryTermFilterFactory>(schema);
    auto docsum_field_writer_factory = std::make_unique<DocsumFieldWriterFactory>(summaryCfg.usev8geopositions, *this, *query_term_filter_factory);
    if (!resultConfig->readConfig(summaryCfg, make_string("SummaryManager(%s)", baseDir.c_str()).c_str(),
                                  *docsum_field_writer_factory)) {
        std::ostringstream oss;
        ::config::OstreamConfigWriter writer(oss);
        writer.write(summaryCfg);
        throw IllegalArgumentException
            (make_string("Could not initialize summary result config for directory '%s' based on summary config '%s'",
                         baseDir.c_str(), oss.str().c_str()));
    }
    docsum_field_writer_factory.reset();

    _docsumWriter = std::make_unique<DynamicDocsumWriter>(std::move(resultConfig));
}

IDocsumStore::UP
SummaryManager::SummarySetup::createDocsumStore()
{
    return std::make_unique<DocumentStoreAdapter>(*_docStore, *_repo);
}


ISummaryManager::ISummarySetup::SP
SummaryManager::createSummarySetup(const SummaryConfig & summaryCfg,
                                   const JuniperrcConfig & juniperCfg, const std::shared_ptr<const DocumentTypeRepo> &repo,
                                   const search::IAttributeManager::SP &attributeMgr,
                                   const search::index::Schema& schema)
{
    return std::make_shared<SummarySetup>(_baseDir, summaryCfg,
                                          juniperCfg, attributeMgr, _docStore, repo, schema);
}

SummaryManager::SummaryManager(vespalib::Executor &shared_executor, const LogDocumentStore::Config & storeConfig,
                               const search::GrowStrategy & growStrategy, const vespalib::string &baseDir,
                               const TuneFileSummary &tuneFileSummary,
                               const FileHeaderContext &fileHeaderContext, search::transactionlog::SyncProxy &tlSyncer,
                               search::IBucketizer::SP bucketizer)
    : _baseDir(baseDir),
      _docStore()
{
    _docStore = std::make_shared<LogDocumentStore>(shared_executor, baseDir, storeConfig, growStrategy, tuneFileSummary,
                                                   fileHeaderContext, tlSyncer, std::move(bucketizer));
}

SummaryManager::~SummaryManager() = default;

void
SummaryManager::putDocument(uint64_t syncToken, search::DocumentIdT lid, const Document & doc)
{
    _docStore->write(syncToken, lid, doc);
}

void
SummaryManager::putDocument(uint64_t syncToken, search::DocumentIdT lid, const vespalib::nbostream & doc)
{
    _docStore->write(syncToken, lid, doc);
}

void
SummaryManager::removeDocument(uint64_t syncToken, search::DocumentIdT lid)
{
    _docStore->remove(syncToken, lid);
}

namespace {

IFlushTarget::SP
createShrinkLidSpaceFlushTarget(vespalib::Executor & summaryService, IDocumentStore::SP docStore)
{
    return std::make_shared<ShrinkSummaryLidSpaceFlushTarget>("summary.shrink",
                                                       IFlushTarget::Type::GC,
                                                       IFlushTarget::Component::DOCUMENT_STORE,
                                                       docStore->lastSyncToken(),
                                                       docStore->getLastFlushTime(),
                                                       summaryService,
                                                       std::move(docStore));
}

}

IFlushTarget::List
SummaryManager::getFlushTargets(vespalib::Executor & summaryService)
{
    IFlushTarget::List ret;
    ret.push_back(std::make_shared<SummaryFlushTarget>(getBackingStore(), summaryService));
    if (dynamic_cast<LogDocumentStore *>(_docStore.get()) != nullptr) {
        ret.push_back(std::make_shared<SummaryCompactBloatTarget>(summaryService, getBackingStore()));
        ret.push_back(std::make_shared<SummaryCompactSpreadTarget>(summaryService, getBackingStore()));
    }
    ret.push_back(createShrinkLidSpaceFlushTarget(summaryService, _docStore));
    return ret;
}

void
SummaryManager::reconfigure(const LogDocumentStore::Config & config) {
    auto & docStore = dynamic_cast<LogDocumentStore &> (*_docStore);
    docStore.reconfigure(config);
}

} // namespace proton