Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
FIFO replacement strategies in btree cache may fail somtime (T52771)
  • Loading branch information
gliu committed Aug 24, 2012
1 parent 684b3f8 commit 454e2d4
Showing 1 changed file with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -169,15 +170,21 @@ LeafEntry<K, V> getFirstEntry( ) throws IOException
while ( nodeId != -1 )
{
BTreeNode<K, V> node = loadBTreeNode( nodeId );
if ( node.getNodeType( ) == NODE_LEAF )
try
{
return ( (LeafNode<K, V>) node ).getFirstEntry( );
if ( node.getNodeType( ) == NODE_LEAF )
{
return ( (LeafNode<K, V>) node ).getFirstEntry( );
}
else
{
nodeId = ( (IndexNode<K, V>) node ).getFirstChild( );
}
}
else
finally
{
nodeId = ( (IndexNode<K, V>) node ).getFirstChild( );
node.unlock( );
}
node.unlock( );
}
return null;
}
Expand All @@ -188,15 +195,22 @@ LeafEntry<K, V> getLastEntry( ) throws IOException
while ( nodeId != -1 )
{
BTreeNode<K, V> node = loadBTreeNode( nodeId );
if ( node.getNodeType( ) == NODE_LEAF )
try
{
return ( (LeafNode<K, V>) node ).getLastEntry( );
if ( node.getNodeType( ) == NODE_LEAF )
{
return ( (LeafNode<K, V>) node ).getLastEntry( );
}
else
{
nodeId = ( (IndexNode<K, V>) node ).getLastChild( );
}

}
else
finally
{
nodeId = ( (IndexNode<K, V>) node ).getLastChild( );
node.unlock( );
}
node.unlock( );
}
return null;
}
Expand Down Expand Up @@ -503,9 +517,38 @@ protected boolean removeEldestEntry(
if ( size( ) >= cacheSize )
{
BTreeNode<K, V> node = arg.getValue( );

if ( node.isLocked( ) )
{
Iterator<Map.Entry<Integer, BTreeNode<K, V>>> iter = this
.entrySet( ).iterator( );
while ( iter.hasNext( ) )
{
Map.Entry<Integer, BTreeNode<K, V>> entry = iter.next( );
BTreeNode<K, V> value = entry.getValue( );
if ( !value.isLocked( ) )
{
// remove this node
if ( node.isDirty( ) )
{
try
{
writeNode( node );
}
catch ( IOException ex )
{
logger.log(
Level.WARNING,
"failed to write node "
+ node.getNodeId( )
+ " type "
+ node.getNodeType( ), ex );
return false;
}
}
remove( entry.getKey( ) );
break;
}
}
return false;
}
if ( node.isDirty( ) )
Expand All @@ -516,9 +559,9 @@ protected boolean removeEldestEntry(
}
catch ( IOException ex )
{
logger.log( Level.WARNING, "failed to write node "
+ node.getNodeId( ) + " type "
+ node.getNodeType( ), ex );
logger.log( Level.WARNING,
"failed to write node " + node.getNodeId( )
+ " type " + node.getNodeType( ), ex );
return false;
}
}
Expand Down Expand Up @@ -696,7 +739,7 @@ ExternalValueList<K, V> createExternalValueList( BTreeValues<V> values )

BTreeValue<K> createKey( K key ) throws IOException
{
if ( key == null)
if ( key == null )
{
assert allowNullKey == true;
return NULL_KEY;
Expand Down Expand Up @@ -833,7 +876,8 @@ int writeValue( DataOutput out, BTreeValue<V> value ) throws IOException
if ( valueSize != 0 && valueSize != bytes.length )
{
throw new IOException(
CoreMessages.getString( ResourceConstants.MISMATCH_VALUE_LENGTH ) );
CoreMessages
.getString( ResourceConstants.MISMATCH_VALUE_LENGTH ) );
}
if ( valueSize == 0 )
{
Expand Down

0 comments on commit 454e2d4

Please sign in to comment.