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

#pragma once

#include "maintenancedocumentsubdb.h"
#include "i_maintenance_job.h"
#include <vespa/searchcore/proton/common/doctypename.h>
#include <vespa/vespalib/util/retain_guard.h>
#include <mutex>

class FNET_Transport;

namespace vespalib {

class Executor;
class MonitoredRefCount;
class Timer;

}
namespace searchcorespi::index {
    struct IThreadService;
    struct ISyncableThreadService;
}

namespace proton {

class MaintenanceJobRunner;
class IScheduledExecutor;

/**
 * Class that controls the bucket moving between ready and notready sub databases
 * and a set of maintenance jobs for a document db.
 * The maintenance jobs are independent of the controller.
 */
class MaintenanceController
{
public:
    using IThreadService = searchcorespi::index::IThreadService;
    using ISyncableThreadService = searchcorespi::index::ISyncableThreadService;
    using JobList = std::vector<std::shared_ptr<MaintenanceJobRunner>>;
    using UP = std::unique_ptr<MaintenanceController>;
    enum class State {INITIALIZING, STARTED, PAUSED, STOPPING};

    MaintenanceController(FNET_Transport & transport, ISyncableThreadService& masterThread,
                          vespalib::MonitoredRefCount& refCount, const DocTypeName& docTypeName);

    ~MaintenanceController();
    void registerJob(IMaintenanceJob::UP job);

    void killJobs();

    JobList getJobList() const {
        Guard guard(_jobsLock);
        return _jobs;
    }

    void stop();
    void start();
    void newConfig();
    void updateMetrics(DocumentDBTaggedMetrics & metrics);

    void
    syncSubDBs(const MaintenanceDocumentSubDB &readySubDB,
               const MaintenanceDocumentSubDB &remSubdB,
               const MaintenanceDocumentSubDB &notReadySubDB);

    void kill();

    bool  getStarted() const { return _state >= State::STARTED; }
    bool getStopping() const { return _state == State::STOPPING; }
    bool getPaused() const { return _state == State::PAUSED; }

    const MaintenanceDocumentSubDB &    getReadySubDB() const { return _readySubDB; }
    const MaintenanceDocumentSubDB &      getRemSubDB() const { return _remSubDB; }
    const MaintenanceDocumentSubDB & getNotReadySubDB() const { return _notReadySubDB; }
    IThreadService & masterThread();
    const DocTypeName & getDocTypeName() const { return _docTypeName; }
    vespalib::RetainGuard retainDB() { return vespalib::RetainGuard(_refCount); }
private:
    using Mutex = std::mutex;
    using Guard = std::lock_guard<Mutex>;
    using TaskHandle = std::unique_ptr<vespalib::IDestructorCallback>;

    ISyncableThreadService              &_masterThread;
    vespalib::MonitoredRefCount         &_refCount;
    MaintenanceDocumentSubDB             _readySubDB;
    MaintenanceDocumentSubDB             _remSubDB;
    MaintenanceDocumentSubDB             _notReadySubDB;
    std::unique_ptr<IScheduledExecutor>  _periodicTimer;
    std::vector<TaskHandle>              _periodicTaskHandles;
    State                                _state;
    const DocTypeName                   &_docTypeName;
    JobList                              _jobs;
    mutable Mutex                        _jobsLock;

    void addJobsToPeriodicTimer();
    void restart();
    void performHoldJobs(JobList jobs);
};

} // namespace proton