Search in sources :

Example 1 with KeyDistribution

use of org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution in project wildfly by wildfly.

the class DefaultKeyAffinityService method accept.

private void accept(ConsistentHash hash) {
    KeyDistribution distribution = new ConsistentHashKeyDistribution(this.partitioner, hash);
    KeyRegistry<K> registry = new ConsistentHashKeyRegistry<>(hash, this.filter, this);
    Set<Address> addresses = registry.getAddresses();
    List<Future<?>> futures = !addresses.isEmpty() ? new ArrayList<>(addresses.size()) : Collections.emptyList();
    try {
        for (Address address : addresses) {
            BlockingQueue<K> keys = registry.getKeys(address);
            futures.add(this.executor.submit(new GenerateKeysTask<>(this.generator, distribution, address, keys)));
        }
        KeyAffinityState<K> previousState = this.currentState.getAndSet(new KeyAffinityState<K>() {

            @Override
            public KeyDistribution getDistribution() {
                return distribution;
            }

            @Override
            public KeyRegistry<K> getRegistry() {
                return registry;
            }

            @Override
            public Iterable<Future<?>> getFutures() {
                return futures;
            }
        });
        if (previousState != null) {
            for (Future<?> future : previousState.getFutures()) {
                future.cancel(true);
            }
        }
    } catch (RejectedExecutionException e) {
        // Executor was shutdown. Cancel any tasks that were already submitted
        for (Future<?> future : futures) {
            future.cancel(true);
        }
    }
}
Also used : Address(org.infinispan.remoting.transport.Address) KeyDistribution(org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution) ConsistentHashKeyDistribution(org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashKeyDistribution) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ConsistentHashKeyDistribution(org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashKeyDistribution) Future(java.util.concurrent.Future)

Example 2 with KeyDistribution

use of org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution in project wildfly by wildfly.

the class PrimaryOwnerLocatorTestCase method test.

@Test
public void test() {
    KeyDistribution distribution = mock(KeyDistribution.class);
    NodeFactory<Address> memberFactory = mock(NodeFactory.class);
    Address address = mock(Address.class);
    Node member = mock(Node.class);
    Object key = new Object();
    Function<Object, Node> locator = new PrimaryOwnerLocator<>(distribution, memberFactory);
    when(distribution.getPrimaryOwner(key)).thenReturn(address);
    when(memberFactory.createNode(address)).thenReturn(member);
    Node result = locator.apply(key);
    assertSame(member, result);
}
Also used : Address(org.infinispan.remoting.transport.Address) Node(org.wildfly.clustering.group.Node) KeyDistribution(org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution) Test(org.junit.Test)

Example 3 with KeyDistribution

use of org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution in project wildfly by wildfly.

the class RankedRouteLocatorTestCase method test.

@Test
public void test() {
    KeyDistribution distribution = mock(KeyDistribution.class);
    NodeFactory<Address> factory = mock(NodeFactory.class);
    Registry<String, Void> registry = mock(Registry.class);
    Group group = mock(Group.class);
    Address owner1 = mock(Address.class);
    Address owner2 = mock(Address.class);
    Address owner3 = mock(Address.class);
    Address owner4 = mock(Address.class);
    Address unregistered = mock(Address.class);
    Address local = mock(Address.class);
    Node member1 = mock(Node.class);
    Node member2 = mock(Node.class);
    Node member3 = mock(Node.class);
    Node member4 = mock(Node.class);
    Node unregisteredMember = mock(Node.class);
    Node localMember = mock(Node.class);
    when(registry.getGroup()).thenReturn(group);
    when(group.getLocalMember()).thenReturn(localMember);
    when(registry.getEntry(member1)).thenReturn(new SimpleImmutableEntry<>("member1", null));
    when(registry.getEntry(member2)).thenReturn(new SimpleImmutableEntry<>("member2", null));
    when(registry.getEntry(member3)).thenReturn(new SimpleImmutableEntry<>("member3", null));
    when(registry.getEntry(member4)).thenReturn(new SimpleImmutableEntry<>("member4", null));
    when(registry.getEntry(localMember)).thenReturn(new SimpleImmutableEntry<>("local", null));
    when(registry.getEntry(unregisteredMember)).thenReturn(null);
    when(factory.createNode(owner1)).thenReturn(member1);
    when(factory.createNode(owner2)).thenReturn(member2);
    when(factory.createNode(owner3)).thenReturn(member3);
    when(factory.createNode(owner4)).thenReturn(member4);
    when(factory.createNode(local)).thenReturn(localMember);
    when(factory.createNode(unregistered)).thenReturn(unregisteredMember);
    RouteLocator locator = new RankedRouteLocator(distribution, registry, factory, ".", 3);
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1, owner2, owner3, owner4));
    assertEquals("member1.member2.member3", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1, owner2, owner3, local));
    assertEquals("member1.member2.member3", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1, owner2, unregistered, owner4));
    assertEquals("member1.member2.member4", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1, local, owner3));
    assertEquals("member1.local.member3", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1, owner2));
    assertEquals("member1.member2.local", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(local, owner2));
    assertEquals("local.member2", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(owner1));
    assertEquals("member1.local", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(local));
    assertEquals("local", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Arrays.asList(unregistered));
    assertEquals("local", locator.locate("key"));
    when(distribution.getOwners(new GroupedKey<>("key"))).thenReturn(Collections.emptyList());
    assertEquals("local", locator.locate("key"));
}
Also used : Group(org.wildfly.clustering.group.Group) RouteLocator(org.wildfly.clustering.web.routing.RouteLocator) Address(org.infinispan.remoting.transport.Address) Node(org.wildfly.clustering.group.Node) KeyDistribution(org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution) Test(org.junit.Test)

Aggregations

Address (org.infinispan.remoting.transport.Address)3 KeyDistribution (org.wildfly.clustering.infinispan.spi.distribution.KeyDistribution)3 Test (org.junit.Test)2 Node (org.wildfly.clustering.group.Node)2 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Group (org.wildfly.clustering.group.Group)1 ConsistentHashKeyDistribution (org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashKeyDistribution)1 RouteLocator (org.wildfly.clustering.web.routing.RouteLocator)1