Skip to content

Commit 9056fe4

Browse files
authored
Convert some ILM classes to records (#118466)
1 parent 2c5efd2 commit 9056fe4

26 files changed

+199
-421
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ public boolean isCompletable() {
3333
return true;
3434
}
3535

36-
public static class Result {
37-
private final boolean complete;
38-
private final ToXContentObject informationContext;
39-
40-
public Result(boolean complete, ToXContentObject informationContext) {
41-
this.complete = complete;
42-
this.informationContext = informationContext;
43-
}
44-
45-
public boolean isComplete() {
46-
return complete;
47-
}
48-
49-
public ToXContentObject getInformationContext() {
50-
return informationContext;
51-
}
52-
}
36+
public record Result(boolean complete, ToXContentObject informationContext) {}
5337

5438
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
6262

6363
Result stepResult = stepToExecute.isConditionMet(index, clusterState);
6464

65-
if (stepResult.isComplete() == false) {
65+
if (stepResult.complete() == false) {
6666
// checking the threshold after we execute the step to make sure we execute the wrapped step at least once (because time is a
6767
// wonderful thing)
6868
TimeValue retryThreshold = LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING.get(idxMeta.getSettings());
@@ -77,7 +77,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
7777
getKey().name(),
7878
getKey().action(),
7979
idxMeta.getIndex().getName(),
80-
Strings.toString(stepResult.getInformationContext()),
80+
Strings.toString(stepResult.informationContext()),
8181
nextKeyOnThresholdBreach
8282
);
8383
logger.debug(message);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,20 @@ public boolean equals(Object obj) {
114114
return super.equals(obj) && Objects.equals(maxNumSegments, other.maxNumSegments);
115115
}
116116

117-
public static class Info implements ToXContentObject {
118-
119-
private final long numberShardsLeftToMerge;
117+
public record Info(long numberShardsLeftToMerge) implements ToXContentObject {
120118

121119
static final ParseField SHARDS_TO_MERGE = new ParseField("shards_left_to_merge");
122120
static final ParseField MESSAGE = new ParseField("message");
123121
static final ConstructingObjectParser<Info, Void> PARSER = new ConstructingObjectParser<>(
124122
"segment_count_step_info",
125123
a -> new Info((long) a[0])
126124
);
125+
127126
static {
128127
PARSER.declareLong(ConstructingObjectParser.constructorArg(), SHARDS_TO_MERGE);
129128
PARSER.declareString((i, s) -> {}, MESSAGE);
130129
}
131130

132-
public Info(long numberShardsLeftToMerge) {
133-
this.numberShardsLeftToMerge = numberShardsLeftToMerge;
134-
}
135-
136-
public long getNumberShardsLeftToMerge() {
137-
return numberShardsLeftToMerge;
138-
}
139-
140131
@Override
141132
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
142133
builder.startObject();
@@ -150,23 +141,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
150141
return builder;
151142
}
152143

153-
@Override
154-
public int hashCode() {
155-
return Objects.hash(numberShardsLeftToMerge);
156-
}
157-
158-
@Override
159-
public boolean equals(Object obj) {
160-
if (obj == null) {
161-
return false;
162-
}
163-
if (getClass() != obj.getClass()) {
164-
return false;
165-
}
166-
Info other = (Info) obj;
167-
return Objects.equals(numberShardsLeftToMerge, other.numberShardsLeftToMerge);
168-
}
169-
170144
@Override
171145
public String toString() {
172146
return Strings.toString(this);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java

Lines changed: 20 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.Objects;
2625
import java.util.stream.Collectors;
2726

2827
import static org.elasticsearch.xpack.core.ilm.UnfollowAction.CCR_METADATA_KEY;
@@ -70,9 +69,9 @@ static void handleResponse(FollowStatsAction.StatsResponses responses, Listener
7069
if (conditionMet) {
7170
listener.onResponse(true, null);
7271
} else {
73-
List<Info.ShardFollowTaskInfo> shardFollowTaskInfos = unSyncedShardFollowStatuses.stream()
72+
List<ShardFollowTaskInfo> shardFollowTaskInfos = unSyncedShardFollowStatuses.stream()
7473
.map(
75-
status -> new Info.ShardFollowTaskInfo(
74+
status -> new ShardFollowTaskInfo(
7675
status.followerIndex(),
7776
status.getShardId(),
7877
status.leaderGlobalCheckpoint(),
@@ -84,21 +83,11 @@ static void handleResponse(FollowStatsAction.StatsResponses responses, Listener
8483
}
8584
}
8685

87-
static final class Info implements ToXContentObject {
86+
record Info(List<ShardFollowTaskInfo> shardFollowTaskInfos) implements ToXContentObject {
8887

8988
static final ParseField SHARD_FOLLOW_TASKS = new ParseField("shard_follow_tasks");
9089
static final ParseField MESSAGE = new ParseField("message");
9190

92-
private final List<ShardFollowTaskInfo> shardFollowTaskInfos;
93-
94-
Info(List<ShardFollowTaskInfo> shardFollowTaskInfos) {
95-
this.shardFollowTaskInfos = shardFollowTaskInfos;
96-
}
97-
98-
List<ShardFollowTaskInfo> getShardFollowTaskInfos() {
99-
return shardFollowTaskInfos;
100-
}
101-
10291
@Override
10392
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
10493
builder.startObject();
@@ -114,85 +103,30 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
114103
return builder;
115104
}
116105

117-
@Override
118-
public boolean equals(Object o) {
119-
if (this == o) return true;
120-
if (o == null || getClass() != o.getClass()) return false;
121-
Info info = (Info) o;
122-
return Objects.equals(shardFollowTaskInfos, info.shardFollowTaskInfos);
123-
}
124-
125-
@Override
126-
public int hashCode() {
127-
return Objects.hash(shardFollowTaskInfos);
128-
}
129-
130106
@Override
131107
public String toString() {
132108
return Strings.toString(this);
133109
}
110+
}
134111

135-
static final class ShardFollowTaskInfo implements ToXContentObject {
136-
137-
static final ParseField FOLLOWER_INDEX_FIELD = new ParseField("follower_index");
138-
static final ParseField SHARD_ID_FIELD = new ParseField("shard_id");
139-
static final ParseField LEADER_GLOBAL_CHECKPOINT_FIELD = new ParseField("leader_global_checkpoint");
140-
static final ParseField FOLLOWER_GLOBAL_CHECKPOINT_FIELD = new ParseField("follower_global_checkpoint");
141-
142-
private final String followerIndex;
143-
private final int shardId;
144-
private final long leaderGlobalCheckpoint;
145-
private final long followerGlobalCheckpoint;
146-
147-
ShardFollowTaskInfo(String followerIndex, int shardId, long leaderGlobalCheckpoint, long followerGlobalCheckpoint) {
148-
this.followerIndex = followerIndex;
149-
this.shardId = shardId;
150-
this.leaderGlobalCheckpoint = leaderGlobalCheckpoint;
151-
this.followerGlobalCheckpoint = followerGlobalCheckpoint;
152-
}
153-
154-
String getFollowerIndex() {
155-
return followerIndex;
156-
}
157-
158-
int getShardId() {
159-
return shardId;
160-
}
161-
162-
long getLeaderGlobalCheckpoint() {
163-
return leaderGlobalCheckpoint;
164-
}
165-
166-
long getFollowerGlobalCheckpoint() {
167-
return followerGlobalCheckpoint;
168-
}
169-
170-
@Override
171-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
172-
builder.startObject();
173-
builder.field(FOLLOWER_INDEX_FIELD.getPreferredName(), followerIndex);
174-
builder.field(SHARD_ID_FIELD.getPreferredName(), shardId);
175-
builder.field(LEADER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), leaderGlobalCheckpoint);
176-
builder.field(FOLLOWER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), followerGlobalCheckpoint);
177-
builder.endObject();
178-
return builder;
179-
}
112+
record ShardFollowTaskInfo(String followerIndex, int shardId, long leaderGlobalCheckpoint, long followerGlobalCheckpoint)
113+
implements
114+
ToXContentObject {
180115

181-
@Override
182-
public boolean equals(Object o) {
183-
if (this == o) return true;
184-
if (o == null || getClass() != o.getClass()) return false;
185-
ShardFollowTaskInfo that = (ShardFollowTaskInfo) o;
186-
return shardId == that.shardId
187-
&& leaderGlobalCheckpoint == that.leaderGlobalCheckpoint
188-
&& followerGlobalCheckpoint == that.followerGlobalCheckpoint
189-
&& Objects.equals(followerIndex, that.followerIndex);
190-
}
116+
static final ParseField FOLLOWER_INDEX_FIELD = new ParseField("follower_index");
117+
static final ParseField SHARD_ID_FIELD = new ParseField("shard_id");
118+
static final ParseField LEADER_GLOBAL_CHECKPOINT_FIELD = new ParseField("leader_global_checkpoint");
119+
static final ParseField FOLLOWER_GLOBAL_CHECKPOINT_FIELD = new ParseField("follower_global_checkpoint");
191120

192-
@Override
193-
public int hashCode() {
194-
return Objects.hash(followerIndex, shardId, leaderGlobalCheckpoint, followerGlobalCheckpoint);
195-
}
121+
@Override
122+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
123+
builder.startObject();
124+
builder.field(FOLLOWER_INDEX_FIELD.getPreferredName(), followerIndex);
125+
builder.field(SHARD_ID_FIELD.getPreferredName(), shardId);
126+
builder.field(LEADER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), leaderGlobalCheckpoint);
127+
builder.field(FOLLOWER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), followerGlobalCheckpoint);
128+
builder.endObject();
129+
return builder;
196130
}
197131
}
198132
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationInfo.java

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414
import org.elasticsearch.xcontent.XContentBuilder;
1515

1616
import java.io.IOException;
17-
import java.util.Objects;
1817

1918
/**
2019
* Represents the state of an index's shards allocation, including a user friendly message describing the current state.
2120
* It allows to transfer the allocation information to {@link org.elasticsearch.xcontent.XContent} using
2221
* {@link #toXContent(XContentBuilder, Params)}
2322
*/
24-
public class AllocationInfo implements ToXContentObject {
25-
26-
private final long numberOfReplicas;
27-
private final long numberShardsLeftToAllocate;
28-
private final boolean allShardsActive;
29-
private final String message;
23+
public record AllocationInfo(long numberOfReplicas, long numberShardsLeftToAllocate, boolean allShardsActive, String message)
24+
implements
25+
ToXContentObject {
3026

3127
static final ParseField NUMBER_OF_REPLICAS = new ParseField("number_of_replicas");
3228
static final ParseField SHARDS_TO_ALLOCATE = new ParseField("shards_left_to_allocate");
@@ -44,13 +40,6 @@ public class AllocationInfo implements ToXContentObject {
4440
PARSER.declareString(ConstructingObjectParser.constructorArg(), MESSAGE);
4541
}
4642

47-
public AllocationInfo(long numberOfReplicas, long numberShardsLeftToAllocate, boolean allShardsActive, String message) {
48-
this.numberOfReplicas = numberOfReplicas;
49-
this.numberShardsLeftToAllocate = numberShardsLeftToAllocate;
50-
this.allShardsActive = allShardsActive;
51-
this.message = message;
52-
}
53-
5443
/**
5544
* Builds the AllocationInfo representing a cluster state with a routing table that does not have enough shards active for a
5645
* particular index.
@@ -72,22 +61,6 @@ public static AllocationInfo allShardsActiveAllocationInfo(long numReplicas, lon
7261
);
7362
}
7463

75-
public long getNumberOfReplicas() {
76-
return numberOfReplicas;
77-
}
78-
79-
public long getNumberShardsLeftToAllocate() {
80-
return numberShardsLeftToAllocate;
81-
}
82-
83-
public boolean allShardsActive() {
84-
return allShardsActive;
85-
}
86-
87-
public String getMessage() {
88-
return message;
89-
}
90-
9164
@Override
9265
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
9366
builder.startObject();
@@ -99,26 +72,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
9972
return builder;
10073
}
10174

102-
@Override
103-
public int hashCode() {
104-
return Objects.hash(numberOfReplicas, numberShardsLeftToAllocate, allShardsActive);
105-
}
106-
107-
@Override
108-
public boolean equals(Object obj) {
109-
if (obj == null) {
110-
return false;
111-
}
112-
if (getClass() != obj.getClass()) {
113-
return false;
114-
}
115-
AllocationInfo other = (AllocationInfo) obj;
116-
return Objects.equals(numberOfReplicas, other.numberOfReplicas)
117-
&& Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate)
118-
&& Objects.equals(message, other.message)
119-
&& Objects.equals(allShardsActive, other.allShardsActive);
120-
}
121-
12275
@Override
12376
public String toString() {
12477
return Strings.toString(this);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/SingleMessageFieldInfo.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212
import org.elasticsearch.xcontent.XContentBuilder;
1313

1414
import java.io.IOException;
15-
import java.util.Objects;
1615

1716
/**
1817
* A simple object that allows a `message` field to be transferred to `XContent`.
1918
*/
20-
public class SingleMessageFieldInfo implements ToXContentObject {
19+
public record SingleMessageFieldInfo(String message) implements ToXContentObject {
2120

22-
private final String message;
23-
24-
static final ParseField MESSAGE = new ParseField("message");
25-
26-
public SingleMessageFieldInfo(String message) {
27-
this.message = message;
28-
}
21+
private static final ParseField MESSAGE = new ParseField("message");
2922

3023
@Override
3124
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
@@ -35,24 +28,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
3528
return builder;
3629
}
3730

38-
public String getMessage() {
39-
return message;
40-
}
41-
42-
@Override
43-
public boolean equals(Object o) {
44-
if (this == o) {
45-
return true;
46-
}
47-
if (o == null || getClass() != o.getClass()) {
48-
return false;
49-
}
50-
SingleMessageFieldInfo that = (SingleMessageFieldInfo) o;
51-
return Objects.equals(message, that.message);
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
return Objects.hash(message);
57-
}
5831
}

0 commit comments

Comments
 (0)