You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by kr...@apache.org on 2022/10/30 22:19:01 UTC

[solr] 02/02: SOLR-16511: testBackupIncremental fails with ArrayIndexOutOfBoundsException - switch from File to Path

This is an automated email from the ASF dual-hosted git repository.

krisden pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 089d2796f04d7cf1aae20446748957a7783c90d4
Author: Kevin Risden <kr...@apache.org>
AuthorDate: Sun Oct 30 15:52:49 2022 -0400

    SOLR-16511: testBackupIncremental fails with ArrayIndexOutOfBoundsException - switch from File to Path
    
    This makes this test reproducible instead of randomly
    choosing a file from the collection, uses random()
    to explicitly pick an index from sorted collection.
---
 .../collections/AbstractIncrementalBackupTest.java | 52 +++++++++++-----------
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/api/collections/AbstractIncrementalBackupTest.java b/solr/test-framework/src/java/org/apache/solr/cloud/api/collections/AbstractIncrementalBackupTest.java
index 9af8553dd5c..14bfedee455 100644
--- a/solr/test-framework/src/java/org/apache/solr/cloud/api/collections/AbstractIncrementalBackupTest.java
+++ b/solr/test-framework/src/java/org/apache/solr/cloud/api/collections/AbstractIncrementalBackupTest.java
@@ -19,13 +19,12 @@ package org.apache.solr.cloud.api.collections;
 
 import static org.apache.solr.core.TrackingBackupRepository.copiedFiles;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.invoke.MethodHandles;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -33,7 +32,6 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.Random;
 import java.util.Set;
@@ -350,31 +348,33 @@ public abstract class AbstractIncrementalBackupTest extends SolrCloudTestCase {
   }
 
   protected void corruptIndexFiles() throws IOException {
-    Collection<Slice> slices = getCollectionState(getCollectionName()).getSlices();
-    Slice slice = slices.iterator().next();
-    JettySolrRunner leaderNode = cluster.getReplicaJetty(slice.getLeader());
-
-    SolrCore solrCore = leaderNode.getCoreContainer().getCore(slice.getLeader().getCoreName());
-    Set<String> fileNames =
-        new HashSet<>(solrCore.getDeletionPolicy().getLatestCommit().getFileNames());
-    File indexFolder = new File(solrCore.getIndexDir());
-    File fileGetCorrupted =
-        Stream.of(Objects.requireNonNull(indexFolder.listFiles()))
-            .filter(x -> fileNames.contains(x.getName()))
-            .findAny()
-            .get();
-    try (FileInputStream fis = new FileInputStream(fileGetCorrupted)) {
-      byte[] contents = fis.readAllBytes();
-      for (int i = 1; i < 5; i++) {
-        int key = contents.length - CodecUtil.footerLength() - i;
-        contents[key] = (byte) (contents[key] + 1);
+    List<Slice> slices = new ArrayList<>(getCollectionState(getCollectionName()).getSlices());
+    Replica leader = slices.get(random().nextInt(slices.size()) - 1).getLeader();
+    JettySolrRunner leaderNode = cluster.getReplicaJetty(leader);
+
+    final Path fileToCorrupt;
+    try (SolrCore solrCore = leaderNode.getCoreContainer().getCore(leader.getCoreName())) {
+      Set<String> fileNames =
+          new HashSet<>(solrCore.getDeletionPolicy().getLatestCommit().getFileNames());
+      final List<Path> indexFiles;
+      try (Stream<Path> indexFolderFiles = Files.list(Path.of(solrCore.getIndexDir()))) {
+        indexFiles =
+            indexFolderFiles
+                .filter(x -> fileNames.contains(x.getFileName().toString()))
+                .sorted()
+                .collect(Collectors.toList());
       }
-      try (FileOutputStream fos = new FileOutputStream(fileGetCorrupted)) {
-        fos.write(contents);
+      if (indexFiles.isEmpty()) {
+        return;
       }
-    } finally {
-      solrCore.close();
+      fileToCorrupt = indexFiles.get(random().nextInt(indexFiles.size()) - 1);
     }
+    final byte[] contents = Files.readAllBytes(fileToCorrupt);
+    for (int i = 1; i < 5; i++) {
+      int key = contents.length - CodecUtil.footerLength() - i;
+      contents[key] = (byte) (contents[key] + 1);
+    }
+    Files.write(fileToCorrupt, contents);
   }
 
   private void addDummyFileToIndex(BackupRepository repository, URI indexDir, String fileName)