org.springframework.integration.leader.event.OnGrantedEvent Java Examples

The following examples show how to use org.springframework.integration.leader.event.OnGrantedEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalLeaderTest.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure behavior in case of start, stop in case the module is not configured to be leader.
 */
@Test
void startAndStopIfNotLeader() {
    this.localLeader = new LocalLeader(this.genieEventBus, false);
    Assertions.assertThat(this.localLeader.isRunning()).isFalse();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Start
    this.localLeader.start();
    Assertions.assertThat(this.localLeader.isRunning()).isTrue();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Stop
    this.localLeader.stopLeadership(this.contextClosedEvent);
    Assertions.assertThat(this.localLeader.isRunning()).isFalse();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));
}
 
Example #2
Source File: LeaderAspect.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
@EventListener
public void granted(OnGrantedEvent event) {
    if (!leader) {
        leader = true;
        log.info("Transitioned to leader");
    }
}
 
Example #3
Source File: SingleInstanceTaskListener.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
	if (applicationEvent instanceof OnGrantedEvent) {
		this.lockReady = true;
	}
	else if (applicationEvent instanceof OnFailedToAcquireMutexEvent) {
		this.lockFailed = true;
	}
}
 
Example #4
Source File: LocalLeader.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * If configured to be leader, activate and send a leadership acquired notification.
 * NOOP if already running or if this node is not configured to be leader.
 */
public void start() {
    if (this.isRunning.compareAndSet(false, true) && this.isLeader) {
        log.debug("Starting Leadership");
        this.genieEventBus.publishSynchronousEvent(new OnGrantedEvent(this, null, "leader"));
    }
}
 
Example #5
Source File: LocalLeaderTest.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure behavior in case of start, stop, start when already running and stop when not running in case the module
 * is configured to be leader.
 */
@Test
void startAndStopIfLeader() {
    this.localLeader = new LocalLeader(this.genieEventBus, true);
    Assertions.assertThat(this.localLeader.isRunning()).isFalse();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Start with application context event
    this.localLeader.startLeadership(this.contextRefreshedEvent);
    Assertions.assertThat(this.localLeader.isRunning()).isTrue();
    Assertions.assertThat(this.localLeader.isLeader()).isTrue();
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Start again
    this.localLeader.start();
    Assertions.assertThat(this.localLeader.isRunning()).isTrue();
    Assertions.assertThat(this.localLeader.isLeader()).isTrue();
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.never()).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Stop with application context event
    this.localLeader.stopLeadership(this.contextClosedEvent);
    Assertions.assertThat(this.localLeader.isRunning()).isFalse();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));

    // Stop again
    this.localLeader.stopLeadership(this.contextClosedEvent);
    Assertions.assertThat(this.localLeader.isRunning()).isFalse();
    Assertions.assertThat(this.localLeader.isLeader()).isFalse();
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnGrantedEvent.class));
    Mockito.verify(this.genieEventBus, Mockito.times(1)).publishSynchronousEvent(Mockito.any(OnRevokedEvent.class));
}
 
Example #6
Source File: LeaderTasksCoordinatorTest.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure all leadership activities are started when leadership is granted.
 */
@Test
void canStartLeadershipTasks() {
    final long task1Period = 13238;
    Mockito.when(this.task1.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_RATE);
    Mockito.when(this.task1.getFixedRate()).thenReturn(task1Period);
    final long task2Period = 3891082;
    Mockito.when(this.task2.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_DELAY);
    Mockito.when(this.task2.getFixedDelay()).thenReturn(task2Period);
    final Trigger task3Trigger = Mockito.mock(Trigger.class);
    Mockito.when(this.task3.getScheduleType()).thenReturn(GenieTaskScheduleType.TRIGGER);
    Mockito.when(this.task3.getTrigger()).thenReturn(task3Trigger);

    final OnGrantedEvent event = new OnGrantedEvent(this, null, "blah");

    this.coordinator.onLeaderEvent(event);

    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleAtFixedRate(this.task1, task1Period);
    Mockito.verify(this.task1, Mockito.never()).getFixedDelay();
    Mockito.verify(this.task1, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleWithFixedDelay(this.task2, task2Period);
    Mockito.verify(this.task2, Mockito.never()).getFixedRate();
    Mockito.verify(this.task2, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).schedule(this.task3, task3Trigger);
    Mockito.verify(this.task3, Mockito.never()).getFixedRate();
    Mockito.verify(this.task3, Mockito.never()).getFixedDelay();

    //Make sure a second OnGrantedEvent doesn't do anything if it's already running
    this.coordinator.onLeaderEvent(event);

    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleAtFixedRate(this.task1, task1Period);
    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleWithFixedDelay(this.task2, task2Period);
    Mockito.verify(this.scheduler, Mockito.times(1)).schedule(this.task3, task3Trigger);
}
 
Example #7
Source File: LeaderController.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
/**
 * Handle a notification that this instance has become a leader.
 * @param event on granted event
 */
@EventListener
public void handleEvent(OnGrantedEvent event) {
	System.out.println(String.format("'%s' leadership granted", event.getRole()));
	this.context = event.getContext();
}
 
Example #8
Source File: LeaderTasksCoordinator.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
 * Leadership event listener. Starts and stop processes when this Genie node is elected the leader of the cluster.
 * <p>
 * Synchronized to ensure no race conditions between threads trying to start and stop leadership tasks.
 *
 * @param leaderEvent The leader grant or revoke event
 * @see org.springframework.integration.leader.event.OnGrantedEvent
 * @see org.springframework.integration.leader.event.OnRevokedEvent
 */
@EventListener
public synchronized void onLeaderEvent(final AbstractLeaderEvent leaderEvent) {
    if (leaderEvent instanceof OnGrantedEvent) {
        if (this.isRunning) {
            return;
        }
        log.info("Leadership granted.");
        this.isRunning = true;
        this.tasks.forEach(
            task -> {
                switch (task.getScheduleType()) {
                    case TRIGGER:
                        final Trigger trigger = task.getTrigger();
                        log.info(
                            "Scheduling leadership task {} to run with trigger {}",
                            task.getClass().getCanonicalName(),
                            trigger
                        );
                        this.futures.add(this.taskScheduler.schedule(task, trigger));
                        break;
                    case FIXED_RATE:
                        final long rate = task.getFixedRate();
                        log.info(
                            "Scheduling leadership task {} to run every {} second(s)",
                            task.getClass().getCanonicalName(),
                            rate / 1000.0
                        );
                        this.futures.add(this.taskScheduler.scheduleAtFixedRate(task, rate));
                        break;
                    case FIXED_DELAY:
                        final long delay = task.getFixedDelay();
                        log.info(
                            "Scheduling leadership task {} to run at a fixed delay of every {} second(s)",
                            task.getClass().getCanonicalName(),
                            delay / 1000.0
                        );
                        this.futures.add(this.taskScheduler.scheduleWithFixedDelay(task, delay));
                        break;
                    default:
                        log.error("Unknown Genie task type {}", task.getScheduleType());
                }
            }
        );
    } else if (leaderEvent instanceof OnRevokedEvent) {
        if (!this.isRunning) {
            return;
        }
        log.info("Leadership revoked.");
        this.isRunning = false;
        this.cancelTasks();
    } else {
        log.warn("Unknown leadership event {}. Ignoring.", leaderEvent);
    }
}
 
Example #9
Source File: LeaderTasksCoordinatorTest.java    From genie with Apache License 2.0 4 votes vote down vote up
/**
 * Make sure all leadership activities are stopped when leadership is revoked.
 */
@Test
@SuppressWarnings("unchecked")
void canStopLeadershipTasks() {
    final long task1Period = 13238;
    Mockito.when(this.task1.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_RATE);
    Mockito.when(this.task1.getFixedRate()).thenReturn(task1Period);
    final long task2Period = 3891082;
    Mockito.when(this.task2.getScheduleType()).thenReturn(GenieTaskScheduleType.FIXED_DELAY);
    Mockito.when(this.task2.getFixedDelay()).thenReturn(task2Period);
    final Trigger task3Trigger = Mockito.mock(Trigger.class);
    Mockito.when(this.task3.getScheduleType()).thenReturn(GenieTaskScheduleType.TRIGGER);
    Mockito.when(this.task3.getTrigger()).thenReturn(task3Trigger);

    final ScheduledFuture future1 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future1.cancel(true)).thenReturn(true);
    Mockito.when(this.scheduler.scheduleAtFixedRate(this.task1, task1Period)).thenReturn(future1);

    final ScheduledFuture future2 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future2.cancel(true)).thenReturn(true);
    Mockito.when(this.scheduler.scheduleWithFixedDelay(this.task2, task2Period)).thenReturn(future2);

    final ScheduledFuture future3 = Mockito.mock(ScheduledFuture.class);
    Mockito.when(future3.cancel(true)).thenReturn(false);
    Mockito.when(this.scheduler.schedule(this.task3, task3Trigger)).thenReturn(future3);

    final OnGrantedEvent grantedEvent = new OnGrantedEvent(this, null, "blah");

    this.coordinator.onLeaderEvent(grantedEvent);

    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleAtFixedRate(this.task1, task1Period);
    Mockito.verify(this.task1, Mockito.never()).getFixedDelay();
    Mockito.verify(this.task1, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).scheduleWithFixedDelay(this.task2, task2Period);
    Mockito.verify(this.task2, Mockito.never()).getFixedRate();
    Mockito.verify(this.task2, Mockito.never()).getTrigger();
    Mockito.verify(this.scheduler, Mockito.times(1)).schedule(this.task3, task3Trigger);
    Mockito.verify(this.task3, Mockito.never()).getFixedRate();
    Mockito.verify(this.task3, Mockito.never()).getFixedDelay();

    // Should now be running

    final OnRevokedEvent revokedEvent = new OnRevokedEvent(this, null, "blah");

    this.coordinator.onLeaderEvent(revokedEvent);

    Mockito.verify(future1, Mockito.times(1)).cancel(true);
    Mockito.verify(future2, Mockito.times(1)).cancel(true);
    Mockito.verify(future3, Mockito.times(1)).cancel(true);

    // Call again to make sure nothing is invoked even though they were cancelled
    this.coordinator.onLeaderEvent(revokedEvent);

    Mockito.verify(future1, Mockito.times(1)).cancel(true);
    Mockito.verify(future2, Mockito.times(1)).cancel(true);
    Mockito.verify(future3, Mockito.times(1)).cancel(true);
}