Skip to content

Commit f0561f7

Browse files
committed
Add test for direct IO not specified
1 parent 2852a90 commit f0561f7

File tree

2 files changed

+62
-15
lines changed
  • server/src/internalClusterTest/java/org/elasticsearch/index/store
  • test/framework/src/main/java/org/elasticsearch/test

2 files changed

+62
-15
lines changed

server/src/internalClusterTest/java/org/elasticsearch/index/store/DirectIOIT.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
7171
return List.of(InternalSettingsPlugin.class);
7272
}
7373

74-
private void indexVectors() {
74+
private String indexVectors(boolean directIO) {
75+
String indexName = "test-vectors-" + directIO;
7576
String type = randomFrom("bbq_hnsw", "bbq_disk");
7677
assertAcked(
77-
prepareCreate("foo-vectors").setSettings(Settings.builder().put(InternalSettingsPlugin.USE_COMPOUND_FILE.getKey(), false))
78+
prepareCreate(indexName).setSettings(Settings.builder().put(InternalSettingsPlugin.USE_COMPOUND_FILE.getKey(), false))
7879
.setMapping("""
7980
{
8081
"properties": {
@@ -85,27 +86,28 @@ private void indexVectors() {
8586
"index": true,
8687
"similarity": "l2_norm",
8788
"index_options": {
88-
"type": "%type%",
89-
"on_disk_rescore": true
89+
"type": "%s",
90+
"on_disk_rescore": %s
9091
}
9192
}
9293
}
9394
}
94-
""".replace("%type%", type))
95+
""".formatted(type, directIO))
9596
);
96-
ensureGreen("foo-vectors");
97+
ensureGreen(indexName);
9798

9899
for (int i = 0; i < 1000; i++) {
99-
indexDoc("foo-vectors", Integer.toString(i), "fooVector", IntStream.range(0, 64).mapToDouble(d -> randomFloat()).toArray());
100+
indexDoc(indexName, Integer.toString(i), "fooVector", IntStream.range(0, 64).mapToDouble(d -> randomFloat()).toArray());
100101
}
101102
refresh();
102-
assertBBQIndexType(type); // test assertion to ensure that the correct index type is being used
103+
assertBBQIndexType(indexName, type); // test assertion to ensure that the correct index type is being used
104+
return indexName;
103105
}
104106

105107
@SuppressWarnings("unchecked")
106-
static void assertBBQIndexType(String type) {
107-
var response = indicesAdmin().prepareGetFieldMappings("foo-vectors").setFields("fooVector").get();
108-
var map = (Map<String, Object>) response.fieldMappings("foo-vectors", "fooVector").sourceAsMap().get("fooVector");
108+
static void assertBBQIndexType(String indexName, String type) {
109+
var response = indicesAdmin().prepareGetFieldMappings(indexName).setFields("fooVector").get();
110+
var map = (Map<String, Object>) response.fieldMappings(indexName, "fooVector").sourceAsMap().get("fooVector");
109111
assertThat((String) ((Map<String, Object>) map.get("index_options")).get("type"), is(equalTo(type)));
110112
}
111113

@@ -128,11 +130,39 @@ public void testDirectIOUsed() {
128130
);
129131
mockLog.addExpectation(expectation);
130132

131-
indexVectors();
133+
String indexName = indexVectors(true);
132134

133135
// do a search
134136
var knn = List.of(new KnnSearchBuilder("fooVector", new VectorData(null, new byte[64]), 10, 20, 10f, null, null));
135-
assertHitCount(prepareSearch("foo-vectors").setKnnSearch(knn), 10);
137+
assertHitCount(prepareSearch(indexName).setKnnSearch(knn), 10);
138+
mockLog.assertAllExpectationsMatched();
139+
}
140+
}
141+
142+
@TestLogging(value = "org.elasticsearch.index.store.FsDirectoryFactory:DEBUG", reason = "to capture trace logging for direct IO")
143+
public void testDirectIONotUsed() {
144+
try (MockLog mockLog = MockLog.capture(FsDirectoryFactory.class)) {
145+
// nothing about direct IO should be logged at all
146+
MockLog.LoggingExpectation expectation = SUPPORTED
147+
? new MockLog.PatternNotSeenEventExpectation(
148+
"Direct IO used",
149+
FsDirectoryFactory.class.getCanonicalName(),
150+
Level.DEBUG,
151+
"Opening .*\\.vec with direct IO"
152+
)
153+
: new MockLog.PatternNotSeenEventExpectation(
154+
"Direct IO not used",
155+
FsDirectoryFactory.class.getCanonicalName(),
156+
Level.DEBUG,
157+
"Could not open .*\\.vec with direct IO"
158+
);
159+
mockLog.addExpectation(expectation);
160+
161+
String indexName = indexVectors(false);
162+
163+
// do a search
164+
var knn = List.of(new KnnSearchBuilder("fooVector", new VectorData(null, new byte[64]), 10, 20, 10f, null, null));
165+
assertHitCount(prepareSearch(indexName).setKnnSearch(knn), 10);
136166
mockLog.assertAllExpectationsMatched();
137167
}
138168
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ public boolean innerMatch(final LogEvent event) {
283283

284284
public static class PatternSeenEventExpectation implements LoggingExpectation {
285285

286-
private final String name;
286+
final String name;
287287
private final String logger;
288288
private final Level level;
289289
private final Pattern pattern;
290-
private final CountDownLatch seenLatch = new CountDownLatch(1);
290+
final CountDownLatch seenLatch = new CountDownLatch(1);
291291

292292
public PatternSeenEventExpectation(String name, String logger, Level level, String pattern) {
293293
this.name = name;
@@ -316,6 +316,23 @@ public void awaitMatched(long millis) throws InterruptedException {
316316
}
317317
}
318318

319+
public static class PatternNotSeenEventExpectation extends PatternSeenEventExpectation {
320+
321+
public PatternNotSeenEventExpectation(String name, String logger, Level level, String pattern) {
322+
super(name, logger, level, pattern);
323+
}
324+
325+
@Override
326+
public void assertMatched() {
327+
assertThat("expected not to see " + name + " but did", seenLatch.getCount(), equalTo(1L));
328+
}
329+
330+
@Override
331+
public void awaitMatched(long millis) throws InterruptedException {
332+
assertThat(name, seenLatch.await(millis, TimeUnit.MILLISECONDS), equalTo(false));
333+
}
334+
}
335+
319336
public static class PatternAndExceptionSeenEventExpectation extends SeenEventExpectation {
320337

321338
private final Pattern pattern;

0 commit comments

Comments
 (0)