aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/frt/frtconnectionpool.h
blob: 2356af672c52803794808182e557afe0d14da28e (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
109
110
111
112
113
114
115
116
117
118
119
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "frtconnection.h"
#include "connectionfactory.h"
#include <vespa/config/subscription/sourcespec.h>
#include <vector>
#include <map>

class FNET_Transport;

namespace config {

class FRTConnectionPool : public ConnectionFactory {
private:

    /**
     * This class makes it possible to iterate over the entries in the
     * connections map in the order they were inserted. Used to keep
     * consistency with the Java version that uses LinkedHashMap.
     */
    class FRTConnectionKey {
    private:
        int _idx;
        vespalib::string _hostname;
    public:
        FRTConnectionKey() : FRTConnectionKey(0, "") {}
        FRTConnectionKey(int idx, const vespalib::string& hostname);
        int operator<(const FRTConnectionKey& right) const;
        int operator==(const FRTConnectionKey& right) const;
    };

    std::unique_ptr<FRT_Supervisor> _supervisor;
    int _selectIdx;
    vespalib::string _hostname;
    using ConnectionMap = std::map<FRTConnectionKey, FRTConnection::SP>;
    ConnectionMap _connections;

public:
    FRTConnectionPool(FNET_Transport & transport, const ServerSpec & spec, const TimingValues & timingValues);
    FRTConnectionPool(const FRTConnectionPool&) = delete;
    FRTConnectionPool& operator=(const FRTConnectionPool&) = delete;
    ~FRTConnectionPool() override;

    void syncTransport() override;

    /**
     * Sets the hostname to the host where this program is running.
     */
    void setHostname();

    /**
     * Sets the hostname.
     *
     * @param hostname the hostname
     */
    void setHostname(const vespalib::string & hostname) { _hostname = hostname; }

    FNET_Scheduler * getScheduler() override;

    /**
     * Returns the current FRTConnection instance, taken from the list of error-free sources.
     * If no sources are error-free, an instance from the list of sources with errors
     * is returned.
     *
     * @return The next FRTConnection instance in the list.
     */
    Connection* getCurrent() override;

    /**
     * Returns the next FRTConnection instance from the list of error-free sources in a round robin
     * fashion. If no sources are error-free, an instance from the list of sources with errors
     * is returned.
     *
     * @return The next FRTConnection instance in the list.
     */
    FRTConnection* getNextRoundRobin();

    /**
     * Returns the current FRTConnection instance from the list of error-free sources, based on the
     * hostname where this program is currently running. If no sources are error-free, an instance
     * from the list of sources with errors is returned.
     *
     * @return The next FRTConnection instance in the list.
     */
    FRTConnection* getNextHashBased();

    /**
     * Gets list of sources that are not suspended.
     *
     * @return list of FRTConnection pointers
     */
    std::vector<FRTConnection*> getReadySources() const;

    /**
     * Gets list of sources that are suspended.
     *
     * @param suspendedSources is list of FRTConnection pointers
     */
    std::vector<FRTConnection*> getSuspendedSources() const;
};

class FRTConnectionPoolWithTransport : public ConnectionFactory {
public:
    FRTConnectionPoolWithTransport(std::unique_ptr<FNET_Transport> transport,
                                   const ServerSpec & spec, const TimingValues & timingValues);
    FRTConnectionPoolWithTransport(const FRTConnectionPoolWithTransport&) = delete;
    FRTConnectionPoolWithTransport& operator=(const FRTConnectionPoolWithTransport&) = delete;
    ~FRTConnectionPoolWithTransport() override;
    FNET_Scheduler * getScheduler() override { return _connectionPool->getScheduler(); }
    void syncTransport() override { _connectionPool->syncTransport(); }
    Connection* getCurrent() override { return _connectionPool->getCurrent(); }
private:
    std::unique_ptr<FNET_Transport>    _transport;
    std::unique_ptr<FRTConnectionPool> _connectionPool;
};

} // namespace config