Skip to content

Commit 326f0de

Browse files
authored
Implement setting snapshot retention configuration on SLM lifecycles. (#4261)
1 parent 48cdd63 commit 326f0de

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

src/Nest/XPack/Slm/PutLifecycle/PutSnapshotLifecycleRequest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public partial interface IPutSnapshotLifecycleRequest
3535
/// </summary>
3636
[DataMember(Name = "schedule")]
3737
CronExpression Schedule { get; set; }
38+
39+
/// <summary>
40+
/// Automatic deletion of older snapshots is an optional feature of snapshot lifecycle management. Retention is run as a cluster level task
41+
/// that is not associated with a particular policy’s schedule (though the configuration of which snapshots to keep is done on a
42+
/// per-policy basis).
43+
/// </summary>
44+
[DataMember(Name = "retention")]
45+
ISnapshotRetentionConfiguration Retention { get; set; }
3846
}
3947

4048
public partial class PutSnapshotLifecycleRequest
@@ -50,6 +58,9 @@ public partial class PutSnapshotLifecycleRequest
5058

5159
/// <inheritdoc />
5260
public CronExpression Schedule { get; set; }
61+
62+
/// <inheritdoc />
63+
public ISnapshotRetentionConfiguration Retention { get; set; }
5364
}
5465

5566
public partial class PutSnapshotLifecycleDescriptor
@@ -59,6 +70,8 @@ public partial class PutSnapshotLifecycleDescriptor
5970
string IPutSnapshotLifecycleRequest.Repository { get; set; }
6071
CronExpression IPutSnapshotLifecycleRequest.Schedule { get; set; }
6172

73+
ISnapshotRetentionConfiguration IPutSnapshotLifecycleRequest.Retention { get; set; }
74+
6275
/// <inheritdoc cref="IPutSnapshotLifecycleRequest.Name" />
6376
public PutSnapshotLifecycleDescriptor Name(string name) => Assign(name, (a, v) => a.Name = v);
6477

@@ -71,5 +84,9 @@ public partial class PutSnapshotLifecycleDescriptor
7184
/// <inheritdoc cref="IPutSnapshotLifecycleRequest.Config" />
7285
public PutSnapshotLifecycleDescriptor Config(Func<SnapshotLifecycleConfigDescriptor, ISnapshotLifecycleConfig> selector) =>
7386
Assign(selector, (a, v) => a.Config = v.InvokeOrDefault(new SnapshotLifecycleConfigDescriptor()));
87+
88+
/// <inheritdoc cref="IPutSnapshotLifecycleRequest.Retention" />
89+
public PutSnapshotLifecycleDescriptor Retention(Func<SnapshotRetentionConfigurationDescriptor, ISnapshotRetentionConfiguration> selector) =>
90+
Assign(selector, (a, v) => a.Retention = v.InvokeOrDefault(new SnapshotRetentionConfigurationDescriptor()));
7491
}
7592
}

src/Nest/XPack/Slm/SnapshotLifecycleConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public interface ISnapshotLifecycleConfig
2424
Indices Indices { get; set; }
2525
}
2626

27-
2827
public class SnapshotLifecycleConfig : ISnapshotLifecycleConfig
2928
{
3029
/// <inheritdoc />

src/Nest/XPack/Slm/SnapshotLifecyclePolicy.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class SnapshotLifecyclePolicy
2020

2121
/// <inheritdoc />
2222
public CronExpression Schedule { get; set; }
23+
24+
/// <inheritdoc />
25+
public ISnapshotRetentionConfiguration Retention { get; set; }
2326
}
2427

2528
/// <inheritdoc />
@@ -54,5 +57,13 @@ public interface ISnapshotLifecyclePolicy
5457
/// </summary>
5558
[DataMember(Name = "schedule")]
5659
CronExpression Schedule { get; set; }
60+
61+
/// <summary>
62+
/// Automatic deletion of older snapshots is an optional feature of snapshot lifecycle management. Retention is run as a cluster level task
63+
/// that is not associated with a particular policy’s schedule (though the configuration of which snapshots to keep is done on a
64+
/// per-policy basis).
65+
/// </summary>
66+
[DataMember(Name = "retention")]
67+
ISnapshotRetentionConfiguration Retention { get; set; }
5768
}
5869
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Runtime.Serialization;
2+
using Elasticsearch.Net.Utf8Json;
3+
4+
namespace Nest
5+
{
6+
/// <summary>
7+
/// A snapshot lifecycle retention configuration
8+
/// </summary>
9+
[ReadAs(typeof(SnapshotRetentionConfig))]
10+
[InterfaceDataContract]
11+
public interface ISnapshotRetentionConfiguration
12+
{
13+
/// <summary>
14+
/// How old a snapshot must be in order to be eligible for deletion.
15+
/// </summary>
16+
[DataMember(Name = "expire_after")]
17+
Time ExpireAfter { get; set; }
18+
19+
/// <summary>
20+
/// A minimum number of snapshots to keep, regardless of age.
21+
/// </summary>
22+
[DataMember(Name = "min_count")]
23+
int? MinimumCount { get; set; }
24+
25+
/// <summary>
26+
/// The maximum number of snapshots to keep, regardless of age. If the number of snapshots in the repository exceeds this limit, the
27+
/// policy retains the most recent snapshots and deletes older snapshots.
28+
/// </summary>
29+
[DataMember(Name = "max_count")]
30+
int? MaximumCount { get; set; }
31+
}
32+
33+
public class SnapshotRetentionConfig : ISnapshotRetentionConfiguration
34+
{
35+
/// <inheritdoc />
36+
public Time ExpireAfter { get; set; }
37+
38+
/// <inheritdoc />
39+
public int? MinimumCount { get; set; }
40+
41+
/// <inheritdoc />
42+
public int? MaximumCount { get; set; }
43+
}
44+
45+
public class SnapshotRetentionConfigurationDescriptor : DescriptorBase<SnapshotRetentionConfigurationDescriptor, ISnapshotRetentionConfiguration>, ISnapshotRetentionConfiguration
46+
{
47+
Time ISnapshotRetentionConfiguration.ExpireAfter { get; set; }
48+
49+
int? ISnapshotRetentionConfiguration.MinimumCount { get; set; }
50+
51+
int? ISnapshotRetentionConfiguration.MaximumCount { get; set; }
52+
53+
/// <inheritdoc cref="ISnapshotRetentionConfiguration.ExpireAfter" />
54+
public SnapshotRetentionConfigurationDescriptor ExpireAfter(Time time) => Assign(time, (a, v) => a.ExpireAfter = v);
55+
56+
/// <inheritdoc cref="ISnapshotRetentionConfiguration.MinimumCount" />
57+
public SnapshotRetentionConfigurationDescriptor MinimumCount(int? count) => Assign(count, (a, v) => a.MinimumCount = v);
58+
59+
/// <inheritdoc cref="ISnapshotRetentionConfiguration.MaximumCount" />
60+
public SnapshotRetentionConfigurationDescriptor MaximumCount(int? count) => Assign(count, (a, v) => a.MaximumCount = v);
61+
}
62+
}

src/Tests/Tests/XPack/Slm/SlmApiTests.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Tests.XPack.Slm
1313
{
14-
[SkipVersion("<7.4.0", "All APIs exist in Elasticsearch 7.4.0")]
14+
[SkipVersion("<7.5.0", "Snapshot lifecycle retention implement in 7.5.0")]
1515
public class SlmApiTests : CoordinatedIntegrationTestBase<XPackCluster>
1616
{
1717
private const string CreateRepositoryStep = nameof(CreateRepositoryStep);
@@ -50,6 +50,12 @@ public SlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new Coordin
5050
Config = new SnapshotLifecycleConfig
5151
{
5252
Indices = typeof(Project)
53+
},
54+
Retention = new SnapshotRetentionConfig
55+
{
56+
ExpireAfter = "30d",
57+
MinimumCount = 1,
58+
MaximumCount = 5
5359
}
5460
},
5561
(v, d) => d
@@ -58,6 +64,11 @@ public SlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new Coordin
5864
.Schedule("0 0 0 1 1 ? *")
5965
.Config(c => c
6066
.Indices<Project>()
67+
)
68+
.Retention(r => r
69+
.ExpireAfter("30d")
70+
.MinimumCount(1)
71+
.MaximumCount(5)
6172
),
6273
(v, c, f) => c.SnapshotLifecycleManagement.PutSnapshotLifecycle(v, f),
6374
(v, c, f) => c.SnapshotLifecycleManagement.PutSnapshotLifecycleAsync(v, f),
@@ -170,6 +181,9 @@ [I] public async Task GetSnapshotLifecycleResponse() => await Assert<GetSnapshot
170181
metadata.Policy.Schedule.Should().BeEquivalentTo(new CronExpression("0 0 0 1 1 ? *"));
171182
metadata.Policy.Config.Should().NotBeNull();
172183
metadata.Policy.Config.Indices.Should().NotBeNull().And.Be(Nest.Indices.Parse("project"));
184+
metadata.Policy.Retention.ExpireAfter.Should().Be("30d");
185+
metadata.Policy.Retention.MinimumCount.Should().Be(1);
186+
metadata.Policy.Retention.MaximumCount.Should().Be(5);
173187
});
174188

175189
[I] public async Task GetAllSnapshotLifecycleResponse() => await Assert<GetSnapshotLifecycleResponse>(GetAllSnapshotLifecycleStep, (v, r) =>
@@ -200,7 +214,6 @@ [I] public async Task GetSnapshotLifeCycleAfterExecuteResponse() => await Assert
200214
metadata.InProgress.State.Should().NotBeNullOrWhiteSpace();
201215
metadata.InProgress.StartTime.Should().BeAfter(DateTimeOffset.MinValue);
202216
}
203-
204217
});
205218

206219
[I] public async Task DeleteSnapshotLifecycleResponse() => await Assert<DeleteSnapshotLifecycleResponse>(DeleteSnapshotLifecycleStep,

0 commit comments

Comments
 (0)