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

#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/time.h>
#include <memory>
#include <atomic>

namespace proton {

class IBlockableMaintenanceJob;
class IMaintenanceJobRunner;
struct DocumentDBTaggedMetrics;

/**
 * Interface for a maintenance job that is executed after "delay" seconds and
 * then every "interval" seconds.
 */
class IMaintenanceJob
{
private:
    const vespalib::string     _name;
    const vespalib::duration   _delay;
    const vespalib::duration   _interval;
    std::atomic<bool>          _stopped;
protected:
    virtual void onStop() = 0;
public:
    using UP = std::unique_ptr<IMaintenanceJob>;
    using SP = std::shared_ptr<IMaintenanceJob>;

    IMaintenanceJob(const vespalib::string &name,
                    vespalib::duration delay,
                    vespalib::duration interval)
        : _name(name),
          _delay(delay),
          _interval(interval),
          _stopped(false)
    {}

    virtual ~IMaintenanceJob() = default;

    virtual const vespalib::string &getName() const { return _name; }
    virtual vespalib::duration getDelay() const { return _delay; }
    virtual vespalib::duration getInterval() const { return _interval; }
    virtual bool isBlocked() const { return false; }
    virtual IBlockableMaintenanceJob *asBlockable() { return nullptr; }
    virtual void updateMetrics(DocumentDBTaggedMetrics &) const {}
    void stop() {
        _stopped = true;
        onStop();
    }
    bool stopped() const { return _stopped.load(std::memory_order_relaxed); }
    /**
     * Register maintenance job runner, in case event passed to the
     * job causes it to want to be run again.
     */
    virtual void registerRunner(IMaintenanceJobRunner *runner) {
        (void) runner;
    }

    /**
     * Run this maintenance job every "interval" seconds in an external executor thread.
     * It is first executed after "delay" seconds.
     *
     * Return true if the job was finished (it will be executed again in "interval" seconds).
     * Return false if the job was not finished and need to be executed again immediately. This
     * should be used to split up a large job to avoid starvation of other tasks that also are
     * executed on the external executor thread.
     */
    virtual bool run() = 0;
};

} // namespace proton