Skip to content

Commit 48e4209

Browse files
authored
Added docs for C++ SDK balancing policies (#25452)
1 parent 490c4fb commit 48e4209

12 files changed

+194
-14
lines changed

ydb/docs/en/core/recipes/ydb-sdk/balancing-prefer-local.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Below are examples of the code for setting the "prefer the nearest data center"
3232
panic(err)
3333
}
3434
defer db.Close(ctx)
35-
...
35+
// ...
3636
}
3737
```
3838

@@ -77,8 +77,28 @@ Below are examples of the code for setting the "prefer the nearest data center"
7777

7878
db := sql.OpenDB(connector)
7979
defer db.Close()
80-
...
80+
// ...
8181
}
8282
```
8383

84+
- С++
85+
86+
The C++ SDK uses the `prefer_local_dc` (prefer the nearest data center) algorithm by default.
87+
88+
```cpp
89+
#include <ydb-cpp-sdk/client/driver/driver.h>
90+
91+
int main() {
92+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
93+
94+
auto driverConfig = NYdb::TDriverConfig(connectionString)
95+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferableLocation());
96+
97+
NYdb::TDriver driver(driverConfig);
98+
// ...
99+
driver.Stop(true);
100+
return 0;
101+
}
102+
```
103+
84104
{% endlist %}

ydb/docs/en/core/recipes/ydb-sdk/balancing-prefer-location.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Below are examples of the code for setting the "prefer the availability zone" ba
3434
panic(err)
3535
}
3636
defer db.Close(ctx)
37-
...
37+
// ...
3838
}
3939
```
4040

@@ -81,8 +81,28 @@ Below are examples of the code for setting the "prefer the availability zone" ba
8181

8282
db := sql.OpenDB(connector)
8383
defer db.Close()
84-
...
84+
// ...
8585
}
8686
```
8787

88+
- С++
89+
90+
In the C++ SDK, you can select only one availability zone as the preferred one.
91+
92+
```cpp
93+
#include <ydb-cpp-sdk/client/driver/driver.h>
94+
95+
int main() {
96+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
97+
98+
auto driverConfig = NYdb::TDriverConfig(connectionString)
99+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferableLocation("datacenter1"));
100+
101+
NYdb::TDriver driver(driverConfig);
102+
// ...
103+
driver.Stop(true);
104+
return 0;
105+
}
106+
```
107+
88108
{% endlist %}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Prefer a pile with a specific state
2+
3+
Below is example of the code for setting the "prefer pile with a specific state" balancing algorithm in {{ ydb-short-name }} SDK.
4+
5+
If no state is specified when setting the option, the SDK prefers the PRIMARY pile.
6+
7+
This option only makes sense if the cluster is operating in bridge mode. If it is not, the SDK will use [random choice balancing algorithm](./balancing-random-choice.md).
8+
9+
{% list tabs %}
10+
11+
- С++
12+
13+
```cpp
14+
#include <ydb-cpp-sdk/client/driver/driver.h>
15+
16+
int main() {
17+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
18+
19+
auto driverConfig = NYdb::TDriverConfig(connectionString)
20+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferablePileState(NYdb::EPileState::PRIMARY));
21+
22+
NYdb::TDriver driver(driverConfig);
23+
// ...
24+
driver.Stop(true);
25+
return 0;
26+
}
27+
```
28+
29+
{% endlist %}

ydb/docs/en/core/recipes/ydb-sdk/balancing-random-choice.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Random choice
22

3-
The {{ ydb-short-name }} SDK uses the `random_choice` algorithm by default.
3+
The {{ ydb-short-name }} SDK uses the `random_choice` algorithm by default, except for the C++ SDK, which uses the ["prefer the nearest data center"](./balancing-prefer-local.md) algorithm by default.
44

55
Below are examples of the code for forced setting of the "random choice" balancing algorithm in different {{ ydb-short-name }} SDKs.
66

@@ -32,7 +32,7 @@ Below are examples of the code for forced setting of the "random choice" balanci
3232
panic(err)
3333
}
3434
defer db.Close(ctx)
35-
...
35+
// ...
3636
}
3737
```
3838

@@ -75,8 +75,26 @@ Below are examples of the code for forced setting of the "random choice" balanci
7575

7676
db := sql.OpenDB(connector)
7777
defer db.Close()
78-
...
78+
// ...
7979
}
8080
```
8181

82+
- C++
83+
84+
```cpp
85+
#include <ydb-cpp-sdk/client/driver/driver.h>
86+
87+
int main() {
88+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
89+
90+
auto driverConfig = NYdb::TDriverConfig(connectionString)
91+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UseAllNodes());
92+
93+
NYdb::TDriver driver(driverConfig);
94+
// ...
95+
driver.Stop(true);
96+
return 0;
97+
}
98+
```
99+
82100
{% endlist %}

ydb/docs/en/core/recipes/ydb-sdk/balancing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Table of contents:
1212
- [Random choice](balancing-random-choice.md)
1313
- [Prefer the nearest data center](balancing-prefer-local.md)
1414
- [Prefer the specific availability zone](balancing-prefer-location.md)
15+
- [Prefer a pile with a specific state](balancing-prefer-pile.md)

ydb/docs/en/core/recipes/ydb-sdk/toc_i.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ items:
2929
href: balancing-prefer-local.md
3030
- name: Prefer the specific availability zone
3131
href: balancing-prefer-location.md
32+
- name: Prefer a pile with a specific state
33+
href: balancing-prefer-pile.md
3234
- name: Running repeat queries
3335
href: retry.md
3436
- name: Setting the session pool size

ydb/docs/ru/core/recipes/ydb-sdk/balancing-prefer-local.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
panic(err)
3333
}
3434
defer db.Close(ctx)
35-
...
35+
// ...
3636
}
3737
```
3838

@@ -77,7 +77,27 @@
7777

7878
db := sql.OpenDB(connector)
7979
defer db.Close()
80-
...
80+
// ...
81+
}
82+
```
83+
84+
- С++
85+
86+
{{ ydb-short-name }} C++ SDK использует алгоритм `prefer_local_dc` (предпочитать ближайший дата-центр) по умолчанию.
87+
88+
```cpp
89+
#include <ydb-cpp-sdk/client/driver/driver.h>
90+
91+
int main() {
92+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
93+
94+
auto driverConfig = NYdb::TDriverConfig(connectionString)
95+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferableLocation());
96+
97+
NYdb::TDriver driver(driverConfig);
98+
// ...
99+
driver.Stop(true);
100+
return 0;
81101
}
82102
```
83103

ydb/docs/ru/core/recipes/ydb-sdk/balancing-prefer-location.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
panic(err)
3535
}
3636
defer db.Close(ctx)
37-
...
37+
// ...
3838
}
3939
```
4040

@@ -81,7 +81,27 @@
8181

8282
db := sql.OpenDB(connector)
8383
defer db.Close()
84-
...
84+
// ...
85+
}
86+
```
87+
88+
- С++
89+
90+
В C++ SDK можно выбрать только одну зону доступности в качестве предпочитаемой.
91+
92+
```cpp
93+
#include <ydb-cpp-sdk/client/driver/driver.h>
94+
95+
int main() {
96+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
97+
98+
auto driverConfig = NYdb::TDriverConfig(connectionString)
99+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferableLocation("datacenter1"));
100+
101+
NYdb::TDriver driver(driverConfig);
102+
// ...
103+
driver.Stop(true);
104+
return 0;
85105
}
86106
```
87107
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Предпочитать пайл c конкретным состоянием
2+
3+
Ниже приведен пример кода установки опции алгоритма балансировки "предпочитать [пайл](../../concepts/glossary.md#pile) c конкретным [состоянием](../../concepts/bridge.md#pile-states)" в {{ ydb-short-name }} SDK.
4+
5+
Если при установке опции состояние не задано, SDK предпочитает PRIMARY [пайл](../../concepts/glossary.md#pile).
6+
7+
Данная опция имеет смысл, только если кластер находится в [bridge режиме](../../concepts/bridge.md). Если это неверно, SDK будет использовать [равномерную случайную балансировку](./balancing-random-choice.md).
8+
9+
{% list tabs %}
10+
11+
- С++
12+
13+
```cpp
14+
#include <ydb-cpp-sdk/client/driver/driver.h>
15+
16+
int main() {
17+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
18+
19+
auto driverConfig = NYdb::TDriverConfig(connectionString)
20+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UsePreferablePileState(NYdb::EPileState::PRIMARY));
21+
22+
NYdb::TDriver driver(driverConfig);
23+
// ...
24+
driver.Stop(true);
25+
return 0;
26+
}
27+
```
28+
29+
{% endlist %}

ydb/docs/ru/core/recipes/ydb-sdk/balancing-random-choice.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Равномерный случайный выбор
22

3-
{{ ydb-short-name }} SDK использует алгоритм `random_choice` (равномерную случайную балансировку) по умолчанию.
3+
{{ ydb-short-name }} SDK использует алгоритм `random_choice` (равномерную случайную балансировку) по умолчанию, кроме С++ SDK, который использует алгоритм ["предпочитать ближайший дата-центр"](./balancing-prefer-local.md) по умолчанию.
44

55
Ниже приведены примеры кода принудительной установки алгоритма балансировки "равномерный случайный выбор" в разных {{ ydb-short-name }} SDK.
66

@@ -32,7 +32,7 @@
3232
panic(err)
3333
}
3434
defer db.Close(ctx)
35-
...
35+
// ...
3636
}
3737
```
3838

@@ -75,7 +75,25 @@
7575

7676
db := sql.OpenDB(connector)
7777
defer db.Close()
78-
...
78+
// ...
79+
}
80+
```
81+
82+
- C++
83+
84+
```cpp
85+
#include <ydb-cpp-sdk/client/driver/driver.h>
86+
87+
int main() {
88+
auto connectionString = std::string(std::getenv("YDB_CONNECTION_STRING"));
89+
90+
auto driverConfig = NYdb::TDriverConfig(connectionString)
91+
.SetBalancingPolicy(NYdb::TBalancingPolicy::UseAllNodes());
92+
93+
NYdb::TDriver driver(driverConfig);
94+
// ...
95+
driver.Stop(true);
96+
return 0;
7997
}
8098
```
8199

0 commit comments

Comments
 (0)