Search in sources :

Example 1 with PartitionTableView

use of com.hazelcast.internal.partition.PartitionTableView 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->partitions mapping
 * @return address->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 2 with PartitionTableView

use of com.hazelcast.internal.partition.PartitionTableView in project hazelcast by hazelcast.

the class ClusterViewListenerService method getPartitionViewMessageOrNull.

private ClientMessage getPartitionViewMessageOrNull() {
    InternalPartitionService partitionService = (InternalPartitionService) nodeEngine.getPartitionService();
    PartitionTableView partitionTableView = partitionService.createPartitionTableView();
    Map<UUID, List<Integer>> partitions = getPartitions(partitionTableView);
    if (partitions.size() == 0) {
        return null;
    }
    int version;
    long currentStamp = partitionTableView.stamp();
    long latestStamp = latestPartitionStamp.get();
    if (currentStamp != latestStamp && latestPartitionStamp.compareAndSet(latestStamp, currentStamp)) {
        partitionTableVersion.incrementAndGet();
    }
    version = partitionTableVersion.get();
    return ClientAddClusterViewListenerCodec.encodePartitionsViewEvent(version, partitions.entrySet());
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) PartitionTableView(com.hazelcast.internal.partition.PartitionTableView) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID)

Example 3 with PartitionTableView

use of com.hazelcast.internal.partition.PartitionTableView in project hazelcast by hazelcast.

the class FrozenPartitionTableTest method testPartitionTableIsFrozenDuring.

private void testPartitionTableIsFrozenDuring(final ClusterState clusterState) {
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance[] instances = factory.newInstances();
    warmUpPartitions(instances);
    changeClusterStateEventually(instances[0], clusterState);
    List<HazelcastInstance> instancesList = new ArrayList<HazelcastInstance>(asList(instances));
    Collections.shuffle(instancesList);
    final PartitionTableView partitionTable = getPartitionTable(instances[0]);
    while (instancesList.size() > 1) {
        final HazelcastInstance instanceToShutdown = instancesList.remove(0);
        instanceToShutdown.shutdown();
        for (HazelcastInstance instance : instancesList) {
            assertClusterSizeEventually(instancesList.size(), instance);
            assertEquals(partitionTable, getPartitionTable(instance));
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance) PartitionTableView(com.hazelcast.internal.partition.PartitionTableView) ArrayList(java.util.ArrayList) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory)

Example 4 with PartitionTableView

use of com.hazelcast.internal.partition.PartitionTableView in project hazelcast by hazelcast.

the class FrozenPartitionTableTest method partitionTable_isFrozen_whenMemberReJoins_duringClusterStateIs.

private void partitionTable_isFrozen_whenMemberReJoins_duringClusterStateIs(ClusterState state) {
    Config config = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
    HazelcastInstance[] instances = factory.newInstances(config, 3);
    HazelcastInstance hz1 = instances[0];
    HazelcastInstance hz2 = instances[1];
    HazelcastInstance hz3 = instances[2];
    Address hz3Address = getNode(hz3).getThisAddress();
    warmUpPartitions(instances);
    final PartitionTableView partitionTable = getPartitionTable(hz1);
    changeClusterStateEventually(hz2, state);
    final Member member3 = getClusterService(hz3).getLocalMember();
    terminateInstance(hz2);
    terminateInstance(hz3);
    hz3 = factory.newHazelcastInstance(hz3Address);
    final Member newMember3 = getClusterService(hz3).getLocalMember();
    assertClusterSizeEventually(2, hz1, hz3);
    final List<HazelcastInstance> instanceList = asList(hz1, hz3);
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() {
            for (HazelcastInstance instance : instanceList) {
                PartitionTableView newPartitionTable = getPartitionTable(instance);
                for (int i = 0; i < newPartitionTable.length(); i++) {
                    for (int j = 0; j < InternalPartition.MAX_REPLICA_COUNT; j++) {
                        PartitionReplica replica = partitionTable.getReplica(i, j);
                        PartitionReplica newReplica = newPartitionTable.getReplica(i, j);
                        if (replica == null) {
                            assertNull(newReplica);
                        } else if (replica.equals(PartitionReplica.from(member3))) {
                            assertEquals(PartitionReplica.from(newMember3), newReplica);
                        } else {
                            assertEquals(replica, newReplica);
                        }
                    }
                }
            }
        }
    }, 5);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance) Address(com.hazelcast.cluster.Address) PartitionTableView(com.hazelcast.internal.partition.PartitionTableView) PartitionReplica(com.hazelcast.internal.partition.PartitionReplica) Config(com.hazelcast.config.Config) TestHazelcastInstanceFactory.initOrCreateConfig(com.hazelcast.test.TestHazelcastInstanceFactory.initOrCreateConfig) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.cluster.Member)

Example 5 with PartitionTableView

use of com.hazelcast.internal.partition.PartitionTableView in project hazelcast by hazelcast.

the class InternalPartitionServiceImplTest method test_setInitialState_multipleTimes.

@Test(expected = IllegalStateException.class)
public void test_setInitialState_multipleTimes() {
    InternalPartition[] partitions = new InternalPartition[partitionCount];
    for (int i = 0; i < partitionCount; i++) {
        PartitionReplica[] replicas = new PartitionReplica[MAX_REPLICA_COUNT];
        replicas[0] = PartitionReplica.from(localMember);
        partitions[i] = new ReadonlyInternalPartition(replicas, i, RandomPicker.getInt(1, 10));
    }
    partitionService.setInitialState(new PartitionTableView(partitions));
    partitionService.setInitialState(new PartitionTableView(partitions));
}
Also used : PartitionReplica(com.hazelcast.internal.partition.PartitionReplica) PartitionTableView(com.hazelcast.internal.partition.PartitionTableView) InternalPartition(com.hazelcast.internal.partition.InternalPartition) ReadonlyInternalPartition(com.hazelcast.internal.partition.ReadonlyInternalPartition) ReadonlyInternalPartition(com.hazelcast.internal.partition.ReadonlyInternalPartition) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

PartitionTableView (com.hazelcast.internal.partition.PartitionTableView)12 ArrayList (java.util.ArrayList)8 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 Member (com.hazelcast.cluster.Member)6 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)6 PartitionReplica (com.hazelcast.internal.partition.PartitionReplica)5 HashMap (java.util.HashMap)5 Address (com.hazelcast.cluster.Address)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance)3 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 UUID (java.util.UUID)3 InternalPartition (com.hazelcast.internal.partition.InternalPartition)2 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)2 ReadonlyInternalPartition (com.hazelcast.internal.partition.ReadonlyInternalPartition)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)1 ClientAddClusterViewListenerCodec (com.hazelcast.client.impl.protocol.codec.ClientAddClusterViewListenerCodec)1