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

#include <vespa/storage/common/cluster_context.h>
#include <vespa/storage/common/messagesender.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/stllike/hash_map.h>

namespace storage::api {
    class BucketCommand;
    class BucketReply;
}

namespace storage::distributor {

class MessageTracker {
public:
    class ToSend {
    public:
        ToSend(std::shared_ptr<api::BucketCommand> msg, uint16_t target) noexcept
            : _msg(std::move(msg)), _target(target)
        {}

        std::shared_ptr<api::BucketCommand> _msg;
        uint16_t _target;
    };

    explicit MessageTracker(const ClusterContext& cluster_context);
    MessageTracker(MessageTracker&&) noexcept = default;
    MessageTracker& operator=(MessageTracker&&) noexcept = delete;
    MessageTracker(const MessageTracker &) = delete;
    MessageTracker& operator=(const MessageTracker&) = delete;
    ~MessageTracker();

    void queueCommand(std::shared_ptr<api::BucketCommand> msg, uint16_t target) {
        _commandQueue.emplace_back(std::move(msg), target);
    }
    void reserve_more_commands(size_t sz) {
        _commandQueue.reserve(_commandQueue.size() + sz);
    }

    void flushQueue(MessageSender& sender);

    /**
       If the reply is for a message that is being tracked here, returns the node the message was sent to. If not, returns (uint16_t)-1
    */
    uint16_t handleReply(api::BucketReply& reply);

    /**
       Returns true if all messages sent have been received.
    */
    bool finished() const noexcept {
        return _sentMessages.empty();
    }

protected:
    std::vector<ToSend>                    _commandQueue;
    // Keeps track of which node a message was sent to.
    vespalib::hash_map<uint64_t, uint16_t> _sentMessages;
    const ClusterContext&                  _cluster_ctx;
};

}