From e43fdbdc988d8aa79c0ae69c1bf67a865653aad3 Mon Sep 17 00:00:00 2001 From: eeSquared Date: Thu, 21 Mar 2024 21:16:59 +0000 Subject: [PATCH 1/2] Add SessionConflict return code --- src/Agent.Listener/Agent.cs | 5 +++-- src/Agent.Listener/MessageListener.cs | 14 +++++++++----- .../Constants.cs | 1 + src/Test/L0/Listener/AgentL0.cs | 16 ++++++++-------- src/Test/L0/Listener/MessageListenerL0.cs | 12 ++++++------ 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Agent.Listener/Agent.cs b/src/Agent.Listener/Agent.cs index 34bb691944..5b2cc25cb2 100644 --- a/src/Agent.Listener/Agent.cs +++ b/src/Agent.Listener/Agent.cs @@ -327,9 +327,10 @@ private async Task RunAsync(AgentSettings settings, bool runOnce = false) { Trace.Info(nameof(RunAsync)); _listener = HostContext.GetService(); - if (!await _listener.CreateSessionAsync(HostContext.AgentShutdownToken)) + int returnCode = await _listener.CreateSessionAsync(HostContext.AgentShutdownToken); + if (returnCode != Constants.Agent.ReturnCode.Success) { - return Constants.Agent.ReturnCode.TerminatedError; + return returnCode; } HostContext.WritePerfCounter("SessionCreated"); diff --git a/src/Agent.Listener/MessageListener.cs b/src/Agent.Listener/MessageListener.cs index eebca9d1f7..c8bae56440 100644 --- a/src/Agent.Listener/MessageListener.cs +++ b/src/Agent.Listener/MessageListener.cs @@ -24,7 +24,7 @@ namespace Microsoft.VisualStudio.Services.Agent.Listener [ServiceLocator(Default = typeof(MessageListener))] public interface IMessageListener : IAgentService { - Task CreateSessionAsync(CancellationToken token); + Task CreateSessionAsync(CancellationToken token); Task DeleteSessionAsync(); Task GetNextMessageAsync(CancellationToken token); Task KeepAlive(CancellationToken token); @@ -52,7 +52,7 @@ public override void Initialize(IHostContext hostContext) _agentServer = HostContext.GetService(); } - public async Task CreateSessionAsync(CancellationToken token) + public async Task CreateSessionAsync(CancellationToken token) { Trace.Entering(); @@ -108,7 +108,7 @@ public async Task CreateSessionAsync(CancellationToken token) encounteringError = false; } - return true; + return Constants.Agent.ReturnCode.Success; } catch (OperationCanceledException) when (token.IsCancellationRequested) { @@ -133,7 +133,11 @@ public async Task CreateSessionAsync(CancellationToken token) if (!IsSessionCreationExceptionRetriable(ex)) { _term.WriteError(StringUtil.Loc("SessionCreateFailed", ex.Message)); - return false; + if (ex is TaskAgentSessionConflictException) + { + return Constants.Agent.ReturnCode.SessionConflict; + } + return Constants.Agent.ReturnCode.TerminatedError; } if (!encounteringError) //print the message only on the first error @@ -211,7 +215,7 @@ public async Task GetNextMessageAsync(CancellationToken token) Trace.Error(ex); // don't retry if SkipSessionRecover = true, DT service will delete agent session to stop agent from taking more jobs. - if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && await CreateSessionAsync(token)) + if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == Constants.Agent.ReturnCode.Success)) { Trace.Info($"{nameof(TaskAgentSessionExpiredException)} received, recovered by recreate session."); } diff --git a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs index 2b0f630ae8..8a63242dae 100644 --- a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs +++ b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs @@ -209,6 +209,7 @@ public static class ReturnCode public const int RetryableError = 2; public const int AgentUpdating = 3; public const int RunOnceAgentUpdating = 4; + public const int SessionConflict = 5; } public static class AgentConfigurationProvider diff --git a/src/Test/L0/Listener/AgentL0.cs b/src/Test/L0/Listener/AgentL0.cs index 14250ab78d..2fc37e0a4c 100644 --- a/src/Test/L0/Listener/AgentL0.cs +++ b/src/Test/L0/Listener/AgentL0.cs @@ -107,7 +107,7 @@ public async void TestRunAsync() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -208,7 +208,7 @@ public async void TestExecuteCommandForRunAsService(string[] args, bool configur _configStore.Setup(x => x.IsServiceConfigured()).Returns(configureAsService); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -245,7 +245,7 @@ public async void TestMachineProvisionerCLI() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -282,7 +282,7 @@ public async void TestMachineProvisionerCLICompat() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(false)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -330,7 +330,7 @@ public async void TestRunOnce() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -435,7 +435,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -537,7 +537,7 @@ public async void TestRunOnceHandleUpdateMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -766,7 +766,7 @@ public async void TestMetadataUpdate() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(true)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { diff --git a/src/Test/L0/Listener/MessageListenerL0.cs b/src/Test/L0/Listener/MessageListenerL0.cs index 45a24d7b7e..5359c915d5 100644 --- a/src/Test/L0/Listener/MessageListenerL0.cs +++ b/src/Test/L0/Listener/MessageListenerL0.cs @@ -88,11 +88,11 @@ public async void CreatesSession() MessageListener listener = new MessageListener(); listener.Initialize(tc); - bool result = await listener.CreateSessionAsync(tokenSource.Token); + int result = await listener.CreateSessionAsync(tokenSource.Token); trace.Info("result: {0}", result); // Assert. - Assert.True(result); + Assert.Equal(Constants.Agent.ReturnCode.Success, result); _agentServer .Verify(x => x.CreateAgentSessionAsync( _settings.PoolId, @@ -132,8 +132,8 @@ public async void DeleteSession() MessageListener listener = new MessageListener(); listener.Initialize(tc); - bool result = await listener.CreateSessionAsync(tokenSource.Token); - Assert.True(result); + int result = await listener.CreateSessionAsync(tokenSource.Token); + Assert.Equal(Constants.Agent.ReturnCode.Success, result); _agentServer .Setup(x => x.DeleteAgentSessionAsync( @@ -179,8 +179,8 @@ public async void GetNextMessage() MessageListener listener = new MessageListener(); listener.Initialize(tc); - bool result = await listener.CreateSessionAsync(tokenSource.Token); - Assert.True(result); + int result = await listener.CreateSessionAsync(tokenSource.Token); + Assert.Equal(Constants.Agent.ReturnCode.Success, result); var arMessages = new TaskAgentMessage[] { From 131198b30e6006a454d6957ac860c00a3cca1ca8 Mon Sep 17 00:00:00 2001 From: eeSquared Date: Tue, 26 Mar 2024 21:52:17 +0000 Subject: [PATCH 2/2] Add enum to return for CreateSessionAsync --- src/Agent.Listener/Agent.cs | 10 +++++++--- src/Agent.Listener/MessageListener.cs | 12 ++++++------ .../Constants.cs | 7 +++++++ src/Test/L0/Listener/AgentL0.cs | 16 ++++++++-------- src/Test/L0/Listener/MessageListenerL0.cs | 12 ++++++------ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/Agent.Listener/Agent.cs b/src/Agent.Listener/Agent.cs index 5b2cc25cb2..bc10f46e44 100644 --- a/src/Agent.Listener/Agent.cs +++ b/src/Agent.Listener/Agent.cs @@ -327,10 +327,14 @@ private async Task RunAsync(AgentSettings settings, bool runOnce = false) { Trace.Info(nameof(RunAsync)); _listener = HostContext.GetService(); - int returnCode = await _listener.CreateSessionAsync(HostContext.AgentShutdownToken); - if (returnCode != Constants.Agent.ReturnCode.Success) + Constants.Agent.CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.AgentShutdownToken); + if (createSessionResult == Constants.Agent.CreateSessionResult.SessionConflict) { - return returnCode; + return Constants.Agent.ReturnCode.SessionConflict; + } + else if (createSessionResult == Constants.Agent.CreateSessionResult.Failure) + { + return Constants.Agent.ReturnCode.TerminatedError; } HostContext.WritePerfCounter("SessionCreated"); diff --git a/src/Agent.Listener/MessageListener.cs b/src/Agent.Listener/MessageListener.cs index c8bae56440..2b4dbfce0e 100644 --- a/src/Agent.Listener/MessageListener.cs +++ b/src/Agent.Listener/MessageListener.cs @@ -24,7 +24,7 @@ namespace Microsoft.VisualStudio.Services.Agent.Listener [ServiceLocator(Default = typeof(MessageListener))] public interface IMessageListener : IAgentService { - Task CreateSessionAsync(CancellationToken token); + Task CreateSessionAsync(CancellationToken token); Task DeleteSessionAsync(); Task GetNextMessageAsync(CancellationToken token); Task KeepAlive(CancellationToken token); @@ -52,7 +52,7 @@ public override void Initialize(IHostContext hostContext) _agentServer = HostContext.GetService(); } - public async Task CreateSessionAsync(CancellationToken token) + public async Task CreateSessionAsync(CancellationToken token) { Trace.Entering(); @@ -108,7 +108,7 @@ public async Task CreateSessionAsync(CancellationToken token) encounteringError = false; } - return Constants.Agent.ReturnCode.Success; + return Constants.Agent.CreateSessionResult.Success; } catch (OperationCanceledException) when (token.IsCancellationRequested) { @@ -135,9 +135,9 @@ public async Task CreateSessionAsync(CancellationToken token) _term.WriteError(StringUtil.Loc("SessionCreateFailed", ex.Message)); if (ex is TaskAgentSessionConflictException) { - return Constants.Agent.ReturnCode.SessionConflict; + return Constants.Agent.CreateSessionResult.SessionConflict; } - return Constants.Agent.ReturnCode.TerminatedError; + return Constants.Agent.CreateSessionResult.Failure; } if (!encounteringError) //print the message only on the first error @@ -215,7 +215,7 @@ public async Task GetNextMessageAsync(CancellationToken token) Trace.Error(ex); // don't retry if SkipSessionRecover = true, DT service will delete agent session to stop agent from taking more jobs. - if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == Constants.Agent.ReturnCode.Success)) + if (ex is TaskAgentSessionExpiredException && !_settings.SkipSessionRecover && (await CreateSessionAsync(token) == Constants.Agent.CreateSessionResult.Success)) { Trace.Info($"{nameof(TaskAgentSessionExpiredException)} received, recovered by recreate session."); } diff --git a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs index 8a63242dae..143ecd8e4f 100644 --- a/src/Microsoft.VisualStudio.Services.Agent/Constants.cs +++ b/src/Microsoft.VisualStudio.Services.Agent/Constants.cs @@ -202,6 +202,13 @@ public static class Flags } } + public enum CreateSessionResult + { + Success, + Failure, + SessionConflict + } + public static class ReturnCode { public const int Success = 0; diff --git a/src/Test/L0/Listener/AgentL0.cs b/src/Test/L0/Listener/AgentL0.cs index 2fc37e0a4c..a74c710fdf 100644 --- a/src/Test/L0/Listener/AgentL0.cs +++ b/src/Test/L0/Listener/AgentL0.cs @@ -107,7 +107,7 @@ public async void TestRunAsync() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); + .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -208,7 +208,7 @@ public async void TestExecuteCommandForRunAsService(string[] args, bool configur _configStore.Setup(x => x.IsServiceConfigured()).Returns(configureAsService); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -245,7 +245,7 @@ public async void TestMachineProvisionerCLI() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -282,7 +282,7 @@ public async void TestMachineProvisionerCLICompat() .Returns(false); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.TerminatedError)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Failure)); agent.Initialize(hc); await agent.ExecuteCommand(command); @@ -330,7 +330,7 @@ public async void TestRunOnce() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -435,7 +435,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -537,7 +537,7 @@ public async void TestRunOnceHandleUpdateMessage() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { @@ -766,7 +766,7 @@ public async void TestMetadataUpdate() _configurationManager.Setup(x => x.IsConfigured()) .Returns(true); _messageListener.Setup(x => x.CreateSessionAsync(It.IsAny())) - .Returns(Task.FromResult(Constants.Agent.ReturnCode.Success)); + .Returns(Task.FromResult(Constants.Agent.CreateSessionResult.Success)); _messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny())) .Returns(async () => { diff --git a/src/Test/L0/Listener/MessageListenerL0.cs b/src/Test/L0/Listener/MessageListenerL0.cs index 5359c915d5..1674f268d9 100644 --- a/src/Test/L0/Listener/MessageListenerL0.cs +++ b/src/Test/L0/Listener/MessageListenerL0.cs @@ -88,11 +88,11 @@ public async void CreatesSession() MessageListener listener = new MessageListener(); listener.Initialize(tc); - int result = await listener.CreateSessionAsync(tokenSource.Token); + Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token); trace.Info("result: {0}", result); // Assert. - Assert.Equal(Constants.Agent.ReturnCode.Success, result); + Assert.Equal(Constants.Agent.CreateSessionResult.Success, result); _agentServer .Verify(x => x.CreateAgentSessionAsync( _settings.PoolId, @@ -132,8 +132,8 @@ public async void DeleteSession() MessageListener listener = new MessageListener(); listener.Initialize(tc); - int result = await listener.CreateSessionAsync(tokenSource.Token); - Assert.Equal(Constants.Agent.ReturnCode.Success, result); + Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token); + Assert.Equal(Constants.Agent.CreateSessionResult.Success, result); _agentServer .Setup(x => x.DeleteAgentSessionAsync( @@ -179,8 +179,8 @@ public async void GetNextMessage() MessageListener listener = new MessageListener(); listener.Initialize(tc); - int result = await listener.CreateSessionAsync(tokenSource.Token); - Assert.Equal(Constants.Agent.ReturnCode.Success, result); + Constants.Agent.CreateSessionResult result = await listener.CreateSessionAsync(tokenSource.Token); + Assert.Equal(Constants.Agent.CreateSessionResult.Success, result); var arMessages = new TaskAgentMessage[] {