Search in sources :

Example 1 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project elasticsearch by elastic.

the class ClusterService method patchVersionsAndNoMasterBlocks.

private ClusterState patchVersionsAndNoMasterBlocks(ClusterState previousClusterState, ClusterTasksResult<Object> executionResult) {
    ClusterState newClusterState = executionResult.resultingState;
    if (executionResult.noMaster) {
        assert newClusterState == previousClusterState : "state can only be changed by ClusterService when noMaster = true";
        if (previousClusterState.nodes().getMasterNodeId() != null) {
            // remove block if it already exists before adding new one
            assert previousClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "NO_MASTER_BLOCK should only be added by ClusterService";
            ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(previousClusterState.blocks()).addGlobalBlock(discoverySettings.getNoMasterBlock()).build();
            DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder(previousClusterState.nodes()).masterNodeId(null).build();
            newClusterState = ClusterState.builder(previousClusterState).blocks(clusterBlocks).nodes(discoveryNodes).build();
        }
    } else if (newClusterState.nodes().isLocalNodeElectedMaster() && previousClusterState != newClusterState) {
        // only the master controls the version numbers
        Builder builder = ClusterState.builder(newClusterState).incrementVersion();
        if (previousClusterState.routingTable() != newClusterState.routingTable()) {
            builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build());
        }
        if (previousClusterState.metaData() != newClusterState.metaData()) {
            builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1));
        }
        // remove the no master block, if it exists
        if (newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id())) {
            builder.blocks(ClusterBlocks.builder().blocks(newClusterState.blocks()).removeGlobalBlock(discoverySettings.getNoMasterBlock().id()));
        }
        newClusterState = builder.build();
    }
    assert newClusterState.nodes().getMasterNodeId() == null || newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "cluster state with master node must not have NO_MASTER_BLOCK";
    return newClusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Builder(org.elasticsearch.cluster.ClusterState.Builder) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 2 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project elasticsearch by elastic.

the class MainActionTests method testMainActionClusterAvailable.

public void testMainActionClusterAvailable() {
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterName clusterName = new ClusterName("elasticsearch");
    final Settings settings = Settings.builder().put("node.name", "my-node").build();
    final boolean available = randomBoolean();
    ClusterBlocks blocks;
    if (available) {
        if (randomBoolean()) {
            blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        } else {
            blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 400", randomBoolean(), randomBoolean(), RestStatus.BAD_REQUEST, ClusterBlockLevel.ALL)).build();
        }
    } else {
        blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 503", randomBoolean(), randomBoolean(), RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL)).build();
    }
    ClusterState state = ClusterState.builder(clusterName).blocks(blocks).build();
    when(clusterService.state()).thenReturn(state);
    TransportService transportService = new TransportService(Settings.EMPTY, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null);
    TransportMainAction action = new TransportMainAction(settings, mock(ThreadPool.class), transportService, mock(ActionFilters.class), mock(IndexNameExpressionResolver.class), clusterService);
    AtomicReference<MainResponse> responseRef = new AtomicReference<>();
    action.doExecute(new MainRequest(), new ActionListener<MainResponse>() {

        @Override
        public void onResponse(MainResponse mainResponse) {
            responseRef.set(mainResponse);
        }

        @Override
        public void onFailure(Exception e) {
            logger.error("unexpected error", e);
        }
    });
    assertNotNull(responseRef.get());
    assertEquals(available, responseRef.get().isAvailable());
    verify(clusterService, times(1)).state();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ThreadPool(org.elasticsearch.threadpool.ThreadPool) AtomicReference(java.util.concurrent.atomic.AtomicReference) ActionFilters(org.elasticsearch.action.support.ActionFilters) IOException(java.io.IOException) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterService(org.elasticsearch.cluster.service.ClusterService) TransportService(org.elasticsearch.transport.TransportService) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project elasticsearch by elastic.

the class MetaDataDeleteIndexService method deleteIndices.

/**
     * Delete some indices from the cluster state.
     */
public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) {
    final MetaData meta = currentState.metaData();
    final Set<IndexMetaData> metaDatas = indices.stream().map(i -> meta.getIndexSafe(i)).collect(toSet());
    // Check if index deletion conflicts with any running snapshots
    SnapshotsService.checkIndexDeletion(currentState, metaDatas);
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
    MetaData.Builder metaDataBuilder = MetaData.builder(meta);
    ClusterBlocks.Builder clusterBlocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    final IndexGraveyard.Builder graveyardBuilder = IndexGraveyard.builder(metaDataBuilder.indexGraveyard());
    final int previousGraveyardSize = graveyardBuilder.tombstones().size();
    for (final Index index : indices) {
        String indexName = index.getName();
        logger.info("{} deleting index", index);
        routingTableBuilder.remove(indexName);
        clusterBlocksBuilder.removeIndexBlocks(indexName);
        metaDataBuilder.remove(indexName);
    }
    // add tombstones to the cluster state for each deleted index
    final IndexGraveyard currentGraveyard = graveyardBuilder.addTombstones(indices).build(settings);
    // the new graveyard set on the metadata
    metaDataBuilder.indexGraveyard(currentGraveyard);
    logger.trace("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.", graveyardBuilder.getNumPurged(), previousGraveyardSize, currentGraveyard.getTombstones().size());
    MetaData newMetaData = metaDataBuilder.build();
    ClusterBlocks blocks = clusterBlocksBuilder.build();
    // update snapshot restore entries
    ImmutableOpenMap<String, ClusterState.Custom> customs = currentState.getCustoms();
    final RestoreInProgress restoreInProgress = currentState.custom(RestoreInProgress.TYPE);
    if (restoreInProgress != null) {
        RestoreInProgress updatedRestoreInProgress = RestoreService.updateRestoreStateWithDeletedIndices(restoreInProgress, indices);
        if (updatedRestoreInProgress != restoreInProgress) {
            ImmutableOpenMap.Builder<String, ClusterState.Custom> builder = ImmutableOpenMap.builder(customs);
            builder.put(RestoreInProgress.TYPE, updatedRestoreInProgress);
            customs = builder.build();
        }
    }
    return allocationService.reroute(ClusterState.builder(currentState).routingTable(routingTableBuilder.build()).metaData(newMetaData).blocks(blocks).customs(customs).build(), "deleted indices [" + indices + "]");
}
Also used : Arrays(java.util.Arrays) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) Priority(org.elasticsearch.common.Priority) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) ClusterService(org.elasticsearch.cluster.service.ClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) Set(java.util.Set) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Index(org.elasticsearch.index.Index) RestoreService(org.elasticsearch.snapshots.RestoreService) Sets(org.elasticsearch.common.util.set.Sets) Inject(org.elasticsearch.common.inject.Inject) DeleteIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) ActionListener(org.elasticsearch.action.ActionListener) Collectors.toSet(java.util.stream.Collectors.toSet) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Index(org.elasticsearch.index.Index) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable)

Example 4 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testRecreateBlocksOnRestore.

public void testRecreateBlocksOnRestore() throws Exception {
    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)));
    Settings.Builder indexSettings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s");
    logger.info("--> create index");
    assertAcked(prepareCreate("test-idx", 2, indexSettings));
    try {
        List<String> initialBlockSettings = randomSubsetOf(randomInt(3), IndexMetaData.SETTING_BLOCKS_WRITE, IndexMetaData.SETTING_BLOCKS_METADATA, IndexMetaData.SETTING_READ_ONLY);
        Settings.Builder initialSettingsBuilder = Settings.builder();
        for (String blockSetting : initialBlockSettings) {
            initialSettingsBuilder.put(blockSetting, true);
        }
        Settings initialSettings = initialSettingsBuilder.build();
        logger.info("--> using initial block settings {}", initialSettings.getAsMap());
        if (!initialSettings.isEmpty()) {
            logger.info("--> apply initial blocks to index");
            client().admin().indices().prepareUpdateSettings("test-idx").setSettings(initialSettingsBuilder).get();
        }
        logger.info("--> snapshot index");
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        logger.info("--> remove blocks and delete index");
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_READ);
        cluster().wipeIndices("test-idx");
        logger.info("--> restore index with additional block changes");
        List<String> changeBlockSettings = randomSubsetOf(randomInt(4), IndexMetaData.SETTING_BLOCKS_METADATA, IndexMetaData.SETTING_BLOCKS_WRITE, IndexMetaData.SETTING_READ_ONLY, IndexMetaData.SETTING_BLOCKS_READ);
        Settings.Builder changedSettingsBuilder = Settings.builder();
        for (String blockSetting : changeBlockSettings) {
            changedSettingsBuilder.put(blockSetting, randomBoolean());
        }
        Settings changedSettings = changedSettingsBuilder.build();
        logger.info("--> applying changed block settings {}", changedSettings.getAsMap());
        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndexSettings(changedSettings).setWaitForCompletion(true).execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        ClusterBlocks blocks = client.admin().cluster().prepareState().clear().setBlocks(true).get().getState().blocks();
        // compute current index settings (as we cannot query them if they contain SETTING_BLOCKS_METADATA)
        Settings mergedSettings = Settings.builder().put(initialSettings).put(changedSettings).build();
        logger.info("--> merged block settings {}", mergedSettings.getAsMap());
        logger.info("--> checking consistency between settings and blocks");
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_METADATA_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_READ_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_WRITE_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetaData.SETTING_READ_ONLY, false), is(blocks.hasIndexBlock("test-idx", IndexMetaData.INDEX_READ_ONLY_BLOCK)));
    } finally {
        logger.info("--> cleaning up blocks");
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetaData.SETTING_BLOCKS_READ);
    }
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 5 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project fabric8 by jboss-fuse.

the class FabricDiscovery method updateCluster.

private void updateCluster() {
    try {
        singleton.update(new ESNode(clusterName.value(), localNode, singleton.isMaster()));
    } catch (Exception e) {
    // Ignore if not joined
    }
    if (singleton.isMaster()) {
        if (logger.isDebugEnabled()) {
            String master = singleton.master() != null ? singleton.master().node.name() : null;
            List<String> slaves = new ArrayList<>();
            for (ESNode s : singleton.slaves()) {
                slaves.add(s.node.name());
            }
            logger.debug("Updating cluster: master {}, slaves {}", master, slaves);
        }
        clusterService.submitStateUpdateTask("fabric-discovery-master", Priority.URGENT, new ProcessedClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                // Rebuild state
                ClusterState.Builder stateBuilder = ClusterState.builder(currentState);
                // Rebuild nodes
                DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder().localNodeId(localNode.id()).masterNodeId(singleton.master().getNode().id()).put(singleton.master().getNode());
                for (ESNode node : singleton.slaves()) {
                    nodesBuilder.put(node.getNode());
                }
                latestDiscoNodes = nodesBuilder.build();
                stateBuilder.nodes(latestDiscoNodes);
                for (DiscoveryNode node : latestDiscoNodes) {
                    if (!currentState.nodes().nodeExists(node.id())) {
                        transportService.connectToNode(node);
                    }
                }
                // update the fact that we are the master...
                if (!localNode().id().equals(currentState.nodes().masterNodeId())) {
                    ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(currentState.blocks()).removeGlobalBlock(NO_MASTER_BLOCK).build();
                    stateBuilder.blocks(clusterBlocks);
                }
                return stateBuilder.build();
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                logger.debug("Cluster updated");
                sendInitialStateEventIfNeeded();
            }

            @Override
            public void onFailure(String source, Throwable t) {
                logger.error("unexpected failure during [{}]", t, source);
            }
        });
    } else if (singleton.master() != null) {
        DiscoveryNode masterNode = singleton.master().getNode();
        try {
            // first, make sure we can connect to the master
            transportService.connectToNode(masterNode);
        } catch (Exception e) {
            logger.warn("failed to connect to master [{}], retrying...", e, masterNode);
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ProcessedClusterStateUpdateTask(org.elasticsearch.cluster.ProcessedClusterStateUpdateTask) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchIllegalStateException(org.elasticsearch.ElasticsearchIllegalStateException) IOException(java.io.IOException)

Aggregations

ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)11 ClusterState (org.elasticsearch.cluster.ClusterState)5 Settings (org.elasticsearch.common.settings.Settings)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)3 Index (org.elasticsearch.index.Index)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Set (java.util.Set)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 ActionListener (org.elasticsearch.action.ActionListener)2 DeleteIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest)2 AckedClusterStateUpdateTask (org.elasticsearch.cluster.AckedClusterStateUpdateTask)2 RestoreInProgress (org.elasticsearch.cluster.RestoreInProgress)2 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)2 ClusterBlock (org.elasticsearch.cluster.block.ClusterBlock)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)2 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)2 Priority (org.elasticsearch.common.Priority)2 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)2