org.elasticsearch.cluster.block.ClusterBlocks Java Examples

The following examples show how to use org.elasticsearch.cluster.block.ClusterBlocks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterServiceUtils.java    From crate with Apache License 2.0 6 votes vote down vote up
public static ClusterService createClusterService(ThreadPool threadPool, DiscoveryNode localNode, ClusterSettings clusterSettings) {
    Settings settings = Settings.builder()
            .put("node.name", "test")
            .put("cluster.name", "ClusterServiceTests")
            .build();
    ClusterService clusterService = new ClusterService(settings, clusterSettings, threadPool);
    clusterService.setNodeConnectionsService(createNoOpNodeConnectionsService());
    ClusterState initialClusterState = ClusterState.builder(new ClusterName(ClusterServiceUtils.class.getSimpleName()))
        .nodes(DiscoveryNodes.builder()
            .add(localNode)
            .localNodeId(localNode.getId())
            .masterNodeId(localNode.getId()))
        .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
    clusterService.getClusterApplierService().setInitialState(initialClusterState);
    clusterService.getMasterService().setClusterStatePublisher(
        createClusterStatePublisher(clusterService.getClusterApplierService()));
    clusterService.getMasterService().setClusterStateSupplier(clusterService.getClusterApplierService()::state);
    clusterService.start();
    return clusterService;
}
 
Example #2
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static ClusterState createIndexBlockedState(String indexName, Settings hackedSettings, String alias) {
    ClusterState blockedClusterState = null;
    IndexMetadata.Builder builder = IndexMetadata.builder(indexName);
    if (alias != null) {
        builder.putAlias(AliasMetadata.builder(alias));
    }
    IndexMetadata indexMetaData = builder
        .settings(
            Settings
                .builder()
                .put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
                .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
                .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
                .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
                .put(hackedSettings)
        )
        .build();
    Metadata metaData = Metadata.builder().put(indexMetaData, false).build();
    blockedClusterState = ClusterState
        .builder(new ClusterName("test cluster"))
        .metadata(metaData)
        .blocks(ClusterBlocks.builder().addBlocks(indexMetaData))
        .build();
    return blockedClusterState;
}
 
Example #3
Source File: MasterServiceTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private TimedMasterService createTimedMasterService(boolean makeMaster) throws InterruptedException {
    DiscoveryNode localNode = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(),
        emptySet(), Version.CURRENT);
    TimedMasterService timedMasterService = new TimedMasterService(Settings.builder().put("cluster.name",
        MasterServiceTests.class.getSimpleName()).build(), threadPool);
    ClusterState initialClusterState = ClusterState.builder(new ClusterName(MasterServiceTests.class.getSimpleName()))
        .nodes(DiscoveryNodes.builder()
            .add(localNode)
            .localNodeId(localNode.getId())
            .masterNodeId(makeMaster ? localNode.getId() : null))
        .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
    AtomicReference<ClusterState> clusterStateRef = new AtomicReference<>(initialClusterState);
    timedMasterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
        clusterStateRef.set(event.state());
        publishListener.onResponse(null);
    });
    timedMasterService.setClusterStateSupplier(clusterStateRef::get);
    timedMasterService.start();
    return timedMasterService;
}
 
Example #4
Source File: NodeJoinTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private static ClusterState initialState(DiscoveryNode localNode, long term, long version,
                                         VotingConfiguration config) {
    return ClusterState.builder(new ClusterName(ClusterServiceUtils.class.getSimpleName()))
        .nodes(DiscoveryNodes.builder()
            .add(localNode)
            .localNodeId(localNode.getId()))
        .metaData(MetaData.builder()
                .coordinationMetaData(
                    CoordinationMetaData.builder()
                    .term(term)
                    .lastAcceptedConfiguration(config)
                    .lastCommittedConfiguration(config)
                .build()))
        .version(version)
        .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
}
 
Example #5
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 6 votes vote down vote up
private void removeOccurrences(ClusterState state,
                               ClusterBlocks.Builder blocksBuilder,
                               RoutingTable.Builder routingBuilder,
                               MetaData.Builder updatedMetaData,
                               RelationName name) {
    String aliasOrIndexName = name.indexNameOrAlias();
    String templateName = PartitionName.templateName(name.schema(), name.name());
    for (Index index : indexNameResolver.concreteIndices(
        state, IndicesOptions.LENIENT_EXPAND_OPEN, aliasOrIndexName)) {

        String indexName = index.getName();
        routingBuilder.remove(indexName);
        updatedMetaData.remove(indexName);
        blocksBuilder.removeIndexBlocks(indexName);
    }
    updatedMetaData.removeTemplate(templateName);
}
 
Example #6
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public ClusterState readFrom(StreamInput in, DiscoveryNode localNode) throws IOException {
    ClusterName clusterName = ClusterName.readClusterName(in);
    Builder builder = new Builder(clusterName);
    builder.version = in.readLong();
    builder.uuid = in.readString();
    builder.metaData = MetaData.Builder.readFrom(in);
    builder.routingTable = RoutingTable.Builder.readFrom(in);
    builder.nodes = DiscoveryNodes.Builder.readFrom(in, localNode);
    builder.blocks = ClusterBlocks.Builder.readClusterBlocks(in);
    int customSize = in.readVInt();
    for (int i = 0; i < customSize; i++) {
        String type = in.readString();
        Custom customIndexMetaData = lookupPrototypeSafe(type).readFrom(in);
        builder.putCustom(type, customIndexMetaData);
    }
    return builder.build();
}
 
Example #7
Source File: Coordinator.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void doStart() {
    synchronized (mutex) {
        CoordinationState.PersistedState persistedState = persistedStateSupplier.get();
        coordinationState.set(new CoordinationState(settings, getLocalNode(), persistedState));
        peerFinder.setCurrentTerm(getCurrentTerm());
        configuredHostsResolver.start();
        VotingConfiguration votingConfiguration = coordinationState.get().getLastAcceptedState().getLastCommittedConfiguration();
        if (singleNodeDiscovery &&
            votingConfiguration.isEmpty() == false &&
            votingConfiguration.hasQuorum(Collections.singleton(getLocalNode().getId())) == false) {
            throw new IllegalStateException("cannot start with [" + DiscoveryModule.DISCOVERY_TYPE_SETTING.getKey() + "] set to [" +
                DiscoveryModule.SINGLE_NODE_DISCOVERY_TYPE + "] when local node " + getLocalNode() +
                " does not have quorum in voting configuration " + votingConfiguration);
        }
        ClusterState initialState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(settings))
            .blocks(ClusterBlocks.builder()
                .addGlobalBlock(STATE_NOT_RECOVERED_BLOCK)
                .addGlobalBlock(noMasterBlockService.getNoMasterBlock()))
            .nodes(DiscoveryNodes.builder().add(getLocalNode()).localNodeId(getLocalNode().getId()))
            .build();
        applierState = initialState;
        clusterApplier.setInitialState(initialState);
    }
}
 
Example #8
Source File: ClusterState.java    From crate with Apache License 2.0 6 votes vote down vote up
public static ClusterState readFrom(StreamInput in, DiscoveryNode localNode) throws IOException {
    ClusterName clusterName = new ClusterName(in);
    Builder builder = new Builder(clusterName);
    builder.version = in.readLong();
    builder.uuid = in.readString();
    builder.metaData = MetaData.readFrom(in);
    builder.routingTable = RoutingTable.readFrom(in);
    builder.nodes = DiscoveryNodes.readFrom(in, localNode);
    builder.blocks = new ClusterBlocks(in);
    int customSize = in.readVInt();
    for (int i = 0; i < customSize; i++) {
        Custom customIndexMetaData = in.readNamedWriteable(Custom.class);
        builder.putCustom(customIndexMetaData.getWriteableName(), customIndexMetaData);
    }
    return builder.build();
}
 
Example #9
Source File: ClusterStateUpdaters.java    From crate with Apache License 2.0 6 votes vote down vote up
static ClusterState recoverClusterBlocks(final ClusterState state) {
    final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(state.blocks());

    if (MetaData.SETTING_READ_ONLY_SETTING.get(state.metaData().settings())) {
        blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
    }

    if (MetaData.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.get(state.metaData().settings())) {
        blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_ALLOW_DELETE_BLOCK);
    }

    for (final IndexMetaData indexMetaData : state.metaData()) {
        blocks.addBlocks(indexMetaData);
    }

    return ClusterState.builder(state).blocks(blocks).build();
}
 
Example #10
Source File: ClusterState.java    From crate with Apache License 2.0 5 votes vote down vote up
public ClusterState(ClusterName clusterName, long version, String stateUUID, MetaData metaData, RoutingTable routingTable,
                    DiscoveryNodes nodes, ClusterBlocks blocks, ImmutableOpenMap<String, Custom> customs,
                    boolean wasReadFromDiff) {
    this.version = version;
    this.stateUUID = stateUUID;
    this.clusterName = clusterName;
    this.metaData = metaData;
    this.routingTable = routingTable;
    this.nodes = nodes;
    this.blocks = blocks;
    this.customs = customs;
    this.wasReadFromDiff = wasReadFromDiff;
}
 
Example #11
Source File: CrateDummyClusterServiceUnitTest.java    From crate with Apache License 2.0 5 votes vote down vote up
protected ClusterService createClusterService(Collection<Setting<?>> additionalClusterSettings, Version version) {
    Set<Setting<?>> clusterSettingsSet = Sets.newHashSet(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    clusterSettingsSet.addAll(additionalClusterSettings);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, clusterSettingsSet);
    ClusterService clusterService = new ClusterService(
        Settings.builder()
            .put("cluster.name", "ClusterServiceTests")
            .put(Node.NODE_NAME_SETTING.getKey(), NODE_NAME)
            .build(),
        clusterSettings,
        THREAD_POOL
    );
    clusterService.setNodeConnectionsService(createNoOpNodeConnectionsService());
    DiscoveryNode discoveryNode = new DiscoveryNode(
        NODE_NAME,
        NODE_ID,
        buildNewFakeTransportAddress(),
        Collections.emptyMap(),
        new HashSet<>(Arrays.asList(DiscoveryNode.Role.values())),
        version
    );
    DiscoveryNodes nodes = DiscoveryNodes.builder()
        .add(discoveryNode)
        .localNodeId(NODE_ID)
        .masterNodeId(NODE_ID)
        .build();
    ClusterState clusterState = ClusterState.builder(new ClusterName(this.getClass().getSimpleName()))
        .nodes(nodes).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();

    ClusterApplierService clusterApplierService = clusterService.getClusterApplierService();
    clusterApplierService.setInitialState(clusterState);

    MasterService masterService = clusterService.getMasterService();
    masterService.setClusterStatePublisher(createClusterStatePublisher(clusterApplierService));
    masterService.setClusterStateSupplier(clusterApplierService::state);

    clusterService.start();
    return clusterService;
}
 
Example #12
Source File: ClusterServiceUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
public static MasterService createMasterService(ThreadPool threadPool, DiscoveryNode localNode) {
    ClusterState initialClusterState = ClusterState.builder(new ClusterName(ClusterServiceUtils.class.getSimpleName()))
        .nodes(DiscoveryNodes.builder()
            .add(localNode)
            .localNodeId(localNode.getId())
            .masterNodeId(localNode.getId()))
        .blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
    return createMasterService(threadPool, initialClusterState);
}
 
Example #13
Source File: SwapAndDropIndexExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState execute(ClusterState currentState, SwapAndDropIndexRequest request) throws Exception {
    final MetaData metaData = currentState.getMetaData();
    final ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    final MetaData.Builder mdBuilder = MetaData.builder(metaData);
    final RoutingTable.Builder routingBuilder = RoutingTable.builder(currentState.routingTable());

    String sourceIndexName = request.source();
    String targetIndexName = request.target();

    mdBuilder.remove(sourceIndexName);
    mdBuilder.remove(targetIndexName);
    routingBuilder.remove(sourceIndexName);
    routingBuilder.remove(targetIndexName);
    blocksBuilder.removeIndexBlocks(sourceIndexName);
    blocksBuilder.removeIndexBlocks(targetIndexName);

    IndexMetaData sourceIndex = metaData.index(sourceIndexName);
    if (sourceIndex == null) {
        throw new IllegalArgumentException("Source index must exist: " + sourceIndexName);
    }

    IndexMetaData newIndexMetaData = IndexMetaData.builder(sourceIndex).index(targetIndexName).build();
    mdBuilder.put(newIndexMetaData, true);
    routingBuilder.addAsFromCloseToOpen(newIndexMetaData);
    blocksBuilder.addBlocks(newIndexMetaData);

    return allocationService.reroute(
        ClusterState.builder(currentState)
            .metaData(mdBuilder)
            .routingTable(routingBuilder.build())
            .blocks(blocksBuilder)
            .build(),
        "swap and drop index"
    );
}
 
Example #14
Source File: MetaDataUpdateSettingsService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the cluster block only iff the setting exists in the given settings
 */
public static void maybeUpdateClusterBlock(String[] actualIndices, ClusterBlocks.Builder blocks, ClusterBlock block, Setting<Boolean> setting, Settings openSettings) {
    if (setting.exists(openSettings)) {
        final boolean updateBlock = setting.get(openSettings);
        for (String index : actualIndices) {
            if (updateBlock) {
                blocks.addIndexBlock(index, block);
            } else {
                blocks.removeIndexBlock(index, block);
            }
        }
    }
}
 
Example #15
Source File: ClusterState.java    From crate with Apache License 2.0 5 votes vote down vote up
ClusterStateDiff(StreamInput in, DiscoveryNode localNode) throws IOException {
    clusterName = new ClusterName(in);
    fromUuid = in.readString();
    toUuid = in.readString();
    toVersion = in.readLong();
    routingTable = RoutingTable.readDiffFrom(in);
    nodes = DiscoveryNodes.readDiffFrom(in, localNode);
    metaData = MetaData.readDiffFrom(in);
    blocks = ClusterBlocks.readDiffFrom(in);
    customs = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), CUSTOM_VALUE_SERIALIZER);
}
 
Example #16
Source File: ADClusterEventListenerTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testIsWarmNode() {
    HashMap<String, String> attributesForNode1 = new HashMap<>();
    attributesForNode1.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
    dataNode1 = new DiscoveryNode(dataNode1Id, buildNewFakeTransportAddress(), attributesForNode1, BUILT_IN_ROLES, Version.CURRENT);

    ClusterState warmNodeClusterState = ClusterState
        .builder(new ClusterName(clusterName))
        .nodes(new DiscoveryNodes.Builder().masterNodeId(masterNodeId).localNodeId(dataNode1Id).add(masterNode).add(dataNode1))
        .blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK))
        .build();
    listener.clusterChanged(new ClusterChangedEvent("foo", warmNodeClusterState, oldClusterState));
    assertTrue(testAppender.containsMessage(ADClusterEventListener.NODE_NOT_APPLIED_MSG));
}
 
Example #17
Source File: ADClusterEventListenerTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public void testNotRecovered() {
    ClusterState blockedClusterState = ClusterState
        .builder(new ClusterName(clusterName))
        .nodes(new DiscoveryNodes.Builder().masterNodeId(masterNodeId).localNodeId(dataNode1Id).add(masterNode).add(dataNode1))
        .blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK))
        .build();
    listener.clusterChanged(new ClusterChangedEvent("foo", blockedClusterState, oldClusterState));
    assertTrue(testAppender.containsMessage(ADClusterEventListener.NOT_RECOVERED_MSG));
}
 
Example #18
Source File: TribeService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void addNewIndex(ClusterState tribeState, ClusterBlocks.Builder blocks, MetaData.Builder metaData, RoutingTable.Builder routingTable, IndexMetaData tribeIndex) {
    Settings tribeSettings = Settings.builder().put(tribeIndex.getSettings()).put(TRIBE_NAME, tribeName).build();
    metaData.put(IndexMetaData.builder(tribeIndex).settings(tribeSettings));
    routingTable.add(tribeState.routingTable().index(tribeIndex.getIndex()));
    if (Regex.simpleMatch(blockIndicesMetadata, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_METADATA_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesRead, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_READ_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesWrite, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_WRITE_BLOCK);
    }
}
 
Example #19
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ClusterState(ClusterName clusterName, long version, String stateUUID, MetaData metaData, RoutingTable routingTable, DiscoveryNodes nodes, ClusterBlocks blocks, ImmutableOpenMap<String, Custom> customs, boolean wasReadFromDiff) {
    this.version = version;
    this.stateUUID = stateUUID;
    this.clusterName = clusterName;
    this.metaData = metaData;
    this.routingTable = routingTable;
    this.nodes = nodes;
    this.blocks = blocks;
    this.customs = customs;
    this.status = ClusterStateStatus.UNKNOWN;
    this.wasReadFromDiff = wasReadFromDiff;
}
 
Example #20
Source File: InternalClusterService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Inject
public InternalClusterService(Settings settings, DiscoveryService discoveryService, OperationRouting operationRouting, TransportService transportService,
                              NodeSettingsService nodeSettingsService, ThreadPool threadPool, ClusterName clusterName, DiscoveryNodeService discoveryNodeService, Version version) {
    super(settings);
    this.operationRouting = operationRouting;
    this.transportService = transportService;
    this.discoveryService = discoveryService;
    this.threadPool = threadPool;
    this.nodeSettingsService = nodeSettingsService;
    this.discoveryNodeService = discoveryNodeService;
    this.version = version;

    // will be replaced on doStart.
    this.clusterState = ClusterState.builder(clusterName).build();

    this.nodeSettingsService.setClusterService(this);
    this.nodeSettingsService.addListener(new ApplySettings());

    this.reconnectInterval = this.settings.getAsTime(SETTING_CLUSTER_SERVICE_RECONNECT_INTERVAL, TimeValue.timeValueSeconds(10));

    this.slowTaskLoggingThreshold = this.settings.getAsTime(SETTING_CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD, TimeValue.timeValueSeconds(30));

    localNodeMasterListeners = new LocalNodeMasterListeners(threadPool);

    initialBlocks = ClusterBlocks.builder().addGlobalBlock(discoveryService.getNoMasterBlock());

    taskManager = transportService.getTaskManager();

    this.auditService = new AuditService(nodeSettingsService);
}
 
Example #21
Source File: ElasticSearchHealthCheckTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
private FakeClusterHealthResponse(ClusterHealthStatus clusterHealthStatus) {
    super("fake-cluster", new String[0],
        new ClusterState(new ClusterName("fake-cluster"), 0, null, null, RoutingTable.builder().build(),
            DiscoveryNodes.builder().build(),
            ClusterBlocks.builder().build(), null, false));
    this.status = clusterHealthStatus;
}
 
Example #22
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ClusterBlocks getBlocks() {
    return blocks;
}
 
Example #23
Source File: TribeService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void removeIndex(ClusterBlocks.Builder blocks, MetaData.Builder metaData, RoutingTable.Builder routingTable, IndexMetaData index) {
    metaData.remove(index.getIndex());
    routingTable.remove(index.getIndex());
    blocks.removeIndexBlocks(index.getIndex());
}
 
Example #24
Source File: RenameTableClusterStateExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
    RelationName source = request.sourceTableIdent();
    RelationName target = request.targetTableIdent();
    boolean isPartitioned = request.isPartitioned();

    MetaData currentMetaData = currentState.getMetaData();
    MetaData.Builder newMetaData = MetaData.builder(currentMetaData);

    if (isPartitioned) {
        IndexTemplateMetaData indexTemplateMetaData = DDLClusterStateHelpers.templateMetaData(currentMetaData, source);
        if (indexTemplateMetaData == null) {
            throw new IndexTemplateMissingException("Template for partitioned table is missing");
        }
        renameTemplate(newMetaData, indexTemplateMetaData, target);
    }

    RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());

    logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());

    try {
        Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(
            currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());

        for (Index sourceIndex : sourceIndices) {
            IndexMetaData sourceIndexMetaData = currentMetaData.getIndexSafe(sourceIndex);
            String sourceIndexName = sourceIndex.getName();
            newMetaData.remove(sourceIndexName);
            newRoutingTable.remove(sourceIndexName);
            blocksBuilder.removeIndexBlocks(sourceIndexName);

            IndexMetaData targetMd;
            if (isPartitioned) {
                PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
                String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
                targetMd = IndexMetaData.builder(sourceIndexMetaData)
                    .removeAllAliases()
                    .putAlias(AliasMetaData.builder(target.indexNameOrAlias()).build())
                    .index(targetIndexName)
                    .build();
            } else {
                targetMd = IndexMetaData.builder(sourceIndexMetaData)
                    .index(target.indexNameOrAlias())
                    .build();
            }
            newMetaData.put(targetMd, true);
            newRoutingTable.addAsFromCloseToOpen(targetMd);
            blocksBuilder.addBlocks(targetMd);
        }
    } catch (IndexNotFoundException e) {
        if (isPartitioned == false) {
            throw e;
        }
        // empty partition case, no indices, just a template exists
    }

    ClusterState clusterStateAfterRename = ClusterState.builder(currentState)
        .metaData(newMetaData)
        .routingTable(newRoutingTable.build())
        .blocks(blocksBuilder)
        .build();

    return allocationService.reroute(
        ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()),
        "rename-table"
    );
}
 
Example #25
Source File: OpenTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
    Context context = prepare(currentState, request);
    Set<IndexMetaData> indicesToOpen = context.indicesMetaData();
    IndexTemplateMetaData templateMetaData = context.templateMetaData();

    if (indicesToOpen.isEmpty() && templateMetaData == null) {
        return currentState;
    }

    MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder()
        .blocks(currentState.blocks());
    final Version minIndexCompatibilityVersion = currentState.getNodes().getMaxNodeVersion()
        .minimumIndexCompatibilityVersion();
    for (IndexMetaData closedMetaData : indicesToOpen) {
        final String indexName = closedMetaData.getIndex().getName();
        IndexMetaData indexMetaData = IndexMetaData.builder(closedMetaData).state(IndexMetaData.State.OPEN).build();
        // The index might be closed because we couldn't import it due to old incompatible version
        // We need to check that this index can be upgraded to the current version
        indexMetaData = metaDataIndexUpgradeService.upgradeIndexMetaData(indexMetaData, minIndexCompatibilityVersion);
        try {
            indicesService.verifyIndexMetadata(indexMetaData, indexMetaData);
        } catch (Exception e) {
            throw new ElasticsearchException("Failed to verify index " + indexMetaData.getIndex(), e);
        }

        mdBuilder.put(indexMetaData, true);
        blocksBuilder.removeIndexBlock(indexName, INDEX_CLOSED_BLOCK);
    }

    // remove closed flag at possible partitioned table template
    if (templateMetaData != null) {
        mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetaData, true));
    }

    // The MetaData will always be overridden (and not merged!) when applying it on a cluster state builder.
    // So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
    // Otherwise they would operate on the old MetaData and would just ignore any modifications.
    ClusterState updatedState = ClusterState.builder(currentState).metaData(mdBuilder).blocks(blocksBuilder).build();

    // call possible registered modifiers
    if (context.partitionName() != null) {
        updatedState = ddlClusterStateService.onOpenTablePartition(updatedState, context.partitionName());
    } else {
        updatedState = ddlClusterStateService.onOpenTable(updatedState, request.tableIdent());
    }

    RoutingTable.Builder rtBuilder = RoutingTable.builder(updatedState.routingTable());
    for (IndexMetaData index : indicesToOpen) {
        rtBuilder.addAsFromCloseToOpen(updatedState.metaData().getIndexSafe(index.getIndex()));
    }

    //no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
    return allocationService.reroute(
        ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(),
        "indices opened " + indicesToOpen);
}
 
Example #26
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ClusterBlocks blocks() {
    return this.blocks;
}
 
Example #27
Source File: CloseTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, OpenCloseTableOrPartitionRequest request) throws Exception {
    Context context = prepare(currentState, request);

    Set<IndexMetaData> indicesToClose = context.indicesMetaData();
    IndexTemplateMetaData templateMetaData = context.templateMetaData();

    if (indicesToClose.isEmpty() && templateMetaData == null) {
        return currentState;
    }

    // Check if index closing conflicts with any running restores
    RestoreService.checkIndexClosing(currentState, indicesToClose);
    // Check if index closing conflicts with any running snapshots
    SnapshotsService.checkIndexClosing(currentState, indicesToClose);

    MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder()
        .blocks(currentState.blocks());
    for (IndexMetaData openIndexMetadata : indicesToClose) {
        final String indexName = openIndexMetadata.getIndex().getName();
        mdBuilder.put(IndexMetaData.builder(openIndexMetadata).state(IndexMetaData.State.CLOSE));
        blocksBuilder.addIndexBlock(indexName, INDEX_CLOSED_BLOCK);
    }

    // mark closed at possible partitioned table template
    if (templateMetaData != null) {
        mdBuilder.put(updateOpenCloseOnPartitionTemplate(templateMetaData, false));
    }

    // The MetaData will always be overridden (and not merged!) when applying it on a cluster state builder.
    // So we must re-build the state with the latest modifications before we pass this state to possible modifiers.
    // Otherwise they would operate on the old MetaData and would just ignore any modifications.
    ClusterState updatedState = ClusterState.builder(currentState).metaData(mdBuilder).blocks(blocksBuilder).build();

    // call possible registered modifiers
    if (context.partitionName() != null) {
        updatedState = ddlClusterStateService.onCloseTablePartition(updatedState, context.partitionName());
    } else {
        updatedState = ddlClusterStateService.onCloseTable(updatedState, request.tableIdent());
    }


    RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable());
    for (IndexMetaData index : indicesToClose) {
        rtBuilder.remove(index.getIndex().getName());
    }

    //no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
    return allocationService.reroute(
        ClusterState.builder(updatedState).routingTable(rtBuilder.build()).build(),
        "indices closed " + indicesToClose);
}
 
Example #28
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 4 votes vote down vote up
private void addSourceIndicesRenamedToTargetName(ClusterState state,
                                                 MetaData metaData,
                                                 MetaData.Builder updatedMetaData,
                                                 ClusterBlocks.Builder blocksBuilder,
                                                 RoutingTable.Builder routingBuilder,
                                                 RelationName source,
                                                 RelationName target,
                                                 Consumer<String> onProcessedIndex) {
    String sourceTemplateName = PartitionName.templateName(source.schema(), source.name());
    IndexTemplateMetaData sourceTemplate = metaData.templates().get(sourceTemplateName);

    for (Index sourceIndex : indexNameResolver.concreteIndices(
        state, IndicesOptions.LENIENT_EXPAND_OPEN, source.indexNameOrAlias())) {

        String sourceIndexName = sourceIndex.getName();
        IndexMetaData sourceMd = metaData.getIndexSafe(sourceIndex);
        IndexMetaData targetMd;
        if (sourceTemplate == null) {
            targetMd = IndexMetaData.builder(sourceMd)
                .removeAllAliases()
                .index(target.indexNameOrAlias())
                .build();
            onProcessedIndex.accept(target.indexNameOrAlias());
        } else {
            PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
            String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
            targetMd = IndexMetaData.builder(sourceMd)
                .removeAllAliases()
                .putAlias(AliasMetaData.builder(target.indexNameOrAlias()).build())
                .index(targetIndexName)
                .build();
            onProcessedIndex.accept(targetIndexName);
        }
        updatedMetaData.put(targetMd, true);
        blocksBuilder.addBlocks(targetMd);
        routingBuilder.addAsFromCloseToOpen(targetMd);
    }
    if (sourceTemplate != null) {
        IndexTemplateMetaData.Builder templateBuilder = Templates.copyWithNewName(sourceTemplate, target);
        updatedMetaData.put(templateBuilder);
    }
}
 
Example #29
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Builder blocks(ClusterBlocks.Builder blocksBuilder) {
    return blocks(blocksBuilder.build());
}
 
Example #30
Source File: RoutingNodes.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ClusterBlocks blocks() {
    return this.blocks;
}