Skip to content

Commit 431af01

Browse files
committed
wire tests
1 parent ee921fb commit 431af01

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ static TransportVersion def(int id) {
365365
public static final TransportVersion ESQL_LOOKUP_JOIN_ON_MANY_FIELDS = def(9_139_0_00);
366366
public static final TransportVersion SIMULATE_INGEST_EFFECTIVE_MAPPING = def(9_140_0_00);
367367
public static final TransportVersion RESOLVE_INDEX_MODE_ADDED = def(9_141_0_00);
368+
public static final TransportVersion ALLOCATION_DECISION_YES_NOT_PREFERRED = def(9_142_0_00);
368369

369370
/*
370371
* STOP! READ THIS FIRST! No, really,

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private Decision withDeciders(
216216
return decision;
217217
} else if (finalDecision.type() == Decision.Type.YES && decision.type() == Decision.Type.THROTTLE) {
218218
finalDecision = decision;
219-
} else if (finalDecision.preferred() == Decision.Preferred.NO) {
219+
} else if (decision.preferred() == Decision.Preferred.NO) {
220220
preferred = Decision.Preferred.NO;
221221
}
222222
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/Decision.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.cluster.routing.allocation.decider;
1111

12+
import org.elasticsearch.TransportVersions;
1213
import org.elasticsearch.common.io.stream.StreamInput;
1314
import org.elasticsearch.common.io.stream.StreamOutput;
1415
import org.elasticsearch.common.io.stream.Writeable;
@@ -90,16 +91,20 @@ static Decision readFrom(StreamInput in) throws IOException {
9091

9192
private static Single readSingleFrom(StreamInput in) throws IOException {
9293
final Type type = Type.readFrom(in);
94+
Preferred preferred = Preferred.YES;
95+
if (in.getTransportVersion().onOrAfter(TransportVersions.ALLOCATION_DECISION_YES_NOT_PREFERRED)) {
96+
preferred = in.readEnum(Preferred.class);
97+
}
9398
final String label = in.readOptionalString();
9499
final String explanation = in.readOptionalString();
95100
if (label == null && explanation == null) {
96101
return switch (type) {
97-
case YES -> YES;
102+
case YES -> preferred == Preferred.YES ? YES : YES_NOT_PREFERRED;
98103
case THROTTLE -> THROTTLE;
99104
case NO -> NO;
100105
};
101106
}
102-
return new Single(type, label, explanation);
107+
return new Single(type, preferred, label, explanation);
103108
}
104109

105110
/**
@@ -253,7 +258,7 @@ public String getExplanation() {
253258

254259
@Override
255260
public String toString() {
256-
var preference = type == Type.YES && preferred == Preferred.NO ? "_NOT_PREFERRED": "";
261+
var preference = type == Type.YES && preferred == Preferred.NO ? "_NOT_PREFERRED" : "";
257262
if (explanationString != null) {
258263
return type + preference + "(" + explanationString + ")";
259264
}
@@ -274,6 +279,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
274279
public void writeTo(StreamOutput out) throws IOException {
275280
out.writeBoolean(false); // flag specifying its a single decision
276281
type.writeTo(out);
282+
if (out.getTransportVersion().onOrAfter(TransportVersions.ALLOCATION_DECISION_YES_NOT_PREFERRED)) {
283+
out.writeEnum(preferred);
284+
}
277285
out.writeOptionalString(label);
278286
// Flatten explanation on serialization, so that explanationParams
279287
// do not need to be serialized
@@ -317,16 +325,12 @@ public Type type() {
317325

318326
@Override
319327
public Preferred preferred() {
320-
var preferred = Preferred.YES;
321328
for (var decision : decisions) {
322-
if (decision.type() != Type.YES) {
329+
if (decision.type() != Type.YES || decision.preferred() == Preferred.NO) {
323330
return Preferred.NO;
324331
}
325-
if (decision.preferred() == Preferred.NO) {
326-
preferred = Preferred.NO;
327-
}
328332
}
329-
return preferred;
333+
return Preferred.YES;
330334
}
331335

332336
@Override

server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DecisionTests.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
package org.elasticsearch.cluster.routing.allocation.decider;
1111

12+
import org.elasticsearch.TransportVersions;
1213
import org.elasticsearch.cluster.routing.allocation.decider.Decision.Type;
13-
import org.elasticsearch.test.ESTestCase;
14+
import org.elasticsearch.common.io.stream.Writeable;
15+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
16+
import org.elasticsearch.test.TransportVersionUtils;
17+
18+
import java.io.IOException;
1419

1520
import static org.elasticsearch.cluster.routing.allocation.decider.Decision.Type.NO;
1621
import static org.elasticsearch.cluster.routing.allocation.decider.Decision.Type.THROTTLE;
@@ -19,7 +24,7 @@
1924
/**
2025
* A class for unit testing the {@link Decision} class.
2126
*/
22-
public class DecisionTests extends ESTestCase {
27+
public class DecisionTests extends AbstractWireSerializingTestCase<Decision> {
2328

2429
/**
2530
* Tests {@link Type#higherThan(Type)}
@@ -40,4 +45,34 @@ public void testHigherThan() {
4045
assertFalse(NO.higherThan(THROTTLE));
4146
assertFalse(NO.higherThan(YES));
4247
}
48+
49+
public void testNotPreferredSerializationBeforeVersion() throws IOException {
50+
var inDecision = Decision.YES_NOT_PREFERRED;
51+
var outDecision = copyInstance(
52+
inDecision,
53+
TransportVersionUtils.getPreviousVersion(TransportVersions.ALLOCATION_DECISION_YES_NOT_PREFERRED)
54+
);
55+
assertEquals("should fallback to always-preferred in prior versions", Decision.YES, outDecision);
56+
}
57+
58+
public void testNotPreferredSerialization() throws IOException {
59+
var inDecision = Decision.YES_NOT_PREFERRED;
60+
var outDecision = copyInstance(inDecision, TransportVersions.ALLOCATION_DECISION_YES_NOT_PREFERRED);
61+
assertEquals(inDecision, outDecision);
62+
}
63+
64+
@Override
65+
protected Writeable.Reader<Decision> instanceReader() {
66+
return Decision::readFrom;
67+
}
68+
69+
@Override
70+
protected Decision createTestInstance() {
71+
return Decision.single(YES, null, null, (Object[]) null);
72+
}
73+
74+
@Override
75+
protected Decision mutateInstance(Decision instance) throws IOException {
76+
return instance.type() == YES ? Decision.NO : Decision.YES;
77+
}
4378
}

x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/NodeDecisionTestUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
1212
import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider;
1313
import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider;
14+
import org.elasticsearch.cluster.routing.allocation.decider.WriteLoadConstraintDecider;
1415

1516
import static java.util.Collections.emptySet;
1617
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength;
@@ -30,6 +31,7 @@ static Decision randomDecision() {
3031
return randomFrom(
3132
new Decision.Single(Decision.Type.NO, DiskThresholdDecider.NAME, "Unable to allocate on disk"),
3233
new Decision.Single(Decision.Type.YES, FilterAllocationDecider.NAME, "Filter allows allocation"),
34+
new Decision.Single(Decision.Type.YES, Decision.Preferred.NO, WriteLoadConstraintDecider.NAME, "Write-Load is above threshold"),
3335
new Decision.Single(Decision.Type.THROTTLE, "throttle label", "Throttling the consumer"),
3436
new Decision.Multi().add(randomFrom(Decision.NO, Decision.YES, Decision.THROTTLE))
3537
.add(new Decision.Single(Decision.Type.NO, "multi_no", "No multi decision"))

0 commit comments

Comments
 (0)