summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/docsummary/summarymanager.cpp
blob: e65209bf52673b50e94d2aee104443f68bfd74ce (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentstoreadapter.h"
#include "summarycompacttarget.h"
#include "summaryflushtarget.h"
#include "summarymanager.h"
#include <vespa/config/print/ostreamconfigwriter.h>
#include <vespa/juniper/rpinterface.h>
#include <vespa/searchcorespi/index/i_thread_service.h>
#include <vespa/searchcore/proton/flushengine/shrink_lid_space_flush_target.h>
#include <vespa/searchlib/docstore/logdocumentstore.h>
#include <vespa/searchlib/common/lambdatask.h>
#include <vespa/searchsummary/docsummary/docsumconfig.h>
#include <vespa/vespalib/util/exceptions.h>
#include <sstream>
#include <future>

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

using namespace config;
using namespace document;
using namespace search::docsummary;
using namespace vespa::config::search::core;
using namespace vespa::config::search::summary;
using namespace vespa::config::search;
using vespalib::make_string;
using vespalib::IllegalArgumentException;
using search::DocumentStore;
using search::IDocumentStore;
using search::LogDocumentStore;
using search::LogDataStore;
using search::WriteableFileChunk;
using search::makeLambdaTask;

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

namespace proton {

namespace {

class ShrinkSummaryLidSpaceFlushTarget : public  ShrinkLidSpaceFlushTarget
{
    using ICompactableLidSpace = search::common::ICompactableLidSpace;
    searchcorespi::index::IThreadService & _summaryService;

public:
    ShrinkSummaryLidSpaceFlushTarget(const vespalib::string &name,
                                     Type type,
                                     Component component,
                                     SerialNum flushedSerialNum,
                                     Time lastFlushTime,
                                     searchcorespi::index::IThreadService & summaryService,
                                     std::shared_ptr<ICompactableLidSpace> target);
    ~ShrinkSummaryLidSpaceFlushTarget();
    virtual Task::UP initFlush(SerialNum currentSerial) override;
};

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

ShrinkSummaryLidSpaceFlushTarget::~ShrinkSummaryLidSpaceFlushTarget() {}

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

}

SummaryManager::SummarySetup::
SummarySetup(const vespalib::string & baseDir,
             const DocTypeName & docTypeName,
             const SummaryConfig & summaryCfg,
             const SummarymapConfig & summarymapCfg,
             const JuniperrcConfig & juniperCfg,
             const search::IAttributeManager::SP &attributeMgr,
             const search::IDocumentStore::SP & docStore,
             const DocumentTypeRepo::SP &repo)
    : _docsumWriter(),
      _wordFolder(),
      _juniperProps(juniperCfg),
      _juniperConfig(),
      _attributeMgr(attributeMgr),
      _docStore(docStore),
      _fieldCacheRepo(),
      _repo(repo),
      _markupFields()
{
    std::unique_ptr<ResultConfig> resultConfig(new ResultConfig());
    if (!resultConfig->ReadConfig(summaryCfg, make_string("SummaryManager(%s)", baseDir.c_str()).c_str())) {
        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()));
    }

    _juniperConfig.reset(new juniper::Juniper(&_juniperProps, &_wordFolder));
    _docsumWriter.reset(new DynamicDocsumWriter(resultConfig.release(), NULL));
    DynamicDocsumConfig dynCfg(this, _docsumWriter.get());
    dynCfg.configure(summarymapCfg);
    for (size_t i = 0; i < summarymapCfg.override.size(); ++i) {
        const SummarymapConfig::Override & o = summarymapCfg.override[i];
        if (o.command == "dynamicteaser" || o.command == "textextractor") {
            vespalib::string markupField = o.arguments;
            if (markupField.empty())
                continue;
            // Assume just one argument: source field that must contain markup
            _markupFields.insert(markupField);
        }
    }
    const DocumentType *docType = repo->getDocumentType(docTypeName.getName());
    if (docType != NULL) {
        _fieldCacheRepo.reset(new FieldCacheRepo(getResultConfig(), *docType));
    } else if (getResultConfig().GetNumResultClasses() == 0) {
        LOG(debug, "Create empty field cache repo for document type '%s'", docTypeName.toString().c_str());
        _fieldCacheRepo.reset(new FieldCacheRepo());
    } else {
        throw IllegalArgumentException(make_string("Did not find document type '%s' in current document type repo."
                                                   " Cannot setup field cache repo for the summary setup",
                                                   docTypeName.toString().c_str()));
    }
}

IDocsumStore::UP
SummaryManager::SummarySetup::createDocsumStore(const vespalib::string &resultClassName) {
    return std::make_unique<DocumentStoreAdapter>(*_docStore, *_repo, getResultConfig(), resultClassName,
                                                  _fieldCacheRepo->getFieldCache(resultClassName), _markupFields);
}


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

namespace {

template<typename T>
document::CompressionConfig
deriveCompression(const T & config) {
    document::CompressionConfig compression;
    if (config.type == T::LZ4) {
        compression.type = document::CompressionConfig::LZ4;
    } else if (config.type == T::ZSTD) {
        compression.type = document::CompressionConfig::ZSTD;
    }
    compression.compressionLevel = config.level;
    return compression;
}

DocumentStore::Config
getStoreConfig(const ProtonConfig::Summary::Cache & cache)
{
    return DocumentStore::Config(deriveCompression(cache.compression), cache.maxbytes, cache.initialentries).allowVisitCaching(cache.allowvisitcaching);
}

}

SummaryManager::SummaryManager(vespalib::ThreadExecutor & executor,
                               const ProtonConfig::Summary & summary,
                               const search::GrowStrategy & growStrategy,
                               const vespalib::string &baseDir,
                               const DocTypeName &docTypeName,
                               const TuneFileSummary &tuneFileSummary,
                               const FileHeaderContext &fileHeaderContext,
                               search::transactionlog::SyncProxy &tlSyncer,
                               const search::IBucketizer::SP & bucketizer)
    : _baseDir(baseDir),
      _docTypeName(docTypeName),
      _docStore(),
      _tuneFileSummary(tuneFileSummary),
      _currentSerial(0u)
{
    DocumentStore::Config config(getStoreConfig(summary.cache));
    const ProtonConfig::Summary::Log & log(summary.log);
    const ProtonConfig::Summary::Log::Chunk & chunk(log.chunk);

    WriteableFileChunk::Config fileConfig(deriveCompression(chunk.compression), chunk.maxbytes);
    LogDataStore::Config logConfig(log.maxfilesize, log.maxdiskbloatfactor, log.maxbucketspread,
                                   log.minfilesizefactor, log.numthreads, log.compact2activefile,
                                   deriveCompression(log.compact.compression), fileConfig);
    logConfig.disableCrcOnRead(chunk.skipcrconread);
    _docStore.reset(new LogDocumentStore(executor, baseDir,
                                         LogDocumentStore::Config(config, logConfig),
                                         growStrategy, tuneFileSummary, fileHeaderContext, tlSyncer,
                                         summary.compact2buckets ? bucketizer : search::IBucketizer::SP()));
}

SummaryManager::~SummaryManager() {}

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

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

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

namespace {

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

}

IFlushTarget::List SummaryManager::getFlushTargets(searchcorespi::index::IThreadService & summaryService)
{
    IFlushTarget::List ret;
    ret.push_back(std::make_shared<SummaryFlushTarget>(getBackingStore(), summaryService));
    if (dynamic_cast<LogDocumentStore *>(_docStore.get()) != NULL) {
        ret.push_back(std::make_shared<SummaryCompactTarget>(summaryService, getBackingStore()));
    }
    ret.push_back(createShrinkLidSpaceFlushTarget(summaryService, _docStore));
    return ret;
}

} // namespace proton