Skip to content

Commit 0f46b56

Browse files
authored
Optimize IngestCtxMap construction (#120833) (#120926)
1 parent ecc4c92 commit 0f46b56

File tree

18 files changed

+133
-58
lines changed

18 files changed

+133
-58
lines changed

docs/changelog/120833.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 120833
2+
summary: Optimize `IngestCtxMap` construction
3+
area: Ingest Node
4+
type: enhancement
5+
issues: []

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public void testAutoConvertNotString() throws Exception {
527527
}
528528
default -> throw new UnsupportedOperationException();
529529
}
530-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", randomValue));
530+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", randomValue)));
531531
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
532532
processor.execute(ingestDocument);
533533
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -536,7 +536,7 @@ public void testAutoConvertNotString() throws Exception {
536536

537537
public void testAutoConvertStringNotMatched() throws Exception {
538538
String value = "notAnIntFloatOrBool";
539-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", value));
539+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", value)));
540540
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
541541
processor.execute(ingestDocument);
542542
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -546,7 +546,7 @@ public void testAutoConvertStringNotMatched() throws Exception {
546546
public void testAutoConvertMatchBoolean() throws Exception {
547547
boolean randomBoolean = randomBoolean();
548548
String booleanString = Boolean.toString(randomBoolean);
549-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", booleanString));
549+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", booleanString)));
550550
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
551551
processor.execute(ingestDocument);
552552
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -556,7 +556,7 @@ public void testAutoConvertMatchBoolean() throws Exception {
556556
public void testAutoConvertMatchInteger() throws Exception {
557557
int randomInt = randomInt();
558558
String randomString = Integer.toString(randomInt);
559-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", randomString));
559+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", randomString)));
560560
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
561561
processor.execute(ingestDocument);
562562
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -566,7 +566,7 @@ public void testAutoConvertMatchInteger() throws Exception {
566566
public void testAutoConvertMatchLong() throws Exception {
567567
long randomLong = randomLong();
568568
String randomString = Long.toString(randomLong);
569-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", randomString));
569+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", randomString)));
570570
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
571571
processor.execute(ingestDocument);
572572
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -577,7 +577,7 @@ public void testAutoConvertDoubleNotMatched() throws Exception {
577577
double randomDouble = randomDouble();
578578
String randomString = Double.toString(randomDouble);
579579
float randomFloat = Float.parseFloat(randomString);
580-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", randomString));
580+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", randomString)));
581581
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
582582
processor.execute(ingestDocument);
583583
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);
@@ -588,7 +588,7 @@ public void testAutoConvertDoubleNotMatched() throws Exception {
588588
public void testAutoConvertMatchFloat() throws Exception {
589589
float randomFloat = randomFloat();
590590
String randomString = Float.toString(randomFloat);
591-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("field", randomString));
591+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(Map.of("field", randomString)));
592592
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false);
593593
processor.execute(ingestDocument);
594594
Object convertedValue = ingestDocument.getFieldValue("field", Object.class);

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DissectProcessorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class DissectProcessorTests extends ESTestCase {
3131

3232
public void testMatch() {
33-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("message", "foo,bar,baz"));
33+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("message", "foo,bar,baz")));
3434
DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true);
3535
dissectProcessor.execute(ingestDocument);
3636
assertThat(ingestDocument.getFieldValue("a", String.class), equalTo("foo"));
@@ -45,7 +45,7 @@ public void testMatchOverwrite() {
4545
1,
4646
null,
4747
null,
48-
Map.of("message", "foo,bar,baz", "a", "willgetstompped")
48+
new HashMap<>(Map.of("message", "foo,bar,baz", "a", "willgetstompped"))
4949
);
5050
assertThat(ingestDocument.getFieldValue("a", String.class), equalTo("willgetstompped"));
5151
DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true);
@@ -62,7 +62,7 @@ public void testAdvancedMatch() {
6262
1,
6363
null,
6464
null,
65-
Map.of("message", "foo bar,,,,,,,baz nope:notagain 😊 🐇 🙃")
65+
new HashMap<>(Map.of("message", "foo bar,,,,,,,baz nope:notagain 😊 🐇 🙃"))
6666
);
6767
DissectProcessor dissectProcessor = new DissectProcessor(
6868
"",
@@ -81,7 +81,7 @@ public void testAdvancedMatch() {
8181
}
8282

8383
public void testMiss() {
84-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("message", "foo:bar,baz"));
84+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("message", "foo:bar,baz")));
8585
DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true);
8686
DissectException e = expectThrows(DissectException.class, () -> dissectProcessor.execute(ingestDocument));
8787
assertThat(e.getMessage(), containsString("Unable to find match for dissect pattern"));

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorTests.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testExecuteWithAsyncProcessor() throws Exception {
3939
values.add("foo");
4040
values.add("bar");
4141
values.add("baz");
42-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("values", values));
42+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("values", values)));
4343

4444
ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", new AsyncUpperCaseProcessor("_ingest._value"), false);
4545
execProcessor(processor, ingestDocument, (result, e) -> {});
@@ -55,7 +55,14 @@ public void testExecuteWithAsyncProcessor() throws Exception {
5555
}
5656

5757
public void testExecuteWithFailure() {
58-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("values", List.of("a", "b", "c")));
58+
IngestDocument ingestDocument = new IngestDocument(
59+
"_index",
60+
"_id",
61+
1,
62+
null,
63+
null,
64+
new HashMap<>(Map.of("values", List.of("a", "b", "c")))
65+
);
5966

6067
TestProcessor testProcessor = new TestProcessor(id -> {
6168
if ("c".equals(id.getFieldValue("_ingest._value", String.class))) {
@@ -173,7 +180,7 @@ public String getDescription() {
173180
int numValues = randomIntBetween(1, 10000);
174181
List<String> values = IntStream.range(0, numValues).mapToObj(i -> "").toList();
175182

176-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("values", values));
183+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("values", values)));
177184

178185
ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", innerProcessor, false);
179186
execProcessor(processor, ingestDocument, (result, e) -> {});
@@ -189,7 +196,7 @@ public void testModifyFieldsOutsideArray() {
189196
values.add("string");
190197
values.add(1);
191198
values.add(null);
192-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("values", values));
199+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("values", values)));
193200

194201
TemplateScript.Factory template = new TestTemplateService.MockTemplateScript.Factory("errors");
195202

@@ -282,7 +289,7 @@ public void testNestedForEachWithMapIteration() {
282289
Map<String, Object> innerMap3 = Map.of("foo3", 7, "bar3", 8, "baz3", 9, "otherKey", 42);
283290

284291
Map<String, Object> outerMap = Map.of("foo", innerMap1, "bar", innerMap2, "baz", innerMap3);
285-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("field", outerMap));
292+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("field", outerMap)));
286293

287294
List<String> visitedKeys = new ArrayList<>();
288295
List<Object> visitedValues = new ArrayList<>();
@@ -361,7 +368,7 @@ public void testRemovingFromTheSameField() {
361368

362369
public void testMapIteration() {
363370
Map<String, Object> mapValue = Map.of("foo", 1, "bar", 2, "baz", 3);
364-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("field", mapValue));
371+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("field", mapValue)));
365372

366373
List<String> encounteredKeys = new ArrayList<>();
367374
List<Object> encounteredValues = new ArrayList<>();
@@ -390,7 +397,7 @@ public void testMapIteration() {
390397

391398
public void testRemovalOfMapKey() {
392399
Map<String, Object> mapValue = Map.of("foo", 1, "bar", 2, "baz", 3);
393-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("field", mapValue));
400+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("field", mapValue)));
394401

395402
List<String> encounteredKeys = new ArrayList<>();
396403
List<Object> encounteredValues = new ArrayList<>();
@@ -419,7 +426,7 @@ public void testMapIterationWithAsyncProcessor() throws Exception {
419426
Map<String, Object> innerMap3 = Map.of("foo3", 7, "bar3", 8, "baz3", 9, "otherKey", 42);
420427

421428
Map<String, Object> outerMap = Map.of("foo", innerMap1, "bar", innerMap2, "baz", innerMap3);
422-
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, Map.of("field", outerMap));
429+
IngestDocument ingestDocument = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of("field", outerMap)));
423430

424431
List<String> visitedKeys = new ArrayList<>();
425432
List<Object> visitedValues = new ArrayList<>();

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testCreateWithMissingField() throws Exception {
7474
public void testCreateWithStrictParsingParameter() throws Exception {
7575
String fieldName = randomAlphaOfLength(10);
7676
String processorTag = randomAlphaOfLength(10);
77-
IngestDocument document = new IngestDocument("_index", "_id", 1, null, null, Map.of(fieldName, "123 \"foo\""));
77+
IngestDocument document = new IngestDocument("_index", "_id", 1, null, null, new HashMap<>(Map.of(fieldName, "123 \"foo\"")));
7878

7979
{
8080
Map<String, Object> strictConfig = new HashMap<>();

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,9 @@ public void testDuplicateKeys() throws Exception {
170170
String processorTag = randomAlphaOfLength(3);
171171
JsonProcessor lenientJsonProcessor = new JsonProcessor(processorTag, null, "a", null, true, REPLACE, true);
172172

173-
Map<String, Object> document = new HashMap<>();
174-
String json = "{\"a\": 1, \"a\": 2}";
175-
document.put("a", json);
176-
document.put("c", "see");
173+
Map<String, Object> document = Map.of("a", "{\"a\": 1, \"a\": 2}", "c", "see");
177174

178-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
175+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(document));
179176
lenientJsonProcessor.execute(ingestDocument);
180177

181178
Map<String, Object> sourceAndMetadata = ingestDocument.getSourceAndMetadata();
@@ -185,7 +182,7 @@ public void testDuplicateKeys() throws Exception {
185182
JsonProcessor strictJsonProcessor = new JsonProcessor(processorTag, null, "a", null, true, REPLACE, false);
186183
Exception exception = expectThrows(
187184
IllegalArgumentException.class,
188-
() -> strictJsonProcessor.execute(RandomDocumentPicks.randomIngestDocument(random(), document))
185+
() -> strictJsonProcessor.execute(RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>(document)))
189186
);
190187
assertThat(exception.getMessage(), containsString("Duplicate field 'a'"));
191188
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void test() throws Exception {
4040
}
4141

4242
public void testRootTarget() throws Exception {
43-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
43+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
4444
ingestDocument.setFieldValue("myField", "first=hello&second=world&second=universe");
4545
Processor processor = createKvProcessor("myField", "&", "=", null, null, null, false);
4646
processor.execute(ingestDocument);
@@ -49,7 +49,7 @@ public void testRootTarget() throws Exception {
4949
}
5050

5151
public void testKeySameAsSourceField() throws Exception {
52-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
52+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
5353
ingestDocument.setFieldValue("first", "first=hello");
5454
Processor processor = createKvProcessor("first", "&", "=", null, null, null, false);
5555
processor.execute(ingestDocument);

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Collections;
2020
import java.util.HashMap;
2121
import java.util.List;
22-
import java.util.Map;
2322

2423
import static org.hamcrest.Matchers.containsString;
2524
import static org.hamcrest.Matchers.equalTo;
@@ -266,7 +265,7 @@ public void testSortNullValue() throws Exception {
266265
}
267266

268267
public void testDescendingSortWithTargetField() throws Exception {
269-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
268+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
270269
int numItems = randomIntBetween(1, 10);
271270
List<String> fieldValue = new ArrayList<>(numItems);
272271
List<String> expectedResult = new ArrayList<>(numItems);
@@ -286,7 +285,7 @@ public void testDescendingSortWithTargetField() throws Exception {
286285
}
287286

288287
public void testAscendingSortWithTargetField() throws Exception {
289-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
288+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
290289
int numItems = randomIntBetween(1, 10);
291290
List<String> fieldValue = new ArrayList<>(numItems);
292291
List<String> expectedResult = new ArrayList<>(numItems);
@@ -306,7 +305,7 @@ public void testAscendingSortWithTargetField() throws Exception {
306305
}
307306

308307
public void testSortWithTargetFieldLeavesOriginalUntouched() throws Exception {
309-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
308+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
310309
List<Integer> fieldValue = List.of(1, 5, 4);
311310
List<Integer> expectedResult = new ArrayList<>(fieldValue);
312311
Collections.sort(expectedResult);

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/TerminateProcessorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.ingest.ValueSource;
1717
import org.elasticsearch.test.ESTestCase;
1818

19+
import java.util.HashMap;
1920
import java.util.Map;
2021

2122
import static org.elasticsearch.ingest.RandomDocumentPicks.randomIngestDocument;
@@ -48,7 +49,7 @@ public void testTerminateInPipeline() throws Exception {
4849
)
4950
)
5051
);
51-
IngestDocument input = randomIngestDocument(random(), Map.of("foo", "bar"));
52+
IngestDocument input = randomIngestDocument(random(), new HashMap<>(Map.of("foo", "bar")));
5253
PipelineOutput output = new PipelineOutput();
5354

5455
pipeline.execute(input, output::set);

modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void test() throws Exception {
9999
1L,
100100
"routing",
101101
VersionType.EXTERNAL,
102-
Map.of("_field", "89.160.20.128")
102+
new HashMap<>(Map.of("_field", "89.160.20.128"))
103103
);
104104
processor1.execute(document1);
105105
assertThat(document1.getSourceAndMetadata().get("geoip"), notNullValue());
@@ -109,7 +109,7 @@ public void test() throws Exception {
109109
1L,
110110
"routing",
111111
VersionType.EXTERNAL,
112-
Map.of("_field", "89.160.20.128")
112+
new HashMap<>(Map.of("_field", "89.160.20.128"))
113113
);
114114
processor2.execute(document2);
115115
assertThat(document2.getSourceAndMetadata().get("geoip"), notNullValue());

0 commit comments

Comments
 (0)