Search in sources :

Example 11 with ClusterMemberAvailability

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();
}
Also used : ClusterClient(org.neo4j.cluster.client.ClusterClient) SwitchToSlaveCopyThenBranch(org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch) InOrder(org.mockito.InOrder) InstanceId(org.neo4j.cluster.InstanceId) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) SwitchToMaster(org.neo4j.kernel.ha.cluster.SwitchToMaster) Election(org.neo4j.cluster.protocol.election.Election) Test(org.junit.Test)

Example 12 with ClusterMemberAvailability

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();
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Election(org.neo4j.cluster.protocol.election.Election) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterClient(org.neo4j.cluster.client.ClusterClient) SwitchToSlaveCopyThenBranch(org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) SwitchToMaster(org.neo4j.kernel.ha.cluster.SwitchToMaster) CancellationRequest(org.neo4j.helpers.CancellationRequest) Test(org.junit.Test)

Aggregations

ClusterMemberAvailability (org.neo4j.cluster.member.ClusterMemberAvailability)12 Test (org.junit.Test)11 InstanceId (org.neo4j.cluster.InstanceId)11 HighAvailabilityMemberChangeEvent (org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent)8 ClusterClient (org.neo4j.cluster.client.ClusterClient)7 Election (org.neo4j.cluster.protocol.election.Election)7 SwitchToMaster (org.neo4j.kernel.ha.cluster.SwitchToMaster)6 SwitchToSlaveCopyThenBranch (org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch)6 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)4 URI (java.net.URI)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 InOrder (org.mockito.InOrder)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CancellationRequest (org.neo4j.helpers.CancellationRequest)2 File (java.io.File)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BindingNotifier (org.neo4j.cluster.com.BindingNotifier)1