Example usage for org.apache.lucene.util IOUtils rm

List of usage examples for org.apache.lucene.util IOUtils rm

Introduction

In this page you can find the example usage for org.apache.lucene.util IOUtils rm.

Prototype

public static void rm(Path... locations) throws IOException 

Source Link

Document

Deletes one or more files or directories (and everything underneath it).

Usage

From source file:io.crate.blob.v2.BlobIndex.java

License:Apache License

/**
 * Deletes the directory for the given path.
 *//* w  w  w  . ja  v a  2s  . co m*/
private void deleteIndex(Path blobRoot, String index) {
    if (Files.exists(blobRoot)) {
        logger.debug("[{}] Deleting blob index directory '{}'", index, blobRoot);
        try {
            IOUtils.rm(blobRoot);
        } catch (IOException e) {
            logger.warn("Could not delete blob index directory {}", blobRoot);
        }
    } else {
        logger.warn("Wanted to delete blob index directory {} but it was already gone", blobRoot);
    }
}

From source file:io.crate.blob.v2.BlobIndices.java

License:Apache License

private void deleteBlobIndexLocation(IndexMetaData current, String index) {
    File indexLocation = null;//from  w w  w.j  a  v  a 2 s. c  om
    File customBlobsPath = null;
    if (current.getSettings().get(BlobIndices.SETTING_INDEX_BLOBS_PATH) != null) {
        customBlobsPath = new File(current.getSettings().get(BlobIndices.SETTING_INDEX_BLOBS_PATH));
        indexLocation = blobEnvironment.indexLocation(new Index(index), customBlobsPath);
    } else if (blobEnvironment.blobsPath() != null) {
        indexLocation = blobEnvironment.indexLocation(new Index(index));
    }

    if (indexLocation == null) {
        // default shard location - ES logic deletes everything in this case
        return;
    }

    String absolutePath = indexLocation.getAbsolutePath();
    if (indexLocation.exists()) {
        logger.debug("[{}] Deleting blob index directory '{}'", index, absolutePath);
        try {
            IOUtils.rm(indexLocation.toPath());
        } catch (IOException e) {
            logger.warn("Could not delete blob index directory {}", absolutePath);
        }
    } else {
        logger.warn("wanted to delete blob index directory {} but it was already gone", absolutePath);
    }

    // check if custom index blobs path is empty, if so delete whole path
    if (customBlobsPath != null && blobEnvironment.isCustomBlobPathEmpty(customBlobsPath)) {
        logger.debug("[{}] Empty per table defined blobs path found, deleting leftover folders inside {}",
                index, customBlobsPath.getAbsolutePath());
        try {
            FileSystemUtils.deleteSubDirectories(customBlobsPath.toPath());
        } catch (IOException e) {
            logger.warn("Could not delete custom blob path {}", customBlobsPath.getAbsolutePath());
        }
    }
}

From source file:io.crate.blob.v2.BlobIndicesService.java

License:Apache License

private void deleteBlobIndexLocation(IndexMetaData current, String index) {
    File indexLocation = null;/*from w ww .  java  2  s .c  om*/
    File customBlobsPath = null;
    if (current.getSettings().get(BlobIndicesService.SETTING_INDEX_BLOBS_PATH) != null) {
        customBlobsPath = new File(current.getSettings().get(BlobIndicesService.SETTING_INDEX_BLOBS_PATH));
        indexLocation = blobEnvironment.indexLocation(new Index(index), customBlobsPath);
    } else if (blobEnvironment.blobsPath() != null) {
        indexLocation = blobEnvironment.indexLocation(new Index(index));
    }

    if (indexLocation == null) {
        // default shard location - ES logic deletes everything in this case
        return;
    }

    String absolutePath = indexLocation.getAbsolutePath();
    if (indexLocation.exists()) {
        logger.debug("[{}] Deleting blob index directory '{}'", index, absolutePath);
        try {
            IOUtils.rm(indexLocation.toPath());
        } catch (IOException e) {
            logger.warn("Could not delete blob index directory {}", absolutePath);
        }
    } else {
        logger.warn("wanted to delete blob index directory {} but it was already gone", absolutePath);
    }

    // check if custom index blobs path is empty, if so delete whole path
    if (customBlobsPath != null && blobEnvironment.isCustomBlobPathEmpty(customBlobsPath)) {
        logger.debug("[{}] Empty per table defined blobs path found, deleting leftover folders inside {}",
                index, customBlobsPath.getAbsolutePath());
        try {
            FileSystemUtils.deleteSubDirectories(customBlobsPath.toPath());
        } catch (IOException e) {
            logger.warn("Could not delete custom blob path {}", customBlobsPath.getAbsolutePath());
        }
    }
}

From source file:io.crate.blob.v2.BlobShard.java

License:Apache License

void deleteShard() {
    Path baseDirectory = blobContainer.getBaseDirectory();
    try {/*from   www.j  a  v a  2s  . c  o m*/
        IOUtils.rm(baseDirectory);
    } catch (IOException e) {
        logger.warn("Could not delete blob directory: {} {}", baseDirectory, e);
    }
}

From source file:org.codelibs.elasticsearch.common.io.FileSystemUtils.java

License:Apache License

/**
 * Deletes all subdirectories in the given path recursively
 * @throws java.lang.IllegalArgumentException if the given path is not a directory
 *///from  ww w  .  j ava 2 s.  co m
public static void deleteSubDirectories(Path... paths) throws IOException {
    for (Path path : paths) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
            for (Path subPath : stream) {
                if (Files.isDirectory(subPath)) {
                    IOUtils.rm(subPath);
                }
            }
        }
    }
}

From source file:org.elasticsearch.cassandra.gateway.CassandraGateway.java

License:Apache License

public void reset() throws Exception {
    try {/*from   www  .j  a  v  a  2s. co  m*/
        Path[] dataPaths = nodeEnv.nodeDataPaths();
        logger.trace("removing node data paths: [{}]", dataPaths);
        IOUtils.rm(dataPaths);
    } catch (Exception ex) {
        logger.debug("failed to delete shard locations", ex);
    }
}

From source file:org.elasticsearch.cluster.allocation.ClusterRerouteIT.java

License:Apache License

private void rerouteWithAllocateLocalGateway(Settings commonSettings) throws Exception {
    logger.info("--> starting 2 nodes");
    String node_1 = internalCluster().startNode(commonSettings);
    internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(2));
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("2")
            .execute().actionGet();//  w  w  w.  j av a 2  s.  co  m
    assertThat(healthResponse.isTimedOut(), equalTo(false));

    logger.info("--> create an index with 1 shard, 1 replica, nothing should allocate");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE)
            .setSettings(Settings.builder().put("index.number_of_shards", 1)).execute().actionGet();

    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(2));

    logger.info("--> explicitly allocate shard 1, actually allocating, no dry run");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean())
            .add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).execute().actionGet()
            .getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(
            state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(),
            equalTo(ShardRoutingState.INITIALIZING));

    healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID)
            .setWaitForYellowStatus().execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));

    logger.info("--> get the state, verify shard 1 primary allocated");
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(
            state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(),
            equalTo(ShardRoutingState.STARTED));

    client().prepareIndex("test", "type", "1").setSource("field", "value")
            .setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    final Index index = resolveIndex("test");

    logger.info("--> closing all nodes");
    Path[] shardLocation = internalCluster().getInstance(NodeEnvironment.class, node_1)
            .availableShardPaths(new ShardId(index, 0));
    assertThat(FileSystemUtils.exists(shardLocation), equalTo(true)); // make sure the data is there!
    internalCluster().closeNonSharedNodes(false); // don't wipe data directories the index needs to be there!

    logger.info("--> deleting the shard data [{}] ", Arrays.toString(shardLocation));
    assertThat(FileSystemUtils.exists(shardLocation), equalTo(true)); // verify again after cluster was shut down
    IOUtils.rm(shardLocation);

    logger.info(
            "--> starting nodes back, will not allocate the shard since it has no data, but the index will be there");
    node_1 = internalCluster().startNode(commonSettings);
    internalCluster().startNode(commonSettings);
    // wait a bit for the cluster to realize that the shard is not there...
    // TODO can we get around this? the cluster is RED, so what do we wait for?
    client().admin().cluster().prepareReroute().get();
    assertThat(
            client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet().getStatus(),
            equalTo(ClusterHealthStatus.RED));
    logger.info("--> explicitly allocate primary");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean())
            .add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).execute().actionGet()
            .getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(
            state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(),
            equalTo(ShardRoutingState.INITIALIZING));

    logger.info("--> get the state, verify shard 1 primary allocated");
    final String nodeToCheck = node_1;
    assertBusy(() -> {
        ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
        String nodeId = clusterState.nodes().resolveNode(nodeToCheck).getId();
        assertThat(clusterState.getRoutingNodes().node(nodeId).iterator().next().state(),
                equalTo(ShardRoutingState.STARTED));
    });
}

From source file:org.elasticsearch.common.blobstore.fs.FsBlobStore.java

License:Apache License

@Override
public void delete(BlobPath path) throws IOException {
    IOUtils.rm(buildPath(path));
}

From source file:org.elasticsearch.common.util.MultiDataPathUpgrader.java

License:Apache License

/**
 * Upgrades the given shard Id from multiple shard paths into the given target path.
 *
 * @see #pickShardPath(org.elasticsearch.index.shard.ShardId)
 *//*from   w  ww.  j a va2s  . c  o  m*/
public void upgrade(ShardId shard, ShardPath targetPath) throws IOException {
    final Path[] paths = nodeEnvironment.availableShardPaths(shard); // custom data path doesn't need upgrading
    if (isTargetPathConfigured(paths, targetPath) == false) {
        throw new IllegalArgumentException("shard path must be one of the shards data paths");
    }
    assert needsUpgrading(shard) : "Should not upgrade a path that needs no upgrading";
    logger.info("{} upgrading multi data dir to {}", shard, targetPath.getDataPath());
    final ShardStateMetaData loaded = ShardStateMetaData.FORMAT.loadLatestState(logger, paths);
    if (loaded == null) {
        throw new IllegalStateException(shard + " no shard state found in any of: " + Arrays.toString(paths)
                + " please check and remove them if possible");
    }
    logger.info("{} loaded shard state {}", shard, loaded);

    ShardStateMetaData.FORMAT.write(loaded, loaded.version, targetPath.getShardStatePath());
    Files.createDirectories(targetPath.resolveIndex());
    try (SimpleFSDirectory directory = new SimpleFSDirectory(targetPath.resolveIndex())) {
        try (final Lock lock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
            upgradeFiles(shard, targetPath, targetPath.resolveIndex(), ShardPath.INDEX_FOLDER_NAME, paths);
        } catch (LockObtainFailedException ex) {
            throw new IllegalStateException("Can't obtain lock on " + targetPath.resolveIndex(), ex);
        }

    }

    upgradeFiles(shard, targetPath, targetPath.resolveTranslog(), ShardPath.TRANSLOG_FOLDER_NAME, paths);

    logger.info("{} wipe upgraded directories", shard);
    for (Path path : paths) {
        if (path.equals(targetPath.getShardStatePath()) == false) {
            logger.info("{} wipe shard directories: [{}]", shard, path);
            IOUtils.rm(path);
        }
    }

    if (FileSystemUtils.files(targetPath.resolveIndex()).length == 0) {
        throw new IllegalStateException("index folder [" + targetPath.resolveIndex() + "] is empty");
    }

    if (FileSystemUtils.files(targetPath.resolveTranslog()).length == 0) {
        throw new IllegalStateException("translog folder [" + targetPath.resolveTranslog() + "] is empty");
    }
}

From source file:org.elasticsearch.gateway.MetaDataStateFormat.java

License:Apache License

/**
 * Deletes all meta state directories recursively for the given data locations
 * @param dataLocations the data location to delete
 */// w  w w  .j  a va2 s  .  c o  m
public static void deleteMetaState(Path... dataLocations) throws IOException {
    Path[] stateDirectories = new Path[dataLocations.length];
    for (int i = 0; i < dataLocations.length; i++) {
        stateDirectories[i] = dataLocations[i].resolve(STATE_DIR_NAME);
    }
    IOUtils.rm(stateDirectories);
}