Search in sources :

Example 1 with ClusterSettings

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

the class AbstractSimpleTransportTestCase method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    threadPool = new TestThreadPool(getClass().getName());
    clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    // this one supports dynamic tracer updates
    serviceA = buildService("TS_A", version0, clusterSettings);
    nodeA = serviceA.getLocalNode();
    // this one doesn't support dynamic tracer updates
    serviceB = buildService("TS_B", version1, null);
    nodeB = serviceB.getLocalNode();
    // wait till all nodes are properly connected and the event has been sent, so tests in this class
    // will not get this callback called on the connections done in this setup
    final CountDownLatch latch = new CountDownLatch(2);
    TransportConnectionListener waitForConnection = new TransportConnectionListener() {

        @Override
        public void onNodeConnected(DiscoveryNode node) {
            latch.countDown();
        }

        @Override
        public void onNodeDisconnected(DiscoveryNode node) {
            fail("disconnect should not be called " + node);
        }
    };
    serviceA.addConnectionListener(waitForConnection);
    serviceB.addConnectionListener(waitForConnection);
    int numHandshakes = 1;
    serviceA.connectToNode(nodeB);
    serviceB.connectToNode(nodeA);
    assertNumHandshakes(numHandshakes, serviceA.getOriginalTransport());
    assertNumHandshakes(numHandshakes, serviceB.getOriginalTransport());
    assertThat("failed to wait for all nodes to connect", latch.await(5, TimeUnit.SECONDS), equalTo(true));
    serviceA.removeConnectionListener(waitForConnection);
    serviceB.removeConnectionListener(waitForConnection);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) Before(org.junit.Before)

Example 2 with ClusterSettings

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

the class SettingsUpdaterTests method testUpdateSetting.

public void testUpdateSetting() {
    AtomicReference<Float> index = new AtomicReference<>();
    AtomicReference<Float> shard = new AtomicReference<>();
    ClusterState.Builder builder = ClusterState.builder(new ClusterName("foo"));
    ClusterSettings settingsService = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    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(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.5).build(), Settings.builder().put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.4).build());
    assertNotSame(clusterState, build);
    assertEquals(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.get(clusterState.metaData().persistentSettings()), 0.4, 0.1);
    assertEquals(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.get(clusterState.metaData().persistentSettings()), 2.5, 0.1);
    assertEquals(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.get(clusterState.metaData().transientSettings()), 0.5, 0.1);
    assertEquals(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.get(clusterState.metaData().transientSettings()), 4.5, 0.1);
    clusterState = updater.updateSettings(clusterState, Settings.builder().putNull("cluster.routing.*").build(), Settings.EMPTY);
    assertEquals(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.get(clusterState.metaData().persistentSettings()), 0.4, 0.1);
    assertEquals(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.get(clusterState.metaData().persistentSettings()), 2.5, 0.1);
    assertFalse(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.exists(clusterState.metaData().transientSettings()));
    assertFalse(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.exists(clusterState.metaData().transientSettings()));
    clusterState = updater.updateSettings(clusterState, Settings.EMPTY, Settings.builder().putNull("cluster.routing.*").put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 10.0).build());
    assertEquals(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.get(clusterState.metaData().persistentSettings()), 10.0, 0.1);
    assertFalse(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.exists(clusterState.metaData().persistentSettings()));
    assertFalse(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.exists(clusterState.metaData().transientSettings()));
    assertFalse(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.exists(clusterState.metaData().transientSettings()));
    assertNull("updater only does a dryRun", index.get());
    assertNull("updater only does a dryRun", shard.get());
}
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 3 with ClusterSettings

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

the class BalanceConfigurationTests method testNoRebalanceOnPrimaryOverload.

public void testNoRebalanceOnPrimaryOverload() {
    Settings.Builder settings = Settings.builder();
    AllocationService strategy = new AllocationService(settings.build(), randomAllocationDeciders(settings.build(), new ClusterSettings(Settings.Builder.EMPTY_SETTINGS, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), random()), new TestGatewayAllocator(), new ShardsAllocator() {

        /*
             *  // this allocator tries to rebuild this scenario where a rebalance is
             *  // triggered solely by the primary overload on node [1] where a shard
             *  // is rebalanced to node 0
                routing_nodes:
                -----node_id[0][V]
                --------[test][0], node[0], [R], s[STARTED]
                --------[test][4], node[0], [R], s[STARTED]
                -----node_id[1][V]
                --------[test][0], node[1], [P], s[STARTED]
                --------[test][1], node[1], [P], s[STARTED]
                --------[test][3], node[1], [R], s[STARTED]
                -----node_id[2][V]
                --------[test][1], node[2], [R], s[STARTED]
                --------[test][2], node[2], [R], s[STARTED]
                --------[test][4], node[2], [P], s[STARTED]
                -----node_id[3][V]
                --------[test][2], node[3], [P], s[STARTED]
                --------[test][3], node[3], [P], s[STARTED]
                ---- unassigned
            */
        public void allocate(RoutingAllocation allocation) {
            RoutingNodes.UnassignedShards unassigned = allocation.routingNodes().unassigned();
            ShardRouting[] drain = unassigned.drain();
            // we have to allocate primaries first
            ArrayUtil.timSort(drain, (a, b) -> {
                return a.primary() ? -1 : 1;
            });
            for (ShardRouting sr : drain) {
                switch(sr.id()) {
                    case 0:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node0", null, -1, allocation.changes());
                        }
                        break;
                    case 1:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        }
                        break;
                    case 2:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node3", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        }
                        break;
                    case 3:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node3", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        }
                        break;
                    case 4:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node0", null, -1, allocation.changes());
                        }
                        break;
                }
            }
        }

        @Override
        public ShardAllocationDecision decideShardAllocation(ShardRouting shard, RoutingAllocation allocation) {
            throw new UnsupportedOperationException("explain not supported");
        }
    }, EmptyClusterInfoService.INSTANCE);
    MetaData.Builder metaDataBuilder = MetaData.builder();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    IndexMetaData.Builder indexMeta = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1);
    metaDataBuilder = metaDataBuilder.put(indexMeta);
    MetaData metaData = metaDataBuilder.build();
    for (ObjectCursor<IndexMetaData> cursor : metaData.indices().values()) {
        routingTableBuilder.addAsNew(cursor.value);
    }
    RoutingTable routingTable = routingTableBuilder.build();
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
    for (int i = 0; i < 4; i++) {
        DiscoveryNode node = newNode("node" + i);
        nodes.add(node);
    }
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(routingTable).build();
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.INITIALIZING));
        }
    }
    strategy = createAllocationService(settings.build(), new NoopGatewayAllocator());
    logger.info("use the new allocator and check if it moves shards");
    routingNodes = clusterState.getRoutingNodes();
    routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
    logger.info("start the replica shards");
    routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
    logger.info("rebalancing");
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ESAllocationTestCase(org.elasticsearch.cluster.ESAllocationTestCase) INITIALIZING(org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) STARTED(org.elasticsearch.cluster.routing.ShardRoutingState.STARTED) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) EmptyClusterInfoService(org.elasticsearch.cluster.EmptyClusterInfoService) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) Loggers(org.elasticsearch.common.logging.Loggers) ArrayUtil(org.apache.lucene.util.ArrayUtil) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) Matchers(org.hamcrest.Matchers) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 4 with ClusterSettings

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

the class BalanceConfigurationTests method testPersistedSettings.

public void testPersistedSettings() {
    Settings.Builder settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.2);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.3);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 2.0);
    ClusterSettings service = new ClusterSettings(Settings.builder().build(), ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings.build(), service);
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
    settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.2);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.3);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 2.0);
    settings.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString());
    service.applySettings(settings.build());
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
    settings = Settings.builder();
    settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.5);
    settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.1);
    settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 3.0);
    service.applySettings(settings.build());
    assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.5f));
    assertThat(allocator.getShardBalance(), Matchers.equalTo(0.1f));
    assertThat(allocator.getThreshold(), Matchers.equalTo(3.0f));
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 5 with ClusterSettings

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

the class OperationRoutingTests method testThatOnlyNodesSupportNodeIds.

public void testThatOnlyNodesSupportNodeIds() throws InterruptedException, IOException {
    TestThreadPool threadPool = null;
    ClusterService clusterService = null;
    try {
        threadPool = new TestThreadPool("testThatOnlyNodesSupportNodeIds");
        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);
            }
        }
        if (expected.size() > 0) {
            final ShardIterator it = new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)).getShards(clusterService.state(), indexName, 0, "_only_nodes:" + String.join(",", nodes));
            final List<ShardRouting> only = new ArrayList<>();
            ShardRouting shard;
            while ((shard = it.nextOrNull()) != null) {
                only.add(shard);
            }
            assertThat(new HashSet<>(only), equalTo(new HashSet<>(expected)));
        } else {
            final ClusterService cs = clusterService;
            final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)).getShards(cs.state(), indexName, 0, "_only_nodes:" + String.join(",", nodes)));
            if (nodes.size() == 1) {
                assertThat(e, hasToString(containsString("no data nodes with criteria [" + String.join(",", nodes) + "] found for shard: [test][0]")));
            } else {
                assertThat(e, hasToString(containsString("no data nodes with criterion [" + String.join(",", nodes) + "] found for shard: [test][0]")));
            }
        }
    } 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)

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