aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/throttlingoperationstarter.h
blob: 8b6ade7e7d1502a4cd97b25e50ef3a6e59802df1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "operationstarter.h"
#include <vespa/storage/distributor/maintenance/pending_window_checker.h>
#include <vespa/vespalib/util/hdr_abort.h>
#include <vespa/storage/distributor/operations/operation.h>

namespace storage::distributor {

class ThrottlingOperationStarter : public OperationStarter, public PendingWindowChecker
{
    class ThrottlingOperation : public Operation
    {
    public:
        ThrottlingOperation(Operation::SP operation,
                            ThrottlingOperationStarter& operationStarter)
            : _operation(std::move(operation)),
              _operationStarter(operationStarter)
        {}

        ~ThrottlingOperation() override;
    private:
        Operation::SP _operation;
        ThrottlingOperationStarter& _operationStarter;

        ThrottlingOperation(const ThrottlingOperation&);
        ThrottlingOperation& operator=(const ThrottlingOperation&);
        
        void onClose(DistributorStripeMessageSender& sender) override {
            _operation->onClose(sender);
        }
        [[nodiscard]] const char* getName() const noexcept override {
            return _operation->getName();
        }
        [[nodiscard]] std::string getStatus() const override {
            return _operation->getStatus();
        }
        [[nodiscard]] std::string toString() const override {
            return _operation->toString();
        }
        void start(DistributorStripeMessageSender& sender, vespalib::system_time startTime) override {
            _operation->start(sender, startTime);
        }
        void receive(DistributorStripeMessageSender& sender, const std::shared_ptr<api::StorageReply> & msg) override {
            _operation->receive(sender, msg);
        }
        void onStart(DistributorStripeMessageSender&) override {
            // Should never be called directly on the throttled operation
            // instance, but rather on its wrapped implementation.
            HDR_ABORT("should not be reached");
        }
        void onReceive(DistributorStripeMessageSender&,
                       const std::shared_ptr<api::StorageReply>&) override {
            HDR_ABORT("should not be reached");
        }
    };

    OperationStarter& _starterImpl;
public:
    explicit ThrottlingOperationStarter(OperationStarter& starterImpl)
        : _starterImpl(starterImpl),
          _minPending(0),
          _maxPending(UINT32_MAX),
          _pendingCount(0)
    {}
    ~ThrottlingOperationStarter() override;

    bool start(const std::shared_ptr<Operation>& operation, Priority priority) override;

    [[nodiscard]] bool may_allow_operation_with_priority(Priority priority) const noexcept override;

    [[nodiscard]] bool canStart(uint32_t currentOperationCount, Priority priority) const;

    void setMaxPendingRange(uint32_t minPending, uint32_t maxPending) {
        _minPending = minPending;
        _maxPending = maxPending;
    }

private:
    ThrottlingOperationStarter(const ThrottlingOperationStarter&);
    ThrottlingOperationStarter& operator=(const ThrottlingOperationStarter&);

    friend class ThrottlingOperation;
    void signalOperationFinished(const Operation& op);

    uint32_t _minPending;
    uint32_t _maxPending;
    uint32_t _pendingCount;
};

}