Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Indexing service lifecycle to shutdown indexes on shutdown ins…
…tead of stop During database shutdown we stop life components, wait of all running transaction to finish and then shutdown everything. To be able to finish all transactions that can be potentially still be there we need to keep indexes open till we reach shutdown phase. Update NeoStoreDataSource to provide test access to internal life.
- Loading branch information
1 parent
9e87171
commit 21ada97
Showing
5 changed files
with
168 additions
and
15 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
145 changes: 145 additions & 0 deletions
145
community/neo4j/src/test/java/org/neo4j/index/ShutdownOnIndexUpdateIT.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,145 @@ | |||
/* | |||
* Copyright (c) 2002-2016 "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 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 General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
package org.neo4j.index; | |||
|
|||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
|
|||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.atomic.AtomicLong; | |||
|
|||
import org.neo4j.graphdb.DependencyResolver; | |||
import org.neo4j.graphdb.GraphDatabaseService; | |||
import org.neo4j.graphdb.Label; | |||
import org.neo4j.graphdb.Node; | |||
import org.neo4j.graphdb.Transaction; | |||
import org.neo4j.graphdb.factory.GraphDatabaseFactory; | |||
import org.neo4j.graphdb.schema.Schema; | |||
import org.neo4j.kernel.NeoStoreDataSource; | |||
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine; | |||
import org.neo4j.kernel.internal.GraphDatabaseAPI; | |||
import org.neo4j.kernel.lifecycle.LifeSupport; | |||
import org.neo4j.kernel.lifecycle.LifecycleListener; | |||
import org.neo4j.kernel.lifecycle.LifecycleStatus; | |||
import org.neo4j.test.rule.CleanupRule; | |||
import org.neo4j.test.rule.TestDirectory; | |||
|
|||
import static org.junit.Assert.assertTrue; | |||
|
|||
public class ShutdownOnIndexUpdateIT | |||
{ | |||
private static final String UNIQUE_PROPERTY_NAME = "uniquePropertyName"; | |||
private static final AtomicLong indexProvider = new AtomicLong(); | |||
private static Label constraintIndexLabel = Label.label( "ConstraintIndexLabel" ); | |||
|
|||
@Rule | |||
public TestDirectory testDirectory = TestDirectory.testDirectory(); | |||
@Rule | |||
public CleanupRule cleanupRule = new CleanupRule(); | |||
|
|||
private ExecutorService executors; | |||
|
|||
@Before | |||
public void setUp() | |||
{ | |||
executors = Executors.newCachedThreadPool(); | |||
} | |||
|
|||
@After | |||
public void tearDown() | |||
{ | |||
executors.shutdown(); | |||
} | |||
|
|||
@Test | |||
public void shutdownWhileFinishingTransactionWithIndexUpdates() throws Exception | |||
{ | |||
GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase( testDirectory.graphDbDir() ); | |||
cleanupRule.add( database ); | |||
|
|||
createConstraint( database ); | |||
waitIndexesOnline( database ); | |||
|
|||
try ( Transaction transaction = database.beginTx() ) | |||
{ | |||
Node node = database.createNode( constraintIndexLabel ); | |||
node.setProperty( UNIQUE_PROPERTY_NAME, indexProvider.getAndIncrement() ); | |||
|
|||
DependencyResolver dependencyResolver = ((GraphDatabaseAPI) database).getDependencyResolver(); | |||
NeoStoreDataSource dataSource = dependencyResolver.resolveDependency( NeoStoreDataSource.class ); | |||
LifeSupport dataSourceLife = dataSource.getLife(); | |||
TransactionCloseListener closeListener = new TransactionCloseListener( transaction ); | |||
dataSourceLife.addLifecycleListener( closeListener ); | |||
dataSource.stop(); | |||
|
|||
assertTrue( "Transaction should be closed and no exception should be thrown.", | |||
closeListener.isTransactionClosed() ); | |||
} | |||
} | |||
|
|||
private void waitIndexesOnline( GraphDatabaseService database ) | |||
{ | |||
try ( Transaction ignored = database.beginTx() ) | |||
{ | |||
database.schema().awaitIndexesOnline( 5, TimeUnit.MINUTES ); | |||
} | |||
} | |||
|
|||
private void createConstraint( GraphDatabaseService database ) | |||
{ | |||
try ( Transaction transaction = database.beginTx() ) | |||
{ | |||
Schema schema = database.schema(); | |||
schema.constraintFor( constraintIndexLabel ).assertPropertyIsUnique( UNIQUE_PROPERTY_NAME ).create(); | |||
transaction.success(); | |||
} | |||
} | |||
|
|||
private static class TransactionCloseListener implements LifecycleListener | |||
{ | |||
private final Transaction transaction; | |||
private boolean transactionClosed = false; | |||
|
|||
TransactionCloseListener( Transaction transaction ) | |||
{ | |||
this.transaction = transaction; | |||
} | |||
|
|||
@Override | |||
public void notifyStatusChanged( Object instance, LifecycleStatus from, LifecycleStatus to ) | |||
{ | |||
if ( (LifecycleStatus.STOPPED == to) && (instance instanceof RecordStorageEngine) ) | |||
{ | |||
transaction.success(); | |||
transaction.close(); | |||
transactionClosed = true; | |||
} | |||
} | |||
|
|||
boolean isTransactionClosed() | |||
{ | |||
return transactionClosed; | |||
} | |||
} | |||
} |