/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.cloud;

import com.carrotsearch.randomizedtesting.ThreadFilter;
import com.carrotsearch.randomizedtesting.annotations.Nightly;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.apache.lucene.util.TimeUnits;
import org.apache.solr.cloud.hdfs.HdfsTestUtil;
import org.apache.solr.common.cloud.ZkConfigManager;
import org.apache.solr.util.BadHdfsThreadsFilter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 */
@ThreadLeakFilters(defaultFilters = true, filters = {
    BadHdfsThreadsFilter.class, // hdfs currently leaks thread(s)
    MoveReplicaHDFSTest.ForkJoinThreadsFilter.class
})
@Nightly // test is too long for non nightly
@TimeoutSuite(millis = TimeUnits.HOUR)
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-13060")
public class MoveReplicaHDFSTest extends MoveReplicaTest {

  private static MiniDFSCluster dfsCluster;

  @BeforeClass
  public static void setupClass() throws Exception {
    System.setProperty("solr.hdfs.blockcache.enabled", "false");
    dfsCluster = HdfsTestUtil.setupClass(createTempDir().toFile().getAbsolutePath());

    ZkConfigManager configManager = new ZkConfigManager(zkClient());
    configManager.uploadConfigDir(configset("cloud-hdfs"), "conf1");

    System.setProperty("solr.hdfs.home", HdfsTestUtil.getDataDir(dfsCluster, "data"));
  }

  @AfterClass
  public static void teardownClass() throws Exception {
    cluster.shutdown(); // need to close before the MiniDFSCluster
    HdfsTestUtil.teardownClass(dfsCluster);
    dfsCluster = null;
  }

  @Test
  // 12-Jun-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") //2018-03-10
  public void testNormalMove() throws Exception {
    inPlaceMove = false;
    test();
  }

  @Test
  //2018-06-18 (commented) @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 21-May-2018
  //commented 9-Aug-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Jul-2018
  //commented 23-AUG-2018  @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 17-Aug-2018
  // commented 4-Sep-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 23-Aug-2018
  //commented 20-Sep-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 6-Sep-2018
  //Commented 14-Oct-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
  @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 14-Oct-2018
  public void testNormalFailedMove() throws Exception {
    inPlaceMove = false;
    testFailedMove();
  }

  @Test
  @AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/SOLR-12080") // added 03-Oct-2018
  public void testFailedMove() throws Exception {
    super.testFailedMove();
  }

  public static class ForkJoinThreadsFilter implements ThreadFilter {

    @Override
    public boolean reject(Thread t) {
      String name = t.getName();
      if (name.startsWith("ForkJoinPool.commonPool")) {
        return true;
      }
      return false;
    }
  }

}

@@ -827,16 +827,45 @@
 cloud;%0A%0A
+import java.io.IOException;%0A%0A
 import c
@@ -908,24 +908,24 @@
 readFilter;%0A
-
 import com.c
@@ -1158,24 +1158,63 @@
 DFSCluster;%0A
+import org.apache.lucene.util.IOUtils;%0A
 import org.a
@@ -2508,115 +2508,566 @@
     
-cluster.shutdown(); // need to close before the MiniDFSCluster%0A    HdfsTestUtil.teardownClass(dfsCluster);%0A
+try %7B%0A      IOUtils.close(%0A          () -%3E %7B%0A            try %7B%0A              if (cluster != null) cluster.shutdown();%0A            %7D catch (Exception e) %7B%0A              throw new IOException(%22Could not shut down the cluster.%22, e);%0A            %7D%0A          %7D,%0A          () -%3E %7B%0A            try %7B%0A              if (dfsCluster != null) HdfsTestUtil.teardownClass(dfsCluster);%0A            %7D catch (Exception e) %7B%0A              throw new IOException(%22Could not shut down dfs cluster.%22, e);%0A            %7D%0A          %7D%0A      );%0A    %7D finally %7B%0A      cluster = null;%0A  
     
@@ -3085,16 +3085,22 @@
 = null;%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -4435,17 +4435,16 @@
 ilter %7B%0A
-%0A
     @Ove
@@ -4526,20 +4526,23 @@
 ;%0A      
-if (
+return 
 name.sta
@@ -4579,59 +4579,8 @@
 ol%22)
-) %7B%0A        return true;%0A      %7D%0A      return false
 ;%0A  
@@ -4591,7 +4591,7 @@
   %7D%0A
-%0A
 %7D%0A
+%0A

clean up static variables properly, avoiding NPEs.