Skip to content

Commit 804393a

Browse files
authored
Refactor product check changes to avoid binary breaks (#5918)
* Refactor to remove need to add optional params * Remove binary breaking changes for release * Update http connections
1 parent 770b19c commit 804393a

File tree

11 files changed

+265
-214
lines changed

11 files changed

+265
-214
lines changed

src/Elasticsearch.Net.VirtualizedCluster/VirtualCluster.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class VirtualCluster
1515
{
1616
private readonly List<Node> _nodes;
1717

18-
public VirtualCluster(IEnumerable<Node> nodes, bool productCheckSucceeds = true)
18+
public VirtualCluster(IEnumerable<Node> nodes) : this(nodes, true) { }
19+
20+
public VirtualCluster(IEnumerable<Node> nodes, bool productCheckSucceeds)
1921
{
2022
_nodes = nodes.ToList();
2123

@@ -29,19 +31,20 @@ public VirtualCluster(IEnumerable<Node> nodes, bool productCheckSucceeds = true)
2931
public IReadOnlyList<Node> Nodes => _nodes;
3032

3133
public List<IRule> PingingRules { get; } = new();
32-
public List<ISniffRule> SniffingRules { get; } = new();
3334
public List<IRule> ProductCheckRules { get; } = new();
35+
public List<ISniffRule> SniffingRules { get; } = new();
36+
internal string ElasticsearchVersion { get; private set; } = "7.0.0";
3437

3538
internal string PublishAddressOverride { get; private set; }
3639

3740
internal bool SniffShouldReturnFqnd { get; private set; }
38-
internal string ElasticsearchVersion { get; private set; } = "7.0.0";
3941

4042
public VirtualCluster SniffShouldReturnFqdn()
4143
{
4244
SniffShouldReturnFqnd = true;
4345
return this;
4446
}
47+
4548
public VirtualCluster SniffElasticsearchVersionNumber(string version)
4649
{
4750
ElasticsearchVersion = version;
@@ -93,7 +96,7 @@ public VirtualCluster Sniff(Func<SniffRule, ISniffRule> selector)
9396
SniffingRules.Add(selector(new SniffRule()));
9497
return this;
9598
}
96-
99+
97100
public VirtualCluster ProductCheck(Func<ProductCheckRule, IRule> selector)
98101
{
99102
ProductCheckRules.Add(selector(new ProductCheckRule()));

src/Elasticsearch.Net.VirtualizedCluster/VirtualClusterWith.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ namespace Elasticsearch.Net.VirtualizedCluster
1010
{
1111
public static class VirtualClusterWith
1212
{
13-
public static VirtualCluster Nodes(int numberOfNodes, int startFrom = 9200, bool productCheckAlwaysSucceeds = true) =>
14-
new (Enumerable.Range(startFrom, numberOfNodes).Select(n => new Node(new Uri($"http://localhost:{n}"))), productCheckAlwaysSucceeds);
13+
public static VirtualCluster Nodes(int numberOfNodes, int startFrom = 9200) => Nodes(numberOfNodes, true, startFrom);
1514

16-
public static VirtualCluster MasterOnlyNodes(int numberOfNodes, int startFrom = 9200, bool productCheckSucceeds = true) =>
17-
new (Enumerable.Range(startFrom, numberOfNodes).Select(n => new Node(new Uri($"http://localhost:{n}")) { HoldsData = false, MasterEligible = true }), productCheckSucceeds);
15+
public static VirtualCluster Nodes(int numberOfNodes, bool productCheckSucceeds, int startFrom = 9200) =>
16+
new(Enumerable.Range(startFrom, numberOfNodes).Select(n => new Node(new Uri($"http://localhost:{n}"))), productCheckSucceeds);
1817

19-
public static VirtualCluster Nodes(IEnumerable<Node> nodes, bool productCheckSucceeds = true) => new (nodes, productCheckSucceeds);
18+
public static VirtualCluster MasterOnlyNodes(int numberOfNodes, int startFrom = 9200) => MasterOnlyNodes(numberOfNodes, true, startFrom);
19+
20+
public static VirtualCluster MasterOnlyNodes(int numberOfNodes, bool productCheckSucceeds, int startFrom = 9200) =>
21+
new(Enumerable.Range(startFrom, numberOfNodes)
22+
.Select(n => new Node(new Uri($"http://localhost:{n}")) { HoldsData = false, MasterEligible = true }), productCheckSucceeds);
23+
24+
public static VirtualCluster Nodes(IEnumerable<Node> nodes) => Nodes(nodes, true);
25+
26+
public static VirtualCluster Nodes(IEnumerable<Node> nodes, bool productCheckSucceeds) => new(nodes, productCheckSucceeds);
2027
}
2128
}

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,22 @@ internal class WebProxy : IWebProxy
4040
/// <summary> The default IConnection implementation. Uses <see cref="HttpClient" />.</summary>
4141
public class HttpConnection : IConnection
4242
{
43-
private static DiagnosticSource DiagnosticSource { get; } = new DiagnosticListener(DiagnosticSources.HttpConnection.SourceName);
44-
4543
private static readonly string MissingConnectionLimitMethodError =
4644
$"Your target platform does not support {nameof(ConnectionConfiguration.ConnectionLimit)}"
4745
+ $" please set {nameof(ConnectionConfiguration.ConnectionLimit)} to -1 on your connection configuration/settings."
4846
+ $" this will cause the {nameof(HttpClientHandler.MaxConnectionsPerServer)} not to be set on {nameof(HttpClientHandler)}";
4947

50-
private RequestDataHttpClientFactory HttpClientFactory { get; }
51-
5248
[Obsolete("HttpConnection now uses a HttpClientFactory implementation to manage HttpClient and HttpMessageHandler instances. "
5349
+ "This property is no longer used and will be removed in the next major release")]
5450
protected readonly ConcurrentDictionary<int, HttpClient> Clients = new ConcurrentDictionary<int, HttpClient>();
5551

52+
public HttpConnection() => HttpClientFactory = new RequestDataHttpClientFactory(r => CreateHttpClientHandler(r));
53+
5654
public int InUseHandlers => HttpClientFactory.InUseHandlers;
5755
public int RemovedHandlers => HttpClientFactory.RemovedHandlers;
56+
private static DiagnosticSource DiagnosticSource { get; } = new DiagnosticListener(DiagnosticSources.HttpConnection.SourceName);
5857

59-
public HttpConnection() => HttpClientFactory = new RequestDataHttpClientFactory(r => CreateHttpClientHandler(r));
58+
private RequestDataHttpClientFactory HttpClientFactory { get; }
6059

6160
public virtual TResponse Request<TResponse>(RequestData requestData)
6261
where TResponse : class, IElasticsearchResponse, new()
@@ -80,7 +79,7 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
8079
if (requestData.PostData != null)
8180
SetContent(requestMessage, requestData);
8281

83-
using(requestMessage?.Content ?? (IDisposable)Stream.Null)
82+
using (requestMessage?.Content ?? (IDisposable)Stream.Null)
8483
using (var d = DiagnosticSource.Diagnose<RequestData, int?>(DiagnosticSources.HttpConnection.SendAndReceiveHeaders, requestData))
8584
{
8685
if (requestData.TcpStats)
@@ -113,10 +112,11 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
113112
{
114113
ex = e;
115114
}
116-
using(receive)
115+
using (receive)
117116
using (responseStream ??= Stream.Null)
118117
{
119-
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames?.FirstOrDefault());
118+
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream,
119+
productNames?.FirstOrDefault(), mimeType);
120120

121121
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
122122
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
@@ -150,7 +150,7 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
150150
if (requestData.PostData != null)
151151
await SetContentAsync(requestMessage, requestData, cancellationToken).ConfigureAwait(false);
152152

153-
using(requestMessage?.Content ?? (IDisposable)Stream.Null)
153+
using (requestMessage?.Content ?? (IDisposable)Stream.Null)
154154
using (var d = DiagnosticSource.Diagnose<RequestData, int?>(DiagnosticSources.HttpConnection.SendAndReceiveHeaders, requestData))
155155
{
156156
if (requestData.TcpStats)
@@ -159,7 +159,8 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
159159
if (requestData.ThreadPoolStats)
160160
threadPoolStats = ThreadPoolStats.GetStats();
161161

162-
responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
162+
responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
163+
.ConfigureAwait(false);
163164
statusCode = (int)responseMessage.StatusCode;
164165
d.EndState = statusCode;
165166
}
@@ -187,7 +188,7 @@ public virtual async Task<TResponse> RequestAsync<TResponse>(RequestData request
187188
using (responseStream ??= Stream.Null)
188189
{
189190
var response = await ResponseBuilder.ToResponseAsync<TResponse>
190-
(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames?.FirstOrDefault(), cancellationToken)
191+
(requestData, ex, statusCode, warnings, responseStream, productNames?.FirstOrDefault(), mimeType, cancellationToken)
191192
.ConfigureAwait(false);
192193

193194
// set TCP and thread pool stats on the response here so that in the event the request fails after the point of
@@ -209,7 +210,6 @@ protected virtual HttpMessageHandler CreateHttpClientHandler(RequestData request
209210

210211
// same limit as desktop clr
211212
if (requestData.ConnectionSettings.ConnectionLimit > 0)
212-
{
213213
try
214214
{
215215
handler.MaxConnectionsPerServer = requestData.ConnectionSettings.ConnectionLimit;
@@ -222,7 +222,6 @@ protected virtual HttpMessageHandler CreateHttpClientHandler(RequestData request
222222
{
223223
throw new Exception(MissingConnectionLimitMethodError, e);
224224
}
225-
}
226225

227226
if (!requestData.ProxyAddress.IsNullOrEmpty())
228227
{
@@ -291,7 +290,6 @@ protected virtual bool SetApiKeyAuthenticationIfNeeded(HttpRequestMessage reques
291290

292291
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("ApiKey", apiKey);
293292
return true;
294-
295293
}
296294

297295
// TODO - make private in 8.0 and only expose SetAuthenticationIfNeeded
@@ -401,7 +399,7 @@ private static async Task SetContentAsync(HttpRequestMessage message, RequestDat
401399
await stream.DisposeAsync().ConfigureAwait(false);
402400

403401
#else
404-
stream.Dispose();
402+
stream.Dispose();
405403
#endif
406404
}
407405
else

src/Elasticsearch.Net/Connection/HttpWebRequestConnection.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,12 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
5050
var data = requestData.PostData;
5151

5252
if (data != null)
53-
{
5453
using (var stream = request.GetRequestStream())
55-
{
5654
if (requestData.HttpCompression)
57-
{
5855
using (var zipStream = new GZipStream(stream, CompressionMode.Compress))
5956
data.Write(zipStream, requestData.ConnectionSettings);
60-
}
6157
else
6258
data.Write(stream, requestData.ConnectionSettings);
63-
}
64-
}
6559
requestData.MadeItToResponse = true;
6660

6761
if (requestData.TcpStats)
@@ -94,8 +88,8 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
9488
}
9589

9690
responseStream ??= Stream.Null;
97-
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream, mimeType,
98-
productNames?.FirstOrDefault());
91+
var response = ResponseBuilder.ToResponse<TResponse>(requestData, ex, statusCode, warnings, responseStream,
92+
productNames?.FirstOrDefault(), mimeType);
9993

10094
// set TCP and threadpool stats on the response here so that in the event the request fails after the point of
10195
// gathering stats, they are still exposed on the call details. Ideally these would be set inside ResponseBuilder.ToResponse,
@@ -134,15 +128,11 @@ CancellationToken cancellationToken
134128
unregisterWaitHandle = RegisterApmTaskTimeout(apmGetRequestStreamTask, request, requestData);
135129

136130
using (var stream = await apmGetRequestStreamTask.ConfigureAwait(false))
137-
{
138131
if (requestData.HttpCompression)
139-
{
140132
using (var zipStream = new GZipStream(stream, CompressionMode.Compress))
141133
await data.WriteAsync(zipStream, requestData.ConnectionSettings, cancellationToken).ConfigureAwait(false);
142-
}
143134
else
144135
await data.WriteAsync(stream, requestData.ConnectionSettings, cancellationToken).ConfigureAwait(false);
145-
}
146136
unregisterWaitHandle?.Invoke();
147137
}
148138
requestData.MadeItToResponse = true;
@@ -183,7 +173,7 @@ CancellationToken cancellationToken
183173
}
184174
responseStream ??= Stream.Null;
185175
var response = await ResponseBuilder.ToResponseAsync<TResponse>
186-
(requestData, ex, statusCode, warnings, responseStream, mimeType, productNames?.FirstOrDefault(), cancellationToken)
176+
(requestData, ex, statusCode, warnings, responseStream, productNames?.FirstOrDefault(), mimeType, cancellationToken)
187177
.ConfigureAwait(false);
188178

189179
// set TCP and thread pool stats on the response here so that in the event the request fails after the point of
@@ -329,10 +319,8 @@ protected virtual void SetBasicAuthenticationIfNeeded(HttpWebRequest request, Re
329319
if (!string.IsNullOrEmpty(requestData.Uri.UserInfo))
330320
userInfo = Uri.UnescapeDataString(requestData.Uri.UserInfo);
331321
else if (requestData.BasicAuthorizationCredentials != null)
332-
{
333322
userInfo =
334323
$"{requestData.BasicAuthorizationCredentials.Username}:{requestData.BasicAuthorizationCredentials.Password.CreateString()}";
335-
}
336324

337325
if (string.IsNullOrWhiteSpace(userInfo))
338326
return;

0 commit comments

Comments
 (0)