use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldUseProperServerIdWhenDemotingFromMasterOnException.
@Test
public void shouldUseProperServerIdWhenDemotingFromMasterOnException() throws Throwable {
/*
* This a test that acts as a driver to prove a bug which had an instance send out a demote message
* with instance id -1, since it used HAMS#getServerId(URI) with a URI coming from the NetworkReceiver binding
* which did not contain the serverId URI argument. This has been fixed by explicitly adding the instanceid
* as a constructor argument of the HAMS.
*/
// Given
SwitchToSlaveCopyThenBranch sts = mock(SwitchToSlaveCopyThenBranch.class);
SwitchToMaster stm = mock(SwitchToMaster.class);
// this is necessary to trigger a revert which uses the serverId from the HAMS#me field
when(stm.switchToMaster(any(LifeSupport.class), any(URI.class))).thenThrow(new RuntimeException());
Election election = mock(Election.class);
ClusterMemberAvailability cma = mock(ClusterMemberAvailability.class);
InstanceId instanceId = new InstanceId(14);
HighAvailabilityModeSwitcher theSwitcher = new HighAvailabilityModeSwitcher(sts, stm, election, cma, mock(ClusterClient.class), storeSupplierMock(), instanceId, new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
theSwitcher.init();
theSwitcher.start();
/*
* This is the trick, kind of. NetworkReceiver creates this and passes it on to NetworkReceiver#getURI() and
* that
* is what HAMS uses as the HAMS#me field value. But we should not be using this to extract the instanceId.
* Note the lack of a serverId argument
*/
URI listeningAt = URI.create("ha://0.0.0.0:5001?name=someName");
theSwitcher.listeningAt(listeningAt);
// When
try {
// the instance fails to switch to master
theSwitcher.masterIsElected(new HighAvailabilityMemberChangeEvent(HighAvailabilityMemberState.PENDING, HighAvailabilityMemberState.TO_MASTER, instanceId, listeningAt));
} finally {
theSwitcher.stop();
theSwitcher.shutdown();
}
// Then
// The demotion message must have used the proper instance id
verify(election).demote(instanceId);
}
use of org.neo4j.cluster.InstanceId 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));
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class SwitchToSlaveCopyThenBranchTest method newSwitchToSlaveSpy.
@SuppressWarnings("unchecked")
private SwitchToSlaveCopyThenBranch newSwitchToSlaveSpy(PageCache pageCacheMock, StoreCopyClient storeCopyClient) throws IOException {
ClusterMembers clusterMembers = mock(ClusterMembers.class);
ClusterMember master = mock(ClusterMember.class);
when(master.getStoreId()).thenReturn(storeId);
when(master.getHARole()).thenReturn(HighAvailabilityModeSwitcher.MASTER);
when(master.hasRole(eq(HighAvailabilityModeSwitcher.MASTER))).thenReturn(true);
when(master.getInstanceId()).thenReturn(new InstanceId(1));
when(clusterMembers.getMembers()).thenReturn(asList(master));
Dependencies resolver = new Dependencies();
resolver.satisfyDependencies(requestContextFactory, clusterMembers, mock(TransactionObligationFulfiller.class), mock(OnlineBackupKernelExtension.class), mock(IndexConfigStore.class), mock(TransactionCommittingResponseUnpacker.class), mock(DataSourceManager.class), mock(StoreLockerLifecycleAdapter.class), mock(FileSystemWatcherService.class));
NeoStoreDataSource dataSource = mock(NeoStoreDataSource.class);
when(dataSource.getStoreId()).thenReturn(storeId);
TransactionStats transactionCounters = mock(TransactionStats.class);
when(transactionCounters.getNumberOfActiveTransactions()).thenReturn(0L);
Response<HandshakeResult> response = mock(Response.class);
when(response.response()).thenReturn(new HandshakeResult(42, 2));
when(masterClient.handshake(anyLong(), any(StoreId.class))).thenReturn(response);
when(masterClient.getProtocolVersion()).thenReturn(MasterClient.CURRENT);
TransactionIdStore transactionIdStoreMock = mock(TransactionIdStore.class);
// note that the checksum (the second member of the array) is the same as the one in the handshake mock above
when(transactionIdStoreMock.getLastCommittedTransaction()).thenReturn(new TransactionId(42, 42, 42));
MasterClientResolver masterClientResolver = mock(MasterClientResolver.class);
when(masterClientResolver.instantiate(anyString(), anyInt(), anyString(), any(Monitors.class), any(StoreId.class), any(LifeSupport.class))).thenReturn(masterClient);
return spy(new SwitchToSlaveCopyThenBranch(new File(""), NullLogService.getInstance(), configMock(), resolver, mock(HaIdGeneratorFactory.class), mock(DelegateInvocationHandler.class), mock(ClusterMemberAvailability.class), requestContextFactory, pullerFactory, masterClientResolver, mock(SwitchToSlave.Monitor.class), storeCopyClient, Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave -> {
SlaveServer server = mock(SlaveServer.class);
InetSocketAddress inetSocketAddress = InetSocketAddress.createUnresolved("localhost", 42);
when(server.getSocketAddress()).thenReturn(inetSocketAddress);
return server;
}, updatePuller, pageCacheMock, mock(Monitors.class), transactionCounters));
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldTakeNoActionIfSwitchingToSlaveForItselfAsMaster.
@Test
public void shouldTakeNoActionIfSwitchingToSlaveForItselfAsMaster() throws Throwable {
// Given
// A HAMS
SwitchToSlaveCopyThenBranch switchToSlave = mock(SwitchToSlaveCopyThenBranch.class);
AssertableLogProvider logProvider = new AssertableLogProvider();
SimpleLogService logService = new SimpleLogService(NullLogProvider.getInstance(), logProvider);
HighAvailabilityModeSwitcher toTest = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), mock(Election.class), mock(ClusterMemberAvailability.class), mock(ClusterClient.class), storeSupplierMock(), new InstanceId(2), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), logService);
// That is properly started
toTest.init();
toTest.start();
/*
* This is the URI at which we are registered as server - includes our own id, but we don't necessarily listen
* there
*/
URI serverHaUri = URI.create("ha://server2?serverId=2");
toTest.listeningAt(serverHaUri);
// When
// The HAMS tries to switch to slave for a master that is itself
toTest.masterIsAvailable(new HighAvailabilityMemberChangeEvent(PENDING, TO_SLAVE, new InstanceId(2), serverHaUri));
// Then
// No switching to slave must happen
verifyZeroInteractions(switchToSlave);
// And an error must be logged
logProvider.assertAtLeastOnce(inLog(HighAvailabilityModeSwitcher.class).error("I (ha://server2?serverId=2) tried to switch to " + "slave for myself as master (ha://server2?serverId=2)"));
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HighAvailabilityModeSwitcherTest method shouldNotBroadcastIfSlaveAndReceivesMasterIsElected.
@Test
public void shouldNotBroadcastIfSlaveAndReceivesMasterIsElected() throws Exception {
// Given
ClusterMemberAvailability availability = mock(ClusterMemberAvailability.class);
HighAvailabilityModeSwitcher toTest = createModeSwitcher(availability);
// When
toTest.masterIsElected(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.
*/
verifyZeroInteractions(availability);
}
Aggregations