From 3b8475de3e9cfc63bea39aab23c3b816b9d8456d Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Fri, 7 Jan 2022 14:28:21 -0500 Subject: [PATCH] Skip adding line to console line queue if the queue is backed up. (#1592) * Skip adding line to console line queue if the queue is backed up. * . --- src/Runner.Common/JobServerQueue.cs | 30 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Runner.Common/JobServerQueue.cs b/src/Runner.Common/JobServerQueue.cs index 9ea0bf41f..eb8fa9e67 100644 --- a/src/Runner.Common/JobServerQueue.cs +++ b/src/Runner.Common/JobServerQueue.cs @@ -1,14 +1,13 @@ -using GitHub.DistributedTask.WebApi; -using GitHub.Runner.Common.Util; using System; -using System.Collections.Generic; using System.Collections.Concurrent; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Pipelines = GitHub.DistributedTask.Pipelines; +using GitHub.DistributedTask.WebApi; using GitHub.Runner.Sdk; +using Pipelines = GitHub.DistributedTask.Pipelines; namespace GitHub.Runner.Common { @@ -76,6 +75,7 @@ namespace GitHub.Runner.Common // at the same time we can cut the load to server after the build run for more than 60s private int _webConsoleLineAggressiveDequeueCount = 0; private const int _webConsoleLineAggressiveDequeueLimit = 4 * 60; + private const int _webConsoleLineQueueSizeLimit = 1024; private bool _webConsoleLineAggressiveDequeue = true; private bool _firstConsoleOutputs = true; @@ -161,8 +161,20 @@ namespace GitHub.Runner.Common public void QueueWebConsoleLine(Guid stepRecordId, string line, long? lineNumber) { - Trace.Verbose("Enqueue web console line queue: {0}", line); - _webConsoleLineQueue.Enqueue(new ConsoleLineInfo(stepRecordId, line, lineNumber)); + // We only process 500 lines of the queue everytime. + // If the queue is backing up due to slow Http request or flood of output from step, + // we will drop the output to avoid extra memory consumption from the runner since the live console feed is best effort. + if (!string.IsNullOrEmpty(line) && _webConsoleLineQueue.Count < _webConsoleLineQueueSizeLimit) + { + Trace.Verbose("Enqueue web console line queue: {0}", line); + if (line.Length > 1024) + { + Trace.Verbose("Web console line is more than 1024 chars, truncate to first 1024 chars"); + line = $"{line.Substring(0, 1024)}..."; + } + + _webConsoleLineQueue.Enqueue(new ConsoleLineInfo(stepRecordId, line, lineNumber)); + } } public void QueueFileUpload(Guid timelineId, Guid timelineRecordId, string type, string name, string path, bool deleteSource) @@ -230,12 +242,6 @@ namespace GitHub.Runner.Common stepRecordIds.Add(lineInfo.StepRecordId); } - if (!string.IsNullOrEmpty(lineInfo.Line) && lineInfo.Line.Length > 1024) - { - Trace.Verbose("Web console line is more than 1024 chars, truncate to first 1024 chars"); - lineInfo.Line = $"{lineInfo.Line.Substring(0, 1024)}..."; - } - stepsConsoleLines[lineInfo.StepRecordId].Add(new TimelineRecordLogLine(lineInfo.Line, lineInfo.LineNumber)); linesCounter++;