Java Code Examples for org.elasticsearch.cluster.metadata.MetaData#builder()

The following examples show how to use org.elasticsearch.cluster.metadata.MetaData#builder() . 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: ClusterStateUpdaters.java    From crate with Apache License 2.0 6 votes vote down vote up
static ClusterState upgradeAndArchiveUnknownOrInvalidSettings(final ClusterState clusterState,
                                                              final ClusterSettings clusterSettings) {
    final MetaData.Builder metaDataBuilder = MetaData.builder(clusterState.metaData());

    metaDataBuilder.persistentSettings(
        clusterSettings.archiveUnknownOrInvalidSettings(
            clusterSettings.upgradeSettings(metaDataBuilder.persistentSettings()),
            e -> logUnknownSetting("persistent", e),
            (e, ex) -> logInvalidSetting("persistent", e, ex)));
    metaDataBuilder.transientSettings(
        clusterSettings.archiveUnknownOrInvalidSettings(
            clusterSettings.upgradeSettings(metaDataBuilder.transientSettings()),
            e -> logUnknownSetting("transient", e),
            (e, ex) -> logInvalidSetting("transient", e, ex)));
    return ClusterState.builder(clusterState).metaData(metaDataBuilder).build();
}
 
Example 2
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Remove any customs except for customs that we know all clients understand.
 *
 * @param clusterState the cluster state to remove possibly-unknown customs from
 * @return the cluster state with possibly-unknown customs removed
 */
private ClusterState removePluginCustoms(final ClusterState clusterState) {
    final ClusterState.Builder builder = ClusterState.builder(clusterState);
    clusterState.customs().keysIt().forEachRemaining(key -> {
        if (SAFE_CUSTOMS.contains(key) == false) {
            builder.removeCustom(key);
        }
    });
    final MetaData.Builder mdBuilder = MetaData.builder(clusterState.metaData());
    clusterState.metaData().customs().keysIt().forEachRemaining(key -> {
        if (SAFE_METADATA_CUSTOMS.contains(key) == false) {
            mdBuilder.removeCustom(key);
        }
    });
    builder.metaData(mdBuilder);
    return builder.build();
}
 
Example 3
Source File: JoinTaskExecutorTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testSuccess() {
    Settings.builder().build();
    MetaData.Builder metaBuilder = MetaData.builder();
    IndexMetaData indexMetaData = IndexMetaData.builder("test")
        .settings(settings(VersionUtils.randomVersionBetween(random(),
            Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
        .numberOfShards(1)
        .numberOfReplicas(1).build();
    metaBuilder.put(indexMetaData, false);
    indexMetaData = IndexMetaData.builder("test1")
        .settings(settings(VersionUtils.randomVersionBetween(random(),
            Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
        .numberOfShards(1)
        .numberOfReplicas(1).build();
    metaBuilder.put(indexMetaData, false);
    MetaData metaData = metaBuilder.build();
        JoinTaskExecutor.ensureIndexCompatibility(Version.CURRENT,
            metaData);
}
 
Example 4
Source File: DropTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected ClusterState execute(ClusterState currentState, DropTableRequest request) throws Exception {
    RelationName relationName = request.tableIdent();
    final Set<Index> concreteIndices = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndices(
        currentState, IndicesOptions.lenientExpandOpen(), relationName.indexNameOrAlias())));
    currentState = deleteIndexService.deleteIndices(currentState, concreteIndices);

    if (request.isPartitioned()) {
        // delete template
        String templateName = PartitionName.templateName(relationName.schema(), relationName.name());
        MetaData.Builder metaData = MetaData.builder(currentState.metaData());
        metaData.removeTemplate(templateName);
        currentState = ClusterState.builder(currentState).metaData(metaData).build();
    }

    // call possible modifiers
    currentState = ddlClusterStateService.onDropTable(currentState, request.tableIdent());

    return currentState;
}
 
Example 5
Source File: ClusterModule.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * For interoperability with transport clients older than 6.3, we need to strip customs
 * from the cluster state that the client might not be able to deserialize
 *
 * @param clusterState the cluster state to filter the customs from
 * @return the adapted cluster state
 */
public static ClusterState filterCustomsForPre63Clients(ClusterState clusterState) {
    final ClusterState.Builder builder = ClusterState.builder(clusterState);
    clusterState.customs().keysIt().forEachRemaining(name -> {
        if (PRE_6_3_CLUSTER_CUSTOMS_WHITE_LIST.contains(name) == false) {
            builder.removeCustom(name);
        }
    });
    final MetaData.Builder metaBuilder = MetaData.builder(clusterState.metaData());
    clusterState.metaData().customs().keysIt().forEachRemaining(name -> {
        if (PRE_6_3_METADATA_CUSTOMS_WHITE_LIST.contains(name) == false) {
            metaBuilder.removeCustom(name);
        }
    });
    return builder.metaData(metaBuilder).build();
}
 
Example 6
Source File: TransportPrivilegesActionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyPrivilegesCreatesNewPrivilegesInstance() {
    // given
    MetaData.Builder mdBuilder = MetaData.builder();
    Map<String, Set<Privilege>> usersPrivileges = new HashMap<>();
    usersPrivileges.put("Ford", new HashSet<>(PRIVILEGES));
    UsersPrivilegesMetaData initialPrivilegesMetadata = new UsersPrivilegesMetaData(usersPrivileges);
    mdBuilder.putCustom(UsersPrivilegesMetaData.TYPE, initialPrivilegesMetadata);
    PrivilegesRequest denyPrivilegeRequest =
        new PrivilegesRequest(Collections.singletonList("Ford"), Collections.singletonList(DENY_DQL));

    //when
    TransportPrivilegesAction.applyPrivileges(mdBuilder, denyPrivilegeRequest);

    // then
    UsersPrivilegesMetaData newPrivilegesMetadata =
        (UsersPrivilegesMetaData) mdBuilder.getCustom(UsersPrivilegesMetaData.TYPE);
    assertNotSame(newPrivilegesMetadata, initialPrivilegesMetadata);
}
 
Example 7
Source File: GatewayMetaState.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Elasticsearch 2.0 removed several deprecated features and as well as support for Lucene 3.x. This method calls
 * {@link MetaDataIndexUpgradeService} to makes sure that indices are compatible with the current version. The
 * MetaDataIndexUpgradeService might also update obsolete settings if needed.
 * Allows upgrading global custom meta data via {@link MetaDataUpgrader#customMetaDataUpgraders}
 *
 * @return input <code>metaData</code> if no upgrade is needed or an upgraded metaData
 */
static MetaData upgradeMetaData(MetaData metaData,
                                MetaDataIndexUpgradeService metaDataIndexUpgradeService,
                                MetaDataUpgrader metaDataUpgrader) {
    // upgrade index meta data
    boolean changed = false;
    final MetaData.Builder upgradedMetaData = MetaData.builder(metaData);
    for (IndexMetaData indexMetaData : metaData) {
        IndexMetaData newMetaData = metaDataIndexUpgradeService.upgradeIndexMetaData(indexMetaData,
                Version.CURRENT.minimumIndexCompatibilityVersion());
        changed |= indexMetaData != newMetaData;
        upgradedMetaData.put(newMetaData, false);
    }
    // upgrade global custom meta data
    if (applyPluginUpgraders(metaData.getCustoms(), metaDataUpgrader.customMetaDataUpgraders,
            upgradedMetaData::removeCustom, upgradedMetaData::putCustom)) {
        changed = true;
    }
    // upgrade current templates
    if (applyPluginUpgraders(metaData.getTemplates(), metaDataUpgrader.indexTemplateMetaDataUpgraders,
            upgradedMetaData::removeTemplate, (s, indexTemplateMetaData) -> upgradedMetaData.put(indexTemplateMetaData))) {
        changed = true;
    }
    return changed ? upgradedMetaData.build() : metaData;
}
 
Example 8
Source File: MetaStateService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the full state, which includes both the global state and all the indices
 * meta state.
 */
MetaData loadFullState() throws Exception {
    MetaData globalMetaData = loadGlobalState();
    MetaData.Builder metaDataBuilder;
    if (globalMetaData != null) {
        metaDataBuilder = MetaData.builder(globalMetaData);
    } else {
        metaDataBuilder = MetaData.builder();
    }

    final Set<String> indices = nodeEnv.findAllIndices();
    for (String index : indices) {
        IndexMetaData indexMetaData = loadIndexState(index);
        if (indexMetaData == null) {
            logger.debug("[{}] failed to find metadata for existing index location", index);
        } else {
            metaDataBuilder.put(indexMetaData, false);
        }
    }
    return metaDataBuilder.build();
}
 
Example 9
Source File: IndexMetaDataUpdater.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the current {@link MetaData} based on the changes of this RoutingChangesObserver. Specifically
 * we update {@link IndexMetaData#getInSyncAllocationIds()} and {@link IndexMetaData#primaryTerm(int)} based on
 * the changes made during this allocation.
 *
 * @param oldMetaData {@link MetaData} object from before the routing nodes was changed.
 * @param newRoutingTable {@link RoutingTable} object after routing changes were applied.
 * @return adapted {@link MetaData}, potentially the original one if no change was needed.
 */
public MetaData applyChanges(MetaData oldMetaData, RoutingTable newRoutingTable) {
    Map<Index, List<Map.Entry<ShardId, Updates>>> changesGroupedByIndex =
        shardChanges.entrySet().stream().collect(Collectors.groupingBy(e -> e.getKey().getIndex()));

    MetaData.Builder metaDataBuilder = null;
    for (Map.Entry<Index, List<Map.Entry<ShardId, Updates>>> indexChanges : changesGroupedByIndex.entrySet()) {
        Index index = indexChanges.getKey();
        final IndexMetaData oldIndexMetaData = oldMetaData.getIndexSafe(index);
        IndexMetaData.Builder indexMetaDataBuilder = null;
        for (Map.Entry<ShardId, Updates> shardEntry : indexChanges.getValue()) {
            ShardId shardId = shardEntry.getKey();
            Updates updates = shardEntry.getValue();
            indexMetaDataBuilder = updateInSyncAllocations(newRoutingTable, oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
            indexMetaDataBuilder = updatePrimaryTerm(oldIndexMetaData, indexMetaDataBuilder, shardId, updates);
        }

        if (indexMetaDataBuilder != null) {
            if (metaDataBuilder == null) {
                metaDataBuilder = MetaData.builder(oldMetaData);
            }
            metaDataBuilder.put(indexMetaDataBuilder);
        }
    }

    if (metaDataBuilder != null) {
        return metaDataBuilder.build();
    } else {
        return oldMetaData;
    }
}
 
Example 10
Source File: SnapshotsService.java    From crate with Apache License 2.0 5 votes vote down vote up
private static MetaData metaDataForSnapshot(SnapshotsInProgress.Entry snapshot, MetaData metaData) {
    if (snapshot.includeGlobalState() == false) {
        // Remove global state from the cluster state
        MetaData.Builder builder = MetaData.builder();
        for (IndexId index : snapshot.indices()) {
            builder.put(metaData.index(index.getName()), false);
        }
        metaData = builder.build();
    }
    return metaData;
}
 
Example 11
Source File: SwapRelationsOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
private UpdatedState applyDropRelations(ClusterState state, UpdatedState updatedState, List<RelationName> dropRelations) {
    ClusterState stateAfterRename = updatedState.newState;
    MetaData.Builder updatedMetaData = MetaData.builder(stateAfterRename.metaData());
    RoutingTable.Builder routingBuilder = RoutingTable.builder(stateAfterRename.routingTable());

    for (RelationName dropRelation : dropRelations) {
        for (Index index : indexNameResolver.concreteIndices(
            state, IndicesOptions.LENIENT_EXPAND_OPEN, dropRelation.indexNameOrAlias())) {

            String indexName = index.getName();
            updatedMetaData.remove(indexName);
            routingBuilder.remove(indexName);
            updatedState.newIndices.remove(indexName);
        }
    }
    ClusterState stateAfterDropRelations = ClusterState
        .builder(stateAfterRename)
        .metaData(updatedMetaData)
        .routingTable(routingBuilder.build())
        .build();
    return new UpdatedState(
        allocationService.reroute(
            applyDropTableClusterStateModifiers(stateAfterDropRelations, dropRelations),
            "indices drop after name switch"
        ),
        updatedState.newIndices
    );
}
 
Example 12
Source File: MetaStateService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the full state, which includes both the global state and all the indices meta data. <br>
 * When loading, manifest file is consulted (represented by {@link Manifest} class), to load proper generations. <br>
 * If there is no manifest file on disk, this method fallbacks to BWC mode, where latest generation of global and indices
 * metadata is loaded. Please note that currently there is no way to distinguish between manifest file being removed and manifest
 * file was not yet created. It means that this method always fallbacks to BWC mode, if there is no manifest file.
 *
 * @return tuple of {@link Manifest} and {@link MetaData} with global metadata and indices metadata. If there is no state on disk,
 * meta state with globalGeneration -1 and empty meta data is returned.
 * @throws IOException if some IOException when loading files occurs or there is no metadata referenced by manifest file.
 */
Tuple<Manifest, MetaData> loadFullState() throws IOException {
    final Manifest manifest = MANIFEST_FORMAT.loadLatestState(LOGGER, namedXContentRegistry, nodeEnv.nodeDataPaths());
    if (manifest == null) {
        return loadFullStateBWC();
    }

    final MetaData.Builder metaDataBuilder;
    if (manifest.isGlobalGenerationMissing()) {
        metaDataBuilder = MetaData.builder();
    } else {
        final MetaData globalMetaData = META_DATA_FORMAT.loadGeneration(LOGGER, namedXContentRegistry, manifest.getGlobalGeneration(),
                nodeEnv.nodeDataPaths());
        if (globalMetaData != null) {
            metaDataBuilder = MetaData.builder(globalMetaData);
        } else {
            throw new IOException("failed to find global metadata [generation: " + manifest.getGlobalGeneration() + "]");
        }
    }

    for (Map.Entry<Index, Long> entry : manifest.getIndexGenerations().entrySet()) {
        final Index index = entry.getKey();
        final long generation = entry.getValue();
        final String indexFolderName = index.getUUID();
        final IndexMetaData indexMetaData = INDEX_META_DATA_FORMAT.loadGeneration(LOGGER, namedXContentRegistry, generation,
                nodeEnv.resolveIndexFolder(indexFolderName));
        if (indexMetaData != null) {
            metaDataBuilder.put(indexMetaData, false);
        } else {
            throw new IOException("failed to find metadata for existing index " + index.getName() + " [location: " + indexFolderName +
                    ", generation: " + generation + "]");
        }
    }

    return new Tuple<>(manifest, metaDataBuilder.build());
}
 
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: JoinTaskExecutorTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testPreventJoinClusterWithNewerIndices() {
    Settings.builder().build();
    MetaData.Builder metaBuilder = MetaData.builder();
    IndexMetaData indexMetaData = IndexMetaData.builder("test")
        .settings(settings(Version.CURRENT))
        .numberOfShards(1)
        .numberOfReplicas(1).build();
    metaBuilder.put(indexMetaData, false);
    MetaData metaData = metaBuilder.build();
    JoinTaskExecutor.ensureIndexCompatibility(Version.CURRENT, metaData);

    expectThrows(IllegalStateException.class, () ->
    JoinTaskExecutor.ensureIndexCompatibility(VersionUtils.getPreviousVersion(Version.CURRENT),
        metaData));
}
 
Example 15
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterState onRenameTable(ClusterState currentState,
                                  RelationName sourceRelationName,
                                  RelationName targetRelationName,
                                  boolean isPartitionedTable) {
    MetaData currentMetaData = currentState.metaData();
    MetaData.Builder mdBuilder = MetaData.builder(currentMetaData);
    if (transferTablePrivileges(mdBuilder, sourceRelationName, targetRelationName)) {
        return ClusterState.builder(currentState).metaData(mdBuilder).build();
    }
    return currentState;
}
 
Example 16
Source File: UserManagerDDLModifier.java    From crate with Apache License 2.0 5 votes vote down vote up
private ClusterState dropPrivilegesForTableOrView(ClusterState currentState, RelationName relationName) {
    MetaData currentMetaData = currentState.metaData();
    MetaData.Builder mdBuilder = MetaData.builder(currentMetaData);

    if (dropPrivileges(mdBuilder, relationName) == false) {
        // if nothing is affected, don't modify the state and just return the given currentState
        return currentState;
    }

    return ClusterState.builder(currentState).metaData(mdBuilder).build();
}
 
Example 17
Source File: TransportUserActionTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropUser() throws Exception {
    UsersMetaData oldMetaData = new UsersMetaData(UserDefinitions.DUMMY_USERS);
    MetaData.Builder mdBuilder = MetaData.builder();
    boolean res = TransportDropUserAction.dropUser(mdBuilder, oldMetaData, "Arthur");
    assertThat(users(mdBuilder), contains("Ford"));
    assertThat(res, is(true));
}
 
Example 18
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 19
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 20
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"
    );
}