Search in sources :

Example 1 with CoreSnapshot

use of org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot in project neo4j by neo4j.

the class ClusterBinderTest method shouldBootstrapWhenBootstrappable.

@Test
public void shouldBootstrapWhenBootstrappable() throws Throwable {
    // given
    CoreTopology bootstrappableTopology = new CoreTopology(null, true, emptyMap());
    CoreTopologyService topologyService = mock(CoreTopologyService.class);
    when(topologyService.coreServers()).thenReturn(bootstrappableTopology);
    when(topologyService.setClusterId(any())).thenReturn(true);
    ClusterBinder binder = new ClusterBinder(new StubClusterIdStorage(), topologyService, NullLogProvider.getInstance(), clock, () -> clock.forward(1, TimeUnit.SECONDS), 3_000, coreBootstrapper);
    ThrowingConsumer<CoreSnapshot, Throwable> snapshotInstaller = mock(ThrowingConsumer.class);
    // when
    binder.bindToCluster(snapshotInstaller);
    // then
    verify(coreBootstrapper).bootstrap(any());
    Optional<ClusterId> clusterId = binder.get();
    assertTrue(clusterId.isPresent());
    verify(topologyService).setClusterId(clusterId.get());
    verify(snapshotInstaller).accept(any());
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Test(org.junit.Test)

Example 2 with CoreSnapshot

use of org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot in project neo4j by neo4j.

the class ClusterBinder method bindToCluster.

/**
     * The cluster binding process tries to establish a common cluster ID. If there is no common cluster ID
     * then a single instance will eventually create one and publish it through the underlying topology service.
     *
     * @throws IOException If there is an issue with I/O.
     * @throws InterruptedException If the process gets interrupted.
     * @throws TimeoutException If the process times out.
     */
public void bindToCluster(ThrowingConsumer<CoreSnapshot, Throwable> snapshotInstaller) throws Throwable {
    if (clusterIdStorage.exists()) {
        clusterId = clusterIdStorage.readState();
        publishClusterId(clusterId);
        log.info("Already bound to cluster: " + clusterId);
        return;
    }
    CoreTopology topology;
    long endTime = clock.millis() + timeoutMillis;
    do {
        topology = topologyService.coreServers();
        if (topology.clusterId() != null) {
            clusterId = topology.clusterId();
            log.info("Bound to cluster: " + clusterId);
        } else if (topology.canBeBootstrapped()) {
            clusterId = new ClusterId(UUID.randomUUID());
            CoreSnapshot snapshot = coreBootstrapper.bootstrap(topology.members().keySet());
            log.info(String.format("Bootstrapped with snapshot: %s and clusterId: %s", snapshot, clusterId));
            snapshotInstaller.accept(snapshot);
            publishClusterId(clusterId);
        } else {
            retryWaiter.apply();
        }
    } while (clusterId == null && clock.millis() < endTime);
    if (clusterId == null) {
        throw new TimeoutException(String.format("Failed to join a cluster with members %s. Another member should have published " + "a clusterId but none was detected. Please restart the cluster.", topology));
    }
    clusterIdStorage.writeState(clusterId);
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with CoreSnapshot

use of org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot in project neo4j by neo4j.

the class CommandApplicationProcess method snapshot.

public synchronized CoreSnapshot snapshot(RaftMachine raft) throws IOException, InterruptedException {
    applier.sync(false);
    long prevIndex = lastApplied;
    long prevTerm = raftLog.readEntryTerm(prevIndex);
    CoreSnapshot coreSnapshot = new CoreSnapshot(prevIndex, prevTerm);
    coreStateMachines.addSnapshots(coreSnapshot);
    coreSnapshot.add(CoreStateType.SESSION_TRACKER, sessionTracker.snapshot());
    coreSnapshot.add(CoreStateType.RAFT_CORE_STATE, raft.coreState());
    return coreSnapshot;
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot)

Example 4 with CoreSnapshot

use of org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot in project neo4j by neo4j.

the class CoreBootstrapper method bootstrap.

public CoreSnapshot bootstrap(Set<MemberId> members) throws IOException {
    StoreFactory factory = new StoreFactory(storeDir, config, new DefaultIdGeneratorFactory(fs), pageCache, fs, logProvider);
    NeoStores neoStores = factory.openAllNeoStores(true);
    neoStores.close();
    CoreSnapshot coreSnapshot = new CoreSnapshot(FIRST_INDEX, FIRST_TERM);
    coreSnapshot.add(CoreStateType.ID_ALLOCATION, deriveIdAllocationState(storeDir));
    coreSnapshot.add(CoreStateType.LOCK_TOKEN, new ReplicatedLockTokenState());
    coreSnapshot.add(CoreStateType.RAFT_CORE_STATE, new RaftCoreState(new MembershipEntry(FIRST_INDEX, members)));
    coreSnapshot.add(CoreStateType.SESSION_TRACKER, new GlobalSessionTrackerState());
    appendNullTransactionLogEntryToSetRaftIndexToMinusOne();
    return coreSnapshot;
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) NeoStores(org.neo4j.kernel.impl.store.NeoStores) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState)

Example 5 with CoreSnapshot

use of org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot in project neo4j by neo4j.

the class CoreBootstrapperTest method shouldSetAllCoreState.

@Test
public void shouldSetAllCoreState() throws Exception {
    // given
    int nodeCount = 100;
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    File classicNeo4jStore = RestoreClusterUtils.createClassicNeo4jStore(testDirectory.directory(), fileSystem, nodeCount, Standard.LATEST_NAME);
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    CoreBootstrapper bootstrapper = new CoreBootstrapper(classicNeo4jStore, pageCache, fileSystem, Config.defaults(), NullLogProvider.getInstance());
    // when
    Set<MemberId> membership = asSet(randomMember(), randomMember(), randomMember());
    CoreSnapshot snapshot = bootstrapper.bootstrap(membership);
    // then
    assertEquals(nodeCount, ((IdAllocationState) snapshot.get(CoreStateType.ID_ALLOCATION)).firstUnallocated(IdType.NODE));
    /* Bootstrapped state is created in RAFT land at index -1 and term -1. */
    assertEquals(0, snapshot.prevIndex());
    assertEquals(0, snapshot.prevTerm());
    /* Lock is initially not taken. */
    assertEquals(new ReplicatedLockTokenState(), snapshot.get(CoreStateType.LOCK_TOKEN));
    /* Raft has the bootstrapped set of members initially. */
    assertEquals(membership, ((RaftCoreState) snapshot.get(CoreStateType.RAFT_CORE_STATE)).committed().members());
    /* The session state is initially empty. */
    assertEquals(new GlobalSessionTrackerState(), snapshot.get(CoreStateType.SESSION_TRACKER));
    LastCommittedIndexFinder lastCommittedIndexFinder = new LastCommittedIndexFinder(new ReadOnlyTransactionIdStore(pageCache, classicNeo4jStore), new ReadOnlyTransactionStore(pageCache, fileSystem, classicNeo4jStore, new Monitors()), NullLogProvider.getInstance());
    long lastCommittedIndex = lastCommittedIndexFinder.getLastCommittedIndex();
    assertEquals(-1, lastCommittedIndex);
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) LastCommittedIndexFinder(org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Monitors(org.neo4j.kernel.monitoring.Monitors) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Aggregations

CoreSnapshot (org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot)5 Test (org.junit.Test)2 GlobalSessionTrackerState (org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState)2 ReplicatedLockTokenState (org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState)2 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)2 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)2 File (java.io.File)1 TimeoutException (java.util.concurrent.TimeoutException)1 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)1 LastCommittedIndexFinder (org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder)1 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)1 PageCache (org.neo4j.io.pagecache.PageCache)1 NeoStores (org.neo4j.kernel.impl.store.NeoStores)1 StoreFactory (org.neo4j.kernel.impl.store.StoreFactory)1 DefaultIdGeneratorFactory (org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory)1 ReadOnlyTransactionIdStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore)1 ReadOnlyTransactionStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)1 Monitors (org.neo4j.kernel.monitoring.Monitors)1