Search in sources :

Example 1 with ComException

use of org.neo4j.com.ComException in project neo4j by neo4j.

the class HighAvailabilityModeSwitcherTest method shouldRecognizeNewMasterIfNewMasterBecameAvailableDuringSwitch.

@Test
public void shouldRecognizeNewMasterIfNewMasterBecameAvailableDuringSwitch() throws Throwable {
    // When messages coming in the following ordering, the slave should detect that the master id has changed
    // M1: Get masterIsAvailable for instance 1 at PENDING state, changing PENDING -> TO_SLAVE
    // M2: Get masterIsAvailable for instance 2 at TO_SLAVE state, changing TO_SLAVE -> TO_SLAVE
    // Given
    final CountDownLatch firstMasterAvailableHandled = new CountDownLatch(1);
    final CountDownLatch secondMasterAvailableComes = new CountDownLatch(1);
    final CountDownLatch secondMasterAvailableHandled = new CountDownLatch(1);
    SwitchToSlaveCopyThenBranch switchToSlave = mock(SwitchToSlaveCopyThenBranch.class);
    HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), mock(Election.class), mock(ClusterMemberAvailability.class), mock(ClusterClient.class), mock(Supplier.class), new InstanceId(4), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance()) {

        @Override
        ScheduledExecutorService createExecutor() {
            final ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
            final ExecutorService realExecutor = Executors.newSingleThreadExecutor();
            when(executor.submit(any(Runnable.class))).thenAnswer((Answer<Future<?>>) invocation -> realExecutor.submit((Runnable) () -> ((Runnable) invocation.getArguments()[0]).run()));
            when(executor.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))).thenAnswer((Answer<Future<?>>) invocation -> {
                realExecutor.submit((Callable<Void>) () -> {
                    firstMasterAvailableHandled.countDown();
                    secondMasterAvailableComes.await();
                    ((Runnable) invocation.getArguments()[0]).run();
                    secondMasterAvailableHandled.countDown();
                    return null;
                });
                return mock(ScheduledFuture.class);
            });
            return executor;
        }
    };
    modeSwitcher.init();
    modeSwitcher.start();
    modeSwitcher.listeningAt(URI.create("ha://server3?serverId=3"));
    // When
    // masterIsAvailable for instance 1
    URI uri1 = URI.create("ha://server1");
    // The first masterIsAvailable should fail so that the slave instance stops at TO_SLAVE state
    doThrow(new ComException("Fail to switch to slave and reschedule to retry")).when(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri1), any(CancellationRequest.class));
    modeSwitcher.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, new InstanceId(1), uri1));
    // wait until the first masterIsAvailable triggers the exception handling
    firstMasterAvailableHandled.await();
    verify(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri1), any(CancellationRequest.class));
    // masterIsAvailable for instance 2
    URI uri2 = URI.create("ha://server2");
    modeSwitcher.masterIsAvailable(new HighAvailabilityMemberChangeEvent(TO_SLAVE, TO_SLAVE, new InstanceId(2), uri2));
    secondMasterAvailableComes.countDown();
    // wait until switchToSlave method is invoked again
    secondMasterAvailableHandled.await();
    // Then
    // switchToSlave should be retried with new master id
    verify(switchToSlave).switchToSlave(any(LifeSupport.class), any(URI.class), eq(uri2), any(CancellationRequest.class));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) StoreId(org.neo4j.kernel.impl.store.StoreId) ScheduledFuture(java.util.concurrent.ScheduledFuture) NeoStoreDataSource(org.neo4j.kernel.NeoStoreDataSource) SwitchToMaster(org.neo4j.kernel.ha.cluster.SwitchToMaster) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TO_SLAVE(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberState.TO_SLAVE) Callable(java.util.concurrent.Callable) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) NullLogProvider(org.neo4j.logging.NullLogProvider) Supplier(java.util.function.Supplier) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Answer(org.mockito.stubbing.Answer) Mockito.doThrow(org.mockito.Mockito.doThrow) Future(java.util.concurrent.Future) PENDING(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberState.PENDING) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Matchers.eq(org.mockito.Matchers.eq) CancellationRequest(org.neo4j.helpers.CancellationRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Matchers.anyLong(org.mockito.Matchers.anyLong) URI(java.net.URI) MismatchingStoreIdException(org.neo4j.kernel.impl.store.MismatchingStoreIdException) ExecutorService(java.util.concurrent.ExecutorService) HighAvailabilityMemberState(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberState) InOrder(org.mockito.InOrder) ComException(org.neo4j.com.ComException) SwitchToSlaveCopyThenBranch(org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) AssertableLogProvider.inLog(org.neo4j.logging.AssertableLogProvider.inLog) Election(org.neo4j.cluster.protocol.election.Election) Mockito.when(org.mockito.Mockito.when) DataSourceManager(org.neo4j.kernel.impl.transaction.state.DataSourceManager) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) NullLogService(org.neo4j.kernel.impl.logging.NullLogService) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) Mockito.inOrder(org.mockito.Mockito.inOrder) SimpleLogService(org.neo4j.kernel.impl.logging.SimpleLogService) ClusterClient(org.neo4j.cluster.client.ClusterClient) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) ComException(org.neo4j.com.ComException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstanceId(org.neo4j.cluster.InstanceId) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) CountDownLatch(java.util.concurrent.CountDownLatch) Election(org.neo4j.cluster.protocol.election.Election) URI(java.net.URI) Callable(java.util.concurrent.Callable) ScheduledFuture(java.util.concurrent.ScheduledFuture) ClusterClient(org.neo4j.cluster.client.ClusterClient) SwitchToSlaveCopyThenBranch(org.neo4j.kernel.ha.cluster.SwitchToSlaveCopyThenBranch) HighAvailabilityMemberChangeEvent(org.neo4j.kernel.ha.cluster.HighAvailabilityMemberChangeEvent) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) Supplier(java.util.function.Supplier) SwitchToMaster(org.neo4j.kernel.ha.cluster.SwitchToMaster) CancellationRequest(org.neo4j.helpers.CancellationRequest) Test(org.junit.Test)

Example 2 with ComException

use of org.neo4j.com.ComException in project neo4j by neo4j.

the class SlaveLocksClientTest method acquireExclusiveMustThrowIfMasterThrows.

@Test(expected = DistributedLockFailureException.class)
public void acquireExclusiveMustThrowIfMasterThrows() throws Exception {
    whenMasterAcquireExclusive().thenThrow(new ComException());
    client.acquireExclusive(LockTracer.NONE, NODE, 1);
}
Also used : ComException(org.neo4j.com.ComException) Test(org.junit.Test)

Example 3 with ComException

use of org.neo4j.com.ComException in project neo4j by neo4j.

the class SlaveLocksClientTest method closeMustThrowIfMasterThrows.

@Test(expected = DistributedLockFailureException.class)
public void closeMustThrowIfMasterThrows() throws Exception {
    when(master.endLockSession(any(RequestContext.class), anyBoolean())).thenThrow(new ComException());
    // initialise
    client.acquireExclusive(LockTracer.NONE, NODE, 1);
    client.close();
}
Also used : ComException(org.neo4j.com.ComException) RequestContext(org.neo4j.com.RequestContext) Test(org.junit.Test)

Example 4 with ComException

use of org.neo4j.com.ComException in project neo4j by neo4j.

the class SlaveLocksClientTest method stopDoesNotThrowWhenMasterCommunicationThrowsComException.

@Test
public void stopDoesNotThrowWhenMasterCommunicationThrowsComException() {
    ComException error = new ComException("Communication failure");
    when(master.endLockSession(any(RequestContext.class), anyBoolean())).thenThrow(error);
    client.stop();
    logProvider.assertExactly(inLog(SlaveLocksClient.class).warn(equalTo("Unable to stop lock session on master"), CoreMatchers.<Throwable>instanceOf(DistributedLockFailureException.class)));
}
Also used : ComException(org.neo4j.com.ComException) RequestContext(org.neo4j.com.RequestContext) Test(org.junit.Test)

Example 5 with ComException

use of org.neo4j.com.ComException in project neo4j by neo4j.

the class BackupTool method executeBackup.

BackupOutcome executeBackup(HostnamePort hostnamePort, File to, ConsistencyCheck consistencyCheck, Config config, long timeout, boolean forensics) throws ToolFailureException {
    try {
        systemOut.println("Performing backup from '" + hostnamePort + "'");
        String host = hostnamePort.getHost();
        int port = hostnamePort.getPort();
        BackupOutcome outcome = backupService.doIncrementalBackupOrFallbackToFull(host, port, to, consistencyCheck, config, timeout, forensics);
        systemOut.println("Done");
        return outcome;
    } catch (UnexpectedStoreVersionException e) {
        throw new ToolFailureException(e.getMessage(), e);
    } catch (MismatchingStoreIdException e) {
        throw new ToolFailureException(String.format(MISMATCHED_STORE_ID, e.getExpected(), e.getEncountered()));
    } catch (ComException e) {
        throw new ToolFailureException("Couldn't connect to '" + hostnamePort + "'", e);
    } catch (IncrementalBackupNotPossibleException e) {
        throw new ToolFailureException(e.getMessage(), e);
    }
}
Also used : ComException(org.neo4j.com.ComException) UnexpectedStoreVersionException(org.neo4j.kernel.impl.store.UnexpectedStoreVersionException) BackupOutcome(org.neo4j.backup.BackupService.BackupOutcome) MismatchingStoreIdException(org.neo4j.kernel.impl.store.MismatchingStoreIdException)

Aggregations

ComException (org.neo4j.com.ComException)23 Test (org.junit.Test)12 RequestContext (org.neo4j.com.RequestContext)9 ZooKeeperException (org.neo4j.kernel.ha.zookeeper.ZooKeeperException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 Node (org.neo4j.graphdb.Node)2 Relationship (org.neo4j.graphdb.Relationship)2 MismatchingStoreIdException (org.neo4j.kernel.impl.store.MismatchingStoreIdException)2 URISyntaxException (java.net.URISyntaxException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1