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

#include "blockable_maintenance_job.h"
#include "disk_mem_usage_state.h"
#include "imaintenancejobrunner.h"
#include "document_db_maintenance_config.h"
#include "move_operation_limiter.h"

namespace proton {

void
BlockableMaintenanceJob::updateBlocked(const LockGuard &)
{
    _blocked = !_blockReasons.empty();
}

void
BlockableMaintenanceJob::internalNotifyDiskMemUsage(const DiskMemUsageState &state)
{
    bool resourcesOK = !state.aboveDiskLimit(_resourceLimitFactor) && !state.aboveMemoryLimit(_resourceLimitFactor);
    if (resourcesOK) {
        if (isBlocked(BlockedReason::RESOURCE_LIMITS)) {
            unBlock(BlockedReason::RESOURCE_LIMITS);
        }
    } else {
        setBlocked(BlockedReason::RESOURCE_LIMITS);
    }
}

BlockableMaintenanceJob::BlockableMaintenanceJob(const vespalib::string &name,
                                                 vespalib::duration delay,
                                                 vespalib::duration interval)
    : BlockableMaintenanceJob(name, delay, interval, BlockableMaintenanceJobConfig())
{
}

BlockableMaintenanceJob::BlockableMaintenanceJob(const vespalib::string &name,
                                                 vespalib::duration delay,
                                                 vespalib::duration interval,
                                                 const BlockableMaintenanceJobConfig &config)
    : IBlockableMaintenanceJob(name, delay, interval),
      _mutex(),
      _blockReasons(),
      _blocked(false),
      _runner(nullptr),
      _resourceLimitFactor(config.getResourceLimitFactor()),
      _moveOpsLimiter(std::make_shared<MoveOperationLimiter>(this, config.getMaxOutstandingMoveOps()))
{
}

BlockableMaintenanceJob::~BlockableMaintenanceJob()
{
    dynamic_cast<MoveOperationLimiter &>(*_moveOpsLimiter).clearJob();
}

bool
BlockableMaintenanceJob::isBlocked(BlockedReason reason)
{
    LockGuard guard(_mutex);
    return (_blockReasons.find(reason) != _blockReasons.end());
}

void
BlockableMaintenanceJob::considerRun()
{
    if (_runner && !isBlocked()) {
        _runner->run();
    }
}

void
BlockableMaintenanceJob::setBlocked(BlockedReason reason)
{
    LockGuard guard(_mutex);
    _blockReasons.insert(reason);
    updateBlocked(guard);
}

void
BlockableMaintenanceJob::unBlock(BlockedReason reason)
{
    bool considerRun = false;
    {
        LockGuard guard(_mutex);
        _blockReasons.erase(reason);
        updateBlocked(guard);
        considerRun = !_blocked;
    }
    if (_runner && considerRun) {
        _runner->run();
    }
}

void
BlockableMaintenanceJob::onStop()
{
    LockGuard guard(_mutex);
    _runner = nullptr;
}

bool
BlockableMaintenanceJob::isBlocked() const
{
    LockGuard guard(_mutex);
    return _blocked;
}

}