Search in sources :

Example 56 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class SettingsUpdaterTests method testClusterBlock.

public void testClusterBlock() {
    ClusterState.Builder builder = ClusterState.builder(new ClusterName("foo"));
    ClusterSettings settingsService = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    AtomicReference<Float> index = new AtomicReference<>();
    AtomicReference<Float> shard = new AtomicReference<>();
    settingsService.addSettingsUpdateConsumer(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING, index::set);
    settingsService.addSettingsUpdateConsumer(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING, shard::set);
    SettingsUpdater updater = new SettingsUpdater(settingsService);
    MetaData.Builder metaData = MetaData.builder().persistentSettings(Settings.builder().put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 1.5).put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 2.5).build()).transientSettings(Settings.builder().put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 3.5).put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 4.5).build());
    ClusterState build = builder.metaData(metaData).build();
    ClusterState clusterState = updater.updateSettings(build, Settings.builder().put(MetaData.SETTING_READ_ONLY_SETTING.getKey(), true).build(), Settings.builder().put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 1.6).put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 1.0f).build());
    assertEquals(clusterState.blocks().global().size(), 1);
    assertEquals(clusterState.blocks().global().iterator().next(), MetaData.CLUSTER_READ_ONLY_BLOCK);
    clusterState = updater.updateSettings(build, Settings.EMPTY, Settings.builder().put(MetaData.SETTING_READ_ONLY_SETTING.getKey(), false).build());
    assertEquals(clusterState.blocks().global().size(), 0);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) MetaData(org.elasticsearch.cluster.metadata.MetaData) ClusterName(org.elasticsearch.cluster.ClusterName) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 57 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class OperationRoutingTests method testPreferNodes.

public void testPreferNodes() throws InterruptedException, IOException {
    TestThreadPool threadPool = null;
    ClusterService clusterService = null;
    try {
        threadPool = new TestThreadPool("testPreferNodes");
        clusterService = ClusterServiceUtils.createClusterService(threadPool);
        final String indexName = "test";
        ClusterServiceUtils.setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(indexName, true, randomInt(8)));
        final Index index = clusterService.state().metaData().index(indexName).getIndex();
        final List<ShardRouting> shards = clusterService.state().getRoutingNodes().assignedShards(new ShardId(index, 0));
        final int count = randomIntBetween(1, shards.size());
        int position = 0;
        final List<String> nodes = new ArrayList<>();
        final List<ShardRouting> expected = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            if (randomBoolean() && !shards.get(position).initializing()) {
                nodes.add(shards.get(position).currentNodeId());
                expected.add(shards.get(position));
                position++;
            } else {
                nodes.add("missing_" + i);
            }
        }
        final ShardIterator it = new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)).getShards(clusterService.state(), indexName, 0, "_prefer_nodes:" + String.join(",", nodes));
        final List<ShardRouting> all = new ArrayList<>();
        ShardRouting shard;
        while ((shard = it.nextOrNull()) != null) {
            all.add(shard);
        }
        final Set<ShardRouting> preferred = new HashSet<>();
        preferred.addAll(all.subList(0, expected.size()));
        // the preferred shards should be at the front of the list
        assertThat(preferred, containsInAnyOrder(expected.toArray()));
        // verify all the shards are there
        assertThat(all.size(), equalTo(shards.size()));
    } finally {
        IOUtils.close(clusterService);
        terminate(threadPool);
    }
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashSet(java.util.HashSet)

Example 58 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class DiskThresholdSettingsTests method testDefaults.

public void testDefaults() {
    ClusterSettings nss = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    DiskThresholdSettings diskThresholdSettings = new DiskThresholdSettings(Settings.EMPTY, nss);
    ByteSizeValue zeroBytes = ByteSizeValue.parseBytesSizeValue("0b", "test");
    assertEquals(zeroBytes, diskThresholdSettings.getFreeBytesThresholdHigh());
    assertEquals(10.0D, diskThresholdSettings.getFreeDiskThresholdHigh(), 0.0D);
    assertEquals(zeroBytes, diskThresholdSettings.getFreeBytesThresholdLow());
    assertEquals(15.0D, diskThresholdSettings.getFreeDiskThresholdLow(), 0.0D);
    assertEquals(60L, diskThresholdSettings.getRerouteInterval().seconds());
    assertTrue(diskThresholdSettings.isEnabled());
    assertTrue(diskThresholdSettings.includeRelocations());
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue)

Example 59 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class ReplicaShardAllocatorTests method testThrottleWhenAllocatingToMatchingNode.

/**
     * Tests when the node to allocate to due to matching is being throttled, we move the shard to ignored
     * to wait till throttling on it is done.
     */
public void testThrottleWhenAllocatingToMatchingNode() {
    RoutingAllocation allocation = onePrimaryOnNode1And1Replica(new AllocationDeciders(Settings.EMPTY, Arrays.asList(new TestAllocateDecision(Decision.YES), new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (node.node().equals(node2)) {
                return Decision.THROTTLE;
            }
            return Decision.YES;
        }
    })));
    testAllocator.addData(node1, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM")).addData(node2, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM"));
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodes().unassigned().ignored().size(), equalTo(1));
    assertThat(allocation.routingNodes().unassigned().ignored().get(0).shardId(), equalTo(shardId));
}
Also used : SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 60 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class SearchAsyncActionTests method testFanOutAndCollect.

public void testFanOutAndCollect() throws InterruptedException {
    SearchRequest request = new SearchRequest();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<TestSearchResponse> response = new AtomicReference<>();
    ActionListener<SearchResponse> responseListener = new ActionListener<SearchResponse>() {

        @Override
        public void onResponse(SearchResponse searchResponse) {
            response.set((TestSearchResponse) searchResponse);
        }

        @Override
        public void onFailure(Exception e) {
            logger.warn("test failed", e);
            fail(e.getMessage());
        }
    };
    DiscoveryNode primaryNode = new DiscoveryNode("node_1", buildNewFakeTransportAddress(), Version.CURRENT);
    DiscoveryNode replicaNode = new DiscoveryNode("node_2", buildNewFakeTransportAddress(), Version.CURRENT);
    Map<DiscoveryNode, Set<Long>> nodeToContextMap = new HashMap<>();
    AtomicInteger contextIdGenerator = new AtomicInteger(0);
    GroupShardsIterator shardsIter = getShardsIter("idx", randomIntBetween(1, 10), randomBoolean(), primaryNode, replicaNode);
    AtomicInteger numFreedContext = new AtomicInteger();
    SearchTransportService transportService = new SearchTransportService(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, Collections.singleton(RemoteClusterService.REMOTE_CLUSTERS_SEEDS)), null) {

        @Override
        public void sendFreeContext(Transport.Connection connection, long contextId, SearchRequest request) {
            numFreedContext.incrementAndGet();
            assertTrue(nodeToContextMap.containsKey(connection.getNode()));
            assertTrue(nodeToContextMap.get(connection.getNode()).remove(contextId));
        }
    };
    Map<String, Transport.Connection> lookup = new HashMap<>();
    lookup.put(primaryNode.getId(), new MockConnection(primaryNode));
    lookup.put(replicaNode.getId(), new MockConnection(replicaNode));
    Map<String, AliasFilter> aliasFilters = Collections.singletonMap("_na_", new AliasFilter(null, Strings.EMPTY_ARRAY));
    AbstractSearchAsyncAction asyncAction = new AbstractSearchAsyncAction<TestSearchPhaseResult>("test", logger, transportService, lookup::get, aliasFilters, Collections.emptyMap(), null, request, responseListener, shardsIter, 0, 0, null, new InitialSearchPhase.SearchPhaseResults<>(shardsIter.size())) {

        TestSearchResponse response = new TestSearchResponse();

        @Override
        protected void executePhaseOnShard(ShardIterator shardIt, ShardRouting shard, ActionListener<TestSearchPhaseResult> listener) {
            assertTrue("shard: " + shard.shardId() + " has been queried twice", response.queried.add(shard.shardId()));
            Transport.Connection connection = getConnection(shard.currentNodeId());
            TestSearchPhaseResult testSearchPhaseResult = new TestSearchPhaseResult(contextIdGenerator.incrementAndGet(), connection.getNode());
            Set<Long> ids = nodeToContextMap.computeIfAbsent(connection.getNode(), (n) -> new HashSet<>());
            ids.add(testSearchPhaseResult.id);
            if (randomBoolean()) {
                listener.onResponse(testSearchPhaseResult);
            } else {
                new Thread(() -> listener.onResponse(testSearchPhaseResult)).start();
            }
        }

        @Override
        protected SearchPhase getNextPhase(SearchPhaseResults<TestSearchPhaseResult> results, SearchPhaseContext context) {
            return new SearchPhase("test") {

                @Override
                public void run() throws IOException {
                    for (int i = 0; i < results.getNumShards(); i++) {
                        TestSearchPhaseResult result = results.results.get(i);
                        assertEquals(result.node.getId(), result.shardTarget().getNodeId());
                        sendReleaseSearchContext(result.id(), new MockConnection(result.node));
                    }
                    responseListener.onResponse(response);
                    latch.countDown();
                }
            };
        }
    };
    asyncAction.start();
    latch.await();
    assertNotNull(response.get());
    assertFalse(nodeToContextMap.isEmpty());
    assertTrue(nodeToContextMap.toString(), nodeToContextMap.containsKey(primaryNode) || nodeToContextMap.containsKey(replicaNode));
    assertEquals(shardsIter.size(), numFreedContext.get());
    if (nodeToContextMap.containsKey(primaryNode)) {
        assertTrue(nodeToContextMap.get(primaryNode).toString(), nodeToContextMap.get(primaryNode).isEmpty());
    } else {
        assertTrue(nodeToContextMap.get(replicaNode).toString(), nodeToContextMap.get(replicaNode).isEmpty());
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashSet(java.util.HashSet) Set(java.util.Set) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) HashMap(java.util.HashMap) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Transport(org.elasticsearch.transport.Transport)

Aggregations

ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)109 Settings (org.elasticsearch.common.settings.Settings)58 ClusterState (org.elasticsearch.cluster.ClusterState)50 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)25 Matchers.containsString (org.hamcrest.Matchers.containsString)25 Test (org.junit.Test)25 MetaData (org.elasticsearch.cluster.metadata.MetaData)21 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)21 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)20 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)18 DiskUsage (org.elasticsearch.cluster.DiskUsage)18 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)18 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)18 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)18 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)17 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)14 HashSet (java.util.HashSet)13 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)13