Skip to content

Commit 4fbd994

Browse files
authored
[pack] Initialize Java language worker at webhost level (#3824)
1 parent 9d7d401 commit 4fbd994

File tree

59 files changed

+1206
-614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1206
-614
lines changed

schemas/json/host.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,7 @@
126126
"description": "Configuration settings for Language Workers.",
127127
"type": "object",
128128

129-
"properties": {
130-
"maxMessageLength": {
131-
"description": "Defines maximum message size in MegaBytes (MB) for the streaming messages between the functions host and a language worker",
132-
"type": "integer",
133-
"minimum": 4,
134-
"default": 32
135-
},
129+
"properties": {
136130
"workersDirectory": {
137131
"description": "Specifies full path of the directory for language workers",
138132
"type": "string"

src/WebJobs.Script.Grpc/Abstractions/IRpcServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public interface IRpcServer
2121
/// <returns>A task that completes when the server is ready</returns>
2222
Task StartAsync();
2323

24+
/// <summary>
25+
/// Forces a server shutdown
26+
/// </summary>
27+
/// <returns>A task that completes when the server is killed</returns>
28+
Task KillAsync();
29+
2430
/// <summary>
2531
/// Gracefully shuts down the rpc server, allowing existing calls to finish
2632
/// </summary>

src/WebJobs.Script.Grpc/Server/GrpcServer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ public class GrpcServer : IRpcServer, IDisposable
1414
{
1515
private Server _server;
1616
private bool _disposed = false;
17+
public const int MaxMessageLengthBytes = 128 * 1024 * 1024;
1718

18-
public GrpcServer(FunctionRpc.FunctionRpcBase serviceImpl, int grpcMaxMessageLength)
19+
public GrpcServer(FunctionRpc.FunctionRpcBase serviceImpl)
1920
{
20-
ChannelOption maxReceiveMessageLength = new ChannelOption(ChannelOptions.MaxReceiveMessageLength, grpcMaxMessageLength);
21-
ChannelOption maxSendMessageLength = new ChannelOption(ChannelOptions.MaxSendMessageLength, grpcMaxMessageLength);
21+
ChannelOption maxReceiveMessageLength = new ChannelOption(ChannelOptions.MaxReceiveMessageLength, MaxMessageLengthBytes);
22+
ChannelOption maxSendMessageLength = new ChannelOption(ChannelOptions.MaxSendMessageLength, MaxMessageLengthBytes);
2223
ChannelOption[] grpcChannelOptions = { maxReceiveMessageLength, maxSendMessageLength };
2324
_server = new Server(grpcChannelOptions)
2425
{
@@ -37,6 +38,8 @@ public Task StartAsync()
3738

3839
public Task ShutdownAsync() => _server.ShutdownAsync();
3940

41+
public Task KillAsync() => _server.KillAsync();
42+
4043
protected virtual void Dispose(bool disposing)
4144
{
4245
if (!_disposed)

src/WebJobs.Script.Grpc/WebJobs.Script.Grpc.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Google.Protobuf" Version="3.5.1" />
24-
<PackageReference Include="Google.Protobuf.Tools" Version="3.5.1" />
25-
<PackageReference Include="Grpc.Core" Version="1.12.0" />
26-
<PackageReference Include="Grpc.Tools" Version="1.12.0">
23+
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
24+
<PackageReference Include="Google.Protobuf.Tools" Version="3.6.1" />
25+
<PackageReference Include="Grpc.Core" Version="1.16.0" />
26+
<PackageReference Include="Grpc.Tools" Version="1.16.0">
2727
<PrivateAssets>All</PrivateAssets>
2828
</PackageReference>
2929
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />

src/WebJobs.Script.Grpc/azure-functions-language-worker-protobuf/src/proto/FunctionRpc.proto

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ option java_multiple_files = true;
55
option java_package = "com.microsoft.azure.functions.rpc.messages";
66
option java_outer_classname = "FunctionProto";
77
option csharp_namespace = "Microsoft.Azure.WebJobs.Script.Grpc.Messages";
8+
option go_package ="github.com/Azure/azure-functions-go-worker/internal/rpc";
89

910
package AzureFunctionsRpcMessages;
1011

@@ -64,6 +65,10 @@ message StreamingMessage {
6465

6566
// Worker logs a message back to the host
6667
RpcLog rpc_log = 2;
68+
69+
FunctionEnvironmentReloadRequest function_environment_reload_request = 25;
70+
71+
FunctionEnvironmentReloadResponse function_environment_reload_response = 26;
6772
}
6873
}
6974

@@ -179,6 +184,16 @@ message WorkerStatusRequest{
179184
message WorkerStatusResponse {
180185
}
181186

187+
message FunctionEnvironmentReloadRequest {
188+
// Environment variables from the current process
189+
map<string, string> environment_variables = 1;
190+
}
191+
192+
message FunctionEnvironmentReloadResponse {
193+
// Status of the response
194+
StatusResult result = 3;
195+
}
196+
182197
// Host tells the worker to load a Function
183198
message FunctionLoadRequest {
184199
// unique function identifier (avoid name collisions, facilitate reload case)
@@ -354,4 +369,4 @@ message RpcHttp {
354369
map<string,string> query = 15;
355370
bool enable_content_negotiation= 16;
356371
TypedData rawBody = 17;
357-
}
372+
}

src/WebJobs.Script.Grpc/generate_protos.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ setlocal
3535
cd /d %~dp0
3636

3737
set NUGET_PATH=%UserProfile%\.nuget\packages
38-
set GRPC_TOOLS_PATH=%NUGET_PATH%\grpc.tools\1.12.0\tools\windows_x86
38+
set GRPC_TOOLS_PATH=%NUGET_PATH%\grpc.tools\1.16.0\tools\windows_x86
3939
set PROTO_PATH=.\azure-functions-language-worker-protobuf\src\proto
4040
set PROTO=.\azure-functions-language-worker-protobuf\src\proto\FunctionRpc.proto
41-
set PROTOBUF_TOOLS=%NUGET_PATH%\google.protobuf.tools\3.5.1\tools
41+
set PROTOBUF_TOOLS=%NUGET_PATH%\google.protobuf.tools\3.6.1\tools
4242
set MSGDIR=.\Messages
4343

4444
if exist %MSGDIR% rmdir /s /q %MSGDIR%

src/WebJobs.Script.Grpc/generate_protos.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ else
4949
NUGET_PATH=$NUGET_ROOT/packages
5050
fi
5151

52-
GRPC_TOOLS_PATH=$NUGET_PATH/grpc.tools/1.12.0/tools/$PLATFORM
52+
GRPC_TOOLS_PATH=$NUGET_PATH/grpc.tools/1.16.0/tools/$PLATFORM
5353
PROTO_PATH=./azure-functions-language-worker-protobuf/src/proto
54-
PROTOBUF_TOOLS=$NUGET_PATH/google.protobuf.tools/3.5.1/tools
54+
PROTOBUF_TOOLS=$NUGET_PATH/google.protobuf.tools/3.6.1/tools
5555
PROTO=./azure-functions-language-worker-protobuf/src/proto/FunctionRpc.proto
5656
MSGDIR=./Messages
5757

src/WebJobs.Script.WebHost/Standby/StandbyManager.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
7+
using System.Reactive.Linq;
68
using System.Threading;
79
using System.Threading.Tasks;
10+
using Microsoft.Azure.WebJobs.Script.Eventing;
11+
using Microsoft.Azure.WebJobs.Script.Rpc;
812
using Microsoft.Azure.WebJobs.Script.WebHost.Properties;
913
using Microsoft.Extensions.Configuration;
1014
using Microsoft.Extensions.FileProviders;
@@ -24,17 +28,17 @@ public class StandbyManager : IStandbyManager
2428
private readonly Lazy<Task> _specializationTask;
2529
private readonly IScriptWebHostEnvironment _webHostEnvironment;
2630
private readonly IEnvironment _environment;
31+
private readonly ILanguageWorkerChannelManager _languageWorkerChannelManager;
2732
private readonly IConfigurationRoot _configuration;
2833
private readonly ILogger _logger;
29-
3034
private readonly TimeSpan _specializationTimerInterval = TimeSpan.FromMilliseconds(500);
31-
private Timer _specializationTimer;
3235

36+
private Timer _specializationTimer;
3337
private static CancellationTokenSource _standbyCancellationTokenSource = new CancellationTokenSource();
3438
private static IChangeToken _standbyChangeToken = new CancellationChangeToken(_standbyCancellationTokenSource.Token);
3539
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
3640

37-
public StandbyManager(IScriptHostManager scriptHostManager, IConfiguration configuration, IScriptWebHostEnvironment webHostEnvironment,
41+
public StandbyManager(IScriptHostManager scriptHostManager, ILanguageWorkerChannelManager languageWorkerChannelManager, IConfiguration configuration, IScriptWebHostEnvironment webHostEnvironment,
3842
IEnvironment environment, IOptionsMonitor<ScriptApplicationHostOptions> options, ILogger<StandbyManager> logger)
3943
{
4044
_scriptHostManager = scriptHostManager ?? throw new ArgumentNullException(nameof(scriptHostManager));
@@ -43,8 +47,8 @@ public StandbyManager(IScriptHostManager scriptHostManager, IConfiguration confi
4347
_specializationTask = new Lazy<Task>(SpecializeHostCoreAsync, LazyThreadSafetyMode.ExecutionAndPublication);
4448
_webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
4549
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
46-
4750
_configuration = configuration as IConfigurationRoot ?? throw new ArgumentNullException(nameof(configuration));
51+
_languageWorkerChannelManager = languageWorkerChannelManager ?? throw new ArgumentNullException(nameof(languageWorkerChannelManager));
4852
}
4953

5054
public static IChangeToken ChangeToken => _standbyChangeToken;
@@ -66,8 +70,8 @@ public async Task SpecializeHostCoreAsync()
6670
// Trigger a configuration reload to pick up all current settings
6771
_configuration?.Reload();
6872

73+
await _languageWorkerChannelManager.SpecializeAsync();
6974
NotifyChange();
70-
7175
await _scriptHostManager.RestartHostAsync();
7276
await _scriptHostManager.DelayUntilHostReady();
7377
}

src/WebJobs.Script.WebHost/WebHostServiceCollectionExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi
6565
// Standby services
6666
services.AddStandbyServices();
6767

68-
// Core script host services
69-
services.AddSingleton<WebJobsScriptHostService>();
70-
services.AddSingleton<IHostedService>(s => s.GetRequiredService<WebJobsScriptHostService>());
7168
services.AddSingleton<IScriptHostManager>(s => s.GetRequiredService<WebJobsScriptHostService>());
7269
services.AddSingleton<IScriptWebHostEnvironment, ScriptWebHostEnvironment>();
7370
services.AddSingleton<IStandbyManager, StandbyManager>();
@@ -99,7 +96,6 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi
9996
// Management services
10097
services.AddSingleton<IWebFunctionsManager, WebFunctionsManager>();
10198
services.AddSingleton<IInstanceManager, InstanceManager>();
102-
10399
services.AddSingleton(_ => new HttpClient());
104100
services.AddSingleton<IFileSystem>(_ => FileUtility.Instance);
105101
services.AddTransient<VirtualFileSystem>();
@@ -109,8 +105,13 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi
109105
services.TryAddSingleton<ISecretManagerProvider, DefaultSecretManagerProvider>();
110106

111107
// Register common services with the WebHost
108+
// Language Worker Hosted Services need to be intialized before WebJobsScriptHostService
112109
ScriptHostBuilderExtensions.AddCommonServices(services);
113110

111+
// Core script host services
112+
services.AddSingleton<WebJobsScriptHostService>();
113+
services.AddSingleton<IHostedService>(s => s.GetRequiredService<WebJobsScriptHostService>());
114+
114115
// Configuration
115116
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<ScriptApplicationHostOptions>, ScriptApplicationHostOptionsSetup>());
116117
services.ConfigureOptions<LanguageWorkerOptionsSetup>();

src/WebJobs.Script.WebHost/WebJobsScriptHostService.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private async Task StartHostAsync(CancellationToken cancellationToken, int attem
125125
State = ScriptHostState.Default;
126126
}
127127

128-
bool isOffline = CheckAppOffline();
128+
bool isOffline = Utility.CheckAppOffline(_applicationHostOptions.CurrentValue.ScriptPath);
129+
State = isOffline ? ScriptHostState.Offline : State;
129130
bool hasNonTransientErrors = startupMode.HasFlag(JobHostStartupMode.HandlingNonTransientError);
130131

131132
// If we're in a non-transient error state or offline, skip host initialization
@@ -290,19 +291,6 @@ private void LogInitialization(bool isOffline, int attemptCount, int startCount)
290291
}
291292
}
292293

293-
private bool CheckAppOffline()
294-
{
295-
// check if we should be in an offline state
296-
string offlineFilePath = Path.Combine(_applicationHostOptions.CurrentValue.ScriptPath, ScriptConstants.AppOfflineFileName);
297-
if (File.Exists(offlineFilePath))
298-
{
299-
State = ScriptHostState.Offline;
300-
return true;
301-
}
302-
303-
return false;
304-
}
305-
306294
private void OnHostHealthCheckTimer(object state)
307295
{
308296
bool isHealthy = IsHostHealthy();

0 commit comments

Comments
 (0)