From baeb93c645c64934c8d9ad1ddbd40d05f99406ee Mon Sep 17 00:00:00 2001 From: XmasApple Date: Wed, 15 Nov 2023 15:36:14 +0300 Subject: [PATCH] QueryService: not use begin/commit/rollback transaction methods from TableService --- src/Ydb.Sdk/src/Services/Query/QueryClient.cs | 180 +----------------- src/Ydb.Sdk/src/Services/Query/Tx.cs | 11 +- 2 files changed, 8 insertions(+), 183 deletions(-) diff --git a/src/Ydb.Sdk/src/Services/Query/QueryClient.cs b/src/Ydb.Sdk/src/Services/Query/QueryClient.cs index 2cd4d5a3..580b0c74 100644 --- a/src/Ydb.Sdk/src/Services/Query/QueryClient.cs +++ b/src/Ydb.Sdk/src/Services/Query/QueryClient.cs @@ -248,7 +248,9 @@ internal BeginTransactionResponse(Status status) : base(status) private BeginTransactionResponse(Ydb.Query.BeginTransactionResponse proto) : base( Status.FromProto(proto.Status, proto.Issues)) { - Tx.TxId = proto.TxMeta.Id; + var txId = proto.TxMeta.Id; + var tx = new Tx(new TransactionControl { TxId = txId }); + Tx = tx; } internal static BeginTransactionResponse FromProto(Ydb.Query.BeginTransactionResponse proto) @@ -291,66 +293,6 @@ internal static RollbackTransactionResponse FromProto(Ydb.Query.RollbackTransact } } -// TODO remove crutch calls -// =========================================================================== -// WARNING: temporary structs just for testing should be removed -// =========================================================================== -public class BeginCrutchTxSettings : OperationRequestSettings -{ -} - -public class CommitCrutchTxSettings : OperationRequestSettings -{ -} - -public class RollbackCrutchTxSettings : OperationRequestSettings -{ -} - -internal class BeginCrutchTxResponse : ResponseWithResultBase -{ - internal BeginCrutchTxResponse(Status status, ResultData? result = null) : base(status, result) - { - } - - public class ResultData - { - public Tx? Tx { get; } - - public ResultData(Tx? tx) - { - Tx = tx; - } - - internal static ResultData FromProto(Ydb.Table.BeginTransactionResult resultProto) - { - var txId = resultProto.TxMeta.Id; - // var tx = new Tx { TxId = resultProto.TxMeta.Id }; - var tx = new Tx(new TransactionControl { TxId = txId }); - var result = new ResultData(tx); - - return result; - } - } -} - -internal class CommitCrutchTxResponse : ResponseBase -{ - internal CommitCrutchTxResponse(Status status) : base(status) - { - } -} - -internal class RollbackCrutchTxResponse : ResponseBase -{ - internal RollbackCrutchTxResponse(Status status) : base(status) - { - } -} -// =========================================================================== -// END WARNING: temporary structs just for testing should be removed -// =========================================================================== - public class QueryClientConfig { public SessionPoolConfig SessionPoolConfig { get; } @@ -638,12 +580,10 @@ public async Task Exec(string queryString, private async Task> Rollback(Session session, Tx tx, Status status) { - // TODO remove crutch calls _logger.LogTrace($"Transaction {tx.TxId} not committed, try to rollback"); try { - // var rollbackResponse = await RollbackTransaction(session.Id, tx); - var rollbackResponse = await RollbackCrutchTx(session.Id, tx); + var rollbackResponse = await RollbackTransaction(session.Id, tx); rollbackResponse.EnsureSuccess(); } catch (StatusUnsuccessfulException e) @@ -659,19 +599,12 @@ public async Task> DoTx(Func> func, ITxModeSettings? txModeSettings = null, RetrySettings? retrySettings = null) { - // TODO remove crutch calls var response = await ExecOnSession( async session => { - // var beginTransactionResponse = await BeginTransaction(session.Id, Tx.Begin(txModeSettings)); - // beginTransactionResponse.EnsureSuccess(); - // var tx = beginTransactionResponse.Tx; - // tx.QueryClient = this; - // tx.SessionId = session.Id; - - var beginTransactionResponse = await BeginCrutchTx(session.Id, Tx.Begin(txModeSettings)); + var beginTransactionResponse = await BeginTransaction(session.Id, Tx.Begin(txModeSettings)); beginTransactionResponse.EnsureSuccess(); - var tx = beginTransactionResponse.Result.Tx!; + var tx = beginTransactionResponse.Tx; tx.QueryClient = this; tx.SessionId = session.Id; @@ -694,8 +627,7 @@ public async Task> DoTx(Func> func, return rollbackResponse; } - // var commitResponse = await CommitTransaction(session.Id, tx); - var commitResponse = await CommitCrutchTx(session.Id, tx); + var commitResponse = await CommitTransaction(session.Id, tx); if (!commitResponse.Status.IsSuccess) { return await Rollback(session, tx, commitResponse.Status); @@ -755,104 +687,6 @@ private void Dispose(bool disposing) _disposed = true; } - - -// =========================================================================== -// WARNING: temporary methods just for testing should be removed -// =========================================================================== - internal async Task BeginCrutchTx(string sessionId, - Tx tx, BeginCrutchTxSettings? settings = null) - { - settings ??= new BeginCrutchTxSettings(); - var request = new Ydb.Table.BeginTransactionRequest - { - SessionId = sessionId, - OperationParams = MakeOperationParams(settings), - TxSettings = new Ydb.Table.TransactionSettings - { SerializableReadWrite = new Ydb.Table.SerializableModeSettings() } - }; - try - { - var response = await Driver.UnaryCall( - method: Ydb.Table.V1.TableService.BeginTransactionMethod, - request: request, - settings: settings - ); - var status = UnpackOperation(response.Data.Operation, out Ydb.Table.BeginTransactionResult? resultProto); - BeginCrutchTxResponse.ResultData? result = null; - - if (status.IsSuccess && resultProto is not null) - { - result = BeginCrutchTxResponse.ResultData.FromProto(resultProto); - } - - return new BeginCrutchTxResponse(status, result); - } - catch (Driver.TransportException e) - { - return new BeginCrutchTxResponse(e.Status); - } - } - - internal async Task CommitCrutchTx(string sessionId, Tx tx, - CommitCrutchTxSettings? settings = null) - { - settings ??= new CommitCrutchTxSettings(); - var request = new Ydb.Table.CommitTransactionRequest - { - SessionId = sessionId, - OperationParams = MakeOperationParams(settings), - TxId = tx.TxId - }; - try - { - var response = await Driver.UnaryCall( - method: Ydb.Table.V1.TableService.CommitTransactionMethod, - request: request, - settings: settings - ); - var status = UnpackOperation(response.Data.Operation); - - return new CommitCrutchTxResponse(status); - } - catch (Driver.TransportException e) - { - return new CommitCrutchTxResponse(e.Status); - } - } - - internal async Task RollbackCrutchTx(string sessionId, Tx tx, - RollbackCrutchTxSettings? settings = null) - { - settings ??= new RollbackCrutchTxSettings(); - var request = new Ydb.Table.RollbackTransactionRequest - { - SessionId = sessionId, - OperationParams = MakeOperationParams(settings), - TxId = tx.TxId - }; - - try - { - var response = await Driver.UnaryCall( - method: Ydb.Table.V1.TableService.RollbackTransactionMethod, - request: request, - settings: settings - ); - - var status = UnpackOperation(response.Data.Operation); - - return new RollbackCrutchTxResponse(status); - } - catch (Driver.TransportException e) - { - return new RollbackCrutchTxResponse(e.Status); - } - } - -// =========================================================================== -// END WARNING: temporary methods just for testing should be removed -// =========================================================================== } public class QueryResponse : ResponseBase diff --git a/src/Ydb.Sdk/src/Services/Query/Tx.cs b/src/Ydb.Sdk/src/Services/Query/Tx.cs index 43eb3220..f0962bb0 100644 --- a/src/Ydb.Sdk/src/Services/Query/Tx.cs +++ b/src/Ydb.Sdk/src/Services/Query/Tx.cs @@ -34,16 +34,7 @@ public class Tx internal QueryClient? QueryClient { private get; set; } internal string? SessionId { private get; set; } - public string? TxId - { - get => _proto.TxId; - set - { - _proto.TxId = value; - if (!string.IsNullOrEmpty(value)) - _proto.BeginTx = null; - } - } + public string? TxId => _proto.TxId; private readonly TransactionControl _proto;