Search in sources :

Example 11 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class CacheGroup method getNodes.

@Override
public List<Node> getNodes() {
    List<Address> addresses = this.getAddresses();
    List<Node> nodes = new ArrayList<>(addresses.size());
    for (Address address : addresses) {
        nodes.add(this.factory.createNode(address));
    }
    return nodes;
}
Also used : Address(org.infinispan.remoting.transport.Address) Node(org.wildfly.clustering.group.Node) ArrayList(java.util.ArrayList)

Example 12 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class CacheServiceProviderRegistry method register.

@Override
public ServiceProviderRegistration<T> register(T service, Listener listener) {
    Map.Entry<Listener, ExecutorService> newEntry = new AbstractMap.SimpleEntry<>(listener, null);
    // Only create executor for new registrations
    Map.Entry<Listener, ExecutorService> entry = this.listeners.computeIfAbsent(service, key -> {
        if (listener != null) {
            newEntry.setValue(Executors.newSingleThreadExecutor(createThreadFactory(listener.getClass())));
        }
        return newEntry;
    });
    if (entry != newEntry) {
        throw new IllegalArgumentException(service.toString());
    }
    try (Batch batch = this.batcher.createBatch()) {
        this.register(this.group.getLocalNode(), service);
    }
    return new SimpleServiceProviderRegistration<>(service, this, () -> {
        Node node = this.getGroup().getLocalNode();
        try (Batch batch = this.batcher.createBatch()) {
            Set<Node> nodes = this.cache.getAdvancedCache().withFlags(Flag.FORCE_WRITE_LOCK).get(service);
            if ((nodes != null) && nodes.remove(node)) {
                Cache<T, Set<Node>> cache = this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES);
                if (nodes.isEmpty()) {
                    cache.remove(service);
                } else {
                    cache.replace(service, nodes);
                }
            }
        } finally {
            Map.Entry<Listener, ExecutorService> oldEntry = this.listeners.remove(service);
            if (oldEntry != null) {
                ExecutorService executor = oldEntry.getValue();
                if (executor != null) {
                    PrivilegedAction<List<Runnable>> action = () -> executor.shutdownNow();
                    WildFlySecurityManager.doUnchecked(action);
                    try {
                        executor.awaitTermination(this.cache.getCacheConfiguration().transaction().cacheStopTimeout(), TimeUnit.MILLISECONDS);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    });
}
Also used : Listener(org.wildfly.clustering.provider.ServiceProviderRegistration.Listener) HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Node(org.wildfly.clustering.group.Node) Batch(org.wildfly.clustering.ee.Batch) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap)

Example 13 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class KeyMapperTestCase method test.

@Test
public void test() throws UnknownHostException {
    TwoWayKey2StringMapper mapper = new KeyMapper();
    Assert.assertTrue(mapper.isSupportedType(LocalNode.class));
    Assert.assertTrue(mapper.isSupportedType(AddressableNode.class));
    Assert.assertTrue(mapper.isSupportedType(ServiceName.class));
    Set<String> formatted = new HashSet<>();
    Node localNode = new LocalNode("cluster", "node");
    String mappedLocalNode = mapper.getStringMapping(localNode);
    Assert.assertEquals(localNode, mapper.getKeyMapping(mappedLocalNode));
    Assert.assertTrue(formatted.add(mappedLocalNode));
    Node addressableNode = new AddressableNode(UUID.randomUUID(), "node", new InetSocketAddress(InetAddress.getLocalHost(), 0));
    String mappedAddressableNode = mapper.getStringMapping(addressableNode);
    Assert.assertEquals(addressableNode, mapper.getKeyMapping(mappedAddressableNode));
    Assert.assertTrue(formatted.add(mappedAddressableNode));
    ServiceName serviceName = ServiceName.of("node");
    String mappedServiceName = mapper.getStringMapping(serviceName);
    Assert.assertEquals(serviceName, mapper.getKeyMapping(mappedServiceName));
    Assert.assertTrue(formatted.add(mappedServiceName));
}
Also used : TwoWayKey2StringMapper(org.infinispan.persistence.keymappers.TwoWayKey2StringMapper) LocalNode(org.wildfly.clustering.server.group.LocalNode) ServiceName(org.jboss.msc.service.ServiceName) InetSocketAddress(java.net.InetSocketAddress) AddressableNode(org.wildfly.clustering.server.group.AddressableNode) LocalNode(org.wildfly.clustering.server.group.LocalNode) Node(org.wildfly.clustering.group.Node) AddressableNode(org.wildfly.clustering.server.group.AddressableNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class NamePreferenceTestCase method test.

@Test
public void test() {
    Preference preference = new NamePreference("node1");
    Node node1 = mock(Node.class);
    Node node2 = mock(Node.class);
    when(node1.getName()).thenReturn("node1");
    when(node2.getName()).thenReturn("node2");
    assertTrue(preference.preferred(node1));
    assertFalse(preference.preferred(node2));
}
Also used : NamePreference(org.wildfly.clustering.singleton.election.NamePreference) NamePreference(org.wildfly.clustering.singleton.election.NamePreference) Preference(org.wildfly.clustering.singleton.election.Preference) Node(org.wildfly.clustering.group.Node) Test(org.junit.Test)

Example 15 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class PreferredSingletonElectionPolicyTestCase method elect.

@Test
public void elect() {
    SingletonElectionPolicy policy = mock(SingletonElectionPolicy.class);
    Preference preference1 = mock(Preference.class);
    Preference preference2 = mock(Preference.class);
    Node node1 = mock(Node.class);
    Node node2 = mock(Node.class);
    Node node3 = mock(Node.class);
    Node node4 = mock(Node.class);
    when(preference1.preferred(same(node1))).thenReturn(true);
    when(preference1.preferred(same(node2))).thenReturn(false);
    when(preference1.preferred(same(node3))).thenReturn(false);
    when(preference1.preferred(same(node4))).thenReturn(false);
    when(preference2.preferred(same(node1))).thenReturn(false);
    when(preference2.preferred(same(node2))).thenReturn(true);
    when(preference2.preferred(same(node3))).thenReturn(false);
    when(preference2.preferred(same(node4))).thenReturn(false);
    assertSame(node1, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node1, node2, node3, node4)));
    assertSame(node1, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node4, node3, node2, node1)));
    assertSame(node2, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node2, node3, node4)));
    assertSame(node2, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(Arrays.asList(node4, node3, node2)));
    List<Node> nodes = Arrays.asList(node3, node4);
    when(policy.elect(nodes)).thenReturn(node3);
    assertSame(node3, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
    when(policy.elect(nodes)).thenReturn(node4);
    assertSame(node4, new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
    when(policy.elect(nodes)).thenReturn(null);
    assertNull(new PreferredSingletonElectionPolicy(policy, preference1, preference2).elect(nodes));
}
Also used : SingletonElectionPolicy(org.wildfly.clustering.singleton.SingletonElectionPolicy) PreferredSingletonElectionPolicy(org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy) Preference(org.wildfly.clustering.singleton.election.Preference) Node(org.wildfly.clustering.group.Node) PreferredSingletonElectionPolicy(org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy) Test(org.junit.Test)

Aggregations

Node (org.wildfly.clustering.group.Node)22 Map (java.util.Map)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashSet (java.util.HashSet)5 Set (java.util.Set)4 Test (org.junit.Test)4 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Batch (org.wildfly.clustering.ee.Batch)3 Group (org.wildfly.clustering.group.Group)3 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Collections (java.util.Collections)2 List (java.util.List)2 CancellationException (java.util.concurrent.CancellationException)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2