Skip to content

Commit 037f303

Browse files
authored
Merge pull request #48 Add logging for transactions
2 parents 5ccb9cd + a331d34 commit 037f303

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Add logging for transactions
2+
13
## v0.1.5
24
- Fix timeout error on create session
35
- Fix transport error on delete session

src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task<ExecuteDataQueryResponse> ExecuteDataQuery(
6565
{
6666
OperationParams = MakeOperationParams(settings),
6767
SessionId = Id,
68-
TxControl = txControl.ToProto(),
68+
TxControl = txControl.ToProto(_logger),
6969
Query = new Query
7070
{
7171
YqlText = query
@@ -96,7 +96,7 @@ public async Task<ExecuteDataQueryResponse> ExecuteDataQuery(
9696
? TransactionState.Active
9797
: TransactionState.Void;
9898

99-
tx = Transaction.FromProto(resultProto.TxMeta);
99+
tx = Transaction.FromProto(resultProto.TxMeta, _logger);
100100
}
101101

102102
ExecuteDataQueryResponse.ResultData? result = null;

src/Ydb.Sdk/src/Services/Table/Transaction.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Ydb.Table;
1+
using Microsoft.Extensions.Logging;
2+
using Ydb.Table;
23

34
namespace Ydb.Sdk.Services.Table;
45

@@ -11,15 +12,36 @@ internal Transaction(string txId)
1112

1213
public string TxId { get; }
1314

14-
internal static Transaction? FromProto(TransactionMeta proto)
15+
internal int? TxNum { get; private set; }
16+
17+
internal static Transaction? FromProto(TransactionMeta proto, ILogger? logger = null)
1518
{
1619
if (proto.Id.Length == 0)
1720
{
1821
return null;
1922
}
2023

21-
return new Transaction(
24+
var tx = new Transaction(
2225
txId: proto.Id);
26+
if (!string.IsNullOrEmpty(proto.Id))
27+
{
28+
tx.TxNum = GetTxCounter();
29+
logger.LogTrace($"Received tx #{tx.TxNum}");
30+
}
31+
32+
return tx;
33+
}
34+
35+
private static readonly object TxCounterLock = new();
36+
private static int _txCounter;
37+
38+
private static int GetTxCounter()
39+
{
40+
lock (TxCounterLock)
41+
{
42+
_txCounter++;
43+
return _txCounter;
44+
}
2345
}
2446
}
2547

@@ -34,9 +56,12 @@ public class TxControl
3456
{
3557
private readonly TransactionControl _proto;
3658

37-
private TxControl(TransactionControl proto)
59+
private readonly int? _txNum;
60+
61+
private TxControl(TransactionControl proto, int? txNum = null)
3862
{
3963
_proto = proto;
64+
_txNum = txNum;
4065
}
4166

4267
public static TxControl BeginSerializableRW()
@@ -80,7 +105,7 @@ public static TxControl Tx(Transaction tx)
80105
return new TxControl(new TransactionControl
81106
{
82107
TxId = tx.TxId
83-
});
108+
}, tx.TxNum);
84109
}
85110

86111
public TxControl Commit()
@@ -89,8 +114,13 @@ public TxControl Commit()
89114
return this;
90115
}
91116

92-
internal TransactionControl ToProto()
117+
internal TransactionControl ToProto(ILogger? logger = null)
93118
{
119+
if (_txNum != null)
120+
{
121+
logger.LogTrace($"Using tx #{_txNum}");
122+
}
123+
94124
return _proto.Clone();
95125
}
96126
}

0 commit comments

Comments
 (0)