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);
}
}
}
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);
}
}
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();
}
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);
}
}
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();
}
Aggregations