Search in sources :

Example 21 with Partition

use of com.hazelcast.partition.Partition in project hazelcast by hazelcast.

the class QueryUtils method createPartitionMap.

/**
 * Create map from member ID to owned partitions.
 *
 * @param nodeEngine node engine
 * @param localMemberVersion version of the local member. If any of partition owners have a different version, an exception
 *                           is thrown. The check is ignored if passed version is {@code null}
 * @param failOnUnassignedPartition whether the call should fail in case an unassigned partition is found; when set to
 *                                  {@code false} the missing partitions will not be included in the result
 * @return partition mapping
 */
public static Map<UUID, PartitionIdSet> createPartitionMap(NodeEngine nodeEngine, @Nullable MemberVersion localMemberVersion, boolean failOnUnassignedPartition) {
    Collection<Partition> parts = nodeEngine.getHazelcastInstance().getPartitionService().getPartitions();
    int partCnt = parts.size();
    Map<UUID, PartitionIdSet> partMap = new LinkedHashMap<>();
    for (Partition part : parts) {
        Member owner = part.getOwner();
        if (owner == null) {
            if (failOnUnassignedPartition) {
                throw QueryException.error(SqlErrorCode.PARTITION_DISTRIBUTION, "Partition is not assigned to any member: " + part.getPartitionId());
            } else {
                continue;
            }
        }
        if (localMemberVersion != null) {
            if (!localMemberVersion.equals(owner.getVersion())) {
                UUID localMemberId = nodeEngine.getLocalMember().getUuid();
                throw QueryException.error("Cannot execute SQL query when members have different versions " + "(make sure that all members have the same version) {localMemberId=" + localMemberId + ", localMemberVersion=" + localMemberVersion + ", remoteMemberId=" + owner.getUuid() + ", remoteMemberVersion=" + owner.getVersion() + "}");
            }
        }
        partMap.computeIfAbsent(owner.getUuid(), (key) -> new PartitionIdSet(partCnt)).add(part.getPartitionId());
    }
    return partMap;
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) Member(com.hazelcast.cluster.Member) Partition(com.hazelcast.partition.Partition) Collection(java.util.Collection) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) UUID(java.util.UUID) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TableResolver(com.hazelcast.sql.impl.schema.TableResolver) List(java.util.List) MemberVersion(com.hazelcast.version.MemberVersion) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PartitionIdSet(com.hazelcast.internal.util.collection.PartitionIdSet) Version(com.hazelcast.version.Version) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) Partition(com.hazelcast.partition.Partition) PartitionIdSet(com.hazelcast.internal.util.collection.PartitionIdSet) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) LinkedHashMap(java.util.LinkedHashMap)

Example 22 with Partition

use of com.hazelcast.partition.Partition in project hazelcast by hazelcast.

the class PartitionServiceProxyTest method testGetPartition.

@Test
public void testGetPartition() {
    String key = "Key";
    PartitionService clientPartitionService = client.getPartitionService();
    Partition clientPartition = clientPartitionService.getPartition(key);
    PartitionService serverPartitionService = server.getPartitionService();
    Partition serverPartition = serverPartitionService.getPartition(key);
    assertEquals(clientPartition.getPartitionId(), serverPartition.getPartitionId());
}
Also used : Partition(com.hazelcast.partition.Partition) PartitionService(com.hazelcast.partition.PartitionService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 23 with Partition

use of com.hazelcast.partition.Partition in project hazelcast by hazelcast.

the class PartitionServiceProxyTest method testGetPartitions.

@Test
public void testGetPartitions() {
    PartitionService clientPartitionService = client.getPartitionService();
    Set<Partition> clientPartitions = clientPartitionService.getPartitions();
    PartitionService serverPartitionService = server.getPartitionService();
    Set<Partition> serverPartitions = serverPartitionService.getPartitions();
    assertEquals(clientPartitions.size(), serverPartitions.size());
}
Also used : Partition(com.hazelcast.partition.Partition) PartitionService(com.hazelcast.partition.PartitionService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with Partition

use of com.hazelcast.partition.Partition in project hazelcast by hazelcast.

the class ConsoleApp method handlePartitions.

protected void handlePartitions(String[] args) {
    Set<Partition> partitions = hazelcast.getPartitionService().getPartitions();
    Map<Member, Integer> partitionCounts = new HashMap<>();
    for (Partition partition : partitions) {
        Member owner = partition.getOwner();
        if (owner != null) {
            Integer count = partitionCounts.get(owner);
            int newCount = 1;
            if (count != null) {
                newCount = count + 1;
            }
            partitionCounts.put(owner, newCount);
        }
        println(partition);
    }
    Set<Map.Entry<Member, Integer>> entries = partitionCounts.entrySet();
    for (Map.Entry<Member, Integer> entry : entries) {
        println(entry.getKey() + ": " + entry.getValue());
    }
}
Also used : Partition(com.hazelcast.partition.Partition) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Member(com.hazelcast.cluster.Member) Map(java.util.Map) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) MultiMap(com.hazelcast.multimap.MultiMap) IMap(com.hazelcast.map.IMap)

Example 25 with Partition

use of com.hazelcast.partition.Partition in project hazelcast by hazelcast.

the class RingbufferStoreFailureConsistencyTest method getPrimaryInstance.

private HazelcastInstance getPrimaryInstance(HazelcastInstance instance, HazelcastInstance instance2) {
    Partition primaryPartition = instance.getPartitionService().getPartition(RINGBUFFER_NAME);
    UUID primaryInstanceUuid = primaryPartition.getOwner().getUuid();
    UUID instanceOneUuid = instance.getCluster().getLocalMember().getUuid();
    return primaryInstanceUuid.equals(instanceOneUuid) ? instance : instance2;
}
Also used : Partition(com.hazelcast.partition.Partition) UUID(java.util.UUID)

Aggregations

Partition (com.hazelcast.partition.Partition)28 PartitionService (com.hazelcast.partition.PartitionService)13 Member (com.hazelcast.cluster.Member)12 Test (org.junit.Test)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 IPartition (com.hazelcast.internal.partition.IPartition)5 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)5 UUID (java.util.UUID)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Cluster (com.hazelcast.cluster.Cluster)3 PartitionIdSet (com.hazelcast.internal.util.collection.PartitionIdSet)3 Entry (java.util.Map.Entry)3 Config (com.hazelcast.config.Config)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Assert.assertEquals (org.junit.Assert.assertEquals)2