use of org.neo4j.cluster.member.ClusterMemberAvailability in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldPerformForcedElectionsOnlyOnce.
@Test
public void shouldPerformForcedElectionsOnlyOnce() {
// Given: HAMS
ClusterMemberAvailability memberAvailability = mock(ClusterMemberAvailability.class);
Election election = mock(Election.class);
HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher(mock(SwitchToSlaveCopyThenBranch.class), mock(SwitchToMaster.class), election, memberAvailability, mock(ClusterClient.class), storeSupplierMock(), mock(InstanceId.class), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
// When: reelections are forced multiple times
modeSwitcher.forceElections();
modeSwitcher.forceElections();
modeSwitcher.forceElections();
// Then: instance sens out memberIsUnavailable and asks for elections and does this only once
InOrder inOrder = inOrder(memberAvailability, election);
inOrder.verify(memberAvailability).memberIsUnavailable(HighAvailabilityModeSwitcher.SLAVE);
inOrder.verify(election).performRoleElections();
inOrder.verifyNoMoreInteractions();
}
use of org.neo4j.cluster.member.ClusterMemberAvailability in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldReswitchToSlaveIfNewMasterBecameElectedAndAvailableDuringSwitch.
@Test
public void shouldReswitchToSlaveIfNewMasterBecameElectedAndAvailableDuringSwitch() throws Throwable {
// Given
final CountDownLatch switching = new CountDownLatch(1);
final CountDownLatch slaveAvailable = new CountDownLatch(2);
final AtomicBoolean firstSwitch = new AtomicBoolean(true);
ClusterMemberAvailability availability = mock(ClusterMemberAvailability.class);
SwitchToSlaveCopyThenBranch switchToSlave = mock(SwitchToSlaveCopyThenBranch.class);
@SuppressWarnings("resource") SwitchToMaster switchToMaster = mock(SwitchToMaster.class);
when(switchToSlave.switchToSlave(any(LifeSupport.class), any(URI.class), any(URI.class), any(CancellationRequest.class))).thenAnswer(invocationOnMock -> {
switching.countDown();
CancellationRequest cancel = (CancellationRequest) invocationOnMock.getArguments()[3];
if (firstSwitch.get()) {
while (!cancel.cancellationRequested()) {
Thread.sleep(1);
}
firstSwitch.set(false);
}
slaveAvailable.countDown();
return URI.create("ha://slave");
});
HighAvailabilityModeSwitcher toTest = new HighAvailabilityModeSwitcher(switchToSlave, switchToMaster, mock(Election.class), availability, mock(ClusterClient.class), storeSupplierMock(), mock(InstanceId.class), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
toTest.init();
toTest.start();
toTest.listeningAt(URI.create("ha://server3?serverId=3"));
// When
// This will start a switch to slave
toTest.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, mock(InstanceId.class), URI.create("ha://server1")));
// Wait until it starts and blocks on the cancellation request
switching.await();
// change the elected master, moving to pending, cancelling the previous change. This will block until the
// previous switch is aborted
toTest.masterIsElected(new HighAvailabilityMemberChangeEvent(TO_SLAVE, PENDING, new InstanceId(2), URI.create("ha://server2")));
// Now move to the new master by switching to TO_SLAVE
toTest.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, new InstanceId(2), URI.create("ha://server2")));
// Then
// The second switch must happen and this test won't block
slaveAvailable.await();
}
Aggregations