Search in sources :

Example 66 with IndexMetaData

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

the class TransportShrinkActionTests method createClusterState.

private ClusterState createClusterState(String name, int numShards, int numReplicas, Settings settings) {
    MetaData.Builder metaBuilder = MetaData.builder();
    IndexMetaData indexMetaData = IndexMetaData.builder(name).settings(settings(Version.CURRENT).put(settings)).numberOfShards(numShards).numberOfReplicas(numReplicas).build();
    metaBuilder.put(indexMetaData, false);
    MetaData metaData = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    routingTableBuilder.addAsNew(metaData.index(name));
    RoutingTable routingTable = routingTableBuilder.build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).blocks(ClusterBlocks.builder().addBlocks(indexMetaData)).build();
    return clusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 67 with IndexMetaData

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

the class ClusterChangedEventTests method testIndexMetaDataChange.

/**
     * Test the index metadata change check.
     */
public void testIndexMetaDataChange() {
    final int numNodesInCluster = 3;
    final ClusterState state = createState(numNodesInCluster, randomBoolean(), initialIndices);
    // test when its not the same IndexMetaData
    final Index index = initialIndices.get(0);
    final IndexMetaData originalIndexMeta = state.metaData().index(index);
    // make sure the metadata is actually on the cluster state
    assertNotNull("IndexMetaData for " + index + " should exist on the cluster state", originalIndexMeta);
    IndexMetaData newIndexMeta = createIndexMetadata(index, originalIndexMeta.getVersion() + 1);
    assertTrue("IndexMetaData with different version numbers must be considered changed", ClusterChangedEvent.indexMetaDataChanged(originalIndexMeta, newIndexMeta));
    // test when it doesn't exist
    newIndexMeta = createIndexMetadata(new Index("doesntexist", UUIDs.randomBase64UUID()));
    assertTrue("IndexMetaData that didn't previously exist should be considered changed", ClusterChangedEvent.indexMetaDataChanged(originalIndexMeta, newIndexMeta));
    // test when its the same IndexMetaData
    assertFalse("IndexMetaData should be the same", ClusterChangedEvent.indexMetaDataChanged(originalIndexMeta, originalIndexMeta));
}
Also used : Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 68 with IndexMetaData

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

the class OldIndexBackwardsCompatibilityIT method testOldClusterStates.

public void testOldClusterStates() throws Exception {
    // dangling indices do not load the global state, only the per-index states
    // so we make sure we can read them separately
    MetaDataStateFormat<MetaData> globalFormat = new MetaDataStateFormat<MetaData>(XContentType.JSON, "global-") {

        @Override
        public void toXContent(XContentBuilder builder, MetaData state) throws IOException {
            throw new UnsupportedOperationException();
        }

        @Override
        public MetaData fromXContent(XContentParser parser) throws IOException {
            return MetaData.Builder.fromXContent(parser);
        }
    };
    MetaDataStateFormat<IndexMetaData> indexFormat = new MetaDataStateFormat<IndexMetaData>(XContentType.JSON, "state-") {

        @Override
        public void toXContent(XContentBuilder builder, IndexMetaData state) throws IOException {
            throw new UnsupportedOperationException();
        }

        @Override
        public IndexMetaData fromXContent(XContentParser parser) throws IOException {
            return IndexMetaData.Builder.fromXContent(parser);
        }
    };
    Collections.shuffle(indexes, random());
    for (String indexFile : indexes) {
        String indexName = indexFile.replace(".zip", "").toLowerCase(Locale.ROOT).replace("unsupported-", "index-");
        Path nodeDir = getNodeDir(indexFile);
        logger.info("Parsing cluster state files from index [{}]", indexName);
        final MetaData metaData = globalFormat.loadLatestState(logger, xContentRegistry(), nodeDir);
        assertNotNull(metaData);
        final Version version = Version.fromString(indexName.substring("index-".length()));
        final Path dataDir;
        if (version.before(Version.V_5_0_0_alpha1)) {
            dataDir = nodeDir.getParent().getParent();
        } else {
            dataDir = nodeDir.getParent();
        }
        final Path indexDir = getIndexDir(logger, indexName, indexFile, dataDir);
        assertNotNull(indexFormat.loadLatestState(logger, xContentRegistry(), indexDir));
    }
}
Also used : MetaDataStateFormat(org.elasticsearch.gateway.MetaDataStateFormat) Path(java.nio.file.Path) Version(org.elasticsearch.Version) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 69 with IndexMetaData

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

the class StartedShardsRoutingTests method testStartedShardsMatching.

public void testStartedShardsMatching() {
    AllocationService allocation = createAllocationService();
    logger.info("--> building initial cluster state");
    AllocationId allocationId = AllocationId.newRelocation(AllocationId.newInitializing());
    final IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0).putInSyncAllocationIds(1, Collections.singleton(allocationId.getId())).build();
    final Index index = indexMetaData.getIndex();
    ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).metaData(MetaData.builder().put(indexMetaData, false));
    final ShardRouting initShard = TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.INITIALIZING);
    final ShardRouting relocatingShard = TestShardRouting.newShardRouting(new ShardId(index, 1), "node1", "node2", true, ShardRoutingState.RELOCATING, allocationId);
    stateBuilder.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(initShard.shardId()).addShard(initShard).build()).addIndexShard(new IndexShardRoutingTable.Builder(relocatingShard.shardId()).addShard(relocatingShard).build())).build());
    ClusterState state = stateBuilder.build();
    logger.info("--> test starting of shard");
    ClusterState newState = allocation.applyStartedShards(state, Arrays.asList(initShard));
    assertThat("failed to start " + initShard + "\ncurrent routing table:" + newState.routingTable(), newState, not(equalTo(state)));
    assertTrue(initShard + "isn't started \ncurrent routing table:" + newState.routingTable(), newState.routingTable().index("test").shard(initShard.id()).allShardsStarted());
    state = newState;
    logger.info("--> testing starting of relocating shards");
    newState = allocation.applyStartedShards(state, Arrays.asList(relocatingShard.getTargetRelocatingShard()));
    assertThat("failed to start " + relocatingShard + "\ncurrent routing table:" + newState.routingTable(), newState, not(equalTo(state)));
    ShardRouting shardRouting = newState.routingTable().index("test").shard(relocatingShard.id()).getShards().get(0);
    assertThat(shardRouting.state(), equalTo(ShardRoutingState.STARTED));
    assertThat(shardRouting.currentNodeId(), equalTo("node2"));
    assertThat(shardRouting.relocatingNodeId(), nullValue());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 70 with IndexMetaData

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

the class ThrottlingAllocationTests method createRecoveryStateAndInitalizeAllocations.

private ClusterState createRecoveryStateAndInitalizeAllocations(MetaData metaData, TestGatewayAllocator gatewayAllocator) {
    DiscoveryNode node1 = newNode("node1");
    MetaData.Builder metaDataBuilder = new MetaData.Builder(metaData);
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    for (ObjectCursor<IndexMetaData> cursor : metaData.indices().values()) {
        Index index = cursor.value.getIndex();
        IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(cursor.value);
        final int recoveryType = randomInt(5);
        if (recoveryType <= 4) {
            addInSyncAllocationIds(index, indexMetaDataBuilder, gatewayAllocator, node1);
        }
        IndexMetaData indexMetaData = indexMetaDataBuilder.build();
        metaDataBuilder.put(indexMetaData, false);
        switch(recoveryType) {
            case 0:
                routingTableBuilder.addAsRecovery(indexMetaData);
                break;
            case 1:
                routingTableBuilder.addAsFromCloseToOpen(indexMetaData);
                break;
            case 2:
                routingTableBuilder.addAsFromDangling(indexMetaData);
                break;
            case 3:
                routingTableBuilder.addAsNewRestore(indexMetaData, new SnapshotRecoverySource(new Snapshot("repo", new SnapshotId("snap", "randomId")), Version.CURRENT, indexMetaData.getIndex().getName()), new IntHashSet());
                break;
            case 4:
                routingTableBuilder.addAsRestore(indexMetaData, new SnapshotRecoverySource(new Snapshot("repo", new SnapshotId("snap", "randomId")), Version.CURRENT, indexMetaData.getIndex().getName()));
                break;
            case 5:
                routingTableBuilder.addAsNew(indexMetaData);
                break;
            default:
                throw new IndexOutOfBoundsException();
        }
    }
    return ClusterState.builder(CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(DiscoveryNodes.builder().add(node1)).metaData(metaDataBuilder.build()).routingTable(routingTableBuilder.build()).build();
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IntHashSet(com.carrotsearch.hppc.IntHashSet) Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)253 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)194 ClusterState (org.elasticsearch.cluster.ClusterState)124 Settings (org.elasticsearch.common.settings.Settings)104 Index (org.elasticsearch.index.Index)100 Test (org.junit.Test)90 ShardId (org.elasticsearch.index.shard.ShardId)71 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)68 IOException (java.io.IOException)65 Metadata (org.elasticsearch.cluster.metadata.Metadata)62 IndexSettings (org.elasticsearch.index.IndexSettings)62 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)60 MetaData (org.elasticsearch.cluster.metadata.MetaData)58 HashSet (java.util.HashSet)56 HashMap (java.util.HashMap)54 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)54 Map (java.util.Map)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)49 ArrayList (java.util.ArrayList)47 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)44