You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by bp...@apache.org on 2012/03/02 11:06:46 UTC

svn commit: r1296107 - in /incubator/directmemory/trunk: directmemory-cache/src/main/java/org/apache/directmemory/memory/ directmemory-cache/src/test/java/org/apache/directmemory/cache/ integrations/ehcache/src/main/java/org/apache/directmemory/ehcache...

Author: bperroud
Date: Fri Mar  2 10:06:46 2012
New Revision: 1296107

URL: http://svn.apache.org/viewvc?rev=1296107&view=rev
Log:
DIRECTMEMORY-77 : make MemoryManagerServiceWithAllocationPolicyImpl the default MemoryManagerService

Removed:
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceWithAllocationPolicyImpl.java
Modified:
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
    incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java
    incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java?rev=1296107&r1=1296106&r2=1296107&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java Fri Mar  2 10:06:46 2012
@@ -19,13 +19,8 @@ package org.apache.directmemory.memory;
  * under the License.
  */
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 public class MemoryManager
 {
-    private static Logger logger = LoggerFactory.getLogger( MemoryManager.class );
-
     private static MemoryManagerService<Object> memoryManager = new MemoryManagerServiceImpl<Object>();
 
     private MemoryManager()

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java?rev=1296107&r1=1296106&r2=1296107&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java Fri Mar  2 10:06:46 2012
@@ -53,13 +53,13 @@ public class MemoryManagerServiceImpl<V>
     private List<ByteBufferAllocator> allocators;
 
     private final Set<Pointer<V>> pointers = Collections.newSetFromMap( new ConcurrentHashMap<Pointer<V>, Boolean>() );
-    
-    protected int activeAllocatorIndex = 0;
 
     private final boolean returnNullWhenFull;
     
     protected final AtomicLong used = new AtomicLong( 0L );
 
+    protected final AllocationPolicy allocationPolicy;
+    
     public MemoryManagerServiceImpl()
     {
         this( true );
@@ -67,6 +67,12 @@ public class MemoryManagerServiceImpl<V>
     
     public MemoryManagerServiceImpl( final boolean returnNullWhenFull )
     {
+        this( new RoundRobinAllocationPolicy(), returnNullWhenFull );
+    }
+    
+    public MemoryManagerServiceImpl( final AllocationPolicy allocationPolicy, final boolean returnNullWhenFull )
+    {
+        this.allocationPolicy = allocationPolicy;
         this.returnNullWhenFull = returnNullWhenFull;
     }
 
@@ -82,6 +88,8 @@ public class MemoryManagerServiceImpl<V>
             allocators.add( allocator );
         }
 
+        allocationPolicy.init( allocators );
+        
         logger.info( format( "MemoryManager initialized - %d buffers, %s each", numberOfBuffers, Ram.inMb( size ) ) );
     }
 
@@ -102,39 +110,48 @@ public class MemoryManagerServiceImpl<V>
         return allocators.get( allocatorIndex );
     }
 
+    protected ByteBufferAllocator getCurrentAllocator()
+    {
+        return allocationPolicy.getActiveAllocator( null, 0 );
+    }
+    
     @Override
     public Pointer<V> store( byte[] payload, int expiresIn )
     {
-        
-        int allocatorIndex = activeAllocatorIndex;
-        
-        ByteBuffer buffer = getAllocator( allocatorIndex ).allocate( payload.length );
-        
-        if (buffer == null && allocators.size() > 1)
-        {
-            allocatorIndex = nextAllocator();
-            buffer = getAllocator( allocatorIndex ).allocate( payload.length );
-        }
-        
-        if (buffer == null)
-        {
-            if (returnsNullWhenFull())
+        Pointer<V> p = null;
+        ByteBufferAllocator allocator = null;
+        int allocationNumber = 0;
+        do
+        {
+            allocationNumber++;
+            allocator = allocationPolicy.getActiveAllocator( allocator, allocationNumber );
+            if ( allocator == null )
             {
-                return null;
+                if (returnsNullWhenFull())
+                {
+                    return null;
+                }
+                else
+                {
+                    throw new BufferOverflowException();
+                }
             }
-            else
+            final ByteBuffer buffer = allocator.allocate( payload.length );
+            
+            if ( buffer == null )
             {
-                throw new BufferOverflowException();
+                continue;
             }
+            
+            p = instanciatePointer( buffer, allocator.getNumber(), expiresIn, NEVER_EXPIRES );
+
+            buffer.rewind();
+            buffer.put( payload );
+            
+            used.addAndGet( payload.length );
+            
         }
-        
-        buffer.rewind();
-        buffer.put( payload );
-        
-        Pointer<V> p = instanciatePointer( buffer, allocatorIndex, expiresIn, NEVER_EXPIRES );
-        
-        used.addAndGet( payload.length );
-        
+        while ( p == null );
         return p;
     }
 
@@ -195,6 +212,7 @@ public class MemoryManagerServiceImpl<V>
         {
             allocator.clear();
         }
+        allocationPolicy.reset();
     }
 
     @Override
@@ -299,39 +317,41 @@ public class MemoryManagerServiceImpl<V>
     public <T extends V> Pointer<V> allocate( final Class<T> type, final int size, final long expiresIn, final long expires )
     {
         
-        int allocatorIndex = activeAllocatorIndex;
-        
-        ByteBuffer buffer = getAllocator( allocatorIndex ).allocate( size );
-        
-        if (buffer == null && allocators.size() > 1)
-        {
-            allocatorIndex = nextAllocator();
-            buffer = getAllocator( allocatorIndex ).allocate( size );
-        }
-        
-        if (buffer == null)
-        {
-            if (returnsNullWhenFull())
+        Pointer<V> p = null;
+        ByteBufferAllocator allocator = null;
+        int allocationNumber = 0;
+        do
+        {
+            allocationNumber++;
+            allocator = allocationPolicy.getActiveAllocator( allocator, allocationNumber );
+            if ( allocator == null )
             {
-                return null;
+                if (returnsNullWhenFull())
+                {
+                    return null;
+                }
+                else
+                {
+                    throw new BufferOverflowException();
+                }
             }
-            else
+            
+            final ByteBuffer buffer = allocator.allocate( size );
+            
+            if ( buffer == null )
             {
-                throw new BufferOverflowException();
+                continue;
             }
+            
+            p = instanciatePointer( buffer, allocator.getNumber(), expiresIn, NEVER_EXPIRES );
+            
+            used.addAndGet( size );
         }
+        while ( p == null );
         
-        Pointer<V> pointer = instanciatePointer( buffer, allocatorIndex, expiresIn, expires );
-        
-        if (pointer != null)
-        {
-            pointer.setClazz( type );
-        }
-        
-        used.addAndGet( size );
-        
-        return pointer;
+        p.setClazz( type );
         
+        return p;
     }
     
     protected Pointer<V> instanciatePointer( final ByteBuffer buffer, final int allocatorIndex, final long expiresIn, final long expires )
@@ -349,13 +369,7 @@ public class MemoryManagerServiceImpl<V>
         
         return p;
     }
-    
-    protected int nextAllocator()
-    {
-        activeAllocatorIndex = ( activeAllocatorIndex + 1 ) % allocators.size();
-        return activeAllocatorIndex;
-    }
-    
+        
     protected boolean returnsNullWhenFull()
     {
         return returnNullWhenFull;

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java?rev=1296107&r1=1296106&r2=1296107&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java Fri Mar  2 10:06:46 2012
@@ -25,7 +25,7 @@ import org.apache.directmemory.DirectMem
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.AllocationPolicy;
 import org.apache.directmemory.memory.MemoryManagerService;
-import org.apache.directmemory.memory.MemoryManagerServiceWithAllocationPolicyImpl;
+import org.apache.directmemory.memory.MemoryManagerServiceImpl;
 import org.apache.directmemory.memory.Pointer;
 import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
 import org.junit.Test;
@@ -38,7 +38,7 @@ public class CacheServiceImplTest
     {
         AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
         MemoryManagerService<byte[]> memoryManager =
-            new MemoryManagerServiceWithAllocationPolicyImpl<byte[]>( allocationPolicy, true );
+            new MemoryManagerServiceImpl<byte[]>( allocationPolicy, true );
         CacheService<Integer, byte[]> cache = new DirectMemory<Integer, byte[]>()
                         .setMemoryManager( memoryManager )
                         .setNumberOfBuffers( 1 )

Modified: incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java?rev=1296107&r1=1296106&r2=1296107&view=diff
==============================================================================
--- incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java (original)
+++ incubator/directmemory/trunk/integrations/ehcache/src/main/java/org/apache/directmemory/ehcache/DirectMemoryCache.java Fri Mar  2 10:06:46 2012
@@ -27,9 +27,8 @@ import java.util.Set;
 import org.apache.directmemory.DirectMemory;
 import org.apache.directmemory.cache.CacheService;
 import org.apache.directmemory.memory.MemoryManagerService;
-import org.apache.directmemory.memory.MemoryManagerServiceWithAllocationPolicyImpl;
+import org.apache.directmemory.memory.MemoryManagerServiceImpl;
 import org.apache.directmemory.memory.Pointer;
-import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
 import org.apache.directmemory.serialization.Serializer;
 
 /**
@@ -45,7 +44,7 @@ public class DirectMemoryCache<K, V>
     public DirectMemoryCache( int numberOfBuffers, int size, int initialCapacity, int concurrencyLevel )
     {
         MemoryManagerService<V> memoryManager =
-                    new MemoryManagerServiceWithAllocationPolicyImpl<V>( new RoundRobinAllocationPolicy(), false );
+                    new MemoryManagerServiceImpl<V>( false );
 
         cacheService = new DirectMemory<K, V>().setMemoryManager( memoryManager )
                         .setNumberOfBuffers( numberOfBuffers )

Modified: incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java?rev=1296107&r1=1296106&r2=1296107&view=diff
==============================================================================
--- incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java (original)
+++ incubator/directmemory/trunk/itests/osgi/src/test/java/org/apache/directmemory/tests/osgi/cache/CacheServiceExportingActivator.java Fri Mar  2 10:06:46 2012
@@ -22,10 +22,8 @@ package org.apache.directmemory.tests.os
 import org.apache.directmemory.DirectMemory;
 import org.apache.directmemory.cache.CacheService;
 import org.apache.directmemory.measures.Ram;
-import org.apache.directmemory.memory.AllocationPolicy;
 import org.apache.directmemory.memory.MemoryManagerService;
-import org.apache.directmemory.memory.MemoryManagerServiceWithAllocationPolicyImpl;
-import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
+import org.apache.directmemory.memory.MemoryManagerServiceImpl;
 import org.apache.directmemory.serialization.SerializerFactory;
 import org.apache.directmemory.serialization.StandardSerializer;
 import org.osgi.framework.BundleActivator;
@@ -43,9 +41,8 @@ public class CacheServiceExportingActiva
     public void start( BundleContext context )
         throws Exception
     {
-        AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
         MemoryManagerService<SimpleObject> memoryManager =
-            new MemoryManagerServiceWithAllocationPolicyImpl<SimpleObject>( allocationPolicy, true );
+            new MemoryManagerServiceImpl<SimpleObject>();
         this.cacheService =
             new DirectMemory<String, SimpleObject>().setNumberOfBuffers( 1 ).setSize( Ram.Mb( 1 ) ).setMemoryManager(
                 memoryManager ).setSerializer(