aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/docstore/filechunk.cpp
blob: c57650bb16f3c445196a4d1e429ba34ee0308e3d (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "filechunk.h"
#include "data_store_file_chunk_stats.h"
#include "summaryexceptions.h"
#include "randreaders.h"
#include <vespa/searchlib/util/filekit.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/data/fileheader.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/executor.h>
#include <vespa/vespalib/util/arrayqueue.hpp>
#include <vespa/fastos/file.h>
#include <filesystem>
#include <future>

#include <vespa/log/log.h>
LOG_SETUP(".search.filechunk");

using vespalib::CpuUsage;
using vespalib::GenericHeader;
using vespalib::getErrorString;

namespace search {

namespace {

constexpr size_t ALIGNMENT=0x1000;
constexpr size_t ENTRY_BIAS_SIZE=8;
const vespalib::string DOC_ID_LIMIT_KEY("docIdLimit");

}

using vespalib::make_string;

FileChunk::ChunkInfo::ChunkInfo(uint64_t offset, uint32_t size, uint64_t lastSerial) noexcept
    : _lastSerial(lastSerial),
      _offset(offset),
      _size(size)
{
    assert(valid());
}

vespalib::string
FileChunk::NameId::createName(const vespalib::string &baseName) const {
    vespalib::asciistream os;
    os << baseName << '/' << vespalib::setfill('0') << vespalib::setw(19) << getId();
    return os.str();
}

vespalib::string
FileChunk::createIdxFileName(const vespalib::string & name) {
    return name + ".idx";
}

vespalib::string
FileChunk::createDatFileName(const vespalib::string & name) {
    return name + ".dat";
}

FileChunk::FileChunk(FileId fileId, NameId nameId, const vespalib::string & baseName,
                     const TuneFileSummary & tune, const IBucketizer * bucketizer)
    : _fileId(fileId),
      _nameId(nameId),
      _name(nameId.createName(baseName)),
      _erasedCount(0),
      _erasedBytes(0),
      _diskFootprint(0),
      _sumNumBuckets(0),
      _numChunksWithBuckets(0),
      _numUniqueBuckets(0),
      _file(),
      _bucketizer(bucketizer),
      _addedBytes(0),
      _tune(tune),
      _dataFileName(createDatFileName(_name)),
      _idxFileName(createIdxFileName(_name)),
      _chunkInfo(),
      _lastPersistedSerialNum(0),
      _dataHeaderLen(0u),
      _idxHeaderLen(0u),
      _numLids(0),
      _docIdLimit(std::numeric_limits<uint32_t>::max()),
      _modificationTime()
{
    FastOS_File dataFile(_dataFileName.c_str());
    if (dataFile.OpenReadOnly()) {
        if (!dataFile.Sync()) {
            throw SummaryException("Failed syncing dat file", dataFile, VESPA_STRLOC);
        }
        _diskFootprint.fetch_add(dataFile.getSize(), std::memory_order_relaxed);
        FastOS_File idxFile(_idxFileName.c_str());
        if (idxFile.OpenReadOnly()) {
            if (!idxFile.Sync()) {
                throw SummaryException("Failed syncing idx file", idxFile, VESPA_STRLOC);
            }
            _diskFootprint.fetch_add(idxFile.getSize(), std::memory_order_relaxed);
            _modificationTime = FileKit::getModificationTime(_idxFileName);
        } else {
            throw SummaryException("Failed opening idx file", idxFile, VESPA_STRLOC);
        }
    }
}

FileChunk::~FileChunk() = default;

void
FileChunk::addNumBuckets(size_t numBucketsInChunk)
{
    _sumNumBuckets += numBucketsInChunk;
    if (numBucketsInChunk != 0) {
        ++_numChunksWithBuckets;
    }
}

void
FileChunk::TmpChunkMeta::fill(vespalib::nbostream & is) {
    resize(getNumEntries());
    for (LidMeta & lm : *this) {
        lm.deserialize(is);
    }
}

void
FileChunk::erase()
{
    _file.reset();
    std::filesystem::remove(std::filesystem::path(_idxFileName));
    std::filesystem::remove(std::filesystem::path(_dataFileName));
}

void
FileChunk::updateLidMap(const unique_lock &guard, ISetLid &ds, uint64_t serialNum, uint32_t docIdLimit)
{
    assert(_chunkInfo.empty());

    FastOS_File idxFile(_idxFileName.c_str());
    idxFile.enableMemoryMap(0);
    if ( ! idxFile.OpenReadOnly()) {
        LOG_ABORT("should not reach here");
    }
    if ( ! idxFile.IsMemoryMapped()) {
        assert(idxFile.getSize() == 0);
        return;
    }
    const int64_t fileSize = idxFile.getSize();
    if (_idxHeaderLen == 0) {
        _idxHeaderLen = readIdxHeader(idxFile, _docIdLimit);
    }
    BucketDensityComputer globalBucketMap(_bucketizer);
    // Guard comes from the same bucketizer so the same guard can be used
    // for both local and global BucketDensityComputer
    vespalib::GenerationHandler::Guard bucketizerGuard = globalBucketMap.getGuard();
    vespalib::nbostream is(static_cast<const char *>(idxFile.MemoryMapPtr(0)) + _idxHeaderLen,
                           fileSize - _idxHeaderLen);
    for (size_t count=0; ! is.empty() && is.good(); count++) {
        const int64_t lastKnownGoodPos = _idxHeaderLen + is.rp();
        TmpChunkMeta chunkMeta;
        try {
            chunkMeta.deserialize(is);
            chunkMeta.fill(is);
            if ((count == 0) && (chunkMeta.getLastSerial() < serialNum)) {
                LOG(warning, "last serial num(%" PRIu64 ") from previous file is bigger than my first(%" PRIu64
                             "). That is odd.Current filename is '%s'",
                    serialNum, chunkMeta.getLastSerial(), _idxFileName.c_str());
                serialNum = chunkMeta.getLastSerial();
            }
            assert(serialNum <= chunkMeta.getLastSerial());
            serialNum = handleChunk(guard, ds, docIdLimit, bucketizerGuard, globalBucketMap, chunkMeta);
            assert(serialNum >= _lastPersistedSerialNum.load(std::memory_order_relaxed));
            _lastPersistedSerialNum.store(serialNum, std::memory_order_relaxed);
        } catch (const vespalib::IllegalStateException & e) {
            LOG(warning, "Exception deserializing idx file : %s", e.what());
            LOG(warning, "File '%s' seems to be partially truncated. Will truncate from size=%" PRId64 " to %" PRId64,
                         _idxFileName.c_str(), fileSize, lastKnownGoodPos);
            FastOS_File toTruncate(_idxFileName.c_str());
            if ( toTruncate.OpenReadWrite()) {
                if (toTruncate.SetSize(lastKnownGoodPos)) {
                } else {
                    throw SummaryException("SetSize() failed.", toTruncate, VESPA_STRLOC);
                }
            } else {
                throw SummaryException("Open for truncation failed.", toTruncate, VESPA_STRLOC);
            }
            break;
        }
    }
    _numUniqueBuckets = globalBucketMap.getNumBuckets();
}

uint64_t
FileChunk::handleChunk(const unique_lock &guard, ISetLid &ds, uint32_t docIdLimit,
                       const vespalib::GenerationHandler::Guard & bucketizerGuard, BucketDensityComputer &globalBucketMap,
                       const TmpChunkMeta & chunkMeta) {
    BucketDensityComputer bucketMap(_bucketizer);
    for (size_t i(0), m(chunkMeta.getNumEntries()); i < m; i++) {
        const LidMeta & lidMeta(chunkMeta[i]);
        if (lidMeta.getLid() < docIdLimit) {
            if (_bucketizer && (lidMeta.size() > 0)) {
                document::BucketId bucketId = _bucketizer->getBucketOf(bucketizerGuard, lidMeta.getLid());
                bucketMap.recordLid(bucketId);
                globalBucketMap.recordLid(bucketId);
            }
            ds.setLid(guard, lidMeta.getLid(), LidInfo(getFileId().getId(), _chunkInfo.size(), lidMeta.size()));
            _numLids++;
        } else {
            remove(lidMeta.getLid(), lidMeta.size());
        }
        _addedBytes.store(getAddedBytes() + adjustSize(lidMeta.size()), std::memory_order_relaxed);
    }
    uint64_t serialNum = chunkMeta.getLastSerial();
    addNumBuckets(bucketMap.getNumBuckets());
    _chunkInfo.emplace_back(chunkMeta.getOffset(), chunkMeta.getSize(), chunkMeta.getLastSerial());
    return serialNum;
}


void
FileChunk::enableRead()
{
    if (_tune._randRead.getWantDirectIO()) {
        LOG(debug, "enableRead(): DirectIORandRead: file='%s'", _dataFileName.c_str());
        _file = std::make_unique<DirectIORandRead>(_dataFileName);
    } else if (_tune._randRead.getWantMemoryMap()) {
        const int mmapFlags(_tune._randRead.getMemoryMapFlags());
        const int fadviseOptions(_tune._randRead.getAdvise());
        if (frozen()) {
            LOG(debug, "enableRead(): MMapRandRead: file='%s'", _dataFileName.c_str());
            _file = std::make_unique<MMapRandRead>(_dataFileName, mmapFlags, fadviseOptions);
        } else {
            LOG(debug, "enableRead(): MMapRandReadDynamic: file='%s'", _dataFileName.c_str());
            _file = std::make_unique<MMapRandReadDynamic>(_dataFileName, mmapFlags, fadviseOptions);
        }
    } else {
        LOG(debug, "enableRead(): NormalRandRead: file='%s'", _dataFileName.c_str());
        _file = std::make_unique<NormalRandRead>(_dataFileName);
    }
    _dataHeaderLen = readDataHeader(*_file);
    if (_dataHeaderLen == 0u) {
        throw std::runtime_error(make_string("bad file header: %s", _dataFileName.c_str()));
    }
}

size_t FileChunk::adjustSize(size_t sz) {
    return sz + ENTRY_BIAS_SIZE;
}
void
FileChunk::remove(uint32_t lid, uint32_t size)
{
     (void) lid;
     _erasedCount.store(getErasedCount() + 1, std::memory_order_relaxed);
     _erasedBytes.store(getErasedBytes() + adjustSize(size), std::memory_order_relaxed);
}

uint64_t
FileChunk::getLastPersistedSerialNum() const
{
    return _lastPersistedSerialNum.load(std::memory_order_relaxed);
}

vespalib::system_time
FileChunk::getModificationTime() const
{
    return _modificationTime;
}

namespace {

struct FixedParams {
    const IGetLid & db;
    IWriteData & dest;
    const vespalib::GenerationHandler::Guard & lidReadGuard;
    uint32_t fileId;
    IFileChunkVisitorProgress *visitorProgress;
};

void
appendChunks(FixedParams * args, Chunk::UP chunk)
{
    const Chunk::LidList ll(chunk->getUniqueLids());
    for (const Chunk::Entry & e : ll) {
        LidInfo lidInfo(args->fileId, chunk->getId(), e.netSize());
        if (args->db.getLid(args->lidReadGuard, e.getLid()) == lidInfo) {
            auto guard(args->db.getLidGuard(e.getLid()));
            if (args->db.getLid(args->lidReadGuard, e.getLid()) == lidInfo) {
                // I am still in use, so I need to be taken care of.
                vespalib::ConstBufferRef data(chunk->getLid(e.getLid()));
                args->dest.write(std::move(guard), chunk->getId(), e.getLid(), data);
            }
        }
    }
    if (args->visitorProgress != nullptr) {
        args->visitorProgress->updateProgress();
    }
}

}

void
FileChunk::appendTo(vespalib::Executor & executor, const IGetLid & db, IWriteData & dest,
                    uint32_t numChunks, IFileChunkVisitorProgress *visitorProgress,
                    vespalib::CpuUsage::Category cpu_category)
{
    assert(frozen() || visitorProgress);
    vespalib::GenerationHandler::Guard lidReadGuard(db.getLidReadGuard());
    assert(numChunks <= getNumChunks());
    FixedParams fixedParams = {db, dest, lidReadGuard, getFileId().getId(), visitorProgress};
    size_t limit = std::thread::hardware_concurrency();
    vespalib::ArrayQueue<std::future<Chunk::UP>> queue;
    for (size_t chunkId(0); chunkId < numChunks; chunkId++) {
        std::promise<Chunk::UP> promisedChunk;
        std::future<Chunk::UP> futureChunk = promisedChunk.get_future();
        auto task = vespalib::makeLambdaTask([promise = std::move(promisedChunk), chunkId, this]() mutable {
            const ChunkInfo & cInfo(_chunkInfo[chunkId]);
            vespalib::DataBuffer whole(0ul, ALIGNMENT);
            FileRandRead::FSP keepAlive(_file->read(cInfo.getOffset(), whole, cInfo.getSize()));
            promise.set_value(std::make_unique<Chunk>(chunkId, whole.getData(), whole.getDataLen()));
        });
        executor.execute(CpuUsage::wrap(std::move(task), cpu_category));

        while (queue.size() >= limit) {
            appendChunks(&fixedParams, queue.front().get());
            queue.pop();
        }

        queue.push(std::move(futureChunk));
    }
    while ( ! queue.empty() ) {
        appendChunks(&fixedParams, queue.front().get());
        queue.pop();
    }
    dest.close();
}

void
FileChunk::read(LidInfoWithLidV::const_iterator begin, size_t count, IBufferVisitor & visitor) const
{
    if (count == 0) { return; }
    uint32_t prevChunk = begin->getChunkId();
    uint32_t start(0);
    for (size_t i(0); i < count; i++) {
        const LidInfoWithLid & li = *(begin + i);
        if (li.getChunkId() != prevChunk) {
            ChunkInfo ci = _chunkInfo[prevChunk];
            read(begin + start, i - start, ci, visitor);
            prevChunk = li.getChunkId();
            start = i;
        }
    }
    ChunkInfo ci = _chunkInfo[prevChunk];
    read(begin + start, count - start, ci, visitor);
}

void
FileChunk::read(LidInfoWithLidV::const_iterator begin, size_t count, ChunkInfo ci, IBufferVisitor & visitor) const
{
    vespalib::DataBuffer whole(0ul, ALIGNMENT);
    FileRandRead::FSP keepAlive = _file->read(ci.getOffset(), whole, ci.getSize());
    Chunk chunk(begin->getChunkId(), whole.getData(), whole.getDataLen());
    for (size_t i(0); i < count; i++) {
        const LidInfoWithLid & li = *(begin + i);
        vespalib::ConstBufferRef buf = chunk.getLid(li.getLid());
        if (buf.size() != 0) {
            visitor.visit(li.getLid(), buf);
        }
    }
}

ssize_t
FileChunk::read(uint32_t lid, SubChunkId chunkId,
                vespalib::DataBuffer & buffer) const
{
    return (chunkId < _chunkInfo.size())
        ? read(lid, chunkId, _chunkInfo[chunkId], buffer)
        : -1;
}

ssize_t
FileChunk::read(uint32_t lid, SubChunkId chunkId, const ChunkInfo & chunkInfo,
                vespalib::DataBuffer & buffer) const
{
    vespalib::DataBuffer whole(0ul, ALIGNMENT);
    FileRandRead::FSP keepAlive(_file->read(chunkInfo.getOffset(), whole, chunkInfo.getSize()));
    Chunk chunk(chunkId, whole.getData(), whole.getDataLen());
    return chunk.read(lid, buffer);
}

uint64_t
FileChunk::readDataHeader(FileRandRead &datFile)
{
    uint64_t dataHeaderLen(0);
    int64_t fileSize = datFile.getSize();
    uint32_t hl = GenericHeader::getMinSize();
    if (fileSize >= hl) {
        vespalib::DataBuffer h(hl, ALIGNMENT);
        datFile.read(0, h, hl);
        GenericHeader::BufferReader rd(h);
        uint32_t headerLen = GenericHeader::readSize(rd);
        if (headerLen <= fileSize) {
            dataHeaderLen = headerLen;
        }
    }
    return dataHeaderLen;
}


uint64_t
FileChunk::readIdxHeader(FastOS_FileInterface &idxFile, uint32_t &docIdLimit)
{
    int64_t fileSize = idxFile.getSize();
    uint32_t hl = GenericHeader::getMinSize();
    uint64_t idxHeaderLen = 0;
    if (fileSize >= hl) {
        GenericHeader::MMapReader rd(static_cast<const char *> (idxFile.MemoryMapPtr(0)), hl);
        uint32_t headerLen = GenericHeader::readSize(rd);
        if (headerLen <= fileSize) {
            idxHeaderLen = headerLen;
        }
    }
    if (idxHeaderLen == 0u) {
        throw SummaryException("bad file header", idxFile, VESPA_STRLOC);
    }
    GenericHeader::MMapReader reader(static_cast<const char *> (idxFile.MemoryMapPtr(0)), idxHeaderLen);
    GenericHeader header;
    header.read(reader);
    docIdLimit = readDocIdLimit(header);
    return idxHeaderLen;
}

uint32_t
FileChunk::readDocIdLimit(vespalib::GenericHeader &header)
{
    if (header.hasTag(DOC_ID_LIMIT_KEY)) {
        return header.getTag(DOC_ID_LIMIT_KEY).asInteger();
    } else {
        return std::numeric_limits<uint32_t>::max();
    }
}

void
FileChunk::writeDocIdLimit(vespalib::GenericHeader &header, uint32_t docIdLimit)
{
    header.putTag(vespalib::GenericHeader::Tag(DOC_ID_LIMIT_KEY, docIdLimit));
}

void
FileChunk::verify(bool reportOnly) const
{
    (void) reportOnly;
    LOG(info,
        "Verifying file '%s' with fileid '%u'. erased-count='%zu' and erased-bytes='%zu'. diskFootprint='%zu'",
        _name.c_str(), _fileId.getId(), getErasedCount(), getErasedBytes(), _diskFootprint.load(std::memory_order_relaxed));
    uint64_t lastSerial(0);
    size_t chunkId(0);
    bool errorInPrev(false);
    for (const ChunkInfo & ci : _chunkInfo) {
        vespalib::DataBuffer whole(0ul, ALIGNMENT);
        FileRandRead::FSP keepAlive(_file->read(ci.getOffset(), whole, ci.getSize()));
        try {
            Chunk chunk(chunkId++, whole.getData(), whole.getDataLen());
            assert(chunk.getLastSerial() >= lastSerial);
            lastSerial = chunk.getLastSerial();
            if (errorInPrev) {
                LOG(error, "Last serial number in first good chunk is %" PRIu64, chunk.getLastSerial());
                errorInPrev = false;
            }
        } catch (const std::exception & e) {
            LOG(error,
                "Errors in chunk number %zu/%zu at file offset %" PRIu64 " and size %u."
                " Last known good serial number = %" PRIu64 "\n.Got Exception : %s",
                chunkId, _chunkInfo.size(), ci.getOffset(), ci.getSize(), lastSerial, e.what());
            errorInPrev = true;
        }
    }
}

uint32_t
FileChunk::getNumChunks() const
{
    return _chunkInfo.size();
}

size_t
FileChunk::getMemoryFootprint() const
{
    // The memory footprint does not vary before or after flush
    // Once frozen, there is no variable component.
    // It is all captured by getMemoryMetaFootprint()
    return 0;
}   
    
size_t
FileChunk::getMemoryMetaFootprint() const
{
    return sizeof(*this) + _chunkInfo.capacity()*sizeof(ChunkInfoVector::value_type);
}

vespalib::MemoryUsage
FileChunk::getMemoryUsage() const
{
    vespalib::MemoryUsage result;
    result.incAllocatedBytes(sizeof(*this));
    result.incUsedBytes(sizeof(*this));
    result.incAllocatedBytes(_chunkInfo.capacity()*sizeof(ChunkInfoVector::value_type));
    result.incUsedBytes(_chunkInfo.size()*sizeof(ChunkInfoVector::value_type));
    return result;
}

bool
FileChunk::isIdxFileEmpty(const vespalib::string & name)
{
    vespalib::string fileName(name + ".idx");
    FastOS_File idxFile(fileName.c_str());
    idxFile.enableMemoryMap(0);
    if (idxFile.OpenReadOnly()) {
        if (idxFile.IsMemoryMapped()) {
            int64_t fileSize = idxFile.getSize();
            uint32_t docIdLimit = std::numeric_limits<uint32_t>::max();
            int64_t idxHeaderLen = FileChunk::readIdxHeader(idxFile, docIdLimit);
            return fileSize <= idxHeaderLen;
        } else if ( idxFile.getSize() == 0u) {
            return true;
        } else {
            throw SummaryException("Failed opening idx file for memorymapping", idxFile, VESPA_STRLOC);
        }
    } else {
        throw SummaryException("Failed opening idx file readonly ", idxFile, VESPA_STRLOC);
    }
}

void
FileChunk::eraseIdxFile(const vespalib::string & name)
{
    vespalib::string fileName(createIdxFileName(name));
    std::filesystem::remove(std::filesystem::path(fileName));
}

void
FileChunk::eraseDatFile(const vespalib::string & name)
{
    vespalib::string fileName(createDatFileName(name));
    std::filesystem::remove(std::filesystem::path(fileName));
}


DataStoreFileChunkStats
FileChunk::getStats() const
{
    uint64_t diskFootprint = getDiskFootprint();
    uint64_t diskBloat = getDiskBloat();
    double bucketSpread = getBucketSpread();
    uint64_t serialNum = getLastPersistedSerialNum();
    uint32_t docIdLimit = getDocIdLimit();
    uint64_t nameId = getNameId().getId();
    return {diskFootprint, diskBloat, bucketSpread, serialNum, serialNum, docIdLimit, nameId};
}

} // namespace search