cs-codex-dist-tests/ProjectPlugins/CodexPlugin/CodexNodeGroup.cs

85 lines
2.7 KiB
C#
Raw Normal View History

2023-09-12 11:32:06 +00:00
using Core;
using KubernetesWorkflow.Types;
2023-09-13 09:59:21 +00:00
using MetricsPlugin;
2023-04-13 07:33:10 +00:00
using System.Collections;
namespace CodexPlugin
2023-04-13 07:33:10 +00:00
{
2023-09-19 09:51:59 +00:00
public interface ICodexNodeGroup : IEnumerable<ICodexNode>, IHasManyMetricScrapeTargets
2023-04-13 07:33:10 +00:00
{
2023-09-11 14:57:57 +00:00
void BringOffline();
2023-09-19 09:51:59 +00:00
ICodexNode this[int index] { get; }
2023-04-13 07:33:10 +00:00
}
public class CodexNodeGroup : ICodexNodeGroup
{
private readonly CodexStarter starter;
public CodexNodeGroup(CodexStarter starter, IPluginTools tools, RunningContainers[] containers, ICodexNodeFactory codexNodeFactory)
2023-04-13 07:33:10 +00:00
{
this.starter = starter;
2023-04-13 07:33:10 +00:00
Containers = containers;
2023-09-12 08:31:55 +00:00
Nodes = containers.Containers().Select(c => CreateOnlineCodexNode(c, tools, codexNodeFactory)).ToArray();
2023-07-31 09:51:29 +00:00
Version = new CodexDebugVersionResponse();
2023-04-13 07:33:10 +00:00
}
2023-09-19 09:51:59 +00:00
public ICodexNode this[int index]
2023-04-13 07:33:10 +00:00
{
get
{
return Nodes[index];
}
}
2023-09-11 14:57:57 +00:00
public void BringOffline()
2023-04-13 09:07:36 +00:00
{
starter.BringOffline(this);
2023-04-13 09:07:36 +00:00
// Clear everything. Prevent accidental use.
2023-09-19 09:51:59 +00:00
Nodes = Array.Empty<CodexNode>();
Containers = null!;
2023-04-13 09:07:36 +00:00
}
2023-04-13 07:33:10 +00:00
public RunningContainers[] Containers { get; private set; }
2023-09-19 09:51:59 +00:00
public CodexNode[] Nodes { get; private set; }
2023-07-31 09:51:29 +00:00
public CodexDebugVersionResponse Version { get; private set; }
2023-09-13 09:59:21 +00:00
public IMetricsScrapeTarget[] ScrapeTargets => Nodes.Select(n => n.MetricsScrapeTarget).ToArray();
2023-04-13 07:33:10 +00:00
2023-09-19 09:51:59 +00:00
public IEnumerator<ICodexNode> GetEnumerator()
2023-04-13 07:33:10 +00:00
{
2023-09-19 09:51:59 +00:00
return Nodes.Cast<ICodexNode>().GetEnumerator();
2023-04-13 07:33:10 +00:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return Nodes.GetEnumerator();
}
public string Describe()
{
2023-04-19 12:57:00 +00:00
return $"group:[{Containers.Describe()}]";
2023-04-13 07:33:10 +00:00
}
public void EnsureOnline()
2023-04-13 07:33:10 +00:00
{
2023-07-31 09:51:29 +00:00
foreach (var node in Nodes) node.EnsureOnlineGetVersionResponse();
var versionResponses = Nodes.Select(n => n.Version);
var first = versionResponses.First();
if (!versionResponses.All(v => v.version == first.version && v.revision == first.revision))
2023-06-29 14:07:49 +00:00
{
2023-07-31 09:51:29 +00:00
throw new Exception("Inconsistent version information received from one or more Codex nodes: " +
string.Join(",", versionResponses.Select(v => v.ToString())));
2023-06-29 14:07:49 +00:00
}
2023-07-31 09:51:29 +00:00
Version = first;
2023-04-13 07:33:10 +00:00
}
2023-09-19 09:51:59 +00:00
private CodexNode CreateOnlineCodexNode(RunningContainer c, IPluginTools tools, ICodexNodeFactory factory)
{
2023-09-20 10:55:09 +00:00
var watcher = factory.CreateCrashWatcher(c);
var access = new CodexAccess(tools, c, watcher);
2023-09-11 14:57:57 +00:00
return factory.CreateOnlineCodexNode(access, this);
}
2023-04-13 07:33:10 +00:00
}
}