[hibernate-commits] Hibernate SVN: r11458 - in trunk/HibernateExt/shards/src: test/org/hibernate/shards and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed May 2 02:23:27 EDT 2007


Author: max.ross
Date: 2007-05-02 02:23:27 -0400 (Wed, 02 May 2007)
New Revision: 11458

Modified:
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardedConfiguration.java
   trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardedConfigurationTest.java
   trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java
Log:
simplify the way we establish our ShardedConfiguration

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardedConfiguration.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardedConfiguration.java	2007-05-02 06:18:10 UTC (rev 11457)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardedConfiguration.java	2007-05-02 06:23:27 UTC (rev 11458)
@@ -18,24 +18,24 @@
 
 package org.hibernate.shards;
 
-import org.hibernate.shards.cfg.ShardedEnvironment;
-import org.hibernate.shards.strategy.ShardStrategyFactory;
-import org.hibernate.shards.session.ShardedSessionFactoryImpl;
-import org.hibernate.shards.session.ShardedSessionFactory;
-import org.hibernate.shards.util.Preconditions;
-import org.hibernate.shards.util.Sets;
-import org.hibernate.shards.util.Maps;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
 import org.hibernate.engine.SessionFactoryImplementor;
 import org.hibernate.mapping.OneToOne;
 import org.hibernate.mapping.PersistentClass;
 import org.hibernate.mapping.Property;
+import org.hibernate.shards.cfg.ShardConfiguration;
+import org.hibernate.shards.cfg.ShardedEnvironment;
+import org.hibernate.shards.session.ShardedSessionFactory;
+import org.hibernate.shards.session.ShardedSessionFactoryImpl;
+import org.hibernate.shards.strategy.ShardStrategyFactory;
+import org.hibernate.shards.util.Maps;
+import org.hibernate.shards.util.Preconditions;
+import org.hibernate.shards.util.Sets;
 import org.hibernate.util.PropertiesHelper;
-import org.hibernate.SessionFactory;
 
 import java.util.Collections;
 import java.util.Iterator;
@@ -59,7 +59,7 @@
   private final Configuration prototypeConfiguration;
 
   // shard-specific configs
-  private final List<Configuration> configurations;
+  private final List<ShardConfiguration> shardConfigs;
 
   // user-defined sharding behavior
   private final ShardStrategyFactory shardStrategyFactory;
@@ -73,80 +73,57 @@
   // our lovely logger
   private final Log log = LogFactory.getLog(getClass());
 
-  // constant used in config files to specify the shard id
-  static final String SHARD_ID_PROPERTY = "hibernate.connection.shard_id";
-
-  // the properties that we let users vary across shards.  if the property
-  // isn't in this list we'll take the value from the prototype config
-  static final Set<String> VARIABLE_PROPERTIES = Sets.newHashSet(
-      Environment.URL,
-      Environment.USER,
-      Environment.PASS,
-      Environment.SESSION_FACTORY_NAME,
-      SHARD_ID_PROPERTY);
-
   /**
    * Constructs a ShardedConfiguration.
    *
-   * @param prototypeConfiguration The prototype for all configurations that
+   * @param prototypeConfiguration The prototype for all shardConfigs that
    * will be used to create the {@link SessionFactory} objects
    * that are internal to the {@link ShardedSessionFactory}.
-   * Every {@link SessionFactory} within the
-   * {@link ShardedSessionFactory} objects created by the ShardedConfiguration
-   * will look the same, except for its values that correspond to
-   * Configuration properties that we consider to be "variable" (they can
-   * vary from shard to shard).  These properties are:
-   * {@link Environment#URL}
-   * {@link Environment#USER}
-   * {@link Environment#PASS}
-   * {@link Environment#SESSION_FACTORY_NAME}
-   * Unlike the {@link Configuration} instances contained in the configurations
-   * param, this {@link Configuration} needs to have all of its mappings.
+   * Every {@link org.hibernate.SessionFactory} within the
+   * {@link org.hibernate.shards.session.ShardedSessionFactory} objects created by the ShardedConfiguration
+   * will look the same, except for properties that we consider to be "variable" (they can
+   * vary from shard to shard).  The variable properties are defined by the
+   * {@link ShardedConfiguration} interface.
    *
-   * @param configurations The shard-specific {@link Configuration}s
+   * @param shardConfigs Shard-specific configuration data for each shard.
    * @param shardStrategyFactory factory that knows how to create the right type of shard strategy
    */
   public ShardedConfiguration(
       Configuration prototypeConfiguration,
-      List<Configuration> configurations,
+      List<ShardConfiguration> shardConfigs,
       ShardStrategyFactory shardStrategyFactory) {
-    this(prototypeConfiguration, configurations, shardStrategyFactory, Maps.<Integer, Integer>newHashMap());
+    this(prototypeConfiguration, shardConfigs, shardStrategyFactory, Maps.<Integer, Integer>newHashMap());
   }
 
   /**
    * Constructs a ShardedConfiguration.
    *
-   * @param prototypeConfiguration The prototype for all configurations that
+   * @param prototypeConfiguration The prototype for all shardConfigs that
    * will be used to create the {@link org.hibernate.SessionFactory} objects
    * that are internal to the {@link org.hibernate.shards.session.ShardedSessionFactory}.
    * Every {@link org.hibernate.SessionFactory} within the
    * {@link org.hibernate.shards.session.ShardedSessionFactory} objects created by the ShardedConfiguration
-   * will look the same, except for its values that correspond to
-   * Configuration properties that we consider to be "variable" (they can
-   * vary from shard to shard).  These properties are:
-   * {@link org.hibernate.cfg.Environment#URL}
-   * {@link org.hibernate.cfg.Environment#USER}
-   * {@link org.hibernate.cfg.Environment#PASS}
-   * {@link org.hibernate.cfg.Environment#SESSION_FACTORY_NAME}
-   * Unlike the {@link org.hibernate.cfg.Configuration} instances contained in the configurations
-   * param, this {@link org.hibernate.cfg.Configuration} needs to have all of its mappings.
-   *@param configurations Cannot be empty.
+   * will look the same, except for properties that we consider to be "variable" (they can
+   * vary from shard to shard).  The variable properties are defined by the
+   * {@link ShardedConfiguration} interface.
+   *
+   * @param shardConfigs Shard-specific configuration data for each shard.
    * @param shardStrategyFactory factory that knows how to create the right kind of shard strategy
    * @param virtualShardToShardMap A map that maps virtual shard ids to real
    */
   public ShardedConfiguration(
       Configuration prototypeConfiguration,
-      List<Configuration> configurations,
+      List<ShardConfiguration> shardConfigs,
       ShardStrategyFactory shardStrategyFactory,
       Map<Integer, Integer> virtualShardToShardMap) {
     Preconditions.checkNotNull(prototypeConfiguration);
-    Preconditions.checkNotNull(configurations);
-    Preconditions.checkArgument(!configurations.isEmpty());
+    Preconditions.checkNotNull(shardConfigs);
+    Preconditions.checkArgument(!shardConfigs.isEmpty());
     Preconditions.checkNotNull(shardStrategyFactory);
     Preconditions.checkNotNull(virtualShardToShardMap);
 
     this.prototypeConfiguration = prototypeConfiguration;
-    this.configurations = configurations;
+    this.shardConfigs = shardConfigs;
     this.shardStrategyFactory = shardStrategyFactory;
     this.virtualShardToShardMap = virtualShardToShardMap;
     if (!virtualShardToShardMap.isEmpty()) {
@@ -178,10 +155,16 @@
     // we can get the set from the prototype and then just reuse it.
     Set<Class<?>> classesWithoutTopLevelSaveSupport =
         determineClassesWithoutTopLevelSaveSupport(prototypeConfiguration);
-    for (Configuration config : configurations) {
+    for (ShardConfiguration config : shardConfigs) {
       populatePrototypeWithVariableProperties(config);
       // get the shardId from the shard-specific config
-      Integer shardId = Integer.parseInt(config.getProperty(SHARD_ID_PROPERTY));
+      Integer shardId = config.getShardId();
+      if(shardId == null) {
+        final String msg = "Attempt to build a ShardedSessionFactory using a "
+            + "ShardConfiguration that has a null shard id.";
+        log.fatal(msg);
+        throw new NullPointerException(msg);
+      }
       Set<ShardId> virtualShardIds;
       if (virtualShardToShardMap.isEmpty()) {
         // simple case, virtual and physical are the same
@@ -241,29 +224,15 @@
    * a shard-specific config and sets them as the values of the same properties
    * in the prototype config.
    */
-  void populatePrototypeWithVariableProperties(Configuration config) {
-    copyVariableProperties(prototypeConfiguration, config);
+  void populatePrototypeWithVariableProperties(ShardConfiguration config) {
+    prototypeConfiguration.setProperty(Environment.USER, config.getShardUser());
+    prototypeConfiguration.setProperty(Environment.PASS, config.getShardPassword());
+    prototypeConfiguration.setProperty(Environment.URL, config.getShardUrl());
+    prototypeConfiguration.setProperty(Environment.SESSION_FACTORY_NAME, config.getShardSessionFactoryName());
+    prototypeConfiguration.setProperty(ShardedEnvironment.SHARD_ID_PROPERTY, config.getShardId().toString());
   }
 
   /**
-   * Helper function to copy variable properties from the shard-specific config
-   * to the prototype config
-   */
-  static void copyVariableProperties(Configuration prototype, Configuration config) {
-    for(String property : VARIABLE_PROPERTIES) {
-      copyPropertyToPrototype(prototype, config, property);
-    }
-  }
-
-  /**
-   * Helper function to copy one variable property from the shard-specific config
-   * to the prototype config
-   */
-  static void copyPropertyToPrototype(Configuration prototype, Configuration config, String property) {
-    prototype.setProperty(property, config.getProperty(property));
-  }
-
-  /**
    * Helper function that creates an actual SessionFactory.
    */
   private SessionFactoryImplementor buildSessionFactory() {

Modified: trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardedConfigurationTest.java
===================================================================
--- trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardedConfigurationTest.java	2007-05-02 06:18:10 UTC (rev 11457)
+++ trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardedConfigurationTest.java	2007-05-02 06:23:27 UTC (rev 11458)
@@ -18,13 +18,9 @@
 
 package org.hibernate.shards;
 
-import org.hibernate.shards.strategy.ShardStrategy;
-import org.hibernate.shards.strategy.ShardStrategyFactoryDefaultMock;
-import org.hibernate.shards.session.ShardedSessionFactoryImpl;
-import org.hibernate.shards.util.Lists;
-
 import junit.framework.TestCase;
 
+import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
 import org.hibernate.dialect.HSQLDialect;
@@ -33,6 +29,11 @@
 import org.hibernate.mapping.Property;
 import org.hibernate.mapping.RootClass;
 import org.hibernate.mapping.Table;
+import org.hibernate.shards.cfg.ShardConfiguration;
+import org.hibernate.shards.session.ShardedSessionFactoryImpl;
+import org.hibernate.shards.strategy.ShardStrategy;
+import org.hibernate.shards.strategy.ShardStrategyFactoryDefaultMock;
+import org.hibernate.shards.util.Lists;
 
 import java.util.Collections;
 import java.util.List;
@@ -43,7 +44,7 @@
 public class ShardedConfigurationTest extends TestCase {
 
   private MyShardStrategyFactory shardStrategyFactory;
-  private Configuration config;
+  private ShardConfiguration shardConfig;
   private ShardedConfiguration shardedConfiguration;
 
   @Override
@@ -51,23 +52,21 @@
     super.setUp();
 
     shardStrategyFactory = new MyShardStrategyFactory();
-    config = new Configuration();
-    for(String prop : ShardedConfiguration.VARIABLE_PROPERTIES) {
-      config.setProperty(prop, "33");
-    }
-    config.setProperty(Environment.DIALECT, HSQLDialect.class.getName());
+    Configuration protoConfig = new Configuration();
+    protoConfig.setProperty(Environment.DIALECT, HSQLDialect.class.getName());
+    shardConfig = new MyShardConfig("user", "url", "pwd", "sfname", 33);
 
     shardedConfiguration =
         new ShardedConfiguration(
-            config,
-            Collections.singletonList(config),
+            protoConfig,
+            Collections.singletonList(shardConfig),
             shardStrategyFactory);
   }
 
   public void testBuildShardedSessionFactoryPreconditions() throws Exception {
-    List<Configuration> configList = Lists.newArrayList(config);
+    List<ShardConfiguration> shardConfigs = Lists.newArrayList(shardConfig);
     try {
-      new ShardedConfiguration(null, configList, shardStrategyFactory);
+      new ShardedConfiguration(null, shardConfigs, shardStrategyFactory);
       fail("Expected npe");
     } catch (NullPointerException npe) {
       // good
@@ -81,9 +80,9 @@
       // good
     }
 
-    configList.clear();
+    shardConfigs.clear();
     try {
-      new ShardedConfiguration(config, configList, shardStrategyFactory);
+      new ShardedConfiguration(config, shardConfigs, shardStrategyFactory);
       fail("Expected iae");
     } catch (IllegalArgumentException iae) {
       // good
@@ -91,7 +90,7 @@
   }
 
   public void testShardIdRequired() {
-    Configuration config = new Configuration();
+    ShardConfiguration config = new MyShardConfig("user", "url", "pwd", "sfname", null);
     try {
       shardedConfiguration.populatePrototypeWithVariableProperties(config);
       fail("expected npe");
@@ -100,17 +99,13 @@
     }
   }
 
-  public void testCopyPropertyToPrototype() {
-    Configuration prototype = new Configuration();
-    String copyMe = "copyMe";
-    config.setProperty(copyMe, "yamma");
-    ShardedConfiguration.copyPropertyToPrototype(prototype, config, copyMe);
-    assertEquals(config.getProperty(copyMe), prototype.getProperty(copyMe));
-  }
-
   public void testBuildShardedSessionFactory() {
     ShardedSessionFactoryImpl ssfi = (ShardedSessionFactoryImpl)shardedConfiguration.buildShardedSessionFactory();
     assertNotNull(ssfi);
+    // make sure the session factory contained in the sharded session factory
+    // has the number of session factories we expect
+    List<SessionFactory> sfList = ssfi.getSessionFactories();
+    assertEquals(1, sfList.size());
   }
 
   public void testRequiresShardLock() {
@@ -130,4 +125,42 @@
       return null;
     }
   }
+
+  private static final class MyShardConfig implements ShardConfiguration {
+
+    private final String user;
+    private final String url;
+    private final String password;
+    private final String sessionFactoryName;
+    private final Integer shardId;
+
+    public MyShardConfig(String user, String url, String password,
+        String sessionFactoryName, Integer shardId) {
+      this.user = user;
+      this.url = url;
+      this.password = password;
+      this.sessionFactoryName = sessionFactoryName;
+      this.shardId = shardId;
+    }
+
+    public String getShardUser() {
+      return user;
+    }
+
+    public String getShardUrl() {
+      return url;
+    }
+
+    public String getShardPassword() {
+      return password;
+    }
+
+    public String getShardSessionFactoryName() {
+      return sessionFactoryName;
+    }
+
+    public Integer getShardId() {
+      return shardId;
+    }
+  }
 }

Modified: trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java
===================================================================
--- trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java	2007-05-02 06:18:10 UTC (rev 11457)
+++ trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/BaseShardingIntegrationTestCase.java	2007-05-02 06:23:27 UTC (rev 11458)
@@ -18,17 +18,23 @@
 
 package org.hibernate.shards.integration;
 
+import junit.framework.TestCase;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
 import org.hibernate.shards.ShardId;
 import org.hibernate.shards.ShardedConfiguration;
-import org.hibernate.shards.session.ShardedSessionFactory;
+import org.hibernate.shards.cfg.ConfigurationToShardConfigurationAdapter;
+import org.hibernate.shards.cfg.ShardConfiguration;
 import org.hibernate.shards.integration.platform.DatabasePlatform;
 import org.hibernate.shards.integration.platform.DatabasePlatformFactory;
 import org.hibernate.shards.loadbalance.RoundRobinShardLoadBalancer;
+import org.hibernate.shards.session.ShardedSession;
+import org.hibernate.shards.session.ShardedSessionFactory;
 import org.hibernate.shards.session.ShardedSessionImpl;
-import org.hibernate.shards.session.ShardedSession;
 import org.hibernate.shards.strategy.ShardStrategy;
+import org.hibernate.shards.strategy.ShardStrategyFactory;
 import org.hibernate.shards.strategy.ShardStrategyImpl;
-import org.hibernate.shards.strategy.ShardStrategyFactory;
 import org.hibernate.shards.strategy.access.ParallelShardAccessStrategy;
 import org.hibernate.shards.strategy.access.SequentialShardAccessStrategy;
 import org.hibernate.shards.strategy.access.ShardAccessStrategy;
@@ -40,12 +46,6 @@
 import org.hibernate.shards.util.Lists;
 import org.hibernate.shards.util.Maps;
 
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
-import org.hibernate.Session;
-import org.hibernate.cfg.Configuration;
-
 import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -102,7 +102,8 @@
       destroyDatabase(i);
       createDatabase(i);
     }
-    List<Configuration> configurations = buildConfigurations();
+    Configuration prototypeConfig = buildPrototypeConfig();
+    List<ShardConfiguration> configurations = buildConfigurations();
     // now we use these configs to build our sharded config
     ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
     Map<Integer, Integer> virtualShardMap = buildVirtualShardToShardMap();
@@ -110,7 +111,7 @@
     // that all other configs will be the same
     ShardedConfiguration shardedConfig =
         new ShardedConfiguration(
-            configurations.get(0),
+            prototypeConfig,
             configurations,
             shardStrategyFactory,
             virtualShardMap);
@@ -132,16 +133,24 @@
     return virtualShardToShardMap;
   }
 
-  protected List<Configuration> buildConfigurations() {
+  private Configuration buildPrototypeConfig() {
     DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
     String dbPlatformConfigDirectory = "platform/" + dbPlatform.getName().toLowerCase() +"/config/";
     IdGenType idGenType = getIdGenType();
-    List<Configuration> configs = Lists.newArrayList();
+    Configuration config = new Configuration();
+    config.configure(BaseShardingIntegrationTestCase.class.getResource(dbPlatformConfigDirectory + "shard0.hibernate.cfg.xml"));
+    config.addURL(BaseShardingIntegrationTestCase.class.getResource(dbPlatformConfigDirectory + idGenType.getMappingFile()));
+    return config;
+  }
+
+  protected List<ShardConfiguration> buildConfigurations() {
+    DatabasePlatform dbPlatform = DatabasePlatformFactory.FACTORY.getDatabasePlatform();
+    String dbPlatformConfigDirectory = "platform/" + dbPlatform.getName().toLowerCase() +"/config/";
+    List<ShardConfiguration> configs = Lists.newArrayList();
     for(int i = 0; i < getNumDatabases(); i++) {
       Configuration config = new Configuration();
       config.configure(BaseShardingIntegrationTestCase.class.getResource(dbPlatformConfigDirectory + "shard" + i + ".hibernate.cfg.xml"));
-      config.addURL(BaseShardingIntegrationTestCase.class.getResource(dbPlatformConfigDirectory + idGenType.getMappingFile()));
-      configs.add(config);
+      configs.add(new ConfigurationToShardConfigurationAdapter(config));
     }
     return configs;
   }
@@ -158,6 +167,12 @@
     };
   }
 
+  protected void commitAndResetSession() {
+    session.getTransaction().commit();
+    resetSession();
+    session.beginTransaction();
+  }
+
   protected void resetSession() {
     MemoryLeakPlugger.plug((ShardedSessionImpl)session);
     session.close();
@@ -334,26 +349,11 @@
   public void runBare() throws Throwable {
     try {
       super.runBare();
-    } catch (AssertionFailedError afe) {
-      AssertionFailedError newError = new AssertionFailedError(perm.getMessageWithPermutationPrefix(afe.getMessage()));
-      newError.setStackTrace(afe.getStackTrace());
-      throw newError;
     } catch (Throwable t) {
-      throw new ThrowableDecorator(t);
+      throw new RuntimeException(perm.getMessageWithPermutationPrefix(t.getMessage()), t);
+      // TODO(maxr) handel assertion failure separately so they get properly reported
     }
   }
-
-  private final class ThrowableDecorator extends Throwable {
-
-    public ThrowableDecorator(Throwable t) {
-      super(t);
-    }
-
-    @Override
-    public String getMessage() {
-      return perm.getMessageWithPermutationPrefix(getCause().getMessage());
-    }
-  }
 }
 
 




More information about the hibernate-commits mailing list