Search in sources :

Example 1 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testBatchingShardUpdateTask.

public void testBatchingShardUpdateTask() throws Exception {
    final Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    assertAcked(prepareCreate("test-idx", 0, Settings.builder().put("number_of_shards", between(1, 10)).put("number_of_replicas", 0)));
    ensureGreen();
    logger.info("--> indexing some data");
    final int numdocs = randomIntBetween(10, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test-idx", "type1", Integer.toString(i)).setSource("field1", "bar " + i);
    }
    indexRandom(true, builders);
    flushAndRefresh();
    final int numberOfShards = getNumShards("test-idx").numPrimaries;
    logger.info("number of shards: {}", numberOfShards);
    final ClusterService clusterService = internalCluster().clusterService(internalCluster().getMasterName());
    BlockingClusterStateListener snapshotListener = new BlockingClusterStateListener(clusterService, "update_snapshot [", "update snapshot state", Priority.HIGH);
    try {
        clusterService.addListener(snapshotListener);
        logger.info("--> snapshot");
        ListenableActionFuture<CreateSnapshotResponse> snapshotFuture = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute();
        // Await until shard updates are in pending state.
        assertBusyPendingTasks("update snapshot state", numberOfShards);
        snapshotListener.unblock();
        // Check that the snapshot was successful
        CreateSnapshotResponse createSnapshotResponse = snapshotFuture.actionGet();
        assertEquals(SnapshotState.SUCCESS, createSnapshotResponse.getSnapshotInfo().state());
        assertEquals(numberOfShards, createSnapshotResponse.getSnapshotInfo().totalShards());
        assertEquals(numberOfShards, createSnapshotResponse.getSnapshotInfo().successfulShards());
    } finally {
        clusterService.removeListener(snapshotListener);
    }
    // Check that we didn't timeout
    assertFalse(snapshotListener.timedOut());
    // Check that cluster state update task was called only once
    assertEquals(1, snapshotListener.count());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ClusterService(org.elasticsearch.cluster.service.ClusterService) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client)

Example 2 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testDeleteOrphanSnapshot.

public void testDeleteOrphanSnapshot() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    final String repositoryName = "test-repo";
    assertAcked(client.admin().cluster().preparePutRepository(repositoryName).setType("mock").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    logger.info("--> create the index");
    final String idxName = "test-idx";
    createIndex(idxName);
    ensureGreen();
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class, internalCluster().getMasterName());
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    logger.info("--> snapshot");
    final String snapshotName = "test-snap";
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repositoryName, snapshotName).setWaitForCompletion(true).setIndices(idxName).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> emulate an orphan snapshot");
    RepositoriesService repositoriesService = internalCluster().getInstance(RepositoriesService.class, internalCluster().getMasterName());
    final RepositoryData repositoryData = repositoriesService.repository(repositoryName).getRepositoryData();
    final IndexId indexId = repositoryData.resolveIndexId(idxName);
    clusterService.submitStateUpdateTask("orphan snapshot test", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) {
            // Simulate orphan snapshot
            ImmutableOpenMap.Builder<ShardId, ShardSnapshotStatus> shards = ImmutableOpenMap.builder();
            shards.put(new ShardId(idxName, "_na_", 0), new ShardSnapshotStatus("unknown-node", State.ABORTED));
            shards.put(new ShardId(idxName, "_na_", 1), new ShardSnapshotStatus("unknown-node", State.ABORTED));
            shards.put(new ShardId(idxName, "_na_", 2), new ShardSnapshotStatus("unknown-node", State.ABORTED));
            List<Entry> entries = new ArrayList<>();
            entries.add(new Entry(new Snapshot(repositoryName, createSnapshotResponse.getSnapshotInfo().snapshotId()), true, false, State.ABORTED, Collections.singletonList(indexId), System.currentTimeMillis(), repositoryData.getGenId(), shards.build()));
            return ClusterState.builder(currentState).putCustom(SnapshotsInProgress.TYPE, new SnapshotsInProgress(Collections.unmodifiableList(entries))).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            fail();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, final ClusterState newState) {
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    logger.info("--> try deleting the orphan snapshot");
    assertAcked(client.admin().cluster().prepareDeleteSnapshot(repositoryName, snapshotName).get("10s"));
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) ClusterState(org.elasticsearch.cluster.ClusterState) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException) ExecutionException(java.util.concurrent.ExecutionException) RepositoryException(org.elasticsearch.repositories.RepositoryException) RepositoryData(org.elasticsearch.repositories.RepositoryData) ShardId(org.elasticsearch.index.shard.ShardId) Entry(org.elasticsearch.cluster.SnapshotsInProgress.Entry) ClusterService(org.elasticsearch.cluster.service.ClusterService) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ArrayList(java.util.ArrayList) List(java.util.List) Client(org.elasticsearch.client.Client)

Example 3 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class DedicatedClusterSnapshotRestoreIT method testMasterShutdownDuringSnapshot.

public void testMasterShutdownDuringSnapshot() throws Exception {
    Settings masterSettings = Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build();
    Settings dataSettings = Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).build();
    logger.info("-->  starting two master nodes and two data nodes");
    internalCluster().startNode(masterSettings);
    internalCluster().startNode(masterSettings);
    internalCluster().startNode(dataSettings);
    internalCluster().startNode(dataSettings);
    final Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    assertAcked(prepareCreate("test-idx", 0, Settings.builder().put("number_of_shards", between(1, 20)).put("number_of_replicas", 0)));
    ensureGreen();
    logger.info("--> indexing some data");
    final int numdocs = randomIntBetween(10, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test-idx", "type1", Integer.toString(i)).setSource("field1", "bar " + i);
    }
    indexRandom(true, builders);
    flushAndRefresh();
    final int numberOfShards = getNumShards("test-idx").numPrimaries;
    logger.info("number of shards: {}", numberOfShards);
    final ClusterService clusterService = internalCluster().clusterService(internalCluster().getMasterName());
    BlockingClusterStateListener snapshotListener = new BlockingClusterStateListener(clusterService, "update_snapshot [", "update snapshot state", Priority.HIGH);
    try {
        clusterService.addListener(snapshotListener);
        logger.info("--> snapshot");
        dataNodeClient().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(false).setIndices("test-idx").get();
        // Await until some updates are in pending state.
        assertBusyPendingTasks("update snapshot state", 1);
        logger.info("--> stopping master node");
        internalCluster().stopCurrentMasterNode();
        logger.info("--> unblocking snapshot execution");
        snapshotListener.unblock();
    } finally {
        clusterService.removeListener(snapshotListener);
    }
    logger.info("--> wait until the snapshot is done");
    assertBusy(new Runnable() {

        @Override
        public void run() {
            GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get();
            SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0);
            assertTrue(snapshotInfo.state().completed());
        }
    }, 1, TimeUnit.MINUTES);
    logger.info("--> verify that snapshot was succesful");
    GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get();
    SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0);
    assertEquals(SnapshotState.SUCCESS, snapshotInfo.state());
    assertEquals(snapshotInfo.totalShards(), snapshotInfo.successfulShards());
    assertEquals(0, snapshotInfo.failedShards());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) ClusterService(org.elasticsearch.cluster.service.ClusterService) NodeClient(org.elasticsearch.client.node.NodeClient) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings)

Example 4 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class SlowClusterStateProcessing method interruptClusterStateProcessing.

private boolean interruptClusterStateProcessing(final TimeValue duration) throws InterruptedException {
    final String disruptionNodeCopy = disruptedNode;
    if (disruptionNodeCopy == null) {
        return false;
    }
    logger.info("delaying cluster state updates on node [{}] for [{}]", disruptionNodeCopy, duration);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy);
    if (clusterService == null) {
        return false;
    }
    final AtomicBoolean stopped = new AtomicBoolean(false);
    clusterService.submitStateUpdateTask("service_disruption_delay", new LocalClusterUpdateTask(Priority.IMMEDIATE) {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) throws Exception {
            long count = duration.millis() / 200;
            // wait while checking for a stopped
            for (; count > 0 && !stopped.get(); count--) {
                Thread.sleep(200);
            }
            if (!stopped.get()) {
                Thread.sleep(duration.millis() % 200);
            }
            countDownLatch.countDown();
            return unchanged();
        }

        @Override
        public void onFailure(String source, Exception e) {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        stopped.set(true);
        // try to wait again, we really want the cluster state thread to be freed up when stopping disruption
        countDownLatch.await();
    }
    return true;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class BlockClusterStateProcessing method startDisrupting.

@Override
public void startDisrupting() {
    final String disruptionNodeCopy = disruptedNode;
    if (disruptionNodeCopy == null) {
        return;
    }
    ClusterService clusterService = cluster.getInstance(ClusterService.class, disruptionNodeCopy);
    if (clusterService == null) {
        return;
    }
    logger.info("delaying cluster state updates on node [{}]", disruptionNodeCopy);
    boolean success = disruptionLatch.compareAndSet(null, new CountDownLatch(1));
    assert success : "startDisrupting called without waiting on stopDisrupting to complete";
    final CountDownLatch started = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("service_disruption_block", new LocalClusterUpdateTask(Priority.IMMEDIATE) {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) throws Exception {
            started.countDown();
            CountDownLatch latch = disruptionLatch.get();
            if (latch != null) {
                latch.await();
            }
            return unchanged();
        }

        @Override
        public void onFailure(String source, Exception e) {
            logger.error("unexpected error during disruption", e);
        }
    });
    try {
        started.await();
    } catch (InterruptedException e) {
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ClusterService (org.elasticsearch.cluster.service.ClusterService)52 ClusterState (org.elasticsearch.cluster.ClusterState)31 Settings (org.elasticsearch.common.settings.Settings)25 ThreadPool (org.elasticsearch.threadpool.ThreadPool)20 TransportService (org.elasticsearch.transport.TransportService)20 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)17 CountDownLatch (java.util.concurrent.CountDownLatch)15 IOException (java.io.IOException)13 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 ActionFilters (org.elasticsearch.action.support.ActionFilters)12 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 ClusterServiceUtils.createClusterService (org.elasticsearch.test.ClusterServiceUtils.createClusterService)12 Before (org.junit.Before)12 ShardId (org.elasticsearch.index.shard.ShardId)11 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 TimeUnit (java.util.concurrent.TimeUnit)10 ExecutionException (java.util.concurrent.ExecutionException)9 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 ESTestCase (org.elasticsearch.test.ESTestCase)9