directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1404161 - in /directory/apacheds/trunk: core-api/src/main/java/org/apache/directory/server/core/api/ core-api/src/main/java/org/apache/directory/server/core/api/partition/ core-api/src/main/resources/ core-api/src/test/java/org/apache/dire...
Date Wed, 31 Oct 2012 14:20:56 GMT
Author: kayyagari
Date: Wed Oct 31 14:20:55 2012
New Revision: 1404161

URL: http://svn.apache.org/viewvc?rev=1404161&view=rev
Log:
o completely changed the way CacheService is initialized
o added methods to let Partition implementations handle the Entry cache
o moved the ldapServer.start() statement out of the try-catch block to fail fast
o added some sample cache configurations

Modified:
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/CacheService.java
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/DirectoryService.java
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/AbstractPartition.java
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/Partition.java
    directory/apacheds/trunk/core-api/src/main/resources/directory-cacheservice.xml
    directory/apacheds/trunk/core-api/src/test/java/org/apache/directory/server/core/api/MockDirectoryService.java
    directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
    directory/apacheds/trunk/service/src/main/java/org/apache/directory/server/ApacheDsService.java
    directory/apacheds/trunk/xdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/AbstractBTreePartition.java

Modified: directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/CacheService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/CacheService.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/CacheService.java
(original)
+++ directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/CacheService.java
Wed Oct 31 14:20:55 2012
@@ -26,6 +26,8 @@ import java.io.File;
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Status;
+import net.sf.ehcache.config.Configuration;
+import net.sf.ehcache.config.ConfigurationFactory;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -51,50 +53,111 @@ public class CacheService
     /** the ehcache cache manager */
     private CacheManager cacheManager;
 
+    private boolean initialized;
 
     public CacheService()
     {
     }
 
-
-    public void initialize( DirectoryService dirService )
+    
+    /**
+     * Creates a new instance of CacheService with the given cache manager.
+     *
+     * @param cachemanager
+     */
+    public CacheService( CacheManager cachemanager )
+    {
+        this.cacheManager = cachemanager;
+        if ( cachemanager != null )
+        {
+           initialized = true; 
+        }
+    }
+    
+    
+    public void initialize( InstanceLayout layout )
     {
+        if ( initialized )
+        {
+            LOG.debug( "CacheService was already initialized, returning" );
+            return;
+        }
+        
         if ( ( cacheManager != null ) && ( cacheManager.getStatus() == Status.STATUS_ALIVE
) )
         {
             LOG.warn( "cache service was already initialized and is alive" );
+            initialized = true;
             return;
         }
 
-        File configFile = new File( dirService.getInstanceLayout().getConfDirectory(), DIRECTORY_CACHESERVICE_XML
);
+        File configFile = new File( layout.getConfDirectory(), DIRECTORY_CACHESERVICE_XML
);
 
+        Configuration cc;
+        
         if ( !configFile.exists() )
         {
             LOG.info( "no custom cache configuration was set, loading the default cache configuration"
);
-
-            cacheManager = new CacheManager( getClass().getClassLoader().getResource( DIRECTORY_CACHESERVICE_XML
) );
+            cc = ConfigurationFactory.parseConfiguration( getClass().getClassLoader().getResource(
DIRECTORY_CACHESERVICE_XML ) );
         }
         else
         {
             LOG.info( "loading cache configuration from the file {}", configFile );
 
-            cacheManager = new CacheManager( configFile.getAbsolutePath() );
+            cc = ConfigurationFactory.parseConfiguration( configFile );
         }
+        
+        cc.getDiskStoreConfiguration().setPath( layout.getCacheDirectory().getAbsolutePath()
);
+        cacheManager = new CacheManager( cc );
+
+        initialized = true;
     }
 
 
     public void destroy()
     {
+        if ( !initialized )
+        {
+            return;
+        }
+
         LOG.info( "clearing all the caches" );
 
+        initialized = false;
+
         cacheManager.clearAll();
-        cacheManager.shutdown();
     }
 
 
     public Cache getCache( String name )
     {
+        if ( !initialized )
+        {
+            throw new IllegalStateException( "CacheService was not initialized" );
+        }
+
         LOG.info( "fetching the cache named {}", name );
 
-        return cacheManager.getCache( name );
+        Cache cache = cacheManager.getCache( name );
+        
+        if( cache == null )
+        {
+            cacheManager.addCache( name );
+            cache = cacheManager.getCache( name );
+        }
+        
+        return cache;
+    }
+
+
+    public void remove( String name )
+    {
+        cacheManager.removeCache( name );
     }
+
+
+    public void attach( Cache cache )
+    {
+        cacheManager.addCache( cache );
+    }
+
 }

Modified: directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/DirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/DirectoryService.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/DirectoryService.java
(original)
+++ directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/DirectoryService.java
Wed Oct 31 14:20:55 2012
@@ -47,7 +47,6 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.model.schema.SchemaManager;
 import org.apache.directory.shared.ldap.util.tree.DnNode;
 
-
 /**
  * Provides JNDI service to {@link AbstractContextFactory}.
  *
@@ -614,4 +613,12 @@ public interface DirectoryService extend
      * @return the Dn factory
      */
     DnFactory getDnFactory();
+
+    
+    /**
+     * Sets the CacheService
+     * 
+     * @param cacheService the cache service
+     */
+    void setCacheService( CacheService cacheService );
 }

Modified: directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/AbstractPartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/AbstractPartition.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/AbstractPartition.java
(original)
+++ directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/AbstractPartition.java
Wed Oct 31 14:20:55 2012
@@ -25,6 +25,7 @@ import java.io.OutputStream;
 
 import javax.naming.InvalidNameException;
 
+import org.apache.directory.server.core.api.CacheService;
 import org.apache.directory.server.i18n.I18n;
 import org.apache.directory.shared.ldap.model.entry.Entry;
 import org.apache.directory.shared.ldap.model.exception.LdapException;
@@ -59,7 +60,9 @@ public abstract class AbstractPartition 
     /** The root Dn for this partition */
     protected Dn suffixDn;
 
-
+    /** the cache service */
+    protected CacheService cacheService;
+    
     /**
      * {@inheritDoc}
      */
@@ -234,4 +237,14 @@ public abstract class AbstractPartition 
     {
         this.contextEntry = contextEntry;
     }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setCacheService( CacheService cacheService )
+    {
+        this.cacheService = cacheService;
+    }
 }

Modified: directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/Partition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/Partition.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/Partition.java
(original)
+++ directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/api/partition/Partition.java
Wed Oct 31 14:20:55 2012
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.UUID;
 
+import org.apache.directory.server.core.api.CacheService;
 import org.apache.directory.server.core.api.entry.ServerSearchResult;
 import org.apache.directory.server.core.api.filtering.EntryFilteringCursor;
 import org.apache.directory.server.core.api.interceptor.context.AddOperationContext;
@@ -294,4 +295,12 @@ public interface Partition
      * @throws IOException if we can't write the data
      */
     void dumpIndex( OutputStream stream, String name ) throws IOException;
+    
+    
+    /**
+     * set the Cache service 
+     *
+     * @param cacheService
+     */
+    void setCacheService( CacheService cacheService );
 }

Modified: directory/apacheds/trunk/core-api/src/main/resources/directory-cacheservice.xml
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/resources/directory-cacheservice.xml?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/resources/directory-cacheservice.xml (original)
+++ directory/apacheds/trunk/core-api/src/main/resources/directory-cacheservice.xml Wed Oct
31 14:20:55 2012
@@ -160,4 +160,29 @@
 		   timeToIdleSeconds="300"
 		   diskPersistent="false" />
 		   
+		   <!-- partition caches
+		     the name format:
+		     name="{partition-suffix}" for partition
+		     name="{partition-suffix}:{index name/OID}" for each partition's index (currently only
RDN index supports cache)
+		    -->
+	<cache name="ou=system" 
+	       maxElementsInMemory="1000"
+		   eternal="false" 
+		   overflowToDisk="true"
+		   diskSpoolBufferSizeMB="20"
+		   timeToIdleSeconds="300"
+		   timeToLiveSeconds="600"
+		   memoryStoreEvictionPolicy="LFU"
+		   diskPersistent="false"/>
+		   
+	<cache name="ou=system:apacheRdn" 
+	       maxElementsInMemory="10000"
+		   eternal="false" 
+		   overflowToDisk="false"
+		   timeToIdleSeconds="300"
+		   timeToLiveSeconds="600"
+		   memoryStoreEvictionPolicy="LFU"
+		   diskPersistent="false"/>
+		   
+		   
 </ehcache>
\ No newline at end of file

Modified: directory/apacheds/trunk/core-api/src/test/java/org/apache/directory/server/core/api/MockDirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/test/java/org/apache/directory/server/core/api/MockDirectoryService.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core-api/src/test/java/org/apache/directory/server/core/api/MockDirectoryService.java
(original)
+++ directory/apacheds/trunk/core-api/src/test/java/org/apache/directory/server/core/api/MockDirectoryService.java
Wed Oct 31 14:20:55 2012
@@ -612,4 +612,12 @@ public class MockDirectoryService implem
     {
         // TODO Auto-generated method stub
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setCacheService( CacheService cacheService )
+    {
+      // nothing
+    }
 }

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
Wed Oct 31 14:20:55 2012
@@ -1813,15 +1813,19 @@ public class DefaultDirectoryService imp
         {
             LOG.debug( "---> Initializing the DefaultDirectoryService " );
         }
-
+        
+        csnFactory.setReplicaId( replicaId );
+        
         // If no interceptor list is defined, setup a default list
         if ( interceptors == null )
         {
             setDefaultInterceptorConfigurations();
         }
 
-        cacheService = new CacheService();
-        cacheService.initialize( this );
+        if ( cacheService != null )
+        {
+            cacheService.initialize( instanceLayout );
+        }
 
         // Initialize the AP caches
         accessControlAPCache = new DnNode<AccessControlAdministrativePoint>();
@@ -1832,8 +1836,10 @@ public class DefaultDirectoryService imp
         dnFactory = new DefaultDnFactory( schemaManager, cacheService.getCache( "dnCache"
) );
 
         // triggers partition to load schema fully from schema partition
+        schemaPartition.setCacheService( cacheService );
         schemaPartition.initialize();
         partitions.add( schemaPartition );
+        systemPartition.setCacheService( cacheService );
         systemPartition.getSuffixDn().apply( schemaManager );
 
         adminDn = getDnFactory().create( ServerDNConstants.ADMIN_SYSTEM_DN );
@@ -2327,4 +2333,14 @@ public class DefaultDirectoryService imp
     {
         return evaluator;
     }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setCacheService( CacheService cacheService )
+    {
+        this.cacheService = cacheService;
+    }
+
 }
\ No newline at end of file

Modified: directory/apacheds/trunk/service/src/main/java/org/apache/directory/server/ApacheDsService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/service/src/main/java/org/apache/directory/server/ApacheDsService.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/service/src/main/java/org/apache/directory/server/ApacheDsService.java
(original)
+++ directory/apacheds/trunk/service/src/main/java/org/apache/directory/server/ApacheDsService.java
Wed Oct 31 14:20:55 2012
@@ -38,6 +38,7 @@ import org.apache.directory.server.confi
 import org.apache.directory.server.config.beans.LdapServerBean;
 import org.apache.directory.server.config.beans.NtpServerBean;
 import org.apache.directory.server.config.builder.ServiceBuilder;
+import org.apache.directory.server.core.api.CacheService;
 import org.apache.directory.server.core.api.CoreSession;
 import org.apache.directory.server.core.api.DirectoryService;
 import org.apache.directory.server.core.api.InstanceLayout;
@@ -161,9 +162,12 @@ public class ApacheDsService
 
         LOG.info( "using partition dir {}", partitionsDir.getAbsolutePath() );
 
+        CacheService cacheService = new CacheService();
+        cacheService.initialize( instanceLayout );
+
         initSchemaManager( instanceLayout );
         initSchemaLdifPartition( instanceLayout );
-        initConfigPartition( instanceLayout );
+        initConfigPartition( instanceLayout, cacheService );
 
         // Read the configuration
         cpReader = new ConfigPartitionReader( configPartition );
@@ -173,7 +177,7 @@ public class ApacheDsService
         DirectoryServiceBean directoryServiceBean = configBean.getDirectoryServiceBean();
 
         // Initialize the DirectoryService now
-        DirectoryService directoryService = initDirectoryService( instanceLayout, directoryServiceBean
);
+        DirectoryService directoryService = initDirectoryService( instanceLayout, directoryServiceBean,
cacheService );
 
         // start the LDAP server
         startLdap( directoryServiceBean.getLdapServerBean(), directoryService );
@@ -258,9 +262,10 @@ public class ApacheDsService
      * initializes a LDIF partition for configuration
      * 
      * @param instanceLayout the instance layout
+     * @param cacheService the Cache service
      * @throws Exception in case of any issues while extracting the schema
      */
-    private void initConfigPartition( InstanceLayout instanceLayout ) throws Exception
+    private void initConfigPartition( InstanceLayout instanceLayout, CacheService cacheService
) throws Exception
     {
         File confFile = new File( instanceLayout.getConfDirectory(), LdifConfigExtractor.LDIF_CONFIG_FILE
);
 
@@ -280,13 +285,14 @@ public class ApacheDsService
         configPartition.setPartitionPath( confFile.toURI() );
         configPartition.setSuffixDn( new Dn( schemaManager, "ou=config" ) );
         configPartition.setSchemaManager( schemaManager );
-
+        configPartition.setCacheService( cacheService );
+        
         configPartition.initialize();
     }
 
 
     private DirectoryService initDirectoryService( InstanceLayout instanceLayout,
-        DirectoryServiceBean directoryServiceBean ) throws Exception
+        DirectoryServiceBean directoryServiceBean, CacheService cacheService ) throws Exception
     {
         LOG.info( "Initializing the DirectoryService..." );
 
@@ -304,7 +310,9 @@ public class ApacheDsService
 
         // Store the default directories
         directoryService.setInstanceLayout( instanceLayout );
-
+        
+        directoryService.setCacheService( cacheService );
+        
         directoryService.startup();
 
         AttributeType ocAt = schemaManager.lookupAttributeTypeRegistry( SchemaConstants.OBJECT_CLASS_AT
);
@@ -374,14 +382,7 @@ public class ApacheDsService
         ldapServer.setDirectoryService( directoryService );
 
         // And start the server now
-        try
-        {
-            ldapServer.start();
-        }
-        catch ( Exception e )
-        {
-            LOG.error( "Cannot start the server : " + e.getMessage() );
-        }
+        ldapServer.start();
 
         LOG.info( "LDAP server: started in {} milliseconds", ( System.currentTimeMillis()
- startTime ) + "" );
     }

Modified: directory/apacheds/trunk/xdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/AbstractBTreePartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/xdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/AbstractBTreePartition.java?rev=1404161&r1=1404160&r2=1404161&view=diff
==============================================================================
--- directory/apacheds/trunk/xdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/AbstractBTreePartition.java
(original)
+++ directory/apacheds/trunk/xdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/AbstractBTreePartition.java
Wed Oct 31 14:20:55 2012
@@ -45,6 +45,7 @@ import org.apache.directory.server.core.
 import org.apache.directory.server.core.api.interceptor.context.ModifyOperationContext;
 import org.apache.directory.server.core.api.interceptor.context.MoveAndRenameOperationContext;
 import org.apache.directory.server.core.api.interceptor.context.MoveOperationContext;
+import org.apache.directory.server.core.api.interceptor.context.OperationContext;
 import org.apache.directory.server.core.api.interceptor.context.RenameOperationContext;
 import org.apache.directory.server.core.api.interceptor.context.SearchOperationContext;
 import org.apache.directory.server.core.api.interceptor.context.UnbindOperationContext;
@@ -802,6 +803,8 @@ public abstract class AbstractBTreeParti
 
             // We now defer the deletion to the implementing class
             delete( id );
+            
+            updateCache( deleteContext );
         }
         catch ( LdapException le )
         {
@@ -1180,7 +1183,14 @@ public abstract class AbstractBTreeParti
         try
         {
             lockRead();
-            Entry entry = master.get( id );
+            Entry entry = lookupCache( id );
+            
+            if( entry != null )
+            {
+                return new ClonedServerEntry( entry );
+            }
+            
+            entry = master.get( id );
 
             if ( entry != null )
             {
@@ -1195,6 +1205,8 @@ public abstract class AbstractBTreeParti
 
                 entry.setDn( dn );
 
+                addToCache( id, entry );
+                
                 return new ClonedServerEntry( entry );
             }
 
@@ -1229,8 +1241,13 @@ public abstract class AbstractBTreeParti
     {
         try
         {
-            Entry entry = null;
-
+            Entry entry = lookupCache( id );
+            
+            if( entry != null )
+            {
+                return new ClonedServerEntry( entry );    
+            }
+            
             try
             {
                 lockRead();
@@ -1249,6 +1266,7 @@ public abstract class AbstractBTreeParti
                     entry.setDn( dn );
                 }
 
+                addToCache( id, entry );
                 return new ClonedServerEntry( entry );
             }
 
@@ -1275,6 +1293,8 @@ public abstract class AbstractBTreeParti
                 modifyContext.getModItems().toArray( new Modification[]
                     {} ) );
             modifyContext.setAlteredEntry( modifiedEntry );
+            
+            updateCache( modifyContext );
         }
         catch ( Exception e )
         {
@@ -1618,6 +1638,7 @@ public abstract class AbstractBTreeParti
             Entry modifiedEntry = moveContext.getModifiedEntry();
 
             move( oldDn, newSuperior, newDn, modifiedEntry );
+            updateCache( moveContext );
         }
         catch ( Exception e )
         {
@@ -1741,6 +1762,7 @@ public abstract class AbstractBTreeParti
             Entry modifiedEntry = moveAndRenameContext.getModifiedEntry();
 
             moveAndRename( oldDn, newSuperiorDn, newRdn, modifiedEntry, deleteOldRdn );
+            updateCache( moveAndRenameContext );
         }
         catch ( LdapException le )
         {
@@ -1908,6 +1930,8 @@ public abstract class AbstractBTreeParti
             {
                 rename( oldDn, newRdn, deleteOldRdn, null );
             }
+            
+            updateCache( renameContext );
         }
         catch ( Exception e )
         {
@@ -2881,4 +2905,41 @@ public abstract class AbstractBTreeParti
     {
         masterTableLock.writeLock().unlock();
     }
+    
+    
+    /**
+     * updates the cache based on the type of OperationContext
+     * 
+     * @param opCtx the operation's context
+     */
+    public void updateCache( OperationContext opCtx )
+    {
+        // partition implementations should override this if they want to use cache
+    }
+
+    
+    /**
+     * looks up for the entry with the given ID in the cache
+     *
+     * @param id the ID of the entry
+     * @return the Entry if exists, null otherwise
+     */
+    public Entry lookupCache( String id )
+    {
+        return null;
+    }
+    
+    
+    /**
+     * adds the given entry to cache
+     *  
+     * Note: this method is not called during add operation to avoid filling the cache
+     *       with all the added entries
+     *       
+     * @param id ID of the entry
+     * @param entry the Entry
+     */
+    public void addToCache( String id, Entry entry )
+    {
+    }
 }



Mime
View raw message