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

93 lines
3.1 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
{
void BringOffline(bool waitTillStopped);
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;
2024-04-13 14:09:17 +00:00
public CodexNodeGroup(CodexStarter starter, IPluginTools tools, RunningPod[] containers, ICodexNodeFactory codexNodeFactory)
2023-04-13 07:33:10 +00:00
{
this.starter = starter;
2023-04-13 07:33:10 +00:00
Containers = containers;
2024-04-13 14:09:17 +00:00
Nodes = containers.Select(c => CreateOnlineCodexNode(c, tools, codexNodeFactory)).ToArray();
2024-03-26 09:03:52 +00:00
Version = new DebugInfoVersion();
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];
}
}
public void BringOffline(bool waitTillStopped)
2023-04-13 09:07:36 +00:00
{
starter.BringOffline(this, waitTillStopped);
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
2024-04-13 14:09:17 +00:00
public void Stop(CodexNode node, bool waitTillStopped)
{
starter.Stop(node.Pod, waitTillStopped);
Nodes = Nodes.Where(n => n != node).ToArray();
Containers = Containers.Where(c => c != node.Pod).ToArray();
}
public RunningPod[] Containers { get; private set; }
2023-09-19 09:51:59 +00:00
public CodexNode[] Nodes { get; private set; }
2024-03-26 09:03:52 +00:00
public DebugInfoVersion 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();
2024-03-26 07:58:16 +00:00
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;
foreach (var node in Nodes) node.Initialize();
2023-04-13 07:33:10 +00:00
}
2024-04-13 14:09:17 +00:00
private CodexNode CreateOnlineCodexNode(RunningPod c, IPluginTools tools, ICodexNodeFactory factory)
{
2024-04-13 14:09:17 +00:00
var watcher = factory.CreateCrashWatcher(c.Containers.Single());
2023-09-20 10:55:09 +00:00
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
}
}