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

AzureServiceBus ensure client reuse #1974

Merged

Conversation

TomMalow
Copy link
Contributor

@TomMalow TomMalow commented Jul 31, 2023

What this PR does / why we need it:
HealthChecks.AzureServiceBus did not reuse created clients clients when possible.
Using multiple health check to queues and subscription within the same ServiceBus would resolve in multiple clients being created. Thereby experience a long first time execution of the health check and unnecessary open connections.

Which issue(s) this PR fixes:
#1890

Special notes for your reviewer:
I moved the code over to use the shared ClientCache instead of an internal cache. The internal cache did not allow for caching across health check instances.
The HealthChecks.AzureServiceBus did not have any unit test before. I have added for most of the health check except EventBus health check as it was not modified by this PR. This is due to the EventBus health check not face the same problem.

Does this PR introduce a user-facing change?:
Yes:

  • New constructors are added to all ServiceBus Health Checks to allow for injecting customServiceBus client . Mainly for testing purpose.
  • A previous Prefix field as been made obsolete and superseded by the existing ConnectionKey

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Unit tests passing
  • End-to-end tests passing
  • Extended the documentation - No prior documentation
  • Provided sample for the feature - No prior samples

@TomMalow
Copy link
Contributor Author

@dotnet-policy-service agree company="Delegate A/S"

Previous commit introduced a new ServiceBusClientProvider which was added to all ServiceBus Health Check constructors. But this would introduce a breaking change.
This commit would re-add constructors with 1 argument to allow for backward compatability.
@sungam3r sungam3r added this to the V 7.1 milestone Aug 2, 2023
@sungam3r
Copy link
Collaborator

sungam3r commented Aug 2, 2023

@TomMalow I'm fine to merge after fixing all copy-paste issues. Thank you very much for exhaustive tests.

@sungam3r sungam3r added the enhancement New feature or request label Aug 2, 2023
also collect all values used to define a queue key together
Change to field to avoid recalculation of the value each time
@TomMalow TomMalow force-pushed the feature/ensure-service-bus-client-reuse branch from 6de43bf to a2ac5a3 Compare August 3, 2023 07:26
@TomMalow
Copy link
Contributor Author

TomMalow commented Aug 3, 2023

@sungam3r It should be ready now. Found some other similar mistakes that have been correct.
Thanks for being so thorough!

@sungam3r
Copy link
Collaborator

sungam3r commented Aug 6, 2023

Please bump <HealthCheckAzureServiceBus>7.0.0</HealthCheckAzureServiceBus>.

FullyQualifiedNamespace = FullyQualifiedName,
Credential = _tokenCredential,
};
var otherHealthCheck = new AzureServiceBusQueueHealthCheck(options, _clientProvider);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var otherHealthCheck = new AzureServiceBusQueueHealthCheck(options, _clientProvider);
var otherHealthCheck = new AzureServiceBusQueueHealthCheck(otherOptions, _clientProvider);

var otherHealthCheck = new AzureServiceBusQueueHealthCheck(options, _clientProvider);
var otherContext = new HealthCheckContext
{
Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null)
Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, otherHealthCheck, HealthStatus.Unhealthy, null)

Registration = new HealthCheckRegistration(HEALTH_CHECK_NAME, healthCheck, HealthStatus.Unhealthy, null)
};

var otherActual = await healthCheck
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var otherActual = await healthCheck
var otherActual = await otherHealthCheck

Please, look through test files one more time...

Some test was setup incorrectly but were not caught by in any assertion. Added som additional assert to ensure that healthchecks were executing as intended
HealthCheck was always called after setting it up in helper methods so to reduce boiler plate the call to health check is moved intothe helper method. Only a few had a substitute setup inbetween, but these could be moved to earlier in the respective tests
@TomMalow TomMalow force-pushed the feature/ensure-service-bus-client-reuse branch from f60355d to 9491eaf Compare August 10, 2023 06:11
@TomMalow
Copy link
Contributor Author

Got tired of embarrassing myself, so ended up refactoring the tests to reduce the potential for me making, or overseeing, any mistakes. The test now has reduced boilerplate around setting up and executing the HealthChecks. This also resulted in the tests being much easier to read.

@sungam3r
Copy link
Collaborator

@TomMalow I believe everything is OK now though I did not deep in each line this time.

@sungam3r sungam3r merged commit 7d9804e into Xabaril:master Aug 15, 2023
2 checks passed
@@ -8,40 +8,46 @@ namespace HealthChecks.AzureServiceBus
public abstract class AzureServiceBusHealthCheck<TOptions>
where TOptions : HealthChecks.AzureServiceBus.Configuration.AzureServiceBusHealthCheckOptions
{
protected static readonly System.Collections.Concurrent.ConcurrentDictionary<string, Azure.Messaging.ServiceBus.ServiceBusClient> ClientConnections;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Techically breaking changes, but I think v7.1 is OK.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 90.24390% with 4 lines in your changes missing coverage. Please review.

Project coverage is 67.45%. Comparing base (4259ef3) to head (ea6e21c).
Report is 99 commits behind head on master.

Files with missing lines Patch % Lines
...Checks.AzureServiceBus/ServiceBusClientProvider.cs 0.00% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1974      +/-   ##
==========================================
+ Coverage   66.35%   67.45%   +1.09%     
==========================================
  Files         251      252       +1     
  Lines        8692     8729      +37     
  Branches      622      624       +2     
==========================================
+ Hits         5768     5888     +120     
+ Misses       2762     2677      -85     
- Partials      162      164       +2     
Flag Coverage Δ
AzureServiceBus 72.56% <90.24%> (+8.76%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
azure enhancement New feature or request test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants