Search in sources :

Example 1 with Preference

use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.

the class ElectionPolicyServiceConfigurator method configure.

@Override
public ServiceConfigurator configure(OperationContext context, ModelNode model) throws OperationFailedException {
    List<ModelNode> socketBindingPreferences = SOCKET_BINDING_PREFERENCES.resolveModelAttribute(context, model).asListOrEmpty();
    List<ModelNode> namePreferences = NAME_PREFERENCES.resolveModelAttribute(context, model).asListOrEmpty();
    List<Preference> preferences = new ArrayList<>(socketBindingPreferences.size() + namePreferences.size());
    List<Dependency> dependencies = new ArrayList<>(socketBindingPreferences.size());
    for (ModelNode preference : socketBindingPreferences) {
        SupplierDependency<OutboundSocketBinding> binding = new ServiceSupplierDependency<>(CommonUnaryRequirement.OUTBOUND_SOCKET_BINDING.getServiceName(context, preference.asString()));
        preferences.add(new OutboundSocketBindingPreference(binding));
        dependencies.add(binding);
    }
    for (ModelNode preference : namePreferences) {
        preferences.add(new NamePreference(preference.asString()));
    }
    this.dependencies = dependencies;
    this.preferences = preferences;
    return this;
}
Also used : OutboundSocketBindingPreference(org.wildfly.extension.clustering.singleton.election.OutboundSocketBindingPreference) NamePreference(org.wildfly.clustering.singleton.election.NamePreference) OutboundSocketBinding(org.jboss.as.network.OutboundSocketBinding) OutboundSocketBindingPreference(org.wildfly.extension.clustering.singleton.election.OutboundSocketBindingPreference) Preference(org.wildfly.clustering.singleton.election.Preference) NamePreference(org.wildfly.clustering.singleton.election.NamePreference) ArrayList(java.util.ArrayList) SupplierDependency(org.wildfly.clustering.service.SupplierDependency) ServiceSupplierDependency(org.wildfly.clustering.service.ServiceSupplierDependency) Dependency(org.wildfly.clustering.service.Dependency) ServiceSupplierDependency(org.wildfly.clustering.service.ServiceSupplierDependency) ModelNode(org.jboss.dmr.ModelNode)

Example 2 with Preference

use of org.wildfly.clustering.singleton.election.Preference in project wildfly by wildfly.

the class SocketAddressPreferenceTestCase method test.

@Test
public void test() throws UnknownHostException {
    InetSocketAddress preferredAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 1);
    InetSocketAddress otherAddress1 = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2);
    InetSocketAddress otherAddress2 = new InetSocketAddress(InetAddress.getByName("127.0.0.2"), 1);
    Preference preference = new SocketAddressPreference(preferredAddress);
    Node preferredNode = mock(Node.class);
    Node otherNode1 = mock(Node.class);
    Node otherNode2 = mock(Node.class);
    when(preferredNode.getSocketAddress()).thenReturn(preferredAddress);
    when(otherNode1.getSocketAddress()).thenReturn(otherAddress1);
    when(otherNode2.getSocketAddress()).thenReturn(otherAddress2);
    assertTrue(preference.preferred(preferredNode));
    assertFalse(preference.preferred(otherNode1));
    assertFalse(preference.preferred(otherNode2));
}
Also used : SocketAddressPreference(org.wildfly.clustering.singleton.election.SocketAddressPreference) SocketAddressPreference(org.wildfly.clustering.singleton.election.SocketAddressPreference) Preference(org.wildfly.clustering.singleton.election.Preference) InetSocketAddress(java.net.InetSocketAddress) Node(org.wildfly.clustering.group.Node) Test(org.junit.Test)

Example 3 with Preference

use of org.wildfly.clustering.singleton.election.Preference 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)

Example 4 with Preference

use of org.wildfly.clustering.singleton.election.Preference 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)

Aggregations

Preference (org.wildfly.clustering.singleton.election.Preference)4 Test (org.junit.Test)3 Node (org.wildfly.clustering.group.Node)3 NamePreference (org.wildfly.clustering.singleton.election.NamePreference)2 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 OutboundSocketBinding (org.jboss.as.network.OutboundSocketBinding)1 ModelNode (org.jboss.dmr.ModelNode)1 Dependency (org.wildfly.clustering.service.Dependency)1 ServiceSupplierDependency (org.wildfly.clustering.service.ServiceSupplierDependency)1 SupplierDependency (org.wildfly.clustering.service.SupplierDependency)1 SingletonElectionPolicy (org.wildfly.clustering.singleton.SingletonElectionPolicy)1 PreferredSingletonElectionPolicy (org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy)1 SocketAddressPreference (org.wildfly.clustering.singleton.election.SocketAddressPreference)1 OutboundSocketBindingPreference (org.wildfly.extension.clustering.singleton.election.OutboundSocketBindingPreference)1