Search in sources :

Example 1 with SyncedFlushResponse

use of org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse in project elasticsearch by elastic.

the class FlushIT method testSyncedFlush.

public void testSyncedFlush() throws ExecutionException, InterruptedException, IOException {
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("test").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).get();
    ensureGreen();
    final Index index = client().admin().cluster().prepareState().get().getState().metaData().index("test").getIndex();
    IndexStats indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    ShardsSyncedFlushResult result;
    if (randomBoolean()) {
        logger.info("--> sync flushing shard 0");
        result = SyncedFlushUtil.attemptSyncedFlush(internalCluster(), new ShardId(index, 0));
    } else {
        logger.info("--> sync flushing index [test]");
        SyncedFlushResponse indicesResult = client().admin().indices().prepareSyncedFlush("test").get();
        result = indicesResult.getShardsResultPerIndex().get("test").get(0);
    }
    assertFalse(result.failed());
    assertThat(result.totalShards(), equalTo(indexStats.getShards().length));
    assertThat(result.successfulShards(), equalTo(indexStats.getShards().length));
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    String syncId = result.syncId();
    for (ShardStats shardStats : indexStats.getShards()) {
        final String shardSyncId = shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID);
        assertThat(shardSyncId, equalTo(syncId));
    }
    // now, start new node and relocate a shard there and see if sync id still there
    String newNodeName = internalCluster().startNode();
    ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
    ShardRouting shardRouting = clusterState.getRoutingTable().index("test").shard(0).iterator().next();
    String currentNodeName = clusterState.nodes().resolveNode(shardRouting.currentNodeId()).getName();
    assertFalse(currentNodeName.equals(newNodeName));
    internalCluster().client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, currentNodeName, newNodeName)).get();
    client().admin().cluster().prepareHealth().setWaitForNoRelocatingShards(true).get();
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build()).get();
    ensureGreen("test");
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, internalCluster().numDataNodes() - 1).build()).get();
    ensureGreen("test");
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Index(org.elasticsearch.index.Index) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 2 with SyncedFlushResponse

use of org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse in project elasticsearch by elastic.

the class RestSyncedFlushAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, IndicesOptions.lenientExpandOpen());
    SyncedFlushRequest syncedFlushRequest = new SyncedFlushRequest(Strings.splitStringByCommaToArray(request.param("index")));
    syncedFlushRequest.indicesOptions(indicesOptions);
    return channel -> client.admin().indices().syncedFlush(syncedFlushRequest, new RestBuilderListener<SyncedFlushResponse>(channel) {

        @Override
        public RestResponse buildResponse(SyncedFlushResponse results, XContentBuilder builder) throws Exception {
            builder.startObject();
            results.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(results.restStatus(), builder);
        }
    });
}
Also used : SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 3 with SyncedFlushResponse

use of org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse in project elasticsearch by elastic.

the class SyncedFlushService method attemptSyncedFlush.

/**
     * a utility method to perform a synced flush for all shards of multiple indices. see {@link #attemptSyncedFlush(ShardId, ActionListener)}
     * for more details.
     */
public void attemptSyncedFlush(final String[] aliasesOrIndices, IndicesOptions indicesOptions, final ActionListener<SyncedFlushResponse> listener) {
    final ClusterState state = clusterService.state();
    final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, indicesOptions, aliasesOrIndices);
    final Map<String, List<ShardsSyncedFlushResult>> results = ConcurrentCollections.newConcurrentMap();
    int numberOfShards = 0;
    for (Index index : concreteIndices) {
        final IndexMetaData indexMetaData = state.metaData().getIndexSafe(index);
        numberOfShards += indexMetaData.getNumberOfShards();
        results.put(index.getName(), Collections.synchronizedList(new ArrayList<>()));
    }
    if (numberOfShards == 0) {
        listener.onResponse(new SyncedFlushResponse(results));
        return;
    }
    final CountDown countDown = new CountDown(numberOfShards);
    for (final Index concreteIndex : concreteIndices) {
        final String index = concreteIndex.getName();
        final IndexMetaData indexMetaData = state.metaData().getIndexSafe(concreteIndex);
        final int indexNumberOfShards = indexMetaData.getNumberOfShards();
        for (int shard = 0; shard < indexNumberOfShards; shard++) {
            final ShardId shardId = new ShardId(indexMetaData.getIndex(), shard);
            innerAttemptSyncedFlush(shardId, state, new ActionListener<ShardsSyncedFlushResult>() {

                @Override
                public void onResponse(ShardsSyncedFlushResult syncedFlushResult) {
                    results.get(index).add(syncedFlushResult);
                    if (countDown.countDown()) {
                        listener.onResponse(new SyncedFlushResponse(results));
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    logger.debug("{} unexpected error while executing synced flush", shardId);
                    final int totalShards = indexMetaData.getNumberOfReplicas() + 1;
                    results.get(index).add(new ShardsSyncedFlushResult(shardId, totalShards, e.getMessage()));
                    if (countDown.countDown()) {
                        listener.onResponse(new SyncedFlushResponse(results));
                    }
                }
            });
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) CountDown(org.elasticsearch.common.util.concurrent.CountDown) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SyncedFlushResponse

use of org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse in project elasticsearch by elastic.

the class FlushIT method testSyncedFlushWithConcurrentIndexing.

public void testSyncedFlushWithConcurrentIndexing() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(3);
    createIndex("test");
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.PB)).put("index.refresh_interval", -1).put("index.number_of_replicas", internalCluster().numDataNodes() - 1)).get();
    ensureGreen();
    final AtomicBoolean stop = new AtomicBoolean(false);
    final AtomicInteger numDocs = new AtomicInteger(0);
    Thread indexingThread = new Thread() {

        @Override
        public void run() {
            while (stop.get() == false) {
                client().prepareIndex().setIndex("test").setType("doc").setSource("{}", XContentType.JSON).get();
                numDocs.incrementAndGet();
            }
        }
    };
    indexingThread.start();
    IndexStats indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    logger.info("--> trying sync flush");
    SyncedFlushResponse syncedFlushResult = client().admin().indices().prepareSyncedFlush("test").get();
    logger.info("--> sync flush done");
    stop.set(true);
    indexingThread.join();
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    assertFlushResponseEqualsShardStats(indexStats.getShards(), syncedFlushResult.getShardsResultPerIndex().get("test"));
    refresh();
    assertThat(client().prepareSearch().setSize(0).get().getHits().getTotalHits(), equalTo((long) numDocs.get()));
    logger.info("indexed {} docs", client().prepareSearch().setSize(0).get().getHits().getTotalHits());
    logClusterState();
    internalCluster().fullRestart();
    ensureGreen();
    assertThat(client().prepareSearch().setSize(0).get().getHits().getTotalHits(), equalTo((long) numDocs.get()));
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats)

Aggregations

SyncedFlushResponse (org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse)4 IOException (java.io.IOException)2 IndexStats (org.elasticsearch.action.admin.indices.stats.IndexStats)2 ShardStats (org.elasticsearch.action.admin.indices.stats.ShardStats)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 Index (org.elasticsearch.index.Index)2 ShardId (org.elasticsearch.index.shard.ShardId)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 SyncedFlushRequest (org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest)1 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1 MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)1 Strings (org.elasticsearch.common.Strings)1 Settings (org.elasticsearch.common.settings.Settings)1