From 994ff232ceaec9789ef062336820b123a9235060 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Thu, 25 Feb 2021 10:29:06 +0000 Subject: [PATCH] Add rollover API support for max_primary_shard_size --- .../RolloverIndex/RolloverConditions.cs | 16 ++ .../RolloverIndex/RolloverIndexApiTests.cs | 162 ++++++++++++++++++ 2 files changed, 178 insertions(+) diff --git a/src/Nest/Indices/IndexManagement/RolloverIndex/RolloverConditions.cs b/src/Nest/Indices/IndexManagement/RolloverIndex/RolloverConditions.cs index 90b091e2750..6f37fb10b4a 100644 --- a/src/Nest/Indices/IndexManagement/RolloverIndex/RolloverConditions.cs +++ b/src/Nest/Indices/IndexManagement/RolloverIndex/RolloverConditions.cs @@ -35,6 +35,15 @@ public interface IRolloverConditions /// [DataMember(Name ="max_size")] string MaxSize { get; set; } + + /// + /// The maximum size of the primary shards in the index e.g. "2gb" + /// + /// + /// Valid in Elasticsearch 7.12.0+ + /// + [DataMember(Name = "max_primary_shard_size")] + string MaxPrimaryShardSize { get; set; } } /// @@ -48,6 +57,9 @@ public class RolloverConditions : IRolloverConditions /// public string MaxSize { get; set; } + + /// + public string MaxPrimaryShardSize { get; set; } } /// @@ -57,6 +69,7 @@ public class RolloverConditionsDescriptor Time IRolloverConditions.MaxAge { get; set; } long? IRolloverConditions.MaxDocs { get; set; } string IRolloverConditions.MaxSize { get; set; } + string IRolloverConditions.MaxPrimaryShardSize { get; set; } /// public RolloverConditionsDescriptor MaxAge(Time maxAge) => Assign(maxAge, (a, v) => a.MaxAge = v); @@ -66,5 +79,8 @@ public class RolloverConditionsDescriptor /// public RolloverConditionsDescriptor MaxSize(string maxSize) => Assign(maxSize, (a, v) => a.MaxSize = v); + + /// + public RolloverConditionsDescriptor MaxPrimaryShardSize(string maxPrimaryShardSize) => Assign(maxPrimaryShardSize, (a, v) => a.MaxPrimaryShardSize = v); } } diff --git a/tests/Tests/Indices/IndexManagement/RolloverIndex/RolloverIndexApiTests.cs b/tests/Tests/Indices/IndexManagement/RolloverIndex/RolloverIndexApiTests.cs index ce2004dbc2b..d055e74ec32 100644 --- a/tests/Tests/Indices/IndexManagement/RolloverIndex/RolloverIndexApiTests.cs +++ b/tests/Tests/Indices/IndexManagement/RolloverIndex/RolloverIndexApiTests.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using Elastic.Elasticsearch.Xunit.XunitPlumbing; using Elasticsearch.Net; using FluentAssertions; using Nest; @@ -166,4 +167,165 @@ protected override void ExpectResponse(RolloverIndexResponse response) response.Conditions["[max_docs: 1000]"].Should().BeTrue(); } } + + [SkipVersion("<7.12.0", "Tests all parameters available in 7.12.0 and higher")] + public class RolloverIndexApiWithAllParametersTests + : ApiIntegrationTestBase + { + public RolloverIndexApiWithAllParametersTests(WritableCluster cluster, EndpointUsage usage) + : base(cluster, usage) { } + + protected override bool ExpectIsValid => true; + protected override int ExpectStatusCode => 200; + protected override HttpMethod HttpMethod => HttpMethod.POST; + protected override bool SupportsDeserialization => false; + protected override string UrlPath => $"/{CallIsolatedValue}-alias/_rollover/{CallIsolatedValue}-new"; + + protected override object ExpectJson => new + { + conditions = new + { + max_age = "7d", + max_docs = 1000, + max_size = "5gb", + max_primary_shard_size = "2gb" + }, + settings = new Dictionary + { + { "index.number_of_shards", 1 }, + { "index.number_of_replicas", 1 } + }, + mappings = new + { + properties = new + { + branches = new + { + type = "text", + fields = new + { + keyword = new + { + type = "keyword", + ignore_above = 256 + } + } + } + } + }, + aliases = new Dictionary + { + { CallIsolatedValue + "-new_projects", new { } } + } + }; + + protected override Func Fluent => f => f + .NewIndex(CallIsolatedValue + "-new") + .Conditions(c => c + .MaxAge("7d") + .MaxDocs(1000) + .MaxSize("5gb") + .MaxPrimaryShardSize("2gb") + ) + .Settings(s => s + .NumberOfShards(1) + .NumberOfReplicas(1) + ) + .Map(p => p + .Properties(pp => pp + .Text(t => t + .Name(n => n.Branches) + .Fields(pf => pf + .Keyword(k => k + .Name("keyword") + .IgnoreAbove(256) + ) + ) + ) + ) + ) + .Aliases(a => a + .Alias(CallIsolatedValue + "-new_projects") + ); + + protected override RolloverIndexRequest Initializer => new RolloverIndexRequest(CallIsolatedValue + "-alias", CallIsolatedValue + "-new") + { + Conditions = new RolloverConditions + { + MaxAge = "7d", + MaxDocs = 1000, + MaxSize = "5gb", + MaxPrimaryShardSize = "2gb" + }, + Settings = new Nest.IndexSettings + { + NumberOfShards = 1, + NumberOfReplicas = 1 + }, + Mappings = new TypeMapping + { + Properties = new Properties + { + { + p => p.Branches, new TextProperty + { + Fields = new Properties + { + { + "keyword", new KeywordProperty + { + IgnoreAbove = 256 + } + } + } + } + } + } + }, + Aliases = new Aliases + { + { CallIsolatedValue + "-new_projects", new Alias() } + } + }; + + protected override void OnBeforeCall(IElasticClient client) + { + var create = client.Indices.Create(CallIsolatedValue, c => c + .Aliases(a => a + .Alias(CallIsolatedValue + "-alias") + ) + ); + create.ShouldBeValid(); + var someDocs = client.Bulk(b => b + .Index(CallIsolatedValue) + .Refresh(Refresh.True) + .IndexMany(Project.Generator.Generate(1200)) + ); + someDocs.ShouldBeValid(); + + } + + protected override LazyResponses ClientUsage() => Calls( + (client, f) => client.Indices.Rollover(CallIsolatedValue + "-alias", f), + (client, f) => client.Indices.RolloverAsync(CallIsolatedValue + "-alias", f), + (client, r) => client.Indices.Rollover(r), + (client, r) => client.Indices.RolloverAsync(r) + ); + + protected override RolloverIndexDescriptor NewDescriptor() => new RolloverIndexDescriptor(CallIsolatedValue + "-alias"); + + protected override void ExpectResponse(RolloverIndexResponse response) + { + response.ShouldBeValid(); + response.OldIndex.Should().NotBeNullOrEmpty(); + response.NewIndex.Should().NotBeNullOrEmpty(); + response.RolledOver.Should().BeTrue(); + response.ShardsAcknowledged.Should().BeTrue(); + response.Conditions.Should().NotBeNull().And.HaveCount(4); + response.Conditions["[max_age: 7d]"].Should().BeFalse(); + response.Conditions["[max_docs: 1000]"].Should().BeTrue(); + response.Conditions["[max_size: 5gb]"].Should().BeFalse(); + response.Conditions["[max_primary_shard_size: 2gb]"].Should().BeFalse(); + } + } }