Search in sources :

Example 1 with SettingUpgrader

use of org.opensearch.common.settings.SettingUpgrader in project OpenSearch by opensearch-project.

the class ClusterStateUpdatersTests method runUpgradeSettings.

private void runUpgradeSettings(final BiConsumer<Metadata.Builder, Settings> applySettingsToBuilder, final Function<Metadata, Settings> metadataSettings) {
    final Setting<String> oldSetting = Setting.simpleString("foo.old", Setting.Property.Dynamic, Setting.Property.NodeScope);
    final Setting<String> newSetting = Setting.simpleString("foo.new", Setting.Property.Dynamic, Setting.Property.NodeScope);
    final Set<Setting<?>> settingsSet = Stream.concat(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), Stream.of(oldSetting, newSetting)).collect(Collectors.toSet());
    final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, settingsSet, Collections.singleton(new SettingUpgrader<String>() {

        @Override
        public Setting<String> getSetting() {
            return oldSetting;
        }

        @Override
        public String getKey(final String key) {
            return "foo.new";
        }

        @Override
        public String getValue(final String value) {
            return "new." + value;
        }
    }));
    final ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, null);
    final Metadata.Builder builder = Metadata.builder();
    final Settings settings = Settings.builder().put("foo.old", randomAlphaOfLength(8)).build();
    applySettingsToBuilder.accept(builder, settings);
    final ClusterState initialState = ClusterState.builder(clusterService.getClusterName()).metadata(builder.build()).build();
    final ClusterState state = upgradeAndArchiveUnknownOrInvalidSettings(initialState, clusterService.getClusterSettings());
    assertFalse(oldSetting.exists(metadataSettings.apply(state.metadata())));
    assertTrue(newSetting.exists(metadataSettings.apply(state.metadata())));
    assertThat(newSetting.get(metadataSettings.apply(state.metadata())), equalTo("new." + oldSetting.get(settings)));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterService(org.opensearch.cluster.service.ClusterService) Setting(org.opensearch.common.settings.Setting) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) SettingUpgrader(org.opensearch.common.settings.SettingUpgrader) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ClusterStateUpdaters.upgradeAndArchiveUnknownOrInvalidSettings(org.opensearch.gateway.ClusterStateUpdaters.upgradeAndArchiveUnknownOrInvalidSettings)

Aggregations

ClusterState (org.opensearch.cluster.ClusterState)1 CoordinationMetadata (org.opensearch.cluster.coordination.CoordinationMetadata)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 Setting (org.opensearch.common.settings.Setting)1 SettingUpgrader (org.opensearch.common.settings.SettingUpgrader)1 Settings (org.opensearch.common.settings.Settings)1 ClusterStateUpdaters.upgradeAndArchiveUnknownOrInvalidSettings (org.opensearch.gateway.ClusterStateUpdaters.upgradeAndArchiveUnknownOrInvalidSettings)1