Skip to content

Commit

Permalink
RavenDB-22225 capture stacktrace in debug for cluster write tx
Browse files Browse the repository at this point in the history
  • Loading branch information
karmeli87 committed May 1, 2024
1 parent 1844f40 commit ed2bb90
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Raven.Server/Rachis/RachisConsensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ public void Initialize(StorageEnvironment env, RavenConfiguration configuration,
try
{
_persistentState = env;
#if DEBUG
_persistentState.CaptureTransactionStackTrace = ServerStore.EnableCaptureWriteTransactionStackTrace;
#endif

OperationTimeout = configuration.Cluster.OperationTimeout.AsTimeSpan;
ElectionTimeout = configuration.Cluster.ElectionTimeout.AsTimeSpan;
Expand Down
4 changes: 4 additions & 0 deletions src/Raven.Server/ServerWide/ServerStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3858,6 +3858,10 @@ internal sealed class TestingStuff
internal Action<string, List<ClusterTransactionCommand.SingleClusterDatabaseCommand>> BeforeExecuteClusterTransactionBatch;
}

#if DEBUG
public bool EnableCaptureWriteTransactionStackTrace = false;
#endif

public readonly MemoryCache QueryClauseCache;

public void LowMemory(LowMemorySeverity lowMemorySeverity)
Expand Down
11 changes: 9 additions & 2 deletions src/Voron/StorageEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ internal void InvokeAfterCommitWhenNewTransactionsPrevented(LowLevelTransaction
AfterCommitWhenNewTransactionsPrevented?.Invoke(tx);
}

#if DEBUG
public bool CaptureTransactionStackTrace;
#endif

[Conditional("DEBUG")]
private void ThrowOnWriteTransactionOpenedByTheSameThread()
{
Expand All @@ -790,9 +794,12 @@ private void ThrowOnWriteTransactionOpenedByTheSameThread()
$"{currentWriteTransactionHolder.Name}, Id: {currentWriteTransactionHolder.ManagedThreadId}{Environment.NewLine}" +
$"{currentWriteTransactionHolder.CapturedStackTrace}");
}

if (currentWriteTransactionHolder != null)
#if DEBUG
if (currentWriteTransactionHolder != null && CaptureTransactionStackTrace)
{
currentWriteTransactionHolder.CapturedStackTrace = Environment.StackTrace;
}
#endif
}

internal void IncrementUsageOnNewTransaction()
Expand Down
2 changes: 2 additions & 0 deletions test/RachisTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public async Task CanApplyCommitAcrossAllCluster(int amount)
[Fact]
public async Task RavenDB_13659()
{
EnableCaptureWriteTransactionStackTrace = true;

var leader = await CreateNetworkAndGetLeader(1);
var mre = new AsyncManualResetEvent();
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down
6 changes: 5 additions & 1 deletion test/Tests.Infrastructure/RachisConsensusTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ static unsafe RachisConsensusTestBase()
NativeMemory.GetCurrentUnmanagedThreadId = () => (ulong)Pal.rvn_get_current_thread_id();
ZstdLib.CreateDictionaryException = message => new VoronErrorException(message);
RachisStateMachine.EnableDebugLongCommit = true;

Lucene.Net.Util.UnmanagedStringArray.Segment.AllocateMemory = NativeMemory.AllocateMemory;
Lucene.Net.Util.UnmanagedStringArray.Segment.FreeMemory = NativeMemory.Free;
JsonDeserializationCluster.Commands.Add(nameof(TestCommand), JsonDeserializationBase.GenerateJsonDeserializationRoutine<TestCommand>());
Expand Down Expand Up @@ -205,6 +204,8 @@ public static string GetCandidateStatus(IEnumerable<RachisConsensus<CountingStat
return sb.ToString();
}

protected bool EnableCaptureWriteTransactionStackTrace = false;

protected RachisConsensus<CountingStateMachine> SetupServer(bool bootstrap = false, int port = 0, int electionTimeout = 300, [CallerMemberName] string caller = null, bool shouldRunInMemory = true, string nodeTag = null)
{
var tcpListener = new TcpListener(IPAddress.Loopback, port);
Expand Down Expand Up @@ -242,6 +243,9 @@ protected RachisConsensus<CountingStateMachine> SetupServer(bool bootstrap = fal
}

var serverStore = new RavenServer(configuration) { ThrowOnLicenseActivationFailure = true }.ServerStore;
#if DEBUG
serverStore.EnableCaptureWriteTransactionStackTrace = EnableCaptureWriteTransactionStackTrace;
#endif
serverStore.Initialize();
var rachis = new RachisConsensus<CountingStateMachine>(serverStore, seed);
var storageEnvironment = new StorageEnvironment(server);
Expand Down

0 comments on commit ed2bb90

Please sign in to comment.