summaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/rpc_client/client_impl.cpp
blob: 16744e061d540f454f0e8089581cd1c4a496fba9 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "raw_client.h"

#include <yt/cpp/mapreduce/client/client.h>
#include <yt/cpp/mapreduce/client/init.h>

#include <yt/cpp/mapreduce/common/retry_lib.h>

#include <yt/cpp/mapreduce/interface/client_method_options.h>
#include <yt/cpp/mapreduce/interface/logging/yt_log.h>
#include <yt/cpp/mapreduce/interface/tvm.h>

#include <yt/yt/client/api/rpc_proxy/config.h>
#include <yt/yt/client/api/rpc_proxy/connection.h>

namespace NYT::NDetail {

////////////////////////////////////////////////////////////////////////////////

struct TConnectionCacheKey
{
public:
    TMaybe<TString> JobProxySocketPath;
    TString ClusterUrl;
    TMaybe<TString> RpcProxyRole;
    TMaybe<TString> ProxyAddress;

    // N.B. we want to compute hash of this struct, so we use sorted map.
    TMap<TString, TString> ProxyUrlAliasingRules;

public:
    TConnectionCacheKey() = default;

    TConnectionCacheKey(const TClientContext& context)
        : JobProxySocketPath(context.JobProxySocketPath)
        , ClusterUrl(context.ServerName)
        , RpcProxyRole(context.RpcProxyRole)
        , ProxyAddress(context.ProxyAddress)
        , ProxyUrlAliasingRules(context.Config->ProxyUrlAliasingRules.begin(), context.Config->ProxyUrlAliasingRules.end())
    { }

    bool operator==(const TConnectionCacheKey& other) const = default;
};


////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NDetail

template <>
struct THash<NYT::NDetail::TConnectionCacheKey>
{
    size_t operator()(const NYT::NDetail::TConnectionCacheKey& key) const
    {
        using NYT::HashCombine;
        size_t result = 0;
        HashCombine(result, key.JobProxySocketPath);
        HashCombine(result, key.ClusterUrl);
        HashCombine(result, key.RpcProxyRole);
        HashCombine(result, key.ProxyAddress);
        for (const auto& [k, v] : key.ProxyUrlAliasingRules) {
            HashCombine(result, k);
            HashCombine(result, v);
        }
        return result;
    }
};

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

////////////////////////////////////////////////////////////////////////////////

NApi::IConnectionPtr GetOrCreateConnection(const TConnectionCacheKey& key)
{
    static TMutex lock;
    static THashMap<TConnectionCacheKey, TWeakPtr<NApi::IConnection>> cache;
    static size_t maxCacheSize = 32;

    auto g = Guard(lock);

    auto it = cache.find(key);
    if (it != cache.end()) {
        auto connection = it->second.Lock();
        if (connection) {
            return connection;
        }
    }

    auto connectionConfig = New<NApi::NRpcProxy::TConnectionConfig>();
    connectionConfig->SetDefaults();
    if (key.JobProxySocketPath) {
        connectionConfig->ProxyUnixDomainSocket = *key.JobProxySocketPath;
        connectionConfig->EnableProxyDiscovery = false;
    } else {
        connectionConfig->ClusterUrl = key.ClusterUrl;
    }
    if (key.RpcProxyRole) {
        connectionConfig->ProxyRole = *key.RpcProxyRole;
    }
    if (key.ProxyAddress) {
        connectionConfig->ProxyAddresses = {*key.ProxyAddress};
    }

    for (const auto& [clusterName, url] : key.ProxyUrlAliasingRules) {
        connectionConfig->ProxyUrlAliasingRules.emplace(clusterName, url);
    }

    auto connection = NApi::NRpcProxy::CreateConnection(connectionConfig);

    if (it != cache.end()) {
        it->second = connection;
    } else if (cache.size() < maxCacheSize) {
        // N.B. once cache size exceeds limit we disable caching.
        // We believe such cases are rare but if it is the case we don't want to leak memory
        // (memory is leaked since we never clear the cache).
        cache.emplace(key, connection);
    } else {
        YT_LOG_WARNING("Cannot cache IConnection since connection cache reached maximum size");
    }

    return connection;
}

NYT::NApi::IClientPtr CreateApiClient(const TClientContext& context)
{
    auto key = TConnectionCacheKey(context);
    auto connection = GetOrCreateConnection(key);

    NApi::TClientOptions clientOptions;
    clientOptions.Token = context.Token;
    if (context.ServiceTicketAuth) {
        clientOptions.ServiceTicketAuth = context.ServiceTicketAuth->Ptr;
    }
    if (context.ImpersonationUser) {
        clientOptions.User = *context.ImpersonationUser;
    }
    if (context.JobProxySocketPath) {
        clientOptions.MultiproxyTargetCluster = context.MultiproxyTargetCluster;
    }

    return connection->CreateClient(clientOptions);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

IClientPtr CreateRpcClient(
    const TString& serverName,
    const TCreateClientOptions& options)
{
    auto context = NDetail::CreateClientContext(serverName, options);

    auto globalTxId = GetGuid(context.Config->GlobalTxId);

    auto retryConfigProvider = options.RetryConfigProvider_;
    if (!retryConfigProvider) {
        retryConfigProvider = CreateDefaultRetryConfigProvider();
    }

    NDetail::EnsureInitialized();

    auto rawClient = MakeIntrusive<NDetail::TRpcRawClient>(
        NDetail::CreateApiClient(context),
        context.Config);

    return new NDetail::TClient(
        std::move(rawClient),
        context,
        globalTxId,
        CreateDefaultClientRetryPolicy(retryConfigProvider, context.Config));
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT