Skip to content

Commit ab0805d

Browse files
committed
Use TimeValue instead of an int
1 parent 51f67eb commit ab0805d

File tree

8 files changed

+41
-36
lines changed

8 files changed

+41
-36
lines changed

server/src/internalClusterTest/java/org/elasticsearch/synonyms/SynonymsManagementAPIServiceIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.bulk.BulkResponse;
1616
import org.elasticsearch.cluster.ClusterName;
1717
import org.elasticsearch.cluster.ClusterState;
18+
import org.elasticsearch.core.TimeValue;
1819
import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin;
1920
import org.elasticsearch.indices.IndexCreationException;
2021
import org.elasticsearch.plugins.Plugin;
@@ -309,7 +310,7 @@ public void testCreateSynonymsWithYellowSynonymsIndex() throws Exception {
309310
// Override health method check to simulate a timeout in checking the synonyms index
310311
synonymsManagementAPIService = new SynonymsManagementAPIService(client()) {
311312
@Override
312-
void checkSynonymsIndexHealth(int timeout, ActionListener<ClusterHealthResponse> listener) {
313+
void checkSynonymsIndexHealth(TimeValue timeout, ActionListener<ClusterHealthResponse> listener) {
313314
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).build();
314315
ClusterHealthResponse response = new ClusterHealthResponse(randomIdentifier(),
315316
new String[]{SynonymsManagementAPIService.SYNONYMS_INDEX_CONCRETE_NAME},
@@ -365,12 +366,12 @@ public void onFailure(Exception e) {
365366

366367
updateLatch.await(5, TimeUnit.SECONDS);
367368

368-
// But, we can still create a synonyms set with timeout 0
369+
// But, we can still create a synonyms set with timeout -1
369370
CountDownLatch putWithoutTimeoutLatch = new CountDownLatch(1);
370371
synonymsManagementAPIService.putSynonymsSet(
371372
synonymSetId,
372373
randomSynonymsSet(1, 1),
373-
0,
374+
TimeValue.MINUS_ONE,
374375
new ActionListener<>() {
375376
@Override
376377
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {
@@ -391,7 +392,7 @@ public void onFailure(Exception e) {
391392
synonymsManagementAPIService.putSynonymRule(
392393
synonymSetId,
393394
randomSynonymRule(randomIdentifier()),
394-
0,
395+
TimeValue.MINUS_ONE,
395396
new ActionListener<>() {
396397
@Override
397398
public void onResponse(SynonymsManagementAPIService.SynonymsReloadResult synonymsReloadResult) {

server/src/main/java/org/elasticsearch/action/synonyms/PutSynonymRuleAction.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.common.io.stream.StreamInput;
2020
import org.elasticsearch.common.io.stream.StreamOutput;
2121
import org.elasticsearch.common.xcontent.XContentHelper;
22+
import org.elasticsearch.core.TimeValue;
2223
import org.elasticsearch.synonyms.SynonymRule;
2324
import org.elasticsearch.synonyms.SynonymsManagementAPIService;
2425
import org.elasticsearch.xcontent.ConstructingObjectParser;
@@ -35,7 +36,7 @@ public class PutSynonymRuleAction extends ActionType<SynonymUpdateResponse> {
3536
public static final PutSynonymRuleAction INSTANCE = new PutSynonymRuleAction();
3637
public static final String NAME = "cluster:admin/synonym_rules/put";
3738

38-
public static final int DEFAULT_TIMEOUT = 5;
39+
public static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(10);
3940

4041
public PutSynonymRuleAction() {
4142
super(NAME);
@@ -44,7 +45,7 @@ public PutSynonymRuleAction() {
4445
public static class Request extends ActionRequest {
4546
private final String synonymsSetId;
4647
private final SynonymRule synonymRule;
47-
private final int timeout;
48+
private final TimeValue timeout;
4849

4950
public static final ParseField SYNONYMS_FIELD = new ParseField(SynonymsManagementAPIService.SYNONYMS_FIELD);
5051
private static final ConstructingObjectParser<SynonymRule, String> PARSER = new ConstructingObjectParser<>(
@@ -62,14 +63,15 @@ public Request(StreamInput in) throws IOException {
6263
this.synonymsSetId = in.readString();
6364
this.synonymRule = new SynonymRule(in);
6465
if (in.getTransportVersion().onOrAfter(TransportVersions.SYNONYMS_UPDATE_TIMEOUT)) {
65-
this.timeout = in.readInt();
66+
this.timeout = in.readTimeValue();
6667
} else {
6768
this.timeout = DEFAULT_TIMEOUT;
6869
}
6970
}
7071

71-
public Request(String synonymsSetId, String synonymRuleId, int timeout, BytesReference content, XContentType contentType)
72+
public Request(String synonymsSetId, String synonymRuleId, TimeValue timeout, BytesReference content, XContentType contentType)
7273
throws IOException {
74+
Objects.requireNonNull(timeout);
7375
this.synonymsSetId = synonymsSetId;
7476
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, content, contentType)) {
7577
this.synonymRule = PARSER.apply(parser, synonymRuleId);
@@ -79,7 +81,8 @@ public Request(String synonymsSetId, String synonymRuleId, int timeout, BytesRef
7981
this.timeout = timeout;
8082
}
8183

82-
Request(String synonymsSetId, SynonymRule synonymRule, int timeout) {
84+
Request(String synonymsSetId, SynonymRule synonymRule, TimeValue timeout) {
85+
Objects.requireNonNull(timeout);
8386
this.synonymsSetId = synonymsSetId;
8487
this.synonymRule = synonymRule;
8588
this.timeout = timeout;
@@ -94,13 +97,6 @@ public ActionRequestValidationException validate() {
9497
if (Strings.isNullOrEmpty(synonymRule.id())) {
9598
validationException = ValidateActions.addValidationError("synonym rule id must be specified", validationException);
9699
}
97-
if (timeout < 0) {
98-
validationException = ValidateActions.addValidationError("timeout must be greater than or equal to 0", validationException);
99-
}
100-
String error = synonymRule.validate();
101-
if (error != null) {
102-
validationException = ValidateActions.addValidationError(error, validationException);
103-
}
104100

105101
return validationException;
106102
}
@@ -111,7 +107,7 @@ public void writeTo(StreamOutput out) throws IOException {
111107
out.writeString(synonymsSetId);
112108
synonymRule.writeTo(out);
113109
if (out.getTransportVersion().onOrAfter(TransportVersions.SYNONYMS_UPDATE_TIMEOUT)) {
114-
out.writeInt(timeout);
110+
out.writeTimeValue(timeout);
115111
}
116112
}
117113

@@ -123,7 +119,7 @@ public SynonymRule synonymRule() {
123119
return synonymRule;
124120
}
125121

126-
public int timeout() {
122+
public TimeValue timeout() {
127123
return timeout;
128124
}
129125

server/src/main/java/org/elasticsearch/action/synonyms/PutSynonymsAction.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.common.io.stream.StreamInput;
2020
import org.elasticsearch.common.io.stream.StreamOutput;
2121
import org.elasticsearch.common.xcontent.XContentHelper;
22+
import org.elasticsearch.core.TimeValue;
2223
import org.elasticsearch.synonyms.SynonymRule;
2324
import org.elasticsearch.synonyms.SynonymsManagementAPIService;
2425
import org.elasticsearch.xcontent.ConstructingObjectParser;
@@ -46,7 +47,7 @@ public PutSynonymsAction() {
4647
public static class Request extends ActionRequest {
4748
private final String synonymsSetId;
4849
private final SynonymRule[] synonymRules;
49-
private final int timeout;
50+
private final TimeValue timeout;
5051

5152
public static final ParseField SYNONYMS_SET_FIELD = new ParseField(SynonymsManagementAPIService.SYNONYMS_SET_FIELD);
5253
private static final ConstructingObjectParser<SynonymRule[], Void> PARSER = new ConstructingObjectParser<>("synonyms_set", args -> {
@@ -64,13 +65,15 @@ public Request(StreamInput in) throws IOException {
6465
this.synonymsSetId = in.readString();
6566
this.synonymRules = in.readArray(SynonymRule::new, SynonymRule[]::new);
6667
if (in.getTransportVersion().onOrAfter(TransportVersions.SYNONYMS_UPDATE_TIMEOUT)) {
67-
this.timeout = in.readInt();
68+
this.timeout = in.readTimeValue();
6869
} else {
6970
this.timeout = DEFAULT_TIMEOUT;
7071
}
7172
}
7273

73-
public Request(String synonymsSetId, int timeout, BytesReference content, XContentType contentType) throws IOException {
74+
public Request(String synonymsSetId, TimeValue timeout, BytesReference content, XContentType contentType) throws IOException {
75+
Objects.requireNonNull(timeout);
76+
Objects.requireNonNull(synonymsSetId);
7477
this.synonymsSetId = synonymsSetId;
7578
this.timeout = timeout;
7679
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, content, contentType)) {
@@ -80,7 +83,10 @@ public Request(String synonymsSetId, int timeout, BytesReference content, XConte
8083
}
8184
}
8285

83-
Request(String synonymsSetId, SynonymRule[] synonymRules, int timeout) {
86+
Request(String synonymsSetId, SynonymRule[] synonymRules, TimeValue timeout) {
87+
Objects.requireNonNull(timeout);
88+
Objects.requireNonNull(synonymsSetId);
89+
Objects.requireNonNull(synonymRules);
8490
this.synonymsSetId = synonymsSetId;
8591
this.synonymRules = synonymRules;
8692
this.timeout = timeout;
@@ -107,15 +113,15 @@ public void writeTo(StreamOutput out) throws IOException {
107113
out.writeString(synonymsSetId);
108114
out.writeArray(synonymRules);
109115
if (out.getTransportVersion().onOrAfter(TransportVersions.SYNONYMS_UPDATE_TIMEOUT)) {
110-
out.writeInt(timeout);
116+
out.writeTimeValue(timeout);
111117
}
112118
}
113119

114120
public String synonymsSetId() {
115121
return synonymsSetId;
116122
}
117123

118-
public int timeout() {
124+
public TimeValue timeout() {
119125
return timeout;
120126
}
121127

server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymRuleAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.client.internal.node.NodeClient;
1515
import org.elasticsearch.rest.BaseRestHandler;
1616
import org.elasticsearch.rest.RestRequest;
17+
import org.elasticsearch.rest.RestUtils;
1718
import org.elasticsearch.rest.Scope;
1819
import org.elasticsearch.rest.ServerlessScope;
1920
import org.elasticsearch.rest.action.RestToXContentListener;
@@ -41,7 +42,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
4142
PutSynonymRuleAction.Request request = new PutSynonymRuleAction.Request(
4243
restRequest.param("synonymsSet"),
4344
restRequest.param("synonymRuleId"),
44-
restRequest.paramAsInt("timeout", PutSynonymRuleAction.DEFAULT_TIMEOUT),
45+
RestUtils.getAckTimeout(restRequest),
4546
restRequest.content(),
4647
restRequest.getXContentType()
4748
);

server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymsAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.client.internal.node.NodeClient;
1515
import org.elasticsearch.rest.BaseRestHandler;
1616
import org.elasticsearch.rest.RestRequest;
17+
import org.elasticsearch.rest.RestUtils;
1718
import org.elasticsearch.rest.Scope;
1819
import org.elasticsearch.rest.ServerlessScope;
1920
import org.elasticsearch.rest.action.RestToXContentListener;
@@ -41,7 +42,7 @@ public List<Route> routes() {
4142
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
4243
PutSynonymsAction.Request request = new PutSynonymsAction.Request(
4344
restRequest.param("synonymsSet"),
44-
restRequest.paramAsInt("timeout", DEFAULT_TIMEOUT),
45+
RestUtils.getAckTimeout(restRequest),
4546
restRequest.content(),
4647
restRequest.getXContentType()
4748
);

server/src/main/java/org/elasticsearch/synonyms/SynonymsManagementAPIService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private static void logUniqueFailureMessagesWithIndices(List<BulkItemResponse.Fa
306306
});
307307
}
308308

309-
public void putSynonymsSet(String synonymSetId, SynonymRule[] synonymsSet, int timeout, ActionListener<SynonymsReloadResult> listener) {
309+
public void putSynonymsSet(String synonymSetId, SynonymRule[] synonymsSet, TimeValue timeout, ActionListener<SynonymsReloadResult> listener) {
310310
if (synonymsSet.length > maxSynonymsSets) {
311311
listener.onFailure(
312312
new IllegalArgumentException("The number of synonyms rules in a synonym set cannot exceed " + maxSynonymsSets)
@@ -377,7 +377,7 @@ void bulkUpdateSynonymsSet(String synonymSetId, SynonymRule[] synonymsSet, Actio
377377
bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).execute(listener);
378378
}
379379

380-
public void putSynonymRule(String synonymsSetId, SynonymRule synonymRule, int timeout, ActionListener<SynonymsReloadResult> listener) {
380+
public void putSynonymRule(String synonymsSetId, SynonymRule synonymRule, TimeValue timeout, ActionListener<SynonymsReloadResult> listener) {
381381
checkSynonymSetExists(synonymsSetId, listener.delegateFailureAndWrap((l1, obj) -> {
382382
// Count synonym rules to check if we're at maximum
383383
BoolQueryBuilder queryFilter = QueryBuilders.boolQuery()
@@ -399,13 +399,13 @@ public void putSynonymRule(String synonymsSetId, SynonymRule synonymRule, int ti
399399
new IllegalArgumentException("The number of synonym rules in a synonyms set cannot exceed " + maxSynonymsSets)
400400
);
401401
} else {
402-
indexSynonymRule(synonymsSetId, synonymRule, searchListener, timeout);
402+
indexSynonymRule(synonymsSetId, synonymRule, timeout, searchListener);
403403
}
404404
}));
405405
}));
406406
}
407407

408-
private void indexSynonymRule(String synonymsSetId, SynonymRule synonymRule, ActionListener<SynonymsReloadResult> listener, int timeout)
408+
private void indexSynonymRule(String synonymsSetId, SynonymRule synonymRule, TimeValue timeout, ActionListener<SynonymsReloadResult> listener)
409409
throws IOException {
410410
IndexRequest indexRequest = createSynonymRuleIndexRequest(synonymsSetId, synonymRule).setRefreshPolicy(
411411
WriteRequest.RefreshPolicy.IMMEDIATE
@@ -557,10 +557,10 @@ private <T> void ensureSynonymsSearchableAndReloadAnalyzers(
557557
boolean preview,
558558
ActionListener<SynonymsReloadResult> listener,
559559
UpdateSynonymsResultStatus synonymsOperationResult,
560-
int timeout
560+
TimeValue timeout
561561
) {
562562
// Ensure synonyms index is searchable if timeout is present
563-
if (timeout > 0) {
563+
if (TimeValue.MINUS_ONE.equals(timeout) == false) {
564564
checkSynonymsIndexHealth(timeout, listener.delegateFailure((l, response) -> {
565565
if (response.isTimedOut()) {
566566
l.onFailure(new IndexCreationException("synonyms index [" + SYNONYMS_ALIAS_NAME + "] is not searchable. "
@@ -576,8 +576,8 @@ private <T> void ensureSynonymsSearchableAndReloadAnalyzers(
576576
}
577577

578578
// Allows checking failures in tests
579-
void checkSynonymsIndexHealth(int timeout, ActionListener<ClusterHealthResponse> listener) {
580-
ClusterHealthRequest healthRequest = new ClusterHealthRequest(TimeValue.timeValueSeconds(timeout), SYNONYMS_ALIAS_NAME)
579+
void checkSynonymsIndexHealth(TimeValue timeout, ActionListener<ClusterHealthResponse> listener) {
580+
ClusterHealthRequest healthRequest = new ClusterHealthRequest(timeout, SYNONYMS_ALIAS_NAME)
581581
.waitForGreenStatus();
582582

583583
client.execute(TransportClusterHealthAction.TYPE, healthRequest, listener);

server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymRuleActionRequestSerializingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected Writeable.Reader<PutSynonymRuleAction.Request> instanceReader() {
2222

2323
@Override
2424
protected PutSynonymRuleAction.Request createTestInstance() {
25-
return new PutSynonymRuleAction.Request(randomIdentifier(), SynonymsTestUtils.randomSynonymRule(), randomNonNegativeInt());
25+
return new PutSynonymRuleAction.Request(randomIdentifier(), SynonymsTestUtils.randomSynonymRule(), randomTimeValue());
2626
}
2727

2828
@Override

server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymsActionRequestSerializingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected Writeable.Reader<PutSynonymsAction.Request> instanceReader() {
2525

2626
@Override
2727
protected PutSynonymsAction.Request createTestInstance() {
28-
return new PutSynonymsAction.Request(randomIdentifier(), randomSynonymsSet(), randomNonNegativeInt());
28+
return new PutSynonymsAction.Request(randomIdentifier(), randomSynonymsSet(), randomTimeValue());
2929
}
3030

3131
@Override

0 commit comments

Comments
 (0)