Skip to content

Commit 3438942

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

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();
@@ -551,7 +553,6 @@ public void testSortFieldSerialization() throws IOException {
551553
Tuple<SortField, SortField> sortFieldTuple = randomSortField();
552554
SortField deserialized = copyInstance(
553555
sortFieldTuple.v1(),
554-
EMPTY_REGISTRY,
555556
Lucene::writeSortField,
556557
Lucene::readSortField,
557558
TransportVersionUtils.randomVersion(random())
@@ -563,14 +564,25 @@ public void testSortValueSerialization() throws IOException {
563564
Object sortValue = randomSortValue();
564565
Object deserialized = copyInstance(
565566
sortValue,
566-
EMPTY_REGISTRY,
567567
Lucene::writeSortValue,
568568
Lucene::readSortValue,
569569
TransportVersionUtils.randomVersion(random())
570570
);
571571
assertEquals(sortValue, deserialized);
572572
}
573573

574+
private static <T> T copyInstance(T original, Writeable.Writer<T> writer, Writeable.Reader<T> reader, TransportVersion version)
575+
throws IOException {
576+
try (BytesStreamOutput output = new BytesStreamOutput()) {
577+
output.setTransportVersion(version);
578+
writer.write(output, original);
579+
try (StreamInput in = output.bytes().streamInput()) {
580+
in.setTransportVersion(version);
581+
return reader.read(in);
582+
}
583+
}
584+
}
585+
574586
public static Object randomSortValue() {
575587
return switch (randomIntBetween(0, 9)) {
576588
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
@@ -65,6 +65,7 @@
6565
import org.elasticsearch.common.bytes.BytesReference;
6666
import org.elasticsearch.common.bytes.CompositeBytesReference;
6767
import org.elasticsearch.common.io.stream.BytesStreamOutput;
68+
import org.elasticsearch.common.io.stream.DelayableWriteable;
6869
import org.elasticsearch.common.io.stream.NamedWriteable;
6970
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
7071
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -1849,7 +1850,7 @@ public static <C extends NamedWriteable, T extends C> C copyNamedWriteable(
18491850
);
18501851
}
18511852

1852-
protected static <T> T copyInstance(
1853+
protected static <T extends Writeable> T copyInstance(
18531854
T original,
18541855
NamedWriteableRegistry namedWriteableRegistry,
18551856
Writeable.Writer<T> writer,
@@ -1859,9 +1860,20 @@ protected static <T> T copyInstance(
18591860
try (BytesStreamOutput output = new BytesStreamOutput()) {
18601861
output.setTransportVersion(version);
18611862
writer.write(output, original);
1862-
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1863-
in.setTransportVersion(version);
1864-
return reader.read(in);
1863+
if (randomBoolean()) {
1864+
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1865+
in.setTransportVersion(version);
1866+
return reader.read(in);
1867+
}
1868+
} else {
1869+
BytesReference bytesReference = output.copyBytes();
1870+
output.reset();
1871+
output.writeBytesReference(bytesReference);
1872+
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
1873+
in.setTransportVersion(version);
1874+
DelayableWriteable<T> delayableWriteable = DelayableWriteable.delayed(reader, in);
1875+
return delayableWriteable.expand();
1876+
}
18651877
}
18661878
}
18671879
}

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)