Search in sources :

Example 11 with NodeConfigurationSource

use of org.elasticsearch.test.NodeConfigurationSource in project crate by crate.

the class InternalTestClusterTests method testDifferentRolesMaintainPathOnRestart.

public void testDifferentRolesMaintainPathOnRestart() throws Exception {
    final Path baseDir = createTempDir();
    final int numNodes = 5;
    String transportClient = getTestTransportType();
    InternalTestCluster cluster = new InternalTestCluster(randomLong(), baseDir, false, false, 0, 0, "test", new NodeConfigurationSource() {

        @Override
        public Settings nodeSettings(int nodeOrdinal) {
            return Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, getTestTransportType()).put(Node.INITIAL_STATE_TIMEOUT_SETTING.getKey(), 0).putList(DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "file").putList(SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING.getKey()).build();
        }

        @Override
        public Path nodeConfigPath(int nodeOrdinal) {
            return null;
        }

        @Override
        public Settings transportClientSettings() {
            return Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, transportClient).build();
        }
    }, 0, "", mockPlugins());
    cluster.beforeTest(random());
    List<DiscoveryNodeRole> roles = new ArrayList<>();
    for (int i = 0; i < numNodes; i++) {
        final DiscoveryNodeRole role = i == numNodes - 1 && roles.contains(DiscoveryNodeRole.MASTER_ROLE) == false ? // last node and still no master
        DiscoveryNodeRole.MASTER_ROLE : randomFrom(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE);
        roles.add(role);
    }
    cluster.setBootstrapMasterNodeIndex(randomIntBetween(0, (int) roles.stream().filter(role -> role.equals(DiscoveryNodeRole.MASTER_ROLE)).count() - 1));
    try {
        Map<DiscoveryNodeRole, Set<String>> pathsPerRole = new HashMap<>();
        for (int i = 0; i < numNodes; i++) {
            final DiscoveryNodeRole role = roles.get(i);
            final String node;
            if (role == DiscoveryNodeRole.MASTER_ROLE) {
                node = cluster.startMasterOnlyNode();
            } else if (role == DiscoveryNodeRole.DATA_ROLE) {
                node = cluster.startDataOnlyNode();
            } else {
                throw new IllegalStateException("get your story straight");
            }
            Set<String> rolePaths = pathsPerRole.computeIfAbsent(role, k -> new HashSet<>());
            for (Path path : getNodePaths(cluster, node)) {
                assertTrue(rolePaths.add(path.toString()));
            }
        }
        cluster.validateClusterFormed();
        cluster.fullRestart();
        Map<DiscoveryNodeRole, Set<String>> result = new HashMap<>();
        for (String name : cluster.getNodeNames()) {
            DiscoveryNode node = cluster.getInstance(ClusterService.class, name).localNode();
            List<String> paths = Arrays.stream(getNodePaths(cluster, name)).map(Path::toString).collect(Collectors.toList());
            if (node.isMasterEligibleNode()) {
                result.computeIfAbsent(DiscoveryNodeRole.MASTER_ROLE, k -> new HashSet<>()).addAll(paths);
            } else {
                result.computeIfAbsent(DiscoveryNodeRole.DATA_ROLE, k -> new HashSet<>()).addAll(paths);
            }
        }
        assertThat(result.size(), equalTo(pathsPerRole.size()));
        for (DiscoveryNodeRole role : result.keySet()) {
            assertThat("path are not the same for " + role, result.get(role), equalTo(pathsPerRole.get(role)));
        }
    } finally {
        cluster.close();
    }
}
Also used : Path(java.nio.file.Path) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) SettingsBasedSeedHostsProvider(org.elasticsearch.discovery.SettingsBasedSeedHostsProvider) Arrays(java.util.Arrays) NetworkModule(org.elasticsearch.common.network.NetworkModule) Environment(org.elasticsearch.env.Environment) ClusterService(org.elasticsearch.cluster.service.ClusterService) Matchers.not(org.hamcrest.Matchers.not) HashMap(java.util.HashMap) Random(java.util.Random) ElasticsearchAssertions.assertFileNotExists(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFileNotExists) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) DiscoveryModule(org.elasticsearch.discovery.DiscoveryModule) Map(java.util.Map) Node(org.elasticsearch.node.Node) ESTestCase(org.elasticsearch.test.ESTestCase) Path(java.nio.file.Path) Iterator(java.util.Iterator) Files(java.nio.file.Files) IOUtils(io.crate.common.io.IOUtils) Client(org.elasticsearch.client.Client) Collection(java.util.Collection) Plugin(org.elasticsearch.plugins.Plugin) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MockHttpTransport(org.elasticsearch.test.MockHttpTransport) List(java.util.List) ElasticsearchAssertions.assertFileExists(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFileExists) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Matchers.equalTo(org.hamcrest.Matchers.equalTo) NodeConfigurationSource(org.elasticsearch.test.NodeConfigurationSource) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) DISCOVERY_SEED_PROVIDERS_SETTING(org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING) DiscoveryNodeRole(org.elasticsearch.cluster.node.DiscoveryNodeRole) Collections(java.util.Collections) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashSet(java.util.HashSet) Set(java.util.Set) DiscoveryNodeRole(org.elasticsearch.cluster.node.DiscoveryNodeRole) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) NodeConfigurationSource(org.elasticsearch.test.NodeConfigurationSource) ClusterService(org.elasticsearch.cluster.service.ClusterService) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Aggregations

InternalTestCluster (org.elasticsearch.test.InternalTestCluster)11 NodeConfigurationSource (org.elasticsearch.test.NodeConfigurationSource)11 Path (java.nio.file.Path)9 Settings (org.elasticsearch.common.settings.Settings)6 Plugin (org.elasticsearch.plugins.Plugin)5 MockTcpTransportPlugin (org.elasticsearch.transport.MockTcpTransportPlugin)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 Client (org.elasticsearch.client.Client)4 DiscoverySettings (org.elasticsearch.discovery.DiscoverySettings)3 TransportSettings (org.elasticsearch.transport.TransportSettings)3 Files (java.nio.file.Files)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2