Skip to content

Commit

Permalink
Attempt at renames (#96)
Browse files Browse the repository at this point in the history
* Renames

- TransportClient => interface IRequestInvoker
- HttpTransport => interface ITransport
- DefaultHttpTransport => DistributedTransport

* update argument name to transport configuration

* fix testablehttpconnection

* move to ITRansport and use new SemverInfo

* Introduce `ToSemverString` for now

* rename

* update connection args to requestInvoker

* address a few nullable violations

* xmldocs fixes
  • Loading branch information
Mpdreamz authored Feb 9, 2024
1 parent 0146449 commit 4d0e49d
Show file tree
Hide file tree
Showing 59 changed files with 595 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace Elastic.Transport.Benchmarks
{
public class TransportBenchmarks
{
private DefaultHttpTransport _transport;
private ITransport _transport;

[GlobalSetup]
public void Setup()
{
var connection = new InMemoryTransportClient();
var requestInvoker = new InMemoryRequestInvoker();
var pool = new SingleNodePool(new Uri("http://localhost:9200"));
var settings = new TransportConfiguration(pool, connection);
var settings = new TransportConfiguration(pool, requestInvoker);

_transport = new DefaultHttpTransport(settings);
_transport = new DistributedTransport(settings);
}

[Benchmark]
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Elastic.Transport.Profiling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static async Task Main()
var config = new TransportConfiguration(new Uri("http://localhost:9200"),
new ElasticsearchProductRegistration(typeof(ElasticsearchProductRegistration)));

var transport = new DefaultHttpTransport(config);
var transport = new DistributedTransport(config);

// WARMUP
for (var i = 0; i < 50; i++) _ = await transport.GetAsync<VoidResponse>("/");
Expand Down
1 change: 1 addition & 0 deletions elastic-transport-net.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ See the LICENSE file in the project root for more information</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HDR/@EntryIndexedValue">HDR</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TDigest/@EntryIndexedValue">TDigest</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WKT/@EntryIndexedValue">WKT</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ namespace Elastic.Transport.VirtualizedCluster.Components;
/// </summary>
public sealed class ExposingPipelineFactory<TConfiguration> : RequestPipelineFactory<TConfiguration> where TConfiguration : class, ITransportConfiguration
{
public ExposingPipelineFactory(TConfiguration connectionSettings, DateTimeProvider dateTimeProvider)
public ExposingPipelineFactory(TConfiguration configuration, DateTimeProvider dateTimeProvider)
{
DateTimeProvider = dateTimeProvider;
MemoryStreamFactory = TransportConfiguration.DefaultMemoryStreamFactory;
Settings = connectionSettings;
Pipeline = Create(Settings, DateTimeProvider, MemoryStreamFactory, new DefaultRequestParameters());
Transport = new DefaultHttpTransport<TConfiguration>(Settings, this, DateTimeProvider, MemoryStreamFactory);
Configuration = configuration;
Pipeline = Create(Configuration, DateTimeProvider, MemoryStreamFactory, new DefaultRequestParameters());
RequestHandler = new DistributedTransport<TConfiguration>(Configuration, this, DateTimeProvider, MemoryStreamFactory);
}

// ReSharper disable once MemberCanBePrivate.Global
public RequestPipeline Pipeline { get; }
private DateTimeProvider DateTimeProvider { get; }
private MemoryStreamFactory MemoryStreamFactory { get; }
private TConfiguration Settings { get; }
public HttpTransport<TConfiguration> Transport { get; }
private TConfiguration Configuration { get; }
public ITransport<TConfiguration> RequestHandler { get; }

public override RequestPipeline Create(TConfiguration configurationValues, DateTimeProvider dateTimeProvider,
MemoryStreamFactory memoryStreamFactory, RequestParameters requestParameters) =>
new DefaultRequestPipeline<TConfiguration>(Settings, DateTimeProvider, MemoryStreamFactory, requestParameters ?? new DefaultRequestParameters());
new DefaultRequestPipeline<TConfiguration>(Configuration, DateTimeProvider, MemoryStreamFactory, requestParameters ?? new DefaultRequestParameters());
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ namespace Elastic.Transport.VirtualizedCluster.Components;
/// <summary>
/// A continuation of <see cref="VirtualCluster"/>'s builder methods that creates
/// an instance of <see cref="TransportConfiguration"/> for the cluster after which the components such as
/// <see cref="TransportClient"/> and <see cref="NodePool"/> can no longer be updated.
/// <see cref="IRequestInvoker"/> and <see cref="NodePool"/> can no longer be updated.
/// </summary>
public sealed class SealedVirtualCluster
{
private readonly TransportClient _connection;
private readonly NodePool _connectionPool;
private readonly IRequestInvoker _requestInvoker;
private readonly NodePool _nodePool;
private readonly TestableDateTimeProvider _dateTimeProvider;
private readonly MockProductRegistration _productRegistration;

internal SealedVirtualCluster(VirtualCluster cluster, NodePool pool, TestableDateTimeProvider dateTimeProvider, MockProductRegistration productRegistration)
{
_connectionPool = pool;
_connection = new VirtualClusterTransportClient(cluster, dateTimeProvider);
_nodePool = pool;
_requestInvoker = new VirtualClusterRequestInvoker(cluster, dateTimeProvider);
_dateTimeProvider = dateTimeProvider;
_productRegistration = productRegistration;
}

private TransportConfiguration CreateSettings() =>
new(_connectionPool, _connection, serializer: null, _productRegistration.ProductRegistration);
new(_nodePool, _requestInvoker, serializer: null, _productRegistration.ProductRegistration);

/// <summary> Create the cluster using all defaults on <see cref="TransportConfiguration"/> </summary>
public VirtualizedCluster AllDefaults() =>
Expand All @@ -44,7 +44,7 @@ public VirtualizedCluster Settings(Func<TransportConfiguration, TransportConfigu
/// Allows you to create an instance of `<see cref="VirtualClusterConnection"/> using the DSL provided by <see cref="Virtual"/>
/// </summary>
/// <param name="selector">Provide custom configuration options</param>
public VirtualClusterTransportClient VirtualClusterConnection(Func<TransportConfiguration, TransportConfiguration> selector = null) =>
public VirtualClusterRequestInvoker VirtualClusterConnection(Func<TransportConfiguration, TransportConfiguration> selector = null) =>
new VirtualizedCluster(_dateTimeProvider, selector == null ? CreateSettings() : selector(CreateSettings()))
.Connection;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Elastic.Transport.VirtualizedCluster.Components;

/// <summary>
/// An in memory connection that uses a rule engine to return different responses for sniffs/pings and API calls.
/// An in memory requestInvoker that uses a rule engine to return different responses for sniffs/pings and API calls.
/// <pre>
/// Either instantiate through the static <see cref="Success"/> or <see cref="Error"/> for the simplest use-cases
/// </pre>
Expand All @@ -30,7 +30,7 @@ namespace Elastic.Transport.VirtualizedCluster.Components;
/// <see cref="SealedVirtualCluster.VirtualClusterConnection"/> becomes available
/// </pre>
/// </summary>
public class VirtualClusterTransportClient : InMemoryTransportClient
public class VirtualClusterRequestInvoker : IRequestInvoker
{
private static readonly object Lock = new();

Expand All @@ -41,18 +41,23 @@ public class VirtualClusterTransportClient : InMemoryTransportClient
private MockProductRegistration _productRegistration;
private IDictionary<int, State> _calls = new Dictionary<int, State>();

internal VirtualClusterTransportClient(VirtualCluster cluster, TestableDateTimeProvider dateTimeProvider)
private readonly InMemoryRequestInvoker _inMemoryRequestInvoker;

internal VirtualClusterRequestInvoker(VirtualCluster cluster, TestableDateTimeProvider dateTimeProvider)
{
UpdateCluster(cluster);
_dateTimeProvider = dateTimeProvider;
_productRegistration = cluster.ProductRegistration;
_inMemoryRequestInvoker = new InMemoryRequestInvoker();
}

void IDisposable.Dispose() { }

/// <summary>
/// Create a <see cref="VirtualClusterTransportClient"/> instance that always returns a successful response.
/// Create a <see cref="VirtualClusterRequestInvoker"/> instance that always returns a successful response.
/// </summary>
/// <param name="response">The bytes to be returned on every API call invocation</param>
public static VirtualClusterTransportClient Success(byte[] response) =>
public static VirtualClusterRequestInvoker Success(byte[] response) =>
Virtual.Elasticsearch
.Bootstrap(1)
.ClientCalls(r => r.SucceedAlways().ReturnByteResponse(response))
Expand All @@ -61,9 +66,9 @@ public static VirtualClusterTransportClient Success(byte[] response) =>
.Connection;

/// <summary>
/// Create a <see cref="VirtualClusterTransportClient"/> instance that always returns a failed response.
/// Create a <see cref="VirtualClusterRequestInvoker"/> instance that always returns a failed response.
/// </summary>
public static VirtualClusterTransportClient Error() =>
public static VirtualClusterRequestInvoker Error() =>
Virtual.Elasticsearch
.Bootstrap(1)
.ClientCalls(r => r.FailAlways(400))
Expand Down Expand Up @@ -109,12 +114,14 @@ private void UpdateCluster(VirtualCluster cluster)

private bool IsPingRequest(RequestData requestData) => _productRegistration.IsPingRequest(requestData);

/// <inheritdoc cref="TransportClient.RequestAsync{TResponse}"/>>
public override Task<TResponse> RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken) =>
/// <inheritdoc cref="IRequestInvoker.RequestAsync{TResponse}"/>>
public Task<TResponse> RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken)
where TResponse : TransportResponse, new() =>
Task.FromResult(Request<TResponse>(requestData));

/// <inheritdoc cref="TransportClient.Request{TResponse}"/>>
public override TResponse Request<TResponse>(RequestData requestData)
/// <inheritdoc cref="IRequestInvoker.Request{TResponse}"/>>
public TResponse Request<TResponse>(RequestData requestData)
where TResponse : TransportResponse, new()
{
if (!_calls.ContainsKey(requestData.Uri.Port))
throw new Exception($"Expected a call to happen on port {requestData.Uri.Port} but received none");
Expand Down Expand Up @@ -265,7 +272,7 @@ private TResponse Fail<TResponse, TRule>(RequestData requestData, TRule rule, Ru

return ret.Match(
(e) => throw e,
(statusCode) => ReturnConnectionStatus<TResponse>(requestData, CallResponse(rule),
(statusCode) => _inMemoryRequestInvoker.BuildResponse<TResponse>(requestData, CallResponse(rule),
//make sure we never return a valid status code in Fail responses because of a bad rule.
statusCode >= 200 && statusCode < 300 ? 502 : statusCode, rule.ReturnContentType)
);
Expand All @@ -282,7 +289,7 @@ TRule rule
rule.RecordExecuted();

beforeReturn?.Invoke(rule);
return ReturnConnectionStatus<TResponse>(requestData, successResponse(rule), contentType: rule.ReturnContentType);
return _inMemoryRequestInvoker.BuildResponse<TResponse>(requestData, successResponse(rule), contentType: rule.ReturnContentType);
}

private static byte[] CallResponse<TRule>(TRule rule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class VirtualizedCluster
private readonly TestableDateTimeProvider _dateTimeProvider;
private readonly TransportConfiguration _settings;

private Func<HttpTransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, Task<TransportResponse>> _asyncCall;
private Func<HttpTransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, TransportResponse> _syncCall;
private Func<ITransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, Task<TransportResponse>> _asyncCall;
private Func<ITransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, TransportResponse> _syncCall;

private class VirtualResponse : TransportResponse { }

Expand Down Expand Up @@ -48,13 +48,13 @@ internal VirtualizedCluster(TestableDateTimeProvider dateTimeProvider, Transport
};
}

public VirtualClusterTransportClient Connection => Transport.Settings.Connection as VirtualClusterTransportClient;
public NodePool ConnectionPool => Transport.Settings.NodePool;
public HttpTransport<ITransportConfiguration> Transport => _exposingRequestPipeline?.Transport;
public VirtualClusterRequestInvoker Connection => RequestHandler.Configuration.Connection as VirtualClusterRequestInvoker;
public NodePool ConnectionPool => RequestHandler.Configuration.NodePool;
public ITransport<ITransportConfiguration> RequestHandler => _exposingRequestPipeline?.RequestHandler;

public VirtualizedCluster TransportProxiesTo(
Func<HttpTransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, TransportResponse> sync,
Func<HttpTransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, Task<TransportResponse>> async
Func<ITransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, TransportResponse> sync,
Func<ITransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor, IRequestConfiguration>, Task<TransportResponse>> async
)
{
_syncCall = sync;
Expand All @@ -63,10 +63,10 @@ Func<HttpTransport<ITransportConfiguration>, Func<RequestConfigurationDescriptor
}

public TransportResponse ClientCall(Func<RequestConfigurationDescriptor, IRequestConfiguration> requestOverrides = null) =>
_syncCall(Transport, requestOverrides);
_syncCall(RequestHandler, requestOverrides);

public async Task<TransportResponse> ClientCallAsync(Func<RequestConfigurationDescriptor, IRequestConfiguration> requestOverrides = null) =>
await _asyncCall(Transport, requestOverrides).ConfigureAwait(false);
await _asyncCall(RequestHandler, requestOverrides).ConfigureAwait(false);

public void ChangeTime(Func<DateTimeOffset, DateTimeOffset> change) => _dateTimeProvider.ChangeTime(change);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
namespace Elastic.Transport.VirtualizedCluster.Products;

/// <summary>
/// Makes sure <see cref="VirtualClusterTransportClient"/> is mockable by providing a different sniff response based on the current <see cref="ProductRegistration"/>
/// Makes sure <see cref="VirtualClusterRequestInvoker"/> is mockable by providing a different sniff response based on the current <see cref="ProductRegistration"/>
/// </summary>
public abstract class MockProductRegistration
{
/// <summary>
/// Information about the current product we are injecting into <see cref="HttpTransport{TConnectionSettings}"/>
/// Information about the current product we are injecting into <see cref="ITransport{TConfiguration}"/>
/// </summary>
public abstract ProductRegistration ProductRegistration { get; }

/// <summary>
/// Return the sniff response for the product as raw bytes for <see cref="TransportClient.Request{TResponse}"/> to return.
/// Return the sniff response for the product as raw bytes for <see cref="IRequestInvoker.Request{TResponse}"/> to return.
/// </summary>
/// <param name="nodes">The nodes we expect to be returned in the response</param>
/// <param name="stackVersion">The current version under test</param>
Expand All @@ -28,7 +28,7 @@ public abstract class MockProductRegistration
public abstract byte[] CreateSniffResponseBytes(IReadOnlyList<Node> nodes, string stackVersion, string publishAddressOverride, bool returnFullyQualifiedDomainNames);

/// <summary>
/// see <see cref="VirtualClusterTransportClient.Request{TResponse}"/> uses this to determine if the current request is a sniff request and should follow
/// see <see cref="VirtualClusterRequestInvoker.Request{TResponse}"/> uses this to determine if the current request is a sniff request and should follow
/// the sniffing rules
/// </summary>
public abstract bool IsSniffRequest(RequestData requestData);
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Components/NodePool/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Elastic.Transport;

/// <summary>
/// Represents an endpoint <see cref="Uri"/> with additional associated metadata on which the <see cref="HttpTransport{TConnectionSettings}"/> can act.
/// Represents an endpoint <see cref="Uri"/> with additional associated metadata on which the <see cref="ITransport{TConfiguration}"/> can act.
/// </summary>
public sealed class Node : IEquatable<Node>
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public IReadOnlyCollection<string> Features
public Uri Uri { get; }

/// <summary>
/// Indicates whether the node is alive. <see cref="HttpTransport{TConnectionSettings}"/> can take nodes out of rotation by calling
/// Indicates whether the node is alive. <see cref="ITransport{TConfiguration}"/> can take nodes out of rotation by calling
/// <see cref="MarkDead"/> on <see cref="Node"/>.
/// </summary>
public bool IsAlive { get; private set; }
Expand Down
8 changes: 4 additions & 4 deletions src/Elastic.Transport/Components/NodePool/NodePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Elastic.Transport;
/// <summary>
/// A node pool is responsible for maintaining a read only collection of <see cref="Node"/>(s) under <see cref="Nodes"/>.
/// <para>
/// Unlike the name might suggest this component is not responsible for IO level pooling. For that we rely on <see cref="TransportClient"/> abstracting away
/// Unlike the name might suggest this component is not responsible for IO level pooling. For that we rely on <see cref="IRequestInvoker"/> abstracting away
/// the connection IO pooling.
/// </para>
/// <para>This interface signals the current connection strategy to <see cref="HttpTransport{TConnectionSettings}"/>.</para>
/// <para>This interface signals the current connection strategy to <see cref="ITransport{TConfiguration}"/>.</para>
/// </summary>
public abstract class NodePool : IDisposable
{
Expand Down Expand Up @@ -68,7 +68,7 @@ internal NodePool() { }
public abstract bool UsingSsl { get; protected set; }

/// <summary>
///
///
/// </summary>
public void Dispose()
{
Expand All @@ -77,7 +77,7 @@ public void Dispose()
}

/// <summary>
///
///
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Elastic.Transport;

/// <summary>
/// A node pool that enables <see cref="SupportsReseeding"/> which in turn allows the <see cref="HttpTransport{TConnectionSettings}"/> to enable sniffing to
/// A node pool that enables <see cref="SupportsReseeding"/> which in turn allows the <see cref="ITransport{TConfiguration}"/> to enable sniffing to
/// discover the current cluster's list of active nodes.
/// </summary>
public class SniffingNodePool : StaticNodePool
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Components/NodePool/StaticNodePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Elastic.Transport;

/// <summary>
/// A node pool that disables <see cref="SupportsReseeding"/> which in turn disallows the <see cref="HttpTransport{TConnectionSettings}"/> to enable sniffing to
/// A node pool that disables <see cref="SupportsReseeding"/> which in turn disallows the <see cref="ITransport{TConfiguration}"/> to enable sniffing to
/// discover the current cluster's list of active nodes.
/// <para>Therefore the nodes you supply are the list of known nodes throughout its lifetime, hence static</para>
/// </summary>
Expand Down Expand Up @@ -70,7 +70,7 @@ private void Initialize(IEnumerable<Node> nodes, DateTimeProvider dateTimeProvid
}
else if (scheme != node.Uri.Scheme)
// TODO - Diagnostic event here
throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes");
throw new ArgumentException("Trying to instantiate a node pool with mixed URI Schemes");
}

InternalNodes = SortNodes(nodesProvided)
Expand Down
Loading

0 comments on commit 4d0e49d

Please sign in to comment.