Java Code Examples for org.elasticsearch.common.util.set.Sets#newHashSet()

The following examples show how to use org.elasticsearch.common.util.set.Sets#newHashSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoordinatorTests.java    From crate with Apache License 2.0 6 votes vote down vote up
public void testCannotSetInitialConfigurationWithoutQuorum() {
    final Cluster cluster = new Cluster(randomIntBetween(1, 5));
    final Coordinator coordinator = cluster.getAnyNode().coordinator;
    final VotingConfiguration unknownNodeConfiguration = new VotingConfiguration(
        Sets.newHashSet(coordinator.getLocalNode().getId(), "unknown-node"));
    final String exceptionMessage = expectThrows(CoordinationStateRejectedException.class,
        () -> coordinator.setInitialConfiguration(unknownNodeConfiguration)).getMessage();
    assertThat(exceptionMessage,
        startsWith("not enough nodes discovered to form a quorum in the initial configuration [knownNodes=["));
    assertThat(exceptionMessage, containsString("unknown-node"));
    assertThat(exceptionMessage, containsString(coordinator.getLocalNode().toString()));

    // This is VERY BAD: setting a _different_ initial configuration. Yet it works if the first attempt will never be a quorum.
    assertTrue(coordinator.setInitialConfiguration(new VotingConfiguration(Collections.singleton(coordinator.getLocalNode().getId()))));
    cluster.stabilise();
}
 
Example 2
Source File: PolygonBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Validates only 1 vertex is tangential (shared) between the interior and exterior of a polygon
 */
protected void validateHole(LineStringBuilder shell, LineStringBuilder hole) {
    HashSet<Coordinate> exterior = Sets.newHashSet(shell.coordinates);
    HashSet<Coordinate> interior = Sets.newHashSet(hole.coordinates);
    exterior.retainAll(interior);
    if (exterior.size() >= 2) {
        throw new InvalidShapeException("Invalid polygon, interior cannot share more than one point with the exterior");
    }
}
 
Example 3
Source File: UserPrivilegesTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatchPrivilegeComplexSetIncludingDeny() throws Exception {
    Collection<Privilege> privileges = Sets.newHashSet(
        new Privilege(Privilege.State.GRANT, Privilege.Type.DQL, Privilege.Clazz.CLUSTER, null, "crate"),
        new Privilege(Privilege.State.DENY, Privilege.Type.DQL, Privilege.Clazz.SCHEMA, "doc", "crate"),
        new Privilege(Privilege.State.GRANT, Privilege.Type.DQL, Privilege.Clazz.TABLE, "doc.t1", "crate")
    );
    UserPrivileges userPrivileges = new UserPrivileges(privileges);
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.TABLE, "doc.t1", "doc"), is(true));
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.TABLE, "doc.t2", "doc"), is(false));
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.SCHEMA, "my_schema", "doc"), is(true));
}
 
Example 4
Source File: UserPrivilegesTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatchPrivilegeDenyResultsInNoMatch() throws Exception {
    Collection<Privilege> privileges = Sets.newHashSet(
        new Privilege(Privilege.State.DENY, Privilege.Type.DQL, Privilege.Clazz.CLUSTER, null, "crate")
    );
    UserPrivileges userPrivileges = new UserPrivileges(privileges);
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.CLUSTER, null, "doc"), is(false));
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.SCHEMA, "doc", "doc"), is(false));
    assertThat(userPrivileges.matchPrivilege(Privilege.Type.DQL, Privilege.Clazz.TABLE, "doc.t1", "doc"), is(false));
    assertThat(userPrivileges.matchPrivilegeOfAnyType(Privilege.Clazz.CLUSTER, null), is(false));
    assertThat(userPrivileges.matchPrivilegeOfAnyType(Privilege.Clazz.SCHEMA, "doc"), is(false));
    assertThat(userPrivileges.matchPrivilegeOfAnyType(Privilege.Clazz.TABLE, "doc.t1"), is(false));
}
 
Example 5
Source File: CrateDummyClusterServiceUnitTest.java    From crate with Apache License 2.0 5 votes vote down vote up
protected ClusterService createClusterService(Collection<Setting<?>> additionalClusterSettings, Version version) {
    Set<Setting<?>> clusterSettingsSet = Sets.newHashSet(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    clusterSettingsSet.addAll(additionalClusterSettings);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, clusterSettingsSet);
    ClusterService clusterService = new ClusterService(
        Settings.builder()
            .put("cluster.name", "ClusterServiceTests")
            .put(Node.NODE_NAME_SETTING.getKey(), NODE_NAME)
            .build(),
        clusterSettings,
        THREAD_POOL
    );
    clusterService.setNodeConnectionsService(createNoOpNodeConnectionsService());
    DiscoveryNode discoveryNode = new DiscoveryNode(
        NODE_NAME,
        NODE_ID,
        buildNewFakeTransportAddress(),
        Collections.emptyMap(),
        new HashSet<>(Arrays.asList(DiscoveryNode.Role.values())),
        version
    );
    DiscoveryNodes nodes = DiscoveryNodes.builder()
        .add(discoveryNode)
        .localNodeId(NODE_ID)
        .masterNodeId(NODE_ID)
        .build();
    ClusterState clusterState = ClusterState.builder(new ClusterName(this.getClass().getSimpleName()))
        .nodes(nodes).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();

    ClusterApplierService clusterApplierService = clusterService.getClusterApplierService();
    clusterApplierService.setInitialState(clusterState);

    MasterService masterService = clusterService.getMasterService();
    masterService.setClusterStatePublisher(createClusterStatePublisher(clusterApplierService));
    masterService.setClusterStateSupplier(clusterApplierService::state);

    clusterService.start();
    return clusterService;
}
 
Example 6
Source File: PublicationTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testPublishingToMastersFirst() {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
    initializeCluster(singleNodeConfig);

    DiscoveryNodes.Builder discoNodesBuilder = DiscoveryNodes.builder();
    randomNodes(10).forEach(dn -> discoNodesBuilder.add(dn));
    DiscoveryNodes discoveryNodes = discoNodesBuilder.add(n1).localNodeId(n1.getId()).build();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L,
        discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), null, Collections.emptySet());

    List<DiscoveryNode> publicationTargets = new ArrayList<>(publication.pendingPublications.keySet());
    List<DiscoveryNode> sortedPublicationTargets = new ArrayList<>(publicationTargets);
    Collections.sort(sortedPublicationTargets, Comparator.comparing(n -> n.isMasterNode() == false));
    assertEquals(sortedPublicationTargets, publicationTargets);
}
 
Example 7
Source File: PublicationTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testClusterStatePublishingFailsOrTimesOutBeforeCommit() throws InterruptedException {
    VotingConfiguration config = new VotingConfiguration(Sets.newHashSet(n1.getId(), n2.getId()));
    initializeCluster(config);

    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L,
        discoveryNodes, config, config, 42L), ackListener, Collections.emptySet());

    boolean timeOut = randomBoolean();
    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2)) {
            if (timeOut) {
                publication.cancel("timed out");
            } else {
                e.getValue().onFailure(new TransportException(new Exception("dummy failure")));
            }
            assertTrue(publication.completed);
            assertFalse(publication.committed);
        } else if (randomBoolean()) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(
                publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });

    assertThat(publication.pendingCommits.keySet(), equalTo(Collections.emptySet()));
    assertNull(publication.applyCommit);
    assertTrue(publication.completed);
    assertFalse(publication.committed);

    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(3));
    assertThat(errors.stream().map(Tuple::v1).collect(Collectors.toList()), containsInAnyOrder(n1, n2, n3));
    errors.stream().forEach(tuple ->
        assertThat(tuple.v2().getMessage(), containsString(timeOut ? "timed out" :
            tuple.v1().equals(n2) ? "dummy failure" : "non-failed nodes do not form a quorum")));
}
 
Example 8
Source File: CoordinatorTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testCannotSetInitialConfigurationWithoutLocalNode() {
    final Cluster cluster = new Cluster(randomIntBetween(1, 5));
    final Coordinator coordinator = cluster.getAnyNode().coordinator;
    final VotingConfiguration unknownNodeConfiguration = new VotingConfiguration(Sets.newHashSet("unknown-node"));
    final String exceptionMessage = expectThrows(CoordinationStateRejectedException.class,
        () -> coordinator.setInitialConfiguration(unknownNodeConfiguration)).getMessage();
    assertThat(exceptionMessage,
        equalTo("local node is not part of initial configuration"));
}
 
Example 9
Source File: PublicationTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testClusterStatePublishingWithFaultyNodeAfterCommit() throws InterruptedException {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
    initializeCluster(singleNodeConfig);

    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();

    boolean publicationDidNotMakeItToNode2 = randomBoolean();
    AtomicInteger remainingActions = new AtomicInteger(publicationDidNotMakeItToNode2 ? 2 : 3);
    int injectFaultAt = randomInt(remainingActions.get() - 1);
    logger.info("Injecting fault at: {}, publicationDidNotMakeItToNode2: {}", injectFaultAt, publicationDidNotMakeItToNode2);

    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L,
        discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), ackListener, Collections.emptySet());

    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2) == false || publicationDidNotMakeItToNode2 == false) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(
                publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });

    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (e.getKey().equals(n2)) {
            // we must fail node before committing for the node, otherwise failing the node is ignored
            publication.onFaultyNode(n2);
        }
        if (remainingActions.decrementAndGet() == injectFaultAt) {
            publication.onFaultyNode(n2);
        }
        if (e.getKey().equals(n2) == false || randomBoolean()) {
            nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
            e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
        }
    });

    // we need to complete publication by failing the node
    if (publicationDidNotMakeItToNode2 && remainingActions.get() > injectFaultAt) {
        publication.onFaultyNode(n2);
    }

    assertTrue(publication.completed);
    assertTrue(publication.committed);

    publication.onFaultyNode(randomFrom(n1, n3)); // has no influence

    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(1));
    assertThat(errors.get(0).v1(), equalTo(n2));
    assertThat(errors.get(0).v2().getMessage(), containsString("faulty node"));
}
 
Example 10
Source File: PublicationTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testClusterStatePublishingWithFaultyNodeBeforeCommit() throws InterruptedException {
    VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
    initializeCluster(singleNodeConfig);

    AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(n1).add(n2).add(n3).localNodeId(n1.getId()).build();

    AtomicInteger remainingActions = new AtomicInteger(4); // number of publish actions + initial faulty nodes injection
    int injectFaultAt = randomInt(remainingActions.get() - 1);
    logger.info("Injecting fault at: {}", injectFaultAt);

    Set<DiscoveryNode> initialFaultyNodes = remainingActions.decrementAndGet() == injectFaultAt ?
        Collections.singleton(n2) : Collections.emptySet();
    MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L,
        discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), ackListener, initialFaultyNodes);

    publication.pendingPublications.entrySet().stream().collect(shuffle()).forEach(e -> {
        if (remainingActions.decrementAndGet() == injectFaultAt) {
            publication.onFaultyNode(n2);
        }
        if (e.getKey().equals(n2) == false || randomBoolean()) {
            PublishResponse publishResponse = nodeResolver.apply(e.getKey()).coordinationState.handlePublishRequest(
                publication.publishRequest);
            e.getValue().onResponse(new PublishWithJoinResponse(publishResponse, Optional.empty()));
        }
    });

    publication.pendingCommits.entrySet().stream().collect(shuffle()).forEach(e -> {
        nodeResolver.apply(e.getKey()).coordinationState.handleCommit(publication.applyCommit);
        e.getValue().onResponse(TransportResponse.Empty.INSTANCE);
    });

    assertTrue(publication.completed);
    assertTrue(publication.committed);

    publication.onFaultyNode(randomFrom(n1, n3)); // has no influence

    List<Tuple<DiscoveryNode, Throwable>> errors = ackListener.awaitErrors(0L, TimeUnit.SECONDS);
    assertThat(errors.size(), equalTo(1));
    assertThat(errors.get(0).v1(), equalTo(n2));
    assertThat(errors.get(0).v2().getMessage(), containsString("faulty node"));
}
 
Example 11
Source File: ReconfiguratorTests.java    From crate with Apache License 2.0 4 votes vote down vote up
private VotingConfiguration conf(String... nodes) {
    return new VotingConfiguration(Sets.newHashSet(nodes));
}
 
Example 12
Source File: CoordinationMetaDataTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testVotingConfiguration() {
    VotingConfiguration config0 = new VotingConfiguration(Sets.newHashSet());
    assertThat(config0, equalTo(VotingConfiguration.EMPTY_CONFIG));
    assertThat(config0.getNodeIds(), equalTo(Sets.newHashSet()));
    assertThat(config0.isEmpty(), equalTo(true));
    assertThat(config0.hasQuorum(Sets.newHashSet()), equalTo(false));
    assertThat(config0.hasQuorum(Sets.newHashSet("id1")), equalTo(false));

    VotingConfiguration config1 = new VotingConfiguration(Sets.newHashSet("id1"));
    assertThat(config1.getNodeIds(), equalTo(Sets.newHashSet("id1")));
    assertThat(config1.isEmpty(), equalTo(false));
    assertThat(config1.hasQuorum(Sets.newHashSet("id1")), equalTo(true));
    assertThat(config1.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
    assertThat(config1.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
    assertThat(config1.hasQuorum(Sets.newHashSet()), equalTo(false));

    VotingConfiguration config2 = new VotingConfiguration(Sets.newHashSet("id1", "id2"));
    assertThat(config2.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2")));
    assertThat(config2.isEmpty(), equalTo(false));
    assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
    assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true));
    assertThat(config2.hasQuorum(Sets.newHashSet("id1")), equalTo(false));
    assertThat(config2.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
    assertThat(config2.hasQuorum(Sets.newHashSet("id3")), equalTo(false));
    assertThat(config2.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(false));
    assertThat(config2.hasQuorum(Sets.newHashSet()), equalTo(false));

    VotingConfiguration config3 = new VotingConfiguration(Sets.newHashSet("id1", "id2", "id3"));
    assertThat(config3.getNodeIds(), equalTo(Sets.newHashSet("id1", "id2", "id3")));
    assertThat(config3.isEmpty(), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2")), equalTo(true));
    assertThat(config3.hasQuorum(Sets.newHashSet("id2", "id3")), equalTo(true));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id3")), equalTo(true));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id3")), equalTo(true));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id2", "id4")), equalTo(true));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1")), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet("id2")), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet("id3")), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4")), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet("id1", "id4", "id5")), equalTo(false));
    assertThat(config3.hasQuorum(Sets.newHashSet()), equalTo(false));
}
 
Example 13
Source File: CoordinationMetaDataTests.java    From crate with Apache License 2.0 4 votes vote down vote up
private static VotingConfiguration randomVotingConfig() {
    return new VotingConfiguration(Sets.newHashSet(generateRandomStringArray(randomInt(10), 20, false)));
}
 
Example 14
Source File: InternalTestCluster.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a predicate that only accepts settings of nodes with one of the given names.
 */
public static Predicate<Settings> nameFilter(String... nodeNames) {
    final Set<String> nodes = Sets.newHashSet(nodeNames);
    return settings -> nodes.contains(settings.get("node.name"));
}
 
Example 15
Source File: Validators.java    From crate with Apache License 2.0 4 votes vote down vote up
public static Setting.Validator<String> stringValidator(String key, String... allowedValues) {
    if (allowedValues.length == 0) {
        return new StrictStringValidator(key);
    }
    return new StringValidatorAllowedValuesOnly(key, Sets.newHashSet(allowedValues));
}
 
Example 16
Source File: ExtractedEntities.java    From elasticsearch-ingest-opennlp with Apache License 2.0 4 votes vote down vote up
ExtractedEntities(String[] tokens, Span[] spans) {
    this.tokens = tokens;
    this.spans = spans;
    this.entities = Sets.newHashSet(Span.spansToStrings(spans, tokens));
}
 
Example 17
Source File: JobsLogsTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected Set<Setting<?>> additionalClusterSettings() {
    SQLPlugin sqlPlugin = new SQLPlugin(Settings.EMPTY);
    return Sets.newHashSet(sqlPlugin.getSettings());
}
 
Example 18
Source File: PrimaryAllocationIT.java    From crate with Apache License 2.0 4 votes vote down vote up
private Settings createStaleReplicaScenario(String master, String schema, String indexName) throws Exception {
    execute("insert into t values ('value1')");
    refresh();

    ClusterState state = client().admin().cluster().prepareState().all().get().getState();
    List<ShardRouting> shards = state.routingTable().allShards(indexName);
    assertThat(shards.size(), equalTo(2));

    final String primaryNode;
    final String replicaNode;
    if (shards.get(0).primary()) {
        primaryNode = state.getRoutingNodes().node(shards.get(0).currentNodeId()).node().getName();
        replicaNode = state.getRoutingNodes().node(shards.get(1).currentNodeId()).node().getName();
    } else {
        primaryNode = state.getRoutingNodes().node(shards.get(1).currentNodeId()).node().getName();
        replicaNode = state.getRoutingNodes().node(shards.get(0).currentNodeId()).node().getName();
    }

    NetworkDisruption partition = new NetworkDisruption(
        new TwoPartitions(Sets.newHashSet(master, replicaNode), Collections.singleton(primaryNode)),
        new NetworkDisconnect());
    internalCluster().setDisruptionScheme(partition);
    logger.info("--> partitioning node with primary shard from rest of cluster");
    partition.startDisrupting();

    ensureStableCluster(2, master);

    logger.info("--> index a document into previous replica shard (that is now primary)");
    systemExecute("insert into t values ('value2')", schema, replicaNode);

    logger.info("--> shut down node that has new acknowledged document");
    final Settings inSyncDataPathSettings = internalCluster().dataPathSettings(replicaNode);
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode));

    ensureStableCluster(1, master);

    partition.stopDisrupting();

    logger.info("--> waiting for node with old primary shard to rejoin the cluster");
    ensureStableCluster(2, master);

    logger.info("--> check that old primary shard does not get promoted to primary again");
    // kick reroute and wait for all shard states to be fetched
    client(master).admin().cluster().prepareReroute().get();
    assertBusy(() -> assertThat(internalCluster().getInstance(GatewayAllocator.class, master).getNumberOfInFlightFetch(),
        equalTo(0)));
    // kick reroute a second time and check that all shards are unassigned
    assertThat(client(master).admin().cluster().prepareReroute().get().getState().getRoutingNodes().unassigned().size(),
        equalTo(2));
    return inSyncDataPathSettings;
}
 
Example 19
Source File: DecommissionAllocationDeciderTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected Set<Setting<?>> additionalClusterSettings() {
    SQLPlugin sqlPlugin = new SQLPlugin(Settings.EMPTY);
    return Sets.newHashSet(sqlPlugin.getSettings());
}
 
Example 20
Source File: DecommissioningServiceTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected Set<Setting<?>> additionalClusterSettings() {
    SQLPlugin sqlPlugin = new SQLPlugin(Settings.EMPTY);
    return Sets.newHashSet(sqlPlugin.getSettings());
}