diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f25bbaa..e249086a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +- Add logging for transactions + ## v0.1.5 - Fix timeout error on create session - Fix transport error on delete session diff --git a/src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs b/src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs index a01681a5..03fe2553 100644 --- a/src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs +++ b/src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs @@ -65,7 +65,7 @@ public async Task ExecuteDataQuery( { OperationParams = MakeOperationParams(settings), SessionId = Id, - TxControl = txControl.ToProto(), + TxControl = txControl.ToProto(_logger), Query = new Query { YqlText = query @@ -96,7 +96,7 @@ public async Task ExecuteDataQuery( ? TransactionState.Active : TransactionState.Void; - tx = Transaction.FromProto(resultProto.TxMeta); + tx = Transaction.FromProto(resultProto.TxMeta, _logger); } ExecuteDataQueryResponse.ResultData? result = null; diff --git a/src/Ydb.Sdk/src/Services/Table/Transaction.cs b/src/Ydb.Sdk/src/Services/Table/Transaction.cs index 9628b5e3..001bc45e 100644 --- a/src/Ydb.Sdk/src/Services/Table/Transaction.cs +++ b/src/Ydb.Sdk/src/Services/Table/Transaction.cs @@ -1,4 +1,5 @@ -using Ydb.Table; +using Microsoft.Extensions.Logging; +using Ydb.Table; namespace Ydb.Sdk.Services.Table; @@ -11,15 +12,36 @@ internal Transaction(string txId) public string TxId { get; } - internal static Transaction? FromProto(TransactionMeta proto) + internal int? TxNum { get; private set; } + + internal static Transaction? FromProto(TransactionMeta proto, ILogger? logger = null) { if (proto.Id.Length == 0) { return null; } - return new Transaction( + var tx = new Transaction( txId: proto.Id); + if (!string.IsNullOrEmpty(proto.Id)) + { + tx.TxNum = GetTxCounter(); + logger.LogTrace($"Received tx #{tx.TxNum}"); + } + + return tx; + } + + private static readonly object TxCounterLock = new(); + private static int _txCounter; + + private static int GetTxCounter() + { + lock (TxCounterLock) + { + _txCounter++; + return _txCounter; + } } } @@ -34,9 +56,12 @@ public class TxControl { private readonly TransactionControl _proto; - private TxControl(TransactionControl proto) + private readonly int? _txNum; + + private TxControl(TransactionControl proto, int? txNum = null) { _proto = proto; + _txNum = txNum; } public static TxControl BeginSerializableRW() @@ -80,7 +105,7 @@ public static TxControl Tx(Transaction tx) return new TxControl(new TransactionControl { TxId = tx.TxId - }); + }, tx.TxNum); } public TxControl Commit() @@ -89,8 +114,13 @@ public TxControl Commit() return this; } - internal TransactionControl ToProto() + internal TransactionControl ToProto(ILogger? logger = null) { + if (_txNum != null) + { + logger.LogTrace($"Using tx #{_txNum}"); + } + return _proto.Clone(); } }