Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2002-2014 "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.backup;
 
 import java.io.File;
 
 import org.junit.Rule;
 import org.junit.Test;
 
 
 import static org.hamcrest.core.IsInstanceOf.instanceOf;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 public class TestBackup
 {
     private File serverPath;
     private File otherServerPath;
     private File backupPath;
 
     @Rule
     public TestName testName = new TestName();
 
     @Before
     public void before() throws Exception
     {
         File base = TargetDirectory.forTestgetClass() ).cleanDirectory.getMethodName() );
          = new Filebase"server" );
          = new Filebase"server2" );
          = new Filebase"backuedup-serverdb" );
     }
 
     // TODO MP: What happens if the server database keeps growing, virtually making the files endless?
 
     @Test
     public void makeSureFullFailsWhenDbExists() throws Exception
     {
         createInitialDataSet );
         ServerInterface server = startServer );
         OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
         createInitialDataSet );
         try
         {
             backup.full.getPath() );
             fail"Shouldn't be able to do full backup into existing db" );
         }
         catch ( Exception e )
         {
             // good
         }
         shutdownServerserver );
     }
 
     @Test
     public void makeSureIncrementalFailsWhenNoDb() throws Exception
     {
         createInitialDataSet );
         ServerInterface server = startServer );
        OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
        try
        {
            backup.incremental.getPath() );
            fail"Shouldn't be able to do incremental backup into non-existing db" );
        }
        catch ( Exception e )
        {
            // Good
        }
        shutdownServerserver );
    }
    @Test
    public void backupLeavesLastTxInLog() throws Exception
    {
        GraphDatabaseAPI db = null;
        ServerInterface server = null;
        try
        {
            createInitialDataSet );
            server = startServer );
            OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
            backup.full.getPath() );
            shutdownServerserver );
            server = null;
            db = (GraphDatabaseAPInew GraphDatabaseFactory().newEmbeddedDatabase.getPath() );
            {
                ds.getMasterForCommittedTxds.getLastCommittedTxId() );
            }
            db.shutdown();
            addMoreData );
            server = startServer );
            backup.incremental.getPath() );
            shutdownServerserver );
            server = null;
            db = (GraphDatabaseAPInew GraphDatabaseFactory().newEmbeddedDatabase.getPath() );
            {
                ds.getMasterForCommittedTxds.getLastCommittedTxId() );
            }
        }
        finally
        {
            if ( db != null )
            {
                db.shutdown();
            }
            if ( server != null )
            {
                shutdownServerserver );
            }
        }
    }
    @Test
    public void incrementalBackupLeavesOnlyLastTxInLog() throws Exception
    {
        GraphDatabaseAPI db = null;
        ServerInterface server = null;
        try
        {
            createInitialDataSet );
            server = startServer );
            OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
            backup.full.getPath() );
            shutdownServerserver );
            server = null;
            addMoreData );
            server = startServer );
            backup.incremental.getPath() );
            shutdownServerserver );
            server = null;
            // do 2 rotations, add two empty logs
            new GraphDatabaseFactory().newEmbeddedDatabase.getPath() ).shutdown();
            new GraphDatabaseFactory().newEmbeddedDatabase.getPath() ).shutdown();
            addMoreData );
            server = startServer );
            backup.incremental.getPath() );
            shutdownServerserver );
            server = null;
            int logsFound = .listFilesnew FilenameFilter()
            {
                @Override
                public boolean acceptFile dirString name )
                {
                    return name.startsWith"nioneo_logical.log" )
                           && !name.endsWith"active" );
                }
            } ).length;
            // 2 one the real and the other from the rotation of shutdown
            assertEquals( 2, logsFound );
            db = (GraphDatabaseAPInew GraphDatabaseFactory().newEmbeddedDatabase.getPath() );
            {
                ds.getMasterForCommittedTxds.getLastCommittedTxId() );
            }
        }
        finally
        {
            if ( db != null )
            {
                db.shutdown();
            }
            if ( server != null )
            {
                shutdownServerserver );
            }
        }
    }
    @Test
    public void fullThenIncremental() throws Exception
    {
        DbRepresentation initialDataSetRepresentation = createInitialDataSet );
        ServerInterface server = startServer );
        // START SNIPPET: onlineBackup
        OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
        backup.full.getPath() );
        // END SNIPPET: onlineBackup
        assertEqualsinitialDataSetRepresentation, DbRepresentation.of ) );
        shutdownServerserver );
        DbRepresentation furtherRepresentation = addMoreData );
        server = startServer );
        // START SNIPPET: onlineBackup
        backup.incremental.getPath() );
        // END SNIPPET: onlineBackup
        assertEqualsfurtherRepresentation, DbRepresentation.of ) );
        shutdownServerserver );
    }
    @Test
    public void makeSureNoLogFileRemains() throws Exception
    {
        createInitialDataSet );
        ServerInterface server = startServer );
        OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
        // First check full
        backup.full.getPath() );
        // Then check empty incremental
        backup.incremental.getPath() );
        // Then check real incremental
        shutdownServerserver );
        addMoreData );
        server = startServer );
        backup.incremental.getPath() );
        shutdownServerserver );
    }
    @Test
    public void makeSureStoreIdIsEnforced() throws Exception
    {
        // Create data set X on server A
        DbRepresentation initialDataSetRepresentation = createInitialDataSet );
        ServerInterface server = startServer );
        // Grab initial backup from server A
        OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
        backup.full.getPath() );
        assertEqualsinitialDataSetRepresentation, DbRepresentation.of ) );
        shutdownServerserver );
        // Create data set X+Y on server B
        addMoreData );
        server = startServer );
        // Try to grab incremental backup from server B.
        // Data should be OK, but store id check should prevent that.
        try
        {
            backup.incremental.getPath() );
            fail"Shouldn't work" );
        }
        catch ( RuntimeException e )
        {
            assertThat(e.getCause(), instanceOf(MismatchingStoreIdException.class));
        }
        shutdownServerserver );
        // Just make sure incremental backup can be received properly from
        // server A, even after a failed attempt from server B
        DbRepresentation furtherRepresentation = addMoreData );
        server = startServer );
        backup.incremental.getPath() );
        assertEqualsfurtherRepresentation, DbRepresentation.of ) );
        shutdownServerserver );
    }
    private ServerInterface startServerFile path ) throws Exception
    {
        /*
        ServerProcess server = new ServerProcess();
        try
        {
            server.startup( Pair.of( path, "true" ) );
        }
        catch ( Throwable e )
        {
            // TODO Auto-generated catch block
            throw new RuntimeException( e );
        }
        */
        ServerInterface server = new EmbeddedServerpath.getPath(), "127.0.0.1:6362" );
        server.awaitStarted();
        return server;
    }
    private void shutdownServerServerInterface server ) throws Exception
    {
        server.shutdown();
        Thread.sleep( 1000 );
    }
    private DbRepresentation addMoreDataFile path )
    {
        GraphDatabaseService db = startGraphDatabasepath );
        Transaction tx = db.beginTx();
        Node node = db.createNode();
        node.setProperty"backup""Is great" );
        db.createNode().createRelationshipTonode,
                DynamicRelationshipType.withName"LOVES" ) );
        tx.success();
        tx.finish();
        DbRepresentation result = DbRepresentation.ofdb );
        db.shutdown();
        return result;
    }
    private GraphDatabaseService startGraphDatabaseFile path )
    {
        return new GraphDatabaseFactory().
            newEmbeddedDatabaseBuilderpath.getPath() ).
            newGraphDatabase();
    }
    private DbRepresentation createInitialDataSetFile path )
    {
        GraphDatabaseService db = startGraphDatabasepath );
        Transaction tx = db.beginTx();
        Node node = db.createNode();
        node.setProperty"myKey""myValue" );
        Index<NodenodeIndex = db.index().forNodes"db-index" );
        nodeIndex.addnode"myKey""myValue" );
        db.createNode().createRelationshipTonode,
                DynamicRelationshipType.withName"KNOWS" ) );
        tx.success();
        tx.finish();
        DbRepresentation result = DbRepresentation.ofdb );
        db.shutdown();
        return result;
    }
    @Test
    public void multipleIncrementals() throws Exception
    {
        GraphDatabaseService db = null;
        try
        {
            db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder.getPath() ).
                setConfig.. ).
                newGraphDatabase();
            Transaction tx = db.beginTx();
            Index<Nodeindex = db.index().forNodes"yo" );
            index.adddb.createNode(), "justTo""commitATx" );
            tx.success();
            tx.finish();
            OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
            backup.full.getPath() );
            long lastCommittedTxForLucene = getLastCommittedTx.getPath() );
            for ( int i = 0; i < 5; i++ )
            {
                tx = db.beginTx();
                Node node = db.createNode();
                index.addnode"key""value" + i );
                tx.success();
                tx.finish();
                backup = backup.incremental.getPath() );
                assertTrue"Should be consistent"backup.isConsistent() );
                assertEqualslastCommittedTxForLucene + i + 1,
                        getLastCommittedTx.getPath() ) );
            }
        }
        finally
        {
            if ( db != null )
            {
                db.shutdown();
            }
        }
    }
    @Test
    public void backupIndexWithNoCommits() throws Exception
    {
        GraphDatabaseService db = null;
        try
        {
            db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder.getPath() ).
                setConfig.. ).
                newGraphDatabase();
            Transaction transaction = db.beginTx();
            try
            {
                db.index().forNodes"created-no-commits" );
                transaction.success();
            }
            finally
            {
                transaction.finish();
            }
            OnlineBackup backup = OnlineBackup.from"127.0.0.1" );
            backup.full.getPath() );
        }
        finally
        {
            if ( db != null )
            {
                db.shutdown();
            }
        }
    }
    @SuppressWarnings("deprecation")
    private long getLastCommittedTxString path )
    {
        GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabasepath );
        try
        {
            return ds.getLastCommittedTxId();
        }
        finally
        {
            db.shutdown();
        }
    }
    @Test
    public void backupEmptyIndex() throws Exception
    {
        String key = "name";
        String value = "Neo";
            newGraphDatabase();
        Transaction tx = db.beginTx();
        Index<Nodeindex;
        Node node;
        try
        {
            index = db.index().forNodeskey );
            node = db.createNode();
            node.setPropertykeyvalue );
            tx.success();
        }
        finally
        {
            tx.finish();
        }
        OnlineBackup.from"127.0.0.1" ).full.getPath() );
        assertEquals( DbRepresentation.ofdb ), DbRepresentation.of ) );
        FileUtils.deleteDirectorynew File.getPath() ) );
        OnlineBackup.from"127.0.0.1" ).full.getPath() );
        assertEquals( DbRepresentation.ofdb ), DbRepresentation.of ) );
        tx = db.beginTx();
        try
        {
            index.addnodekeyvalue );
            tx.success();
        }
        finally
        {
            tx.finish();
        }
        FileUtils.deleteDirectorynew File.getPath() ) );
        OnlineBackup backup = OnlineBackup.from"127.0.0.1" ).full.getPath() );
        assertTrue"Should be consistent"backup.isConsistent() );
        assertEquals( DbRepresentation.ofdb ), DbRepresentation.of ) );
        db.shutdown();
    }
    @Test
    {
        String sourcePath = "target/var/serverdb-lock";
        FileUtils.deleteDirectorynew FilesourcePath ) );
        GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabaseBuildersourcePath ).
            newGraphDatabase();
        try
        {
            assertStoreIsLockedsourcePath );
            OnlineBackup.from"127.0.0.1" ).full.getPath() );
            assertStoreIsLockedsourcePath );
        }
        finally
        {
            db.shutdown();
        }
    }
    private static void assertStoreIsLockedString path )
    {
        try
        {
            new GraphDatabaseFactory().newEmbeddedDatabasepath).shutdown();
            fail"Could start up database in same process, store not locked" );
        }
        catch ( RuntimeException ex )
        {
            assertThatex.getCause().getCause(), instanceOfStoreLockException.class ) );
        }
        StartupChecker proc = new LockProcess().startpath );
        try
        {
            assertFalse"Could start up database in subprocess, store is not locked"proc.startupOk() );
        }
        finally
        {
            SubProcess.stopproc );
        }
    }
    public interface StartupChecker
    {
        boolean startupOk();
    }
    @SuppressWarnings"serial" )
    private static class LockProcess extends SubProcess<StartupCheckerStringimplements StartupChecker
    {
        private volatile Object state;
        @Override
        public boolean startupOk()
        {
            Object result;
            do
            {
                result = ;
            }
            while ( result == null );
            return !(  instanceof Exception );
        }
        @Override
        protected void startupString path ) throws Throwable
        {
            GraphDatabaseService db = null;
            try
            {
                db = new GraphDatabaseFactory().newEmbeddedDatabasepath );
            }
            catch ( RuntimeException ex )
            {
                if (ex.getCause().getCause() instanceof StoreLockException )
                {
                     = ex;
                    return;
                }
            }
             = new Object();
            if ( db != null )
            {
                db.shutdown();
            }
        }
    }
    private static boolean checkLogFileExistenceString directory )
    {
        return new Filedirectory. ).exists();
    }
New to GrepCode? Check out our FAQ X