Skip to content

Commit 2cc4de6

Browse files
committed
Generalize version check assertion
1 parent e0630a6 commit 2cc4de6

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

server/src/test/java/org/elasticsearch/index/codec/tsdb/TsdbDocValueBwcTests.java

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Arrays;
5151
import java.util.Locale;
5252
import java.util.Map;
53+
import java.util.function.Consumer;
5354
import java.util.function.IntSupplier;
5455

5556
import static org.elasticsearch.index.codec.tsdb.es819.ES819TSDBDocValuesFormatTests.TestES819TSDBDocValuesFormatVersion0;
@@ -67,7 +68,12 @@ public void testMixedIndex() throws Exception {
6768
public void testMixedIndexDocValueVersion0ToCurrent() throws Exception {
6869
var oldCodec = TestUtil.alwaysDocValuesFormat(new TestES819TSDBDocValuesFormatVersion0());
6970
var newCodec = TestUtil.alwaysDocValuesFormat(new ES819TSDBDocValuesFormat());
70-
testMixedIndex(oldCodec, newCodec);
71+
testMixedIndex(
72+
oldCodec,
73+
newCodec,
74+
this::assertVersion819,
75+
this::assertVersion819
76+
);
7177
}
7278

7379
public void testMixedIndex816To900Lucene101() throws Exception {
@@ -92,7 +98,34 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
9298
testMixedIndex(oldCodec, newCodec);
9399
}
94100

95-
void testMixedIndex(Codec oldCodec, Codec newCodec) throws IOException, NoSuchFieldException, IllegalAccessException,
101+
void assertFieldInfoDocValuesFormat(DirectoryReader reader, String expectedSuffix, String expectedFormat) throws IOException, NoSuchFieldException, IllegalAccessException {
102+
// Assert per field format field info attributes:
103+
// (XPerFieldDocValuesFormat must produce the same attributes as PerFieldDocValuesFormat for BWC.
104+
// Otherwise, doc values fields may disappear)
105+
for (var leaf : reader.leaves()) {
106+
for (var fieldInfo : leaf.reader().getFieldInfos()) {
107+
assertThat(fieldInfo.attributes(), Matchers.aMapWithSize(2));
108+
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.suffix", expectedSuffix));
109+
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.format", expectedFormat));
110+
}
111+
}
112+
}
113+
114+
void assertVersion87(DirectoryReader reader) throws IOException, NoSuchFieldException, IllegalAccessException {
115+
assert87DocValuesFormatVersion(reader);
116+
assertFieldInfoDocValuesFormat(reader, "0", "ES87TSDB");
117+
}
118+
119+
void assertVersion819(DirectoryReader reader) throws IOException, NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
120+
assert819DocValuesFormatVersion(reader);
121+
assertFieldInfoDocValuesFormat(reader, "0", "ES819TSDB");
122+
}
123+
124+
void testMixedIndex(Codec oldCodec, Codec newCodec) throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
125+
testMixedIndex(oldCodec, newCodec, this::assertVersion87, this::assertVersion819);
126+
}
127+
128+
void testMixedIndex(Codec oldCodec, Codec newCodec, VersionAssert assertOldVersion, VersionAssert assertNewVersion) throws IOException, NoSuchFieldException, IllegalAccessException,
96129
ClassNotFoundException {
97130
String timestampField = "@timestamp";
98131
String hostnameField = "host.name";
@@ -144,17 +177,7 @@ void testMixedIndex(Codec oldCodec, Codec newCodec) throws IOException, NoSuchFi
144177
}
145178
// Check documents before force merge:
146179
try (var reader = DirectoryReader.open(dir)) {
147-
assertOldDocValuesFormatVersion(reader);
148-
// Assert per field format field info attributes:
149-
// (XPerFieldDocValuesFormat must produce the same attributes as PerFieldDocValuesFormat for BWC.
150-
// Otherwise, doc values fields may disappear)
151-
for (var leaf : reader.leaves()) {
152-
for (var fieldInfo : leaf.reader().getFieldInfos()) {
153-
assertThat(fieldInfo.attributes(), Matchers.aMapWithSize(2));
154-
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.suffix", "0"));
155-
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.format", "ES87TSDB"));
156-
}
157-
}
180+
assertOldVersion.run(reader);
158181

159182
var hostNameDV = MultiDocValues.getSortedValues(reader, hostnameField);
160183
assertNotNull(hostNameDV);
@@ -213,17 +236,9 @@ void testMixedIndex(Codec oldCodec, Codec newCodec) throws IOException, NoSuchFi
213236
try (var reader = DirectoryReader.open(iw)) {
214237
assertEquals(1, reader.leaves().size());
215238
assertEquals(numDocs, reader.maxDoc());
216-
assertNewDocValuesFormatVersion(reader);
217-
var leaf = reader.leaves().get(0).reader();
218-
// Assert per field format field info attributes:
219-
// (XPerFieldDocValuesFormat must produce the same attributes as PerFieldDocValuesFormat for BWC.
220-
// Otherwise, doc values fields may disappear)
221-
for (var fieldInfo : leaf.getFieldInfos()) {
222-
assertThat(fieldInfo.attributes(), Matchers.aMapWithSize(2));
223-
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.suffix", "0"));
224-
assertThat(fieldInfo.attributes(), Matchers.hasEntry("PerFieldDocValuesFormat.format", "ES819TSDB"));
225-
}
239+
assertNewVersion.run(reader);
226240

241+
var leaf = reader.leaves().get(0).reader();
227242
var hostNameDV = leaf.getSortedDocValues(hostnameField);
228243
assertNotNull(hostNameDV);
229244
var timestampDV = DocValues.unwrapSingleton(leaf.getSortedNumericDocValues(timestampField));
@@ -393,7 +408,7 @@ private IndexWriterConfig getTimeSeriesIndexWriterConfig(String hostnameField, S
393408

394409
// A hacky way to figure out whether doc values format is written in what version. Need to use reflection, because
395410
// PerFieldDocValuesFormat hides the doc values formats it wraps.
396-
private void assertOldDocValuesFormatVersion(DirectoryReader reader) throws NoSuchFieldException, IllegalAccessException, IOException {
411+
private void assert87DocValuesFormatVersion(DirectoryReader reader) throws NoSuchFieldException, IllegalAccessException, IOException {
397412
if (System.getSecurityManager() != null) {
398413
// With jvm version 24 entitlements are used and security manager is nog longer used.
399414
// Making this assertion work with security manager requires granting the entire test codebase privileges to use
@@ -414,7 +429,7 @@ private void assertOldDocValuesFormatVersion(DirectoryReader reader) throws NoSu
414429
}
415430
}
416431

417-
private void assertNewDocValuesFormatVersion(DirectoryReader reader) throws NoSuchFieldException, IllegalAccessException, IOException,
432+
private void assert819DocValuesFormatVersion(DirectoryReader reader) throws NoSuchFieldException, IllegalAccessException, IOException,
418433
ClassNotFoundException {
419434

420435
for (var leafReaderContext : reader.leaves()) {
@@ -459,4 +474,7 @@ private static Field getFormatsFieldFromPerFieldFieldsReader(Class<?> c) throws
459474
return field;
460475
}
461476

477+
interface VersionAssert {
478+
void run(DirectoryReader reader) throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException;
479+
}
462480
}

0 commit comments

Comments
 (0)