aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/maintenancecontroller.h
blob: bddd58a349aba09d8a864f6e99ac4e35c649dfe2 (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
// Copyright Yahoo. 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/searchcore/proton/common/monitored_refcount.h>
#include <vespa/vespalib/util/scheduledexecutor.h>
#include <mutex>


namespace vespalib {
    class Timer;
    class Executor;
}
namespace searchcorespi::index { struct IThreadService; }

namespace proton {

class MaintenanceJobRunner;
class DocumentDBMaintenanceConfig;
class MonitoredRefCount;

/**
 * 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 DocumentDBMaintenanceConfigSP = std::shared_ptr<DocumentDBMaintenanceConfig>;
    using JobList = std::vector<std::shared_ptr<MaintenanceJobRunner>>;
    using UP = std::unique_ptr<MaintenanceController>;
    enum class State {INITIALIZING, STARTED, PAUSED, STOPPING};

    MaintenanceController(IThreadService &masterThread, vespalib::Executor & defaultExecutor, MonitoredRefCount & refCount, const DocTypeName &docTypeName);

    ~MaintenanceController();
    void registerJobInMasterThread(IMaintenanceJob::UP job);
    void registerJobInDefaultPool(IMaintenanceJob::UP job);

    void killJobs();

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

    void stop();
    void start(const DocumentDBMaintenanceConfigSP &config);
    void newConfig(const DocumentDBMaintenanceConfigSP &config);
    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() { return _masterThread; }
    const DocTypeName & getDocTypeName() const { return _docTypeName; }
    RetainGuard retainDB() { return RetainGuard(_refCount); }
private:
    using Mutex = std::mutex;
    using Guard = std::lock_guard<Mutex>;

    IThreadService                   &_masterThread;
    vespalib::Executor               &_defaultExecutor;
    MonitoredRefCount                &_refCount;
    MaintenanceDocumentSubDB          _readySubDB;
    MaintenanceDocumentSubDB          _remSubDB;
    MaintenanceDocumentSubDB          _notReadySubDB;
    std::unique_ptr<vespalib::ScheduledExecutor>  _periodicTimer;
    DocumentDBMaintenanceConfigSP     _config;
    State                             _state;
    const DocTypeName                &_docTypeName;
    JobList                           _jobs;
    mutable Mutex                     _jobsLock;

    void addJobsToPeriodicTimer();
    void restart();
    void performHoldJobs(JobList jobs);
    void registerJob(vespalib::Executor & executor, IMaintenanceJob::UP job);
};

} // namespace proton