Skip to content

Commit a026a33

Browse files
Autoscaling role validation 7x (#66116)
While transport client is not supported for autoscaling in 7.x, some tests rely on it and this commit ensures that the validation of roles happen server side and not client side. Relates #65775 and #66082
1 parent 6e15b81 commit a026a33

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/action/PutAutoscalingPolicyAction.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.action.ValidateActions;
1212
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1313
import org.elasticsearch.action.support.master.AcknowledgedResponse;
14-
import org.elasticsearch.cluster.node.DiscoveryNode;
1514
import org.elasticsearch.common.Strings;
1615
import org.elasticsearch.common.io.stream.StreamInput;
1716
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -23,15 +22,13 @@
2322
import java.io.IOException;
2423
import java.util.AbstractMap;
2524
import java.util.Collections;
26-
import java.util.Collections;
2725
import java.util.List;
2826
import java.util.Map;
2927
import java.util.Objects;
3028
import java.util.SortedMap;
3129
import java.util.SortedSet;
3230
import java.util.TreeMap;
3331
import java.util.TreeSet;
34-
import java.util.function.Predicate;
3532
import java.util.stream.Collectors;
3633

3734
public class PutAutoscalingPolicyAction extends ActionType<AcknowledgedResponse> {
@@ -142,15 +139,7 @@ public SortedMap<String, Settings> deciders() {
142139
@Override
143140
public ActionRequestValidationException validate() {
144141
ActionRequestValidationException exception = null;
145-
if (roles != null) {
146-
List<String> errors = roles.stream()
147-
.filter(not(DiscoveryNode.getPossibleRoleNames()::contains))
148-
.collect(Collectors.toList());
149-
if (errors.isEmpty() == false) {
150-
exception = new ActionRequestValidationException();
151-
exception.addValidationErrors(errors);
152-
}
153-
}
142+
// roles validation done in transport action in 7.x to be compatible with transport client.
154143

155144
if (Strings.validFileName(name) == false) {
156145
exception = ValidateActions.addValidationError(
@@ -175,9 +164,4 @@ public int hashCode() {
175164
return Objects.hash(name, roles, deciders);
176165
}
177166
}
178-
179-
// java 11 forward compatibility
180-
private static <T> Predicate<T> not(Predicate<T> predicate) {
181-
return predicate.negate();
182-
}
183167
}

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/action/TransportPutAutoscalingPolicyAction.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.elasticsearch.action.ActionListener;
12+
import org.elasticsearch.action.ActionRequestValidationException;
1213
import org.elasticsearch.action.support.ActionFilters;
1314
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1415
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
@@ -18,6 +19,7 @@
1819
import org.elasticsearch.cluster.block.ClusterBlockLevel;
1920
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2021
import org.elasticsearch.cluster.metadata.Metadata;
22+
import org.elasticsearch.cluster.node.DiscoveryNode;
2123
import org.elasticsearch.cluster.service.ClusterService;
2224
import org.elasticsearch.common.inject.Inject;
2325
import org.elasticsearch.threadpool.ThreadPool;
@@ -28,8 +30,12 @@
2830
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicyMetadata;
2931

3032
import java.util.Collections;
33+
import java.util.List;
3134
import java.util.SortedMap;
35+
import java.util.SortedSet;
3236
import java.util.TreeMap;
37+
import java.util.function.Predicate;
38+
import java.util.stream.Collectors;
3339

3440
public class TransportPutAutoscalingPolicyAction extends AcknowledgedTransportMasterNodeAction<PutAutoscalingPolicyAction.Request> {
3541

@@ -76,6 +82,17 @@ protected void masterOperation(
7682
final ClusterState state,
7783
ActionListener<AcknowledgedResponse> listener
7884
) {
85+
SortedSet<String> roles = request.roles();
86+
if (roles != null) {
87+
List<String> errors = roles.stream().filter(not(DiscoveryNode.getPossibleRoleNames()::contains)).collect(Collectors.toList());
88+
if (errors.isEmpty() == false) {
89+
ActionRequestValidationException exception = new ActionRequestValidationException();
90+
exception.addValidationErrors(errors);
91+
listener.onFailure(exception);
92+
return;
93+
}
94+
}
95+
7996
clusterService.submitStateUpdateTask("put-autoscaling-policy", new AckedClusterStateUpdateTask(request, listener) {
8097
@Override
8198
public ClusterState execute(final ClusterState currentState) {
@@ -144,4 +161,9 @@ static ClusterState putAutoscalingPolicy(
144161
builder.metadata(Metadata.builder(currentState.getMetadata()).putCustom(AutoscalingMetadata.NAME, newMetadata).build());
145162
return builder.build();
146163
}
164+
165+
// java 11 forward compatibility
166+
private static <T> Predicate<T> not(Predicate<T> predicate) {
167+
return predicate.negate();
168+
}
147169
}

x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutoscalingIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testMLAutoscalingCapacity() {
5151
Settings.builder().put(MlAutoscalingDeciderService.DOWN_SCALE_DELAY.getKey(), TimeValue.ZERO).build());
5252
final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request(
5353
"ml_test",
54-
new TreeSet<>(),
54+
new TreeSet<>(org.elasticsearch.common.collect.Set.of("ml")),
5555
deciders
5656
);
5757
assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet());

0 commit comments

Comments
 (0)