Skip to content

Commit eb5b25e

Browse files
committed
Refactor AllocateClusterRerouteCommand and add AllocateStalePrimaryRerouteCommand
See https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_allocation.html#_indices_shard_stores_command Closes #2010
1 parent 8a348c7 commit eb5b25e

File tree

6 files changed

+178
-69
lines changed

6 files changed

+178
-69
lines changed

src/Nest/Cluster/ClusterReroute/ClusterRerouteRequest.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
namespace Nest
66
{
77
[JsonConverter(typeof(ReadAsTypeJsonConverter<ClusterRerouteRequest>))]
8-
public partial interface IClusterRerouteRequest
8+
public partial interface IClusterRerouteRequest
99
{
1010
[JsonProperty("commands")]
1111
IList<IClusterRerouteCommand> Commands { get; set; }
1212
}
1313

14-
public partial class ClusterRerouteRequest
14+
public partial class ClusterRerouteRequest
1515
{
1616
public IList<IClusterRerouteCommand> Commands { get; set; }
1717
}
1818

19-
public partial class ClusterRerouteDescriptor
19+
public partial class ClusterRerouteDescriptor
2020
{
2121
IList<IClusterRerouteCommand> IClusterRerouteRequest.Commands { get; set; } = new List<IClusterRerouteCommand>();
2222

@@ -26,8 +26,14 @@ public ClusterRerouteDescriptor Move(Func<MoveClusterRerouteCommandDescriptor, I
2626
public ClusterRerouteDescriptor Cancel(Func<CancelClusterRerouteCommandDescriptor, ICancelClusterRerouteCommand> selector)=>
2727
AddCommand(selector?.Invoke(new CancelClusterRerouteCommandDescriptor()));
2828

29-
public ClusterRerouteDescriptor Allocate(Func<AllocateClusterRerouteCommandDescriptor, IAllocateClusterRerouteCommand> selector) =>
30-
AddCommand(selector?.Invoke(new AllocateClusterRerouteCommandDescriptor()));
29+
public ClusterRerouteDescriptor AllocateReplica(Func<AllocateReplicaClusterRerouteCommandDescriptor, IAllocateClusterRerouteCommand> selector) =>
30+
AddCommand(selector?.Invoke(new AllocateReplicaClusterRerouteCommandDescriptor()));
31+
32+
public ClusterRerouteDescriptor AllocateEmptyPrimary(Func<AllocateEmptyPrimaryRerouteCommandDescriptor, IAllocateEmptyPrimaryRerouteCommand> selector) =>
33+
AddCommand(selector?.Invoke(new AllocateEmptyPrimaryRerouteCommandDescriptor()));
34+
35+
public ClusterRerouteDescriptor AllocateStalePrimary(Func<AllocateStalePrimaryRerouteCommandDescriptor, IAllocateStalePrimaryRerouteCommand> selector) =>
36+
AddCommand(selector?.Invoke(new AllocateStalePrimaryRerouteCommandDescriptor()));
3137

3238
private ClusterRerouteDescriptor AddCommand(IClusterRerouteCommand rerouteCommand) => Assign(a => a.Commands?.AddIfNotNull(rerouteCommand));
3339
}

src/Nest/Cluster/ClusterReroute/Commands/AllocateClusterRerouteCommand.cs

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Nest
4+
{
5+
public interface IAllocateClusterRerouteCommand : IClusterRerouteCommand
6+
{
7+
[JsonProperty("index")]
8+
IndexName Index { get; set; }
9+
10+
[JsonProperty("shard")]
11+
int Shard { get; set; }
12+
13+
[JsonProperty("node")]
14+
string Node { get; set; }
15+
}
16+
17+
public interface IAllocateReplicaClusterRerouteCommand : IAllocateClusterRerouteCommand
18+
{
19+
[JsonProperty("allow_primary")]
20+
bool AllowPrimary { get; }
21+
}
22+
23+
public interface IAllocateEmptyPrimaryRerouteCommand : IAllocateClusterRerouteCommand
24+
{
25+
[JsonProperty("allow_primary")]
26+
bool AllowPrimary { get; }
27+
28+
[JsonProperty("accept_data_loss")]
29+
bool AcceptDataLoss { get; set; }
30+
}
31+
32+
public interface IAllocateStalePrimaryRerouteCommand : IAllocateClusterRerouteCommand
33+
{
34+
[JsonProperty("accept_data_loss")]
35+
bool AcceptDataLoss { get; set; }
36+
}
37+
38+
public abstract class AllocateClusterRerouteCommandBase : IAllocateClusterRerouteCommand
39+
{
40+
public abstract string Name { get; }
41+
42+
public IndexName Index { get; set; }
43+
44+
public int Shard { get; set; }
45+
46+
public string Node { get; set; }
47+
}
48+
49+
public class AllocateReplicaClusterRerouteCommand : AllocateClusterRerouteCommandBase, IAllocateReplicaClusterRerouteCommand
50+
{
51+
public override string Name => "allocate_replica";
52+
53+
public bool AllowPrimary => false;
54+
}
55+
56+
public class AllocateEmptyPrimaryRerouteCommand : AllocateClusterRerouteCommandBase, IAllocateEmptyPrimaryRerouteCommand
57+
{
58+
public override string Name => "allocate_empty_primary";
59+
60+
public bool AllowPrimary => true;
61+
62+
public bool AcceptDataLoss { get; set; }
63+
}
64+
65+
public class AllocateStalePrimaryRerouteCommand : AllocateClusterRerouteCommandBase, IAllocateStalePrimaryRerouteCommand
66+
{
67+
public override string Name => "allocate_stale_primary";
68+
69+
public bool AcceptDataLoss { get; set; }
70+
}
71+
72+
public abstract class AllocateClusterRerouteCommandDescriptorBase<TDescriptor, TInterface>
73+
: DescriptorBase<TDescriptor, TInterface>, IAllocateClusterRerouteCommand
74+
where TDescriptor : AllocateClusterRerouteCommandDescriptorBase<TDescriptor, TInterface>, TInterface, IAllocateClusterRerouteCommand
75+
where TInterface : class, IAllocateClusterRerouteCommand
76+
{
77+
string IClusterRerouteCommand.Name => Name;
78+
79+
public abstract string Name { get; }
80+
81+
IndexName IAllocateClusterRerouteCommand.Index { get; set; }
82+
83+
int IAllocateClusterRerouteCommand.Shard { get; set; }
84+
85+
string IAllocateClusterRerouteCommand.Node { get; set; }
86+
87+
public TDescriptor Index(IndexName index) => Assign(a => a.Index = index);
88+
89+
public TDescriptor Index<T>() where T : class => Assign(a => a.Index = typeof(T));
90+
91+
public TDescriptor Shard(int shard) => Assign(a => a.Shard = shard);
92+
93+
public TDescriptor Node(string node) => Assign(a => a.Node = node);
94+
}
95+
96+
public class AllocateReplicaClusterRerouteCommandDescriptor
97+
: AllocateClusterRerouteCommandDescriptorBase<AllocateReplicaClusterRerouteCommandDescriptor, IAllocateReplicaClusterRerouteCommand>, IAllocateReplicaClusterRerouteCommand
98+
{
99+
public override string Name => "allocate_replica";
100+
101+
bool IAllocateReplicaClusterRerouteCommand.AllowPrimary => false;
102+
}
103+
104+
public class AllocateEmptyPrimaryRerouteCommandDescriptor
105+
: AllocateClusterRerouteCommandDescriptorBase<AllocateEmptyPrimaryRerouteCommandDescriptor, IAllocateEmptyPrimaryRerouteCommand>, IAllocateEmptyPrimaryRerouteCommand
106+
{
107+
public override string Name => "allocate_empty_primary";
108+
109+
bool IAllocateEmptyPrimaryRerouteCommand.AllowPrimary => true;
110+
111+
bool IAllocateEmptyPrimaryRerouteCommand.AcceptDataLoss { get; set; }
112+
113+
public AllocateEmptyPrimaryRerouteCommandDescriptor AcceptDataLoss(bool acceptDataLoss) => Assign(a => a.AcceptDataLoss = acceptDataLoss);
114+
}
115+
116+
public class AllocateStalePrimaryRerouteCommandDescriptor
117+
: AllocateClusterRerouteCommandDescriptorBase<AllocateStalePrimaryRerouteCommandDescriptor, IAllocateStalePrimaryRerouteCommand>, IAllocateStalePrimaryRerouteCommand
118+
{
119+
public override string Name => "allocate_stale_primary";
120+
121+
bool IAllocateStalePrimaryRerouteCommand.AcceptDataLoss { get; set; }
122+
123+
public AllocateStalePrimaryRerouteCommandDescriptor AcceptDataLoss(bool acceptDataLoss) => Assign(a => a.AcceptDataLoss = acceptDataLoss);
124+
}
125+
}

src/Nest/Cluster/ClusterReroute/Commands/ClusterRerouteCommandJsonConverter.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
1616
IClusterRerouteCommand command = null;
1717
var o = JObject.Load(reader);
1818
var child = o.Children<JProperty>().FirstOrDefault();
19-
if (child == null) return null;
20-
var v = child.Children<JObject>().FirstOrDefault();
19+
var v = child?.Children<JObject>().FirstOrDefault();
2120
if (v == null) return null;
2221
switch (child.Name)
2322
{
24-
case "allocate":
25-
command = v.ToObject<AllocateClusterRerouteCommand>(ElasticContractResolver.Empty);
23+
case "allocate_replica":
24+
command = v.ToObject<AllocateReplicaClusterRerouteCommand>(ElasticContractResolver.Empty);
25+
break;
26+
case "allocate_empty_primary":
27+
command = v.ToObject<AllocateEmptyPrimaryRerouteCommand>(ElasticContractResolver.Empty);
28+
break;
29+
case "allocate_stale_primary":
30+
command = v.ToObject<AllocateStalePrimaryRerouteCommand>(ElasticContractResolver.Empty);
2631
break;
2732
case "move":
2833
command = v.ToObject<MoveClusterRerouteCommand>(ElasticContractResolver.Empty);

src/Nest/Nest.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@
314314
<Compile Include="Cluster\ClusterReroute\ClusterRerouteRequest.cs" />
315315
<Compile Include="Cluster\ClusterReroute\ClusterRerouteResponse.cs" />
316316
<Compile Include="Cluster\ClusterReroute\ClusterRerouteState.cs" />
317-
<Compile Include="Cluster\ClusterReroute\Commands\AllocateClusterRerouteCommand.cs" />
317+
<Compile Include="Cluster\ClusterReroute\Commands\AllocateClusterRerouteCommandBase.cs" />
318318
<Compile Include="Cluster\ClusterReroute\Commands\CancelClusterRerouteCommand.cs" />
319319
<Compile Include="Cluster\ClusterReroute\Commands\ClusterRerouteCommandJsonConverter.cs" />
320320
<Compile Include="Cluster\ClusterReroute\Commands\IClusterRerouteCommand.cs" />

src/Tests/Cluster/ClusterReroute/ClusterRerouteApiTests.cs

+32-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ protected override LazyResponses ClientUsage() => Calls(
2828
protected override string UrlPath => "/_cluster/reroute";
2929

3030
protected override Func<ClusterRerouteDescriptor, IClusterRerouteRequest> Fluent => c => c
31-
.Allocate(a => a
31+
.AllocateEmptyPrimary(a => a
32+
.Index<Project>()
33+
.Node("x")
34+
.Shard(0)
35+
.AcceptDataLoss(true)
36+
)
37+
.AllocateStalePrimary(a => a
38+
.Index<Project>()
39+
.Node("x")
40+
.Shard(0)
41+
.AcceptDataLoss(true)
42+
)
43+
.AllocateReplica(a => a
3244
.Index<Project>()
3345
.Node("x")
34-
.AllowPrimary(false)
3546
.Shard(0)
3647
)
3748
.Move(a => a
@@ -50,7 +61,9 @@ protected override LazyResponses ClientUsage() => Calls(
5061
{
5162
Commands = new List<IClusterRerouteCommand>
5263
{
53-
new AllocateClusterRerouteCommand { AllowPrimary = false, Index = IndexName.From<Project>(), Node = "x", Shard = 0},
64+
new AllocateEmptyPrimaryRerouteCommand { Index = IndexName.From<Project>(), Node = "x", Shard = 0, AcceptDataLoss = true },
65+
new AllocateStalePrimaryRerouteCommand { Index = IndexName.From<Project>(), Node = "x", Shard = 0, AcceptDataLoss = true },
66+
new AllocateReplicaClusterRerouteCommand { Index = IndexName.From<Project>(), Node = "x", Shard = 0 },
5467
new MoveClusterRerouteCommand { Index = IndexName.From<Project>(), FromNode = "x", ToNode = "y", Shard = 0},
5568
new CancelClusterRerouteCommand() { Index = "project", Node = "x", Shard = 1}
5669
}
@@ -60,7 +73,22 @@ protected override LazyResponses ClientUsage() => Calls(
6073
{
6174
commands = new []
6275
{
63-
new Dictionary<string, object> { { "allocate", new
76+
new Dictionary<string, object> { { "allocate_empty_primary", new
77+
{
78+
allow_primary = true,
79+
index = "project",
80+
node = "x",
81+
shard = 0,
82+
accept_data_loss = true
83+
} } },
84+
new Dictionary<string, object> { { "allocate_stale_primary", new
85+
{
86+
index = "project",
87+
node = "x",
88+
shard = 0,
89+
accept_data_loss = true
90+
} } },
91+
new Dictionary<string, object> { { "allocate_replica", new
6492
{
6593
allow_primary = false,
6694
index = "project",

0 commit comments

Comments
 (0)