From f9d601229e89c5d905ee79c2d7b450d125abbe94 Mon Sep 17 00:00:00 2001 From: aviv Date: Thu, 9 May 2024 13:27:19 +0300 Subject: [PATCH] RavenDB-20934 : address PR comments - add method for getting debug logs for a specific node and unify code --- .../Sharding/ETL/ShardedEtlFailoverTests.cs | 38 +--------- test/Tests.Infrastructure/ClusterTestBase.cs | 70 +++++++++++++------ 2 files changed, 48 insertions(+), 60 deletions(-) diff --git a/test/SlowTests/Sharding/ETL/ShardedEtlFailoverTests.cs b/test/SlowTests/Sharding/ETL/ShardedEtlFailoverTests.cs index aa1332d888df..1342feb5a0c9 100644 --- a/test/SlowTests/Sharding/ETL/ShardedEtlFailoverTests.cs +++ b/test/SlowTests/Sharding/ETL/ShardedEtlFailoverTests.cs @@ -861,43 +861,7 @@ private async Task AddDebugInfo(Exception ex, RavenServer node) await GetClusterDebugLogsAsync(sb); sb.AppendLine().AppendLine($"Debug logs for removed node '{node.ServerStore.NodeTag}':"); - - var prevStates = node.ServerStore.Engine.PrevStates; - sb.AppendLine($"{Environment.NewLine}States:{Environment.NewLine}-----------------------"); - foreach (var state in prevStates) - { - sb.AppendLine($"{state}{Environment.NewLine}"); - } - sb.AppendLine(); - - using (node.ServerStore.Engine.ContextPool.AllocateOperationContext(out ClusterOperationContext clusterOperationContext)) - using (clusterOperationContext.OpenReadTransaction()) - { - var historyLogs = node.ServerStore.Engine.LogHistory.GetHistoryLogs(clusterOperationContext); - sb.AppendLine($"HistoryLogs:{Environment.NewLine}-----------------------"); - - using (var context = JsonOperationContext.ShortTermSingleUse()) - { - var c = 0; - foreach (var log in historyLogs) - { - var json = context.ReadObject(log, nameof(log) + $"{c++}"); - sb.AppendLine(json.ToString()); - } - } - sb.AppendLine(); - - var inMemoryDebug = node.ServerStore.Engine.InMemoryDebug.ToJson(); - if (inMemoryDebug != null) - { - sb.AppendLine($"RachisDebug:{Environment.NewLine}-----------------------"); - using (var context = JsonOperationContext.ShortTermSingleUse()) - { - var json = context.ReadObject(inMemoryDebug, nameof(inMemoryDebug)); - sb.AppendLine(json.ToString()); - } - } - } + GetDebugLogsForNode(node, sb); return sb.ToString(); } diff --git a/test/Tests.Infrastructure/ClusterTestBase.cs b/test/Tests.Infrastructure/ClusterTestBase.cs index f99ae9154a00..08bff3144f7b 100644 --- a/test/Tests.Infrastructure/ClusterTestBase.cs +++ b/test/Tests.Infrastructure/ClusterTestBase.cs @@ -1172,43 +1172,55 @@ public async Task DeleteInRegularMode(IDocumentStore store, int n) internal async Task GetClusterDebugLogsAsync(StringBuilder sb) { - (ClusterObserverLogEntry[] List, long Iteration) logs; - List historyLogs = null; - DynamicJsonValue inMemoryDebug = null; - List prevStates = null; - logs.List = null; + NodeDebugInfo debugInfo = null; await ActionWithLeader((l) => { - logs = l.ServerStore.Observer.ReadDecisionsForDatabase(); - prevStates = l.ServerStore.Engine.PrevStates.Select(s => s.ToString()).ToList(); - - using (l.ServerStore.Engine.ContextPool.AllocateOperationContext(out ClusterOperationContext context)) - using (context.OpenReadTransaction()) - { - historyLogs = l.ServerStore.Engine.LogHistory.GetHistoryLogs(context).ToList(); - inMemoryDebug = l.ServerStore.Engine.InMemoryDebug.ToJson(); - } - + debugInfo = GetDebugInfoForNode(l); return Task.CompletedTask; }); - if (prevStates != null) + AppendDebugInfo(sb, debugInfo); + } + + internal static void GetDebugLogsForNode(RavenServer node, StringBuilder sb) => AppendDebugInfo(sb, GetDebugInfoForNode(node)); + + private static NodeDebugInfo GetDebugInfoForNode(RavenServer node) + { + var debugInfo = new NodeDebugInfo + { + ClusterObserverLogs = node.ServerStore.Observer?.ReadDecisionsForDatabase().List, + PrevStates = node.ServerStore.Engine.PrevStates.Select(s => s.ToString()).ToList() + }; + + using (node.ServerStore.Engine.ContextPool.AllocateOperationContext(out ClusterOperationContext context)) + using (context.OpenReadTransaction()) + { + debugInfo.HistoryLogs = node.ServerStore.Engine.LogHistory.GetHistoryLogs(context).ToList(); + debugInfo.InMemoryDebug = node.ServerStore.Engine.InMemoryDebug.ToJson(); + } + + return debugInfo; + } + + private static void AppendDebugInfo(StringBuilder sb, NodeDebugInfo debugInfo) + { + if (debugInfo.PrevStates != null) { sb.AppendLine($"{Environment.NewLine}States:{Environment.NewLine}-----------------------"); - foreach (var state in prevStates) + foreach (var state in debugInfo.PrevStates) { sb.AppendLine($"{state}{Environment.NewLine}"); } sb.AppendLine(); } - if (historyLogs != null) + if (debugInfo.HistoryLogs != null) { sb.AppendLine($"HistoryLogs:{Environment.NewLine}-----------------------"); using (var context = JsonOperationContext.ShortTermSingleUse()) { var c = 0; - foreach (var log in historyLogs) + foreach (var log in debugInfo.HistoryLogs) { var json = context.ReadObject(log, nameof(log) + $"{c++}"); sb.AppendLine(json.ToString()); @@ -1217,13 +1229,13 @@ await ActionWithLeader((l) => sb.AppendLine(); } - if (logs.List.Length > 0) + if (debugInfo.ClusterObserverLogs?.Length > 0) { sb.AppendLine($"Cluster Observer Log Entries:{Environment.NewLine}-----------------------"); using (var context = JsonOperationContext.ShortTermSingleUse()) { var c = 0; - foreach (var log in logs.List) + foreach (var log in debugInfo.ClusterObserverLogs) { var json = context.ReadObject(log.ToJson(), nameof(log) + $"{c++}"); sb.AppendLine(json.ToString()); @@ -1231,17 +1243,29 @@ await ActionWithLeader((l) => } } - if (inMemoryDebug != null) + if (debugInfo.InMemoryDebug != null) { sb.AppendLine($"RachisDebug:{Environment.NewLine}-----------------------"); using (var context = JsonOperationContext.ShortTermSingleUse()) { - var json = context.ReadObject(inMemoryDebug, nameof(inMemoryDebug)); + var json = context.ReadObject(debugInfo.InMemoryDebug, nameof(NodeDebugInfo.InMemoryDebug)); sb.AppendLine(json.ToString()); } } } + private class NodeDebugInfo + { + public ClusterObserverLogEntry[] ClusterObserverLogs; + + public List HistoryLogs; + + public DynamicJsonValue InMemoryDebug; + + public List PrevStates; + } + + public override void Dispose() { foreach (var disposable in _toDispose)