Skip to content

Commit

Permalink
Fix flaky test IndexShardTests.testLocalDirectoryContains (opensearch…
Browse files Browse the repository at this point in the history
…-project#10929)

This test is breaking for WindowsFS only. Moving it to a separate file where it is skipped on WindowsFS.

Signed-off-by: Marc Handalian <[email protected]>
  • Loading branch information
mch2 authored Oct 25, 2023
1 parent 44a9f18 commit fb6fe1b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
package org.opensearch.index.shard;

import org.apache.logging.log4j.Logger;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexCommit;
Expand All @@ -46,7 +45,6 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.tests.mockfile.ExtrasFS;
import org.apache.lucene.tests.store.BaseDirectoryWrapper;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -93,7 +91,6 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.indices.breaker.NoneCircuitBreakerService;
import org.opensearch.core.util.FileSystemUtils;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -166,13 +163,11 @@
import org.junit.Assert;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -4912,53 +4907,6 @@ public void testRecordsForceMerges() throws IOException {
closeShards(shard);
}

public void testLocalDirectoryContains() throws IOException {
IndexShard indexShard = newStartedShard(true);
int numDocs = between(1, 10);
for (int i = 0; i < numDocs; i++) {
indexDoc(indexShard, "_doc", Integer.toString(i));
}
flushShard(indexShard);
indexShard.store().incRef();
Directory localDirectory = indexShard.store().directory();
Path shardPath = indexShard.shardPath().getDataPath().resolve(ShardPath.INDEX_FOLDER_NAME);
Path tempDir = createTempDir();
for (String file : localDirectory.listAll()) {
if (file.equals("write.lock") || file.startsWith("extra")) {
continue;
}
boolean corrupted = randomBoolean();
long checksum = 0;
try (IndexInput indexInput = localDirectory.openInput(file, IOContext.DEFAULT)) {
checksum = CodecUtil.retrieveChecksum(indexInput);
}
if (corrupted) {
Files.copy(shardPath.resolve(file), tempDir.resolve(file));
try (FileChannel raf = FileChannel.open(shardPath.resolve(file), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
CorruptionUtils.corruptAt(shardPath.resolve(file), raf, (int) (raf.size() - 8));
}
}
if (corrupted == false) {
assertTrue(indexShard.localDirectoryContains(localDirectory, file, checksum));
} else {
assertFalse(indexShard.localDirectoryContains(localDirectory, file, checksum));
assertFalse(Files.exists(shardPath.resolve(file)));
}
}
try (Stream<Path> files = Files.list(tempDir)) {
files.forEach(p -> {
try {
Files.copy(p, shardPath.resolve(p.getFileName()));
} catch (IOException e) {
// Ignore
}
});
}
FileSystemUtils.deleteSubDirectories(tempDir);
indexShard.store().decRef();
closeShards(indexShard);
}

private void populateSampleRemoteSegmentStats(RemoteSegmentTransferTracker tracker) {
tracker.addUploadBytesStarted(30L);
tracker.addUploadBytesSucceeded(10L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.shard;

import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.opensearch.core.util.FileSystemUtils;
import org.opensearch.test.CorruptionUtils;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.stream.Stream;

@LuceneTestCase.SuppressFileSystems("WindowsFS")
public class RemoteIndexShardCorruptionTests extends IndexShardTestCase {

public void testLocalDirectoryContains() throws IOException {
IndexShard indexShard = newStartedShard(true);
int numDocs = between(1, 10);
for (int i = 0; i < numDocs; i++) {
indexDoc(indexShard, "_doc", Integer.toString(i));
}
flushShard(indexShard);
indexShard.store().incRef();
Directory localDirectory = indexShard.store().directory();
Path shardPath = indexShard.shardPath().getDataPath().resolve(ShardPath.INDEX_FOLDER_NAME);
Path tempDir = createTempDir();
for (String file : localDirectory.listAll()) {
if (file.equals("write.lock") || file.startsWith("extra")) {
continue;
}
boolean corrupted = randomBoolean();
long checksum = 0;
try (IndexInput indexInput = localDirectory.openInput(file, IOContext.DEFAULT)) {
checksum = CodecUtil.retrieveChecksum(indexInput);
}
if (corrupted) {
Files.copy(shardPath.resolve(file), tempDir.resolve(file));
try (FileChannel raf = FileChannel.open(shardPath.resolve(file), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
CorruptionUtils.corruptAt(shardPath.resolve(file), raf, (int) (raf.size() - 8));
}
}
if (corrupted == false) {
assertTrue(indexShard.localDirectoryContains(localDirectory, file, checksum));
} else {
assertFalse(indexShard.localDirectoryContains(localDirectory, file, checksum));
assertFalse(Files.exists(shardPath.resolve(file)));
}
}
try (Stream<Path> files = Files.list(tempDir)) {
files.forEach(p -> {
try {
Files.copy(p, shardPath.resolve(p.getFileName()));
} catch (IOException e) {
// Ignore
}
});
}
FileSystemUtils.deleteSubDirectories(tempDir);
indexShard.store().decRef();
closeShards(indexShard);
}
}

0 comments on commit fb6fe1b

Please sign in to comment.