Search in sources :

Example 11 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class HighAvailabilityMemberStateMachineTest method whenInSlaveStateLosingOtherSlaveShouldNotPutInPending.

@Test
public void whenInSlaveStateLosingOtherSlaveShouldNotPutInPending() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId master = new InstanceId(2);
    InstanceId otherSlave = new InstanceId(3);
    HighAvailabilityMemberContext context = new SimpleHighAvailabilityMemberContext(me, false);
    AvailabilityGuard guard = mock(AvailabilityGuard.class);
    ObservedClusterMembers members = mockClusterMembers(me, singletonList(master), singletonList(otherSlave));
    ClusterMemberEvents events = mock(ClusterMemberEvents.class);
    ClusterMemberListenerContainer memberListenerContainer = mockAddClusterMemberListener(events);
    HighAvailabilityMemberStateMachine stateMachine = buildMockedStateMachine(context, events, members, guard);
    stateMachine.init();
    ClusterMemberListener memberListener = memberListenerContainer.get();
    HAStateChangeListener probe = new HAStateChangeListener();
    stateMachine.addHighAvailabilityMemberListener(probe);
    // Send it to MASTER
    memberListener.memberIsAvailable(MASTER, master, URI.create("ha://whatever"), StoreId.DEFAULT);
    memberListener.memberIsAvailable(SLAVE, me, URI.create("ha://whatever3"), StoreId.DEFAULT);
    memberListener.memberIsAvailable(SLAVE, otherSlave, URI.create("ha://whatever2"), StoreId.DEFAULT);
    assertThat(stateMachine.getCurrentState(), equalTo(HighAvailabilityMemberState.SLAVE));
    // When
    memberListener.memberIsFailed(otherSlave);
    // Then
    assertThat(stateMachine.getCurrentState(), equalTo(HighAvailabilityMemberState.SLAVE));
    assertThat(probe.instanceStops, is(false));
}
Also used : ObservedClusterMembers(org.neo4j.kernel.ha.cluster.member.ObservedClusterMembers) ClusterMemberEvents(org.neo4j.cluster.member.ClusterMemberEvents) InstanceId(org.neo4j.cluster.InstanceId) ClusterMemberListener(org.neo4j.cluster.member.ClusterMemberListener) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) Test(org.junit.Test)

Example 12 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class HighAvailabilityMemberStateMachineTest method whenInMasterStateLosingQuorumFromTwoInstancesShouldRemainMaster.

@Test
public void whenInMasterStateLosingQuorumFromTwoInstancesShouldRemainMaster() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId other = new InstanceId(2);
    HighAvailabilityMemberContext context = new SimpleHighAvailabilityMemberContext(me, false);
    AvailabilityGuard guard = mock(AvailabilityGuard.class);
    ObservedClusterMembers members = mockClusterMembers(me, emptyList(), singletonList(other));
    ClusterMemberEvents events = mock(ClusterMemberEvents.class);
    ClusterMemberListenerContainer memberListenerContainer = mockAddClusterMemberListener(events);
    HighAvailabilityMemberStateMachine stateMachine = buildMockedStateMachine(context, events, members, guard);
    stateMachine.init();
    ClusterMemberListener memberListener = memberListenerContainer.get();
    HAStateChangeListener probe = new HAStateChangeListener();
    stateMachine.addHighAvailabilityMemberListener(probe);
    // Send it to MASTER
    memberListener.coordinatorIsElected(me);
    memberListener.memberIsAvailable(MASTER, me, URI.create("ha://whatever"), StoreId.DEFAULT);
    assertThat(stateMachine.getCurrentState(), equalTo(HighAvailabilityMemberState.MASTER));
    // When
    memberListener.memberIsFailed(new InstanceId(2));
    // Then
    assertThat(stateMachine.getCurrentState(), equalTo(HighAvailabilityMemberState.MASTER));
    assertThat(probe.instanceStops, is(false));
    assertThat(probe.instanceDetached, is(false));
}
Also used : ObservedClusterMembers(org.neo4j.kernel.ha.cluster.member.ObservedClusterMembers) ClusterMemberEvents(org.neo4j.cluster.member.ClusterMemberEvents) InstanceId(org.neo4j.cluster.InstanceId) ClusterMemberListener(org.neo4j.cluster.member.ClusterMemberListener) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) Test(org.junit.Test)

Example 13 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class HighAvailabilityMemberStateMachineTest method mockClusterMembers.

private ObservedClusterMembers mockClusterMembers(InstanceId me, List<InstanceId> alive, List<InstanceId> failed) {
    ObservedClusterMembers members = mock(ObservedClusterMembers.class);
    // we cannot set outside of the package the isAlive to return false. So do it with a mock
    List<ClusterMember> aliveMembers = new ArrayList<>(alive.size());
    List<ClusterMember> failedMembers = new ArrayList<>(failed.size());
    for (InstanceId instanceId : alive) {
        ClusterMember otherMember = mock(ClusterMember.class);
        when(otherMember.getInstanceId()).thenReturn(instanceId);
        // the failed argument tells us which instance should be marked as failed
        when(otherMember.isAlive()).thenReturn(true);
        aliveMembers.add(otherMember);
    }
    for (InstanceId instanceId : failed) {
        ClusterMember otherMember = mock(ClusterMember.class);
        when(otherMember.getInstanceId()).thenReturn(instanceId);
        // the failed argument tells us which instance should be marked as failed
        when(otherMember.isAlive()).thenReturn(false);
        failedMembers.add(otherMember);
    }
    ClusterMember thisMember = new ClusterMember(me);
    aliveMembers.add(thisMember);
    List<ClusterMember> allMembers = new ArrayList<>();
    // thisMember is in aliveMembers
    allMembers.addAll(aliveMembers);
    allMembers.addAll(failedMembers);
    when(members.getMembers()).thenReturn(allMembers);
    when(members.getAliveMembers()).thenReturn(aliveMembers);
    return members;
}
Also used : ObservedClusterMembers(org.neo4j.kernel.ha.cluster.member.ObservedClusterMembers) ClusterMember(org.neo4j.kernel.ha.cluster.member.ClusterMember) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList)

Example 14 with InstanceId

use of org.neo4j.cluster.InstanceId 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();
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) MasterClient(org.neo4j.kernel.ha.com.slave.MasterClient) InetSocketAddress(java.net.InetSocketAddress) DeadSimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) ObservedClusterMembers(org.neo4j.kernel.ha.cluster.member.ObservedClusterMembers) RequestContextFactory(org.neo4j.kernel.ha.com.RequestContextFactory) PageCache(org.neo4j.io.pagecache.PageCache) HighAvailabilityModeSwitcher(org.neo4j.kernel.ha.cluster.modeswitch.HighAvailabilityModeSwitcher) DeadSimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) InstanceId(org.neo4j.cluster.InstanceId) NeoStoreDataSource(org.neo4j.kernel.NeoStoreDataSource) DelegateInvocationHandler(org.neo4j.kernel.ha.DelegateInvocationHandler) DelegateInvocationHandler(org.neo4j.kernel.ha.DelegateInvocationHandler) DependencyResolver(org.neo4j.graphdb.DependencyResolver) TransactionId(org.neo4j.kernel.impl.store.TransactionId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Monitors(org.neo4j.kernel.monitoring.Monitors) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) HaIdGeneratorFactory(org.neo4j.kernel.ha.id.HaIdGeneratorFactory) HandshakeResult(org.neo4j.kernel.ha.com.master.HandshakeResult) Config(org.neo4j.kernel.configuration.Config) ClusterMemberAvailability(org.neo4j.cluster.member.ClusterMemberAvailability) ClusterMembers(org.neo4j.kernel.ha.cluster.member.ClusterMembers) ObservedClusterMembers(org.neo4j.kernel.ha.cluster.member.ObservedClusterMembers) SlaveServer(org.neo4j.kernel.ha.com.slave.SlaveServer) ClusterMember(org.neo4j.kernel.ha.cluster.member.ClusterMember) ClusterClient(org.neo4j.cluster.client.ClusterClient) StoreId(org.neo4j.kernel.impl.store.StoreId) AbstractComponentSwitcher(org.neo4j.kernel.ha.cluster.modeswitch.AbstractComponentSwitcher) UpdatePuller(org.neo4j.kernel.ha.UpdatePuller) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) PullerFactory(org.neo4j.kernel.ha.PullerFactory) PagedFile(org.neo4j.io.pagecache.PagedFile) ComponentSwitcherContainer(org.neo4j.kernel.ha.cluster.modeswitch.ComponentSwitcherContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Election(org.neo4j.cluster.protocol.election.Election) MasterClientResolver(org.neo4j.kernel.ha.com.slave.MasterClientResolver) Master(org.neo4j.kernel.ha.com.master.Master) TransactionStats(org.neo4j.kernel.impl.transaction.TransactionStats) Test(org.junit.Test)

Example 15 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class ClusterTopologyChangesIT method failedInstanceShouldReceiveCorrectCoordinatorIdUponRejoiningCluster.

@Test
public void failedInstanceShouldReceiveCorrectCoordinatorIdUponRejoiningCluster() throws Throwable {
    // Given
    HighlyAvailableGraphDatabase initialMaster = cluster.getMaster();
    // When
    cluster.shutdown(initialMaster);
    cluster.await(masterAvailable(initialMaster));
    cluster.await(masterSeesSlavesAsAvailable(1));
    // create node on new master to ensure that it has the greatest tx id
    createNodeOn(cluster.getMaster());
    cluster.sync();
    LifeSupport life = new LifeSupport();
    ClusterClientModule clusterClient = newClusterClient(life, new InstanceId(1));
    cleanup.add(life);
    final AtomicReference<InstanceId> coordinatorIdWhenReJoined = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    clusterClient.clusterClient.addClusterListener(new ClusterListener.Adapter() {

        @Override
        public void enteredCluster(ClusterConfiguration clusterConfiguration) {
            coordinatorIdWhenReJoined.set(clusterConfiguration.getElected(COORDINATOR));
            latch.countDown();
        }
    });
    life.start();
    // Then
    assertTrue(latch.await(20, SECONDS));
    assertEquals(new InstanceId(2), coordinatorIdWhenReJoined.get());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterClientModule(org.neo4j.cluster.client.ClusterClientModule) Test(org.junit.Test)

Aggregations

InstanceId (org.neo4j.cluster.InstanceId)154 Test (org.junit.Test)129 URI (java.net.URI)47 Config (org.neo4j.kernel.configuration.Config)37 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)36 Timeouts (org.neo4j.cluster.timeout.Timeouts)32 Executor (java.util.concurrent.Executor)28 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)28 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)27 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)27 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)27 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)24 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)18 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)16 Message (org.neo4j.cluster.com.message.Message)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ClusterMemberAvailability (org.neo4j.cluster.member.ClusterMemberAvailability)13 ClusterMemberEvents (org.neo4j.cluster.member.ClusterMemberEvents)12 ClusterMemberListener (org.neo4j.cluster.member.ClusterMemberListener)12