Search in sources :

Example 1 with Metadata

use of org.opensearch.cluster.metadata.Metadata in project OpenSearch by opensearch-project.

the class AllocationBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    final String[] params = indicesShardsReplicasSourceTargetRecoveries.split("\\|");
    numIndices = toInt(params[0]);
    numShards = toInt(params[1]);
    numReplicas = toInt(params[2]);
    sourceNodes = toInt(params[3]);
    targetNodes = toInt(params[4]);
    concurrentRecoveries = toInt(params[5]);
    int totalShardCount = (numReplicas + 1) * numShards * numIndices;
    initialClusterStrategy = Allocators.createAllocationService(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "zone").put("cluster.routing.allocation.node_concurrent_recoveries", "20").put("cluster.routing.allocation.exclude.tag", "tag_0").build());
    // We'll try to move nodes from tag_1 to tag_0
    clusterConcurrentRecoveries = Math.min(sourceNodes, targetNodes) * concurrentRecoveries;
    Metadata.Builder mb = Metadata.builder();
    for (int i = 1; i <= numIndices; i++) {
        mb.put(IndexMetadata.builder("test_" + i).settings(Settings.builder().put("index.version.created", Version.CURRENT)).numberOfShards(numShards).numberOfReplicas(numReplicas));
    }
    Metadata metadata = mb.build();
    RoutingTable.Builder rb = RoutingTable.builder();
    for (int i = 1; i <= numIndices; i++) {
        rb.addAsNew(metadata.index("test_" + i));
    }
    RoutingTable routingTable = rb.build();
    initialClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).nodes(setUpClusterNodes(sourceNodes, targetNodes)).build();
    // Start all unassigned shards
    initialClusterState = initialClusterStrategy.reroute(initialClusterState, "reroute");
    while (initialClusterState.getRoutingNodes().hasUnassignedShards()) {
        initialClusterState = initialClusterStrategy.applyStartedShards(initialClusterState, initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING));
        initialClusterState = initialClusterStrategy.reroute(initialClusterState, "reroute");
    }
    // Ensure all shards are started
    while (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() > 0) {
        initialClusterState = initialClusterStrategy.applyStartedShards(initialClusterState, initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING));
    }
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size() == totalShardCount);
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() == 0);
    assert (initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size() == 0);
    // make sure shards are only allocated on tag1
    for (ShardRouting startedShard : initialClusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED)) {
        assert (initialClusterState.getRoutingNodes().node(startedShard.currentNodeId()).node().getAttributes().get("tag")).equals("tag_1");
    }
}
Also used : RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with Metadata

use of org.opensearch.cluster.metadata.Metadata in project OpenSearch by opensearch-project.

the class ZenDiscoveryIT method testHandleNodeJoin_incompatibleClusterState.

public void testHandleNodeJoin_incompatibleClusterState() throws InterruptedException, ExecutionException, TimeoutException {
    String masterNode = internalCluster().startMasterOnlyNode();
    String node1 = internalCluster().startNode();
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class, node1);
    Coordinator coordinator = (Coordinator) internalCluster().getInstance(Discovery.class, masterNode);
    final ClusterState state = clusterService.state();
    Metadata.Builder mdBuilder = Metadata.builder(state.metadata());
    mdBuilder.putCustom(CustomMetadata.TYPE, new CustomMetadata("data"));
    ClusterState stateWithCustomMetadata = ClusterState.builder(state).metadata(mdBuilder).build();
    final CompletableFuture<Throwable> future = new CompletableFuture<>();
    DiscoveryNode node = state.nodes().getLocalNode();
    coordinator.sendValidateJoinRequest(stateWithCustomMetadata, new JoinRequest(node, 0L, Optional.empty()), new JoinHelper.JoinCallback() {

        @Override
        public void onSuccess() {
            future.completeExceptionally(new AssertionError("onSuccess should not be called"));
        }

        @Override
        public void onFailure(Exception e) {
            future.complete(e);
        }
    });
    Throwable t = future.get(10, TimeUnit.SECONDS);
    assertTrue(t instanceof IllegalStateException);
    assertTrue(t.getCause() instanceof RemoteTransportException);
    assertTrue(t.getCause().getCause() instanceof IllegalArgumentException);
    assertThat(t.getCause().getCause().getMessage(), containsString("Unknown NamedWriteable"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RemoteTransportException(org.opensearch.transport.RemoteTransportException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Discovery(org.opensearch.discovery.Discovery) Metadata(org.opensearch.cluster.metadata.Metadata) TestCustomMetadata(org.opensearch.test.TestCustomMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeoutException(java.util.concurrent.TimeoutException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterService(org.opensearch.cluster.service.ClusterService) TestCustomMetadata(org.opensearch.test.TestCustomMetadata)

Example 3 with Metadata

use of org.opensearch.cluster.metadata.Metadata in project OpenSearch by opensearch-project.

the class GatewayIndexStateIT method testHalfDeletedIndexImport.

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/48701")
public // This test relates to loading a broken state that was written by a 6.x node, but for now we do not load state from old nodes.
void testHalfDeletedIndexImport() throws Exception {
    // It's possible for a 6.x node to add a tombstone for an index but not actually delete the index metadata from disk since that
    // deletion is slightly deferred and may race against the node being shut down; if you upgrade to 7.x when in this state then the
    // node won't start.
    internalCluster().startNode();
    createIndex("test", Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build());
    ensureGreen("test");
    final Metadata metadata = internalCluster().getInstance(ClusterService.class).state().metadata();
    final Path[] paths = internalCluster().getInstance(NodeEnvironment.class).nodeDataPaths();
    // writeBrokenMeta(metaStateService -> {
    // metaStateService.writeGlobalState("test", Metadata.builder(metadata)
    // // we remove the manifest file, resetting the term and making this look like an upgrade from 6.x, so must also reset the
    // // term in the coordination metadata
    // .coordinationMetadata(CoordinationMetadata.builder(metadata.coordinationMetadata()).term(0L).build())
    // // add a tombstone but do not delete the index metadata from disk
    // .putCustom(IndexGraveyard.TYPE, IndexGraveyard.builder().addTombstone(metadata.index("test").getIndex()).build()).build());
    // for (final Path path : paths) {
    // try (Stream<Path> stateFiles = Files.list(path.resolve(MetadataStateFormat.STATE_DIR_NAME))) {
    // for (final Path manifestPath : stateFiles
    // .filter(p -> p.getFileName().toString().startsWith(Manifest.FORMAT.getPrefix())).collect(Collectors.toList())) {
    // IOUtils.rm(manifestPath);
    // }
    // }
    // }
    // });
    ensureGreen();
    assertBusy(() -> assertThat(internalCluster().getInstance(NodeEnvironment.class).availableIndexFolders(), empty()));
}
Also used : Path(java.nio.file.Path) NodeEnvironment(org.opensearch.env.NodeEnvironment) Metadata(org.opensearch.cluster.metadata.Metadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 4 with Metadata

use of org.opensearch.cluster.metadata.Metadata in project OpenSearch by opensearch-project.

the class UnsafeBootstrapAndDetachCommandIT method test3MasterNodes2Failed.

public void test3MasterNodes2Failed() throws Exception {
    internalCluster().setBootstrapMasterNodeIndex(2);
    List<String> masterNodes = new ArrayList<>();
    logger.info("--> start 1st master-eligible node");
    masterNodes.add(internalCluster().startMasterOnlyNode(Settings.builder().put(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), "0s").build()));
    // node ordinal 0
    logger.info("--> start one data-only node");
    String dataNode = internalCluster().startDataOnlyNode(Settings.builder().put(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), "0s").build());
    // node ordinal 1
    logger.info("--> start 2nd and 3rd master-eligible nodes and bootstrap");
    // node ordinals 2 and 3
    masterNodes.addAll(internalCluster().startMasterOnlyNodes(2));
    logger.info("--> wait for all nodes to join the cluster");
    ensureStableCluster(4);
    List<String> currentClusterNodes = new ArrayList<>(masterNodes);
    currentClusterNodes.add(dataNode);
    currentClusterNodes.forEach(node -> ensureReadOnlyBlock(false, node));
    logger.info("--> create index test");
    createIndex("test");
    ensureGreen("test");
    Settings master1DataPathSettings = internalCluster().dataPathSettings(masterNodes.get(0));
    Settings master2DataPathSettings = internalCluster().dataPathSettings(masterNodes.get(1));
    Settings master3DataPathSettings = internalCluster().dataPathSettings(masterNodes.get(2));
    Settings dataNodeDataPathSettings = internalCluster().dataPathSettings(dataNode);
    logger.info("--> stop 2nd and 3d master eligible node");
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(1)));
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(2)));
    logger.info("--> ensure NO_MASTER_BLOCK on data-only node");
    assertBusy(() -> {
        ClusterState state = internalCluster().client(dataNode).admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
        assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
    });
    logger.info("--> try to unsafely bootstrap 1st master-eligible node, while node lock is held");
    Environment environmentMaster1 = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(master1DataPathSettings).build());
    expectThrows(() -> unsafeBootstrap(environmentMaster1), UnsafeBootstrapMasterCommand.FAILED_TO_OBTAIN_NODE_LOCK_MSG);
    logger.info("--> stop 1st master-eligible node and data-only node");
    NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(0)));
    assertBusy(() -> internalCluster().getInstance(GatewayMetaState.class, dataNode).allPendingAsyncStatesWritten());
    internalCluster().stopRandomDataNode();
    logger.info("--> unsafely-bootstrap 1st master-eligible node");
    MockTerminal terminal = unsafeBootstrap(environmentMaster1, false, true);
    Metadata metadata = OpenSearchNodeCommand.createPersistedClusterStateService(Settings.EMPTY, nodeEnvironment.nodeDataPaths()).loadBestOnDiskState().metadata;
    assertThat(terminal.getOutput(), containsString(String.format(Locale.ROOT, UnsafeBootstrapMasterCommand.CLUSTER_STATE_TERM_VERSION_MSG_FORMAT, metadata.coordinationMetadata().term(), metadata.version())));
    logger.info("--> start 1st master-eligible node");
    String masterNode2 = internalCluster().startMasterOnlyNode(master1DataPathSettings);
    logger.info("--> detach-cluster on data-only node");
    Environment environmentData = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(dataNodeDataPathSettings).build());
    detachCluster(environmentData, false);
    logger.info("--> start data-only node");
    String dataNode2 = internalCluster().startDataOnlyNode(dataNodeDataPathSettings);
    logger.info("--> ensure there is no NO_MASTER_BLOCK and unsafe-bootstrap is reflected in cluster state");
    assertBusy(() -> {
        ClusterState state = internalCluster().client(dataNode2).admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
        assertFalse(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
        assertTrue(state.metadata().persistentSettings().getAsBoolean(UnsafeBootstrapMasterCommand.UNSAFE_BOOTSTRAP.getKey(), false));
    });
    List<String> bootstrappedNodes = new ArrayList<>();
    bootstrappedNodes.add(dataNode2);
    bootstrappedNodes.add(masterNode2);
    bootstrappedNodes.forEach(node -> ensureReadOnlyBlock(true, node));
    logger.info("--> ensure index test is green");
    ensureGreen("test");
    IndexMetadata indexMetadata = clusterService().state().metadata().index("test");
    assertThat(indexMetadata.getSettings().get(IndexMetadata.SETTING_HISTORY_UUID), notNullValue());
    logger.info("--> detach-cluster on 2nd and 3rd master-eligible nodes");
    Environment environmentMaster2 = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(master2DataPathSettings).build());
    detachCluster(environmentMaster2, false);
    Environment environmentMaster3 = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(master3DataPathSettings).build());
    detachCluster(environmentMaster3, false);
    logger.info("--> start 2nd and 3rd master-eligible nodes and ensure 4 nodes stable cluster");
    bootstrappedNodes.add(internalCluster().startMasterOnlyNode(master2DataPathSettings));
    bootstrappedNodes.add(internalCluster().startMasterOnlyNode(master3DataPathSettings));
    ensureStableCluster(4);
    bootstrappedNodes.forEach(node -> ensureReadOnlyBlock(true, node));
    removeBlock();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) NodeEnvironment(org.opensearch.env.NodeEnvironment) ArrayList(java.util.ArrayList) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) NodeEnvironment(org.opensearch.env.NodeEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MockTerminal(org.opensearch.cli.MockTerminal) Settings(org.opensearch.common.settings.Settings) DiscoverySettings(org.opensearch.node.Node.DiscoverySettings)

Example 5 with Metadata

use of org.opensearch.cluster.metadata.Metadata in project OpenSearch by opensearch-project.

the class GatewayMetaState method start.

public void start(Settings settings, TransportService transportService, ClusterService clusterService, MetaStateService metaStateService, MetadataIndexUpgradeService metadataIndexUpgradeService, MetadataUpgrader metadataUpgrader, PersistedClusterStateService persistedClusterStateService) {
    assert persistedState.get() == null : "should only start once, but already have " + persistedState.get();
    if (DiscoveryNode.isMasterNode(settings) || DiscoveryNode.isDataNode(settings)) {
        try {
            final PersistedClusterStateService.OnDiskState onDiskState = persistedClusterStateService.loadBestOnDiskState();
            Metadata metadata = onDiskState.metadata;
            long lastAcceptedVersion = onDiskState.lastAcceptedVersion;
            long currentTerm = onDiskState.currentTerm;
            if (onDiskState.empty()) {
                assert Version.CURRENT.major <= LegacyESVersion.V_7_0_0.major + 1 : "legacy metadata loader is not needed anymore from v9 onwards";
                final Tuple<Manifest, Metadata> legacyState = metaStateService.loadFullState();
                if (legacyState.v1().isEmpty() == false) {
                    metadata = legacyState.v2();
                    lastAcceptedVersion = legacyState.v1().getClusterStateVersion();
                    currentTerm = legacyState.v1().getCurrentTerm();
                }
            }
            PersistedState persistedState = null;
            boolean success = false;
            try {
                final ClusterState clusterState = prepareInitialClusterState(transportService, clusterService, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(settings)).version(lastAcceptedVersion).metadata(upgradeMetadataForNode(metadata, metadataIndexUpgradeService, metadataUpgrader)).build());
                if (DiscoveryNode.isMasterNode(settings)) {
                    persistedState = new LucenePersistedState(persistedClusterStateService, currentTerm, clusterState);
                } else {
                    persistedState = new AsyncLucenePersistedState(settings, transportService.getThreadPool(), new LucenePersistedState(persistedClusterStateService, currentTerm, clusterState));
                }
                if (DiscoveryNode.isDataNode(settings)) {
                    // unreference legacy files (only keep them for dangling indices functionality)
                    metaStateService.unreferenceAll();
                } else {
                    // delete legacy files
                    metaStateService.deleteAll();
                }
                // write legacy node metadata to prevent accidental downgrades from spawning empty cluster state
                NodeMetadata.FORMAT.writeAndCleanup(new NodeMetadata(persistedClusterStateService.getNodeId(), Version.CURRENT), persistedClusterStateService.getDataPaths());
                success = true;
            } finally {
                if (success == false) {
                    IOUtils.closeWhileHandlingException(persistedState);
                }
            }
            this.persistedState.set(persistedState);
        } catch (IOException e) {
            throw new OpenSearchException("failed to load metadata", e);
        }
    } else {
        final long currentTerm = 0L;
        final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(settings)).build();
        if (persistedClusterStateService.getDataPaths().length > 0) {
            // cluster uuid as coordinating-only nodes do not snap into a cluster as they carry no state
            try (PersistedClusterStateService.Writer persistenceWriter = persistedClusterStateService.createWriter()) {
                persistenceWriter.writeFullStateAndCommit(currentTerm, clusterState);
            } catch (IOException e) {
                throw new OpenSearchException("failed to load metadata", e);
            }
            try {
                // delete legacy cluster state files
                metaStateService.deleteAll();
                // write legacy node metadata to prevent downgrades from spawning empty cluster state
                NodeMetadata.FORMAT.writeAndCleanup(new NodeMetadata(persistedClusterStateService.getNodeId(), Version.CURRENT), persistedClusterStateService.getDataPaths());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        persistedState.set(new InMemoryPersistedState(currentTerm, clusterState));
    }
}
Also used : PersistedState(org.opensearch.cluster.coordination.CoordinationState.PersistedState) InMemoryPersistedState(org.opensearch.cluster.coordination.InMemoryPersistedState) ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) NodeMetadata(org.opensearch.env.NodeMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Manifest(org.opensearch.cluster.metadata.Manifest) NodeMetadata(org.opensearch.env.NodeMetadata) InMemoryPersistedState(org.opensearch.cluster.coordination.InMemoryPersistedState) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

Metadata (org.opensearch.cluster.metadata.Metadata)395 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)353 ClusterState (org.opensearch.cluster.ClusterState)283 RoutingTable (org.opensearch.cluster.routing.RoutingTable)190 Settings (org.opensearch.common.settings.Settings)84 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)72 ShardRouting (org.opensearch.cluster.routing.ShardRouting)71 Index (org.opensearch.index.Index)61 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)59 Matchers.containsString (org.hamcrest.Matchers.containsString)52 Version (org.opensearch.Version)49 ClusterSettings (org.opensearch.common.settings.ClusterSettings)47 ShardId (org.opensearch.index.shard.ShardId)47 ClusterService (org.opensearch.cluster.service.ClusterService)46 ClusterName (org.opensearch.cluster.ClusterName)45 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)45 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)43 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)42 List (java.util.List)41 ArrayList (java.util.ArrayList)40