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

202 lines
7.2 KiB
C#
Raw Normal View History

2023-09-12 11:32:06 +00:00
using Core;
using FileUtils;
2023-09-19 09:51:59 +00:00
using GethPlugin;
2023-09-13 10:24:46 +00:00
using KubernetesWorkflow;
using KubernetesWorkflow.Types;
2024-03-26 10:39:59 +00:00
using Logging;
2023-09-13 09:59:21 +00:00
using MetricsPlugin;
2023-08-02 13:11:27 +00:00
using Utils;
2023-04-13 07:33:10 +00:00
namespace CodexPlugin
2023-04-13 07:33:10 +00:00
{
2023-09-19 09:51:59 +00:00
public interface ICodexNode : IHasContainer, IHasMetricsScrapeTarget, IHasEthAddress
2023-04-13 07:33:10 +00:00
{
string GetName();
2024-03-25 14:46:45 +00:00
DebugInfo GetDebugInfo();
DebugPeer GetDebugPeer(string peerId);
2023-09-12 11:32:06 +00:00
ContentId UploadFile(TrackedFile file);
TrackedFile? DownloadContent(ContentId contentId, string fileLabel = "");
2024-03-26 13:07:06 +00:00
LocalDatasetList LocalFiles();
2023-09-19 09:51:59 +00:00
void ConnectToPeer(ICodexNode node);
2024-03-26 09:03:52 +00:00
DebugInfoVersion Version { get; }
2023-09-20 06:45:55 +00:00
IMarketplaceAccess Marketplace { get; }
2023-09-20 10:55:09 +00:00
CrashWatcher CrashWatcher { get; }
PodInfo GetPodInfo();
2023-12-06 08:59:45 +00:00
ITransferSpeeds TransferSpeeds { get; }
void Stop(bool waitTillStopped);
2023-04-13 07:33:10 +00:00
}
2023-09-19 09:51:59 +00:00
public class CodexNode : ICodexNode
2023-04-13 07:33:10 +00:00
{
private const string UploadFailedMessage = "Unable to store block";
2023-09-12 09:43:46 +00:00
private readonly IPluginTools tools;
2023-09-20 08:13:29 +00:00
private readonly EthAddress? ethAddress;
2023-12-06 08:59:45 +00:00
private readonly TransferSpeeds transferSpeeds;
2023-04-13 07:33:10 +00:00
2023-09-20 08:13:29 +00:00
public CodexNode(IPluginTools tools, CodexAccess codexAccess, CodexNodeGroup group, IMarketplaceAccess marketplaceAccess, EthAddress? ethAddress)
2023-04-13 07:33:10 +00:00
{
2023-09-12 09:43:46 +00:00
this.tools = tools;
2023-09-19 09:51:59 +00:00
this.ethAddress = ethAddress;
2023-04-13 07:33:10 +00:00
CodexAccess = codexAccess;
Group = group;
2023-09-20 06:45:55 +00:00
Marketplace = marketplaceAccess;
2024-03-26 09:03:52 +00:00
Version = new DebugInfoVersion();
2023-12-06 08:59:45 +00:00
transferSpeeds = new TransferSpeeds();
2023-04-13 07:33:10 +00:00
}
2023-09-13 10:24:46 +00:00
public RunningContainer Container { get { return CodexAccess.Container; } }
2023-04-13 07:33:10 +00:00
public CodexAccess CodexAccess { get; }
2023-09-20 10:55:09 +00:00
public CrashWatcher CrashWatcher { get => CodexAccess.CrashWatcher; }
2023-04-13 07:33:10 +00:00
public CodexNodeGroup Group { get; }
2023-09-20 06:45:55 +00:00
public IMarketplaceAccess Marketplace { get; }
2024-03-26 09:03:52 +00:00
public DebugInfoVersion Version { get; private set; }
2023-12-06 08:59:45 +00:00
public ITransferSpeeds TransferSpeeds { get => transferSpeeds; }
2024-03-26 09:03:52 +00:00
2023-09-13 09:59:21 +00:00
public IMetricsScrapeTarget MetricsScrapeTarget
{
get
{
return new MetricsScrapeTarget(CodexAccess.Container, CodexContainerRecipe.MetricsPortTag);
2023-09-13 09:59:21 +00:00
}
}
2024-03-26 09:03:52 +00:00
2023-09-20 08:13:29 +00:00
public EthAddress EthAddress
2023-09-19 09:51:59 +00:00
{
get
{
if (ethAddress == null) throw new Exception("Marketplace is not enabled for this Codex node. Please start it with the option '.EnableMarketplace(...)' to enable it.");
return ethAddress;
}
}
2023-04-13 07:33:10 +00:00
public string GetName()
{
return CodexAccess.Container.Name;
2023-04-13 07:33:10 +00:00
}
2024-03-25 14:46:45 +00:00
public DebugInfo GetDebugInfo()
2023-04-13 07:33:10 +00:00
{
2023-06-29 14:07:49 +00:00
var debugInfo = CodexAccess.GetDebugInfo();
2024-03-26 09:03:52 +00:00
var known = string.Join(",", debugInfo.Table.Nodes.Select(n => n.PeerId));
Log($"Got DebugInfo with id: '{debugInfo.Id}'. This node knows: {known}");
2023-04-19 12:57:00 +00:00
return debugInfo;
2023-04-13 07:33:10 +00:00
}
2024-03-25 14:46:45 +00:00
public DebugPeer GetDebugPeer(string peerId)
2023-05-11 10:44:53 +00:00
{
2023-06-29 14:07:49 +00:00
return CodexAccess.GetDebugPeer(peerId);
2023-05-11 10:44:53 +00:00
}
2023-09-12 11:32:06 +00:00
public ContentId UploadFile(TrackedFile file)
2023-04-13 07:33:10 +00:00
{
2023-09-12 09:43:46 +00:00
using var fileStream = File.OpenRead(file.Filename);
2023-09-12 09:43:46 +00:00
var logMessage = $"Uploading file {file.Describe()}...";
Log(logMessage);
2023-12-06 08:59:45 +00:00
var measurement = Stopwatch.Measure(tools.GetLog(), logMessage, () =>
2023-09-12 09:43:46 +00:00
{
return CodexAccess.UploadFile(fileStream);
});
2023-12-06 08:59:45 +00:00
var response = measurement.Value;
transferSpeeds.AddUploadSample(file.GetFilesize(), measurement.Duration);
2023-09-20 08:51:47 +00:00
if (string.IsNullOrEmpty(response)) FrameworkAssert.Fail("Received empty response.");
if (response.StartsWith(UploadFailedMessage)) FrameworkAssert.Fail("Node failed to store block.");
2023-07-11 06:19:14 +00:00
2023-09-12 09:43:46 +00:00
Log($"Uploaded file. Received contentId: '{response}'.");
return new ContentId(response);
2023-04-13 07:33:10 +00:00
}
2023-09-12 11:32:06 +00:00
public TrackedFile? DownloadContent(ContentId contentId, string fileLabel = "")
2023-04-13 07:33:10 +00:00
{
2023-09-12 09:43:46 +00:00
var logMessage = $"Downloading for contentId: '{contentId.Id}'...";
Log(logMessage);
2023-09-13 08:03:11 +00:00
var file = tools.GetFileManager().CreateEmptyFile(fileLabel);
2023-12-06 08:59:45 +00:00
var measurement = Stopwatch.Measure(tools.GetLog(), logMessage, () => DownloadToFile(contentId.Id, file));
transferSpeeds.AddDownloadSample(file.GetFilesize(), measurement);
2023-09-12 09:43:46 +00:00
Log($"Downloaded file {file.Describe()} to '{file.Filename}'.");
return file;
2023-04-13 07:33:10 +00:00
}
2024-03-26 13:07:06 +00:00
public LocalDatasetList LocalFiles()
2023-11-10 07:20:08 +00:00
{
2024-03-26 10:39:59 +00:00
return CodexAccess.LocalFiles();
2023-11-10 07:20:08 +00:00
}
2023-09-19 09:51:59 +00:00
public void ConnectToPeer(ICodexNode node)
2023-04-13 07:33:10 +00:00
{
2023-09-19 09:51:59 +00:00
var peer = (CodexNode)node;
2023-04-13 07:33:10 +00:00
Log($"Connecting to peer {peer.GetName()}...");
var peerInfo = node.GetDebugInfo();
2024-03-26 11:14:02 +00:00
CodexAccess.ConnectToPeer(peerInfo.Id, GetPeerMultiAddresses(peer, peerInfo));
2023-04-13 07:33:10 +00:00
Log($"Successfully connected to peer {peer.GetName()}.");
}
public PodInfo GetPodInfo()
{
return CodexAccess.GetPodInfo();
}
public void Stop(bool waitTillStopped)
2023-04-25 10:52:11 +00:00
{
if (Group.Count() > 1) throw new InvalidOperationException("Codex-nodes that are part of a group cannot be " +
"individually shut down. Use 'BringOffline()' on the group object to stop the group. This method is only " +
"available for codex-nodes in groups of 1.");
Group.BringOffline(waitTillStopped);
2023-04-25 10:52:11 +00:00
}
2023-07-31 09:51:29 +00:00
public void EnsureOnlineGetVersionResponse()
{
2023-08-02 13:11:27 +00:00
var debugInfo = Time.Retry(CodexAccess.GetDebugInfo, "ensure online");
2024-03-26 09:03:52 +00:00
var nodePeerId = debugInfo.Id;
2023-07-31 09:51:29 +00:00
var nodeName = CodexAccess.Container.Name;
2024-03-26 09:03:52 +00:00
if (!debugInfo.Version.IsValid())
2023-07-31 09:51:29 +00:00
{
2024-03-26 09:03:52 +00:00
throw new Exception($"Invalid version information received from Codex node {GetName()}: {debugInfo.Version}");
2023-07-31 09:51:29 +00:00
}
var log = tools.GetLog();
log.AddStringReplace(nodePeerId, nodeName);
2024-03-26 09:03:52 +00:00
log.AddStringReplace(debugInfo.Table.LocalNode.NodeId, nodeName);
Version = debugInfo.Version;
2023-07-31 09:51:29 +00:00
}
2024-03-26 11:14:02 +00:00
private string[] GetPeerMultiAddresses(CodexNode peer, DebugInfo peerInfo)
2023-04-13 07:33:10 +00:00
{
// The peer we want to connect is in a different pod.
// We must replace the default IP with the pod IP in the multiAddress.
var workflow = tools.CreateWorkflow();
var podInfo = workflow.GetPodInfo(peer.Container);
2024-03-26 11:14:02 +00:00
return peerInfo.Addrs.Select(a => a
.Replace("0.0.0.0", podInfo.Ip))
.ToArray();
2023-04-13 07:33:10 +00:00
}
2023-09-12 11:32:06 +00:00
private void DownloadToFile(string contentId, TrackedFile file)
2023-04-13 07:33:10 +00:00
{
using var fileStream = File.OpenWrite(file.Filename);
2023-04-19 12:57:00 +00:00
try
{
2023-06-29 14:07:49 +00:00
using var downloadStream = CodexAccess.DownloadFile(contentId);
2023-04-19 12:57:00 +00:00
downloadStream.CopyTo(fileStream);
}
catch
{
Log($"Failed to download file '{contentId}'.");
throw;
}
2023-04-13 07:33:10 +00:00
}
private void Log(string msg)
{
2023-09-12 09:43:46 +00:00
tools.GetLog().Log($"{GetName()}: {msg}");
2023-04-13 07:33:10 +00:00
}
}
}