From 3865944b155e4947bffc8b1bd39823a532d2526c Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:15:53 +0100 Subject: [PATCH 01/12] handle polly timeout rejected exception --- RelationalAI/Client.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/RelationalAI/Client.cs b/RelationalAI/Client.cs index ee0d042..6010d0e 100644 --- a/RelationalAI/Client.cs +++ b/RelationalAI/Client.cs @@ -24,6 +24,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Polly; +using Polly.Timeout; using Relationalai.Protocol; namespace RelationalAI @@ -157,17 +158,24 @@ public async Task CreateEngineWaitAsync(string engine, string size = "XS { var startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); await CreateEngineAsync(engine, size); - var resp = await Policy + try + { + var resp = await Policy .HandleResult(e => !EngineStates.IsTerminalState(e.State, EngineStates.Provisioned)) .RetryWithTimeout(startTime, 0.1, 120, 10 * 60) .ExecuteAsync(() => GetEngineAsync(engine)); - if (resp.State != EngineStates.Provisioned) + if (resp.State != EngineStates.Provisioned) + { + throw new EngineProvisionFailedException(resp); + } + + return resp; + } catch (TimeoutRejectedException ex) { - throw new EngineProvisionFailedException(resp); + _logger.LogWarning(ex, "Timeout occured when creating engine {engine}.", engine); + throw new EngineProvisionFailedException(await GetEngineAsync(engine)); } - - return resp; } public async Task GetEngineAsync(string engine) From ee0406a773ea1fbb149fe16f176b769c7da4c248 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:17:11 +0100 Subject: [PATCH 02/12] fix linter --- RelationalAI/Client.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RelationalAI/Client.cs b/RelationalAI/Client.cs index 6010d0e..5e0f9a3 100644 --- a/RelationalAI/Client.cs +++ b/RelationalAI/Client.cs @@ -171,7 +171,8 @@ public async Task CreateEngineWaitAsync(string engine, string size = "XS } return resp; - } catch (TimeoutRejectedException ex) + } + catch (TimeoutRejectedException ex) { _logger.LogWarning(ex, "Timeout occured when creating engine {engine}.", engine); throw new EngineProvisionFailedException(await GetEngineAsync(engine)); From 6ad3f97538b1c7709c3efb79c1306dd9b4c0421d Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:23:19 +0100 Subject: [PATCH 03/12] simulate timeout --- RelationalAI/Client.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RelationalAI/Client.cs b/RelationalAI/Client.cs index 5e0f9a3..7db2a61 100644 --- a/RelationalAI/Client.cs +++ b/RelationalAI/Client.cs @@ -162,7 +162,7 @@ public async Task CreateEngineWaitAsync(string engine, string size = "XS { var resp = await Policy .HandleResult(e => !EngineStates.IsTerminalState(e.State, EngineStates.Provisioned)) - .RetryWithTimeout(startTime, 0.1, 120, 10 * 60) + .RetryWithTimeout(startTime, 0.1, 120, 1) .ExecuteAsync(() => GetEngineAsync(engine)); if (resp.State != EngineStates.Provisioned) From e734baad39ca74528c31909ad5a6120f58077d9d Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:24:48 +0100 Subject: [PATCH 04/12] revert --- RelationalAI/Client.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RelationalAI/Client.cs b/RelationalAI/Client.cs index 7db2a61..5e0f9a3 100644 --- a/RelationalAI/Client.cs +++ b/RelationalAI/Client.cs @@ -162,7 +162,7 @@ public async Task CreateEngineWaitAsync(string engine, string size = "XS { var resp = await Policy .HandleResult(e => !EngineStates.IsTerminalState(e.State, EngineStates.Provisioned)) - .RetryWithTimeout(startTime, 0.1, 120, 1) + .RetryWithTimeout(startTime, 0.1, 120, 10 * 60) .ExecuteAsync(() => GetEngineAsync(engine)); if (resp.State != EngineStates.Provisioned) From 27a490902b04a6f8d8f273073de5cc509f6dbd23 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:52:52 +0100 Subject: [PATCH 05/12] handle if engine already exists --- RelationalAI.Test/TestConfig.cs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/RelationalAI.Test/TestConfig.cs b/RelationalAI.Test/TestConfig.cs index 450bc87..23d676a 100644 --- a/RelationalAI.Test/TestConfig.cs +++ b/RelationalAI.Test/TestConfig.cs @@ -13,7 +13,6 @@ namespace RelationalAI.Test { public class EngineFixture : IDisposable { - private Engine _engine; private readonly string engineName = "sdk-csharp-" + Guid.NewGuid().ToString(); // Semaphore is used to lock the CreateEngine function so that only one test creates the engine. private static readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1); @@ -37,9 +36,26 @@ public async Task CreateEngineWaitAsync(Client client) try { await semaphoreSlim.WaitAsync(); - if (_engine == null) + try { - _engine = await client.CreateEngineWaitAsync(engineName); + Engine = await client.GetEngineAsync(engineName); + if (Engine.State != "PROVISIONED") + { + throw new EngineProvisionFailedException(Engine); + } + + return Engine; + } catch(HttpError ex) + { + if (ex.StatusCode != 404) + { + throw ex; + } + } + + if (Engine == null) + { + Engine = await client.CreateEngineWaitAsync(engineName); } } finally @@ -48,16 +64,10 @@ public async Task CreateEngineWaitAsync(Client client) } - return _engine; + return Engine; } - public Engine Engine - { - get - { - return _engine; - } - } + public Engine Engine { get; private set; } } [CollectionDefinition("RelationalAI.Test")] From 4ad41bc7aa48206c6c883844d3a4187242713671 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Thu, 11 May 2023 13:54:19 +0100 Subject: [PATCH 06/12] fix linter --- RelationalAI.Test/TestConfig.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RelationalAI.Test/TestConfig.cs b/RelationalAI.Test/TestConfig.cs index 23d676a..7492782 100644 --- a/RelationalAI.Test/TestConfig.cs +++ b/RelationalAI.Test/TestConfig.cs @@ -45,7 +45,8 @@ public async Task CreateEngineWaitAsync(Client client) } return Engine; - } catch(HttpError ex) + } + catch (HttpError ex) { if (ex.StatusCode != 404) { From 16f7560b0781ce504a89c504909f4b23ab19cab0 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 15:19:21 +0100 Subject: [PATCH 07/12] Introducing EngineProvisionTimeoutException exception --- CHANGELOG.md | 4 +++ RelationalAI/Client.cs | 7 ++--- .../EngineProvisionFailedException.cs | 4 +++ .../EngineProvisionTimeoutException.cs | 28 +++++++++++++++++++ RelationalAI/RelationalAI.csproj | 2 +- 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 RelationalAI/EngineProvisionTimeoutException.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9979d8a..a91b540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# v0.9.14-alpha +* Fix failing tests with `engine already exists`. +* Add `EngineProvisionTimeoutException` thrown from `CreateEngineWaitAsync` when requested engine timeout to provision. + # v0.9.13-alpha * Add `IAccessTokenHandler` interface to implement custom access Auth0 token handlers. * `Client` class consturctor extended to accept custom access token handlers implementations. diff --git a/RelationalAI/Client.cs b/RelationalAI/Client.cs index 5e0f9a3..cad430a 100644 --- a/RelationalAI/Client.cs +++ b/RelationalAI/Client.cs @@ -167,15 +167,14 @@ public async Task CreateEngineWaitAsync(string engine, string size = "XS if (resp.State != EngineStates.Provisioned) { - throw new EngineProvisionFailedException(resp); + throw new EngineProvisionFailedException(engine); } return resp; } - catch (TimeoutRejectedException ex) + catch (TimeoutRejectedException) { - _logger.LogWarning(ex, "Timeout occured when creating engine {engine}.", engine); - throw new EngineProvisionFailedException(await GetEngineAsync(engine)); + throw new EngineProvisionTimeoutException(engine); } } diff --git a/RelationalAI/EngineProvisionFailedException.cs b/RelationalAI/EngineProvisionFailedException.cs index a3f4d49..f06e593 100644 --- a/RelationalAI/EngineProvisionFailedException.cs +++ b/RelationalAI/EngineProvisionFailedException.cs @@ -29,6 +29,10 @@ public EngineProvisionFailedException(Engine engine) Engine = engine; } + public EngineProvisionFailedException(string engine) + : base($"Engine with name `engine` failed to provision") + { } + /// /// Gets the name of the engine that failed to provision. /// diff --git a/RelationalAI/EngineProvisionTimeoutException.cs b/RelationalAI/EngineProvisionTimeoutException.cs new file mode 100644 index 0000000..e1a74f4 --- /dev/null +++ b/RelationalAI/EngineProvisionTimeoutException.cs @@ -0,0 +1,28 @@ +/* + * Copyright 2022 RelationalAI, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace RelationalAI +{ + /// + /// Represents error thrown when engine requested to provision timeout. + /// + public class EngineProvisionTimeoutException : EngineProvisionFailedException + { + public EngineProvisionTimeoutException(string engine) + : base($"Engine with name `engine` failed to provision") + { } + } +} diff --git a/RelationalAI/RelationalAI.csproj b/RelationalAI/RelationalAI.csproj index 66e2f4b..f22dbe2 100644 --- a/RelationalAI/RelationalAI.csproj +++ b/RelationalAI/RelationalAI.csproj @@ -1,7 +1,7 @@  - 0.9.13-alpha + 0.9.14-alpha netcoreapp3.1 RAI RelationalAI From 2963d6126d0fdc32a4a8b1446bd262d8f6b53d39 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 15:29:20 +0100 Subject: [PATCH 08/12] fix string interpolation for engine name --- RelationalAI/EngineProvisionTimeoutException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RelationalAI/EngineProvisionTimeoutException.cs b/RelationalAI/EngineProvisionTimeoutException.cs index e1a74f4..1c1384d 100644 --- a/RelationalAI/EngineProvisionTimeoutException.cs +++ b/RelationalAI/EngineProvisionTimeoutException.cs @@ -22,7 +22,7 @@ namespace RelationalAI public class EngineProvisionTimeoutException : EngineProvisionFailedException { public EngineProvisionTimeoutException(string engine) - : base($"Engine with name `engine` failed to provision") + : base($"Engine with name {engine} failed to provision") { } } } From 917140cf02f97000659d79d097d63087990b8e5c Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 15:30:26 +0100 Subject: [PATCH 09/12] fix --- RelationalAI/EngineProvisionFailedException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RelationalAI/EngineProvisionFailedException.cs b/RelationalAI/EngineProvisionFailedException.cs index f06e593..b291947 100644 --- a/RelationalAI/EngineProvisionFailedException.cs +++ b/RelationalAI/EngineProvisionFailedException.cs @@ -30,7 +30,7 @@ public EngineProvisionFailedException(Engine engine) } public EngineProvisionFailedException(string engine) - : base($"Engine with name `engine` failed to provision") + : base($"Engine with name {engine} failed to provision") { } /// From e71c8f787581efb8a85169965cd16312df93309c Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 15:41:27 +0100 Subject: [PATCH 10/12] fix code style warnings --- RelationalAI/EngineProvisionFailedException.cs | 5 +++-- RelationalAI/EngineProvisionTimeoutException.cs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/RelationalAI/EngineProvisionFailedException.cs b/RelationalAI/EngineProvisionFailedException.cs index b291947..f799b43 100644 --- a/RelationalAI/EngineProvisionFailedException.cs +++ b/RelationalAI/EngineProvisionFailedException.cs @@ -30,8 +30,9 @@ public EngineProvisionFailedException(Engine engine) } public EngineProvisionFailedException(string engine) - : base($"Engine with name {engine} failed to provision") - { } + : base($"Engine with name `{engine}` failed to provision") + { + } /// /// Gets the name of the engine that failed to provision. diff --git a/RelationalAI/EngineProvisionTimeoutException.cs b/RelationalAI/EngineProvisionTimeoutException.cs index 1c1384d..56e9f38 100644 --- a/RelationalAI/EngineProvisionTimeoutException.cs +++ b/RelationalAI/EngineProvisionTimeoutException.cs @@ -22,7 +22,8 @@ namespace RelationalAI public class EngineProvisionTimeoutException : EngineProvisionFailedException { public EngineProvisionTimeoutException(string engine) - : base($"Engine with name {engine} failed to provision") - { } + : base($"Engine with name `{engine}` failed to provision") + { + } } } From cc3131778b155579abbb76675262c9db93224fc9 Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 15:44:28 +0100 Subject: [PATCH 11/12] update exception message --- RelationalAI/EngineProvisionTimeoutException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RelationalAI/EngineProvisionTimeoutException.cs b/RelationalAI/EngineProvisionTimeoutException.cs index 56e9f38..a2a5f0b 100644 --- a/RelationalAI/EngineProvisionTimeoutException.cs +++ b/RelationalAI/EngineProvisionTimeoutException.cs @@ -22,7 +22,7 @@ namespace RelationalAI public class EngineProvisionTimeoutException : EngineProvisionFailedException { public EngineProvisionTimeoutException(string engine) - : base($"Engine with name `{engine}` failed to provision") + : base($"Engine with name `{engine}` provisioning timeout") { } } From 5357b0b145cb8337b5c049dd8d2eba3242f84e8d Mon Sep 17 00:00:00 2001 From: "helmi.nour" Date: Mon, 15 May 2023 17:02:49 +0100 Subject: [PATCH 12/12] fix linter --- RelationalAI/EngineProvisionTimeoutException.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RelationalAI/EngineProvisionTimeoutException.cs b/RelationalAI/EngineProvisionTimeoutException.cs index a2a5f0b..3f9a77f 100644 --- a/RelationalAI/EngineProvisionTimeoutException.cs +++ b/RelationalAI/EngineProvisionTimeoutException.cs @@ -23,7 +23,7 @@ public class EngineProvisionTimeoutException : EngineProvisionFailedException { public EngineProvisionTimeoutException(string engine) : base($"Engine with name `{engine}` provisioning timeout") - { - } + { + } } }