Skip to content

Commit ec9e264

Browse files
author
don-dron
committed
feat userver/ydb: add keepalive options to YDB driver config
Exposes `tcp-keepalive`, `grpc-keepalive-timeout`, and `grpc-keepalive-permit-without-calls `from ydb-cpp-sdk via @ref ydb::Component static config and NYdb::TDriverConfig. When tcp-keepalive is set, all subkeys are required. commit_hash:26073b3ccb69c06134537ef67ec84a07baeb379a
1 parent 5a9e707 commit ec9e264

4 files changed

Lines changed: 89 additions & 15 deletions

File tree

ydb/src/ydb/component.yaml

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,49 @@ properties:
6767
network-threads-num:
6868
type: integer
6969
minimum: 1
70-
default: 2
7170
description: |
72-
Number of gRPC worker threads in YDB C++ SDK (NYdb::TDriverConfig::SetNetworkThreadsNum).
73-
Defaults to the SDK default (2).
71+
NYdb::TDriverConfig::SetNetworkThreadsNum. If omitted, not set (SDK default).
7472
client-threads-num:
7573
type: integer
7674
minimum: 0
77-
default: 0
7875
description: |
79-
Number of threads for async user callbacks in YDB C++ SDK (NYdb::TDriverConfig::SetClientThreadsNum).
80-
0 means adaptive thread pool (SDK default).
76+
NYdb::TDriverConfig::SetClientThreadsNum. If omitted, not set (SDK default).
77+
tcp-keepalive:
78+
type: object
79+
additionalProperties: false
80+
description: |
81+
NYdb::TDriverConfig::SetTcpKeepAliveSettings. If the whole block is omitted, not set (SDK default).
82+
Use 0 for idle/count/interval on Linux to mean OS default.
83+
properties:
84+
enabled:
85+
type: boolean
86+
description: enable TCP keepalive on gRPC sockets (Linux; if omitted in a partial block, parser default
87+
applies)
88+
idle-sec:
89+
type: integer
90+
minimum: 0
91+
description: seconds idle before first keepalive probe (0 = OS default on Linux)
92+
probe-count:
93+
type: integer
94+
minimum: 0
95+
description: unacknowledged probes before drop (0 = OS default on Linux)
96+
interval-sec:
97+
type: integer
98+
minimum: 0
99+
description: seconds between probes (0 = OS default on Linux)
100+
required:
101+
- enabled
102+
- idle-sec
103+
- probe-count
104+
- interval-sec
105+
grpc-keepalive-timeout:
106+
type: string
107+
description: |
108+
Duration string, e.g. 10s. NYdb::TDriverConfig::SetGRpcKeepAliveTimeout. If omitted, not set (SDK default).
109+
grpc-keepalive-permit-without-calls:
110+
type: boolean
111+
description: |
112+
NYdb::TDriverConfig::SetGRpcKeepAlivePermitWithoutCalls. If omitted, not set (SDK default).
81113
prefer_local_dc:
82114
type: boolean
83115
default: true

ydb/src/ydb/impl/config.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,21 @@ DriverSettings ParseDriverSettings(
6767
auto config_database = dbconfig["database"].As<std::optional<std::string>>();
6868

6969
result.prefer_local_dc = dbconfig["prefer_local_dc"].As<bool>(result.prefer_local_dc);
70-
result.network_threads_num = dbconfig["network-threads-num"].As<std::size_t>(result.network_threads_num);
71-
result.client_threads_num = dbconfig["client-threads-num"].As<std::size_t>(result.client_threads_num);
70+
result.network_threads_num = dbconfig["network-threads-num"].As<std::optional<std::size_t>>();
71+
result.client_threads_num = dbconfig["client-threads-num"].As<std::optional<std::size_t>>();
72+
73+
if (!dbconfig["tcp-keepalive"].IsMissing()) {
74+
const auto tcp = dbconfig["tcp-keepalive"];
75+
result.tcp_keepalive = TcpKeepaliveSettings{
76+
tcp["enabled"].As<bool>(),
77+
tcp["idle-sec"].As<std::size_t>(),
78+
tcp["probe-count"].As<std::size_t>(),
79+
tcp["interval-sec"].As<std::size_t>(),
80+
};
81+
}
82+
result.grpc_keepalive_timeout = dbconfig["grpc-keepalive-timeout"].As<std::optional<std::chrono::milliseconds>>();
83+
result.grpc_keepalive_permit_without_calls =
84+
dbconfig["grpc-keepalive-permit-without-calls"].As<std::optional<bool>>();
7285

7386
result.endpoint = MergeWithSecdist(dbsecdist.endpoint, std::move(config_endpoint), dbconfig, "endpoint");
7487
result.database = MergeWithSecdist(dbsecdist.database, std::move(config_database), dbconfig, "database");
@@ -85,8 +98,6 @@ DriverSettings ParseDriverSettings(
8598
return result;
8699
}
87100

88-
using OptMs = std::optional<std::chrono::milliseconds>;
89-
90101
const dynamic_config::Key<std::unordered_map<std::string, utils::RetryBudgetSettings>> kRetryBudgetSettings(
91102
"YDB_RETRY_BUDGET",
92103
dynamic_config::DefaultAsJsonString("{}")

ydb/src/ydb/impl/config.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,24 @@ struct TableSettings {
3737

3838
struct TopicSettings {};
3939

40+
struct TcpKeepaliveSettings {
41+
bool enabled;
42+
std::size_t idle_sec;
43+
std::size_t probe_count;
44+
std::size_t interval_sec;
45+
};
46+
4047
struct DriverSettings {
4148
std::string endpoint;
4249
std::string database;
4350

44-
std::size_t network_threads_num{2};
45-
std::size_t client_threads_num{0};
51+
std::optional<std::size_t> network_threads_num{};
52+
std::optional<std::size_t> client_threads_num{};
53+
54+
std::optional<TcpKeepaliveSettings> tcp_keepalive{};
55+
56+
std::optional<std::chrono::milliseconds> grpc_keepalive_timeout{};
57+
std::optional<bool> grpc_keepalive_permit_without_calls{};
4658

4759
bool prefer_local_dc{false};
4860
std::optional<std::string> oauth_token;

ydb/src/ydb/impl/driver.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ Driver::Driver(std::string dbname, impl::DriverSettings settings)
2828
settings.prefer_local_dc
2929
? NYdb::EBalancingPolicy::UsePreferableLocation
3030
: NYdb::EBalancingPolicy::UseAllNodes
31-
)
32-
.SetNetworkThreadsNum(settings.network_threads_num)
33-
.SetClientThreadsNum(settings.client_threads_num);
31+
);
32+
if (settings.network_threads_num.has_value()) {
33+
driver_config.SetNetworkThreadsNum(*settings.network_threads_num);
34+
}
35+
if (settings.client_threads_num.has_value()) {
36+
driver_config.SetClientThreadsNum(*settings.client_threads_num);
37+
}
3438

3539
if (settings.secure_connection_cert.has_value()) {
3640
driver_config.UseSecureConnection(*settings.secure_connection_cert);
@@ -51,6 +55,21 @@ Driver::Driver(std::string dbname, impl::DriverSettings settings)
5155
driver_config.SetCredentialsProviderFactory(NYdb::CreateLoginCredentialsProviderFactory(std::move(creds)));
5256
}
5357

58+
if (settings.tcp_keepalive.has_value()) {
59+
driver_config.SetTcpKeepAliveSettings(
60+
settings.tcp_keepalive->enabled,
61+
settings.tcp_keepalive->idle_sec,
62+
settings.tcp_keepalive->probe_count,
63+
settings.tcp_keepalive->interval_sec
64+
);
65+
}
66+
if (settings.grpc_keepalive_timeout.has_value()) {
67+
driver_config.SetGRpcKeepAliveTimeout(TDuration(*settings.grpc_keepalive_timeout));
68+
}
69+
if (settings.grpc_keepalive_permit_without_calls.has_value()) {
70+
driver_config.SetGRpcKeepAlivePermitWithoutCalls(*settings.grpc_keepalive_permit_without_calls);
71+
}
72+
5473
driver_ = std::make_unique<NYdb::TDriver>(driver_config);
5574
NSolomonStatExtension::AddMetricRegistry(*driver_, native_metrics_.get());
5675
}

0 commit comments

Comments
 (0)