Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forwarding the LanguageWorkerOptions from the host to the job host scope and ensuring we use the provided instances. #10369

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

fabiocav
Copy link
Member

@fabiocav fabiocav commented Aug 6, 2024

Issue describing the changes in this PR

Addressing an issue leading to double initialization of worker configuration and profile evaluation.

Pull request checklist

IMPORTANT: Currently, changes must be backported to the in-proc branch to be included in Core Tools and non-Flex deployments.

  • Backporting to the in-proc branch is not required
    • Otherwise: Link to backporting PR
  • My changes do not require documentation changes
    • Otherwise: Documentation issue linked to PR
  • My changes should not be added to the release notes for the next release
    • Otherwise: I've added my notes to release_notes.md
  • My changes do not need to be backported to a previous version
    • Otherwise: Backport tracked by issue/PR #issue_or_pr
  • My changes do not require diagnostic events changes
    • Otherwise: I have added/updated all related diagnostic events and their documentation (Documentation issue linked to PR)
  • I have added all required tests (Unit tests, E2E tests)

@fabiocav fabiocav requested a review from a team as a code owner August 6, 2024 00:57
@fabiocav fabiocav requested review from kshyju and brettsam August 6, 2024 00:58
@fabiocav fabiocav force-pushed the fabiocav/iomlwo branch 2 times, most recently from c71bd21 to 1306919 Compare August 6, 2024 01:01
to the job host scope and ensuring we use the provided instances.
@liliankasem
Copy link
Member

Is it possible to add tests to validate this change?

… refresh LanguageWorkerOption after script host is built. This will cause the LanguageWorkerOption to be refreshed after specialization.
@fabiocav fabiocav marked this pull request as draft September 16, 2024 16:24
@kshyju kshyju marked this pull request as ready for review September 28, 2024 00:26
@kshyju
Copy link
Member

kshyju commented Sep 28, 2024

@brettsam Ping on the review.

@@ -42,18 +47,53 @@ public void Configure(LanguageWorkerOptions options)
{
string workerRuntime = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName);

// Parsing worker.config.json should always be done in case of multi language worker
// Parsing worker.latestConfiguration.json should always be done in case of multi language worker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is worker.latestConfiguration.json a new thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking like an accidental change to me. I don't expect a different file name.

if (rpcWorkerChannel != null && !UsePlaceholderChannel(rpcWorkerChannel))
{
_logger.LogInformation("Language worker options changed, and the placeholder worker channel is invalid for other reasons. Shutting down the channel.");
await ShutdownChannelIfExistsAsync(_workerRuntime, rpcWorkerChannel.Id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the scenario here, and why didn't we have this behavior before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to review this to comment. That's part of a set of changes pushed by @kshyju .

Changes to the signal used to reload LanguageWorkerOptions were required. I'm concerned that the logic here is assuming this will always change as a result of specialization, which may change in the future.

At the very least, the message needs to be a bit more descriptive.

src/WebJobs.Script/ScriptHostBuilderExtensions.cs Outdated Show resolved Hide resolved
src/WebJobs.Script/ScriptHostBuilderExtensions.cs Outdated Show resolved Hide resolved
if (applicationHostOptions.HasParentScope)
{
// Forward th host LanguageWorkerOptions to the Job Host.
var languageWorkerOptions = applicationHostOptions.RootServiceProvider.GetService<IOptionsMonitor<LanguageWorkerOptions>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am slightly concerned about how this will work in practice. There are subtleties to the way options are registered and calculated. I would feel better if we did the following:

services.AddSingleton(_ => applicationHostOptions.RootServiceProvider.GetService<IOptionsMonitor<LanguageWorkerOptions>>());
services.AddScoped(_ => applicationHostOptions.RootServiceProvider.GetService<IOptionsSnapshot<LanguageWorkerOptions>>());
services.AddSingleton(_ => applicationHostOptions.RootServiceProvider.GetService<IOptions<LanguageWorkerOptions>>());

Could even lift this to an extension method IServiceCollection ForwardOptionsFrom<TOptions>(this IServiceCollection, IServiceProvider source);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on the concerns? I like the idea of wrapping that into an extension to make this cleaner and reusable (this is applied to other options). But for the functionality, in this case, we are explicitly trying to avoid another cache miss when using IOptions<T>, for LanguageWorkerOptions specifically, given the cost of running its setup today and the state it tracks (those instances would be identical).

Copy link
Member Author

@fabiocav fabiocav Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, one thing to note is that using the suggested pattern will defer execution of the setup to the time services in the child container request the options, if nothing at the host scope consumes one of those option types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, one thing to note is that using the suggested pattern will defer execution of the setup to the time services in the child container request the options, if nothing at the host scope consumes one of those option types.

That was intentional. I wanted to ensure IOptions in both parent and child container would be the same values. Otherwise, the two containers could potentially have different values if config changes between the two. Unlikely, but that would be one confusing bug if it ever happens.

we are explicitly trying to avoid another cache miss when using IOptions

Would this cache miss not already exist in the parent container?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the pattern currently used in the code, the values would be the same, also refreshed when using IOptionsMonitor, one of the differences is that when using IOptions<T>, you'd end up with the same instance that is returned by IOptionsMonitor<T> (at the initial construction here), but that's intentional.

Would this cache miss not already exist in the parent container?

Right now, there are two cache misses in the JobHost/ScriptHost scope with every single initialization:

  1. Services taking a dependency on IOptionsMonitor<LanguageWorkerOptions>
  2. Services taking a dependency on IOptions<LanguageWorkerOptions>

If no WebHost services are using IOptions<LanguageWorkerOptions> (or if that ever changes in the future as a result of changes to WebHost scoped services), we'd end up with a cache miss when initializing the JobHost, which would have an impact on cold start in some cases (specialization flows shouldn't be as impacted as the expectation is that placeholders would have forced this to happen).

The IOptions<LanguageWorkerOptions> is the second hit we're trying to avoid.

For the WebHost, in specialization cases, even if the host has services consuming IOptions<LanguageWorkerOptions>, those wouldn't impact customer cold start as they would happen in placeholders.

Ultimately, much of what we're dealing with here is the fact the this current setup implementation is doing way too much and that logic should be refactored into a separate, singleton, service that is reused, avoiding the duplicate execution of that expensive logic that is not expected to run more than once anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WebHost won't ever be realizing IOptions<LanguageWorkerOptions> when we have a customer payload? If that is the case, then I am fine with the approach as is.

@fabiocav
Copy link
Member Author

fabiocav commented Oct 8, 2024

@jviau / @brettsam / @liliankasem I'm reviewing the latest changes, and will take point on addressing any feedback.

{
var message = $"Call to configure {nameof(LanguageWorkerOptions)} from the JobHost scope. " +
$"If using {nameof(IOptions<LanguageWorkerOptions>)}, please use {nameof(IOptionsMonitor<LanguageWorkerOptions>)} instead.";
Debug.Fail(message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the intention behind this? The comment says to use IOptionsMonitor<LanguageWorkerOptions>, but won't this class still run (and fail) for that type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message is a bit confusing, but with the forwarding, no setups should run for IOptionsMonitor<LanguageWorkerOptions> within the JobHost scope. The same applies to IOptions<LanguageWorkerOptions, though, so either option would take the host injected instances and not run any setup logic within that scope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, can we clarify that?

  1. Add a comment explaining this is only registered for JobHost scope
  2. Update debug message to something like "Unexpected configuration of LanguageWorkerOptions from the JobHost scope. LanguageWorkerOptions should be forwarded from the parent scope with no additional configuration."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants