Skip to content

Commit

Permalink
When using SniffOnStartup() and newing clients all the time it will r…
Browse files Browse the repository at this point in the history
…esend a sniff, this fixes that by doing additional bookkeeping on IConnectionpool
  • Loading branch information
Mpdreamz committed Feb 25, 2015
1 parent a5b262d commit 56a629d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/Elasticsearch.Net/Connection/Transport.cs
Expand Up @@ -65,8 +65,11 @@ public class Transport : ITransport, ITransportDelegator

this._requestHandler = new RequestHandler(this.Settings, this._connectionPool, this.Connection, this._serializer, this._memoryStreamProvider, this);
this._requestHandlerAsync = new RequestHandlerAsync(this.Settings, this._connectionPool, this.Connection, this._serializer, this._memoryStreamProvider, this);
if (this._connectionPool.AcceptsUpdates && this.Settings.SniffsOnStartup)
if (this._connectionPool.AcceptsUpdates && this.Settings.SniffsOnStartup && !this._connectionPool.SniffedOnStartup)
{
Self.SniffClusterState();
this._connectionPool.SniffedOnStartup = true;
}
}

/* PING/SNIFF *** ********************************************/
Expand Down
5 changes: 5 additions & 0 deletions src/Elasticsearch.Net/ConnectionPool/IConnectionPool.cs
Expand Up @@ -24,6 +24,11 @@ public interface IConnectionPool
/// </summary>
bool UsingSsl { get; }

/// <summary>
/// Bookkeeps wheter this connectionpool has seen a sniff on startup.
/// </summary>
bool SniffedOnStartup { get; set; }

/// <summary>
/// Gets the next live Uri to perform the request on
/// </summary>
Expand Down
Expand Up @@ -10,9 +10,15 @@ public class SingleNodeConnectionPool : IConnectionPool
public int MaxRetries { get { return 0; } }

public bool AcceptsUpdates { get { return false; } }

public bool UsingSsl { get { return _uri.Scheme == Uri.UriSchemeHttps; } }

public bool SniffedOnStartup
{
get { return false; }
set { }
}

public SingleNodeConnectionPool(Uri uri)
{
//this makes sure that paths stay relative i.e if the root uri is:
Expand Down
2 changes: 2 additions & 0 deletions src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs
Expand Up @@ -22,6 +22,8 @@ public class StaticConnectionPool : IConnectionPool

public bool UsingSsl { get; internal set; }

public bool SniffedOnStartup { get; set; }

public StaticConnectionPool(
IEnumerable<Uri> uris,
bool randomizeOnStartup = true,
Expand Down
16 changes: 9 additions & 7 deletions src/Nest/Domain/Observers/RestoreObservable.cs
Expand Up @@ -109,14 +109,16 @@ public void Dispose()
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (_timer != null) _timer.Dispose();
if (_restoreStatusHumbleObject != null)
{
_restoreStatusHumbleObject.Next -= _nextEventHandlers;
_restoreStatusHumbleObject.Completed -= _completedEentHandlers;
_restoreStatusHumbleObject.Error -= _errorEventHandlers;

_timer.Dispose();
_restoreStatusHumbleObject.Next -= _nextEventHandlers;
_restoreStatusHumbleObject.Completed -= _completedEentHandlers;
_restoreStatusHumbleObject.Error -= _errorEventHandlers;

_restoreStatusHumbleObject.Completed -= StopTimer;
_restoreStatusHumbleObject.Error -= StopTimer;
_restoreStatusHumbleObject.Completed -= StopTimer;
_restoreStatusHumbleObject.Error -= StopTimer;
}

_disposed = true;
}
Expand Down
15 changes: 9 additions & 6 deletions src/Nest/Domain/Observers/SnapshotObservable.cs
Expand Up @@ -109,14 +109,17 @@ protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

_timer.Dispose();
if (_timer != null) _timer.Dispose();

_snapshotStatusHumbleObject.Next -= _nextEventHandler;
_snapshotStatusHumbleObject.Completed -= _completedEentHandler;
_snapshotStatusHumbleObject.Error -= _errorEventHandler;
if (_snapshotStatusHumbleObject != null)
{
_snapshotStatusHumbleObject.Next -= _nextEventHandler;
_snapshotStatusHumbleObject.Completed -= _completedEentHandler;
_snapshotStatusHumbleObject.Error -= _errorEventHandler;

_snapshotStatusHumbleObject.Completed -= StopTimer;
_snapshotStatusHumbleObject.Error -= StopTimer;
_snapshotStatusHumbleObject.Completed -= StopTimer;
_snapshotStatusHumbleObject.Error -= StopTimer;
}

_disposed = true;
}
Expand Down
Expand Up @@ -57,26 +57,32 @@ public void SniffCalledOnceAndEachEnpointPingedOnce()
var pingCall = FakeCalls.PingAtConnectionLevel(fake);
pingCall.Returns(FakeResponse.Ok(config));
var sniffCall = FakeCalls.Sniff(fake, config, uris);
var transport = FakeCalls.ProvideDefaultTransport(fake);
var getCall = FakeCalls.GetSyncCall(fake);
getCall.Returns(FakeResponse.Ok(config));

var client1 = new ElasticsearchClient(config, transport: transport);
var transport1 = FakeCalls.ProvideRealTranportInstance(fake);
var transport2 = FakeCalls.ProvideRealTranportInstance(fake);
var transport3 = FakeCalls.ProvideRealTranportInstance(fake);
var transport4 = FakeCalls.ProvideRealTranportInstance(fake);

transport1.Should().NotBe(transport2);

var client1 = new ElasticsearchClient(config, transport: transport1);
client1.Info();
client1.Info();
client1.Info();
client1.Info();
var client2 = new ElasticsearchClient(config, transport: transport);
var client2 = new ElasticsearchClient(config, transport: transport2);
client2.Info();
client2.Info();
client2.Info();
client2.Info();
var client3 = new ElasticsearchClient(config, transport: transport);
var client3 = new ElasticsearchClient(config, transport: transport3);
client3.Info();
client3.Info();
client3.Info();
client3.Info();
var client4 = new ElasticsearchClient(config, transport: transport);
var client4 = new ElasticsearchClient(config, transport: transport4);
client4.Info();
client4.Info();
client4.Info();
Expand Down

0 comments on commit 56a629d

Please sign in to comment.