98 lines
2.5 KiB
C#
Raw Permalink Normal View History

2025-01-16 13:24:57 +01:00
using CodexClient;
using Core;
2023-04-13 09:33:10 +02:00
using System.Collections;
using Utils;
2023-04-13 09:33:10 +02:00
namespace CodexPlugin
2023-04-13 09:33:10 +02:00
{
2023-09-19 11:51:59 +02:00
public interface ICodexNodeGroup : IEnumerable<ICodexNode>, IHasManyMetricScrapeTargets
2023-04-13 09:33:10 +02:00
{
2025-01-15 15:43:50 +01:00
void Stop(bool waitTillStopped);
2023-09-19 11:51:59 +02:00
ICodexNode this[int index] { get; }
2023-04-13 09:33:10 +02:00
}
public class CodexNodeGroup : ICodexNodeGroup
{
2025-01-16 13:51:29 +01:00
private readonly ICodexNode[] nodes;
2025-01-16 13:51:29 +01:00
public CodexNodeGroup(IPluginTools tools, ICodexNode[] nodes)
2023-04-13 09:33:10 +02:00
{
this.nodes = nodes;
2024-03-26 10:03:52 +01:00
Version = new DebugInfoVersion();
2023-04-13 09:33:10 +02:00
}
2023-09-19 11:51:59 +02:00
public ICodexNode this[int index]
2023-04-13 09:33:10 +02:00
{
get
{
return Nodes[index];
}
}
2025-01-15 15:43:50 +01:00
public void Stop(bool waitTillStopped)
2023-04-13 11:07:36 +02:00
{
2025-01-15 15:43:50 +01:00
foreach (var node in Nodes) node.Stop(waitTillStopped);
2023-04-13 11:07:36 +02:00
}
2023-04-13 09:33:10 +02:00
2024-04-13 17:09:17 +03:00
public void Stop(CodexNode node, bool waitTillStopped)
{
2025-01-15 15:43:50 +01:00
node.Stop(waitTillStopped);
2024-04-13 17:09:17 +03:00
}
public ICodexNode[] Nodes => nodes;
2024-03-26 10:03:52 +01:00
public DebugInfoVersion Version { get; private set; }
2025-01-16 15:13:16 +01:00
public Address[] GetMetricsScrapeTargets()
{
return Nodes.Select(n => n.GetMetricsScrapeTarget()).ToArray();
}
2023-04-13 09:33:10 +02:00
2023-09-19 11:51:59 +02:00
public IEnumerator<ICodexNode> GetEnumerator()
2023-04-13 09:33:10 +02:00
{
2023-09-19 11:51:59 +02:00
return Nodes.Cast<ICodexNode>().GetEnumerator();
2023-04-13 09:33:10 +02:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return Nodes.GetEnumerator();
}
2025-08-13 11:35:03 +02:00
public string Names()
2023-04-13 09:33:10 +02:00
{
2025-08-13 11:35:03 +02:00
return $"[{string.Join(",", Nodes.Select(n => n.GetName()))}]";
}
public override string ToString()
{
return Names();
2023-04-13 09:33:10 +02:00
}
public void EnsureOnline()
2023-04-13 09:33:10 +02:00
{
2023-07-31 11:51:29 +02:00
var versionResponses = Nodes.Select(n => n.Version);
var first = versionResponses.First();
2024-03-26 08:58:16 +01:00
if (!versionResponses.All(v => v.Version == first.Version && v.Revision == first.Revision))
2023-06-29 16:07:49 +02:00
{
2023-07-31 11:51:29 +02:00
throw new Exception("Inconsistent version information received from one or more Codex nodes: " +
string.Join(",", versionResponses.Select(v => v.ToString())));
2023-06-29 16:07:49 +02:00
}
2023-07-31 11:51:29 +02:00
Version = first;
}
2023-04-13 09:33:10 +02:00
}
2025-08-13 11:35:03 +02:00
public static class CodexNodeGroupExtensions
{
public static string Names(this ICodexNode[] nodes)
{
return $"[{string.Join(",", nodes.Select(n => n.GetName()))}]";
}
public static string Names(this List<ICodexNode> nodes)
{
return $"[{string.Join(",", nodes.Select(n => n.GetName()))}]";
}
}
2023-04-13 09:33:10 +02:00
}