@Override
 protected void shutdownMiniMrClusters() {
   deleteConfFiles();
   if (m_mr != null) {
     m_mr.stop();
     m_mr = null;
   }
 }
  @Override
  public void setupMiniDfsAndMrClusters() {
    try {
      deleteConfFiles();
      CONF_DIR.mkdirs();

      // Build mini DFS cluster
      Configuration hdfsConf = new Configuration();
      m_dfs = new MiniDFSCluster.Builder(hdfsConf).numDataNodes(2).format(true).racks(null).build();
      m_fileSys = m_dfs.getFileSystem();
      m_dfs_conf = m_dfs.getConfiguration(0);
      // Create user home directory
      m_fileSys.mkdirs(m_fileSys.getWorkingDirectory());

      // Write core-site.xml
      Configuration core_site = new Configuration(false);
      core_site.set(FileSystem.FS_DEFAULT_NAME_KEY, m_dfs_conf.get(FileSystem.FS_DEFAULT_NAME_KEY));
      core_site.writeXml(new FileOutputStream(CORE_CONF_FILE));

      Configuration hdfs_site = new Configuration(false);
      for (Entry<String, String> conf : m_dfs_conf) {
        if (ArrayUtils.contains(m_dfs_conf.getPropertySources(conf.getKey()), "programatically")) {
          hdfs_site.set(conf.getKey(), m_dfs_conf.getRaw(conf.getKey()));
        }
      }
      hdfs_site.writeXml(new FileOutputStream(HDFS_CONF_FILE));

      // Build mini YARN cluster
      m_mr = new MiniMRYarnCluster("PigMiniCluster", 2);
      m_mr.init(m_dfs_conf);
      m_mr.start();
      m_mr_conf = m_mr.getConfig();
      m_mr_conf.set(
          YarnConfiguration.YARN_APPLICATION_CLASSPATH, System.getProperty("java.class.path"));
      m_mr_conf.set(MRJobConfig.MAP_JAVA_OPTS, "-Xmx512m");
      m_mr_conf.set(MRJobConfig.REDUCE_JAVA_OPTS, "-Xmx512m");

      Configuration mapred_site = new Configuration(false);
      Configuration yarn_site = new Configuration(false);
      for (Entry<String, String> conf : m_mr_conf) {
        if (ArrayUtils.contains(m_mr_conf.getPropertySources(conf.getKey()), "programatically")) {
          if (conf.getKey().contains("yarn")) {
            yarn_site.set(conf.getKey(), m_mr_conf.getRaw(conf.getKey()));
          } else if (!conf.getKey().startsWith("dfs")) {
            mapred_site.set(conf.getKey(), m_mr_conf.getRaw(conf.getKey()));
          }
        }
      }

      mapred_site.writeXml(new FileOutputStream(MAPRED_CONF_FILE));
      yarn_site.writeXml(new FileOutputStream(YARN_CONF_FILE));

      // Write tez-site.xml
      Configuration tez_conf = new Configuration(false);
      // TODO PIG-3659 - Remove this once memory management is fixed
      tez_conf.set(TezRuntimeConfiguration.TEZ_RUNTIME_IO_SORT_MB, "20");
      tez_conf.set("tez.lib.uris", "hdfs:///tez,hdfs:///tez/lib");
      // Set to a lower value so that tests don't get stuck for long because of 1 AM running at a
      // time
      tez_conf.set(TezConfiguration.TEZ_SESSION_AM_DAG_SUBMIT_TIMEOUT_SECS, "20");
      // Lower the max task attempts to 2 so that negative tests fail
      // faster. By default, tasks retry 4 times
      tez_conf.set(TezConfiguration.TEZ_AM_TASK_MAX_FAILED_ATTEMPTS, "2");
      tez_conf.writeXml(new FileOutputStream(TEZ_CONF_FILE));

      // Copy tez jars to hdfs
      m_fileSys.mkdirs(new Path("/tez/lib"));
      FileFilter fileFilter = new RegexFileFilter("tez-.+\\.jar$");
      File[] tezJars = TEZ_LIB_DIR.listFiles(fileFilter);
      for (int i = 0; i < tezJars.length; i++) {
        if (tezJars[i].getName().startsWith("tez-api")) {
          m_fileSys.copyFromLocalFile(
              new Path(tezJars[i].getAbsoluteFile().toString()), new Path("/tez"));
        } else {
          m_fileSys.copyFromLocalFile(
              new Path(tezJars[i].getAbsoluteFile().toString()), new Path("/tez/lib"));
        }
      }

      m_conf = m_mr_conf;
      // Turn FetchOptimizer off so that we can actually test Tez
      m_conf.set(PigConfiguration.PIG_OPT_FETCH, System.getProperty("test.opt.fetch", "false"));

      System.setProperty("junit.hadoop.conf", CONF_DIR.getPath());
      System.setProperty("hadoop.log.dir", "build/test/logs");
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }