-
Notifications
You must be signed in to change notification settings - Fork 810
Fix UpdateBalancingState not called when address attributes are modified #2553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"sdk": { | ||
"version": "9.0.100-preview.7.24407.12", | ||
"version": "9.0.100-rc.2.24474.11", | ||
"rollForward": "latestFeature" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
test/Grpc.Net.Client.Tests/Balancer/SubchannelsLoadBalancerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#region Copyright notice and license | ||
|
||
// Copyright 2019 The gRPC Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#endregion | ||
|
||
#if SUPPORT_LOAD_BALANCING | ||
using System.Net; | ||
using Grpc.Core; | ||
using Grpc.Net.Client.Balancer; | ||
using Grpc.Net.Client.Balancer.Internal; | ||
using Grpc.Tests.Shared; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Grpc.Net.Client.Tests.Infrastructure.Balancer; | ||
|
||
[TestFixture] | ||
public class SubchannelsLoadBalancerTests | ||
{ | ||
[Test] | ||
public void UpdateChannelState_AddressMatchAndAttributesDifferent_UpdateState() | ||
{ | ||
// Arrange | ||
const string host1 = "127.0.0.1"; | ||
const string host2 = "127.0.0.2"; | ||
const int port = 80; | ||
|
||
const string attributeKey = "key1"; | ||
|
||
var controller = new CustomChannelControlHelper(); | ||
var balancer = new CustomBalancer(controller, NullLoggerFactory.Instance); | ||
|
||
// create 2 addresses with some attributes | ||
var address1 = new BalancerAddress(host1, port); | ||
address1.Attributes.TryAdd(attributeKey, 20); // <-- difference | ||
|
||
var address2 = new BalancerAddress(host2, port); | ||
address2.Attributes.TryAdd(attributeKey, 80); // <-- difference | ||
|
||
var state1 = new ChannelState( | ||
status: new Status(), | ||
addresses: [address1, address2], | ||
loadBalancingConfig: null, | ||
attributes: new BalancerAttributes()); | ||
|
||
// create 2 addresses with the same hosts and ports as previous but with other attribute values | ||
var address3 = new BalancerAddress(host1, port); | ||
address3.Attributes.TryAdd(attributeKey, 40); // <-- difference | ||
|
||
var address4 = new BalancerAddress(host2, port); | ||
address4.Attributes.TryAdd(attributeKey, 60); // <-- difference | ||
|
||
var state2 = new ChannelState( | ||
status: new Status(), | ||
addresses: [address3, address4], | ||
loadBalancingConfig: null, | ||
attributes: new BalancerAttributes()); | ||
|
||
// Act | ||
// first update with `address1` and `address2` | ||
balancer.UpdateChannelState(state1); | ||
|
||
// remember count of `IChannelControlHelper.UpdateState()` calls | ||
var updateStateCallsCount1 = controller.UpdateStateCallsCount; | ||
|
||
// second update with `address3` and `address4` | ||
// which differs from `address1` and `address2` _only_ in attributes values | ||
balancer.UpdateChannelState(state2); | ||
|
||
// get count of `IChannelControlHelper.UpdateState()` calls after second update | ||
var updateStateCallsCount2 = controller.UpdateStateCallsCount; | ||
|
||
// Assert | ||
Assert.True( | ||
updateStateCallsCount2 > updateStateCallsCount1, | ||
"`IChannelControlHelper.UpdateState()` was not called from `SubchannelsLoadBalancer.UpdateChannelState()`"); | ||
} | ||
} | ||
|
||
file class CustomBalancer( | ||
IChannelControlHelper controller, | ||
ILoggerFactory loggerFactory) | ||
: SubchannelsLoadBalancer(controller, loggerFactory) | ||
{ | ||
protected override SubchannelPicker CreatePicker(IReadOnlyList<Subchannel> readySubchannels) | ||
{ | ||
return new CustomPicker(readySubchannels); | ||
} | ||
} | ||
|
||
file class CustomPicker : SubchannelPicker | ||
{ | ||
private IReadOnlyList<Subchannel> readySubchannels; | ||
|
||
public CustomPicker(IReadOnlyList<Subchannel> readySubchannels) | ||
{ | ||
this.readySubchannels = readySubchannels; | ||
} | ||
|
||
public override PickResult Pick(PickContext context) | ||
{ | ||
return PickResult.ForSubchannel(readySubchannels[0]); | ||
} | ||
} | ||
|
||
file class CustomChannelControlHelper : IChannelControlHelper | ||
{ | ||
public int UpdateStateCallsCount { get; private set; } | ||
|
||
public Subchannel CreateSubchannel(SubchannelOptions options) | ||
{ | ||
var subchannelTransportFactory = new CustomSubchannelTransportFactory(); | ||
|
||
var manager = new ConnectionManager( | ||
new CustomResolver(), | ||
true, | ||
NullLoggerFactory.Instance, | ||
new CustomBackoffPolicyFactory(), | ||
subchannelTransportFactory, | ||
[]); | ||
|
||
return ((IChannelControlHelper)manager).CreateSubchannel(options); | ||
} | ||
|
||
public void UpdateState(BalancerState state) | ||
{ | ||
UpdateStateCallsCount++; | ||
} | ||
|
||
public void RefreshResolver() { } | ||
} | ||
|
||
file class CustomResolver() : PollingResolver(NullLoggerFactory.Instance) | ||
{ | ||
protected override Task ResolveAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
file class CustomBackoffPolicyFactory : IBackoffPolicyFactory | ||
{ | ||
public IBackoffPolicy Create() | ||
{ | ||
return new CustomBackoffPolicy(); | ||
} | ||
} | ||
|
||
file class CustomBackoffPolicy : IBackoffPolicy | ||
{ | ||
public TimeSpan NextBackoff() | ||
{ | ||
return TimeSpan.Zero; | ||
} | ||
} | ||
|
||
file class CustomSubchannelTransportFactory : ISubchannelTransportFactory | ||
{ | ||
public ISubchannelTransport Create(Subchannel subchannel) | ||
{ | ||
return new CustomSubchannelTransport(); | ||
} | ||
} | ||
|
||
file class CustomSubchannelTransport : ISubchannelTransport | ||
{ | ||
public void Dispose() { } | ||
|
||
public DnsEndPoint? CurrentEndPoint { get; } | ||
public TimeSpan? ConnectTimeout { get; } | ||
public TransportStatus TransportStatus { get; } | ||
|
||
public ValueTask<Stream> GetStreamAsync(DnsEndPoint endPoint, CancellationToken cancellationToken) | ||
{ | ||
return ValueTask.FromResult<Stream>(new MemoryStream()); | ||
} | ||
|
||
public ValueTask<ConnectResult> TryConnectAsync(ConnectContext context, int attempt) | ||
{ | ||
return ValueTask.FromResult(ConnectResult.Success); | ||
} | ||
|
||
public void Disconnect() { } | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.