Skip to content
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
36 changes: 25 additions & 11 deletions src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,38 @@ 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, null, dateTimeProvider) => Randomize = randomize;
{
nodes.ThrowIfEmpty(nameof(nodes));
Randomize = randomize;
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));
DateTimeProvider = dateTimeProvider ?? Net.DateTimeProvider.Default;
_nodeScorer = nodeScorer;
Initialize(nodes, dateTimeProvider);
}

var nn = nodes.ToList();
var uris = nn.Select(n => n.Uri).ToList();
if (uris.Select(u => u.Scheme).Distinct().Count() > 1)
throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes");
private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvider)
{
DateTimeProvider = dateTimeProvider ?? Net.DateTimeProvider.Default;

UsingSsl = uris.Any(uri => uri.Scheme == "https");
string scheme = null;
foreach (var node in nodes)
{
if (scheme == null)
{
scheme = node.Uri.Scheme;
UsingSsl = scheme == "https";
}
else if (scheme != node.Uri.Scheme)
throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes");
}

_nodeScorer = nodeScorer;
InternalNodes = SortNodes(nn)
InternalNodes = SortNodes(nodes)
.DistinctBy(n => n.Uri)
.ToList();
LastUpdate = DateTimeProvider.Now();
Expand All @@ -57,7 +71,7 @@ protected StaticConnectionPool(IEnumerable<Node> nodes, Func<Node, float> nodeSc
public virtual bool SupportsReseeding => false;

/// <inheritdoc />
public bool UsingSsl { get; }
public bool UsingSsl { get; private set; }

protected List<Node> AliveNodes
{
Expand All @@ -70,7 +84,7 @@ protected List<Node> AliveNodes
}
}

protected IDateTimeProvider DateTimeProvider { get; }
protected IDateTimeProvider DateTimeProvider { get; private set; }

protected List<Node> InternalNodes { get; set; }
protected Random Random { get; } = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,30 @@ [U] public void Static()
}
}

// hide
[U] public void RandomizedInitialNodes()
{
StaticConnectionPool CreatStaticConnectionPool()
{
var uris = new[]
{
new Uri("https://10.0.0.1:9200/"),
new Uri("https://10.0.0.2:9200/"),
new Uri("https://10.0.0.3:9200/")
};

var staticConnectionPool = new StaticConnectionPool(uris);
return staticConnectionPool;
}

// 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.
Enumerable.Repeat(CreatStaticConnectionPool().CreateView().First().Uri.ToString(), 50)
Copy link
Member

Choose a reason for hiding this comment

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

I am looking to seeing this randomly fail 😸

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😃

.All(uri => uri == "https://10.0.0.1:9200/")
.Should()
.BeFalse();
}

/**[[sniffing-connection-pool]]
* ==== SniffingConnectionPool
*
Expand Down