Search in sources :

Example 66 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientCacheHelperTest method testEnableStatisticManagementOnNodes_sneakyThrowsException.

@Test(expected = IllegalArgumentException.class)
public void testEnableStatisticManagementOnNodes_sneakyThrowsException() {
    Member member = mock(Member.class);
    when(member.getUuid()).thenThrow(new IllegalArgumentException("expected"));
    Collection<Member> members = singletonList(member);
    when(exceptionThrowingClient.getClientClusterService().getMemberList()).thenReturn(members);
    enableStatisticManagementOnNodes(exceptionThrowingClient, CACHE_NAME, false, false);
}
Also used : Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 67 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class SqlErrorAbstractTest method newHazelcastInstance.

/**
 * Start the new Hazelcast instance.
 *
 * @param awaitAssignment whether to wait for a partition assignment to a new member
 * @return created instance
 */
protected HazelcastInstance newHazelcastInstance(boolean awaitAssignment) {
    HazelcastInstance instance = factory.newHazelcastInstance(getConfig());
    if (awaitAssignment) {
        assertTrueEventually(() -> {
            Set<UUID> memberIds = new HashSet<>();
            for (Member member : instance.getCluster().getMembers()) {
                memberIds.add(member.getUuid());
            }
            PartitionService partitionService = instance.getPartitionService();
            Set<UUID> assignedMemberIds = new HashSet<>();
            for (Partition partition : partitionService.getPartitions()) {
                Member owner = partition.getOwner();
                assertNotNull(owner);
                assignedMemberIds.add(owner.getUuid());
            }
            assertEquals(memberIds, assignedMemberIds);
        });
    }
    return instance;
}
Also used : Partition(com.hazelcast.partition.Partition) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PartitionService(com.hazelcast.partition.PartitionService) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) HashSet(java.util.HashSet)

Example 68 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClusterViewListenerService method getPartitions.

/**
 * If any partition does not have an owner, this method returns empty collection
 *
 * @param partitionTableView will be converted to address-&gt;partitions mapping
 * @return address-&gt;partitions mapping, where address is the client address of the member
 */
public Map<UUID, List<Integer>> getPartitions(PartitionTableView partitionTableView) {
    Map<UUID, List<Integer>> partitionsMap = new HashMap<>();
    int partitionCount = partitionTableView.length();
    for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
        PartitionReplica owner = partitionTableView.getReplica(partitionId, 0);
        if (owner == null || owner.uuid() == null) {
            partitionsMap.clear();
            return partitionsMap;
        }
        partitionsMap.computeIfAbsent(owner.uuid(), k -> new LinkedList<>()).add(partitionId);
    }
    return partitionsMap;
}
Also used : Address(com.hazelcast.cluster.Address) CancelledKeyException(java.nio.channels.CancelledKeyException) Member(com.hazelcast.cluster.Member) MembershipManager(com.hazelcast.internal.cluster.impl.MembershipManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) EmptyStatement(com.hazelcast.internal.util.EmptyStatement) ExecutionService(com.hazelcast.spi.impl.executionservice.ExecutionService) ArrayList(java.util.ArrayList) PartitionTableView(com.hazelcast.internal.partition.PartitionTableView) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) PartitionReplica(com.hazelcast.internal.partition.PartitionReplica) MembersView(com.hazelcast.internal.cluster.impl.MembersView) LinkedList(java.util.LinkedList) ClusterServiceImpl(com.hazelcast.internal.cluster.impl.ClusterServiceImpl) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) CoalescingDelayedTrigger(com.hazelcast.internal.util.scheduler.CoalescingDelayedTrigger) NodeEngine(com.hazelcast.spi.impl.NodeEngine) Connection(com.hazelcast.internal.nio.Connection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClientAddClusterViewListenerCodec(com.hazelcast.client.impl.protocol.codec.ClientAddClusterViewListenerCodec) UUID(java.util.UUID) TimeUnit(java.util.concurrent.TimeUnit) CLIENT(com.hazelcast.instance.EndpointQualifier.CLIENT) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PartitionReplica(com.hazelcast.internal.partition.PartitionReplica) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID) LinkedList(java.util.LinkedList)

Example 69 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class TcpClientConnectionManager method getRandomConnection.

@Override
public ClientConnection getRandomConnection() {
    // Try getting the connection from the load balancer, if smart routing is enabled
    if (isSmartRoutingEnabled) {
        Member member = loadBalancer.next();
        // Failed to get a member
        ClientConnection connection = member != null ? activeConnections.get(member.getUuid()) : null;
        if (connection != null) {
            return connection;
        }
    }
    // Otherwise iterate over connections and return the first one
    for (Map.Entry<UUID, TcpClientConnection> connectionEntry : activeConnections.entrySet()) {
        return connectionEntry.getValue();
    }
    // Failed to get a connection
    return null;
}
Also used : ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 70 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class TcpClientConnectionManager method doConnectToCandidateCluster.

private boolean doConnectToCandidateCluster(CandidateClusterContext context, boolean switchingToNextCluster) {
    Set<Address> triedAddresses = new HashSet<>();
    try {
        waitStrategy.reset();
        do {
            Set<Address> triedAddressesPerAttempt = new HashSet<>();
            List<Member> memberList = new ArrayList<>(client.getClientClusterService().getMemberList());
            if (shuffleMemberList) {
                Collections.shuffle(memberList);
            }
            // try to connect to a member in the member list first
            for (Member member : memberList) {
                checkClientActive();
                triedAddressesPerAttempt.add(member.getAddress());
                Connection connection = connect(member, o -> getOrConnectToMember((Member) o, switchingToNextCluster));
                if (connection != null) {
                    return true;
                }
            }
            // try to connect to a member given via config(explicit config/discovery mechanisms)
            for (Address address : getPossibleMemberAddresses(context.getAddressProvider())) {
                checkClientActive();
                if (!triedAddressesPerAttempt.add(address)) {
                    // if we can not add it means that it is already tried to be connected with the member list
                    continue;
                }
                Connection connection = connect(address, o -> getOrConnectToAddress((Address) o, switchingToNextCluster));
                if (connection != null) {
                    return true;
                }
            }
            triedAddresses.addAll(triedAddressesPerAttempt);
            // and the lifecycle check is missing, hence we need to repeat the same check at this point.
            if (triedAddressesPerAttempt.isEmpty()) {
                checkClientActive();
            }
        } while (waitStrategy.sleep());
    } catch (ClientNotAllowedInClusterException | InvalidConfigurationException e) {
        logger.warning("Stopped trying on the cluster: " + context.getClusterName() + " reason: " + e.getMessage());
    }
    logger.info("Unable to connect to any address from the cluster with name: " + context.getClusterName() + ". The following addresses were tried: " + triedAddresses);
    return false;
}
Also used : Address(com.hazelcast.cluster.Address) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Connection(com.hazelcast.internal.nio.Connection) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Member(com.hazelcast.cluster.Member) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Aggregations

Member (com.hazelcast.cluster.Member)429 Test (org.junit.Test)210 QuickTest (com.hazelcast.test.annotation.QuickTest)191 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)164 HazelcastInstance (com.hazelcast.core.HazelcastInstance)116 IExecutorService (com.hazelcast.core.IExecutorService)73 Address (com.hazelcast.cluster.Address)62 ArrayList (java.util.ArrayList)55 Future (java.util.concurrent.Future)53 UUID (java.util.UUID)43 Config (com.hazelcast.config.Config)42 CountDownLatch (java.util.concurrent.CountDownLatch)38 Map (java.util.Map)33 HashMap (java.util.HashMap)31 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)28 MultiExecutionCallback (com.hazelcast.core.MultiExecutionCallback)25 List (java.util.List)25 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)24 Operation (com.hazelcast.spi.impl.operationservice.Operation)24 IMap (com.hazelcast.map.IMap)23