Search in sources :

Example 1 with Group

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

the class DistributedSingletonService method providersChanged.

@Override
public void providersChanged(Set<Node> nodes) {
    Group group = this.registry.getValue().getGroup();
    List<Node> candidates = group.getNodes();
    candidates.retainAll(nodes);
    // Only run election on a single node
    if (candidates.isEmpty() || candidates.get(0).equals(group.getLocalNode())) {
        // First validate that quorum was met
        int size = candidates.size();
        boolean quorumMet = size >= this.quorum;
        if ((this.quorum > 1) && (size == this.quorum)) {
            // Log fragility of singleton availability
            ClusteringServerLogger.ROOT_LOGGER.quorumJustReached(this.serviceName.getCanonicalName(), this.quorum);
        }
        Node elected = quorumMet ? this.electionPolicy.elect(candidates) : null;
        try {
            if (elected != null) {
                ClusteringServerLogger.ROOT_LOGGER.elected(elected.getName(), this.serviceName.getCanonicalName());
                // Stop service on every node except elected node
                this.dispatcher.executeOnCluster(new StopCommand<>(), elected);
                // Start service on elected node
                this.dispatcher.executeOnNode(new StartCommand<>(), elected);
            } else {
                if (quorumMet) {
                    ClusteringServerLogger.ROOT_LOGGER.noPrimaryElected(this.serviceName.getCanonicalName());
                } else {
                    ClusteringServerLogger.ROOT_LOGGER.quorumNotReached(this.serviceName.getCanonicalName(), this.quorum);
                }
                // Stop service on every node
                this.dispatcher.executeOnCluster(new StopCommand<>());
            }
        } catch (CommandDispatcherException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Group(org.wildfly.clustering.group.Group) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node)

Example 2 with Group

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

the class NodeServicePolicyActivator method activate.

@Override
public void activate(ServiceActivatorContext context) throws ServiceRegistryException {
    try {
        SingletonPolicy policy = (SingletonPolicy) context.getServiceRegistry().getRequiredService(ServiceName.parse(SingletonDefaultRequirement.SINGLETON_POLICY.getName())).awaitValue();
        InjectedValue<Group> group = new InjectedValue<>();
        NodeService service = new NodeService(group);
        policy.createSingletonServiceBuilder(SERVICE_NAME, service).build(context.getServiceTarget()).addDependency(ServiceName.parse("org.wildfly.clustering.default-group"), Group.class, group).install();
    } catch (InterruptedException e) {
        throw new ServiceRegistryException(e);
    }
}
Also used : InjectedValue(org.jboss.msc.value.InjectedValue) Group(org.wildfly.clustering.group.Group) SingletonPolicy(org.wildfly.clustering.singleton.SingletonPolicy) ServiceRegistryException(org.jboss.msc.service.ServiceRegistryException)

Example 3 with Group

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

the class SingletonServiceActivator method install.

private static void install(ServiceTarget target, SingletonServiceBuilderFactory factory, ServiceName name, String preferredNode) {
    InjectedValue<Group> group = new InjectedValue<>();
    NodeService service = new NodeService(group);
    factory.createSingletonServiceBuilder(name, service).electionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference(preferredNode))).build(target).addDependency(ServiceName.JBOSS.append("clustering", "group", "default"), Group.class, group).install();
}
Also used : InjectedValue(org.jboss.msc.value.InjectedValue) Group(org.wildfly.clustering.group.Group) NamePreference(org.wildfly.clustering.singleton.election.NamePreference) NodeService(org.jboss.as.test.clustering.cluster.singleton.service.NodeService) SimpleSingletonElectionPolicy(org.wildfly.clustering.singleton.election.SimpleSingletonElectionPolicy) PreferredSingletonElectionPolicy(org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy)

Example 4 with Group

use of org.wildfly.clustering.group.Group in project quickstart by wildfly.

the class ServiceActivator method activate.

@Override
public void activate(ServiceActivatorContext serviceActivatorContext) {
    try {
        SingletonPolicy policy = (SingletonPolicy) serviceActivatorContext.getServiceRegistry().getRequiredService(ServiceName.parse(SingletonDefaultRequirement.SINGLETON_POLICY.getName())).awaitValue();
        InjectedValue<Group> group = new InjectedValue<>();
        Service<Node> service = new SingletonService(group);
        policy.createSingletonServiceBuilder(SINGLETON_SERVICE_NAME, service).build(serviceActivatorContext.getServiceTarget()).addDependency(ServiceName.parse("org.wildfly.clustering.default-group"), Group.class, group).install();
        serviceActivatorContext.getServiceTarget().addService(QUERYING_SERVICE_NAME, new QueryingService()).setInitialMode(ServiceController.Mode.ACTIVE).install();
        LOG.info("Singleton and querying services activated.");
    } catch (InterruptedException e) {
        throw new ServiceRegistryException(e);
    }
}
Also used : Group(org.wildfly.clustering.group.Group) InjectedValue(org.jboss.msc.value.InjectedValue) SingletonPolicy(org.wildfly.clustering.singleton.SingletonPolicy) Node(org.wildfly.clustering.group.Node) ServiceRegistryException(org.jboss.msc.service.ServiceRegistryException)

Example 5 with Group

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

the class BeanExpirationSchedulerTestCase method testExpire.

@Test
public void testExpire() throws InterruptedException {
    Group group = mock(Group.class);
    Batcher<TransactionBatch> batcher = mock(Batcher.class);
    TransactionBatch batch = mock(TransactionBatch.class);
    BeanFactory<String, Object> factory = mock(BeanFactory.class);
    ExpirationConfiguration<Object> config = mock(ExpirationConfiguration.class);
    RemoveListener<Object> listener = mock(RemoveListener.class);
    BeanEntry<String> entry = mock(BeanEntry.class);
    BeanRemover<String, Object> remover = mock(BeanRemover.class);
    String beanId = "expiring";
    Duration timeout = Duration.ofMillis(10L);
    when(group.isSingleton()).thenReturn(true);
    when(batcher.createBatch()).thenReturn(batch);
    when(config.getTimeout()).thenReturn(timeout);
    when(config.getRemoveListener()).thenReturn(listener);
    when(entry.getLastAccessedTime()).thenReturn(Instant.now());
    when(remover.remove(beanId, listener)).thenReturn(true);
    try (Scheduler<String, ImmutableBeanEntry<String>> scheduler = new BeanExpirationScheduler<>(group, batcher, factory, config, remover, Duration.ZERO)) {
        scheduler.schedule(beanId, entry);
        Thread.sleep(500);
    }
    verify(batch).close();
}
Also used : Group(org.wildfly.clustering.group.Group) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

Group (org.wildfly.clustering.group.Group)19 Test (org.junit.Test)7 InjectedValue (org.jboss.msc.value.InjectedValue)6 Node (org.wildfly.clustering.group.Node)6 TransactionBatch (org.wildfly.clustering.ee.cache.tx.TransactionBatch)4 Duration (java.time.Duration)3 Service (org.jboss.msc.Service)3 ServiceRegistryException (org.jboss.msc.service.ServiceRegistryException)3 FunctionalService (org.wildfly.clustering.service.FunctionalService)3 SingletonPolicy (org.wildfly.clustering.singleton.SingletonPolicy)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)2 NamePreference (org.wildfly.clustering.singleton.election.NamePreference)2 PreferredSingletonElectionPolicy (org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy)2 SimpleSingletonElectionPolicy (org.wildfly.clustering.singleton.election.SimpleSingletonElectionPolicy)2 RouteLocator (org.wildfly.clustering.web.routing.RouteLocator)2 AbstractMap (java.util.AbstractMap)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 LinkedList (java.util.LinkedList)1