diff --git a/ProjectPlugins/CodexClient/CodexAccess.cs b/ProjectPlugins/CodexClient/CodexAccess.cs index 9f0c22c7..137ebbc3 100644 --- a/ProjectPlugins/CodexClient/CodexAccess.cs +++ b/ProjectPlugins/CodexClient/CodexAccess.cs @@ -32,7 +32,7 @@ namespace CodexClient public IDownloadedLog DownloadLog(string additionalName = "") { var file = log.CreateSubfile(GetName() + additionalName); - Log($"Downloading logs for '{GetName()}' to '{file.Filename}'"); + Log($"Downloading logs to '{file.Filename}'"); return processControl.DownloadLog(file); } @@ -282,7 +282,7 @@ namespace CodexClient private void Log(string msg) { - log.Log($"{GetName()} {msg}"); + log.Log($"({GetName()}) {msg}"); } } diff --git a/ProjectPlugins/CodexPlugin/BinaryCodexStarter.cs b/ProjectPlugins/CodexPlugin/BinaryCodexStarter.cs index 7f3c9d00..617cb701 100644 --- a/ProjectPlugins/CodexPlugin/BinaryCodexStarter.cs +++ b/ProjectPlugins/CodexPlugin/BinaryCodexStarter.cs @@ -59,7 +59,7 @@ namespace CodexPlugin private ICodexInstance StartBinary(CodexStartupConfig config) { - var name = "codex_" + numberSource.GetNextNumber(); + var name = GetName(config); var dataDir = Path.Combine(dataParentDir, $"datadir_{numberSource.GetNextNumber()}"); var pconfig = new CodexProcessConfig(name, freePortFinder, dataDir); Log(pconfig); @@ -93,12 +93,21 @@ namespace CodexPlugin metricsEndpoint: null ); - var pc = new BinaryProcessControl(process, pconfig); + var pc = new BinaryProcessControl(pluginTools.GetLog(), process, pconfig); processControlMap.Add(instance, pc); return instance; } + private string GetName(CodexStartupConfig config) + { + if (!string.IsNullOrEmpty(config.NameOverride)) + { + return config.NameOverride + "_" + numberSource.GetNextNumber(); + } + return "codex_" + numberSource.GetNextNumber(); + } + private void LogSeparator() { Log("----------------------------------------------------------------------------"); diff --git a/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs b/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs index 63128977..de8a75c2 100644 --- a/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs +++ b/ProjectPlugins/CodexPlugin/BinaryProcessControl.cs @@ -6,19 +6,24 @@ namespace CodexPlugin { public class BinaryProcessControl : IProcessControl { + private readonly LogFile logFile; private readonly Process process; private readonly CodexProcessConfig config; - private readonly List logLines = new List(); + private readonly List logBuffer = new List(); + private readonly object bufferLock = new object(); private readonly List streamTasks = new List(); private bool running; - public BinaryProcessControl(Process process, CodexProcessConfig config) + public BinaryProcessControl(ILog log, Process process, CodexProcessConfig config) { + logFile = log.CreateSubfile(config.Name); + running = true; this.process = process; this.config = config; streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardOutput))); streamTasks.Add(Task.Run(() => ReadProcessStream(process.StandardError))); + streamTasks.Add(Task.Run(() => WriteLog())); } private void ReadProcessStream(StreamReader reader) @@ -26,7 +31,32 @@ namespace CodexPlugin while (running) { var line = reader.ReadLine(); - if (!string.IsNullOrEmpty(line)) logLines.Add(line); + if (!string.IsNullOrEmpty(line)) + { + lock (bufferLock) + { + logBuffer.Add(line); + } + } + } + } + + private void WriteLog() + { + while (running || logBuffer.Count > 0) + { + if (logBuffer.Count > 0) + { + var lines = Array.Empty(); + lock (bufferLock) + { + lines = logBuffer.ToArray(); + logBuffer.Clear(); + } + + foreach (var l in lines) logFile.WriteRaw(l); + } + else Thread.Sleep(100); } } @@ -38,8 +68,7 @@ namespace CodexPlugin public IDownloadedLog DownloadLog(LogFile file) { - foreach (var line in logLines) file.WriteRaw(line); - return new DownloadedLog(file, config.Name); + return new DownloadedLog(logFile, config.Name); } public bool HasCrashed() diff --git a/Tests/ExperimentalTests/DownloadConnectivityTests/DatalayerReliabilityTests.cs b/Tests/ExperimentalTests/DownloadConnectivityTests/DatalayerReliabilityTests.cs index 7ffc2d81..ea501e64 100644 --- a/Tests/ExperimentalTests/DownloadConnectivityTests/DatalayerReliabilityTests.cs +++ b/Tests/ExperimentalTests/DownloadConnectivityTests/DatalayerReliabilityTests.cs @@ -14,15 +14,15 @@ namespace ExperimentalTests.DownloadConnectivityTests [Test] [Combinatorial] public void SingleSetTest( - [Values(1, 10, 100, 1000)] int fileSizeMb, + [Values(10, 100, 1000)] int fileSizeMb, [Values(5, 10, 20, 30)] int numDownloaders ) { var file = GenerateTestFile(fileSizeMb.MB()); - var uploaders = StartCodex(n => n.WithName("uploader")); + var uploader = StartCodex(n => n.WithName("uploader")); var downloaders = StartCodex(numDownloaders, n => n.WithName("downloader")); - var cid = uploaders.UploadFile(file); + var cid = uploader.UploadFile(file); var downloadTasks = new List(); foreach (var dl in downloaders)