Skip to content

Commit 5b6387b

Browse files
authored
Deduplicate the list of names when deserializing InternalTopMetrics (#116298) (#116417)
use deduplication infrastructure to deduplicate the names of metrics in InternalTopMetrics.
1 parent fd97a9b commit 5b6387b

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

server/src/test/java/org/elasticsearch/common/lucene/LuceneTests.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
import org.apache.lucene.tests.store.MockDirectoryWrapper;
5151
import org.apache.lucene.util.Bits;
5252
import org.apache.lucene.util.BytesRef;
53-
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
53+
import org.elasticsearch.TransportVersion;
54+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
55+
import org.elasticsearch.common.io.stream.StreamInput;
56+
import org.elasticsearch.common.io.stream.Writeable;
5457
import org.elasticsearch.core.IOUtils;
5558
import org.elasticsearch.core.Tuple;
5659
import org.elasticsearch.index.fielddata.IndexFieldData;
@@ -74,7 +77,6 @@
7477
import static org.hamcrest.Matchers.equalTo;
7578

7679
public class LuceneTests extends ESTestCase {
77-
private static final NamedWriteableRegistry EMPTY_REGISTRY = new NamedWriteableRegistry(Collections.emptyList());
7880

7981
public void testCleanIndex() throws IOException {
8082
MockDirectoryWrapper dir = newMockDirectory();
@@ -566,7 +568,6 @@ public void testSortFieldSerialization() throws IOException {
566568
Tuple<SortField, SortField> sortFieldTuple = randomSortField();
567569
SortField deserialized = copyInstance(
568570
sortFieldTuple.v1(),
569-
EMPTY_REGISTRY,
570571
Lucene::writeSortField,
571572
Lucene::readSortField,
572573
TransportVersionUtils.randomVersion(random())
@@ -578,14 +579,25 @@ public void testSortValueSerialization() throws IOException {
578579
Object sortValue = randomSortValue();
579580
Object deserialized = copyInstance(
580581
sortValue,
581-
EMPTY_REGISTRY,
582582
Lucene::writeSortValue,
583583
Lucene::readSortValue,
584584
TransportVersionUtils.randomVersion(random())
585585
);
586586
assertEquals(sortValue, deserialized);
587587
}
588588

589+
private static <T> T copyInstance(T original, Writeable.Writer<T> writer, Writeable.Reader<T> reader, TransportVersion version)
590+
throws IOException {
591+
try (BytesStreamOutput output = new BytesStreamOutput()) {
592+
output.setTransportVersion(version);
593+
writer.write(output, original);
594+
try (StreamInput in = output.bytes().streamInput()) {
595+
in.setTransportVersion(version);
596+
return reader.read(in);
597+
}
598+
}
599+
}
600+
589601
public static Object randomSortValue() {
590602
return switch (randomIntBetween(0, 9)) {
591603
case 0 -> null;

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.elasticsearch.common.bytes.BytesReference;
6464
import org.elasticsearch.common.bytes.CompositeBytesReference;
6565
import org.elasticsearch.common.io.stream.BytesStreamOutput;
66+
import org.elasticsearch.common.io.stream.DelayableWriteable;
6667
import org.elasticsearch.common.io.stream.NamedWriteable;
6768
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
6869
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -1830,7 +1831,7 @@ public static <C extends NamedWriteable, T extends C> C copyNamedWriteable(
18301831
);
18311832
}
18321833

1833-
protected static <T> T copyInstance(
1834+
protected static <T extends Writeable> T copyInstance(
18341835
T original,
18351836
NamedWriteableRegistry namedWriteableRegistry,
18361837
Writeable.Writer<T> writer,
@@ -1840,9 +1841,20 @@ protected static <T> T copyInstance(
18401841
try (BytesStreamOutput output = new BytesStreamOutput()) {
18411842
output.setTransportVersion(version);
18421843
writer.write(output, original);
1843-
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1844-
in.setTransportVersion(version);
1845-
return reader.read(in);
1844+
if (randomBoolean()) {
1845+
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1846+
in.setTransportVersion(version);
1847+
return reader.read(in);
1848+
}
1849+
} else {
1850+
BytesReference bytesReference = output.copyBytes();
1851+
output.reset();
1852+
output.writeBytesReference(bytesReference);
1853+
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1854+
in.setTransportVersion(version);
1855+
DelayableWriteable<T> delayableWriteable = DelayableWriteable.delayed(reader, in);
1856+
return delayableWriteable.expand();
1857+
}
18461858
}
18471859
}
18481860
}

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetrics.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.xpack.analytics.topmetrics;
88

99
import org.apache.lucene.util.PriorityQueue;
10+
import org.elasticsearch.common.io.stream.DelayableWriteable;
1011
import org.elasticsearch.common.io.stream.StreamInput;
1112
import org.elasticsearch.common.io.stream.StreamOutput;
1213
import org.elasticsearch.common.io.stream.Writeable;
@@ -68,7 +69,12 @@ static InternalTopMetrics buildEmptyAggregation(String name, List<String> metric
6869
public InternalTopMetrics(StreamInput in) throws IOException {
6970
super(in);
7071
sortOrder = SortOrder.readFromStream(in);
71-
metricNames = in.readStringCollectionAsList();
72+
final List<String> metricNames = in.readStringCollectionAsList();
73+
if (in instanceof DelayableWriteable.Deduplicator bo) {
74+
this.metricNames = bo.deduplicate(metricNames);
75+
} else {
76+
this.metricNames = metricNames;
77+
}
7278
size = in.readVInt();
7379
topMetrics = in.readCollectionAsList(TopMetric::new);
7480
}

0 commit comments

Comments
 (0)