Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

[Service Bus] Add timeout to shutdown#120

Merged
loic-sharma merged 6 commits intomasterfrom
loshar-shutdown
Jan 18, 2018
Merged

[Service Bus] Add timeout to shutdown#120
loic-sharma merged 6 commits intomasterfrom
loshar-shutdown

Conversation

@loic-sharma
Copy link
Copy Markdown
Contributor

@loic-sharma loic-sharma commented Jan 17, 2018

Today, the Orchestrator has custom code to ensure that:

  1. New Service Bus messages are dropped once shutdown has started
  2. Shutting down Service Bus does not block the current thread

This brings the logic to the SubscriptionProcessor so that all other jobs can also have these guarantees.

Part of https://github.com/NuGet/Engineering/issues/1115

public async Task StopReturnsTrueIfClientCloseAsyncMethodFinishesFirst()
{
// Arrange
_client.Setup(c => c.CloseAsync()).Returns(Task.Delay(TimeSpan.FromMilliseconds(5)));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does it even need a 5ms sleep, or can we reduce to 1 or 0?

Copy link
Copy Markdown
Contributor Author

@loic-sharma loic-sharma Jan 17, 2018

Choose a reason for hiding this comment

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

Ideally we'd use TimeSpan.MaxValue and TimeSpan.MinValue, but, Task.Delay was throwing exceptions. A human cannot tell the difference between 1ms or 5ms, but sure.

// Start two tasks: one for the shutdown and one for the shutdown's timeout. The first task to finish will
// complete the "start shutdown" operation. Note that this method returns false if the timeout task completes first.
var shutdownTask = _client.CloseAsync();
var shutdownTimeoutTask = Task.Delay(timeout);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This isn't really a timeout for CloseAsync, just a timeout for how long to wait.

It looks like there's a ServiceBus OperationTimeout, settable via the messaging factory. I wonder if that's really the approach we should be taking. Found the timeout via reflector, but see also stackoverflow:
https://stackoverflow.com/questions/24416435/setting-the-operationtimeout-property-for-a-service-bus-worker-role

Copy link
Copy Markdown
Contributor Author

@loic-sharma loic-sharma Jan 17, 2018

Choose a reason for hiding this comment

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

That is correct - it is a timeout on how long to wait. Let me know if you think the documentation on the interface for this method is confusing.

It seems that OperationTimeout does not relate to the time the CloseAsync method takes, but rather, the time a thread can take to process a message. This fix is still necessary (note that this is copying code that already exists in the Orchestrator)

Also, we lease the messages from Service Bus. The handlers will fail to update the state of a message whose lease has expired.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Documentation is clear. Assuming the process will just exit if we don't block on a hung CloseAsync?

Re: OperationTimeout does not relate to the time the CloseAsync method takes...
This is what I was wondering; whether OperationTimeout would prevent a CloseAsync hang due to processing of messages. Looks like it defaults to 1 min, so makes sense if this isn't related.

Seems ok for now, but do we know why CloseAsync hangs? Just want to make sure we clean up properly and don't have side effects.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Kind of... the process won't end if we're blocked on CloseAsync, but, other factors may prevent the process from exiting.

We're not sure what causes the CloseAsync hang, @agr: is still investigating.


private async Task OnMessageAsync(IBrokeredMessage brokeredMessage)
{
if (_running == false)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

!_running?

@loic-sharma loic-sharma merged commit afd08ca into master Jan 18, 2018
@loic-sharma loic-sharma deleted the loshar-shutdown branch January 18, 2018 18:28
loic-sharma added a commit to NuGet/NuGet.Jobs that referenced this pull request Jan 18, 2018
The core logic of Service Bus shutdown was moved to the Service Bus dependencies. This pull request removes the shutdown logic that Service Bus dependency now handles.

Part of https://github.com/NuGet/Engineering/issues/1115
Depends on NuGet/ServerCommon#120
joelverhagen pushed a commit to NuGet/NuGet.Jobs that referenced this pull request Oct 26, 2020
The core logic of Service Bus shutdown was moved to the Service Bus dependencies. This pull request removes the shutdown logic that Service Bus dependency now handles.

Part of https://github.com/NuGet/Engineering/issues/1115
Depends on NuGet/ServerCommon#120
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants