Search in sources :

Example 71 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class WildcardExpressionResolverTests method testMultipleWildcards.

// issue #13334
public void testMultipleWildcards() {
    MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("testXXX")).put(indexBuilder("testXXY")).put(indexBuilder("testXYY")).put(indexBuilder("testYYY")).put(indexBuilder("kuku")).put(indexBuilder("kukuYYY"));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
    IndexNameExpressionResolver.WildcardExpressionResolver resolver = new IndexNameExpressionResolver.WildcardExpressionResolver();
    IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen());
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("test*X*"))), equalTo(newHashSet("testXXX", "testXXY", "testXYY")));
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("test*X*Y"))), equalTo(newHashSet("testXXY", "testXYY")));
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("kuku*Y*"))), equalTo(newHashSet("kukuYYY")));
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("*Y*"))), equalTo(newHashSet("testXXY", "testXYY", "testYYY", "kukuYYY")));
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("test*Y*X"))).size(), equalTo(0));
    assertThat(newHashSet(resolver.resolve(context, Arrays.asList("*Y*X"))).size(), equalTo(0));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterName(org.elasticsearch.cluster.ClusterName)

Example 72 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class ClusterStateChanges method executeClusterStateUpdateTask.

private ClusterState executeClusterStateUpdateTask(ClusterState state, Runnable runnable) {
    ClusterState[] result = new ClusterState[1];
    doAnswer(invocationOnMock -> {
        ClusterStateUpdateTask task = (ClusterStateUpdateTask) invocationOnMock.getArguments()[1];
        result[0] = task.execute(state);
        return null;
    }).when(clusterService).submitStateUpdateTask(anyString(), any(ClusterStateUpdateTask.class));
    runnable.run();
    assertThat(result[0], notNullValue());
    return result[0];
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask)

Example 73 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class IndicesClusterStateServiceRandomUpdatesTests method testRandomClusterStateUpdates.

public void testRandomClusterStateUpdates() {
    // we have an IndicesClusterStateService per node in the cluster
    final Map<DiscoveryNode, IndicesClusterStateService> clusterStateServiceMap = new HashMap<>();
    ClusterState state = randomInitialClusterState(clusterStateServiceMap, MockIndicesService::new);
    // each of the following iterations represents a new cluster state update processed on all nodes
    for (int i = 0; i < 30; i++) {
        logger.info("Iteration {}", i);
        final ClusterState previousState = state;
        // calculate new cluster state
        for (int j = 0; j < randomInt(3); j++) {
            // multiple iterations to simulate batching of cluster states
            try {
                state = randomlyUpdateClusterState(state, clusterStateServiceMap, MockIndicesService::new);
            } catch (AssertionError error) {
                ClusterState finalState = state;
                logger.error((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("failed to random change state. last good state: \n{}", finalState), error);
                throw error;
            }
        }
        // apply cluster state to nodes (incl. master)
        for (DiscoveryNode node : state.nodes()) {
            IndicesClusterStateService indicesClusterStateService = clusterStateServiceMap.get(node);
            ClusterState localState = adaptClusterStateToLocalNode(state, node);
            ClusterState previousLocalState = adaptClusterStateToLocalNode(previousState, node);
            final ClusterChangedEvent event = new ClusterChangedEvent("simulated change " + i, localState, previousLocalState);
            try {
                indicesClusterStateService.applyClusterState(event);
            } catch (AssertionError error) {
                logger.error((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("failed to apply change on [{}].\n ***  Previous state ***\n{}\n ***  New state ***\n{}", node, event.previousState(), event.state()), error);
                throw error;
            }
            // check that cluster state has been properly applied to node
            assertClusterStateMatchesNodeState(localState, indicesClusterStateService);
        }
    }
    // TODO: check if we can go to green by starting all shards and finishing all iterations
    logger.info("Final cluster state: {}", state);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 74 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class FlushIT method testSyncedFlush.

public void testSyncedFlush() throws ExecutionException, InterruptedException, IOException {
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("test").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).get();
    ensureGreen();
    final Index index = client().admin().cluster().prepareState().get().getState().metaData().index("test").getIndex();
    IndexStats indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    ShardsSyncedFlushResult result;
    if (randomBoolean()) {
        logger.info("--> sync flushing shard 0");
        result = SyncedFlushUtil.attemptSyncedFlush(internalCluster(), new ShardId(index, 0));
    } else {
        logger.info("--> sync flushing index [test]");
        SyncedFlushResponse indicesResult = client().admin().indices().prepareSyncedFlush("test").get();
        result = indicesResult.getShardsResultPerIndex().get("test").get(0);
    }
    assertFalse(result.failed());
    assertThat(result.totalShards(), equalTo(indexStats.getShards().length));
    assertThat(result.successfulShards(), equalTo(indexStats.getShards().length));
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    String syncId = result.syncId();
    for (ShardStats shardStats : indexStats.getShards()) {
        final String shardSyncId = shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID);
        assertThat(shardSyncId, equalTo(syncId));
    }
    // now, start new node and relocate a shard there and see if sync id still there
    String newNodeName = internalCluster().startNode();
    ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
    ShardRouting shardRouting = clusterState.getRoutingTable().index("test").shard(0).iterator().next();
    String currentNodeName = clusterState.nodes().resolveNode(shardRouting.currentNodeId()).getName();
    assertFalse(currentNodeName.equals(newNodeName));
    internalCluster().client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, currentNodeName, newNodeName)).get();
    client().admin().cluster().prepareHealth().setWaitForNoRelocatingShards(true).get();
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build()).get();
    ensureGreen("test");
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, internalCluster().numDataNodes() - 1).build()).get();
    ensureGreen("test");
    indexStats = client().admin().indices().prepareStats("test").get().getIndex("test");
    for (ShardStats shardStats : indexStats.getShards()) {
        assertNotNull(shardStats.getCommitStats().getUserData().get(Engine.SYNC_COMMIT_ID));
    }
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Index(org.elasticsearch.index.Index) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 75 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class SyncedFlushSingleNodeTests method testModificationPreventsFlushing.

public void testModificationPreventsFlushing() throws InterruptedException {
    createIndex("test");
    client().prepareIndex("test", "test", "1").setSource("{}", XContentType.JSON).get();
    IndexService test = getInstanceFromNode(IndicesService.class).indexService(resolveIndex("test"));
    IndexShard shard = test.getShardOrNull(0);
    SyncedFlushService flushService = getInstanceFromNode(SyncedFlushService.class);
    final ShardId shardId = shard.shardId();
    final ClusterState state = getInstanceFromNode(ClusterService.class).state();
    final IndexShardRoutingTable shardRoutingTable = flushService.getShardRoutingTable(shardId, state);
    final List<ShardRouting> activeShards = shardRoutingTable.activeShards();
    assertEquals("exactly one active shard", 1, activeShards.size());
    Map<String, Engine.CommitId> commitIds = SyncedFlushUtil.sendPreSyncRequests(flushService, activeShards, state, shardId);
    assertEquals("exactly one commit id", 1, commitIds.size());
    client().prepareIndex("test", "test", "2").setSource("{}", XContentType.JSON).get();
    String syncId = UUIDs.base64UUID();
    SyncedFlushUtil.LatchedListener<ShardsSyncedFlushResult> listener = new SyncedFlushUtil.LatchedListener<>();
    flushService.sendSyncRequests(syncId, activeShards, state, commitIds, shardId, shardRoutingTable.size(), listener);
    listener.latch.await();
    assertNull(listener.error);
    ShardsSyncedFlushResult syncedFlushResult = listener.result;
    assertNotNull(syncedFlushResult);
    assertEquals(0, syncedFlushResult.successfulShards());
    assertEquals(1, syncedFlushResult.totalShards());
    assertEquals(syncId, syncedFlushResult.syncId());
    assertNotNull(syncedFlushResult.shardResponses().get(activeShards.get(0)));
    assertFalse(syncedFlushResult.shardResponses().get(activeShards.get(0)).success());
    assertEquals("pending operations", syncedFlushResult.shardResponses().get(activeShards.get(0)).failureReason());
    // pull another commit and make sure we can't sync-flush with the old one
    SyncedFlushUtil.sendPreSyncRequests(flushService, activeShards, state, shardId);
    listener = new SyncedFlushUtil.LatchedListener();
    flushService.sendSyncRequests(syncId, activeShards, state, commitIds, shardId, shardRoutingTable.size(), listener);
    listener.latch.await();
    assertNull(listener.error);
    syncedFlushResult = listener.result;
    assertNotNull(syncedFlushResult);
    assertEquals(0, syncedFlushResult.successfulShards());
    assertEquals(1, syncedFlushResult.totalShards());
    assertEquals(syncId, syncedFlushResult.syncId());
    assertNotNull(syncedFlushResult.shardResponses().get(activeShards.get(0)));
    assertFalse(syncedFlushResult.shardResponses().get(activeShards.get(0)).success());
    assertEquals("commit has changed", syncedFlushResult.shardResponses().get(activeShards.get(0)).failureReason());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)564 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)211 MetaData (org.elasticsearch.cluster.metadata.MetaData)179 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)150 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)123 Settings (org.elasticsearch.common.settings.Settings)100 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)85 ClusterName (org.elasticsearch.cluster.ClusterName)82 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)75 Matchers.containsString (org.hamcrest.Matchers.containsString)72 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)62 ShardId (org.elasticsearch.index.shard.ShardId)61 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)59 ArrayList (java.util.ArrayList)57 IOException (java.io.IOException)55 Index (org.elasticsearch.index.Index)53 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)49 CountDownLatch (java.util.concurrent.CountDownLatch)47 HashSet (java.util.HashSet)45 List (java.util.List)45