Search in sources :

Example 6 with DiscoveryNodeRole

use of org.elasticsearch.cluster.node.DiscoveryNodeRole 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

DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 DiscoveryNodeRole (org.elasticsearch.cluster.node.DiscoveryNodeRole)6 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 IOException (java.io.IOException)2 List (java.util.List)2 Set (java.util.Set)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 ClusterService (org.elasticsearch.cluster.service.ClusterService)2 Settings (org.elasticsearch.common.settings.Settings)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Test (org.junit.Test)2 IOUtils (io.crate.common.io.IOUtils)1 TimeValue (io.crate.common.unit.TimeValue)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyMap (java.util.Collections.emptyMap)1