Skip to content

Commit

Permalink
RavenDB-20934 : address PR comments - add method for getting debug lo…
Browse files Browse the repository at this point in the history
…gs for a specific node and unify code
  • Loading branch information
aviv committed May 9, 2024
1 parent 88c33f6 commit f9d6012
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 60 deletions.
38 changes: 1 addition & 37 deletions test/SlowTests/Sharding/ETL/ShardedEtlFailoverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,43 +861,7 @@ private async Task<string> 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();
}
Expand Down
70 changes: 47 additions & 23 deletions test/Tests.Infrastructure/ClusterTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,43 +1172,55 @@ public async Task DeleteInRegularMode(IDocumentStore store, int n)

internal async Task GetClusterDebugLogsAsync(StringBuilder sb)
{
(ClusterObserverLogEntry[] List, long Iteration) logs;
List<DynamicJsonValue> historyLogs = null;
DynamicJsonValue inMemoryDebug = null;
List<string> 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());
Expand All @@ -1217,31 +1229,43 @@ 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());
}
}
}

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<DynamicJsonValue> HistoryLogs;

public DynamicJsonValue InMemoryDebug;

public List<string> PrevStates;
}


public override void Dispose()
{
foreach (var disposable in _toDispose)
Expand Down

0 comments on commit f9d6012

Please sign in to comment.