Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ydb/docs/en/core/reference/ydb-sdk/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,48 @@ In case of a _hard interruption_, the client receives a notification that it is

{% list tabs group=lang %}

- C++

The SDK supports two modes for reading topics with autoscaling enabled: full support mode and compatibility mode. The reading mode is set in the reading session creation parameters. The default is compatibility mode.

```cpp
auto settings = NYdb::NTopic::TReadSessionSettings()
.SetAutoscalingSupport(true); // full support is enabled

// or

auto settings = NYdb::NTopic::TReadSessionSettings()
.SetAutoscalingSupport(false); // compatibility mode is enabled

auto readSession = topicClient.CreateReadSession(settings);
```

In full support mode, when all messages from the partition are read, the `TEndPartitionSessionEvent` event will arrive . After receiving this event, no more new messages will appear in the partition for reading. To continue reading from child partitions, you must call `Confirm()` , thereby confirming that the application is ready to accept messages from child partitions. If messages from all partitions are processed in one thread, then `Confirm()` can be called immediately after receiving `TEndPartitionSessionEvent` . If processing messages from different partitions is carried out in different threads, then you should complete the processing of messages, for example, execute the accumulated batch, confirm their processing (commit) or save the reading position in your database, and only after that call `Confirm()` . When processing a batch in different threads from several partitions, the batch may include messages from different partitions. When confirming a batch ( `Confirm()` ), there is no impact on other independent reading processes.

After receiving `TEndPartitionSessionEvent` and processing all messages, it is recommended to always immediately confirm their processing (commit). This will allow you to balance the reading of child partitions between different reading sessions, which will lead to an even distribution of the load across all readers.

An event loop fragment might look like this:

```cpp
auto settings = NYdb::NTopic::TReadSessionSettings()
.SetAutoscalingSupport(true);

auto readSession = topicClient.CreateReadSession(settings);

auto event = readSession->GetEvent(/*block=*/true);
if (auto* endPartitionSessionEvent = std::get_if<NYdb::NTopic::TReadSessionEvent::TEndPartitionSessionEvent>(&*event)) {
endPartitionSessionEvent->Confirm();
} else {
// other event types
}
```

In compatibility mode, there is no explicit signal that a partition has finished reading, and the server will try to heuristically determine that the client has processed the partition to completion. This may result in a delay between finishing reading from a source partition and starting reading from its child partitions.

If the client confirms the processing of messages (commit), then the signal for completing the processing of messages from the partition will be confirmation of the processing of the last message of this partition. If the client does not confirm the processing of messages, the server will periodically interrupt reading from the partition and switch to reading in another session (if there are other sessions ready to process the partition). This will continue until reading [starts](#client-commit) from the end of the partition.

It is recommended to check the correctness of handling soft read interruption: the client must process received messages, confirm their processing (commit) or save the read position in its database, and only after that call `Confirm()` for the `TStopPartitionSessionEvent` event.

- Go

Autoscaling of a topic can be enabled during its creation using the `topicoptions.CreateWithAutoPartitioningSettings` option:
Expand Down
2 changes: 1 addition & 1 deletion ydb/docs/ru/core/reference/ydb-sdk/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@
auto readSession = topicClient.CreateReadSession(settings);
```

В режиме полной поддержки, когда все сообщения из партиции будут прочитаны, придёт событие `TEndPartitionSessionEvent`. После получения этого события в партиции больше не появится новых сообщений для чтения. Чтобы продолжить чтение из дочерних партиций, необходимо вызвать `Confirm()`, тем самым подтвердив, что приложение готово принимать сообщения из дочерних партиций. Если сообщения из всех партиций обрабатываются в одном потоке, то `Confirm()` можно вызвать сразу после получения `TEndPartitionSessionEvent`. Если обработка сообщений из разных партиций осуществляется в разных потоках, то следует завершить обработку сообщений, например, выполнить накопившийся батч, подтвердить их обработку (коммит) или сохранить позицию чтения в своей базе, и только после этого вызвать `Confirm()`.
В режиме полной поддержки, когда все сообщения из партиции будут прочитаны, придёт событие `TEndPartitionSessionEvent`. После получения этого события в партиции больше не появится новых сообщений для чтения. Чтобы продолжить чтение из дочерних партиций, необходимо вызвать `Confirm()`, тем самым подтвердив, что приложение готово принимать сообщения из дочерних партиций. Если сообщения из всех партиций обрабатываются в одном потоке, то `Confirm()` можно вызвать сразу после получения `TEndPartitionSessionEvent`. Если обработка сообщений из разных партиций осуществляется в разных потоках, то следует завершить обработку сообщений, например, выполнить накопившийся батч, подтвердить их обработку (коммит) или сохранить позицию чтения в своей базе, и только после этого вызвать `Confirm()`. При обработке батча в разных потоках из нескольких партиций, батч может включать сообщения из разных партиций. При подтверждении батча (`Confirm()`) влияния на другие независимые процессы чтения нет.

После получения `TEndPartitionSessionEvent` и обработки всех сообщений рекомендуется всегда сразу подтверждать их обработку (коммит). Это позволит сбалансировать чтение дочерних партиций между разными сессиями чтения, что приведёт к равномерному распределению нагрузки по всем читателям.

Expand Down