public HdfsDirectory(Path hdfsDirPath, Configuration configuration) throws IOException {
    setLockFactory(NoLockFactory.getNoLockFactory());
    this.hdfsDirPath = hdfsDirPath;
    this.configuration = configuration;
    fileSystem = FileSystem.newInstance(hdfsDirPath.toUri(), configuration);

    while (true) {
      try {
        if (!fileSystem.exists(hdfsDirPath)) {
          boolean success = fileSystem.mkdirs(hdfsDirPath);
          if (!success) {
            throw new RuntimeException("Could not create directory: " + hdfsDirPath);
          }
        } else {
          fileSystem.mkdirs(hdfsDirPath); // check for safe mode
        }

        break;
      } catch (RemoteException e) {
        if (e.getClassName().equals("org.apache.hadoop.hdfs.server.namenode.SafeModeException")) {
          LOG.warn("The NameNode is in SafeMode - Solr will wait 5 seconds and try again.");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e1) {
            Thread.interrupted();
          }
          continue;
        }
        org.apache.solr.util.IOUtils.closeQuietly(fileSystem);
        throw new RuntimeException("Problem creating directory: " + hdfsDirPath, e);
      } catch (Exception e) {
        org.apache.solr.util.IOUtils.closeQuietly(fileSystem);
        throw new RuntimeException("Problem creating directory: " + hdfsDirPath, e);
      }
    }
  }
 @Override
 public boolean exists(String path) {
   Path hdfsDirPath = new Path(path);
   Configuration conf = getConf();
   FileSystem fileSystem = null;
   try {
     fileSystem = FileSystem.get(hdfsDirPath.toUri(), conf);
     return fileSystem.exists(hdfsDirPath);
   } catch (IOException e) {
     LOG.error("Error checking if hdfs path exists", e);
     throw new RuntimeException("Error checking if hdfs path exists", e);
   } finally {
     IOUtils.closeQuietly(fileSystem);
   }
 }
Esempio n. 3
0
 public HdfsDirectory(Path hdfsDirPath, Configuration configuration) throws IOException {
   assert hdfsDirPath.toString().startsWith("hdfs:/") : hdfsDirPath.toString();
   setLockFactory(NoLockFactory.getNoLockFactory());
   this.hdfsDirPath = hdfsDirPath;
   this.configuration = configuration;
   fileSystem = FileSystem.newInstance(hdfsDirPath.toUri(), configuration);
   try {
     if (!fileSystem.exists(hdfsDirPath)) {
       fileSystem.mkdirs(hdfsDirPath);
     }
   } catch (Exception e) {
     org.apache.solr.util.IOUtils.closeQuietly(fileSystem);
     throw new RuntimeException("Problem creating directory: " + hdfsDirPath, e);
   }
 }
 protected synchronized void removeDirectory(CacheValue cacheValue) throws IOException {
   Configuration conf = getConf();
   FileSystem fileSystem = null;
   try {
     fileSystem = FileSystem.get(new URI(cacheValue.path), conf);
     boolean success = fileSystem.delete(new Path(cacheValue.path), true);
     if (!success) {
       throw new RuntimeException("Could not remove directory");
     }
   } catch (Exception e) {
     LOG.error("Could not remove directory", e);
     throw new SolrException(ErrorCode.SERVER_ERROR, "Could not remove directory", e);
   } finally {
     IOUtils.closeQuietly(fileSystem);
   }
 }