From fec754e2582ac8af4d3c13331b922c3a4e1073ea Mon Sep 17 00:00:00 2001 From: YDBot Date: Fri, 30 Oct 2026 11:48:29 +0000 Subject: [PATCH 01/15] Merge pull request #56629 from ydb-platform/merge-rightlib-261024-0104 --- .github/last_commit.txt | 2 +- library/cpp/http/simple/http_client.cpp | 62 ++++++- library/cpp/http/simple/http_client.h | 19 +- library/cpp/http/simple/ut/http_ut.cpp | 195 ++++++++++++++++++++ library/cpp/yt/system/access_spreader-inl.h | 3 +- 5 files changed, 269 insertions(+), 12 deletions(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index f1da79fc773..49b929bcd7b 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -27b9404fde564e5a88652ef9ad5796d71206fa81 +294f8f3a24830c49b72af16639fb0ea103bf1971 diff --git a/library/cpp/http/simple/http_client.cpp b/library/cpp/http/simple/http_client.cpp index 993ee62f76a..092130ca193 100644 --- a/library/cpp/http/simple/http_client.cpp +++ b/library/cpp/http/simple/http_client.cpp @@ -14,6 +14,32 @@ TSpinLock TKeepAliveHttpClient::ConnectionQuarantineMutex; TQueue> TKeepAliveHttpClient::ConnectionQuarantine; +namespace { + TKeepAliveHttpClient::THeaders GetRedirectHeaders(const TKeepAliveHttpClient::THeaders& headers) { + TKeepAliveHttpClient::THeaders redirectHeaders; + for (const auto& header : headers) { + if (AsciiEqualsIgnoreCase(header.first, "User-Agent") || AsciiEqualsIgnoreCase(header.first, "X-Request-ID")) { + redirectHeaders.emplace(header.first, header.second); + } + } + return redirectHeaders; + } + + TString GetResponseRequestIds(const THttpInput& input) { + TString requestIds; + // S3 uses X-Amz-Request-Id instead of X-Request-ID in responses. + for (TStringBuf name : {TStringBuf("X-Request-ID"), TStringBuf("X-Amz-Request-Id")}) { + if (const auto* header = input.Headers().FindHeader(name)) { + requestIds += "\n"; + requestIds += name; + requestIds += ": "; + requestIds += header->Value(); + } + } + return requestIds; + } +} // namespace + TKeepAliveHttpClient::TKeepAliveHttpClient(const TString& host, ui32 port, TDuration socketTimeout, @@ -243,7 +269,7 @@ void TSimpleHttpClient::DoGet(const TStringBuf relativeUrl, IOutputStream* outpu TKeepAliveHttpClient::THttpCode code = cl.DoGet(relativeUrl, output, headers, outHeaders, std::move(cancellation)); Y_ENSURE(cl.GetHttpInput()); - ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code); + ProcessResponseWithHeaders(relativeUrl, *cl.GetHttpInput(), output, code, headers); } void TSimpleHttpClient::DoPost(const TStringBuf relativeUrl, TStringBuf body, IOutputStream* output, const THashMap& headers, THttpHeaders* outHeaders, NThreading::TCancellationToken cancellation) const { @@ -252,7 +278,7 @@ void TSimpleHttpClient::DoPost(const TStringBuf relativeUrl, TStringBuf body, IO TKeepAliveHttpClient::THttpCode code = cl.DoPost(relativeUrl, body, output, headers, outHeaders, std::move(cancellation)); Y_ENSURE(cl.GetHttpInput()); - ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code); + ProcessResponseWithHeaders(relativeUrl, *cl.GetHttpInput(), output, code, headers); } void TSimpleHttpClient::DoPostRaw(const TStringBuf relativeUrl, const TStringBuf rawRequest, IOutputStream* output, THttpHeaders* outHeaders, NThreading::TCancellationToken cancellation) const { @@ -261,7 +287,7 @@ void TSimpleHttpClient::DoPostRaw(const TStringBuf relativeUrl, const TStringBuf TKeepAliveHttpClient::THttpCode code = cl.DoRequestRaw(rawRequest, output, outHeaders, std::move(cancellation)); Y_ENSURE(cl.GetHttpInput()); - ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code); + ProcessResponseWithHeaders(relativeUrl, *cl.GetHttpInput(), output, code, {}); } namespace NPrivate { @@ -326,14 +352,28 @@ namespace NPrivate { } } -void TSimpleHttpClient::ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream*, const unsigned statusCode) const { +void TSimpleHttpClient::ProcessResponse( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream*, + const unsigned statusCode) const { if (!(statusCode >= 200 && statusCode < 300)) { + TString responseRequestIds = GetResponseRequestIds(input); TString rest = ReadDiagnosticBody(input, statusCode); ythrow THttpRequestException(statusCode) << "Got " << statusCode << " at " << Host << relativeUrl << "\nFull http response:\n" - << rest; + << rest << responseRequestIds; } } +void TSimpleHttpClient::ProcessResponseWithHeaders( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream* output, + const unsigned statusCode, + const THeaders&) const { + ProcessResponse(relativeUrl, input, output, statusCode); +} + TSimpleHttpClient::~TSimpleHttpClient() { } @@ -376,7 +416,12 @@ void TRedirectableHttpClient::PrepareClient(TKeepAliveHttpClient& cl) const { }; } -void TRedirectableHttpClient::ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream* output, const unsigned statusCode) const { +void TRedirectableHttpClient::ProcessResponseWithHeaders( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream* output, + const unsigned statusCode, + const THeaders& headers) const { for (auto i = input.Headers().Begin(), e = input.Headers().End(); i != e; ++i) { if (0 == TString::compare(i->Name(), TStringBuf("Location"))) { if (Opts.MaxRedirectCount() == 0) { @@ -408,14 +453,15 @@ void TRedirectableHttpClient::ProcessResponse(const TStringBuf relativeUrl, THtt if (HttpsVerification) { cl.EnableVerificationForHttps(); } - cl.DoGet(body, output); + cl.DoGet(body, output, GetRedirectHeaders(headers)); return; } } if (!(statusCode >= 200 && statusCode < 300)) { + TString responseRequestIds = GetResponseRequestIds(input); TString rest = ReadDiagnosticBody(input, statusCode); ythrow THttpRequestException(statusCode) << "Got " << statusCode << " at " << Host << relativeUrl << "\nFull http response:\n" - << rest; + << rest << responseRequestIds; } TransferData(&input, output); } diff --git a/library/cpp/http/simple/http_client.h b/library/cpp/http/simple/http_client.h index 3e05aeae612..554898ec566 100644 --- a/library/cpp/http/simple/http_client.h +++ b/library/cpp/http/simple/http_client.h @@ -212,7 +212,17 @@ class TSimpleHttpClient { TKeepAliveHttpClient CreateClient() const; virtual void PrepareClient(TKeepAliveHttpClient& cl) const; - virtual void ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream* output, const unsigned statusCode) const; + virtual void ProcessResponse( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream* output, + const unsigned statusCode) const; + virtual void ProcessResponseWithHeaders( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream* output, + const unsigned statusCode, + const THeaders& headers) const; }; class TRedirectableHttpClient: public TSimpleHttpClient { @@ -226,7 +236,12 @@ class TRedirectableHttpClient: public TSimpleHttpClient { private: void PrepareClient(TKeepAliveHttpClient& cl) const override; - void ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream* output, const unsigned statusCode) const override; + void ProcessResponseWithHeaders( + const TStringBuf relativeUrl, + THttpInput& input, + IOutputStream* output, + const unsigned statusCode, + const THeaders& headers) const override; private: TOptions Opts; diff --git a/library/cpp/http/simple/ut/http_ut.cpp b/library/cpp/http/simple/ut/http_ut.cpp index 927becf8da0..ee6d0f3947f 100644 --- a/library/cpp/http/simple/ut/http_ut.cpp +++ b/library/cpp/http/simple/ut/http_ut.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -205,6 +206,78 @@ Y_UNIT_TEST_SUITE(SimpleHttp) { } }; + struct TObservedRedirectRequest { + TString Path; + TString UserAgent; + TString RequestId; + bool HasNotForwarded; + }; + + class TRedirectHeaders: public TRequestReplier { + ui16 Port_; + TVector* Requests_; + + public: + TRedirectHeaders(ui16 port, TVector* requests) + : Port_(port) + , Requests_(requests) + { + } + + bool DoReply(const TReplyParams& params) override { + const auto path = TParsedHttpFull(params.Input.FirstLine()).Path; + const auto& headers = params.Input.Headers(); + Requests_->push_back({ + TString(path), + GetHeaderValue(headers, "User-Agent"), + GetHeaderValue(headers, "X-Request-ID"), + headers.HasHeader("X-Not-Forwarded"), + }); + + if (path == "/redirect_headers") { + params.Output << "HTTP/1.1 307 Internal Redirect\r\n" + "Location: http://localhost:" + << Port_ + << "/redirect_headers_target\r\n"; + if (const auto* requestId = headers.FindHeader("X-Request-ID")) { + params.Output << "X-Request-ID: " << requestId->Value() << "\r\n"; + } + params.Output << "\r\n"; + return true; + } + + THttpResponse resp(HTTP_FORBIDDEN); + if (const auto* requestId = headers.FindHeader("X-Request-ID")) { + resp.AddHeader("X-Request-ID", requestId->Value()); + } + resp.SetContent("denied"); + resp.OutTo(params.Output); + return true; + } + + private: + static TString GetHeaderValue(const THttpHeaders& headers, TStringBuf name) { + const auto* header = headers.FindHeader(name); + return header ? header->Value() : TString(); + } + }; + + class TFourArgumentProcessResponseClient: public TSimpleHttpClient { + bool* Processed_; + + public: + TFourArgumentProcessResponseClient(const TString& host, ui16 port, bool* processed) + : TSimpleHttpClient(host, port) + , Processed_(processed) + { + } + + private: + void ProcessResponse(const TStringBuf, THttpInput&, IOutputStream*, const unsigned) const override { + *Processed_ = true; + } + }; + static void TestRedirectCountParam(int maxRedirectCount, int redirectCount) { TPortManager pm; ui16 port = pm.GetPort(80); @@ -488,6 +561,128 @@ Y_UNIT_TEST_SUITE(SimpleHttp) { UNIT_ASSERT_VALUES_EQUAL(0, server.GetClientCount()); } + static void TestRedirectHeaders(TStringBuf userAgentHeader, TStringBuf requestIdHeader) { + TPortManager pm; + ui16 port = pm.GetPort(80); + TVector requests; + NMock::TMockServer server(createOptions(port, true), [port, &requests]() { return new TRedirectHeaders(port, &requests); }); + + TRedirectableHttpClient cl("localhost", port); + TKeepAliveHttpClient::THeaders headers = { + {TString(userAgentHeader), "test-agent"}, + {TString(requestIdHeader), "test-request-id"}, + {"X-Not-Forwarded", "secret"}, + }; + TStringStream output; + TString error; + + try { + cl.DoGet("/redirect_headers", &output, headers); + } catch (const THttpRequestException& e) { + error = e.what(); + } + + UNIT_ASSERT_VALUES_EQUAL(2, requests.size()); + UNIT_ASSERT_VALUES_EQUAL("/redirect_headers", requests[0].Path); + UNIT_ASSERT_VALUES_EQUAL("test-agent", requests[0].UserAgent); + UNIT_ASSERT_VALUES_EQUAL("test-request-id", requests[0].RequestId); + UNIT_ASSERT(requests[0].HasNotForwarded); + UNIT_ASSERT_VALUES_EQUAL("/redirect_headers_target", requests[1].Path); + UNIT_ASSERT_VALUES_EQUAL("test-agent", requests[1].UserAgent); + UNIT_ASSERT_VALUES_EQUAL("test-request-id", requests[1].RequestId); + UNIT_ASSERT(!requests[1].HasNotForwarded); + UNIT_ASSERT_C(error.Contains("Got 403 at http://localhost/redirect_headers_target"), error); + UNIT_ASSERT_C(error.Contains("\nX-Request-ID: test-request-id"), error); + UNIT_ASSERT_VALUES_EQUAL("denied", output.Str()); + } + + Y_UNIT_TEST(redirectableForwardsSelectedHeadersAndReportsResponseRequestId) { + TestRedirectHeaders("User-Agent", "X-Request-ID"); + TestRedirectHeaders("uSeR-aGeNt", "x-request-id"); + } + + template + static void TestResponseRequestId() { + const TVector> headerCases = { + {}, + {"X-Request-ID"}, + {"x-ReQuEsT-iD"}, + {"X-Amz-Request-Id"}, + {"x-aMz-ReQuEsT-iD"}, + {"X-Request-ID", "X-Amz-Request-Id"}, + }; + for (const auto& headerNames : headerCases) { + TPortManager pm; + ui16 port = pm.GetPort(80); + NMock::TMockServer server(createOptions(port, true), []() { + class TErrorResponse: public TRequestReplier { + public: + bool DoReply(const TReplyParams& params) override { + THttpResponse resp(HTTP_FORBIDDEN); + for (TStringBuf name : {TStringBuf("X-Request-ID"), TStringBuf("X-Amz-Request-Id")}) { + if (const auto* requestId = params.Input.Headers().FindHeader(name)) { + resp.AddHeader(requestId->Name(), requestId->Value()); + } + } + resp.SetContent("denied"); + resp.OutTo(params.Output); + return true; + } + }; + return new TErrorResponse; + }); + TClient client("localhost", port); + TStringStream output; + TString error; + TKeepAliveHttpClient::THeaders headers; + for (const auto& headerName : headerNames) { + headers.emplace(headerName, "request-id-from-request"); + } + try { + client.DoGet("/error", &output, headers); + } catch (const THttpRequestException& e) { + UNIT_ASSERT_VALUES_EQUAL(e.GetStatusCode(), HTTP_FORBIDDEN); + error = e.what(); + } + + UNIT_ASSERT_C(error.Contains("Got 403 at "), error); + UNIT_ASSERT_C(error.Contains("Full http response:\n"), error); + for (const auto& headerName : headerNames) { + const TStringBuf responseHeader = AsciiEqualsIgnoreCase(headerName, "X-Amz-Request-Id") + ? TStringBuf("X-Amz-Request-Id") + : TStringBuf("X-Request-ID"); + UNIT_ASSERT_C(error.Contains(TString("\n") + responseHeader + ": request-id-from-request"), error); + } + if (headerNames.empty()) { + UNIT_ASSERT_C(!error.Contains("X-Request-ID"), error); + UNIT_ASSERT_C(!error.Contains("X-Amz-Request-Id"), error); + } + UNIT_ASSERT_VALUES_EQUAL("denied", output.Str()); + } + } + + Y_UNIT_TEST(simpleReportsResponseRequestId) { + TestResponseRequestId(); + } + + Y_UNIT_TEST(redirectableReportsResponseRequestId) { + TestResponseRequestId(); + } + + Y_UNIT_TEST(fourArgumentProcessResponseOverrideRemainsSupported) { + TPortManager pm; + ui16 port = pm.GetPort(80); + NMock::TMockServer server(createOptions(port, true), []() { return new TPong; }); + bool processed = false; + TFourArgumentProcessResponseClient client("localhost", port, &processed); + TStringStream output; + + client.DoGet("/ping", &output); + + UNIT_ASSERT(processed); + UNIT_ASSERT_VALUES_EQUAL("pong", output.Str()); + } + Y_UNIT_TEST(keepaliveSuccessful) { auto test = [](bool keepalive, i64 clientCount) { TPortManager pm; diff --git a/library/cpp/yt/system/access_spreader-inl.h b/library/cpp/yt/system/access_spreader-inl.h index 413fc004a1e..a1ea29232d0 100644 --- a/library/cpp/yt/system/access_spreader-inl.h +++ b/library/cpp/yt/system/access_spreader-inl.h @@ -1,6 +1,7 @@ -#pragma once #ifndef ACCESS_SPREADER_INL_H_ #error "Direct inclusion of this file is not allowed, include access_spreader.h" +// For the sake of sane code completion. +#include "access_spreader.h" #endif #include From 5d6d017976d500400103bd9701f4b702d83e7e8b Mon Sep 17 00:00:00 2001 From: Sergey M Date: Fri, 30 Oct 2026 11:48:38 +0000 Subject: [PATCH 02/15] Added an option to disable autoupdate (#56575) --- .github/last_commit.txt | 2 +- include/ydb-cpp-sdk/client/table/table.h | 4 +++ src/api/protos/ydb_table.proto | 4 +++ src/client/table/out.cpp | 1 + src/client/table/table.cpp | 6 +++++ tests/unit/client/table/table_ut.cpp | 33 ++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 1 deletion(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 49b929bcd7b..194da5a4056 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -294f8f3a24830c49b72af16639fb0ea103bf1971 +66a05a8e75fc6ec7ea248c7a04265177665ab6d3 diff --git a/include/ydb-cpp-sdk/client/table/table.h b/include/ydb-cpp-sdk/client/table/table.h index 16350bea3c6..f786960012d 100644 --- a/include/ydb-cpp-sdk/client/table/table.h +++ b/include/ydb-cpp-sdk/client/table/table.h @@ -381,6 +381,10 @@ struct TKMeansTreeSettings { uint32_t OverlapClusters = 0; double OverlapRatio = 0; + // Maintain the index on table writes. Enabled when unset. + // When false, rebuild the index to reflect table changes. + std::optional AutoUpdate = std::nullopt; + static TKMeansTreeSettings FromProto(const Ydb::Table::KMeansTreeSettings& proto); void SerializeTo(Ydb::Table::KMeansTreeSettings& settings) const; diff --git a/src/api/protos/ydb_table.proto b/src/api/protos/ydb_table.proto index 2244b4f940a..3f30f71871a 100644 --- a/src/api/protos/ydb_table.proto +++ b/src/api/protos/ydb_table.proto @@ -106,6 +106,10 @@ message KMeansTreeSettings { optional double overlap_ratio = 5; optional bool adaptive_clusters = 6; + + // Maintain the index on table writes. Enabled when omitted. + // When false, changes are reflected only after rebuilding the index. + optional bool autoupdate = 7; } message GlobalIndex { diff --git a/src/client/table/out.cpp b/src/client/table/out.cpp index 3bddb689398..7431756fc99 100644 --- a/src/client/table/out.cpp +++ b/src/client/table/out.cpp @@ -87,6 +87,7 @@ Y_DECLARE_OUT_SPEC(, NYdb::NTable::TKMeansTreeSettings, stream, value) { ", levels: " << value.Levels << ", overlap_clusters: " << value.OverlapClusters << ", overlap_ratio: " << value.OverlapRatio << + ", autoupdate: " << value.AutoUpdate.value_or(true) << " }"; } diff --git a/src/client/table/table.cpp b/src/client/table/table.cpp index cc35a2639f4..0f761eaa7cd 100644 --- a/src/client/table/table.cpp +++ b/src/client/table/table.cpp @@ -3013,6 +3013,7 @@ TKMeansTreeSettings TKMeansTreeSettings::FromProto(const Ydb::Table::KMeansTreeS .Levels = proto.levels(), .OverlapClusters = proto.overlap_clusters(), .OverlapRatio = proto.overlap_ratio(), + .AutoUpdate = proto.has_autoupdate() ? std::make_optional(proto.autoupdate()) : std::nullopt, }; } @@ -3022,6 +3023,11 @@ void TKMeansTreeSettings::SerializeTo(Ydb::Table::KMeansTreeSettings& settings) settings.set_levels(Levels); settings.set_overlap_clusters(OverlapClusters); settings.set_overlap_ratio(OverlapRatio); + if (AutoUpdate.has_value()) { + settings.set_autoupdate(*AutoUpdate); + } else { + settings.clear_autoupdate(); + } } void TKMeansTreeSettings::Out(IOutputStream& o) const { diff --git a/tests/unit/client/table/table_ut.cpp b/tests/unit/client/table/table_ut.cpp index 44798b3a9a9..a915f8e95d4 100644 --- a/tests/unit/client/table/table_ut.cpp +++ b/tests/unit/client/table/table_ut.cpp @@ -977,6 +977,39 @@ TEST(TableTest, AlterTableSetMetricsSettings) { ); } +TEST(KMeansTreeSettings, AutoUpdateDefaultsInAggregate) { + const NYdb::NTable::TKMeansTreeSettings settings{ + {NYdb::NTable::TVectorIndexSettings::EMetric::CosineDistance, + NYdb::NTable::TVectorIndexSettings::EVectorType::Float, 1024}, + 10, + 3}; + EXPECT_FALSE(settings.AutoUpdate.has_value()); + + Ydb::Table::KMeansTreeSettings proto; + settings.SerializeTo(proto); + EXPECT_FALSE(proto.has_autoupdate()); +} + +TEST(KMeansTreeSettings, AutoUpdateRoundTrip) { + for (const auto autoUpdate : {std::optional(), std::make_optional(true), std::make_optional(false)}) { + NYdb::NTable::TKMeansTreeSettings original; + original.AutoUpdate = autoUpdate; + Ydb::Table::KMeansTreeSettings proto; + original.SerializeTo(proto); + EXPECT_EQ(proto.has_autoupdate(), autoUpdate.has_value()); + if (autoUpdate.has_value()) { + EXPECT_EQ(proto.autoupdate(), *autoUpdate); + } + const auto restored = NYdb::NTable::TKMeansTreeSettings::FromProto(proto); + EXPECT_EQ(restored.AutoUpdate, autoUpdate); + } + + Ydb::Table::KMeansTreeSettings proto; + proto.set_autoupdate(false); + NYdb::NTable::TKMeansTreeSettings().SerializeTo(proto); + EXPECT_FALSE(proto.has_autoupdate()); +} + TEST(TtlTierSettings, ObjectKeyPrefixRoundTrip) { using namespace NYdb::NTable; for (auto&& prefix : {std::optional(), std::make_optional(std::string()), From c9f4ad994894708320aafd70c489d2f59600795e Mon Sep 17 00:00:00 2001 From: Alexander Rutkovsky Date: Fri, 30 Oct 2026 11:48:46 +0000 Subject: [PATCH 03/15] Bridge: per-pool group upgrade (#56580) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 2 ++ src/api/protos/draft/ydb_bridge.proto | 8 ++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 194da5a4056..be686342dc5 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -66a05a8e75fc6ec7ea248c7a04265177665ab6d3 +62c3d6877d73b3f33a6d81894ffc5d0756a988ef diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f0f2957320..1c6127195f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## v3.25.0 +* Added `TConvertToBridgeSettings::SkipStoragePoolConversion()` to leave existing storage pools and their groups non-bridged when converting a cluster into a bridged one. + * Added `TExecuteQueryIterator::Cancel()` to explicitly cancel a query response stream, including a pending read. * Added `TBridgeClient::RemovePile()` to remove a DISCONNECTED pile from a bridged cluster along with its data. diff --git a/src/api/protos/draft/ydb_bridge.proto b/src/api/protos/draft/ydb_bridge.proto index e4f198efff4..5470c0060cd 100644 --- a/src/api/protos/draft/ydb_bridge.proto +++ b/src/api/protos/draft/ydb_bridge.proto @@ -64,10 +64,14 @@ message UpdateClusterStateResult { } // Convert a running non-bridged cluster into a bridged one with a single pile. All nodes of the cluster must already -// run with a single-pile bridge config. Storage groups are converted in the background after the call succeeds; see -// GetClusterStateResult.conversion_in_progress. +// run with a single-pile bridge config. By default, existing storage pools and their groups are converted in the +// background after the call succeeds; see GetClusterStateResult.conversion_in_progress. message ConvertToBridgeRequest { Ydb.Operations.OperationParams operation_params = 1; + // Leave existing storage pools and their groups non-bridged; they are not replicated to added piles until converted + // separately. Static storage groups are still converted to bridge mode. The bridge config must enable non-uniform + // storage, which gets activated along with the conversion. + bool skip_storage_pool_conversion = 2; } message ConvertToBridgeResponse { From f668610365fdd6a17aa086488e91df8a98c70b08 Mon Sep 17 00:00:00 2001 From: Alexander Rutkovsky Date: Fri, 30 Oct 2026 11:48:54 +0000 Subject: [PATCH 04/15] Fix bridge issues from review (#56911) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index be686342dc5..b3d9e948f3f 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -62c3d6877d73b3f33a6d81894ffc5d0756a988ef +6ec2ec3cc1fde3c05cb629af924d79cb81f2844a diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6127195f3..00d6153ede5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -## v3.25.0 - * Added `TConvertToBridgeSettings::SkipStoragePoolConversion()` to leave existing storage pools and their groups non-bridged when converting a cluster into a bridged one. +## v3.25.0 + * Added `TExecuteQueryIterator::Cancel()` to explicitly cancel a query response stream, including a pending read. * Added `TBridgeClient::RemovePile()` to remove a DISCONNECTED pile from a bridged cluster along with its data. From d1f4b5f83db6b70339f0a89d7af32b0a317ee174 Mon Sep 17 00:00:00 2001 From: Evgeniy Ivanov Date: Fri, 30 Oct 2026 11:49:02 +0000 Subject: [PATCH 05/15] Expose per-pile connectivity through GetClusterState (#57075) --- .github/last_commit.txt | 2 +- src/api/protos/draft/ydb_bridge.proto | 37 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index b3d9e948f3f..fd6abe5d1b4 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -6ec2ec3cc1fde3c05cb629af924d79cb81f2844a +4bb0978cfff2912987cc2da862274613d80545e3 diff --git a/src/api/protos/draft/ydb_bridge.proto b/src/api/protos/draft/ydb_bridge.proto index 5470c0060cd..1c3b788863f 100644 --- a/src/api/protos/draft/ydb_bridge.proto +++ b/src/api/protos/draft/ydb_bridge.proto @@ -14,6 +14,10 @@ import "google/protobuf/timestamp.proto"; message GetClusterStateRequest { Ydb.Operations.OperationParams operation_params = 1; + + // Also query static-node connectivity, without requiring BSC or tablets. + // An older node in the forwarding path may reject this optional query. + bool include_pile_connectivity = 2; } message GetClusterStateResponse { @@ -30,6 +34,39 @@ message GetClusterStateResult { bool conversion_in_progress = 3; // Operation changing the set of piles of the cluster that is not finished yet optional PendingPileOperation pending_pile_operation = 4; + optional PileConnectivityReport pile_connectivity = 5; +} + +message PileConnectivity { + string pile_name = 1; + uint32 static_nodes = 2; + uint32 connected_static_nodes = 3; + + // Whether connected static nodes satisfy this pile's distconf node quorum. + // This measures node connectivity, not working disks. INACTIVE piles report false. + bool has_node_quorum = 4; + + // Only piles in config quorum can block configuration commits. NOT_SYNCHRONIZED + // stage 1 is in neither quorum; stage 2 is in both quorums. + bool in_config_quorum = 5; + bool in_data_quorum = 6; +} + +// A snapshot of one distconf binding component. During a partition, each side can +// report the other as lacking quorum. This is not a vote and must not select which +// side survives. Rebinding can temporarily report only part of a reachable pile. +// DISCONNECTED and INACTIVE piles do not connect to other piles, so their missing +// quorum is expected when viewed from another pile. +message PileConnectivityReport { + uint32 reporter_node_id = 1; + bool reporter_is_root = 2; + bool binding_in_progress = 3; + + // Reporter cluster-state generation; may differ from GetClusterStateResult.generation. + uint64 generation = 4; + + // Names and quorum requirements come from the reporter, independently of pile_states. + repeated PileConnectivity piles = 5; } message PendingPileOperation { From d0070345fce7643074ef088f07ac45471bb495da Mon Sep 17 00:00:00 2001 From: YDBot Date: Fri, 30 Oct 2026 11:49:18 +0000 Subject: [PATCH 06/15] Merge pull request #57187 from ydb-platform/robots/branch-sync/merge-rightlib-261028-1105 --- .github/last_commit.txt | 2 +- library/cpp/http/simple/http_client.h | 16 +- library/cpp/http/simple/ut/http_ut.cpp | 15 ++ .../cancellation/cancellation_token.h | 5 + library/cpp/threading/cancellation/ut.cpp | 7 + util/system/context.cpp | 46 ++--- util/system/context.h | 13 +- util/system/context_aarch64.S | 51 ++++++ util/system/context_config.h | 19 +++ util/system/context_ut.cpp | 157 ++++++++++++++++++ util/system/context_x86.S | 43 +++++ 11 files changed, 322 insertions(+), 52 deletions(-) create mode 100644 util/system/context_config.h diff --git a/.github/last_commit.txt b/.github/last_commit.txt index fd6abe5d1b4..1b5e6dac7b4 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -4bb0978cfff2912987cc2da862274613d80545e3 +6c42a49c09af2a08fa8f929acf047c7ef5518b38 diff --git a/library/cpp/http/simple/http_client.h b/library/cpp/http/simple/http_client.h index 554898ec566..0883cb516cd 100644 --- a/library/cpp/http/simple/http_client.h +++ b/library/cpp/http/simple/http_client.h @@ -314,14 +314,16 @@ TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::DoRequestReliable(const T& const bool couldRetry = !haveNewConnection && i == 0; // Actually old connection could be already closed by server, // so we should try one more time in this case. TAtomicSharedPtr cancellationEndEvent = MakeAtomicShared(); - auto cancelSub = cancellation.Future().Subscribe([this, cancellationEndEvent](auto&) { - if (cancellationEndEvent.RefCount() > 1) { - if (Connection && Connection->IsOk()) { - Connection->Shutdown(); + if (!cancellation.IsDefault()) { + cancellation.Future().Subscribe([this, cancellationEndEvent](auto&) { + if (cancellationEndEvent.RefCount() > 1) { + if (Connection && Connection->IsOk()) { + Connection->Shutdown(); + } + cancellationEndEvent->Signal(); } - cancellationEndEvent->Signal(); - } - }); + }); + } try { Connection->Write(raw); diff --git a/library/cpp/http/simple/ut/http_ut.cpp b/library/cpp/http/simple/ut/http_ut.cpp index ee6d0f3947f..d1e4dfd69c5 100644 --- a/library/cpp/http/simple/ut/http_ut.cpp +++ b/library/cpp/http/simple/ut/http_ut.cpp @@ -382,6 +382,21 @@ Y_UNIT_TEST_SUITE(SimpleHttp) { } } + Y_UNIT_TEST(keepAliveDefaultAndPreCancelledToken) { + TPortManager pm; + ui16 port = pm.GetPort(80); + NMock::TMockServer server(createOptions(port, true), []() { return new TPong; }); + + TKeepAliveHttpClient cl("localhost", port); + TStringStream response; + UNIT_ASSERT_VALUES_EQUAL(200, cl.DoGet("/ping", &response)); + UNIT_ASSERT_VALUES_EQUAL("pong", response.Str()); + + NThreading::TCancellationTokenSource cancel; + cancel.Cancel(); + UNIT_ASSERT_EXCEPTION(cl.DoGet("/ping", nullptr, {}, nullptr, cancel.Token()), NThreading::TOperationCancelledException); + } + Y_UNIT_TEST(simpleMessages) { TPortManager pm; ui16 port = pm.GetPort(80); diff --git a/library/cpp/threading/cancellation/cancellation_token.h b/library/cpp/threading/cancellation/cancellation_token.h index 2c1626bc3b2..c825790491e 100644 --- a/library/cpp/threading/cancellation/cancellation_token.h +++ b/library/cpp/threading/cancellation/cancellation_token.h @@ -29,6 +29,11 @@ class TCancellationToken { return Future_.HasValue(); } + //! Shows whether this is the default token, which cannot be cancelled + bool IsDefault() const { + return Future_.StateId() == Default().Future_.StateId(); + } + //! Shows whether a cancellation has been requested bool IsDeadlineReached() const { return TInstant::Now() > Deadline_; diff --git a/library/cpp/threading/cancellation/ut.cpp b/library/cpp/threading/cancellation/ut.cpp index c744795a081..5a4655f51f2 100644 --- a/library/cpp/threading/cancellation/ut.cpp +++ b/library/cpp/threading/cancellation/ut.cpp @@ -43,6 +43,13 @@ Y_UNIT_TEST_SUITE(Cancellation) { Y_UNIT_TEST(Default) { auto const& token = TCancellationToken::Default(); UNIT_ASSERT(!token.IsCancellationRequested()); + UNIT_ASSERT(token.IsDefault()); + UNIT_ASSERT(TCancellationToken(token).IsDefault()); + + TCancellationTokenSource source; + UNIT_ASSERT(!source.Token().IsDefault()); + source.Cancel(); + UNIT_ASSERT(!source.Token().IsDefault()); } Y_UNIT_TEST(ThrowIfDeadlineReached) { diff --git a/util/system/context.cpp b/util/system/context.cpp index 9211c564aff..0d600fd8d38 100644 --- a/util/system/context.cpp +++ b/util/system/context.cpp @@ -114,22 +114,18 @@ namespace { return JmpBufReg(buf, FRAME_CNT); } - #if defined(_x86_64_) - // not sure if Y_NO_SANITIZE is needed - Y_NO_SANITIZE("address") - Y_NO_SANITIZE("memory") extern "C" void - ContextTrampoLine(void*, void*, void*, void*, void*, void*, // register arguments, no defined value - /* first argument passed through the stack */ void* t1, - /* second argument passed through the stack */ void* t2) { - Y_ASSERT(t1 == t2); - Run(t1); + extern "C" void ContextTrampoLine(); + + extern "C" Y_NO_SANITIZE("address") Y_NO_SANITIZE("memory") Y_HIDDEN void + ContextTrampoLineRun(void* trampoline) { + static_cast(trampoline)->DoRunNaked(); } - #else - Y_NO_SANITIZE("address") - Y_NO_SANITIZE("memory") static void - ContextTrampoLine() { - void** argPtr = (void**)((char*)AlignUp(&argPtr + EXTRA_PUSH_ARGS, STACK_ALIGN) + STACK_ALIGN); - Run(*(argPtr - 1)); + + #if !defined(NDEBUG) + extern "C" Y_NO_SANITIZE("address") Y_NO_SANITIZE("memory") Y_HIDDEN void + ContextTrampoLineAbort(void* t1, void* t2) { + Y_ASSERT(t1 == t2); + abort(); } #endif } // namespace @@ -171,24 +167,10 @@ TContMachineContext::TContMachineContext(const TContClosure& c) auto trampoline = c.TrampoLine; #endif - #if defined(_x86_64_) - stack.ReAlign(); - // push twice to preserve alignment by 16 - stack.Push(trampoline); // second stack argument - stack.Push(trampoline); // first stack argument - - stack.Push(nullptr); // fake return address - #else - stack.Push(trampoline); - stack.Push(trampoline); stack.ReAlign(); - /* - * fake return address - */ - for (size_t i = 0; i < EXTRA_PUSH_ARGS; ++i) { - stack.Push(nullptr); - } - #endif + // 16-byte aligned record at SP upon entering ContextTrampoLine: + stack.Push(trampoline); // [SP + sizeof(void*)]: duplicate checked in debug builds + stack.Push(trampoline); // [SP]: ITrampoLine* dispatch pointer __mysetjmp(Buf_); diff --git a/util/system/context.h b/util/system/context.h index 6faf0582c8b..15f408a6992 100644 --- a/util/system/context.h +++ b/util/system/context.h @@ -3,6 +3,7 @@ #include "align.h" #include "defaults.h" #include "compiler.h" +#include "context_config.h" #include #include @@ -30,18 +31,6 @@ #pragma clang diagnostic ignored "-Wdeprecated-declarations" #endif -#if defined(_bionic_) || defined(__IOS__) - #define USE_GENERIC_CONT -#elif defined(_cygwin_) - #define USE_UCONTEXT_CONT -#elif defined(_win_) - #define USE_FIBER_CONT -#elif (defined(_x86_64_) || defined(_arm64_)) && !defined(_k1om_) - #define USE_JUMP_CONT -#else - #define USE_UCONTEXT_CONT -#endif - #if defined(USE_JUMP_CONT) #if defined(_arm64_) #include "context_aarch64.h" diff --git a/util/system/context_aarch64.S b/util/system/context_aarch64.S index 0b2ef4e4a69..32c279c31ea 100644 --- a/util/system/context_aarch64.S +++ b/util/system/context_aarch64.S @@ -1,3 +1,50 @@ +#include "context_config.h" + +#if defined(__APPLE__) + #define CNAME(name) _ ## name +#else + #define CNAME(name) name +#endif + +.text + +#if defined(USE_JUMP_CONT) +.p2align 2 +.global CNAME(ContextTrampoLine) +#if defined(__ELF__) + .type CNAME(ContextTrampoLine), %function +#endif +CNAME(ContextTrampoLine): + .cfi_startproc + .cfi_def_cfa sp, 0 + // Mark x30 undefined to stop unwinding (DWARF 6.4.4). + .cfi_undefined x30 +#if defined(__ARM_FEATURE_BTI_DEFAULT) && (__ARM_FEATURE_BTI_DEFAULT == 1) + hint #38 // bti jc +#endif + ldr x0, [sp] +#if !defined(NDEBUG) + ldr x1, [sp, #8] + cmp x0, x1 + b.ne 1f +#endif + // Provide a return address for unwinding. + bl CNAME(ContextTrampoLineRun) + // ContextTrampoLineRun should never return, if it did, we have a problem. + bl CNAME(abort) + brk #0 + +#if !defined(NDEBUG) +1: + bl CNAME(ContextTrampoLineAbort) + brk #0 +#endif + .cfi_endproc +#if defined(__ELF__) + .size CNAME(ContextTrampoLine), .-CNAME(ContextTrampoLine) +#endif +#endif + .p2align 2 #if !(defined __darwin__) && !(defined __arm64__) .global __mysetjmp @@ -50,3 +97,7 @@ ___mylongjmp: cbnz x1, 1f mov x0, #1 1: br x30 + +#if defined(__ELF__) + .section .note.GNU-stack,"",%progbits +#endif diff --git a/util/system/context_config.h b/util/system/context_config.h new file mode 100644 index 00000000000..fecbc2346e0 --- /dev/null +++ b/util/system/context_config.h @@ -0,0 +1,19 @@ +#pragma once + +#include "platform.h" + +#if defined(_bionic_) || defined(__IOS__) + #define USE_GENERIC_CONT +#elif defined(_cygwin_) + #define USE_UCONTEXT_CONT +#elif defined(_win_) + #define USE_FIBER_CONT +#elif (defined(_x86_64_) || defined(_arm64_)) && !defined(_k1om_) + #define USE_JUMP_CONT +#else + #define USE_UCONTEXT_CONT +#endif + +#if (defined(USE_GENERIC_CONT) + defined(USE_UCONTEXT_CONT) + defined(USE_FIBER_CONT) + defined(USE_JUMP_CONT)) != 1 + #error "exactly one context backend must be selected" +#endif diff --git a/util/system/context_ut.cpp b/util/system/context_ut.cpp index 461bef2ccac..4d1ff9324ce 100644 --- a/util/system/context_ut.cpp +++ b/util/system/context_ut.cpp @@ -1,4 +1,22 @@ +#include "context_config.h" + +// Snapshot USE_JUMP_CONT before context.h clears private backend macros. +#if defined(_unix_) && defined(USE_JUMP_CONT) + #define HAS_ASSEMBLY_CONTEXT_TRAMPOLINE 1 +#endif + #include "context.h" +#include "sanitizers.h" + +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) + #include "backtrace.h" + + #include + #include + #include + +extern "C" void ContextTrampoLine(); +#endif #include @@ -29,6 +47,17 @@ Y_UNIT_TEST_SUITE(TestContext) { return {&tw, TArrayRef(tw.Buf, sizeof(tw.Buf))}; } +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) + static void AssertChildAborted(pid_t pid) { + UNIT_ASSERT_C(pid >= 0, "fork failed"); + + int status = 0; + UNIT_ASSERT_VALUES_EQUAL(waitpid(pid, &status, 0), pid); + UNIT_ASSERT_C(WIFSIGNALED(status), status); + UNIT_ASSERT_VALUES_EQUAL(WTERMSIG(status), SIGABRT); + } +#endif + Y_UNIT_TEST(TestExceptionSafety) { TExceptionSafeContext main; TExceptionSafeContext* volatile nextPtr = nullptr; @@ -68,4 +97,132 @@ Y_UNIT_TEST_SUITE(TestContext) { UNIT_ASSERT(throwed); UNIT_ASSERT(!hasUncaught); } + +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) && !defined(_musl_) + Y_UNIT_TEST(TestUnwindStopsAtTrampoline) { + static constexpr size_t MaxFrames = 64; + + TExceptionSafeContext main; + TExceptionSafeContext* nextPtr = nullptr; + + size_t frameCount = 0; + size_t meaningfulFrameCount = 0; + size_t trampolineFrameCount = 0; + size_t trampolineFrameIndex = 0; + #if defined(_x86_64_) + void** inheritedReturnAddressSlot = nullptr; + #endif + + auto func = [&]() { + const void* const trampolineAddress = reinterpret_cast(&ContextTrampoLine); + + #if defined(_x86_64_) + UNIT_ASSERT(inheritedReturnAddressSlot != nullptr); + void* const inheritedReturnAddress = *inheritedReturnAddressSlot; + UNIT_ASSERT(inheritedReturnAddress != nullptr); + + char inheritedSymbolBuffer[1024]; + const auto inheritedSymbol = ResolveSymbol(inheritedReturnAddress, inheritedSymbolBuffer, sizeof(inheritedSymbolBuffer)); + UNIT_ASSERT_VALUES_EQUAL(inheritedSymbol.NearestSymbol, trampolineAddress); + #endif + + void* frames[MaxFrames]; + frameCount = BackTrace(frames, Y_ARRAY_SIZE(frames)); + meaningfulFrameCount = frameCount; + // Some _Unwind_Backtrace implementations report a terminal zero-IP context. + if (frameCount != 0 && frames[frameCount - 1] == nullptr) { + --meaningfulFrameCount; + } + + for (size_t index = 0; index < meaningfulFrameCount; ++index) { + char symbolBuffer[1024]; + const auto symbol = ResolveSymbol(frames[index], symbolBuffer, sizeof(symbolBuffer)); + if (symbol.NearestSymbol == trampolineAddress) { + ++trampolineFrameCount; + trampolineFrameIndex = index; + } + } + + nextPtr->SwitchTo(&main); + }; + + auto closure = Wrap(func); + TExceptionSafeContext next(closure); + nextPtr = &next; + + #if defined(_x86_64_) + // The assembly CALL initializes this slot without updating MSan shadow state. + // Its nonzero value ensures the CFI root, not a zero return address, stops unwinding. + auto* stackTop = AlignDown(closure.Stack.data() + closure.Stack.size(), STACK_ALIGN); + inheritedReturnAddressSlot = reinterpret_cast(stackTop) - 3; + NSan::Unpoison(inheritedReturnAddressSlot, sizeof(*inheritedReturnAddressSlot)); + #endif + + main.SwitchTo(&next); + + UNIT_ASSERT_C(frameCount + 1 < MaxFrames, frameCount); + UNIT_ASSERT_VALUES_EQUAL(trampolineFrameCount, 1); + UNIT_ASSERT_VALUES_EQUAL(trampolineFrameIndex + 1, meaningfulFrameCount); + } +#endif + +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) + Y_UNIT_TEST(TestReturningTrampolineAborts) { + struct TReturningTrampoline final: ITrampoLine { + void DoRunNaked() override { + } + + char Stack[1000000]; + }; + + static TReturningTrampoline trampoline; + const pid_t pid = fork(); + + if (pid == 0) { + TExceptionSafeContext main; + TExceptionSafeContext next({&trampoline, TArrayRef(trampoline.Stack, sizeof(trampoline.Stack))}); + + main.SwitchTo(&next); + _exit(0); + } + + AssertChildAborted(pid); + } +#endif + +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) && !defined(NDEBUG) + Y_UNIT_TEST(TestCorruptedTrampolineArguments) { + const auto run = [](ptrdiff_t corruptedCopy) { + const pid_t pid = fork(); + + if (pid == 0) { + TExceptionSafeContext main; + TExceptionSafeContext* nextPtr = nullptr; + + auto func = [&]() { + nextPtr->SwitchTo(&main); + }; + + auto closure = Wrap(func); + TExceptionSafeContext next(closure); + nextPtr = &next; + + auto* stackTop = AlignDown(closure.Stack.data() + closure.Stack.size(), STACK_ALIGN); + reinterpret_cast(stackTop)[corruptedCopy] = nullptr; + + main.SwitchTo(&next); + _exit(0); + } + + AssertChildAborted(pid); + }; + + run(-1); + run(-2); + } +#endif } // Y_UNIT_TEST_SUITE(TestContext) + +#if defined(HAS_ASSEMBLY_CONTEXT_TRAMPOLINE) + #undef HAS_ASSEMBLY_CONTEXT_TRAMPOLINE +#endif diff --git a/util/system/context_x86.S b/util/system/context_x86.S index 30cf154edb3..0e8fac3a264 100644 --- a/util/system/context_x86.S +++ b/util/system/context_x86.S @@ -1,3 +1,5 @@ +#include "context_config.h" + #if defined(__APPLE__) #define CNAME(name) _ ## name #else @@ -25,6 +27,43 @@ #define MJB_PC 7 #define MJB_SIZE (8 * 8) +#if defined(USE_JUMP_CONT) +.p2align 4 +.globl CNAME(ContextTrampoLine) +#if defined(__ELF__) + .type CNAME(ContextTrampoLine), @function +#endif +CNAME(ContextTrampoLine): + .cfi_startproc + .cfi_def_cfa rsp, 0 + // Mark RIP undefined to stop unwinding here (DWARF 6.4.4). + .cfi_undefined rip +#if defined(__CET__) && (__CET__ & 1) + endbr64 +#endif + mov rdi, [rsp] +#if !defined(NDEBUG) + cmp rdi, [rsp + 8] + jne 1f +#endif + // Shift RSP to 8 mod 16, provide a return address for unwinding. + call CNAME(ContextTrampoLineRun) + // ContextTrampoLineRun should never return, if it did, we have a problem. + call CNAME(abort) + ud2 + +#if !defined(NDEBUG) +1: + mov rsi, [rsp + 8] + call CNAME(ContextTrampoLineAbort) + ud2 +#endif + .cfi_endproc +#if defined(__ELF__) + .size CNAME(ContextTrampoLine), .-CNAME(ContextTrampoLine) +#endif +#endif + EXPORT(__mylongjmp, 1) mov rbx, [rdi + MJB_RBX * 8] mov rbp, [rdi + MJB_RBP * 8] @@ -53,3 +92,7 @@ EXPORT(__mysetjmp, 2) mov [rdi + MJB_PC * 8], rax mov eax, 0 ret + +#if defined(__ELF__) + .section .note.GNU-stack,"",@progbits +#endif From 87448b3878750aaa0faca863dc898815395367a5 Mon Sep 17 00:00:00 2001 From: Nikolay Perfilov Date: Fri, 30 Oct 2026 11:49:27 +0000 Subject: [PATCH 07/15] CLI: remove Solomon volumes, resource pools and backup collections recursively (#56731) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 2 ++ include/ydb-cpp-sdk/client/scheme/scheme.h | 1 + src/api/protos/ydb_scheme.proto | 1 + src/client/impl/internal/scheme_helpers/helpers.cpp | 2 ++ src/client/scheme/scheme.cpp | 2 ++ 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 1b5e6dac7b4..d84586a2cbb 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -6c42a49c09af2a08fa8f929acf047c7ef5518b38 +bad2bccee896c3778a2d9c9c313afe9972cdcb6e diff --git a/CHANGELOG.md b/CHANGELOG.md index 00d6153ede5..e97ae33552e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## v3.25.0 +* Added recognition of Solomon volumes in Scheme API results and a draft `NKeyValue::TKeyValueClient::DropVolume()` method. + * Added `TExecuteQueryIterator::Cancel()` to explicitly cancel a query response stream, including a pending read. * Added `TBridgeClient::RemovePile()` to remove a DISCONNECTED pile from a bridged cluster along with its data. diff --git a/include/ydb-cpp-sdk/client/scheme/scheme.h b/include/ydb-cpp-sdk/client/scheme/scheme.h index 0cba1491c6e..ad9d8355bcf 100644 --- a/include/ydb-cpp-sdk/client/scheme/scheme.h +++ b/include/ydb-cpp-sdk/client/scheme/scheme.h @@ -42,6 +42,7 @@ enum class ESchemeEntryType : i32 { RtmrVolume = 5, BlockStoreVolume = 6, CoordinationNode = 7, + SolomonVolume = 8, ColumnStore = 12, ColumnTable = 13, Sequence = 15, diff --git a/src/api/protos/ydb_scheme.proto b/src/api/protos/ydb_scheme.proto index c15e6853f98..9caa4d7c9b4 100644 --- a/src/api/protos/ydb_scheme.proto +++ b/src/api/protos/ydb_scheme.proto @@ -57,6 +57,7 @@ message Entry { RTMR_VOLUME = 5; BLOCK_STORE_VOLUME = 6; COORDINATION_NODE = 7; + SOLOMON_VOLUME = 8; COLUMN_STORE = 12; COLUMN_TABLE = 13; SEQUENCE = 15; diff --git a/src/client/impl/internal/scheme_helpers/helpers.cpp b/src/client/impl/internal/scheme_helpers/helpers.cpp index 885fd928626..f513d101c28 100644 --- a/src/client/impl/internal/scheme_helpers/helpers.cpp +++ b/src/client/impl/internal/scheme_helpers/helpers.cpp @@ -27,6 +27,8 @@ ::Ydb::Scheme::Entry::Type SchemeEntryTypeToProto(ESchemeEntryType type) { return ::Ydb::Scheme::Entry::BLOCK_STORE_VOLUME; case ESchemeEntryType::CoordinationNode: return ::Ydb::Scheme::Entry::COORDINATION_NODE; + case ESchemeEntryType::SolomonVolume: + return ::Ydb::Scheme::Entry::SOLOMON_VOLUME; case ESchemeEntryType::Sequence: return ::Ydb::Scheme::Entry::SEQUENCE; case ESchemeEntryType::Replication: diff --git a/src/client/scheme/scheme.cpp b/src/client/scheme/scheme.cpp index ae2d91f60c2..18e54004417 100644 --- a/src/client/scheme/scheme.cpp +++ b/src/client/scheme/scheme.cpp @@ -47,6 +47,8 @@ static ESchemeEntryType ConvertProtoEntryType(::Ydb::Scheme::Entry::Type entry) return ESchemeEntryType::BlockStoreVolume; case ::Ydb::Scheme::Entry::COORDINATION_NODE: return ESchemeEntryType::CoordinationNode; + case ::Ydb::Scheme::Entry::SOLOMON_VOLUME: + return ESchemeEntryType::SolomonVolume; case ::Ydb::Scheme::Entry::SEQUENCE: return ESchemeEntryType::Sequence; case ::Ydb::Scheme::Entry::REPLICATION: From b9c074c090d3a6434f6659defcfb880182c48572 Mon Sep 17 00:00:00 2001 From: Alexander Rutkovsky Date: Fri, 30 Oct 2026 11:49:44 +0000 Subject: [PATCH 08/15] Deprecate quorum_piles of bridge cluster state update (#57230) --- .github/last_commit.txt | 2 +- src/api/protos/draft/ydb_bridge.proto | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index d84586a2cbb..558b2bf5a81 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -bad2bccee896c3778a2d9c9c313afe9972cdcb6e +07371b18b7cd137da92b504f89aaf2d23128320f diff --git a/src/api/protos/draft/ydb_bridge.proto b/src/api/protos/draft/ydb_bridge.proto index 1c3b788863f..4e3d73729ac 100644 --- a/src/api/protos/draft/ydb_bridge.proto +++ b/src/api/protos/draft/ydb_bridge.proto @@ -87,9 +87,9 @@ message UpdateClusterStateRequest { Ydb.Operations.OperationParams operation_params = 1; // List of desired pile states to update repeated PileState updates = 2 [(size).ge = 1]; - // List of piles that are required to form a quorum for this update to succeed. - // These piles act as "witnesses" for the operation. - // If empty, all piles in the cluster will be used for the quorum check. + // Deprecated and ignored. The update is committed by the quorum of every pile that takes part in the + // configuration quorum in the new cluster state, that is, every pile not DISCONNECTED, SUSPENDED or INACTIVE + // there; to go on without an unavailable pile, set it DISCONNECTED in the same update. repeated string quorum_piles = 3; } From 2271bb62f0542bcd8dc96b8ce68a87d4a5c248a1 Mon Sep 17 00:00:00 2001 From: Ermoshkin Artem <94714022+Shfdis@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:49:52 +0000 Subject: [PATCH 09/15] Fix Query session shutdown during pool return (#57159) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 2 + .../impl/session/kqp_session_common.cpp | 23 ++- src/client/impl/session/kqp_session_common.h | 5 +- src/client/impl/session/session_pool.cpp | 21 ++- .../query/impl/session_state_handler.cpp | 9 +- tests/unit/client/query/client_session_ut.cpp | 149 ++++++++++++++++++ .../query/deferred_session_creation_ut.cpp | 83 +--------- tests/unit/client/query/mock_query_service.h | 97 ++++++++++++ 9 files changed, 296 insertions(+), 95 deletions(-) create mode 100644 tests/unit/client/query/mock_query_service.h diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 558b2bf5a81..e0ebd1340b9 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -07371b18b7cd137da92b504f89aaf2d23128320f +6475be4cf75489445cd1c1cd2e723dcd966da7c1 diff --git a/CHANGELOG.md b/CHANGELOG.md index e97ae33552e..4a6dd4dfb35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Fixed Query session shutdown racing with session pool return. + * Added `TConvertToBridgeSettings::SkipStoragePoolConversion()` to leave existing storage pools and their groups non-bridged when converting a cluster into a bridged one. ## v3.25.0 diff --git a/src/client/impl/session/kqp_session_common.cpp b/src/client/impl/session/kqp_session_common.cpp index bb7c4f2a2aa..b5975c1d9d5 100644 --- a/src/client/impl/session/kqp_session_common.cpp +++ b/src/client/impl/session/kqp_session_common.cpp @@ -58,7 +58,7 @@ const TEndpointKey& TKqpSessionCommon::GetEndpointKey() const { // Can be called from interceptor, need lock bool TKqpSessionCommon::MarkBroken() { std::lock_guard guard(Lock_); - const bool firstTerminal = State_ != EState::S_BROKEN && State_ != EState::S_CLOSING; + const bool firstTerminal = !CloseRequested_ && State_ != EState::S_BROKEN; if (State_ == EState::S_ACTIVE) { NeedUpdateActiveCounter_ = true; } @@ -68,13 +68,21 @@ bool TKqpSessionCommon::MarkBroken() { bool TKqpSessionCommon::MarkAsClosing() { std::lock_guard guard(Lock_); - const bool firstTerminal = State_ != EState::S_BROKEN && State_ != EState::S_CLOSING; + if (CloseRequested_ || State_ == S_BROKEN) { + return false; + } + CloseRequested_ = true; + // Idle sessions may already be selected for checkout. Leave their state + // intact, but remember shutdown even before the pool installs its handler. + if (State_ == S_IDLE) { + return true; + } if (State_ == EState::S_ACTIVE) { NeedUpdateActiveCounter_ = true; } State_ = EState::S_CLOSING; - return firstTerminal; + return true; } bool TKqpSessionCommon::MarkActive() { @@ -89,7 +97,7 @@ bool TKqpSessionCommon::MarkActive() { bool TKqpSessionCommon::MarkIdle() { std::lock_guard guard(Lock_); - if (State_ == EState::S_BROKEN || State_ == EState::S_CLOSING) { + if (CloseRequested_ || State_ == EState::S_BROKEN) { return false; } State_ = EState::S_IDLE; @@ -97,13 +105,18 @@ bool TKqpSessionCommon::MarkIdle() { return true; } +bool TKqpSessionCommon::CanReturnToPool() const { + std::lock_guard guard(Lock_); + return State_ == S_IDLE && !CloseRequested_; +} + bool TKqpSessionCommon::IsOwnedBySessionPool() const { return IsOwnedBySessionPool_; } TKqpSessionCommon::EState TKqpSessionCommon::GetState() const { // See comments in InjectSessionStatusInterception about lock - std::lock_guard guard(const_cast(Lock_)); + std::lock_guard guard(Lock_); return State_; } diff --git a/src/client/impl/session/kqp_session_common.h b/src/client/impl/session/kqp_session_common.h index a552702531c..668d3942ccd 100644 --- a/src/client/impl/session/kqp_session_common.h +++ b/src/client/impl/session/kqp_session_common.h @@ -44,9 +44,11 @@ class TKqpSessionCommon : public TEndpointObj { const std::string& GetEndpoint() const; const TEndpointKey& GetEndpointKey() const; bool MarkBroken(); + // Returns true for the first close request; idle sessions keep S_IDLE until pool removal. bool MarkAsClosing(); bool MarkActive(); bool MarkIdle(); + bool CanReturnToPool() const; bool IsOwnedBySessionPool() const; EState GetState() const; void SetNeedUpdateActiveCounter(bool flag); @@ -71,7 +73,7 @@ class TKqpSessionCommon : public TEndpointObj { std::optional PropagatedDeadline_; protected: - TAdaptiveLock Lock_; + mutable TAdaptiveLock Lock_; private: const std::string SessionId_; @@ -79,6 +81,7 @@ class TKqpSessionCommon : public TEndpointObj { const bool IsOwnedBySessionPool_; EState State_; + bool CloseRequested_ = false; // This time is used during async close session handling which does not lock the session // so we need to be able to read this value atomicaly std::atomic TimeToTouch_; diff --git a/src/client/impl/session/session_pool.cpp b/src/client/impl/session/session_pool.cpp index 9ce4d9600c4..686648f5b13 100644 --- a/src/client/impl/session/session_pool.cpp +++ b/src/client/impl/session/session_pool.cpp @@ -62,6 +62,7 @@ bool IsSessionCloseRequested(const TStatus& status) { } void TSessionCloseCommand::Execute(TKqpSessionCommon& session, ISessionClient* client) const { + // Count each session once; the first close reason wins. if (Transition(session) && client && session.IsOwnedBySessionPool()) { client->RecordSessionClosed(Reason); } @@ -268,19 +269,22 @@ bool TSessionPool::ReturnSession(TKqpSessionCommon* impl, bool active) { std::unique_ptr getSessionCtx; { std::lock_guard guard(Mtx_); - if (Closed_ || impl->GetState() != TKqpSessionCommon::S_IDLE) + if (Closed_) + return false; + + // Register before checking eligibility so shutdown cannot miss the pool. + impl->UpdateServerCloseHandler(this); + if (!impl->CanReturnToPool()) { + impl->UpdateServerCloseHandler(nullptr); return false; + } if (auto maybeCtx = WaitersQueue_.TryGet()) { + impl->UpdateServerCloseHandler(nullptr); getSessionCtx = std::move(maybeCtx); if (!active) IncrementActiveCounterUnsafe(); } else { - impl->UpdateServerCloseHandler(this); - if (impl->GetState() != TKqpSessionCommon::S_IDLE) { - impl->UpdateServerCloseHandler(nullptr); - return false; - } Sessions_.emplace(std::make_pair( impl->GetTimeToTouchFast(), impl)); @@ -457,8 +461,11 @@ void TSessionPool::OnCloseSession(const TKqpSessionCommon* s, std::shared_ptrGetState() == TKqpSessionCommon::S_IDLE); - RecordSessionClosed(reason); + if (session->MarkBroken()) { + RecordSessionClosed(reason); + } CloseAndDeleteSession(std::move(session), client); } } diff --git a/src/client/query/impl/session_state_handler.cpp b/src/client/query/impl/session_state_handler.cpp index 1dae07e81b4..dd08b07a5ee 100644 --- a/src/client/query/impl/session_state_handler.cpp +++ b/src/client/query/impl/session_state_handler.cpp @@ -22,12 +22,9 @@ EAttachStreamReadAction HandleAttachSessionState( client->PessimizeNode(nodeId); } } - if (session->GetState() == TKqpSessionCommon::S_IDLE) { - if (client) { - session->CloseFromServer(client, closeCommand.Reason); - } - } else { - closeCommand.Execute(*session, client.get()); + closeCommand.Execute(*session, client.get()); + if (client) { + session->CloseFromServer(client, closeCommand.Reason); } return EAttachStreamReadAction::Stop; } diff --git a/tests/unit/client/query/client_session_ut.cpp b/tests/unit/client/query/client_session_ut.cpp index a6aa5780ef2..c3206e2253a 100644 --- a/tests/unit/client/query/client_session_ut.cpp +++ b/tests/unit/client/query/client_session_ut.cpp @@ -1,3 +1,17 @@ +#include "mock_query_service.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + #include #define INCLUDE_YDB_INTERNAL_H @@ -14,6 +28,8 @@ using namespace NYdb; using namespace NYdb::NQuery; +using NYdb::NTests::StartGrpcServer; +using NYdb::NTests::TMockQueryService; namespace { @@ -87,6 +103,86 @@ Ydb::Query::SessionState MakeNodeShutdownState() { return state; } +template +void WaitUntil(TPredicate predicate) { + const auto deadline = TInstant::Now() + TDuration::Seconds(10); + while (!predicate() && TInstant::Now() < deadline) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + UNIT_ASSERT(predicate()); +} + +TSession GetSession(TAsyncCreateSessionResult future) { + UNIT_ASSERT(future.Wait(TDuration::Seconds(10))); + auto result = future.ExtractValueSync(); + UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString()); + return result.GetSession(); +} + +enum class EShutdownOrder { Active, Idle, Waiter, ConcurrentReturn }; + +void TestSessionShutdown(EShutdownOrder order, bool closeStream = false) { + using namespace NSessionPool::NSessionCloseCommands; + const NSessionPool::TSessionCloseCommand* shutdowns[] = {&SessionShutdown, &NodeShutdown}; + const NSessionPool::TSessionCloseCommand* closures[] = {&AttachClosed, &TransportError}; + for (const auto* command : closeStream ? closures : shutdowns) { + for (bool deferred : {false, true}) { + NTesting::InitPortManagerFromEnv(); + const auto endpoint = TStringBuilder() << "127.0.0.1:" << NTesting::GetFreePort(); + TManualEvent shutdown; + TMockQueryService service; + service.AttachStatus = grpc::Status(command == &AttachClosed ? grpc::StatusCode::OUT_OF_RANGE + : command == &TransportError ? grpc::StatusCode::UNAVAILABLE : grpc::StatusCode::OK, "test attach status"); + service.OnAttach = [&](auto* writer) { + if (shutdown.WaitT(TDuration::Seconds(10)) && !closeStream) { + writer->Write(command == &NodeShutdown ? MakeNodeShutdownState() : MakeSessionShutdownState()); + } + }; + auto server = StartGrpcServer(endpoint, service); + UNIT_ASSERT(server); + auto metrics = std::make_shared(); + TDriver driver(TDriverConfig().SetEndpoint(endpoint).SetDiscoveryMode(EDiscoveryMode::Off) + .SetDatabase("/Root/My/DB").SetMetricRegistry(metrics)); + Y_DEFER { driver.Stop(true); }; + TQueryClient client(driver, TClientSettings().PoolName("shutdown").SessionPoolSettings( + TSessionPoolSettings().MaxActiveSessions(1).MinPoolSize(1).UseDeferredSessionCreation(deferred))); + Y_DEFER { shutdown.Signal(); }; + std::optional session = GetSession(client.GetSession()); + const auto oldId = session->GetId(); + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), 1); + if (order == EShutdownOrder::Idle) session.reset(); + UNIT_ASSERT_VALUES_EQUAL(client.GetCurrentPoolSize(), order == EShutdownOrder::Idle ? 1 : 0); + auto waiter = order == EShutdownOrder::Waiter ? client.GetSession() : TAsyncCreateSessionResult{}; + UNIT_ASSERT(!waiter.Initialized() || !waiter.HasValue()); + shutdown.Signal(); + if (order == EShutdownOrder::ConcurrentReturn) session.reset(); + const NMetrics::TLabels labels = {{"ydb.query.session.pool.name", "shutdown"}, {"reason", std::string(command->Reason)}}; + WaitUntil([&] { + auto count = metrics->GetCounter("ydb.query.session.closed", labels); + return count && count->Get() >= 1; + }); + UNIT_ASSERT(!waiter.Initialized() || !waiter.HasValue()); + if (session) { + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), 1); + UNIT_ASSERT_VALUES_EQUAL(client.GetCurrentPoolSize(), 0); + } + session.reset(); + WaitUntil([&] { return service.ClosedSessions.load() == 1; }); + UNIT_ASSERT_VALUES_EQUAL(client.GetCurrentPoolSize(), 0); + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), waiter.Initialized() ? 1 : 0); + { + auto replacement = GetSession(waiter.Initialized() ? waiter : client.GetSession()); + UNIT_ASSERT(replacement.GetId() != oldId); + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), 1); + } + UNIT_ASSERT_VALUES_EQUAL(service.CreateSessionRequests.load(), 2); + UNIT_ASSERT_VALUES_EQUAL(client.GetCurrentPoolSize(), 1); + UNIT_ASSERT_VALUES_EQUAL(client.GetActiveSessionCount(), 0); + UNIT_ASSERT_VALUES_EQUAL(metrics->GetCounter("ydb.query.session.closed", labels)->Get(), 1); + } + } +} + } // namespace Y_UNIT_TEST_SUITE(QueryAttachSessionState) { @@ -155,6 +251,48 @@ Y_UNIT_TEST(SessionShutdownNullSessionStopsReading) { UNIT_ASSERT_VALUES_EQUAL(client->PessimizeCalls, 0); } +Y_UNIT_TEST(ShutdownPreservesStateAndRejectsPoolReturn) { + using TPrepare = bool (TKqpSessionCommon::*)(); + for (TPrepare prepare : {TPrepare{}, &TKqpSessionCommon::MarkActive, &TKqpSessionCommon::MarkIdle, + &TKqpSessionCommon::MarkBroken, &TKqpSessionCommon::MarkAsClosing}) { + for (const auto& shutdown : {MakeSessionShutdownState(), MakeNodeShutdownState()}) { + for (bool withClient : {false, true}) { + for (bool closedPool : {false, true}) { + auto session = std::make_unique(MakeSessionIdWithNodeId(42), "host:2136", true); + if (prepare == &TKqpSessionCommon::MarkBroken) session->MarkActive(); + if (prepare) UNIT_ASSERT((session.get()->*prepare)()); + const auto initial = session->GetState(); + auto client = withClient ? std::make_shared() : nullptr; + NSessionPool::TSessionPool pool(1); + if (closedPool) pool.Drain([](auto&&) { return true; }, true); + auto expectRejected = [&] { + const bool returned = pool.ReturnSession(session.get(), false); + if (returned) session.release(); + UNIT_ASSERT(!returned); + }; + if (closedPool || initial != TKqpSessionCommon::S_IDLE) expectRejected(); + UNIT_ASSERT(HandleAttachSessionState(shutdown, session.get(), client) == EAttachStreamReadAction::Stop); + const auto expected = initial == TKqpSessionCommon::S_IDLE || initial == TKqpSessionCommon::S_BROKEN + ? initial : TKqpSessionCommon::S_CLOSING; + UNIT_ASSERT(session->GetState() == expected); + UNIT_ASSERT_VALUES_EQUAL(session->NeedUpdateActiveCounter(), + initial == TKqpSessionCommon::S_ACTIVE || initial == TKqpSessionCommon::S_BROKEN); + UNIT_ASSERT(!session->CanReturnToPool()); + expectRejected(); + // A checkout selected before shutdown may finish, but cannot return to the pool. + UNIT_ASSERT_VALUES_EQUAL(session->MarkActive(), initial == TKqpSessionCommon::S_IDLE); + HandleAttachSessionState(shutdown, session.get(), client); + UNIT_ASSERT(!session->MarkIdle()); + if (client) UNIT_ASSERT_VALUES_EQUAL(client->CloseMetrics, + initial == TKqpSessionCommon::S_BROKEN || initial == TKqpSessionCommon::S_CLOSING ? 0 : 1); + UNIT_ASSERT_VALUES_EQUAL(pool.GetCurrentPoolSize(), 0); + UNIT_ASSERT_VALUES_EQUAL(pool.GetActiveSessions(), 0); + } + } + } + } +} + Y_UNIT_TEST(CloseReasonCommandsAreCompleteAndDeduplicated) { const std::pair commands[] = { {&NSessionPool::NSessionCloseCommands::PoolIdleTimeout, "pool_idle_timeout"}, @@ -174,6 +312,9 @@ Y_UNIT_TEST(CloseReasonCommandsAreCompleteAndDeduplicated) { command->Execute(session, client.get()); command->Execute(session, client.get()); UNIT_ASSERT_VALUES_EQUAL(client->LastReason, reason); + session.MarkBroken(); + command->Execute(session, client.get()); + UNIT_ASSERT(session.GetState() == TKqpSessionCommon::S_BROKEN); } UNIT_ASSERT_VALUES_EQUAL(client->CloseMetrics, 10); @@ -212,3 +353,11 @@ Y_UNIT_TEST_SUITE(QuerySessionStatusInterception) { } } // Y_UNIT_TEST_SUITE(QuerySessionStatusInterception) + +Y_UNIT_TEST_SUITE(QuerySessionShutdown) { +Y_UNIT_TEST(ActiveSession) { TestSessionShutdown(EShutdownOrder::Active); } +Y_UNIT_TEST(IdleSession) { TestSessionShutdown(EShutdownOrder::Idle); } +Y_UNIT_TEST(QueuedWaiter) { TestSessionShutdown(EShutdownOrder::Waiter); } +Y_UNIT_TEST(ConcurrentReturn) { TestSessionShutdown(EShutdownOrder::ConcurrentReturn); } +Y_UNIT_TEST(IdleAttachClosure) { TestSessionShutdown(EShutdownOrder::Idle, true); } +} diff --git a/tests/unit/client/query/deferred_session_creation_ut.cpp b/tests/unit/client/query/deferred_session_creation_ut.cpp index e519c51eeee..7a543760260 100644 --- a/tests/unit/client/query/deferred_session_creation_ut.cpp +++ b/tests/unit/client/query/deferred_session_creation_ut.cpp @@ -1,3 +1,5 @@ +#include "mock_query_service.h" + #include #include @@ -6,88 +8,19 @@ #include -#include - -#include -#include -#include - -#include +#include #include using namespace NYdb; using namespace NYdb::NQuery; +using NYdb::NTests::StartGrpcServer; +using NYdb::NTests::TMockQueryService; namespace { constexpr TDuration kShortDeadline = TDuration::MilliSeconds(50); constexpr TDuration kSlowAttach = TDuration::MilliSeconds(300); -class TDelayedMockQueryService : public Ydb::Query::V1::QueryService::Service { -public: - TDuration AttachDelay = TDuration::Zero(); - std::atomic_uint CreateSessionRequests = 0; - Ydb::StatusIds::StatusCode TxStatus = Ydb::StatusIds::SUCCESS; - grpc::StatusCode TxRpcStatus = grpc::StatusCode::OK; - - grpc::Status CreateSession( - grpc::ServerContext*, - const Ydb::Query::CreateSessionRequest*, - Ydb::Query::CreateSessionResponse* response) override - { - ++CreateSessionRequests; - response->set_status(Ydb::StatusIds::SUCCESS); - response->set_session_id("fake-query-session-id"); - response->set_node_id(1); - return grpc::Status::OK; - } - - grpc::Status AttachSession( - grpc::ServerContext* context, - const Ydb::Query::AttachSessionRequest*, - grpc::ServerWriter* writer) override - { - if (AttachDelay != TDuration::Zero()) { - std::this_thread::sleep_for(std::chrono::milliseconds(AttachDelay.MilliSeconds())); - } - Ydb::Query::SessionState state; - state.set_status(Ydb::StatusIds::SUCCESS); - writer->Write(state); - while (!context->IsCancelled()) { - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - } - return grpc::Status::OK; - } - - grpc::Status BeginTransaction(grpc::ServerContext*, const Ydb::Query::BeginTransactionRequest*, - Ydb::Query::BeginTransactionResponse* response) override { - response->set_status(Ydb::StatusIds::SUCCESS); - response->mutable_tx_meta()->set_id("fake-query-transaction-id"); - return grpc::Status::OK; - } - - grpc::Status CommitTransaction(grpc::ServerContext*, const Ydb::Query::CommitTransactionRequest*, - Ydb::Query::CommitTransactionResponse* response) override { - response->set_status(TxStatus); - return grpc::Status(TxRpcStatus, "mock transaction status"); - } - - grpc::Status RollbackTransaction(grpc::ServerContext*, const Ydb::Query::RollbackTransactionRequest*, - Ydb::Query::RollbackTransactionResponse* response) override { - response->set_status(TxStatus); - return grpc::Status(TxRpcStatus, "mock transaction status"); - } - -}; - -template -std::unique_ptr StartGrpcServer(const std::string& address, TService& service) { - return grpc::ServerBuilder() - .AddListeningPort(TString{address}, grpc::InsecureServerCredentials()) - .RegisterService(&service) - .BuildAndStart(); -} - TCreateSessionSettings ShortDeadlineSettings() { return TCreateSessionSettings() .ClientTimeout(kShortDeadline) @@ -110,7 +43,7 @@ Y_UNIT_TEST(TimeoutThenPoolWarmup) { const auto port = NTesting::GetFreePort(); const auto endpoint = TStringBuilder() << "127.0.0.1:" << port; - TDelayedMockQueryService service; + TMockQueryService service; service.AttachDelay = kSlowAttach; auto server = StartGrpcServer(endpoint, service); @@ -143,7 +76,7 @@ Y_UNIT_TEST(DisabledWaitsForAttach) { const auto port = NTesting::GetFreePort(); const auto endpoint = TStringBuilder() << "127.0.0.1:" << port; - TDelayedMockQueryService service; + TMockQueryService service; service.AttachDelay = kSlowAttach; auto server = StartGrpcServer(endpoint, service); @@ -171,7 +104,7 @@ Y_UNIT_TEST(TransactionSessionStatus) { for (EStatus status : {EStatus::SUCCESS, EStatus::ABORTED, EStatus::BAD_SESSION, EStatus::CLIENT_DEADLINE_EXCEEDED}) { const auto port = NTesting::GetFreePort(); const auto endpoint = TStringBuilder() << "127.0.0.1:" << port; - TDelayedMockQueryService service; + TMockQueryService service; if (status == EStatus::CLIENT_DEADLINE_EXCEEDED) { service.TxRpcStatus = grpc::StatusCode::DEADLINE_EXCEEDED; } else { diff --git a/tests/unit/client/query/mock_query_service.h b/tests/unit/client/query/mock_query_service.h new file mode 100644 index 00000000000..72b8c02c182 --- /dev/null +++ b/tests/unit/client/query/mock_query_service.h @@ -0,0 +1,97 @@ +#pragma once + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace NYdb::NTests { + +class TMockQueryService : public Ydb::Query::V1::QueryService::Service { +public: + TDuration AttachDelay = TDuration::Zero(); + std::atomic_uint CreateSessionRequests = 0; + std::atomic_uint ClosedSessions = 0; + std::function*)> OnAttach; + std::string OnAttachSessionId = "fake-query-session-id-1"; + grpc::Status AttachStatus = grpc::Status::OK; + Ydb::StatusIds::StatusCode TxStatus = Ydb::StatusIds::SUCCESS; + grpc::StatusCode TxRpcStatus = grpc::StatusCode::OK; + + grpc::Status CreateSession( + grpc::ServerContext*, + const Ydb::Query::CreateSessionRequest*, + Ydb::Query::CreateSessionResponse* response) override + { + response->set_status(Ydb::StatusIds::SUCCESS); + response->set_session_id("fake-query-session-id-" + std::to_string(++CreateSessionRequests)); + response->set_node_id(1); + return grpc::Status::OK; + } + + grpc::Status AttachSession( + grpc::ServerContext* context, + const Ydb::Query::AttachSessionRequest* request, + grpc::ServerWriter* writer) override + { + Y_DEFER { ++ClosedSessions; }; + if (AttachDelay != TDuration::Zero()) { + std::this_thread::sleep_for(std::chrono::milliseconds(AttachDelay.MilliSeconds())); + } + Ydb::Query::SessionState state; + state.set_status(Ydb::StatusIds::SUCCESS); + writer->Write(state); + if (OnAttach && request->session_id() == OnAttachSessionId) { + OnAttach(writer); + if (!AttachStatus.ok()) { + return AttachStatus; + } + } + while (!context->IsCancelled()) { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } + return grpc::Status::OK; + } + + grpc::Status BeginTransaction(grpc::ServerContext*, const Ydb::Query::BeginTransactionRequest*, + Ydb::Query::BeginTransactionResponse* response) override { + response->set_status(Ydb::StatusIds::SUCCESS); + response->mutable_tx_meta()->set_id("fake-query-transaction-id"); + return grpc::Status::OK; + } + + grpc::Status CommitTransaction(grpc::ServerContext*, const Ydb::Query::CommitTransactionRequest*, + Ydb::Query::CommitTransactionResponse* response) override { + response->set_status(TxStatus); + return grpc::Status(TxRpcStatus, "mock transaction status"); + } + + grpc::Status RollbackTransaction(grpc::ServerContext*, const Ydb::Query::RollbackTransactionRequest*, + Ydb::Query::RollbackTransactionResponse* response) override { + response->set_status(TxStatus); + return grpc::Status(TxRpcStatus, "mock transaction status"); + } + +}; + +template +std::unique_ptr StartGrpcServer(const std::string& address, TService& service) { + return grpc::ServerBuilder() + .AddListeningPort(TString{address}, grpc::InsecureServerCredentials()) + .RegisterService(&service) + .BuildAndStart(); +} + +} // namespace NYdb::NTests From 3bfcd083a83513917cfcb2c3a152a7378f205342 Mon Sep 17 00:00:00 2001 From: Nikolay Shestakov Date: Fri, 30 Oct 2026 11:50:01 +0000 Subject: [PATCH 10/15] federated topic: follow federation membership in read sessions (#56544) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 2 + .../client/federated_topic/federated_topic.h | 11 + .../impl/federated_read_session.cpp | 396 +++++++-- .../impl/federated_read_session.h | 105 ++- .../impl/federation_observer.cpp | 32 +- .../impl/federation_observer.h | 3 + .../federated_topic/ut/basic_usage_ut.cpp | 1 - .../ut/read_session_membership_ut.cpp | 810 ++++++++++++++++++ 9 files changed, 1260 insertions(+), 102 deletions(-) create mode 100644 src/client/federated_topic/ut/read_session_membership_ut.cpp diff --git a/.github/last_commit.txt b/.github/last_commit.txt index e0ebd1340b9..3d2ad1086b2 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -6475be4cf75489445cd1c1cd2e723dcd966da7c1 +27f9c3828d59dbfeaa81ae37180132d29db2f59e diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6dd4dfb35..d9c96ea6f21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Federated topic read sessions now follow federation membership. After the initial discovery, databases that join the federation are read and databases that leave it are dropped; partition sessions of a dropped database get `TPartitionSessionClosedEvent` with the `ConnectionLost` reason. A listed database that becomes unavailable keeps its subsession. The refresh period is `TFederatedTopicClientSettings::FederationDiscoveryInterval` (30 seconds by default). + * Fixed Query session shutdown racing with session pool return. * Added `TConvertToBridgeSettings::SkipStoragePoolConversion()` to leave existing storage pools and their groups non-bridged when converting a cluster into a bridged one. diff --git a/include/ydb-cpp-sdk/client/federated_topic/federated_topic.h b/include/ydb-cpp-sdk/client/federated_topic/federated_topic.h index baf679a1b37..67499cbe8ee 100644 --- a/include/ydb-cpp-sdk/client/federated_topic/federated_topic.h +++ b/include/ydb-cpp-sdk/client/federated_topic/federated_topic.h @@ -407,6 +407,11 @@ struct TFederatedReadSessionSettings: public NTopic::TReadSessionSettings { //! Databases to read from. //! Default (empty) value means reading from all available databases. + //! The session tracks federation membership for its whole lifetime: a database that + //! appears and is readable is subscribed, a database that disappears from the federation + //! is unsubscribed and its partition sessions are closed with the ConnectionLost reason. + //! A listed database that becomes unavailable stays subscribed. + //! Refresh period is TFederatedTopicClientSettings::FederationDiscoveryInterval. //! Adding duplicates or unavailable databases is okay, they will be ignored. struct TReadOriginalSettings { //! Add reading from specified database if it's available. @@ -500,6 +505,12 @@ struct TFederatedTopicClientSettings : public TCommonClientSettingsBase #include +#include +#include +#include +#include + namespace NYdb::inline V3::NFederatedTopic { NTopic::TTopicClientSettings FromFederated(const TFederatedTopicClientSettings& settings); template -typename std::function WrapFederatedHandler(std::function outerHandler, std::shared_ptr db, std::shared_ptr federator) { +typename std::function WrapFederatedHandler(std::function outerHandler, std::shared_ptr db, std::shared_ptr federator, std::shared_ptr> retired) { if (outerHandler) { - return [outerHandler, db = std::move(db), federator = std::move(federator)](TEvent& ev) { - auto fev = federator->LocateFederate(ev, std::move(db)); - return outerHandler(fev); + return [outerHandler, db = std::move(db), federator = std::move(federator), retired = std::move(retired)](TEvent& ev) { + if (auto fev = federator->LocateFederate(ev, db, *retired)) { + outerHandler(*fev); + } }; } return {}; } -NTopic::TReadSessionSettings FromFederated(const TFederatedReadSessionSettings& settings, const std::shared_ptr& db, std::shared_ptr federator) { +NTopic::TReadSessionSettings FromFederated(const TFederatedReadSessionSettings& settings, const std::shared_ptr& db, std::shared_ptr federator, std::shared_ptr> retired) { NTopic::TReadSessionSettings SubsessionSettings = settings; SubsessionSettings.EventHandlers_.MaxMessagesBytes(settings.EventHandlers_.MaxMessagesBytes_); SubsessionSettings.EventHandlers_.HandlersExecutor(settings.EventHandlers_.HandlersExecutor_); @@ -34,16 +40,16 @@ NTopic::TReadSessionSettings FromFederated(const TFederatedReadSessionSettings& if (settings.FederatedEventHandlers_.SimpleDataHandlers_.DataHandler) { SubsessionSettings.EventHandlers_.SimpleDataHandlers( WrapFederatedHandler( - settings.FederatedEventHandlers_.SimpleDataHandlers_.DataHandler, db, federator), + settings.FederatedEventHandlers_.SimpleDataHandlers_.DataHandler, db, federator, retired), settings.FederatedEventHandlers_.SimpleDataHandlers_.CommitDataAfterProcessing, settings.FederatedEventHandlers_.SimpleDataHandlers_.GracefulStopAfterCommit); } -#define MAYBE_CONVERT_HANDLER(type, name) \ - if (settings.FederatedEventHandlers_.name##_) { \ - SubsessionSettings.EventHandlers_.name( \ - WrapFederatedHandler(settings.FederatedEventHandlers_.name##_, db, federator) \ - ); \ +#define MAYBE_CONVERT_HANDLER(type, name) \ + if (settings.FederatedEventHandlers_.name##_) { \ + SubsessionSettings.EventHandlers_.name( \ + WrapFederatedHandler(settings.FederatedEventHandlers_.name##_, db, federator, retired) \ + ); \ } MAYBE_CONVERT_HANDLER(TReadSessionEvent::TDataReceivedEvent, DataReceivedHandler); @@ -57,7 +63,15 @@ NTopic::TReadSessionSettings FromFederated(const TFederatedReadSessionSettings& #undef MAYBE_CONVERT_HANDLER - SubsessionSettings.EventHandlers_.SessionClosedHandler(settings.FederatedEventHandlers_.SessionClosedHandler_); + std::function sessionClosedHandler; + if (auto handler = settings.FederatedEventHandlers_.SessionClosedHandler_) { + sessionClosedHandler = [handler, retired](const NTopic::TSessionClosedEvent& ev) { + if (!*retired) { + handler(ev); + } + }; + } + SubsessionSettings.EventHandlers_.SessionClosedHandler(sessionClosedHandler); return SubsessionSettings; } @@ -77,6 +91,15 @@ TFederatedReadSessionImpl::TFederatedReadSessionImpl(const TFederatedReadSession , EventFederator(std::make_shared()) , Log(Connections->GetLog()) , SessionId(CreateGuidAsString()) + , FederationDiscoveryInterval(clientSettings.FederationDiscoveryInterval_ + ? clientSettings.FederationDiscoveryInterval_ + : TDuration::Seconds(30)) + , OriginalTopics(settings.Topics_) + , HandlersExecutor(Settings.EventHandlers_.HandlersExecutor_ + ? Settings.EventHandlers_.HandlersExecutor_ + : SubClientSettings.DefaultHandlersExecutor_ + ? SubClientSettings.DefaultHandlersExecutor_ + : NYdb::CreateThreadPoolExecutor(1)) { } @@ -88,17 +111,15 @@ void TFederatedReadSessionImpl::Start() { AsyncInit.Subscribe([selfCtx = SelfContext](const auto& f){ Y_UNUSED(f); if (auto self = selfCtx->LockShared()) { - std::lock_guard guard(self->Lock); - if (self->Closing) { - return; - } - self->FederationState = self->Observer->GetState(); - self->OnFederatedStateUpdateImpl(); + self->UpdateFederationState(); } }); } -void TFederatedReadSessionImpl::OpenSubSessionsImpl(const std::vector>& dbInfos) { +std::vector TFederatedReadSessionImpl::OpenSubSessions(const std::vector>& dbInfos) { + if (dbInfos.empty()) { + return {}; + } { TStringBuilder log(GetLogPrefix()); log << "Open read subsessions to databases: "; @@ -111,19 +132,90 @@ void TFederatedReadSessionImpl::OpenSubSessionsImpl(const std::vector subSessions; for (const auto& db : dbInfos) { NTopic::TTopicClientSettings settings = SubClientSettings; settings .Database(db->path()) .DiscoveryEndpoint(db->endpoint()); auto subclient = make_shared(Connections, settings); - auto subsession = subclient->CreateReadSession(FromFederated(Settings, db, EventFederator)); - SubSessions.emplace_back(subsession, db); + auto retired = std::make_shared>(false); + auto subsession = subclient->CreateReadSession(FromFederated(Settings, db, EventFederator, retired)); + subSessions.emplace_back(subsession, db, retired); } - SubsessionIndex = 0; + return subSessions; } -void TFederatedReadSessionImpl::OnFederatedStateUpdateImpl() { +void TFederatedReadSessionImpl::UpdateFederationState() { + std::vector toClose; + std::vector> toOpen; + bool failed = false; + with_lock (Lock) { + if (Closing) { + return; + } + // The observer state is shared by all sessions of the client: copy it before + // this session stores its own BAD_REQUEST there. + FederationState = std::make_shared(*Observer->GetState()); + OnFederatedStateUpdateImpl(toClose, toOpen); + failed = Closing; + if (failed) { + std::move(SubSessions.begin(), SubSessions.end(), std::back_inserter(toClose)); + SubSessions.clear(); + } + } + const bool changed = !toClose.empty() || !toOpen.empty(); + + // Subsessions are closed and opened outside Lock: both can run user callbacks. + // Databases that left are closed first, so two subsessions never read the same database. + CloseSubSessions(std::move(toClose), TDuration::Zero(), failed ? ESubSessionClose::Discard : ESubSessionClose::Leave); + auto opened = OpenSubSessions(toOpen); + + NThreading::TPromise wakeup; + with_lock (Lock) { + if (!Closing) { + std::move(opened.begin(), opened.end(), std::back_inserter(SubSessions)); + opened.clear(); + if (!ScheduleFederationStateUpdateImpl()) { + CloseImpl(); + failed = true; + std::move(SubSessions.begin(), SubSessions.end(), std::back_inserter(opened)); + SubSessions.clear(); + } + } + if (changed || Closing) { + wakeup = std::exchange(Wakeup, {}); + } + } + if (wakeup.Initialized()) { + wakeup.SetValue(); + } + // What is left in `opened` belongs to a session that is closing. + CloseSubSessions(std::move(opened), TDuration::Zero(), ESubSessionClose::Discard); + if (failed) { + NotifySessionClosed(); + } +} + +bool TFederatedReadSessionImpl::ScheduleFederationStateUpdateImpl() { + Y_ABORT_UNLESS(Lock.IsLocked()); + auto ctx = Connections->CreateContext(); + if (!ctx) { + return false; + } + auto cb = [selfCtx = SelfContext](bool ok) { + if (!ok) { + return; + } + if (auto self = selfCtx->LockShared()) { + self->UpdateFederationState(); + } + }; + Connections->ScheduleCallback(FederationDiscoveryInterval, std::move(cb), std::move(ctx)); + return true; +} + +void TFederatedReadSessionImpl::OnFederatedStateUpdateImpl(std::vector& toClose, std::vector>& toOpen) { if (!FederationState->Status.IsSuccess()) { LOG_LAZY(Log, TLOG_ERR, GetLogPrefix() << "Federated state update failed. FederationState: " << *FederationState); CloseImpl(); @@ -132,34 +224,47 @@ void TFederatedReadSessionImpl::OnFederatedStateUpdateImpl() { EventFederator->SetFederationState(FederationState); + bool topicsChanged = false; if (Settings.IsReadMirroredEnabled()) { Y_ABORT_UNLESS(Settings.GetDatabasesToReadFrom().size() == 1); auto dbToReadFrom = *Settings.GetDatabasesToReadFrom().begin(); - std::vector dbNames = GetAllFederationDatabaseNames(); - auto topics = Settings.Topics_; - for (const auto& topic : topics) { - for (const auto& dbName : dbNames) { - if (AsciiEqualsIgnoreCase(dbName, dbToReadFrom)) { + auto topics = OriginalTopics; + for (const auto& topic : OriginalTopics) { + for (const auto& db : FederationState->DbInfos) { + if (AsciiEqualsIgnoreCase(db->name(), dbToReadFrom) || !IsDatabaseReadable(db)) { continue; } auto mirroredTopic = topic; mirroredTopic.PartitionIds_.clear(); - mirroredTopic.Path(topic.Path_ + "-mirrored-from-" + dbName); - Settings.AppendTopics(mirroredTopic); + mirroredTopic.Path(topic.Path_ + "-mirrored-from-" + db->name()); + topics.push_back(mirroredTopic); } } + auto paths = [](const std::vector& list) { + std::set result; + for (const auto& topic : list) { + result.insert(topic.Path_); + } + return result; + }; + topicsChanged = paths(topics) != paths(Settings.Topics_); + Settings.Topics_ = std::move(topics); } + // A listed database keeps its subsession whatever its status: the subsession retries on its own, + // and dropping it on a short UNAVAILABLE would reread uncommitted data. std::vector> databases; + bool anyReadable = false; for (const auto& db : FederationState->DbInfos) { - if (IsDatabaseEligibleForRead(db)) { + if (IsDatabaseSelected(db)) { databases.push_back(db); + anyReadable = anyReadable || IsDatabaseReadable(db); } } - if (databases.empty()) { + if (!anyReadable && !SawReadableDatabase) { // TODO: investigate here, why empty list? // Reason (and returned status) could be BAD_REQUEST or UNAVAILABLE. LOG_LAZY(Log, TLOG_ERR, GetLogPrefix() << "No available databases to read."); @@ -187,24 +292,39 @@ void TFederatedReadSessionImpl::OnFederatedStateUpdateImpl() { CloseImpl(); return; } - - OpenSubSessionsImpl(databases); -} - -std::vector TFederatedReadSessionImpl::GetAllFederationDatabaseNames() { - std::vector result; - for (const auto& db : FederationState->DbInfos) { - result.push_back(db->name()); + SawReadableDatabase = true; + + auto same = [](const std::shared_ptr& lhs, const std::shared_ptr& rhs) { + return AsciiEqualsIgnoreCase(lhs->name(), rhs->name()) && lhs->path() == rhs->path() && lhs->endpoint() == rhs->endpoint(); + }; + + std::vector kept; + for (auto& sub : SubSessions) { + if (!topicsChanged && std::any_of(databases.begin(), databases.end(), [&](const auto& db) { return same(db, sub.DbInfo); })) { + kept.push_back(std::move(sub)); + } else { + LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Stop reading database '" << sub.DbInfo->name() << "'"); + toClose.push_back(std::move(sub)); + } + } + SubSessions = std::move(kept); + + // A discovery response may repeat a database, and nothing validates that. + for (const auto& db : databases) { + if (IsDatabaseReadable(db) && + std::none_of(SubSessions.begin(), SubSessions.end(), [&](const auto& sub) { return same(db, sub.DbInfo); }) && + std::none_of(toOpen.begin(), toOpen.end(), [&](const auto& other) { return same(db, other); })) { + toOpen.push_back(db); + } } - return result; } -bool TFederatedReadSessionImpl::IsDatabaseEligibleForRead(const std::shared_ptr& db) { - if (db->status() != TDbInfo::Status::DatabaseInfo_Status_AVAILABLE && - db->status() != TDbInfo::Status::DatabaseInfo_Status_READ_ONLY) { - return false; - } +bool TFederatedReadSessionImpl::IsDatabaseReadable(const std::shared_ptr& db) { + return db->status() == TDbInfo::Status::DatabaseInfo_Status_AVAILABLE || + db->status() == TDbInfo::Status::DatabaseInfo_Status_READ_ONLY; +} +bool TFederatedReadSessionImpl::IsDatabaseSelected(const std::shared_ptr& db) { if (Settings.GetDatabasesToReadFrom().empty()) { return true; } @@ -222,54 +342,161 @@ bool TFederatedReadSessionImpl::IsDatabaseEligibleForRead(const std::shared_ptr< return false; } +bool TFederatedReadSessionImpl::CloseSubSessions(std::vector subSessions, TDuration timeout, ESubSessionClose mode) { + // Retire before Close: callbacks it queues must not recreate the cached partition sessions. + std::vector lost; + if (mode != ESubSessionClose::Graceful) { + for (const auto& sub : subSessions) { + auto retired = EventFederator->RetireReadSource(*sub.DbInfo, *sub.Retired); + if (mode == ESubSessionClose::Leave) { + std::move(retired.begin(), retired.end(), std::back_inserter(lost)); + } + } + } + bool result = true; + for (const auto& sub : subSessions) { + // TODO substract from user timeout + result = sub.Session->Close(timeout) && result; + } + ReportLostPartitions(std::move(lost)); + return result; +} + +void TFederatedReadSessionImpl::ReportLostPartitions(std::vector lost) { + const auto& handlers = Settings.FederatedEventHandlers_; + // SimpleDataHandlers manage partition sessions on their own and drop these events. + if (lost.empty() || (handlers.SimpleDataHandlers_.DataHandler && !handlers.PartitionSessionClosedHandler_)) { + return; + } + + std::vector events; + for (auto& partition : lost) { + LOG_LAZY(Log, TLOG_INFO, GetLogPrefix() << "Partition session " << partition.FederatedPartitionSession->GetPartitionSessionId() + << " of database '" << partition.FederatedPartitionSession->GetReadSourceDatabaseName() << "' is lost: the database left the federation"); + events.emplace_back( + NTopic::TReadSessionEvent::TPartitionSessionClosedEvent( + std::move(partition.PartitionSession), + NTopic::TReadSessionEvent::TPartitionSessionClosedEvent::EReason::ConnectionLost), + std::move(partition.FederatedPartitionSession)); + } + + if (handlers.PartitionSessionClosedHandler_ || handlers.CommonHandler_) { + Post([specific = handlers.PartitionSessionClosedHandler_, common = handlers.CommonHandler_, events = std::move(events)]() mutable { + for (auto& event : events) { + if (specific) { + specific(event); + } else { + TReadSessionEvent::TEvent wrapped = std::move(event); + common(wrapped); + } + } + }); + return; + } + + NThreading::TPromise wakeup; + with_lock (Lock) { + if (Closing) { + return; + } + std::move(events.begin(), events.end(), std::back_inserter(PendingEvents)); + wakeup = std::exchange(Wakeup, {}); + } + if (wakeup.Initialized()) { + wakeup.SetValue(); + } +} + +void TFederatedReadSessionImpl::Post(std::function callback) const { + HandlersExecutor->Start(); + HandlersExecutor->Post(std::move(callback)); +} + +void TFederatedReadSessionImpl::NotifySessionClosed() const { + const auto& handlers = Settings.FederatedEventHandlers_; + if (!handlers.SessionClosedHandler_ && !handlers.CommonHandler_) { + return; + } + // Runs on the discovery thread: user callbacks belong on the handlers executor. + Post([specific = handlers.SessionClosedHandler_, common = handlers.CommonHandler_, event = MakeSessionClosedEvent()]() { + if (specific) { + specific(event); + } else { + TReadSessionEvent::TEvent wrapped = event; + common(wrapped); + } + }); +} + NThreading::TFuture TFederatedReadSessionImpl::WaitEvent() { // TODO override with read session settings timeout return AsyncInit.Apply([selfCtx = SelfContext](const NThreading::TFuture) { if (auto self = selfCtx->LockShared()) { - std::vector> waiters; std::lock_guard guard(self->Lock); - if (self->Closing) { + if (self->Closing || !self->PendingEvents.empty()) { return NThreading::MakeFuture(); } - Y_ABORT_UNLESS(!self->SubSessions.empty(), "SubSessions empty in discovered state"); + // Subscribing a new WaitAny to every call leaks: a caller that drops a timed-out + // wait does not unsubscribe it, and it stays until some input completes. + if (self->BlockedWait.Initialized() && !self->BlockedWait.IsReady()) { + return self->BlockedWait; + } + std::vector> waiters; + self->Wakeup = NThreading::NewPromise(); + waiters.push_back(self->Wakeup.GetFuture()); for (const auto& sub : self->SubSessions) { waiters.emplace_back(sub.Session->WaitEvent()); } - return NThreading::WaitAny(std::move(waiters)); + self->BlockedWait = NThreading::WaitAny(std::move(waiters)); + return self->BlockedWait; } return NThreading::MakeFuture(); }); } -std::vector TFederatedReadSessionImpl::GetEvents(bool block, std::optional maxEventsCount, size_t maxByteSize) { - if (block) { - WaitEvent().Wait(); - } - { - std::lock_guard guard(Lock); - if (Closing) { - return {NTopic::TSessionClosedEvent(FederationState->Status.GetStatus(), NYdb::NIssue::TIssues(FederationState->Status.GetIssues()))}; - } - // TODO!!! handle aborting or closing state - // via handler on SessionClosedEvent { - // cancel all subsessions, empty SubSessions, set aborting - // } - if (SubSessions.empty()) { - return {}; - } +NTopic::TSessionClosedEvent TFederatedReadSessionImpl::MakeSessionClosedEvent() const { + if (!FederationState) { + return NTopic::TSessionClosedEvent(EStatus::SUCCESS, {}); } + return NTopic::TSessionClosedEvent(FederationState->Status.GetStatus(), NYdb::NIssue::TIssues(FederationState->Status.GetIssues())); +} + +std::vector TFederatedReadSessionImpl::GetEvents(bool block, std::optional maxEventsCount, size_t maxByteSize) { std::vector result; - with_lock (Lock) { - do { - auto sub = SubSessions[SubsessionIndex]; - for (auto&& ev : sub.Session->GetEvents(false, maxEventsCount, maxByteSize)) { - result.push_back(EventFederator->LocateFederate(std::move(ev), sub.DbInfo)); + while (true) { + if (block) { + WaitEvent().Wait(); + } + with_lock (Lock) { + if (Closing) { + return {MakeSessionClosedEvent()}; + } + if (!PendingEvents.empty()) { + const size_t limit = maxEventsCount.value_or(std::numeric_limits::max()); + while (!PendingEvents.empty() && result.size() < limit) { + result.push_back(std::move(PendingEvents.front())); + PendingEvents.pop_front(); + } + return result; + } + // Blocking reads walk the subsessions once and, if none has events, wait again + // instead of spinning: a membership change wakes WaitEvent without a data event. + const size_t steps = block ? SubSessions.size() : std::min(1, SubSessions.size()); + for (size_t step = 0; step < steps && result.empty(); ++step) { + SubsessionIndex %= SubSessions.size(); + auto sub = SubSessions[SubsessionIndex]; + for (auto&& ev : sub.Session->GetEvents(false, maxEventsCount, maxByteSize)) { + if (auto federated = EventFederator->LocateFederate(std::move(ev), sub.DbInfo, *sub.Retired)) { + result.push_back(std::move(*federated)); + } + } + SubsessionIndex = (SubsessionIndex + 1) % SubSessions.size(); } - SubsessionIndex = (SubsessionIndex + 1) % SubSessions.size(); } - while (block && result.empty()); + if (!result.empty() || !block) { + return result; + } } - return result; } void TFederatedReadSessionImpl::CloseImpl() { @@ -277,15 +504,20 @@ void TFederatedReadSessionImpl::CloseImpl() { } bool TFederatedReadSessionImpl::Close(TDuration timeout) { - std::lock_guard guard(Lock); - Closing = true; - - bool result = true; - for (const auto& sub : SubSessions) { - // TODO substract from user timeout - result = sub.Session->Close(timeout); + std::vector subSessions; + NThreading::TPromise wakeup; + with_lock (Lock) { + Closing = true; + subSessions = std::move(SubSessions); + SubSessions.clear(); + wakeup = std::exchange(Wakeup, {}); } - return result; + // Outside Lock: a subscriber of the wakeup may call back into the session. + if (wakeup.Initialized()) { + wakeup.SetValue(); + } + // Subsessions are not retired: an explicit close still delivers their queued handlers and SessionClosedHandler. + return CloseSubSessions(std::move(subSessions), timeout, ESubSessionClose::Graceful); } } // namespace NYdb::NFederatedTopic diff --git a/src/client/federated_topic/impl/federated_read_session.h b/src/client/federated_topic/impl/federated_read_session.h index 661a16f1aa7..962154d4080 100644 --- a/src/client/federated_topic/impl/federated_read_session.h +++ b/src/client/federated_topic/impl/federated_read_session.h @@ -7,6 +7,9 @@ #include +#include +#include + namespace NYdb::inline V3::NFederatedTopic { class TEventFederator { @@ -39,14 +42,18 @@ class TEventFederator { return std::make_tuple(topicOriginDbInfo, topicOriginPath); } + // Returns nothing for an event of a retired subsession, so a callback queued before + // RetireReadSource cannot put its partition session back into the cache. template - auto LocateFederate(TEvent&& event, std::shared_ptr db) { + auto LocateFederate(TEvent&& event, std::shared_ptr db, const std::atomic& retired) { + using TResult = std::optional(), TFederatedPartitionSession::TPtr{}))>; + NTopic::TPartitionSession::TPtr psPtr; TFederatedPartitionSession::TPtr fps; using T = std::decay_t; if constexpr (std::is_same_v) { - return Federate(std::forward(event), std::move(fps)); + return retired ? TResult{} : TResult{Federate(std::forward(event), std::move(fps))}; } else if constexpr (std::is_same_v) { psPtr = std::visit([](auto&& arg) -> NTopic::TPartitionSession::TPtr { using T = std::decay_t; @@ -58,13 +65,16 @@ class TEventFederator { }, event); if (!psPtr) { // TSessionClosedEvent - return Federate(std::forward(event), std::move(fps)); + return retired ? TResult{} : TResult{Federate(std::forward(event), std::move(fps))}; } } else { psPtr = event.GetPartitionSession(); } with_lock(Lock) { + if (retired) { + return TResult{}; + } if (!FederatedPartitionSessions.contains(psPtr.Get())) { auto [topicOriginDbInfo, topicOriginPath] = LocateTopicOrigin(event); FederatedPartitionSessions[psPtr.Get()] = MakeIntrusive(psPtr, std::move(db), std::move(topicOriginDbInfo), std::move(topicOriginPath)); @@ -76,7 +86,7 @@ class TEventFederator { } } - return Federate(std::forward(event), std::move(fps)); + return TResult{Federate(std::forward(event), std::move(fps))}; } template @@ -112,6 +122,33 @@ class TEventFederator { } } + struct TRetiredPartitionSession { + NTopic::TPartitionSession::TPtr PartitionSession; + TFederatedPartitionSession::TPtr FederatedPartitionSession; + }; + + // Marks a subsession retired and drops the cached partition sessions it read from `db`. + // Returns the dropped sessions: the user has seen them and never got their close event. + std::vector RetireReadSource(const TDbInfo& db, std::atomic& retired) { + std::vector result; + with_lock(Lock) { + retired = true; + for (auto it = FederatedPartitionSessions.begin(); it != FederatedPartitionSessions.end();) { + const auto& fps = it->second; + if (fps->GetReadSourceDatabaseId() == db.id() && + fps->GetReadSourceDatabasePath() == db.path() && + AsciiEqualsIgnoreCase(fps->GetReadSourceDatabaseName(), db.name())) { + // The cached federated session owns the partition session, so the raw key is alive. + result.push_back({NTopic::TPartitionSession::TPtr(it->first), fps}); + it = FederatedPartitionSessions.erase(it); + } else { + ++it; + } + } + } + return result; + } + private: TAdaptiveLock Lock; std::unordered_map FederatedPartitionSessions; @@ -124,13 +161,17 @@ class TFederatedReadSessionImpl : public NTopic::TEnableSelfContext session = {}, std::shared_ptr dbInfo = {}) + TSubSession(std::shared_ptr session = {}, std::shared_ptr dbInfo = {}, std::shared_ptr> retired = {}) : Session(std::move(session)) , DbInfo(std::move(dbInfo)) + , Retired(std::move(retired)) {} std::shared_ptr Session; std::shared_ptr DbInfo; + // Set before the federated session closes the subsession on its own: the subsession's + // events and callbacks are dropped, its close is not the end of the federated session. + std::shared_ptr> Retired; }; public: @@ -160,13 +201,35 @@ class TFederatedReadSessionImpl : public NTopic::TEnableSelfContext>& dbInfos); - - std::vector GetAllFederationDatabaseNames(); - - bool IsDatabaseEligibleForRead(const std::shared_ptr& db); + std::vector OpenSubSessions(const std::vector>& dbInfos); + + // Matches the database against DatabasesToReadFrom, ignoring its status. + bool IsDatabaseSelected(const std::shared_ptr& db); + static bool IsDatabaseReadable(const std::shared_ptr& db); + + // Re-reads federation membership, applies it and schedules the next update. + void UpdateFederationState(); + // Requires Lock. Moves subsessions of databases that left to `toClose` and lists databases to open. + // Sets Closing if the state is not usable. + void OnFederatedStateUpdateImpl(std::vector& toClose, std::vector>& toOpen); + // Requires Lock. + bool ScheduleFederationStateUpdateImpl(); + + enum class ESubSessionClose { + // The database left the federation: drop its events and report its partitions as lost. + Leave, + // The whole session is going away on its own: drop its events. + Discard, + // Explicit Close(): handlers already queued on the executor still get their events. + Graceful, + }; + // Outside Lock. + bool CloseSubSessions(std::vector subSessions, TDuration timeout, ESubSessionClose mode); + void ReportLostPartitions(std::vector lost); + void Post(std::function callback) const; + void NotifySessionClosed() const; - void OnFederatedStateUpdateImpl(); + NTopic::TSessionClosedEvent MakeSessionClosedEvent() const; void CloseImpl(); @@ -187,11 +250,31 @@ class TFederatedReadSessionImpl : public NTopic::TEnableSelfContext OriginalTopics; + // The executor subsessions run handlers on. Session-level callbacks use it too, + // so they are ordered with data handlers instead of running beside them. + const IExecutor::TPtr HandlersExecutor; TAdaptiveLock Lock; std::vector SubSessions; size_t SubsessionIndex = 0; + // True after at least one readable database was observed. A later empty + // membership drops subsessions but keeps the session, so a database that + // comes back is read again. An empty first snapshot still fails the session. + bool SawReadableDatabase = false; + + // Signals BlockedWait on a membership change or session close. SetValue happens + // outside Lock: a subscriber may take Lock. + NThreading::TPromise Wakeup; + // One WaitAny for every blocked reader, subscribed to Wakeup and the current + // subsessions. Rebuilt only after it becomes ready: a timed-out wait drops the + // caller's copy and does not subscribe again. + NThreading::TFuture BlockedWait; + // Events produced by the federated session itself, returned by GetEvents + // before subsession events. Only used when no handler takes them. + std::deque PendingEvents; // Exiting. bool Closing = false; diff --git a/src/client/federated_topic/impl/federation_observer.cpp b/src/client/federated_topic/impl/federation_observer.cpp index 9b62d9bc613..25440ab6f0c 100644 --- a/src/client/federated_topic/impl/federation_observer.cpp +++ b/src/client/federated_topic/impl/federation_observer.cpp @@ -5,13 +5,14 @@ namespace NYdb::inline V3::NFederatedTopic { -constexpr TDuration REDISCOVERY_DELAY = TDuration::Seconds(30); - TFederatedDbObserverImpl::TFederatedDbObserverImpl(std::shared_ptr connections, const TFederatedTopicClientSettings& settings) : TClientImplCommon(std::move(connections), settings) , FederatedDbState(std::make_shared()) , PromiseToInitState(NThreading::NewPromise()) , FederationDiscoveryRetryPolicy(settings.RetryPolicy_) + , DiscoveryInterval(settings.FederationDiscoveryInterval_ + ? settings.FederationDiscoveryInterval_ + : TDuration::Seconds(30)) { RpcSettings.EndpointPolicy = TRpcRequestSettings::TEndpointPolicy::UseDiscoveryEndpoint; RpcSettings.UseAuth = true; @@ -159,20 +160,37 @@ void TFederatedDbObserverImpl::OnFederationDiscovery(TStatus&& status, Ydb::Fede // 1) The request was meant for a non-federated topic: fall back to single db mode. // 2) The database path in the request is simply wrong: the client should get the BAD_REQUEST status. if (status.GetStatus() == EStatus::CLIENT_CALL_UNIMPLEMENTED || status.GetStatus() == EStatus::BAD_REQUEST) { + if (Federated) { + // Read sessions follow the published membership. Replacing a known federation with + // the single-database fallback would drop every other database, so keep the last + // state and ask again later. + LOG_LAZY(DbDriverState_->Log, TLOG_WARNING, TStringBuilder() + << "OnFederationDiscovery: Got " << status.GetStatus() + << " after a successful discovery, keeping the last federation state. Description: " + << status.GetIssues().ToOneLineString()); + ScheduleFederationDiscoveryImpl(DiscoveryInterval); + return; + } LOG_LAZY(DbDriverState_->Log, TLOG_INFO, TStringBuilder() << "OnFederationDiscovery fall back to single mode, database=" << DbDriverState_->Database); - FederatedDbState->Status = TPlainStatus{}; // SUCCESS - FederatedDbState->ControlPlaneEndpoint = DbDriverState_->DiscoveryEndpoint; - // FederatedDbState->SelfLocation = ???; + // Sessions copy the published state without Lock, so publish a new object instead of mutating it. + auto newInfo = std::make_shared(); + newInfo->Status = TPlainStatus{}; // SUCCESS + newInfo->ControlPlaneEndpoint = DbDriverState_->DiscoveryEndpoint; + // newInfo->SelfLocation = ???; auto db = std::make_shared(); db->set_path(TStringType{DbDriverState_->Database}); db->set_endpoint(TStringType{DbDriverState_->DiscoveryEndpoint}); db->set_status(Ydb::FederationDiscovery::DatabaseInfo_Status_AVAILABLE); db->set_weight(100); - FederatedDbState->DbInfos.emplace_back(std::move(db)); + newInfo->DbInfos.emplace_back(std::move(db)); + std::swap(FederatedDbState, newInfo); } else { if (status.IsSuccess()) { - ScheduleFederationDiscoveryImpl(REDISCOVERY_DELAY); + Federated = true; + // Each outage gets the full retry budget, not what earlier outages left of it. + FederationDiscoveryRetryState = nullptr; + ScheduleFederationDiscoveryImpl(DiscoveryInterval); } else { LOG_LAZY(DbDriverState_->Log, TLOG_ERR, TStringBuilder() << "OnFederationDiscovery: Got error. Status: " << status.GetStatus() diff --git a/src/client/federated_topic/impl/federation_observer.h b/src/client/federated_topic/impl/federation_observer.h index 3eddb068768..1481bf43657 100644 --- a/src/client/federated_topic/impl/federation_observer.h +++ b/src/client/federated_topic/impl/federation_observer.h @@ -90,6 +90,9 @@ class TFederatedDbObserverImpl : public TClientImplCommon +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace NYdb::NFederatedTopic::NTests { + +namespace { + +using TDbStatus = Ydb::FederationDiscovery::DatabaseInfo::Status; + +struct TDbSpec { + std::string Name; + TDbStatus Status = TDbStatus::DatabaseInfo_Status_AVAILABLE; + std::string Path = "/Root"; +}; + +// Runs posted callbacks only when the test asks. Used to prove that a terminal +// close notification is queued to the handlers executor instead of running on +// the discovery thread. +class TManualExecutor final : public IExecutor { +public: + void Post(TFunction&& function) override { + with_lock (Lock) { + Pending.push_back(std::move(function)); + } + } + + bool IsAsync() const override { + return true; + } + + void Stop() override { + } + + size_t Size() const { + with_lock (Lock) { + return Pending.size(); + } + } + + void Drain() { + std::vector batch; + with_lock (Lock) { + batch.swap(Pending); + } + for (auto& function : batch) { + function(); + } + } + +protected: + void DoStart() override { + } + +private: + mutable TAdaptiveLock Lock; + std::vector Pending; +}; + +struct TAssignment { + std::string Database; + std::string Topic; +}; + +class TMembershipFixture { +public: + TMembershipFixture(const char* name, size_t partitions, + NTopic::IRetryPolicy::TPtr discoveryRetryPolicy = NTopic::IRetryPolicy::GetNoRetryPolicy()) + : Partitions(partitions) + , Setup(std::make_shared( + TString(name), false, ::NPersQueue::TTestServer::LOGGED_SERVICES, + NActors::NLog::PRI_ERROR, 1, partitions)) + { + Setup->Start(true, true); + Fds.Port = Setup->GetGrpcPort(); + const ui16 servicePort = Setup->GetPortManager()->GetPort(4285); + Server = Setup->StartGrpcService(servicePort, &Fds); + Databases.push_back(TDbSpec{"dc1"}); + + Responder = std::thread([this] { + while (!Stop.load()) { + auto request = Fds.GetNextPendingRequest(); + if (!request) { + Sleep(TDuration::MilliSeconds(10)); + continue; + } + request->Result.SetValue(Compose()); + } + const auto deadline = TInstant::Now() + TDuration::Seconds(1); + do { + if (auto request = Fds.GetNextPendingRequest()) { + request->Result.SetValue(Compose()); + } else { + Sleep(TDuration::MilliSeconds(10)); + } + } while (TInstant::Now() < deadline); + }); + + TDriverConfig config; + config.SetEndpoint(TStringBuilder() << "localhost:" << servicePort); + config.SetDatabase("/Root"); + config.SetLog(std::unique_ptr(CreateLogBackend("cerr", ELogPriority::TLOG_ERR).Release())); + Driver = std::make_unique(config); + + TFederatedTopicClientSettings clientSettings; + clientSettings.FederationDiscoveryInterval(TDuration::MilliSeconds(200)); + clientSettings.RetryPolicy(std::move(discoveryRetryPolicy)); + Client = std::make_unique(*Driver, clientSettings); + } + + ~TMembershipFixture() { + Client.reset(); + Driver.reset(); + Stop.store(true); + if (Responder.joinable()) { + Responder.join(); + } + } + + void SetDatabases(std::vector databases) { + with_lock (Lock) { + Databases = std::move(databases); + } + } + + void FailNextDiscoveries() { + FailDiscoveries(-1); + } + + // count < 0 fails every following discovery, count == 0 stops failing. + void FailDiscoveries(int count, Ydb::StatusIds::StatusCode status = Ydb::StatusIds::UNAVAILABLE) { + FailureStatus.store(status); + FailuresLeft.store(count); + } + + int PendingFailures() const { + return FailuresLeft.load(); + } + + // Pumps the session until a data event carrying `text` arrives and hands it to the caller. + std::optional WaitForData(IFederatedReadSession& session, const std::string& text) { + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline) { + if (!session.WaitEvent().Wait(TDuration::MilliSeconds(200))) { + continue; + } + while (auto event = session.GetEvent(false)) { + if (auto* data = std::get_if(&*event)) { + for (const auto& message : data->GetMessages()) { + if (message.GetData() == text) { + return std::move(*data); + } + } + continue; + } + if (Handle(*event)) { + return std::nullopt; + } + } + } + return std::nullopt; + } + + int FailedResponses() const { + return Failed.load(); + } + + int SucceededResponses() const { + return Succeeded.load(); + } + + std::shared_ptr StartReader() { + return StartReader({}); + } + + std::shared_ptr StartReader(TFederatedReadSessionSettings settings) { + if (settings.Topics_.empty()) { + settings.AppendTopics(Setup->GetTestTopic()); + } + if (settings.ConsumerName_.empty()) { + settings.ConsumerName("shared/user"); + } + settings.MaxMemoryUsageBytes(1_MB); + return Client->CreateReadSession(settings); + } + + bool Pump(IFederatedReadSession& session) { + // A closed session returns its close event on every call. + while (auto event = session.GetEvent(false)) { + if (Handle(*event)) { + return true; + } + } + return false; + } + + // True once the session has gone quiet: a fresh WaitEvent stays blocked for a whole + // discovery interval. Used before asserting that something stopped being read, where a + // fixed sleep would race the refresh. + bool WaitUntilIdle(IFederatedReadSession& session) { + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline) { + if (!session.WaitEvent().Wait(TDuration::Seconds(1))) { + return true; + } + Pump(session); + } + return false; + } + + bool WaitUntil(IFederatedReadSession& session, const std::function& predicate) { + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline) { + if (predicate()) { + return true; + } + if (!session.WaitEvent().Wait(TDuration::MilliSeconds(200))) { + continue; + } + Pump(session); + } + return predicate(); + } + + bool AllOn(const std::string& database) const { + if (Assignments.size() != Partitions) { + return false; + } + for (const auto& [partition, assignment] : Assignments) { + Y_UNUSED(partition); + if (assignment.Database != database) { + return false; + } + } + return true; + } + + bool HasDatabase(const std::string& database) const { + for (const auto& [partition, assignment] : Assignments) { + Y_UNUSED(partition); + if (assignment.Database == database) { + return true; + } + } + return false; + } + + bool HasTopicSuffix(const std::string& suffix) const { + for (const auto& [partition, assignment] : Assignments) { + Y_UNUSED(partition); + if (assignment.Topic.find(suffix) != std::string::npos) { + return true; + } + } + return false; + } + + void Write(const TString& text) { + NPersQueue::TWriteSessionSettings writeSettings; + writeSettings.Path(Setup->GetTestTopic()).MessageGroupId("src_id"); + writeSettings.Codec(NPersQueue::ECodec::RAW); + auto writer = Setup->GetPersQueueClient().CreateSimpleBlockingWriteSession(writeSettings); + UNIT_ASSERT(writer->Write(text)); + writer->Close(); + } + + bool Saw(const std::string& text) const { + return std::find(Messages.begin(), Messages.end(), text) != Messages.end(); + } + + // A retired subsession is not polled anymore, so its stop events do not clear Assignments. + // Silence on the topic is the observable sign that reading has stopped. + bool StaysSilent(IFederatedReadSession& session, const std::string& text, TDuration duration) { + const auto deadline = TInstant::Now() + duration; + while (TInstant::Now() < deadline) { + Pump(session); + if (Saw(text)) { + return false; + } + Sleep(TDuration::MilliSeconds(100)); + } + return !Saw(text); + } + + const size_t Partitions; + std::unordered_map OriginStarts; + // Partition sessions closed with ConnectionLost, by read source database. + std::unordered_map Lost; + std::shared_ptr Setup; + std::unordered_map Assignments; + std::vector Messages; + int SessionClosedEvents = 0; + EStatus LastSessionStatus = EStatus::STATUS_UNDEFINED; + +private: + bool Handle(TReadSessionEvent::TEvent& event) { + if (auto* start = std::get_if(&event)) { + const auto session = start->GetFederatedPartitionSession(); + Assignments[session->GetPartitionId()] = TAssignment{ + session->GetReadSourceDatabaseName(), + session->GetTopicPath(), + }; + ++OriginStarts[session->GetTopicOriginDatabaseName()]; + start->Confirm(); + return false; + } + if (auto* data = std::get_if(&event)) { + for (const auto& message : data->GetMessages()) { + Messages.emplace_back(message.GetData()); + } + return false; + } + if (auto* stop = std::get_if(&event)) { + stop->Confirm(); + return false; + } + if (auto* end = std::get_if(&event)) { + end->Confirm(); + return false; + } + if (auto* closed = std::get_if(&event)) { + const auto session = closed->GetFederatedPartitionSession(); + const std::string database = session->GetReadSourceDatabaseName(); + if (closed->GetReason() == NTopic::TReadSessionEvent::TPartitionSessionClosedEvent::EReason::ConnectionLost) { + ++Lost[database]; + } + // Another database may already have taken the partition over. + auto it = Assignments.find(session->GetPartitionId()); + if (it != Assignments.end() && it->second.Database == database) { + Assignments.erase(it); + } + return false; + } + if (auto* sessionClosed = std::get_if(&event)) { + ++SessionClosedEvents; + LastSessionStatus = sessionClosed->GetStatus(); + return true; + } + return false; + } + + TFederationDiscoveryServiceMock::TGrpcResult Compose() { + int left = FailuresLeft.load(); + while (left != 0 && !FailuresLeft.compare_exchange_weak(left, left > 0 ? left - 1 : left)) { + } + if (left != 0) { + ++Failed; + Ydb::FederationDiscovery::ListFederationDatabasesResponse response; + auto* op = response.mutable_operation(); + op->set_status(FailureStatus.load()); + op->set_ready(true); + op->set_id("federation-membership"); + return {std::move(response), grpc::Status::OK}; + } + ++Succeeded; + + std::vector databases; + with_lock (Lock) { + databases = Databases; + } + + Ydb::FederationDiscovery::ListFederationDatabasesResponse response; + auto* op = response.mutable_operation(); + op->set_status(Ydb::StatusIds::SUCCESS); + op->set_ready(true); + op->set_id("federation-membership"); + + Ydb::FederationDiscovery::ListFederationDatabasesResult result; + result.set_control_plane_endpoint("cp.logbroker-federation:2135"); + result.set_self_location("fancy_datacenter"); + for (const auto& database : databases) { + auto* info = result.add_federation_databases(); + info->set_name(database.Name); + info->set_path(database.Path); + info->set_id(std::string("account-") + database.Name); + info->set_endpoint("localhost:" + ToString(Fds.Port)); + info->set_location(database.Name); + info->set_status(database.Status); + info->set_weight(1000); + } + op->mutable_result()->PackFrom(result); + return {std::move(response), grpc::Status::OK}; + } + + TFederationDiscoveryServiceMock Fds; + std::unique_ptr Server; + std::atomic Stop{false}; + std::atomic FailuresLeft{0}; + std::atomic FailureStatus{Ydb::StatusIds::UNAVAILABLE}; + std::atomic Failed{0}; + std::atomic Succeeded{0}; + TAdaptiveLock Lock; + std::vector Databases; + std::thread Responder; + std::unique_ptr Driver; + std::unique_ptr Client; +}; + +} // namespace + +Y_UNIT_TEST_SUITE(ReadSessionMembership) { + + Y_UNIT_TEST(AddsDatabaseWithoutDroppingTheCurrentOne) { + TMembershipFixture fixture(__FUNCTION__, 2); + auto session = fixture.StartReader(); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "dc1 should receive every partition before another database appears"); + + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { + return fixture.Assignments.size() == fixture.Partitions && fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"); + }), "both databases should be reading after dc2 joins"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(RemovesOneDatabaseAndKeepsTheOther) { + TMembershipFixture fixture(__FUNCTION__, 2); + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + auto session = fixture.StartReader(); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { + return fixture.Assignments.size() == fixture.Partitions && fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"); + }), "both databases should receive partitions"); + + fixture.SetDatabases({TDbSpec{"dc1"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "dc2 should stop being read while dc1 keeps the partitions"); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.Lost["dc2"] >= 1; }), + "partition sessions of the database that left should be closed as lost"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(LeavingDatabaseReportsLostPartitionsToHandler) { + TMembershipFixture fixture(__FUNCTION__, 2); + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + + TAdaptiveLock lock; + std::unordered_map lostByHandler; + TFederatedReadSessionSettings settings; + settings.FederatedEventHandlers_.PartitionSessionClosedHandler([&](TReadSessionEvent::TPartitionSessionClosedEvent& event) { + if (event.GetReason() == NTopic::TReadSessionEvent::TPartitionSessionClosedEvent::EReason::ConnectionLost) { + with_lock (lock) { + ++lostByHandler[event.GetFederatedPartitionSession()->GetReadSourceDatabaseName()]; + } + } + }); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { + return fixture.Assignments.size() == fixture.Partitions && fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"); + })); + + fixture.SetDatabases({TDbSpec{"dc1"}}); + auto lostOn = [&](const std::string& database) { + with_lock (lock) { + return lostByHandler[database]; + } + }; + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return lostOn("dc2") >= 1; }), + "PartitionSessionClosedHandler should get the partitions of the database that left"); + UNIT_ASSERT_VALUES_EQUAL(lostOn("dc1"), 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.Lost["dc2"], 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(EmptyMembershipPausesThenResumes) { + TMembershipFixture fixture(__FUNCTION__, 2); + auto session = fixture.StartReader(); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "dc1 should receive every partition"); + + fixture.Write("while-up"); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.Saw("while-up"); }), + "a message written while dc1 is readable should be delivered"); + + fixture.SetDatabases({}); + UNIT_ASSERT_C(fixture.WaitUntilIdle(*session), + "the session should go idle once the federation is empty"); + fixture.Write("while-down"); + UNIT_ASSERT_C(fixture.StaysSilent(*session, "while-down", TDuration::Seconds(3)), + "an empty federation should stop reading without closing the session"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + + auto first = session->WaitEvent(); + auto second = session->WaitEvent(); + UNIT_ASSERT_C(!first.Wait(TDuration::MilliSeconds(400)), "WaitEvent should block while no database is readable"); + + fixture.SetDatabases({TDbSpec{"dc1"}}); + UNIT_ASSERT_C(first.Wait(TDuration::Seconds(20)) && second.Wait(TDuration::Seconds(20)), + "both blocked WaitEvent calls should wake up when a database returns"); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "reading should resume from dc1"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(UnavailableDatabaseKeepsItsSubsession) { + TMembershipFixture fixture(__FUNCTION__, 2); + auto session = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + const int startsBefore = fixture.OriginStarts["dc1"]; + + fixture.SetDatabases({TDbSpec{"dc1", TDbStatus::DatabaseInfo_Status_UNAVAILABLE}}); + const int responses = fixture.SucceededResponses(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.SucceededResponses() >= responses + 3; })); + fixture.Write("while-unavailable"); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.Saw("while-unavailable"); }), + "a listed database that turns UNAVAILABLE should keep its subsession"); + UNIT_ASSERT_C(fixture.AllOn("dc1"), "partitions should stay where they were"); + UNIT_ASSERT_VALUES_EQUAL(fixture.OriginStarts["dc1"], startsBefore); + UNIT_ASSERT_VALUES_EQUAL(fixture.Lost["dc1"], 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(UnavailableDatabaseIsOpenedOnceAvailable) { + TMembershipFixture fixture(__FUNCTION__, 2); + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2", TDbStatus::DatabaseInfo_Status_UNAVAILABLE}}); + auto session = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + const int responses = fixture.SucceededResponses(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.SucceededResponses() >= responses + 3; })); + UNIT_ASSERT_C(fixture.AllOn("dc1") && !fixture.HasDatabase("dc2"), + "a database that is UNAVAILABLE from the start should not be opened"); + + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { + return fixture.Assignments.size() == fixture.Partitions && fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"); + }), "dc2 should be opened once it is AVAILABLE"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(ReadOnlyDatabaseStaysReadable) { + TMembershipFixture fixture(__FUNCTION__, 2); + auto session = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + fixture.SetDatabases({TDbSpec{"dc1", TDbStatus::DatabaseInfo_Status_READ_ONLY}}); + const int responses = fixture.SucceededResponses(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.SucceededResponses() >= responses + 3; })); + UNIT_ASSERT_C(fixture.AllOn("dc1"), "READ_ONLY is still a readable federation database"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(ReadOriginalIgnoresDatabasesOutsideTheRequestedList) { + TMembershipFixture fixture(__FUNCTION__, 2); + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + + TFederatedReadSessionSettings settings; + TFederatedReadSessionSettings::TReadOriginalSettings original; + original.AddDatabase("dc1"); + settings.ReadOriginal(std::move(original)); + auto session = fixture.StartReader(std::move(settings)); + + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "ReadOriginal(dc1) should not read dc2"); + UNIT_ASSERT(!fixture.HasDatabase("dc2")); + + fixture.Write("from-dc1"); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.Saw("from-dc1"); })); + + fixture.SetDatabases({TDbSpec{"dc2"}}); + UNIT_ASSERT_C(fixture.WaitUntilIdle(*session), + "the session should go idle once the only requested database leaves"); + fixture.Write("while-dc1-is-gone"); + UNIT_ASSERT_C(fixture.StaysSilent(*session, "while-dc1-is-gone", TDuration::Seconds(3)), + "dc2 is not requested, so removing dc1 should leave nothing to read"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.Saw("while-dc1-is-gone") && fixture.AllOn("dc1"); }), + "dc1 should be picked up again and dc2 should still be ignored"); + } + + Y_UNIT_TEST(FirstSnapshotWithoutRequestedDatabaseClosesSession) { + TMembershipFixture fixture(__FUNCTION__, 1); + auto executor = std::make_shared(); + EStatus handlerStatus = EStatus::STATUS_UNDEFINED; + + TFederatedReadSessionSettings settings; + TFederatedReadSessionSettings::TReadOriginalSettings original; + original.AddDatabase("missing"); + settings.ReadOriginal(std::move(original)); + settings.EventHandlers_.HandlersExecutor(executor); + settings.FederatedEventHandlers_.SessionClosedHandler([&](const TSessionClosedEvent& event) { + handlerStatus = event.GetStatus(); + }); + auto session = fixture.StartReader(std::move(settings)); + + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline && executor->Size() == 0) { + Sleep(TDuration::MilliSeconds(50)); + } + UNIT_ASSERT_C(executor->Size() >= 1, "a first snapshot with no eligible database should close the session"); + executor->Drain(); + UNIT_ASSERT_VALUES_EQUAL(handlerStatus, EStatus::BAD_REQUEST); + session->Close(TDuration::Zero()); + } + + Y_UNIT_TEST(BadRequestOfOneReaderDoesNotStopAnother) { + TMembershipFixture fixture(__FUNCTION__, 2); + auto healthy = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*healthy, [&] { return fixture.AllOn("dc1"); })); + + TFederatedReadSessionSettings settings; + TFederatedReadSessionSettings::TReadOriginalSettings original; + original.AddDatabase("missing"); + settings.ReadOriginal(std::move(original)); + auto failing = fixture.StartReader(std::move(settings)); + + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + std::optional closed; + while (TInstant::Now() < deadline && !closed) { + if (!failing->WaitEvent().Wait(TDuration::MilliSeconds(200))) { + continue; + } + while (auto event = failing->GetEvent(false)) { + if (std::holds_alternative(*event)) { + closed = std::move(event); + break; + } + } + } + UNIT_ASSERT(closed); + UNIT_ASSERT_VALUES_EQUAL(std::get(*closed).GetStatus(), EStatus::BAD_REQUEST); + + const int responses = fixture.SucceededResponses(); + UNIT_ASSERT(fixture.WaitUntil(*healthy, [&] { return fixture.SucceededResponses() >= responses + 3; })); + UNIT_ASSERT_C(fixture.AllOn("dc1"), "the healthy reader must keep the observer snapshot without BAD_REQUEST"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(LeavingDatabaseDoesNotNotifySessionClosed) { + TMembershipFixture fixture(__FUNCTION__, 2); + std::atomic handlerCalls{0}; + TFederatedReadSessionSettings settings; + settings.FederatedEventHandlers_.SessionClosedHandler([&](const TSessionClosedEvent&) { + ++handlerCalls; + }); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + fixture.SetDatabases({TDbSpec{"dc2"}}); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc2"); })); + UNIT_ASSERT_VALUES_EQUAL(handlerCalls.load(), 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(ExplicitCloseStillNotifiesHandler) { + TMembershipFixture fixture(__FUNCTION__, 1); + std::atomic handlerCalls{0}; + TFederatedReadSessionSettings settings; + settings.FederatedEventHandlers_.SessionClosedHandler([&](const TSessionClosedEvent&) { + ++handlerCalls; + }); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + UNIT_ASSERT(session->Close(TDuration::Seconds(5))); + const auto deadline = TInstant::Now() + TDuration::Seconds(5); + while (TInstant::Now() < deadline && handlerCalls.load() == 0) { + Sleep(TDuration::MilliSeconds(50)); + } + UNIT_ASSERT_C(handlerCalls.load() >= 1, "explicit Close should still deliver SessionClosedHandler"); + } + + Y_UNIT_TEST(ExplicitCloseKeepsQueuedHandlers) { + TMembershipFixture fixture(__FUNCTION__, 1); + auto executor = std::make_shared(); + std::atomic acks{0}; + TFederatedReadSessionSettings settings; + settings.EventHandlers_.HandlersExecutor(executor); + settings.FederatedEventHandlers_.CommitOffsetAcknowledgementHandler([&](TReadSessionEvent::TCommitOffsetAcknowledgementEvent&) { + ++acks; + }); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + fixture.Write("to-commit"); + auto data = fixture.WaitForData(*session, "to-commit"); + UNIT_ASSERT(data); + data->Commit(); + const auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline && executor->Size() == 0) { + Sleep(TDuration::MilliSeconds(50)); + } + UNIT_ASSERT_C(executor->Size() >= 1, "the commit acknowledgement handler should be queued"); + + session->Close(TDuration::Zero()); + executor->Drain(); + UNIT_ASSERT_VALUES_EQUAL_C(acks.load(), 1, "a handler queued before Close should still get its event"); + } + + Y_UNIT_TEST(BadRequestAfterSuccessKeepsMembership) { + TMembershipFixture fixture(__FUNCTION__, 2); + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + auto session = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { + return fixture.Assignments.size() == fixture.Partitions && fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"); + })); + + const int failed = fixture.FailedResponses(); + fixture.FailDiscoveries(-1, Ydb::StatusIds::BAD_REQUEST); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.FailedResponses() >= failed + 3; }), + "discovery should keep polling after BAD_REQUEST"); + UNIT_ASSERT_C(fixture.HasDatabase("dc1") && fixture.HasDatabase("dc2"), + "BAD_REQUEST after a federated answer must not replace the membership"); + UNIT_ASSERT_VALUES_EQUAL(fixture.Lost["dc1"], 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.Lost["dc2"], 0); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + + fixture.SetDatabases({TDbSpec{"dc1"}}); + fixture.FailDiscoveries(0); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1") && fixture.Lost["dc2"] >= 1; }), + "membership updates should resume once discovery answers again"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(DiscoveryRetryBudgetIsRestoredAfterSuccess) { + // One retry per outage. Without a reset the second outage would find the budget spent. + TMembershipFixture fixture(__FUNCTION__, 1, + NTopic::IRetryPolicy::GetFixedIntervalPolicy(TDuration::MilliSeconds(50), TDuration::MilliSeconds(50), 1)); + auto session = fixture.StartReader(); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + for (int outage = 0; outage < 3; ++outage) { + fixture.FailDiscoveries(1); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.PendingFailures() == 0; })); + const int succeeded = fixture.SucceededResponses(); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.SucceededResponses() > succeeded; }), + "discovery should recover after outage " << outage); + UNIT_ASSERT_VALUES_EQUAL_C(fixture.SessionClosedEvents, 0, "outage " << outage); + } + + fixture.Write("after-outages"); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.Saw("after-outages"); })); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + + Y_UNIT_TEST(DiscoveryFailureNotifiesThroughHandlersExecutor) { + TMembershipFixture fixture(__FUNCTION__, 1); + auto executor = std::make_shared(); + std::atomic handlerCalls{0}; + EStatus handlerStatus = EStatus::STATUS_UNDEFINED; + + TFederatedReadSessionSettings settings; + settings.EventHandlers_.HandlersExecutor(executor); + settings.FederatedEventHandlers_.SessionClosedHandler([&](const TSessionClosedEvent& event) { + ++handlerCalls; + handlerStatus = event.GetStatus(); + }); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); })); + + fixture.FailNextDiscoveries(); + auto deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline && fixture.SessionClosedEvents == 0) { + fixture.Pump(*session); + Sleep(TDuration::MilliSeconds(50)); + } + UNIT_ASSERT_C(fixture.SessionClosedEvents >= 1, "a failed discovery should close the session"); + // The executor runs nothing until drained, so the discovery thread did not call the handler. + UNIT_ASSERT_VALUES_EQUAL(handlerCalls.load(), 0); + + deadline = TInstant::Now() + TDuration::Seconds(20); + while (TInstant::Now() < deadline && handlerCalls.load() == 0) { + executor->Drain(); + Sleep(TDuration::MilliSeconds(50)); + } + executor->Drain(); + UNIT_ASSERT_VALUES_EQUAL(handlerCalls.load(), 1); + UNIT_ASSERT(handlerStatus != EStatus::SUCCESS); + UNIT_ASSERT(handlerStatus != EStatus::STATUS_UNDEFINED); + } + + Y_UNIT_TEST(ReadMirroredFollowsAddedAndRemovedDatabase) { + TMembershipFixture fixture(__FUNCTION__, 1); + fixture.Setup->CreateTopic(fixture.Setup->GetTestTopic() + "-mirrored-from-dc2", fixture.Setup->GetLocalCluster()); + + TFederatedReadSessionSettings settings; + settings.ReadMirrored("dc1"); + auto session = fixture.StartReader(std::move(settings)); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.AllOn("dc1"); }), + "the mirrored reader should start from dc1"); + + fixture.SetDatabases({TDbSpec{"dc1"}, TDbSpec{"dc2"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.OriginStarts["dc2"] > 0; }), + "a database that joins should add its mirrored topic"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + + const int startsBeforeRemoval = fixture.OriginStarts["dc1"]; + fixture.SetDatabases({TDbSpec{"dc1"}}); + UNIT_ASSERT_C(fixture.WaitUntil(*session, [&] { return fixture.OriginStarts["dc1"] > startsBeforeRemoval; }), + "removing dc2 should rebuild the mirrored reader and keep dc1"); + UNIT_ASSERT_VALUES_EQUAL(fixture.SessionClosedEvents, 0); + } + +} + +} // namespace NYdb::NFederatedTopic::NTests From 8e89ee2e7e3f577cb477bf9af12338bc2a9dd60c Mon Sep 17 00:00:00 2001 From: Ilnaz Nizametdinov Date: Fri, 30 Oct 2026 11:50:09 +0000 Subject: [PATCH 11/15] Add `AVAILABILITY_PERIOD` to async replication (#57425) --- .github/last_commit.txt | 2 +- src/api/protos/draft/ydb_replication.proto | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 3d2ad1086b2..c441d23da4e 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -27f9c3828d59dbfeaa81ae37180132d29db2f59e +3e12111f5f48819f72cd2314793688046dd497cd diff --git a/src/api/protos/draft/ydb_replication.proto b/src/api/protos/draft/ydb_replication.proto index 51d03d9b5ac..d2f55196d26 100644 --- a/src/api/protos/draft/ydb_replication.proto +++ b/src/api/protos/draft/ydb_replication.proto @@ -7,6 +7,7 @@ import "src/api/protos/ydb_operation.proto"; import "src/api/protos/ydb_scheme.proto"; import "google/protobuf/duration.proto"; +import "google/protobuf/empty.proto"; import "google/protobuf/timestamp.proto"; package Ydb.Replication; @@ -53,6 +54,13 @@ message ConsistencyLevelGlobal { google.protobuf.Duration commit_interval = 1; } +message AvailabilityPeriod { + oneof value { + google.protobuf.Duration duration = 1; + google.protobuf.Empty unlimited = 2; + } +} + message DescribeReplicationResult { message Stats { optional google.protobuf.Duration lag = 1; @@ -84,6 +92,7 @@ message DescribeReplicationResult { // Description of scheme object. Ydb.Scheme.Entry self = 1; + AvailabilityPeriod availability_period = 10; ConnectionParams connection_params = 2; oneof consistency_level { ConsistencyLevelRow row_consistency = 7; From 5a1895388c8020f651f0788591ae509a53b3089a Mon Sep 17 00:00:00 2001 From: YDBot Date: Fri, 30 Oct 2026 11:50:17 +0000 Subject: [PATCH 12/15] Merge pull request #57563 from ydb-platform/robots/branch-sync/merge-rightlib-261030-0054 --- .github/last_commit.txt | 2 +- util/generic/ptr.h | 7 +++++++ util/generic/ptr_ut.cpp | 7 ++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index c441d23da4e..4458adc1079 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -3e12111f5f48819f72cd2314793688046dd497cd +7828ce7a3ae1c3d5b72643d55b2ad204109bb4f8 diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 9f6b5c65273..10dc0b616b8 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -718,6 +718,13 @@ class TIntrusiveConstPtr: public TPointerBase, const p.T_ = nullptr; } + template > + inline TIntrusiveConstPtr(TIntrusivePtr p) noexcept + : T_(p.T_) + { + p.T_ = nullptr; + } + template > inline TIntrusiveConstPtr(const TIntrusiveConstPtr& p) noexcept : T_(p.T_) diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp index 25390efbc46..903492eb41e 100644 --- a/util/generic/ptr_ut.cpp +++ b/util/generic/ptr_ut.cpp @@ -546,10 +546,10 @@ namespace NTestIntrusiveConvertion { void Func(TIntrusivePtr) { } - void Func(TIntrusiveConstPtr) { + void FuncConst(TIntrusiveConstPtr) { } - void Func(TIntrusiveConstPtr) { + void FuncConst(TIntrusiveConstPtr) { } } // namespace NTestIntrusiveConvertion @@ -567,6 +567,7 @@ void TPointerTest::TestIntrusiveConvertion() { // test that Func(TIntrusivePtr) doesn't participate in overload resolution Func(aa); + FuncConst(aa); } void TPointerTest::TestIntrusiveConstConvertion() { @@ -582,7 +583,7 @@ void TPointerTest::TestIntrusiveConstConvertion() { UNIT_ASSERT_VALUES_EQUAL(a->RefCount(), 1); // test that Func(TIntrusiveConstPtr) doesn't participate in overload resolution - Func(aa); + FuncConst(aa); } void TPointerTest::TestMakeIntrusive() { From d59167857d3aa2d107f7c4528bff391b5a9ba16f Mon Sep 17 00:00:00 2001 From: Ermoshkin Artem <94714022+Shfdis@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:50:26 +0000 Subject: [PATCH 13/15] Bump C++ SDK changelog version to 3.26.0 (#57617) --- .github/last_commit.txt | 2 +- CHANGELOG.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/last_commit.txt b/.github/last_commit.txt index 4458adc1079..e31bfe03864 100644 --- a/.github/last_commit.txt +++ b/.github/last_commit.txt @@ -1 +1 @@ -7828ce7a3ae1c3d5b72643d55b2ad204109bb4f8 +e298c2650fafe1aab48d99022912303996c9e710 diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c96ea6f21..a7e7ba85cb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## v3.26.0 + * Federated topic read sessions now follow federation membership. After the initial discovery, databases that join the federation are read and databases that leave it are dropped; partition sessions of a dropped database get `TPartitionSessionClosedEvent` with the `ConnectionLost` reason. A listed database that becomes unavailable keeps its subsession. The refresh period is `TFederatedTopicClientSettings::FederationDiscoveryInterval` (30 seconds by default). * Fixed Query session shutdown racing with session pool return. From 996bde1ce51ffee9bf980b45562a467b85e6807f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:50:26 +0000 Subject: [PATCH 14/15] Update import generation: 52 --- .github/import_generation.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/import_generation.txt b/.github/import_generation.txt index 0691f67b202..59343b09ec7 100644 --- a/.github/import_generation.txt +++ b/.github/import_generation.txt @@ -1 +1 @@ -52 +53 From 619ae57524d24bb1d28261f82ac5835e054d43cf Mon Sep 17 00:00:00 2001 From: Ermoshkin Artem <94714022+Shfdis@users.noreply.github.com> Date: Fri, 30 Oct 2026 14:58:53 +0300 Subject: [PATCH 15/15] Bump SDK version to 3.26.0 --- src/version.h | 2 +- tests/unit/client/federated_topic/CMakeLists.txt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 845de46fbe9..24c1b713a36 100644 --- a/src/version.h +++ b/src/version.h @@ -2,7 +2,7 @@ namespace NYdb { -inline const char* YDB_SDK_VERSION = "3.25.0"; +inline const char* YDB_SDK_VERSION = "3.26.0"; inline const char* YDB_CERTIFICATE_FILE_KEY = "ydb_root_ca_v3.pem"; } // namespace NYdb diff --git a/tests/unit/client/federated_topic/CMakeLists.txt b/tests/unit/client/federated_topic/CMakeLists.txt index 8d61a2291a5..8bfc80c8fbb 100644 --- a/tests/unit/client/federated_topic/CMakeLists.txt +++ b/tests/unit/client/federated_topic/CMakeLists.txt @@ -1,3 +1,6 @@ +# read_session_membership_ut.cpp requires the in-process YDB/PersQueue test +# server, which is not included in the standalone SDK. + add_ydb_test(NAME client-federated-topic-observer_ut INCLUDE_DIRS ${YDB_SDK_SOURCE_DIR}