Search in sources :

Example 11 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class ScriptContextTests method makeScriptService.

ScriptService makeScriptService() throws Exception {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").put("script." + PLUGIN_NAME + "_custom_globally_disabled_op", "false").put("script.engine." + MockScriptEngine.NAME + ".inline." + PLUGIN_NAME + "_custom_exp_disabled_op", "false").build();
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap("1", script -> "1"));
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine));
    List<ScriptContext.Plugin> customContexts = Arrays.asList(new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"), new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(customContexts);
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    ClusterState empty = ClusterState.builder(new ClusterName("_name")).build();
    ScriptMetaData smd = empty.metaData().custom(ScriptMetaData.TYPE);
    smd = ScriptMetaData.putStoredScript(smd, "1", new StoredScriptSource(MockScriptEngine.NAME, "1", Collections.emptyMap()));
    MetaData.Builder mdb = MetaData.builder(empty.getMetaData()).putCustom(ScriptMetaData.TYPE, smd);
    ClusterState stored = ClusterState.builder(empty).metaData(mdb).build();
    scriptService.clusterChanged(new ClusterChangedEvent("test", stored, empty));
    return scriptService;
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ClusterState(org.elasticsearch.cluster.ClusterState) Arrays(java.util.Arrays) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) Environment(org.elasticsearch.env.Environment) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) MetaData(org.elasticsearch.cluster.metadata.MetaData) Environment(org.elasticsearch.env.Environment) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings)

Example 12 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class DedicatedClusterSnapshotRestoreIT method testRestoreCustomMetadata.

public void testRestoreCustomMetadata() throws Exception {
    Path tempDir = randomRepoPath();
    logger.info("--> start node");
    internalCluster().startNode();
    Client client = client();
    createIndex("test-idx");
    logger.info("--> add custom persistent metadata");
    updateClusterState(new ClusterStateUpdater() {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            ClusterState.Builder builder = ClusterState.builder(currentState);
            MetaData.Builder metadataBuilder = MetaData.builder(currentState.metaData());
            metadataBuilder.putCustom(SnapshottableMetadata.TYPE, new SnapshottableMetadata("before_snapshot_s"));
            metadataBuilder.putCustom(NonSnapshottableMetadata.TYPE, new NonSnapshottableMetadata("before_snapshot_ns"));
            metadataBuilder.putCustom(SnapshottableGatewayMetadata.TYPE, new SnapshottableGatewayMetadata("before_snapshot_s_gw"));
            metadataBuilder.putCustom(NonSnapshottableGatewayMetadata.TYPE, new NonSnapshottableGatewayMetadata("before_snapshot_ns_gw"));
            metadataBuilder.putCustom(SnapshotableGatewayNoApiMetadata.TYPE, new SnapshotableGatewayNoApiMetadata("before_snapshot_s_gw_noapi"));
            builder.metaData(metadataBuilder);
            return builder.build();
        }
    });
    logger.info("--> create repository");
    PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", tempDir)).execute().actionGet();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("--> start snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().successfulShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").execute().actionGet().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> change custom persistent metadata");
    updateClusterState(new ClusterStateUpdater() {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            ClusterState.Builder builder = ClusterState.builder(currentState);
            MetaData.Builder metadataBuilder = MetaData.builder(currentState.metaData());
            if (randomBoolean()) {
                metadataBuilder.putCustom(SnapshottableMetadata.TYPE, new SnapshottableMetadata("after_snapshot_s"));
            } else {
                metadataBuilder.removeCustom(SnapshottableMetadata.TYPE);
            }
            metadataBuilder.putCustom(NonSnapshottableMetadata.TYPE, new NonSnapshottableMetadata("after_snapshot_ns"));
            if (randomBoolean()) {
                metadataBuilder.putCustom(SnapshottableGatewayMetadata.TYPE, new SnapshottableGatewayMetadata("after_snapshot_s_gw"));
            } else {
                metadataBuilder.removeCustom(SnapshottableGatewayMetadata.TYPE);
            }
            metadataBuilder.putCustom(NonSnapshottableGatewayMetadata.TYPE, new NonSnapshottableGatewayMetadata("after_snapshot_ns_gw"));
            metadataBuilder.removeCustom(SnapshotableGatewayNoApiMetadata.TYPE);
            builder.metaData(metadataBuilder);
            return builder.build();
        }
    });
    logger.info("--> delete repository");
    assertAcked(client.admin().cluster().prepareDeleteRepository("test-repo"));
    logger.info("--> create repository");
    putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo-2").setType("fs").setSettings(Settings.builder().put("location", tempDir)).execute().actionGet();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("--> restore snapshot");
    client.admin().cluster().prepareRestoreSnapshot("test-repo-2", "test-snap").setRestoreGlobalState(true).setIndices("-*").setWaitForCompletion(true).execute().actionGet();
    logger.info("--> make sure old repository wasn't restored");
    assertThrows(client.admin().cluster().prepareGetRepositories("test-repo"), RepositoryMissingException.class);
    assertThat(client.admin().cluster().prepareGetRepositories("test-repo-2").get().repositories().size(), equalTo(1));
    logger.info("--> check that custom persistent metadata was restored");
    ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
    logger.info("Cluster state: {}", clusterState);
    MetaData metaData = clusterState.getMetaData();
    assertThat(((SnapshottableMetadata) metaData.custom(SnapshottableMetadata.TYPE)).getData(), equalTo("before_snapshot_s"));
    assertThat(((NonSnapshottableMetadata) metaData.custom(NonSnapshottableMetadata.TYPE)).getData(), equalTo("after_snapshot_ns"));
    assertThat(((SnapshottableGatewayMetadata) metaData.custom(SnapshottableGatewayMetadata.TYPE)).getData(), equalTo("before_snapshot_s_gw"));
    assertThat(((NonSnapshottableGatewayMetadata) metaData.custom(NonSnapshottableGatewayMetadata.TYPE)).getData(), equalTo("after_snapshot_ns_gw"));
    logger.info("--> restart all nodes");
    internalCluster().fullRestart();
    ensureYellow();
    logger.info("--> check that gateway-persistent custom metadata survived full cluster restart");
    clusterState = client().admin().cluster().prepareState().get().getState();
    logger.info("Cluster state: {}", clusterState);
    metaData = clusterState.getMetaData();
    assertThat(metaData.custom(SnapshottableMetadata.TYPE), nullValue());
    assertThat(metaData.custom(NonSnapshottableMetadata.TYPE), nullValue());
    assertThat(((SnapshottableGatewayMetadata) metaData.custom(SnapshottableGatewayMetadata.TYPE)).getData(), equalTo("before_snapshot_s_gw"));
    assertThat(((NonSnapshottableGatewayMetadata) metaData.custom(NonSnapshottableGatewayMetadata.TYPE)).getData(), equalTo("after_snapshot_ns_gw"));
    // Shouldn't be returned as part of API response
    assertThat(metaData.custom(SnapshotableGatewayNoApiMetadata.TYPE), nullValue());
    // But should still be in state
    metaData = internalCluster().getInstance(ClusterService.class).state().metaData();
    assertThat(((SnapshotableGatewayNoApiMetadata) metaData.custom(SnapshotableGatewayNoApiMetadata.TYPE)).getData(), equalTo("before_snapshot_s_gw_noapi"));
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) IOException(java.io.IOException) ClusterService(org.elasticsearch.cluster.service.ClusterService) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) MetaData(org.elasticsearch.cluster.metadata.MetaData) TestCustomMetaData(org.elasticsearch.test.TestCustomMetaData) NodeClient(org.elasticsearch.client.node.NodeClient) Client(org.elasticsearch.client.Client)

Example 13 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class RepositoriesIT method testRepositoryCreation.

public void testRepositoryCreation() throws Exception {
    Client client = client();
    Path location = randomRepoPath();
    logger.info("-->  creating repository");
    PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo-1").setType("fs").setSettings(Settings.builder().put("location", location)).get();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("--> verify the repository");
    int numberOfFiles = FileSystemUtils.files(location).length;
    VerifyRepositoryResponse verifyRepositoryResponse = client.admin().cluster().prepareVerifyRepository("test-repo-1").get();
    assertThat(verifyRepositoryResponse.getNodes().length, equalTo(cluster().numDataAndMasterNodes()));
    logger.info("--> verify that we didn't leave any files as a result of verification");
    assertThat(FileSystemUtils.files(location).length, equalTo(numberOfFiles));
    logger.info("--> check that repository is really there");
    ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().clear().setMetaData(true).get();
    MetaData metaData = clusterStateResponse.getState().getMetaData();
    RepositoriesMetaData repositoriesMetaData = metaData.custom(RepositoriesMetaData.TYPE);
    assertThat(repositoriesMetaData, notNullValue());
    assertThat(repositoriesMetaData.repository("test-repo-1"), notNullValue());
    assertThat(repositoriesMetaData.repository("test-repo-1").type(), equalTo("fs"));
    logger.info("-->  creating another repository");
    putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo-2").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())).get();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("--> check that both repositories are in cluster state");
    clusterStateResponse = client.admin().cluster().prepareState().clear().setMetaData(true).get();
    metaData = clusterStateResponse.getState().getMetaData();
    repositoriesMetaData = metaData.custom(RepositoriesMetaData.TYPE);
    assertThat(repositoriesMetaData, notNullValue());
    assertThat(repositoriesMetaData.repositories().size(), equalTo(2));
    assertThat(repositoriesMetaData.repository("test-repo-1"), notNullValue());
    assertThat(repositoriesMetaData.repository("test-repo-1").type(), equalTo("fs"));
    assertThat(repositoriesMetaData.repository("test-repo-2"), notNullValue());
    assertThat(repositoriesMetaData.repository("test-repo-2").type(), equalTo("fs"));
    logger.info("--> check that both repositories can be retrieved by getRepositories query");
    GetRepositoriesResponse repositoriesResponse = client.admin().cluster().prepareGetRepositories(randomFrom("_all", "*", "test-repo-*")).get();
    assertThat(repositoriesResponse.repositories().size(), equalTo(2));
    assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-1"), notNullValue());
    assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue());
    logger.info("--> delete repository test-repo-1");
    client.admin().cluster().prepareDeleteRepository("test-repo-1").get();
    repositoriesResponse = client.admin().cluster().prepareGetRepositories().get();
    assertThat(repositoriesResponse.repositories().size(), equalTo(1));
    assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue());
    logger.info("--> delete repository test-repo-2");
    client.admin().cluster().prepareDeleteRepository("test-repo-2").get();
    repositoriesResponse = client.admin().cluster().prepareGetRepositories().get();
    assertThat(repositoriesResponse.repositories().size(), equalTo(0));
}
Also used : Path(java.nio.file.Path) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) MetaData(org.elasticsearch.cluster.metadata.MetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) VerifyRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse) PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) Client(org.elasticsearch.client.Client) GetRepositoriesResponse(org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse)

Example 14 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class ESSingleNodeTestCase method tearDown.

@Override
public void tearDown() throws Exception {
    logger.info("[{}#{}]: cleaning up after test", getTestClass().getSimpleName(), getTestName());
    super.tearDown();
    assertAcked(client().admin().indices().prepareDelete("*").get());
    MetaData metaData = client().admin().cluster().prepareState().get().getState().getMetaData();
    assertThat("test leaves persistent cluster metadata behind: " + metaData.persistentSettings().getAsMap(), metaData.persistentSettings().size(), equalTo(0));
    assertThat("test leaves transient cluster metadata behind: " + metaData.transientSettings().getAsMap(), metaData.transientSettings().size(), equalTo(0));
    if (resetNodeAfterTest()) {
        assert NODE != null;
        stopNode();
        //the seed can be created within this if as it will either be executed before every test method or will never be.
        startNode(random().nextLong());
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 15 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class ESIntegTestCase method getNumShards.

protected NumShards getNumShards(String index) {
    MetaData metaData = client().admin().cluster().prepareState().get().getState().metaData();
    assertThat(metaData.hasIndex(index), equalTo(true));
    int numShards = Integer.valueOf(metaData.index(index).getSettings().get(SETTING_NUMBER_OF_SHARDS));
    int numReplicas = Integer.valueOf(metaData.index(index).getSettings().get(SETTING_NUMBER_OF_REPLICAS));
    return new NumShards(numShards, numReplicas);
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData)

Aggregations

MetaData (org.elasticsearch.cluster.metadata.MetaData)244 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)223 ClusterState (org.elasticsearch.cluster.ClusterState)179 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)138 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)52 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)48 Settings (org.elasticsearch.common.settings.Settings)43 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)32 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)30 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)27 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)26 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)26 Index (org.elasticsearch.index.Index)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)21 HashMap (java.util.HashMap)19 HashSet (java.util.HashSet)18 ShardId (org.elasticsearch.index.shard.ShardId)17 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)17 ArrayList (java.util.ArrayList)15