Skip to content
Snippets Groups Projects
Commit b8666815 authored by Tobias Pietzsch's avatar Tobias Pietzsch
Browse files

Minor tweaks

parent 7795dd06
No related branches found
No related tags found
No related merge requests found
...@@ -87,4 +87,18 @@ public class IoTimeBudget ...@@ -87,4 +87,18 @@ public class IoTimeBudget
for ( ; l < budget.length && budget[ l ] > budget[ l - 1 ]; ++l ) for ( ; l < budget.length && budget[ l ] > budget[ l - 1 ]; ++l )
budget[ l ] = budget[ l - 1 ]; budget[ l ] = budget[ l - 1 ];
} }
/**
* Returns how much time is left for the specified priority level.
*
* @param level
* priority level. must be greater &ge; 0.
* @return time left for the specified priority level.
*/
public long estimateTimeLeft( final int level )
{
final long[] b = budget;
final int blevel = Math.min( level, b.length - 1 );
return b[ blevel ];
}
} }
...@@ -25,17 +25,17 @@ public class SoftRefCache< K, V > implements Cache< K, V > ...@@ -25,17 +25,17 @@ public class SoftRefCache< K, V > implements Cache< K, V >
public void clean() public void clean()
{ {
map.remove( entry.getKey(), entry ); map.remove( entry.key, entry );
} }
} }
final class Entry final class Entry
{ {
private final K key; final K key;
private SoftReference< V > ref; private SoftReference< V > ref;
private boolean loaded; boolean loaded;
public Entry( final K key ) public Entry( final K key )
{ {
...@@ -44,11 +44,6 @@ public class SoftRefCache< K, V > implements Cache< K, V > ...@@ -44,11 +44,6 @@ public class SoftRefCache< K, V > implements Cache< K, V >
this.loaded = false; this.loaded = false;
} }
public K getKey()
{
return key;
}
public V getValue() public V getValue()
{ {
return ref.get(); return ref.get();
...@@ -59,11 +54,6 @@ public class SoftRefCache< K, V > implements Cache< K, V > ...@@ -59,11 +54,6 @@ public class SoftRefCache< K, V > implements Cache< K, V >
this.loaded = true; this.loaded = true;
this.ref = new CacheSoftReference( value, this ); this.ref = new CacheSoftReference( value, this );
} }
public boolean wasLoaded()
{
return loaded;
}
} }
@Override @Override
...@@ -82,7 +72,7 @@ public class SoftRefCache< K, V > implements Cache< K, V > ...@@ -82,7 +72,7 @@ public class SoftRefCache< K, V > implements Cache< K, V >
{ {
synchronized ( entry ) synchronized ( entry )
{ {
if ( entry.wasLoaded() ) if ( entry.loaded )
{ {
value = entry.getValue(); value = entry.getValue();
if ( value == null ) if ( value == null )
......
...@@ -48,6 +48,9 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -48,6 +48,9 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
} }
} }
/*
* Possible states of Entry.loaded
*/
static final int NOTLOADED = 0; static final int NOTLOADED = 0;
static final int INVALID = 1; static final int INVALID = 1;
static final int VALID = 2; static final int VALID = 2;
...@@ -112,16 +115,13 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -112,16 +115,13 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
if ( v != null && v.isValid() ) if ( v != null && v.isValid() )
return v; return v;
cleanUp( 50 );
switch ( hints.getLoadingStrategy() ) switch ( hints.getLoadingStrategy() )
{ {
case BLOCKING: case BLOCKING:
return getBlocking( entry ); return getBlocking( entry );
case BUDGETED: case BUDGETED:
final int priority = hints.getQueuePriority(); if ( estimatedBugdetTimeLeft( hints ) > 0 )
final IoStatistics stats = CacheIoTiming.getIoStatistics();
final IoTimeBudget budget = stats.getIoTimeBudget();
final long timeLeft = budget.timeLeft( priority );
if ( timeLeft > 0 )
return getBudgeted( entry, hints ); return getBudgeted( entry, hints );
case VOLATILE: case VOLATILE:
enqueue( entry, hints ); enqueue( entry, hints );
...@@ -131,18 +131,27 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -131,18 +131,27 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
} }
} }
private long estimatedBugdetTimeLeft( final CacheHints hints )
{
final int priority = hints.getQueuePriority();
final IoStatistics stats = CacheIoTiming.getIoStatistics();
final IoTimeBudget budget = stats.getIoTimeBudget();
return budget.estimateTimeLeft( priority );
}
@Override @Override
public V get( final K key, final VolatileLoader< ? extends V > loader, final CacheHints hints ) throws ExecutionException public V get( final K key, final VolatileLoader< ? extends V > loader, final CacheHints hints ) throws ExecutionException
{ {
/* /*
* Get existing entry for key or create it. * Get existing entry for key or create it.
*/ */
final Entry entry = map.computeIfAbsent( key, k -> new Entry( key, loader ) ); final Entry entry = map.computeIfAbsent( key, k -> new Entry( k, loader ) );
V v = entry.getValue(); V v = entry.getValue();
if ( v != null && v.isValid() ) if ( v != null && v.isValid() )
return v; return v;
cleanUp( 50 );
switch ( hints.getLoadingStrategy() ) switch ( hints.getLoadingStrategy() )
{ {
case BLOCKING: case BLOCKING:
...@@ -159,8 +168,6 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -159,8 +168,6 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
break; break;
} }
cleanUp( 10 );
if ( v == null ) if ( v == null )
return get( key, loader, hints ); return get( key, loader, hints );
else else
...@@ -298,6 +305,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -298,6 +305,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
V v = entry.getValue(); V v = entry.getValue();
if ( v == null && entry.loaded != NOTLOADED ) if ( v == null && entry.loaded != NOTLOADED )
{ {
// printEntryCollected( "map.remove getBudgeted 1", entry );
map.remove( entry.key, entry ); map.remove( entry.key, entry );
return null; return null;
} }
...@@ -338,6 +346,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -338,6 +346,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
} }
else else
{ {
// printEntryCollected( "map.remove getBudgeted 2", entry );
map.remove( entry.key, entry ); map.remove( entry.key, entry );
return null; return null;
} }
...@@ -354,6 +363,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -354,6 +363,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
final V v = entry.getValue(); final V v = entry.getValue();
if ( v == null && entry.loaded != NOTLOADED ) if ( v == null && entry.loaded != NOTLOADED )
{ {
// printEntryCollected( "map.remove getBlocking 1", entry );
map.remove( entry.key, entry ); map.remove( entry.key, entry );
return null; return null;
} }
...@@ -369,6 +379,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -369,6 +379,7 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
final V v = entry.getValue(); final V v = entry.getValue();
if ( v == null && entry.loaded != NOTLOADED ) if ( v == null && entry.loaded != NOTLOADED )
{ {
// printEntryCollected( "map.remove getBlocking 2", entry );
map.remove( entry.key, entry ); map.remove( entry.key, entry );
return null; return null;
} }
...@@ -395,4 +406,24 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements ...@@ -395,4 +406,24 @@ public class WeakRefVolatileCache< K, V extends VolatileCacheValue > implements
fetchQueue.put( new FetchEntry( entry.key ), hints.getQueuePriority(), hints.isEnqueuToFront() ); fetchQueue.put( new FetchEntry( entry.key ), hints.getQueuePriority(), hints.isEnqueuToFront() );
} }
} }
/**
* For debugging. Print stack trace when an Entry's value has been collected while we want
* to load it.
*
* @param title
* @param entry
*/
private synchronized void printEntryCollected( final String title, final Entry entry )
{
final String state = entry.loaded == 0
? "NOTLOADED"
: ( entry.loaded == 1
? "INVALID"
: ( entry.loaded == 2
? "VALID"
: "UNDEFINED" ) );
System.out.println( title + " entry.loaded = " + state );
// new Throwable().printStackTrace( System.out );
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment