aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/ISchedulerConstraint.h
blob: 47f6905e2658efa7b25a427e87d50c8bb8c4d553 (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
#pragma once

#include <IO/ISchedulerNode.h>

namespace DB
{

/*
 * Constraint defined on the set of requests in consumption state.
 * It allows to track two events:
 *  - dequeueRequest(): resource consumption begins
 *  - finishRequest(): resource consumption finishes
 * This allows to keep track of in-flight requests and implement different constraints (e.g. in-flight limit).
 * When constraint is violated, node must be deactivated by dequeueRequest() returning `false`.
 * When constraint is again satisfied, scheduleActivation() is called from finishRequest().
 *
 * Derived class behaviour requirements:
 *  - dequeueRequest() must fill `request->constraint` iff it is nullptr;
 *  - finishRequest() must be recursive: call to `parent_constraint->finishRequest()`.
 */
class ISchedulerConstraint : public ISchedulerNode
{
public:
    ISchedulerConstraint(EventQueue * event_queue_, const Poco::Util::AbstractConfiguration & config = emptyConfig(), const String & config_prefix = {})
        : ISchedulerNode(event_queue_, config, config_prefix)
    {}

    /// Resource consumption by `request` is finished.
    /// Should be called outside of scheduling subsystem, implementation must be thread-safe.
    virtual void finishRequest(ResourceRequest * request) = 0;

    void setParent(ISchedulerNode * parent_) override
    {
        ISchedulerNode::setParent(parent_);

        // Assign `parent_constraint` to the nearest parent derived from ISchedulerConstraint
        for (ISchedulerNode * node = parent_; node != nullptr; node = node->parent)
        {
            if (auto * constraint = dynamic_cast<ISchedulerConstraint *>(node))
            {
                parent_constraint = constraint;
                break;
            }
        }
    }

protected:
    // Reference to nearest parent that is also derived from ISchedulerConstraint.
    // Request can traverse through multiple constraints while being dequeue from hierarchy,
    // while finishing request should traverse the same chain in reverse order.
    // NOTE: it must be immutable after initialization, because it is accessed in not thread-safe way from finishRequest()
    ISchedulerConstraint * parent_constraint = nullptr;
};

}