-
Notifications
You must be signed in to change notification settings - Fork 151
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
Randomize implicit servers #376
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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,139 @@ | ||
// Copyright 2020 The NATS 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. | ||
|
||
using System.Linq; | ||
using NATS.Client; | ||
using Xunit; | ||
|
||
namespace UnitTests | ||
{ | ||
public class TestServerPool | ||
{ | ||
static readonly string[] startUrls = { | ||
"nats://a:4222", "nats://b:4222", "nats://c:4222", "nats://d:4222", | ||
"nats://e:4222", "nats://f:4222", "nats://g:4222", "nats://h:4222", | ||
"nats://i:4222", "nats://j:4222", "nats://k:4222", "nats://l:4222" | ||
}; | ||
|
||
[Fact] | ||
public void TestDefault() | ||
{ | ||
var sp = new ServerPool(); | ||
sp.Setup(new Options()); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
Assert.True(poolUrls.Length == 1); | ||
Assert.Equal(poolUrls[0], Defaults.Url); | ||
} | ||
|
||
[Fact] | ||
public void TestBasicRandomization() | ||
{ | ||
var opts = ConnectionFactory.GetDefaultOptions(); | ||
opts.Servers = startUrls; | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
var sp = new ServerPool(); | ||
sp.Setup(opts); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
Assert.True(poolUrls.Length == startUrls.Length); | ||
Assert.False(poolUrls.SequenceEqual(startUrls)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestIdempotency() | ||
{ | ||
var opts = ConnectionFactory.GetDefaultOptions(); | ||
opts.Servers = startUrls; | ||
|
||
var sp = new ServerPool(); | ||
sp.Setup(opts); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
Assert.True(poolUrls.Length == startUrls.Length); | ||
|
||
sp.Add(startUrls, true); | ||
Assert.True(poolUrls.Length == startUrls.Length); | ||
} | ||
|
||
[Fact] | ||
public void TestNoRandomization() | ||
{ | ||
var opts = ConnectionFactory.GetDefaultOptions(); | ||
opts.Servers = startUrls; | ||
opts.NoRandomize = true; | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
var sp = new ServerPool(); | ||
sp.Setup(opts); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
Assert.True(poolUrls.Length == startUrls.Length); | ||
Assert.True(poolUrls.SequenceEqual(startUrls)); | ||
} | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
var sp = new ServerPool(); | ||
sp.Setup(opts); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
Assert.True(poolUrls.Length == startUrls.Length); | ||
Assert.True(poolUrls.SequenceEqual(startUrls)); | ||
|
||
string[] impUrls = { | ||
"nats://impA:4222", "nats://impB:4222", "nats://impC:4222", "nats://impD:4222", | ||
"nats://impE:4222", "nats://impF:4222", "nats://impG:4222", "nats://impH:4222", | ||
}; | ||
sp.Add(impUrls, true); | ||
Assert.True(poolUrls.SequenceEqual(startUrls)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestImplicitRandomization() | ||
{ | ||
var opts = ConnectionFactory.GetDefaultOptions(); | ||
opts.Url = null; | ||
opts.Servers = startUrls; | ||
|
||
var sp = new ServerPool(); | ||
sp.Setup(opts); | ||
|
||
string[] impUrls = { | ||
"nats://impA:4222", "nats://impB:4222", "nats://impC:4222", "nats://impD:4222", | ||
"nats://impE:4222", "nats://impF:4222", "nats://impG:4222", "nats://impH:4222", | ||
}; | ||
sp.Add(impUrls, true); | ||
|
||
var poolUrls = sp.GetServerList(false); | ||
|
||
// Ensure length is OK and that we have randomized the list | ||
Assert.True(poolUrls.Length == startUrls.Length + impUrls.Length); | ||
Assert.False(poolUrls.SequenceEqual(startUrls)); | ||
|
||
// Ensure implicit urls aren't placed at the end of the list. | ||
int i; | ||
for (i = 0; i < startUrls.Length; i++) | ||
{ | ||
if (poolUrls[i].Contains("imp")) | ||
break; | ||
} | ||
Assert.True(i != startUrls.Length); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on latest comment on the Go client, I wonder if this need to be updated. Basically in Go I was shuffling the pool after adding all new URLs (when processing INFO). But the point made was that possibly the first URL in the pool would be moved (say to position 2 (index 1)), and on reconnect that would be the very next one to be tried although we just disconnected from it. Let me know if I make sense (note the Go PR has not been updated as I type this).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per discussions in Slack, we've concluded that this achieves the same result, so I'll keep this as is. Appreciate the catch here!