-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise availability guard when core or read replica copy store
Instance might be forced to copy store from another instance if it requires to pull a transaction log that has already been pruned away. This could happen when transaction pulling (catch up) experience problems like network outage. Database is not operational during store copy, most components including the datasource are shutdown so no transactions can be served. However it was possible for users (bolt or embedded) to attempt start new transactions which would hang because of kernel components being stopped. In similar situations availability guard must be raised to block new transactions. This commit makes `LocalDatabase` raise availability guard when it is stopped. `LocalDatabase` is the component used by cores and read replicas to manage the lifecycle of the datasource. It is restarted during store copying.
- Loading branch information
Showing
7 changed files
with
288 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...stering/src/test/java/org/neo4j/causalclustering/catchup/storecopy/LocalDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.causalclustering.catchup.storecopy; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.time.Clock; | ||
|
||
import org.neo4j.io.fs.FileSystemAbstraction; | ||
import org.neo4j.io.pagecache.PageCache; | ||
import org.neo4j.kernel.AvailabilityGuard; | ||
import org.neo4j.kernel.impl.transaction.state.DataSourceManager; | ||
import org.neo4j.kernel.internal.DatabaseHealth; | ||
import org.neo4j.logging.NullLog; | ||
import org.neo4j.logging.NullLogProvider; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class LocalDatabaseTest | ||
{ | ||
@Test | ||
public void availabilityGuardRaisedOnCreation() throws Throwable | ||
{ | ||
AvailabilityGuard guard = newAvailabilityGuard(); | ||
assertTrue( guard.isAvailable() ); | ||
LocalDatabase localDatabase = newLocalDatabase( guard ); | ||
|
||
assertNotNull( localDatabase ); | ||
assertDatabaseIsStoppedAndUnavailable( guard ); | ||
} | ||
|
||
@Test | ||
public void availabilityGuardDroppedOnStart() throws Throwable | ||
{ | ||
AvailabilityGuard guard = newAvailabilityGuard(); | ||
assertTrue( guard.isAvailable() ); | ||
|
||
LocalDatabase localDatabase = newLocalDatabase( guard ); | ||
assertFalse( guard.isAvailable() ); | ||
|
||
localDatabase.start(); | ||
assertTrue( guard.isAvailable() ); | ||
} | ||
|
||
@Test | ||
public void availabilityGuardRaisedOnStop() throws Throwable | ||
{ | ||
AvailabilityGuard guard = newAvailabilityGuard(); | ||
assertTrue( guard.isAvailable() ); | ||
|
||
LocalDatabase localDatabase = newLocalDatabase( guard ); | ||
assertFalse( guard.isAvailable() ); | ||
|
||
localDatabase.start(); | ||
assertTrue( guard.isAvailable() ); | ||
|
||
localDatabase.stop(); | ||
assertDatabaseIsStoppedAndUnavailable( guard ); | ||
} | ||
|
||
private static LocalDatabase newLocalDatabase( AvailabilityGuard availabilityGuard ) | ||
{ | ||
return new LocalDatabase( mock( File.class ), mock( StoreFiles.class ), mock( DataSourceManager.class ), | ||
mock( PageCache.class ), mock( FileSystemAbstraction.class ), () -> mock( DatabaseHealth.class ), | ||
availabilityGuard, NullLogProvider.getInstance() ); | ||
} | ||
|
||
private static AvailabilityGuard newAvailabilityGuard() | ||
{ | ||
return new AvailabilityGuard( Clock.systemUTC(), NullLog.getInstance() ); | ||
} | ||
|
||
private static void assertDatabaseIsStoppedAndUnavailable( AvailabilityGuard guard ) | ||
{ | ||
assertFalse( guard.isAvailable() ); | ||
assertThat( guard.describeWhoIsBlocking(), containsString( "Database is stopped" ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...clustering/src/test/java/org/neo4j/causalclustering/scenarios/ReadReplicaStoreCopyIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.causalclustering.scenarios; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.neo4j.causalclustering.catchup.tx.FileCopyMonitor; | ||
import org.neo4j.causalclustering.discovery.Cluster; | ||
import org.neo4j.causalclustering.discovery.CoreClusterMember; | ||
import org.neo4j.causalclustering.discovery.ReadReplica; | ||
import org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase; | ||
import org.neo4j.graphdb.DependencyResolver; | ||
import org.neo4j.graphdb.TransactionFailureException; | ||
import org.neo4j.graphdb.factory.GraphDatabaseSettings; | ||
import org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer; | ||
import org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo; | ||
import org.neo4j.kernel.impl.transaction.log.rotation.LogRotation; | ||
import org.neo4j.kernel.monitoring.Monitors; | ||
import org.neo4j.test.causalclustering.ClusterRule; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.neo4j.kernel.configuration.Settings.FALSE; | ||
import static org.neo4j.test.assertion.Assert.assertEventually; | ||
|
||
public class ReadReplicaStoreCopyIT | ||
{ | ||
@Rule | ||
public final ClusterRule clusterRule = new ClusterRule( getClass() ) | ||
.withSharedCoreParam( GraphDatabaseSettings.keep_logical_logs, FALSE ) | ||
.withNumberOfCoreMembers( 3 ) | ||
.withNumberOfReadReplicas( 1 ); | ||
|
||
@Test( timeout = 120_000 ) | ||
public void shouldNotBePossibleToStartTransactionsWhenReadReplicaCopiesStore() throws Throwable | ||
{ | ||
Cluster cluster = clusterRule.startCluster(); | ||
|
||
ReadReplica readReplica = cluster.findAnyReadReplica(); | ||
|
||
readReplica.txPollingClient().stop(); | ||
|
||
writeSomeDataAndForceLogRotations( cluster ); | ||
Semaphore storeCopyBlockingSemaphore = addStoreCopyBlockingMonitor( readReplica ); | ||
try | ||
{ | ||
readReplica.txPollingClient().start(); | ||
waitForStoreCopyToStartAndBlock( storeCopyBlockingSemaphore ); | ||
|
||
ReadReplicaGraphDatabase replicaGraphDatabase = readReplica.database(); | ||
try | ||
{ | ||
replicaGraphDatabase.beginTx(); | ||
fail( "Exception expected" ); | ||
} | ||
catch ( Exception e ) | ||
{ | ||
assertThat( e, instanceOf( TransactionFailureException.class ) ); | ||
} | ||
} | ||
finally | ||
{ | ||
// release all waiters of the semaphore | ||
storeCopyBlockingSemaphore.release( Integer.MAX_VALUE ); | ||
} | ||
} | ||
|
||
private static void writeSomeDataAndForceLogRotations( Cluster cluster ) throws Exception | ||
{ | ||
for ( int i = 0; i < 20; i++ ) | ||
{ | ||
cluster.coreTx( ( db, tx ) -> | ||
{ | ||
db.execute( "CREATE ()" ); | ||
tx.success(); | ||
} ); | ||
|
||
forceLogRotationOnAllCores( cluster ); | ||
} | ||
} | ||
|
||
private static void forceLogRotationOnAllCores( Cluster cluster ) | ||
{ | ||
for ( CoreClusterMember core : cluster.coreMembers() ) | ||
{ | ||
forceLogRotationAndPruning( core ); | ||
} | ||
} | ||
|
||
private static void forceLogRotationAndPruning( CoreClusterMember core ) | ||
{ | ||
try | ||
{ | ||
DependencyResolver dependencyResolver = core.database().getDependencyResolver(); | ||
dependencyResolver.resolveDependency( LogRotation.class ).rotateLogFile(); | ||
SimpleTriggerInfo info = new SimpleTriggerInfo( "test" ); | ||
dependencyResolver.resolveDependency( CheckPointer.class ).forceCheckPoint( info ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new UncheckedIOException( e ); | ||
} | ||
} | ||
|
||
private static Semaphore addStoreCopyBlockingMonitor( ReadReplica readReplica ) | ||
{ | ||
DependencyResolver dependencyResolver = readReplica.database().getDependencyResolver(); | ||
Monitors monitors = dependencyResolver.resolveDependency( Monitors.class ); | ||
|
||
Semaphore semaphore = new Semaphore( 0 ); | ||
|
||
monitors.addMonitorListener( (FileCopyMonitor) file -> | ||
{ | ||
try | ||
{ | ||
semaphore.acquire(); | ||
} | ||
catch ( InterruptedException e ) | ||
{ | ||
Thread.currentThread().interrupt(); | ||
throw new RuntimeException( e ); | ||
} | ||
} ); | ||
|
||
return semaphore; | ||
} | ||
|
||
private static void waitForStoreCopyToStartAndBlock( Semaphore storeCopyBlockingSemaphore ) throws Exception | ||
{ | ||
assertEventually( "Read replica did not copy files", storeCopyBlockingSemaphore::hasQueuedThreads, | ||
is( true ), 60, TimeUnit.SECONDS ); | ||
} | ||
} |