forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix flaky test IndexShardTests.testLocalDirectoryContains (opensearch…
…-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
Showing
2 changed files
with
75 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
server/src/test/java/org/opensearch/index/shard/RemoteIndexShardCorruptionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |