use of org.neo4j.kernel.ha.com.master.Master in project neo4j by neo4j.
the class SlaveLocksClientTest method setUp.
@Before
public void setUp() throws Exception {
master = mock(Master.class);
availabilityGuard = new AvailabilityGuard(Clocks.fakeClock(), getInstance());
lockManager = new CommunityLockManger(Config.defaults(), Clocks.systemClock());
local = spy(lockManager.newClient());
logProvider = new AssertableLogProvider();
LockResult lockResultOk = new LockResult(LockStatus.OK_LOCKED);
TransactionStreamResponse<LockResult> responseOk = new TransactionStreamResponse<>(lockResultOk, null, TransactionStream.EMPTY, ResourceReleaser.NO_OP);
whenMasterAcquireShared().thenReturn(responseOk);
whenMasterAcquireExclusive().thenReturn(responseOk);
client = new SlaveLocksClient(master, local, lockManager, mock(RequestContextFactory.class), availabilityGuard, logProvider);
}
use of org.neo4j.kernel.ha.com.master.Master in project neo4j by neo4j.
the class HighAvailabilityMemberStateMachineTest method whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient.
@Test
public void whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient() throws Throwable {
InstanceId me = new InstanceId(1);
StoreId storeId = newStoreIdForCurrentVersion();
HighAvailabilityMemberContext context = mock(HighAvailabilityMemberContext.class);
when(context.getMyId()).thenReturn(me);
AvailabilityGuard guard = mock(AvailabilityGuard.class);
ObservedClusterMembers members = mock(ObservedClusterMembers.class);
ClusterMember masterMember = mock(ClusterMember.class);
when(masterMember.getHARole()).thenReturn("master");
when(masterMember.hasRole("master")).thenReturn(true);
when(masterMember.getInstanceId()).thenReturn(new InstanceId(2));
when(masterMember.getStoreId()).thenReturn(storeId);
ClusterMember self = new ClusterMember(me);
when(members.getMembers()).thenReturn(Arrays.asList(self, masterMember));
when(members.getCurrentMember()).thenReturn(self);
DependencyResolver dependencyResolver = mock(DependencyResolver.class);
FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
when(fs.fileExists(any(File.class))).thenReturn(true);
when(dependencyResolver.resolveDependency(FileSystemAbstraction.class)).thenReturn(fs);
when(dependencyResolver.resolveDependency(Monitors.class)).thenReturn(new Monitors());
NeoStoreDataSource dataSource = mock(NeoStoreDataSource.class);
when(dataSource.getDependencyResolver()).thenReturn(dependencyResolver);
when(dataSource.getStoreId()).thenReturn(storeId);
when(dependencyResolver.resolveDependency(NeoStoreDataSource.class)).thenReturn(dataSource);
when(dependencyResolver.resolveDependency(TransactionIdStore.class)).thenReturn(new DeadSimpleTransactionIdStore());
when(dependencyResolver.resolveDependency(ObservedClusterMembers.class)).thenReturn(members);
UpdatePuller updatePuller = mock(UpdatePuller.class);
when(updatePuller.tryPullUpdates()).thenReturn(true);
when(dependencyResolver.resolveDependency(UpdatePuller.class)).thenReturn(updatePuller);
ClusterMemberAvailability clusterMemberAvailability = mock(ClusterMemberAvailability.class);
final TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();
Election election = mock(Election.class);
HighAvailabilityMemberStateMachine stateMachine = new HighAvailabilityMemberStateMachine(context, guard, members, events, election, NullLogProvider.getInstance());
ClusterMembers clusterMembers = new ClusterMembers(members, stateMachine);
when(dependencyResolver.resolveDependency(ClusterMembers.class)).thenReturn(clusterMembers);
stateMachine.init();
stateMachine.start();
final DelegateInvocationHandler<Master> handler = new DelegateInvocationHandler<>(Master.class);
MasterClientResolver masterClientResolver = mock(MasterClientResolver.class);
MasterClient masterClient = mock(MasterClient.class);
when(masterClient.getProtocolVersion()).thenReturn(MasterClient214.PROTOCOL_VERSION);
when(masterClient.handshake(anyLong(), any(StoreId.class))).thenReturn(new Response<HandshakeResult>(new HandshakeResult(0, 42), storeId, mock(ResourceReleaser.class)) {
@Override
public void accept(Handler handler) throws IOException {
}
@Override
public boolean hasTransactionsToBeApplied() {
return false;
}
});
when(masterClient.toString()).thenReturn("TheExpectedMasterClient!");
when(masterClientResolver.instantiate(anyString(), anyInt(), anyString(), any(Monitors.class), any(StoreId.class), any(LifeSupport.class))).thenReturn(masterClient);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicBoolean switchedSuccessfully = new AtomicBoolean();
SwitchToSlave.Monitor monitor = new SwitchToSlave.Monitor() {
@Override
public void switchToSlaveCompleted(boolean wasSuccessful) {
switchedSuccessfully.set(wasSuccessful);
latch.countDown();
}
};
Config config = Config.embeddedDefaults(Collections.singletonMap(ClusterSettings.server_id.name(), me.toString()));
TransactionStats transactionCounters = mock(TransactionStats.class);
when(transactionCounters.getNumberOfActiveTransactions()).thenReturn(0L);
PageCache pageCacheMock = mock(PageCache.class);
PagedFile pagedFileMock = mock(PagedFile.class);
when(pagedFileMock.getLastPageId()).thenReturn(1L);
when(pageCacheMock.map(any(File.class), anyInt())).thenReturn(pagedFileMock);
TransactionIdStore transactionIdStoreMock = mock(TransactionIdStore.class);
when(transactionIdStoreMock.getLastCommittedTransaction()).thenReturn(new TransactionId(0, 0, 0));
SwitchToSlaveCopyThenBranch switchToSlave = new SwitchToSlaveCopyThenBranch(new File(""), NullLogService.getInstance(), mock(FileSystemAbstraction.class), config, dependencyResolver, mock(HaIdGeneratorFactory.class), handler, mock(ClusterMemberAvailability.class), mock(RequestContextFactory.class), mock(PullerFactory.class, RETURNS_MOCKS), Iterables.empty(), masterClientResolver, monitor, new StoreCopyClient.Monitor.Adapter(), Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave -> {
SlaveServer mock = mock(SlaveServer.class);
when(mock.getSocketAddress()).thenReturn(new InetSocketAddress("localhost", 123));
return mock;
}, updatePuller, pageCacheMock, mock(Monitors.class), transactionCounters);
ComponentSwitcherContainer switcherContainer = new ComponentSwitcherContainer();
HighAvailabilityModeSwitcher haModeSwitcher = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), election, clusterMemberAvailability, mock(ClusterClient.class), storeSupplierMock(), me, switcherContainer, neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
haModeSwitcher.init();
haModeSwitcher.start();
haModeSwitcher.listeningAt(URI.create("http://localhost:12345"));
stateMachine.addHighAvailabilityMemberListener(haModeSwitcher);
final AtomicReference<Master> ref = new AtomicReference<>(null);
//noinspection unchecked
AbstractComponentSwitcher<Object> otherModeSwitcher = new AbstractComponentSwitcher<Object>(mock(DelegateInvocationHandler.class)) {
@Override
protected Object getSlaveImpl() {
Master master = handler.cement();
ref.set(master);
latch.countDown();
return null;
}
@Override
protected Object getMasterImpl() {
return null;
}
};
switcherContainer.add(otherModeSwitcher);
// When
events.switchToSlave(me);
// Then
latch.await();
assertTrue("mode switch failed", switchedSuccessfully.get());
Master actual = ref.get();
// let's test the toString()s since there are too many wrappers of proxies
assertEquals(masterClient.toString(), actual.toString());
stateMachine.stop();
stateMachine.shutdown();
haModeSwitcher.stop();
haModeSwitcher.shutdown();
}
use of org.neo4j.kernel.ha.com.master.Master in project neo4j by neo4j.
the class SlaveTransactionCommitProcessTest method setUp.
@Before
public void setUp() {
lastSeenEventIdentifier = new AtomicInteger(-1);
master = mock(Master.class);
requestContext = new RequestContext(10, 11, 12, 13, 14);
reqFactory = new ConstantRequestContextFactory(requestContext) {
@Override
public RequestContext newRequestContext(int eventIdentifier) {
lastSeenEventIdentifier.set(eventIdentifier);
return super.newRequestContext(eventIdentifier);
}
};
response = new LongResponse(42L);
tx = new PhysicalTransactionRepresentation(Collections.<StorageCommand>emptyList());
tx.setHeader(new byte[] {}, 1, 1, 1, 1, 1, 1337);
commitProcess = new SlaveTransactionCommitProcess(master, reqFactory);
}
use of org.neo4j.kernel.ha.com.master.Master in project neo4j by neo4j.
the class SwitchToMaster method switchToMaster.
/**
* Performs a switch to the master state. Starts communication endpoints, switches components to the master state
* and broadcasts the appropriate Master Is Available event.
* @param haCommunicationLife The LifeSupport instance to register communication endpoints.
* @param me The URI that the communication endpoints should bind to
* @return The URI at which the master communication was bound.
*/
public URI switchToMaster(LifeSupport haCommunicationLife, URI me) {
userLog.info("I am %s, moving to master", myId(config));
// Do not wait for currently active transactions to complete before continuing switching.
// - A master in a cluster is very important, without it the cluster cannot process any write requests
// - Awaiting open transactions to complete assumes that this instance just now was a slave that is
// switching to master, which means the previous master where these active transactions were hosted
// is no longer available so these open transactions cannot continue and complete anyway,
// so what's the point waiting for them?
// - Read transactions may still be able to complete, but the correct response to failures in those
// is to have them throw transient error exceptions hinting that they should be retried,
// at which point they may get redirected to another instance, or to this instance if it has completed
// the switch until then.
idGeneratorFactory.switchToMaster();
NeoStoreDataSource neoStoreXaDataSource = dataSourceSupplier.get();
neoStoreXaDataSource.afterModeSwitch();
ConversationManager conversationManager = conversationManagerFactory.newInstance();
Master master = masterFactory.apply(conversationManager, haCommunicationLife);
MasterServer masterServer = masterServerFactory.apply(master, conversationManager);
haCommunicationLife.add(masterServer);
masterDelegateHandler.setDelegate(master);
haCommunicationLife.start();
URI masterHaURI = getMasterUri(me, masterServer, config);
clusterMemberAvailability.memberIsAvailable(MASTER, masterHaURI, neoStoreXaDataSource.getStoreId());
userLog.info("I am %s, successfully moved to master", myId(config));
slaveFactorySupplier.get().setStoreId(neoStoreXaDataSource.getStoreId());
return masterHaURI;
}
use of org.neo4j.kernel.ha.com.master.Master in project neo4j by neo4j.
the class SlaveLockManagerTest method setUp.
@Before
public void setUp() {
requestContextFactory = new RequestContextFactory(1, singleton(mock(TransactionIdStore.class)));
master = mock(Master.class);
availabilityGuard = new AvailabilityGuard(Clocks.systemClock(), getInstance());
}
Aggregations