From 730242a3c76223a74e0e061fb442c45f16cedaa3 Mon Sep 17 00:00:00 2001 From: Anatoli Beliaev Date: Mon, 15 Jul 2019 13:32:11 -0700 Subject: [PATCH 1/4] Ignore function load requests with duplicate FunctionId values. --- src/FunctionLoader.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FunctionLoader.cs b/src/FunctionLoader.cs index 4c2e1064..134aeeef 100644 --- a/src/FunctionLoader.cs +++ b/src/FunctionLoader.cs @@ -42,7 +42,10 @@ internal static AzFunctionInfo GetFunctionInfo(string functionId) /// internal static void LoadFunction(FunctionLoadRequest request) { - LoadedFunctions.Add(request.FunctionId, new AzFunctionInfo(request.Metadata)); + if (!LoadedFunctions.ContainsKey(request.FunctionId)) + { + LoadedFunctions.Add(request.FunctionId, new AzFunctionInfo(request.Metadata)); + } } /// From c550f23bad3906e68509be61fbc6f2998d174f2a Mon Sep 17 00:00:00 2001 From: Anatoli Beliaev Date: Mon, 15 Jul 2019 13:54:50 -0700 Subject: [PATCH 2/4] Revert "Ignore function load requests with duplicate FunctionId values." This reverts commit 730242a3c76223a74e0e061fb442c45f16cedaa3. --- src/FunctionLoader.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/FunctionLoader.cs b/src/FunctionLoader.cs index 134aeeef..4c2e1064 100644 --- a/src/FunctionLoader.cs +++ b/src/FunctionLoader.cs @@ -42,10 +42,7 @@ internal static AzFunctionInfo GetFunctionInfo(string functionId) /// internal static void LoadFunction(FunctionLoadRequest request) { - if (!LoadedFunctions.ContainsKey(request.FunctionId)) - { - LoadedFunctions.Add(request.FunctionId, new AzFunctionInfo(request.Metadata)); - } + LoadedFunctions.Add(request.FunctionId, new AzFunctionInfo(request.Metadata)); } /// From d8c7d186c4590f6da0e98ae4b689b285822235a9 Mon Sep 17 00:00:00 2001 From: Anatoli Beliaev Date: Mon, 15 Jul 2019 14:09:32 -0700 Subject: [PATCH 3/4] Add FunctionLoader IsLoaded method --- src/FunctionLoader.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/FunctionLoader.cs b/src/FunctionLoader.cs index 4c2e1064..c16faa70 100644 --- a/src/FunctionLoader.cs +++ b/src/FunctionLoader.cs @@ -36,6 +36,14 @@ internal static AzFunctionInfo GetFunctionInfo(string functionId) throw new InvalidOperationException(string.Format(PowerShellWorkerStrings.FunctionNotLoaded, functionId)); } + /// + /// Returns true if the function with the given functionId is already loaded. + /// + public static bool IsLoaded(string functionId) + { + return LoadedFunctions.ContainsKey(functionId); + } + /// /// This method runs once per 'FunctionLoadRequest' during the code start of the worker. /// It will always run synchronously because we process 'FunctionLoadRequest' synchronously. From 9c34c2092e89a6ac1ce62744be61566b0917d5b1 Mon Sep 17 00:00:00 2001 From: Anatoli Beliaev Date: Mon, 15 Jul 2019 14:10:30 -0700 Subject: [PATCH 4/4] Return from ProcessFunctionLoadRequest early if already loaded --- src/RequestProcessor.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/RequestProcessor.cs b/src/RequestProcessor.cs index d72815c5..e5ca4e16 100644 --- a/src/RequestProcessor.cs +++ b/src/RequestProcessor.cs @@ -145,6 +145,16 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request) out StatusResult status); response.FunctionLoadResponse.FunctionId = functionLoadRequest.FunctionId; + // The worker may occasionally receive multiple function load requests with + // the same FunctionId. In order to make function load request idempotent, + // the worker should ignore the duplicates. + if (FunctionLoader.IsLoaded(functionLoadRequest.FunctionId)) + { + // If FunctionLoader considers this function loaded, this means + // the previous request was successful, so respond accordingly. + return response; + } + // When a functionLoadRequest comes in, we check to see if a dependency download has failed in a previous call // or if PowerShell could not be initialized. If this is the case, mark this as a failed request // and submit the exception to the Host (runtime).