Search in sources :

Example 6 with HighAvailabilityMemberChangeEvent

use of org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent in project neo4j by neo4j.

the class UpdatePullingTransactionObligationFulfillerTest method shouldUpdateTransactionIdStoreCorrectly.

@Test
public void shouldUpdateTransactionIdStoreCorrectly() throws Throwable {
    // Given
    TransactionIdStore store1 = mock(TransactionIdStore.class);
    TransactionIdStore store2 = mock(TransactionIdStore.class);
    @SuppressWarnings("unchecked") Supplier<TransactionIdStore> supplier = mock(Supplier.class);
    when(supplier.get()).thenReturn(store1, store2);
    doAnswer(invocation -> {
        ((HighAvailabilityMemberListener) invocation.getArguments()[0]).slaveIsAvailable(new HighAvailabilityMemberChangeEvent(null, null, serverId, null));
        return null;
    }).when(machine).addHighAvailabilityMemberListener(any(HighAvailabilityMemberListener.class));
    doAnswer(invocation -> {
        ((HighAvailabilityMemberListener) invocation.getArguments()[0]).instanceStops(new HighAvailabilityMemberChangeEvent(null, null, serverId, null));
        return null;
    }).when(machine).removeHighAvailabilityMemberListener(any(HighAvailabilityMemberListener.class));
    UpdatePullingTransactionObligationFulfiller fulfiller = new UpdatePullingTransactionObligationFulfiller(updatePuller, machine, serverId, supplier);
    // When
    fulfiller.start();
    fulfiller.fulfill(1);
    fulfiller.stop();
    fulfiller.fulfill(2);
    fulfiller.start();
    fulfiller.fulfill(3);
    fulfiller.stop();
    fulfiller.fulfill(4);
    // Then
    verify(store1, times(1)).getLastClosedTransactionId();
    verify(store2, times(1)).getLastClosedTransactionId();
}
Also used : HighAvailabilityMemberListener(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberListener) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) Test(org.junit.Test)

Example 7 with HighAvailabilityMemberChangeEvent

use of org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent in project neo4j by neo4j.

the class HighAvailabilityModeSwitcherTest method shouldAllowForcedElectionsAfterModeSwitch.

@Test
public void shouldAllowForcedElectionsAfterModeSwitch() throws Throwable {
    // Given
    SwitchToSlaveCopyThenBranch switchToSlave = mock(SwitchToSlaveCopyThenBranch.class);
    when(switchToSlave.switchToSlave(any(LifeSupport.class), any(URI.class), any(URI.class), any(CancellationRequest.class))).thenReturn(URI.create("http://localhost"));
    ClusterMemberAvailability memberAvailability = mock(ClusterMemberAvailability.class);
    Election election = mock(Election.class);
    final CountDownLatch modeSwitchHappened = new CountDownLatch(1);
    HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), election, memberAvailability, mock(ClusterClient.class), storeSupplierMock(), mock(InstanceId.class), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance()) {

        @Override
        ScheduledExecutorService createExecutor() {
            ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
            doAnswer(invocation -> {
                ((Runnable) invocation.getArguments()[0]).run();
                modeSwitchHappened.countDown();
                return mock(Future.class);
            }).when(executor).submit(any(Runnable.class));
            return executor;
        }
    };
    modeSwitcher.init();
    modeSwitcher.start();
    modeSwitcher.forceElections();
    reset(memberAvailability, election);
    // When
    modeSwitcher.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, mock(InstanceId.class), URI.create("http://localhost:9090?serverId=42")));
    modeSwitchHappened.await();
    modeSwitcher.forceElections();
    // Then
    InOrder inOrder = inOrder(memberAvailability, election);
    inOrder.verify(memberAvailability).memberIsUnavailable(HighAvailabilityModeSwitcher.SLAVE);
    inOrder.verify(election).performRoleElections();
    inOrder.verifyNoMoreInteractions();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) 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) 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)

Example 8 with HighAvailabilityMemberChangeEvent

use of org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent in project neo4j by neo4j.

the class HighAvailabilityModeSwitcherTest method shouldSwitchToSlaveForNullMasterAndBeSilentWhenMovingToDetached.

@Test
public void shouldSwitchToSlaveForNullMasterAndBeSilentWhenMovingToDetached() throws Throwable {
    // Given
    SwitchToSlaveCopyThenBranch sts = mock(SwitchToSlaveCopyThenBranch.class);
    SwitchToMaster stm = mock(SwitchToMaster.class);
    Election election = mock(Election.class);
    ClusterMemberAvailability cma = mock(ClusterMemberAvailability.class);
    InstanceId instanceId = new InstanceId(14);
    ComponentSwitcher componentSwitcher = mock(ComponentSwitcher.class);
    HighAvailabilityModeSwitcher theSwitcher = new HighAvailabilityModeSwitcher(sts, stm, election, cma, mock(ClusterClient.class), storeSupplierMock(), instanceId, componentSwitcher, neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
    // When
    theSwitcher.init();
    theSwitcher.start();
    theSwitcher.instanceDetached(new HighAvailabilityMemberChangeEvent(HighAvailabilityMemberState.MASTER, HighAvailabilityMemberState.PENDING, null, null));
    // Then
    verify(componentSwitcher).switchToSlave();
    verifyZeroInteractions(cma);
}
Also used : ClusterClient(org.neo4j.cluster.client.ClusterClient) SwitchToSlaveCopyThenBranch(org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch) InstanceId(org.neo4j.cluster.InstanceId) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) 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 9 with HighAvailabilityMemberChangeEvent

use of org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent in project neo4j by neo4j.

the class HighAvailabilityModeSwitcherTest method shouldBroadcastMasterIsAvailableIfMasterAndReceiveMasterIsElected.

@Test
public void shouldBroadcastMasterIsAvailableIfMasterAndReceiveMasterIsElected() throws Exception {
    // Given
    ClusterMemberAvailability availability = mock(ClusterMemberAvailability.class);
    HighAvailabilityModeSwitcher toTest = createModeSwitcher(availability);
    // When
    toTest.masterIsElected(new HighAvailabilityMemberChangeEvent(HighAvailabilityMemberState.MASTER, HighAvailabilityMemberState.MASTER, new InstanceId(2), URI.create("ha://someone")));
    // Then
    /*
           * The second argument to memberIsAvailable below is null because it has not been set yet. This would require
           * a switch to master which we don't do here.
           */
    verify(availability).memberIsAvailable(HighAvailabilityModeSwitcher.MASTER, null, StoreId.DEFAULT);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) Test(org.junit.Test)

Example 10 with HighAvailabilityMemberChangeEvent

use of org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent in project neo4j by neo4j.

the class HighAvailabilityModeSwitcherTest method shouldBroadcastSlaveIsAvailableIfSlaveAndReceivesMasterIsAvailable.

@Test
public void shouldBroadcastSlaveIsAvailableIfSlaveAndReceivesMasterIsAvailable() throws Exception {
    // Given
    ClusterMemberAvailability availability = mock(ClusterMemberAvailability.class);
    HighAvailabilityModeSwitcher toTest = createModeSwitcher(availability);
    // When
    toTest.masterIsAvailable(new HighAvailabilityMemberChangeEvent(HighAvailabilityMemberState.SLAVE, HighAvailabilityMemberState.SLAVE, new InstanceId(2), URI.create("ha://someone")));
    // Then
    /*
           * The second argument to memberIsAvailable below is null because it has not been set yet. This would require
           * a switch to master which we don't do here.
           */
    verify(availability).memberIsAvailable(HighAvailabilityModeSwitcher.SLAVE, null, StoreId.DEFAULT);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) Test(org.junit.Test)

Aggregations

HighAvailabilityMemberChangeEvent (org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent)13 Test (org.junit.Test)12 InstanceId (org.neo4j.cluster.InstanceId)11 ClusterMemberAvailability (org.neo4j.cluster.member.ClusterMemberAvailability)11 ClusterClient (org.neo4j.cluster.client.ClusterClient)7 Election (org.neo4j.cluster.protocol.election.Election)7 SwitchToMaster (org.neo4j.kernel.ha.cluster.SwitchToMaster)7 SwitchToSlaveCopyThenBranch (org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch)7 URI (java.net.URI)6 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 CancellationRequest (org.neo4j.helpers.CancellationRequest)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InOrder (org.mockito.InOrder)3 Callable (java.util.concurrent.Callable)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 Future (java.util.concurrent.Future)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2