Browse files

Branched data directories end up in <storeDir>/branched/ sub directory

  • Loading branch information...
1 parent e11a5cf commit 1d98ffe48ce6bc7905c51e0df34c1e2ae9636952 @tinwelint committed Apr 10, 2012
View
36 ha/src/main/java/org/neo4j/kernel/BranchedStoreBean.java
@@ -21,11 +21,12 @@
package org.neo4j.kernel;
import java.io.File;
-import java.io.FilenameFilter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
+
import javax.management.NotCompliantMBeanException;
+
import org.neo4j.helpers.Service;
import org.neo4j.jmx.impl.ManagementBeanProvider;
import org.neo4j.jmx.impl.ManagementData;
@@ -70,7 +71,6 @@ private static boolean isHA( ManagementData management )
BranchedStore
{
private final File storePath;
- private static final FilenameFilter branchedDataDirectoryFilenameFilter = new BranchedDataDirectoryFilenameFilter();
protected BranchedStoreImpl( ManagementData management )
throws NotCompliantMBeanException
@@ -104,35 +104,21 @@ private File extractStorePath( ManagementData management )
public BranchedStoreInfo[] getBranchedStores()
{
List<BranchedStoreInfo> toReturn = new LinkedList<BranchedStoreInfo>();
- for ( String filename : storePath.list( branchedDataDirectoryFilenameFilter ) )
+ for ( File branchDirectory : BranchedDataPolicy.getBranchedDataRootDirectory( storePath.getAbsolutePath() ).listFiles() )
{
- toReturn.add( parseBranchedStore( filename ) );
+ if ( !branchDirectory.isDirectory() )
+ continue;
+ toReturn.add( parseBranchedStore( branchDirectory ) );
}
return toReturn.toArray( new BranchedStoreInfo[] {} );
}
- private BranchedStoreInfo parseBranchedStore(
- String branchedStoreDirName )
+ private BranchedStoreInfo parseBranchedStore( File branchDirectory )
{
- File theDir = new File( storePath, branchedStoreDirName );
- File theNeostoreFile = new File( theDir, NeoStore.DEFAULT_NAME );
-
- String timestampFromFilename = branchedStoreDirName.substring( BranchedDataPolicy.BRANCH_PREFIX.length() );
- long timestamp = Long.parseLong( timestampFromFilename );
-
- long txId = NeoStore.getTxId( new DefaultFileSystemAbstraction(), theNeostoreFile.getAbsolutePath() );
- return new BranchedStoreInfo( branchedStoreDirName, txId, timestamp );
- }
-
- private static final class BranchedDataDirectoryFilenameFilter
- implements FilenameFilter
- {
- @Override
- public boolean accept( File dir, String name )
- {
- return new File( dir, name ).isDirectory()
- && name.startsWith( BranchedDataPolicy.BRANCH_PREFIX );
- }
+ File neostoreFile = new File( branchDirectory, NeoStore.DEFAULT_NAME );
+ long txId = NeoStore.getTxId( new DefaultFileSystemAbstraction(), neostoreFile.getAbsolutePath() );
+ long timestamp = Long.parseLong( branchDirectory.getName() );
+ return new BranchedStoreInfo( branchDirectory.getName(), txId, timestamp );
}
}
}
View
101 ha/src/main/java/org/neo4j/kernel/HighlyAvailableGraphDatabase.java
@@ -259,10 +259,54 @@ public StoreId get()
this.broker = createBroker();
this.pullUpdates = false;
this.clusterClient = createClusterClient();
+
+ migrateBranchedDataDirectoriesToRootDirectory();
start();
}
+ private void migrateBranchedDataDirectoriesToRootDirectory()
+ {
+ File branchedDir = BranchedDataPolicy.getBranchedDataRootDirectory( storeDir );
+ branchedDir.mkdirs();
+ for ( File oldBranchedDir : new File( storeDir ).listFiles() )
+ {
+ if ( !oldBranchedDir.isDirectory() || !oldBranchedDir.getName().startsWith( "branched-" ) )
+ continue;
+
+ long timestamp = 0;
+ try
+ {
+ timestamp = Long.parseLong( oldBranchedDir.getName().substring( oldBranchedDir.getName().indexOf( '-' ) + 1 ) );
+ }
+ catch ( NumberFormatException e )
+ { // OK, it wasn't a branched directory after all.
+ continue;
+ }
+
+ File targetDir = BranchedDataPolicy.getBranchedDataDirectory( storeDir, timestamp );
+ boolean success = false;
+ try
+ {
+ success = !fileSystemAbstraction.renameFile( oldBranchedDir.getAbsolutePath(), targetDir.getAbsolutePath() );
+ }
+ catch ( IOException e )
+ { // OK, let's try copying instead.
+ }
+ if ( success )
+ continue;
+
+ try
+ {
+ fileSystemAbstraction.copyFile( oldBranchedDir.getAbsolutePath(), targetDir.getAbsolutePath() );
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( "Couldn't move branched directories to " + branchedDir, e );
+ }
+ }
+ }
+
private Logging createLogging()
{
try
@@ -1492,24 +1536,24 @@ public KernelPanicEventGenerator getKernelPanicGenerator()
return localGraph().getKernelPanicGenerator();
}
- public enum BranchedDataPolicy
+ public static enum BranchedDataPolicy
{
keep_all
{
@Override
void handle( HighlyAvailableGraphDatabase db )
{
- moveAwayDb( db, branchedDataDir( db ) );
+ moveAwayDb( db, newBranchedDataDir( db ) );
}
},
keep_last
{
@Override
void handle( HighlyAvailableGraphDatabase db )
{
- File branchedDataDir = branchedDataDir( db );
+ File branchedDataDir = newBranchedDataDir( db );
moveAwayDb( db, branchedDataDir );
- for ( File file : new File( db.getStoreDir() ).listFiles() )
+ for ( File file : getBranchedDataRootDirectory( db.getStoreDir() ).listFiles() )
{
if ( isBranchedDataDirectory( file ) && !file.equals( branchedDataDir ) )
{
@@ -1552,7 +1596,8 @@ void handle( HighlyAvailableGraphDatabase db )
}
};
- static String BRANCH_PREFIX = "branched-";
+ // Branched directories will end up in <dbStoreDir>/branched/<timestamp>/
+ static String BRANCH_SUBDIRECTORY = "branched";
abstract void handle( HighlyAvailableGraphDatabase db );
@@ -1568,9 +1613,9 @@ protected void moveAwayDb( HighlyAvailableGraphDatabase db, File branchedDataDir
}
}
- File branchedDataDir( HighlyAvailableGraphDatabase db )
+ File newBranchedDataDir( HighlyAvailableGraphDatabase db )
{
- File result = new File( db.getStoreDir(), BRANCH_PREFIX + System.currentTimeMillis() );
+ File result = getBranchedDataDirectory( db.getStoreDir(), System.currentTimeMillis() );
result.mkdirs();
return result;
}
@@ -1585,14 +1630,50 @@ File branchedDataDir( HighlyAvailableGraphDatabase db )
@Override
public boolean accept( File file )
{
- return !file.getName().equals( StringLogger.DEFAULT_NAME ) && !isBranchedDataDirectory( file );
+ return !file.getName().equals( StringLogger.DEFAULT_NAME ) && !isBranchedDataRootDirectory( file );
}
} );
}
- boolean isBranchedDataDirectory( File file )
+ public static boolean isBranchedDataRootDirectory( File directory )
+ {
+ return directory.isDirectory() && directory.getName().equals( BRANCH_SUBDIRECTORY );
+ }
+
+ public static boolean isBranchedDataDirectory( File directory )
+ {
+ return directory.isDirectory() && directory.getParentFile().getName().equals( BRANCH_SUBDIRECTORY ) &&
+ isAllDigits( directory.getName() );
+ }
+
+ private static boolean isAllDigits( String string )
+ {
+ for ( char c : string.toCharArray() )
+ if ( !Character.isDigit( c ) )
+ return false;
+ return true;
+ }
+
+ public static File getBranchedDataRootDirectory( String dbStoreDir )
{
- return file.isDirectory() && file.getName().startsWith( BRANCH_PREFIX );
+ return new File( dbStoreDir, BRANCH_SUBDIRECTORY );
+ }
+
+ public static File getBranchedDataDirectory( String dbStoreDir, long timestamp )
+ {
+ return new File( getBranchedDataRootDirectory( dbStoreDir ), "" + timestamp );
+ }
+
+ public static File[] listBranchedDataDirectories( String storeDir )
+ {
+ return getBranchedDataRootDirectory( storeDir ).listFiles( new FileFilter()
+ {
+ @Override
+ public boolean accept( File directory )
+ {
+ return isBranchedDataDirectory( directory );
+ }
+ } );
}
}
View
2 ha/src/test/java/org/neo4j/ha/TestConfig.java
@@ -72,7 +72,7 @@ public void testZkSessionTimeout() throws Exception
{
long timeout = 80000; // Default is 5000
HighlyAvailableGraphDatabase db = (HighlyAvailableGraphDatabase) new EnterpriseGraphDatabaseFactory().
- newHighlyAvailableDatabaseBuilder( dir.directory( "zkTimeout" ).getAbsolutePath() ).
+ newHighlyAvailableDatabaseBuilder( dir.directory( "zkTimeout", true ).getAbsolutePath() ).
setConfig( HaSettings.server_id, "1" ).
setConfig( HaSettings.coordinators, zoo.getConnectionString() ).
setConfig( HaSettings.zk_session_timeout, ""+timeout ).

0 comments on commit 1d98ffe

Please sign in to comment.