Skip to content

Commit 140ca8e

Browse files
authored
Add rollover API support for max_primary_shard_size (#5353) (#5368)
(cherry picked from commit 2dbaf16)
1 parent 219d7cd commit 140ca8e

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

src/Nest/Indices/IndexManagement/RolloverIndex/RolloverConditions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public interface IRolloverConditions
3535
/// </remarks>
3636
[DataMember(Name ="max_size")]
3737
string MaxSize { get; set; }
38+
39+
/// <summary>
40+
/// The maximum size of the primary shards in the index e.g. "2gb"
41+
/// </summary>
42+
/// <remarks>
43+
/// Valid in Elasticsearch 7.12.0+
44+
/// </remarks>
45+
[DataMember(Name = "max_primary_shard_size")]
46+
string MaxPrimaryShardSize { get; set; }
3847
}
3948

4049
/// <inheritdoc />
@@ -48,6 +57,9 @@ public class RolloverConditions : IRolloverConditions
4857

4958
/// <inheritdoc />
5059
public string MaxSize { get; set; }
60+
61+
/// <inheritdoc />
62+
public string MaxPrimaryShardSize { get; set; }
5163
}
5264

5365
/// <inheritdoc cref="IRolloverConditions" />
@@ -57,6 +69,7 @@ public class RolloverConditionsDescriptor
5769
Time IRolloverConditions.MaxAge { get; set; }
5870
long? IRolloverConditions.MaxDocs { get; set; }
5971
string IRolloverConditions.MaxSize { get; set; }
72+
string IRolloverConditions.MaxPrimaryShardSize { get; set; }
6073

6174
/// <inheritdoc cref="IRolloverConditions.MaxAge" />
6275
public RolloverConditionsDescriptor MaxAge(Time maxAge) => Assign(maxAge, (a, v) => a.MaxAge = v);
@@ -66,5 +79,8 @@ public class RolloverConditionsDescriptor
6679

6780
/// <inheritdoc cref="IRolloverConditions.MaxSize" />
6881
public RolloverConditionsDescriptor MaxSize(string maxSize) => Assign(maxSize, (a, v) => a.MaxSize = v);
82+
83+
/// <inheritdoc cref="IRolloverConditions.MaxPrimaryShardSize" />
84+
public RolloverConditionsDescriptor MaxPrimaryShardSize(string maxPrimaryShardSize) => Assign(maxPrimaryShardSize, (a, v) => a.MaxPrimaryShardSize = v);
6985
}
7086
}

tests/Tests/Indices/IndexManagement/RolloverIndex/RolloverIndexApiTests.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using Elastic.Transport;
8+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
89
using Elasticsearch.Net;
910
using FluentAssertions;
1011
using Nest;
@@ -167,4 +168,165 @@ protected override void ExpectResponse(RolloverIndexResponse response)
167168
response.Conditions["[max_docs: 1000]"].Should().BeTrue();
168169
}
169170
}
171+
172+
[SkipVersion("<7.12.0", "Tests all parameters available in 7.12.0 and higher")]
173+
public class RolloverIndexApiWithAllParametersTests
174+
: ApiIntegrationTestBase<WritableCluster, RolloverIndexResponse, IRolloverIndexRequest, RolloverIndexDescriptor, RolloverIndexRequest>
175+
{
176+
public RolloverIndexApiWithAllParametersTests(WritableCluster cluster, EndpointUsage usage)
177+
: base(cluster, usage) { }
178+
179+
protected override bool ExpectIsValid => true;
180+
protected override int ExpectStatusCode => 200;
181+
protected override HttpMethod HttpMethod => HttpMethod.POST;
182+
protected override bool SupportsDeserialization => false;
183+
protected override string UrlPath => $"/{CallIsolatedValue}-alias/_rollover/{CallIsolatedValue}-new";
184+
185+
protected override object ExpectJson => new
186+
{
187+
conditions = new
188+
{
189+
max_age = "7d",
190+
max_docs = 1000,
191+
max_size = "5gb",
192+
max_primary_shard_size = "2gb"
193+
},
194+
settings = new Dictionary<string, object>
195+
{
196+
{ "index.number_of_shards", 1 },
197+
{ "index.number_of_replicas", 1 }
198+
},
199+
mappings = new
200+
{
201+
properties = new
202+
{
203+
branches = new
204+
{
205+
type = "text",
206+
fields = new
207+
{
208+
keyword = new
209+
{
210+
type = "keyword",
211+
ignore_above = 256
212+
}
213+
}
214+
}
215+
}
216+
},
217+
aliases = new Dictionary<string, object>
218+
{
219+
{ CallIsolatedValue + "-new_projects", new { } }
220+
}
221+
};
222+
223+
protected override Func<RolloverIndexDescriptor, IRolloverIndexRequest> Fluent => f => f
224+
.NewIndex(CallIsolatedValue + "-new")
225+
.Conditions(c => c
226+
.MaxAge("7d")
227+
.MaxDocs(1000)
228+
.MaxSize("5gb")
229+
.MaxPrimaryShardSize("2gb")
230+
)
231+
.Settings(s => s
232+
.NumberOfShards(1)
233+
.NumberOfReplicas(1)
234+
)
235+
.Map<Project>(p => p
236+
.Properties(pp => pp
237+
.Text(t => t
238+
.Name(n => n.Branches)
239+
.Fields(pf => pf
240+
.Keyword(k => k
241+
.Name("keyword")
242+
.IgnoreAbove(256)
243+
)
244+
)
245+
)
246+
)
247+
)
248+
.Aliases(a => a
249+
.Alias(CallIsolatedValue + "-new_projects")
250+
);
251+
252+
protected override RolloverIndexRequest Initializer => new RolloverIndexRequest(CallIsolatedValue + "-alias", CallIsolatedValue + "-new")
253+
{
254+
Conditions = new RolloverConditions
255+
{
256+
MaxAge = "7d",
257+
MaxDocs = 1000,
258+
MaxSize = "5gb",
259+
MaxPrimaryShardSize = "2gb"
260+
},
261+
Settings = new Nest.IndexSettings
262+
{
263+
NumberOfShards = 1,
264+
NumberOfReplicas = 1
265+
},
266+
Mappings = new TypeMapping
267+
{
268+
Properties = new Properties<Project>
269+
{
270+
{
271+
p => p.Branches, new TextProperty
272+
{
273+
Fields = new Properties
274+
{
275+
{
276+
"keyword", new KeywordProperty
277+
{
278+
IgnoreAbove = 256
279+
}
280+
}
281+
}
282+
}
283+
}
284+
}
285+
},
286+
Aliases = new Aliases
287+
{
288+
{ CallIsolatedValue + "-new_projects", new Alias() }
289+
}
290+
};
291+
292+
protected override void OnBeforeCall(IElasticClient client)
293+
{
294+
var create = client.Indices.Create(CallIsolatedValue, c => c
295+
.Aliases(a => a
296+
.Alias(CallIsolatedValue + "-alias")
297+
)
298+
);
299+
create.ShouldBeValid();
300+
var someDocs = client.Bulk(b => b
301+
.Index(CallIsolatedValue)
302+
.Refresh(Refresh.True)
303+
.IndexMany(Project.Generator.Generate(1200))
304+
);
305+
someDocs.ShouldBeValid();
306+
307+
}
308+
309+
protected override LazyResponses ClientUsage() => Calls(
310+
(client, f) => client.Indices.Rollover(CallIsolatedValue + "-alias", f),
311+
(client, f) => client.Indices.RolloverAsync(CallIsolatedValue + "-alias", f),
312+
(client, r) => client.Indices.Rollover(r),
313+
(client, r) => client.Indices.RolloverAsync(r)
314+
);
315+
316+
protected override RolloverIndexDescriptor NewDescriptor() => new RolloverIndexDescriptor(CallIsolatedValue + "-alias");
317+
318+
protected override void ExpectResponse(RolloverIndexResponse response)
319+
{
320+
response.ShouldBeValid();
321+
response.OldIndex.Should().NotBeNullOrEmpty();
322+
response.NewIndex.Should().NotBeNullOrEmpty();
323+
response.RolledOver.Should().BeTrue();
324+
response.ShardsAcknowledged.Should().BeTrue();
325+
response.Conditions.Should().NotBeNull().And.HaveCount(4);
326+
response.Conditions["[max_age: 7d]"].Should().BeFalse();
327+
response.Conditions["[max_docs: 1000]"].Should().BeTrue();
328+
response.Conditions["[max_size: 5gb]"].Should().BeFalse();
329+
response.Conditions["[max_primary_shard_size: 2gb]"].Should().BeFalse();
330+
}
331+
}
170332
}

0 commit comments

Comments
 (0)