Skip to content

Commit 15e8e59

Browse files
Gazizonokigithub-actions[bot]
authored andcommitted
[C++ SDK] Fixed not working PreferLocalDC (#27101)
1 parent 38a1855 commit 15e8e59

File tree

9 files changed

+22
-16
lines changed

9 files changed

+22
-16
lines changed

.github/last_commit.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
97e6c30a04f89ebeb0f4b596d323e0434c346e54
1+
ec02f1861759f156df1cbd12b41565e07cfcc58f

include/ydb-cpp-sdk/client/types/ydb.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "status_codes.h"
55

66
#include <memory>
7+
#include <optional>
78
#include <string>
89

910

@@ -50,8 +51,8 @@ class TBalancingPolicy {
5051
friend class TDriver;
5152
public:
5253
//! Use preferable location,
53-
//! location is a name of datacenter (VLA, MAN), if location is empty local datacenter is used
54-
static TBalancingPolicy UsePreferableLocation(const std::string& location = {});
54+
//! location is a name of datacenter (VLA, MAN), if location is nullopt local datacenter is used
55+
static TBalancingPolicy UsePreferableLocation(const std::optional<std::string>& location = {});
5556

5657
//! Use all available cluster nodes regardless datacenter locality
5758
static TBalancingPolicy UseAllNodes();
@@ -71,6 +72,7 @@ class TBalancingPolicy {
7172
private:
7273
TBalancingPolicy(std::unique_ptr<TImpl>&& impl);
7374

75+
// For deprecated EBalancingPolicy
7476
TBalancingPolicy(EBalancingPolicy policy, const std::string& params);
7577

7678
std::unique_ptr<TImpl> Impl_;

src/client/driver/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class TDriverConfig::TImpl : public IConnectionsParams {
7070
TCP_KEEPALIVE_INTERVAL
7171
};
7272
bool DrainOnDtors = true;
73-
TBalancingPolicy::TImpl BalancingSettings = TBalancingPolicy::TImpl::UsePreferableLocation("");
73+
TBalancingPolicy::TImpl BalancingSettings = TBalancingPolicy::TImpl::UsePreferableLocation(std::nullopt);
7474
TDuration GRpcKeepAliveTimeout = TDuration::Seconds(10);
7575
bool GRpcKeepAlivePermitWithoutCalls = true;
7676
TDuration SocketIdleTimeout = TDuration::Minutes(6);

src/client/impl/internal/common/balancing_policies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TBalancingPolicy::TImpl TBalancingPolicy::TImpl::UseAllNodes() {
99
return impl;
1010
}
1111

12-
TBalancingPolicy::TImpl TBalancingPolicy::TImpl::UsePreferableLocation(const std::string& location) {
12+
TBalancingPolicy::TImpl TBalancingPolicy::TImpl::UsePreferableLocation(const std::optional<std::string>& location) {
1313
TBalancingPolicy::TImpl impl;
1414
impl.PolicyType = EPolicyType::UsePreferableLocation;
1515
impl.Location = location;

src/client/impl/internal/common/balancing_policies.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class TBalancingPolicy::TImpl {
1919

2020
static TImpl UseAllNodes();
2121

22-
static TImpl UsePreferableLocation(const std::string& location);
22+
static TImpl UsePreferableLocation(const std::optional<std::string>& location);
2323

2424
static TImpl UsePreferablePileState(EPileState pileState);
2525

2626
EPolicyType PolicyType;
2727

2828
// UsePreferableLocation
29-
std::string Location;
29+
std::optional<std::string> Location;
3030

3131
// UsePreferablePileState
3232
EPileState PileState;

src/client/impl/internal/db_driver_state/endpoint_pool.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ std::pair<NThreading::TFuture<TEndpointUpdateResult>, bool> TEndpointPool::Updat
4545
// Is used to convert float to integer load factor
4646
// same integer values will be selected randomly.
4747
const float multiplicator = 10.0;
48+
std::string selfLocation = result.Result.self_location();
4849
std::unordered_map<std::string, Ydb::Bridge::PileState> pileStates;
4950
for (const auto& pile : result.Result.pile_states()) {
5051
pileStates[pile.pile_name()] = pile;
@@ -53,7 +54,7 @@ std::pair<NThreading::TFuture<TEndpointUpdateResult>, bool> TEndpointPool::Updat
5354
for (const auto& endpoint : result.Result.endpoints()) {
5455
std::int32_t loadFactor = static_cast<std::int32_t>(multiplicator * std::min(LoadMax, std::max(LoadMin, endpoint.load_factor())));
5556
std::uint64_t nodeId = endpoint.node_id();
56-
if (!IsLocalEndpoint(endpoint, pileStates)) {
57+
if (!IsPreferredEndpoint(endpoint, selfLocation, pileStates)) {
5758
// Location mismatch, shift this endpoint
5859
loadFactor += GetLocalityShift();
5960
}
@@ -173,13 +174,14 @@ constexpr std::int32_t TEndpointPool::GetLocalityShift() {
173174
return LoadMax * Multiplicator;
174175
}
175176

176-
bool TEndpointPool::IsLocalEndpoint(const Ydb::Discovery::EndpointInfo& endpoint,
177-
const std::unordered_map<std::string, Ydb::Bridge::PileState>& pileStates) const {
177+
bool TEndpointPool::IsPreferredEndpoint(const Ydb::Discovery::EndpointInfo& endpoint,
178+
const std::string& selfLocation,
179+
const std::unordered_map<std::string, Ydb::Bridge::PileState>& pileStates) const {
178180
switch (BalancingPolicy_.PolicyType) {
179181
case TBalancingPolicy::TImpl::EPolicyType::UseAllNodes:
180182
return true;
181183
case TBalancingPolicy::TImpl::EPolicyType::UsePreferableLocation:
182-
return endpoint.location() == BalancingPolicy_.Location;
184+
return endpoint.location() == BalancingPolicy_.Location.value_or(selfLocation);
183185
case TBalancingPolicy::TImpl::EPolicyType::UsePreferablePileState:
184186
if (auto it = pileStates.find(endpoint.bridge_pile_name()); it != pileStates.end()) {
185187
return GetPileState(it->second.state()) == BalancingPolicy_.PileState;

src/client/impl/internal/db_driver_state/endpoint_pool.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ class TEndpointPool {
4646
static constexpr std::int32_t GetLocalityShift();
4747

4848
private:
49-
bool IsLocalEndpoint(const Ydb::Discovery::EndpointInfo& endpoint,
50-
const std::unordered_map<std::string, Ydb::Bridge::PileState>& pileStates) const;
49+
bool IsPreferredEndpoint(const Ydb::Discovery::EndpointInfo& endpoint,
50+
const std::string& selfLocation,
51+
const std::unordered_map<std::string, Ydb::Bridge::PileState>& pileStates) const;
5152
EPileState GetPileState(const Ydb::Bridge::PileState::State& state) const;
5253

5354
private:

src/client/impl/session/kqp_session_common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,10 @@ std::function<void(TKqpSessionCommon*)> TKqpSessionCommon::GetSmartDeleter(std::
171171
switch (sessionImpl->GetState()) {
172172
case TKqpSessionCommon::S_STANDALONE:
173173
case TKqpSessionCommon::S_BROKEN:
174-
case TKqpSessionCommon::S_CLOSING:
174+
case TKqpSessionCommon::S_CLOSING: {
175175
client->DeleteSession(sessionImpl);
176176
break;
177+
}
177178
case TKqpSessionCommon::S_IDLE:
178179
case TKqpSessionCommon::S_ACTIVE: {
179180
if (!client->ReturnSession(sessionImpl)) {

src/client/types/ydb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace NYdb::inline V3 {
1010
TBalancingPolicy::TBalancingPolicy(EBalancingPolicy policy, const std::string& params) {
1111
switch (policy) {
1212
case EBalancingPolicy::UsePreferableLocation:
13-
Impl_ = std::make_unique<TImpl>(TImpl::UsePreferableLocation(params));
13+
Impl_ = std::make_unique<TImpl>(TImpl::UsePreferableLocation(params.empty() ? std::nullopt : std::make_optional(params)));
1414
break;
1515
case EBalancingPolicy::UseAllNodes:
1616
Impl_ = std::make_unique<TImpl>(TImpl::UseAllNodes());
1717
break;
1818
}
1919
}
2020

21-
TBalancingPolicy TBalancingPolicy::UsePreferableLocation(const std::string& location) {
21+
TBalancingPolicy TBalancingPolicy::UsePreferableLocation(const std::optional<std::string>& location) {
2222
return TBalancingPolicy(std::make_unique<TImpl>(TImpl::UsePreferableLocation(location)));
2323
}
2424

0 commit comments

Comments
 (0)