Skip to content

Commit 33e8155

Browse files
authored
Fix IndexServiceTests sort field test cases (#18352)
As far as I can tell these test cases have never been working as intended. They appear to have been created as a copy-paste error from `testIllegalFsyncInterval`. They passed because the createIndex call was failing due to setting the translog sync interval to an invalid value, which is what the try-catch was expecting. After removing the invalid setting the tests did not work properly for multiple reasons. I believe I have fixed the tests back to the original intent. The reason for digging into this test was to remove the deprecated `createIndex` calls from OpenSearchSingleNodeTestCase which take the now removed type parameter. I will follow up with a separate PR to do that work, as this was a significant enough change to stand on its own. Signed-off-by: Andrew Ross <andrross@amazon.com>
1 parent ab0827a commit 33e8155

File tree

2 files changed

+52
-65
lines changed

2 files changed

+52
-65
lines changed

server/src/test/java/org/opensearch/index/IndexServiceTests.java

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.apache.lucene.search.MatchAllDocsQuery;
3636
import org.apache.lucene.search.SortField;
37+
import org.apache.lucene.search.SortedNumericSortField;
3738
import org.apache.lucene.search.TopDocs;
3839
import org.opensearch.Version;
3940
import org.opensearch.action.support.ActiveShardCount;
@@ -61,16 +62,20 @@
6162
import org.opensearch.threadpool.ThreadPool;
6263

6364
import java.io.IOException;
65+
import java.util.Arrays;
6466
import java.util.Collection;
6567
import java.util.Collections;
68+
import java.util.Map;
6669
import java.util.concurrent.CountDownLatch;
6770
import java.util.concurrent.atomic.AtomicInteger;
6871
import java.util.concurrent.atomic.AtomicReference;
72+
import java.util.stream.Collectors;
6973

7074
import static org.opensearch.index.shard.IndexShardTestCase.getEngine;
7175
import static org.opensearch.test.InternalSettingsPlugin.TRANSLOG_RETENTION_CHECK_INTERVAL_SETTING;
7276
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
7377
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
78+
import static org.hamcrest.Matchers.containsInAnyOrder;
7479
import static org.hamcrest.core.IsEqual.equalTo;
7580

7681
/** Unit test(s) for IndexService */
@@ -81,6 +86,11 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
8186
return Collections.singleton(InternalSettingsPlugin.class);
8287
}
8388

89+
@Override
90+
protected boolean forbidPrivateIndexSettings() {
91+
return false;
92+
}
93+
8494
public static CompressedXContent filter(QueryBuilder filterBuilder) throws IOException {
8595
XContentBuilder builder = XContentFactory.jsonBuilder();
8696
filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
@@ -535,64 +545,41 @@ public void testUpdateRemoteTranslogBufferIntervalDynamically() {
535545
}
536546

537547
public void testIndexSort() {
538-
Settings settings = Settings.builder()
539-
.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), "0ms") // disable
540-
.putList("index.sort.field", "sortfield")
541-
.build();
542-
try {
543-
// Integer index sort should be remained to int sort type
544-
IndexService index = createIndex("test", settings, createTestMapping("integer"));
545-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.INT);
546-
547-
// Long index sort should be remained to long sort type
548-
index = createIndex("test", settings, createTestMapping("long"));
549-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.LONG);
550-
551-
// Float index sort should be remained to float sort type
552-
index = createIndex("test", settings, createTestMapping("float"));
553-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.FLOAT);
554-
555-
// Double index sort should be remained to double sort type
556-
index = createIndex("test", settings, createTestMapping("double"));
557-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.DOUBLE);
558-
559-
// String index sort should be remained to string sort type
560-
index = createIndex("test", settings, createTestMapping("string"));
561-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.STRING);
562-
} catch (IllegalArgumentException ex) {
563-
assertEquals("failed to parse value [0ms] for setting [index.translog.sync_interval], must be >= [100ms]", ex.getMessage());
564-
}
548+
runSortFieldTest(Settings.builder(), SortField.Type.INT);
565549
}
566550

567551
public void testIndexSortBackwardCompatible() {
568-
Settings settings = Settings.builder()
569-
.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), "0ms") // disable
570-
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.V_2_6_1)
571-
.putList("index.sort.field", "sortfield")
572-
.build();
573-
try {
574-
// Integer index sort should be converted to long sort type
575-
IndexService index = createIndex("test", settings, createTestMapping("integer"));
576-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.LONG);
577-
578-
// Long index sort should be remained to long sort type
579-
index = createIndex("test", settings, createTestMapping("long"));
580-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.LONG);
581-
582-
// Float index sort should be remained to float sort type
583-
index = createIndex("test", settings, createTestMapping("float"));
584-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.FLOAT);
585-
586-
// Double index sort should be remained to double sort type
587-
index = createIndex("test", settings, createTestMapping("double"));
588-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.DOUBLE);
552+
runSortFieldTest(
553+
Settings.builder().put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.V_2_6_1),
554+
SortField.Type.LONG
555+
);
556+
}
589557

590-
// String index sort should be remained to string sort type
591-
index = createIndex("test", settings, createTestMapping("string"));
592-
assertTrue(index.getIndexSortSupplier().get().getSort()[0].getType() == SortField.Type.STRING);
593-
} catch (IllegalArgumentException ex) {
594-
assertEquals("failed to parse value [0ms] for setting [index.translog.sync_interval], must be >= [100ms]", ex.getMessage());
595-
}
558+
private void runSortFieldTest(Settings.Builder settingsBuilder, SortField.Type expectedIntType) {
559+
String[] sortFieldNames = new String[] { "int-sortfield", "long-sortfield", "double-sortfield", "float-sortfield" };
560+
Settings settings = settingsBuilder.putList("index.sort.field", sortFieldNames).build();
561+
IndexService index = createIndexWithSimpleMappings(
562+
"sort-field-test-index",
563+
settings,
564+
"int-sortfield",
565+
"type=integer",
566+
"long-sortfield",
567+
"type=long",
568+
"double-sortfield",
569+
"type=double",
570+
"float-sortfield",
571+
"type=float"
572+
);
573+
SortField[] sortFields = index.getIndexSortSupplier().get().getSort();
574+
Map<String, SortField.Type> map = Arrays.stream(sortFields)
575+
.filter(s -> s instanceof SortedNumericSortField)
576+
.map(s -> (SortedNumericSortField) s)
577+
.collect(Collectors.toMap(SortField::getField, SortedNumericSortField::getNumericType));
578+
assertThat(map.keySet(), containsInAnyOrder(sortFieldNames));
579+
assertThat(map.get("int-sortfield"), equalTo(expectedIntType));
580+
assertThat(map.get("long-sortfield"), equalTo(SortField.Type.LONG));
581+
assertThat(map.get("float-sortfield"), equalTo(SortField.Type.FLOAT));
582+
assertThat(map.get("double-sortfield"), equalTo(SortField.Type.DOUBLE));
596583
}
597584

598585
public void testReplicationTask() throws Exception {
@@ -843,15 +830,4 @@ public void testRefreshTaskUpdatesWithDynamicShardLevelRefreshes() throws Except
843830
client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().putNull("*")).get();
844831

845832
}
846-
847-
private static String createTestMapping(String type) {
848-
return " \"properties\": {\n"
849-
+ " \"test\": {\n"
850-
+ " \"type\": \"text\"\n"
851-
+ " },\n"
852-
+ " \"sortfield\": {\n"
853-
+ " \"type\": \" + type + \"\n"
854-
+ " }\n"
855-
+ " }";
856-
}
857833
}

test/framework/src/main/java/org/opensearch/test/OpenSearchSingleNodeTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,17 @@ protected IndexService createIndex(String index, Settings settings, String type,
368368
return createIndex(index, createIndexRequestBuilder);
369369
}
370370

371+
/**
372+
* Creates an index with mappings provided in the format expected by {@link org.opensearch.action.admin.indices.create.CreateIndexRequest#simpleMapping(String...)}
373+
*/
374+
protected IndexService createIndexWithSimpleMappings(String index, Settings settings, String... mappings) {
375+
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
376+
if (mappings != null) {
377+
createIndexRequestBuilder.setMapping(mappings);
378+
}
379+
return createIndex(index, createIndexRequestBuilder);
380+
}
381+
371382
protected IndexService createIndex(String index, CreateIndexRequestBuilder createIndexRequestBuilder) {
372383
assertAcked(createIndexRequestBuilder.get());
373384
// Wait for the index to be allocated so that cluster state updates don't override

0 commit comments

Comments
 (0)