Skip to content

Commit

Permalink
Merge pull request #48 Add logging for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 14, 2023
2 parents 5ccb9cd + a331d34 commit 037f303
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add logging for transactions

## v0.1.5
- Fix timeout error on create session
- Fix transport error on delete session
Expand Down
4 changes: 2 additions & 2 deletions src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task<ExecuteDataQueryResponse> ExecuteDataQuery(
{
OperationParams = MakeOperationParams(settings),
SessionId = Id,
TxControl = txControl.ToProto(),
TxControl = txControl.ToProto(_logger),
Query = new Query
{
YqlText = query
Expand Down Expand Up @@ -96,7 +96,7 @@ public async Task<ExecuteDataQueryResponse> ExecuteDataQuery(
? TransactionState.Active
: TransactionState.Void;

tx = Transaction.FromProto(resultProto.TxMeta);
tx = Transaction.FromProto(resultProto.TxMeta, _logger);
}

ExecuteDataQueryResponse.ResultData? result = null;
Expand Down
42 changes: 36 additions & 6 deletions src/Ydb.Sdk/src/Services/Table/Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ydb.Table;
using Microsoft.Extensions.Logging;
using Ydb.Table;

namespace Ydb.Sdk.Services.Table;

Expand All @@ -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;
}
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -80,7 +105,7 @@ public static TxControl Tx(Transaction tx)
return new TxControl(new TransactionControl
{
TxId = tx.TxId
});
}, tx.TxNum);
}

public TxControl Commit()
Expand All @@ -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();
}
}

0 comments on commit 037f303

Please sign in to comment.