Skip to content

Commit d88104c

Browse files
authored
Fix long handling for version, if_seq_no and if_primary_term (#69238)
After #66197 we've lost ability to use longs for version, if_seq_no and if_primary_term (only ints would work). That slipped through because 1 test went missing after refactoring in #50131. This change fixes that with new ConfigurationUtils.readOptionalStringOrLongProperty method and and brings back the test.
1 parent 4a6c957 commit d88104c

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
175175
dataMap, Metadata.ROUTING.getFieldName());
176176
Long version = null;
177177
if (dataMap.containsKey(Metadata.VERSION.getFieldName())) {
178-
String versionValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
178+
String versionValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
179179
dataMap, Metadata.VERSION.getFieldName());
180180
if (versionValue != null) {
181181
version = Long.valueOf(versionValue);
@@ -191,7 +191,7 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
191191
IngestDocument ingestDocument =
192192
new IngestDocument(index, id, routing, version, versionType, document);
193193
if (dataMap.containsKey(Metadata.IF_SEQ_NO.getFieldName())) {
194-
String ifSeqNoValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
194+
String ifSeqNoValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
195195
dataMap, Metadata.IF_SEQ_NO.getFieldName());
196196
if (ifSeqNoValue != null) {
197197
Long ifSeqNo = Long.valueOf(ifSeqNoValue);
@@ -201,7 +201,7 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
201201
}
202202
}
203203
if (dataMap.containsKey(Metadata.IF_PRIMARY_TERM.getFieldName())) {
204-
String ifPrimaryTermValue = ConfigurationUtils.readOptionalStringOrIntProperty(null, null,
204+
String ifPrimaryTermValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
205205
dataMap, Metadata.IF_PRIMARY_TERM.getFieldName());
206206
if (ifPrimaryTermValue != null) {
207207
Long ifPrimaryTerm = Long.valueOf(ifPrimaryTermValue);

server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ public static String readOptionalStringOrIntProperty(String processorType, Strin
139139
return readStringOrInt(processorType, processorTag, propertyName, value);
140140
}
141141

142+
private static String readStringOrLong(String processorType, String processorTag,
143+
String propertyName, Object value) {
144+
if (value == null) {
145+
return null;
146+
}
147+
if (value instanceof String) {
148+
return (String) value;
149+
} else if (value instanceof Long || value instanceof Integer) {
150+
return String.valueOf(value);
151+
}
152+
throw newConfigurationException(processorType, processorTag, propertyName,
153+
"property isn't a string or long, but of type [" + value.getClass().getName() + "]");
154+
}
155+
156+
/**
157+
* Returns and removes the specified property from the specified configuration map.
158+
*
159+
* If the property value isn't of type string or long a {@link ElasticsearchParseException} is thrown.
160+
*/
161+
public static String readOptionalStringOrLongProperty(String processorType, String processorTag,
162+
Map<String, Object> configuration, String propertyName) {
163+
Object value = configuration.remove(propertyName);
164+
if (value == null) {
165+
return null;
166+
}
167+
return readStringOrLong(processorType, processorTag, propertyName, value);
168+
}
169+
142170
public static Boolean readBooleanProperty(String processorType, String processorTag, Map<String, Object> configuration,
143171
String propertyName, boolean defaultValue) {
144172
Object value = configuration.remove(propertyName);

server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void testParseUsingPipelineStore() throws Exception {
101101
assertThat(actualRequest.getPipeline().getProcessors().size(), equalTo(1));
102102
}
103103

104-
public void innerTestParseWithProvidedPipeline() throws Exception {
104+
public void testParseWithProvidedPipeline() throws Exception {
105105
int numDocs = randomIntBetween(1, 10);
106106

107107
Map<String, Object> requestContent = new HashMap<>();
@@ -113,12 +113,22 @@ public void innerTestParseWithProvidedPipeline() throws Exception {
113113
Map<String, Object> expectedDoc = new HashMap<>();
114114
List<IngestDocument.Metadata> fields = Arrays.asList(INDEX, ID, ROUTING, VERSION, VERSION_TYPE, IF_SEQ_NO, IF_PRIMARY_TERM);
115115
for(IngestDocument.Metadata field : fields) {
116-
if (field == VERSION_TYPE) {
116+
if (field == VERSION) {
117+
Object value = randomBoolean() ? randomLong() : randomInt();
118+
doc.put(field.getFieldName(), randomBoolean() ? value : value.toString());
119+
long longValue = (long) value;
120+
expectedDoc.put(field.getFieldName(), longValue);
121+
} else if (field == VERSION_TYPE) {
117122
String value = VersionType.toString(
118123
randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE)
119124
);
120125
doc.put(field.getFieldName(), value);
121126
expectedDoc.put(field.getFieldName(), value);
127+
} else if (field == IF_SEQ_NO || field == IF_PRIMARY_TERM) {
128+
Object value = randomBoolean() ? randomNonNegativeLong() : randomInt(1000);
129+
doc.put(field.getFieldName(), randomBoolean() ? value : value.toString());
130+
long longValue = (long) value;
131+
expectedDoc.put(field.getFieldName(), longValue);
122132
} else {
123133
if (randomBoolean()) {
124134
String value = randomAlphaOfLengthBetween(1, 10);

0 commit comments

Comments
 (0)