Skip to content

Commit

Permalink
Moved commit "Added new versions of RetryQuery" from ydb repo
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-shchetinin authored and Gazizonoki committed Aug 26, 2024
1 parent f74c4e8 commit 5c4dfb9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
25 changes: 21 additions & 4 deletions include/ydb-cpp-sdk/client/query/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ namespace NYdb {
namespace NRetry::Async {
template <typename TClient, typename TAsyncStatusType>
class TRetryContext;
}
} // namespace NRetry::Async
namespace NRetry::Sync {
template <typename TClient, typename TStatusType>
class TRetryContext;
} // namespace NRetry::Sync
}

namespace NYdb::NQuery {
Expand Down Expand Up @@ -55,10 +59,15 @@ class TSession;
class TQueryClient {
friend class TSession;
friend class NRetry::Async::TRetryContext<TQueryClient, TAsyncExecuteQueryResult>;
friend class NRetry::Async::TRetryContext<TQueryClient, TAsyncStatus>;
friend class NRetry::Sync::TRetryContext<TQueryClient, TStatus>;

public:
using TQueryFunc = std::function<TAsyncExecuteQueryResult(TSession session)>;
using TQueryWithoutSessionFunc = std::function<TAsyncExecuteQueryResult(TQueryClient& client)>;
using TQueryResultFunc = std::function<TAsyncExecuteQueryResult(TSession session)>;
using TQueryFunc = std::function<TAsyncStatus(TSession session)>;
using TQuerySyncFunc = std::function<TStatus(TSession session)>;
using TQueryWithoutSessionFunc = std::function<TAsyncStatus(TQueryClient& client)>;
using TQueryWithoutSessionSyncFunc = std::function<TStatus(TQueryClient& client)>;
using TSettings = TClientSettings;
using TSession = TSession;
using TCreateSessionSettings = TCreateSessionSettings;
Expand All @@ -79,7 +88,15 @@ class TQueryClient {
TAsyncExecuteQueryIterator StreamExecuteQuery(const std::string& query, const TTxControl& txControl,
const TParams& params, const TExecuteQuerySettings& settings = TExecuteQuerySettings());

TAsyncExecuteQueryResult RetryQuery(TQueryFunc&& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());
TAsyncExecuteQueryResult RetryQuery(TQueryResultFunc&& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());

TAsyncStatus RetryQuery(TQueryFunc&& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());

TAsyncStatus RetryQuery(TQueryWithoutSessionFunc&& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());

TStatus RetryQuery(const TQuerySyncFunc& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());

TStatus RetryQuery(const TQueryWithoutSessionSyncFunc& queryFunc, TRetryOperationSettings settings = TRetryOperationSettings());

TAsyncExecuteQueryResult RetryQuery(const std::string& query, const TTxControl& txControl,
TDuration timeout, bool isIndempotent);
Expand Down
27 changes: 24 additions & 3 deletions src/client/query/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

namespace NYdb::NQuery {

using TRetryContextAsync = NRetry::Async::TRetryContext<TQueryClient, TAsyncExecuteQueryResult>;
using TRetryContextResultAsync = NRetry::Async::TRetryContext<TQueryClient, TAsyncExecuteQueryResult>;
using TRetryContextAsync = NRetry::Async::TRetryContext<TQueryClient, TAsyncStatus>;

NYdb::NRetry::TRetryOperationSettings GetRetrySettings(TDuration timeout, bool isIndempotent) {
return NYdb::NRetry::TRetryOperationSettings()
Expand Down Expand Up @@ -577,20 +578,40 @@ int64_t TQueryClient::GetCurrentPoolSize() const {
return Impl_->GetCurrentPoolSize();
}

TAsyncExecuteQueryResult TQueryClient::RetryQuery(TQueryFunc&& queryFunc, TRetryOperationSettings settings)
TAsyncExecuteQueryResult TQueryClient::RetryQuery(TQueryResultFunc&& queryFunc, TRetryOperationSettings settings)
{
TRetryContextResultAsync::TPtr ctx(new NRetry::Async::TRetryWithSession(*this, std::move(queryFunc), settings));
return ctx->Execute();
}

TAsyncStatus TQueryClient::RetryQuery(TQueryFunc&& queryFunc, TRetryOperationSettings settings) {
TRetryContextAsync::TPtr ctx(new NRetry::Async::TRetryWithSession(*this, std::move(queryFunc), settings));
return ctx->Execute();
}

TAsyncStatus TQueryClient::RetryQuery(TQueryWithoutSessionFunc&& queryFunc, TRetryOperationSettings settings) {
TRetryContextAsync::TPtr ctx(new NRetry::Async::TRetryWithoutSession(*this, std::move(queryFunc), settings));
return ctx->Execute();
}

TStatus TQueryClient::RetryQuery(const TQuerySyncFunc& queryFunc, TRetryOperationSettings settings) {
NRetry::Sync::TRetryWithSession ctx(*this, queryFunc, settings);
return ctx.Execute();
}

TStatus TQueryClient::RetryQuery(const TQueryWithoutSessionSyncFunc& queryFunc, TRetryOperationSettings settings) {
NRetry::Sync::TRetryWithoutSession ctx(*this, queryFunc, settings);
return ctx.Execute();
}

TAsyncExecuteQueryResult TQueryClient::RetryQuery(const std::string& query, const TTxControl& txControl,
TDuration timeout, bool isIndempotent)
{
auto settings = GetRetrySettings(timeout, isIndempotent);
auto queryFunc = [&query, &txControl](TSession session, TDuration duration) -> TAsyncExecuteQueryResult {
return session.ExecuteQuery(query, txControl, TExecuteQuerySettings().ClientTimeout(duration));
};
TRetryContextAsync::TPtr ctx(new NRetry::Async::TRetryWithSession(*this, std::move(queryFunc), settings));
TRetryContextResultAsync::TPtr ctx(new NRetry::Async::TRetryWithSession(*this, std::move(queryFunc), settings));
return ctx->Execute();
}

Expand Down

0 comments on commit 5c4dfb9

Please sign in to comment.