aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/lid_space_compaction_job.h
blob: 5254c2696c3efa13cfa19a6a7c31a8bed1c6efa6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "blockable_maintenance_job.h"
#include "document_db_maintenance_config.h"
#include "i_disk_mem_usage_listener.h"
#include "iclusterstatechangedhandler.h"
#include <vespa/document/bucket/bucketspace.h>
#include <vespa/searchlib/common/idocumentmetastore.h>
#include <vespa/vespalib/util/retain_guard.h>
#include <atomic>

namespace storage::spi { struct BucketExecutor; }
namespace searchcorespi::index { struct IThreadService; }
namespace vespalib { class IDestructorCallback; }
namespace proton {
    class MoveOperation;
    class IDiskMemUsageNotifier;
    class IClusterStateChangedNotifier;
    struct IOperationStorer;
    struct ILidSpaceCompactionHandler;
    struct IDocumentScanIterator;
    class RemoveOperationsRateTracker;
}

namespace proton::lidspace {

/**
 * Moves documents from higher lids to lower lids. It uses a BucketExecutor that ensures that the bucket
 * is locked for changes while the document is moved.
 */
class CompactionJob : public BlockableMaintenanceJob,
                      public IDiskMemUsageListener,
                      public IClusterStateChangedHandler,
                      public std::enable_shared_from_this<CompactionJob>
{
private:
    using BucketExecutor = storage::spi::BucketExecutor;
    using IDestructorCallback = vespalib::IDestructorCallback;
    using IThreadService = searchcorespi::index::IThreadService;
    const DocumentDBLidSpaceCompactionConfig      _cfg;
    std::shared_ptr<ILidSpaceCompactionHandler>   _handler;
    IOperationStorer                             &_opStorer;
    std::unique_ptr<IDocumentScanIterator>        _scanItr;
    IDiskMemUsageNotifier                        &_diskMemUsageNotifier;
    IClusterStateChangedNotifier                 &_clusterStateChangedNotifier;
    std::shared_ptr<RemoveOperationsRateTracker>  _ops_rate_tracker;
    bool                                          _is_disabled;
    bool                                          _shouldCompactLidSpace;
    IThreadService                               &_master;
    BucketExecutor                               &_bucketExecutor;
    vespalib::RetainGuard                         _dbRetainer;
    document::BucketSpace                         _bucketSpace;

    bool hasTooMuchLidBloat(const search::LidUsageStats &stats) const;
    bool shouldRestartScanDocuments(const search::LidUsageStats &stats) const;
    void compactLidSpace(const search::LidUsageStats &stats);
    bool remove_batch_is_ongoing() const;
    bool remove_is_ongoing() const;
    search::DocumentMetaData getNextDocument(const search::LidUsageStats &stats);

    bool scanDocuments(const search::LidUsageStats &stats);
    static void moveDocument(std::shared_ptr<CompactionJob> job, const search::DocumentMetaData & metaThen,
                             std::shared_ptr<IDestructorCallback> onDone);
    void completeMove(const search::DocumentMetaData & metaThen, std::unique_ptr<MoveOperation> moveOp,
                      std::shared_ptr<IDestructorCallback> onDone);
    class MoveTask;

    CompactionJob(const DocumentDBLidSpaceCompactionConfig &config,
                  vespalib::RetainGuard dbRetainer,
                  std::shared_ptr<ILidSpaceCompactionHandler> handler,
                  IOperationStorer &opStorer,
                  IThreadService & master,
                  BucketExecutor & bucketExecutor,
                  IDiskMemUsageNotifier &diskMemUsageNotifier,
                  const BlockableMaintenanceJobConfig &blockableConfig,
                  IClusterStateChangedNotifier &clusterStateChangedNotifier,
                  bool nodeRetired,
                  document::BucketSpace bucketSpace);
public:
    static std::shared_ptr<CompactionJob>
    create(const DocumentDBLidSpaceCompactionConfig &config,
           vespalib::RetainGuard dbRetainer,
           std::shared_ptr<ILidSpaceCompactionHandler> handler,
           IOperationStorer &opStorer,
           IThreadService & master,
           BucketExecutor & bucketExecutor,
           IDiskMemUsageNotifier &diskMemUsageNotifier,
           const BlockableMaintenanceJobConfig &blockableConfig,
           IClusterStateChangedNotifier &clusterStateChangedNotifier,
           bool nodeRetired,
           document::BucketSpace bucketSpace);
    ~CompactionJob() override;
    void notifyDiskMemUsage(DiskMemUsageState state) override;
    void notifyClusterStateChanged(const std::shared_ptr<IBucketStateCalculator> &newCalc) override;
    bool run() override;
};

} // namespace proton