Skip to content

Fix test for random initialized node #4134

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 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,35 @@ public StaticConnectionPool(IEnumerable<Uri> uris, bool randomize = true, IDateT
: this(uris.Select(uri => new Node(uri)), randomize, dateTimeProvider) { }

public StaticConnectionPool(IEnumerable<Node> nodes, bool randomize = true, IDateTimeProvider dateTimeProvider = null)
: this(nodes, randomize, randomizeSeed: null, dateTimeProvider) { }

protected StaticConnectionPool(IEnumerable<Node> nodes, bool randomize, int? randomizeSeed = null, IDateTimeProvider dateTimeProvider = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if for master, we should consider accepting an instance of Random here instead of randomize and randomizeSeed

{
nodes.ThrowIfEmpty(nameof(nodes));
Randomize = randomize;
Random = !randomize || !randomizeSeed.HasValue
? new Random()
: new Random(randomizeSeed.Value);

Initialize(nodes, dateTimeProvider);
}

//this constructor is protected because nodeScorer only makes sense on subclasses that support reseeding
//otherwise just manually sort `nodes` before instantiating.
protected StaticConnectionPool(IEnumerable<Node> nodes, Func<Node, float> nodeScorer, IDateTimeProvider dateTimeProvider = null)
{
nodes.ThrowIfEmpty(nameof(nodes));
_nodeScorer = nodeScorer;
Initialize(nodes, dateTimeProvider);
}

private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvider)
{

var nodesProvided = nodes?.ToList() ?? throw new ArgumentNullException(nameof(nodes));
nodesProvided.ThrowIfEmpty(nameof(nodes));
DateTimeProvider = dateTimeProvider ?? Net.DateTimeProvider.Default;

string scheme = null;
foreach (var node in nodes)
foreach (var node in nodesProvided)
{
if (scheme == null)
{
Expand All @@ -46,7 +54,7 @@ private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvi
throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes");
}

InternalNodes = SortNodes(nodes)
InternalNodes = SortNodes(nodesProvided)
.DistinctBy(n => n.Uri)
.ToList();
LastUpdate = DateTimeProvider.Now();
Expand Down Expand Up @@ -87,7 +95,7 @@ protected List<Node> AliveNodes
protected IDateTimeProvider DateTimeProvider { get; private set; }

protected List<Node> InternalNodes { get; set; }
protected Random Random { get; } = new Random();
protected Random Random { get; }
protected bool Randomize { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Configuration;
using Tests.Framework;
using Tests.XPack.Security.Privileges;

Expand Down Expand Up @@ -216,20 +217,30 @@ [U] public void Static()
}
}

//hide
private class SeededRandomConectionPool : StaticConnectionPool
{
public SeededRandomConectionPool(IEnumerable<Node> nodes, int seed)
: base(nodes, randomize: true, randomizeSeed: seed, dateTimeProvider: null)
{}
}

// hide
[U] public void RandomizedInitialNodes()
{
IEnumerable<StaticConnectionPool> CreatStaticConnectionPools()
IEnumerable<StaticConnectionPool> CreateSeededPools(int nodeCount, int pools)
{
Thread.Sleep(1);
var uris = Enumerable.Range(1, 50).Select(i => new Uri($"https://10.0.0.{i}:9200/"));
yield return new StaticConnectionPool(uris);
var seed = TestConfiguration.Instance.Seed;
var nodes = Enumerable.Range(1, nodeCount)
.Select(i => new Node(new Uri($"https://10.0.0.{i}:9200/")))
.ToList();
for(var i = 0; i < nodeCount; i++)
yield return new SeededRandomConectionPool(nodes, seed + i);
}

// assertion works on the probability of seeing a Uri other than https://10.0.0.1:9200/
// as the first value over 50 runs, when randomized.
CreatStaticConnectionPools()
.Take(50)
var connectionPools = CreateSeededPools(100, 100).ToList();
connectionPools.Should().HaveCount(100);
connectionPools
.Select(p => p.CreateView().First().Uri.ToString())
.All(uri => uri == "https://10.0.0.1:9200/")
.Should()
Expand Down