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 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 recovery;
 
 import java.io.File;
 import org.junit.Test;
 
 import static java.nio.ByteBuffer.*;
 import static org.junit.Assert.*;
 import static org.neo4j.helpers.Exceptions.*;
 import static org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.*;
 
 @Ignore"Doesn't work yet" )
 {
     private static final int TX_COUNT = 10;
     
     private DebuggedThread committer;
     private CountDownLatch commitLatch = new CountDownLatch( 1 );
     private CountDownLatch latch = new CountDownLatch );
     
     private final BreakPoint commit = new BreakPointXaResourceManager.class"commit"Xid.class. )
     {
         private volatile int letPass = 1;
         
         @Override
         protected void callbackDebugInterface debug ) throws KillSubProcess
         {
             int pass = --;
             if ( pass > 0 ) return;
             else if ( pass == 0 )
             {
                  = debug.thread().suspendthis );
                 .countDown();
             }
             else
             {
                 .countDown();
             }
         }
     };
     private final BreakPoint continueCommitting = new BreakPointgetClass(), "pleaseContinue" )
     {
         @Override
         protected void callbackDebugInterface debug ) throws KillSubProcess
         {
             ..println"continue" );
             .resume();
             .countDown();
         }
     };
     
     static void pleaseContinue()
     {   // Triggers breakpoint
     }
     
     protected BreakPoint[] breakpointsint id )
     {
        return new BreakPoint[] { .enable(), .enable() };
    }
    
    private static class CreateIndexedNodeTask implements Task
    {
        @Override
        public void runGraphDatabaseAPI graphdb )
        {
            Transaction tx = graphdb.beginTx();
            try
            {
                Node node = graphdb.createNode();
                graphdb.index().forNodes"index" ).addnode"key""value" );
                tx.success();
            }
            finally
            {
                tx.finish();
            }
        }
    }
    
    private static class CommandFactory extends XaCommandFactory
    {
        @Override
        public XaCommand readCommandReadableByteChannel byteChannel,
                ByteBuffer buffer ) throws IOException
        {
            return Command.readCommandnullbyteChannelbuffer );
        }
    }
    
    private static class MessUpTask implements Task
    {
        @Override
        public void runGraphDatabaseAPI graphdb )
        {
            try
            {
                XaResourceManager resourceManager = graphdb.getXaDataSourceManager().getNeoStoreDataSource().getXaContainer().getResourceManager();
                ArrayMap<Xid, ?> xidMap = (ArrayMap<Xid, ?>) inaccessibleFieldresourceManager"xidMap" ).getresourceManager );
                xidMap.clear();
            }
            catch ( Exception e )
            {
                throw launderedExceptione );
            }
            finally
            {
                pleaseContinue();
            }
        }
    }
    
    @Test
    {
        /* Will start many 2PC transactions and halt them right before committing, creating
         * many 2PC transactions which will have to recovered and committed during recovery. */
        for ( int i = 0; i < i++ ) runInThreadnew CreateIndexedNodeTask() );
        .await();
        
        runnew MessUpTask() );
        .await();
        
        /* Restart and recover */
        restart();
        
        verifyOrderedRecords();
    }
    private void verifyOrderedRecords() throws FileNotFoundExceptionIOException
    {
        /* Look in the .v0 log for the 2PC records and that they are ordered by txId */
        RandomAccessFile file = new RandomAccessFilenew FilegetStoreDirthis, 0 ), "nioneo_logical.log.v0" ), "r" );
        CommandFactory cf = new CommandFactory();
        try
        {
            FileChannel channel = file.getChannel();
            ByteBuffer buffer = allocate( 10000 );
            readLogHeaderbufferchanneltrue );
            long lastOne = -1;
            int counted = 0;
            for ( LogEntry entry = null; (entry = readEntrybufferchannelcf )) != null; )
            {
                if ( entry instanceof TwoPhaseCommit )
                {
                    long txId = ((TwoPhaseCommitentry).getTxId();
                    if ( lastOne == -1 ) txId = lastOne;
                    else assertEqualslastOne+1, txId );
                    lastOne = txId;
                    counted++;
                }
            }
            assertEqualscounted );
        }
        finally
        {
            file.close();
        }
    }
New to GrepCode? Check out our FAQ X