|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Elastic.Elasticsearch.Xunit.XunitPlumbing; |
| 7 | +using Elasticsearch.Net.VirtualizedCluster; |
| 8 | +using Elasticsearch.Net.VirtualizedCluster.Audit; |
| 9 | +using Elasticsearch.Net.VirtualizedCluster.Rules; |
| 10 | +using static Elasticsearch.Net.AuditEvent; |
| 11 | + |
| 12 | +namespace Tests.ClientConcepts.ConnectionPooling.ProductChecking |
| 13 | +{ |
| 14 | + public class ProductCheckAtStartup |
| 15 | + { |
| 16 | + /**=== Product check on first usage |
| 17 | + * |
| 18 | + * Since 7.14.0, the client performs a required product check during the first call. |
| 19 | + * This pre-flight product check allows us to establish the version of Elasticsearch we are communicating with. |
| 20 | + * |
| 21 | + * The product check requires one additional HTTP request to be sent to the server as part of the request pipeline. |
| 22 | + * Once the product check succeeds, no further product check HTTP requests are sent as part of the pipeline. |
| 23 | + */ |
| 24 | + [U] public async Task ProductCheckPerformedOnlyOnFirstCallWhenSuccessful() |
| 25 | + { |
| 26 | + var audit = new Auditor(() => VirtualClusterWith |
| 27 | + .Nodes(1) |
| 28 | + .ClientCalls(r => r.SucceedAlways()) |
| 29 | + .StaticConnectionPool() |
| 30 | + .Settings(s => s.DisablePing()) |
| 31 | + ); |
| 32 | + |
| 33 | + audit = await audit.TraceCalls(skipProductCheck: false, |
| 34 | + new ClientCall() { |
| 35 | + { ProductCheckOnStartup }, |
| 36 | + { ProductCheckSuccess, 9200 }, // <1> as this is the first call, the product check is executed |
| 37 | + { HealthyResponse, 9200 } // <2> following the product check, the actual request is sent |
| 38 | + }, |
| 39 | + new ClientCall() { |
| 40 | + { HealthyResponse, 9200 } // <3> subsequent calls no longer perform product check |
| 41 | + } |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + [U] |
| 46 | + public async Task ProductCheckPerformedOnSecondCallWhenFirstCheckFails() |
| 47 | + { |
| 48 | + /** Here's an example with a single node cluster which fails for some reason during the first product check attempt. */ |
| 49 | + var audit = new Auditor(() => VirtualClusterWith |
| 50 | + .Nodes(1, productCheckAlwaysSucceeds: false) |
| 51 | + .ProductCheck(r => r.Fails(TimesHelper.Once)) |
| 52 | + .ProductCheck(r => r.SucceedAlways()) |
| 53 | + .ClientCalls(r => r.SucceedAlways()) |
| 54 | + .StaticConnectionPool() |
| 55 | + .Settings(s => s.DisablePing()) |
| 56 | + ); |
| 57 | + |
| 58 | + audit = await audit.TraceCalls(skipProductCheck: false, |
| 59 | + new ClientCall() { |
| 60 | + { ProductCheckOnStartup }, |
| 61 | + { ProductCheckFailure, 9200 }, // <1> as this is the first call, the product check is executed, but fails |
| 62 | + { HealthyResponse, 9200 } // <2> the actual request is still sent and succeeds |
| 63 | + }, |
| 64 | + new ClientCall() { |
| 65 | + { ProductCheckOnStartup }, |
| 66 | + { ProductCheckSuccess, 9200 }, // <3> as the previous product check failed, it runs on the second call |
| 67 | + { HealthyResponse, 9200 } |
| 68 | + }, |
| 69 | + new ClientCall() { |
| 70 | + { HealthyResponse, 9200 } // <4> subsequent calls no longer perform product check |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + [U] |
| 76 | + public async Task ProductCheckAttemptsAllNodes() |
| 77 | + { |
| 78 | + /** Here's an example with a three node cluster which fails for some reason during the first and second product check attempts. */ |
| 79 | + var audit = new Auditor(() => VirtualClusterWith |
| 80 | + .Nodes(3, productCheckAlwaysSucceeds: false) |
| 81 | + .ProductCheck(r => r.FailAlways()) |
| 82 | + .ProductCheck(r => r.OnPort(9202).SucceedAlways()) |
| 83 | + .ClientCalls(r => r.SucceedAlways()) |
| 84 | + .StaticConnectionPool() |
| 85 | + .Settings(s => s.DisablePing()) |
| 86 | + ); |
| 87 | + |
| 88 | + audit = await audit.TraceCalls(skipProductCheck: false, |
| 89 | + new ClientCall() { |
| 90 | + { ProductCheckOnStartup }, |
| 91 | + { ProductCheckFailure, 9200 }, // <1> this is the first call, the product check is executed, but fails on this node |
| 92 | + { ProductCheckFailure, 9201 }, // <2> the next node is also tried and fails |
| 93 | + { ProductCheckSuccess, 9202 }, // <3> the third node is tried, successfully responds and the product check succeeds |
| 94 | + { HealthyResponse, 9200 } // <4> the actual request is sent and succeeds |
| 95 | + }, |
| 96 | + new ClientCall() { |
| 97 | + { HealthyResponse, 9201 } // <5> subsequent calls no longer perform product check |
| 98 | + } |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments