Package org.jboss.cache.loader

Source Code of org.jboss.cache.loader.JDBCCacheLoaderStateTransferTest

/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.cache.loader;

import org.jboss.cache.CacheSPI;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.transaction.GenericTransactionManagerLookup;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

/**
* UT for testing JDBCCacheLoader during state transfer.
* @author Mircea.Markus@jboss.com
* @since 3.0
*/
@Test(groups = "functional")
public class JDBCCacheLoaderStateTransferTest extends AbstractCacheLoaderTestBase
{

   CacheSPI first;
   CacheSPI second;

   @AfterMethod
   public void tearDown()
   {
      if (first != null) first.stop();
      if (second != null) second.stop();
   }

   private Configuration getConfiguration(int instance) throws Exception
   {
      Configuration c = new Configuration();
      c.setTransactionManagerLookupClass(GenericTransactionManagerLookup.class.getName());
      CacheLoaderConfig clc = getSingleCacheLoaderConfig("/", JDBCCacheLoader.class.getName(), "cache.jdbc.table.name=jbosscache\n" +
            "            cache.jdbc.table.create=true\n" +
            "            cache.jdbc.table.drop=true\n" +
            "            cache.jdbc.table.primarykey=jbosscache_pk\n" +
            "            cache.jdbc.fqn.column=fqn\n" +
            "            cache.jdbc.fqn.type=varchar(255)\n" +
            "            cache.jdbc.node.column=node\n" +
            "            cache.jdbc.node.type=blob\n" +
            "            cache.jdbc.parent.column=parent\n" +
            "            cache.jdbc.sql-concat=1 || 2\n" +
            "            cache.jdbc.driver = org.apache.derby.jdbc.EmbeddedDriver\n" +
            "            cache.jdbc.url=jdbc:derby:jbossdb"+instance+";create=true\n" +
            "            cache.jdbc.user=user1\n" +
            "            cache.jdbc.password=user1\n" +
            "            cache.jdbc.batch.enable=true\n" +
            "            cache.jdbc.batch.size=1000", false, true, false);
      clc.setPassivation(false);
      clc.getFirstCacheLoaderConfig().setPurgeOnStartup(true);     
      c.setCacheLoaderConfig(clc);
      c.setCacheMode(CacheMode.REPL_SYNC);
      return c;
   }

   public void testSimpleStateTransfer() throws Exception
   {
      first = (CacheSPI) new DefaultCacheFactory().createCache(getConfiguration(0));
      first.put("/a/b/c", "key", "value");
      first.put("/a/b/d", "key", "value");
      first.put("/a/b/e", "key", "value");
      second = (CacheSPI) new DefaultCacheFactory().createCache(getConfiguration(2));
      assert second.get("/a/b/c","key").equals("value");
      assert second.get("/a/b/d","key").equals("value");
      assert second.get("/a/b/e","key").equals("value");
      JDBCCacheLoader cacheLoader = (JDBCCacheLoader) second.getCacheLoaderManager().getCacheLoader();
      assert cacheLoader.exists(Fqn.fromString("/a"));
      assert cacheLoader.exists(Fqn.fromString("/a/b"));
   }


   public void testMoreState() throws Exception
   {
      long startTime = System.currentTimeMillis();

      first = (CacheSPI) new DefaultCacheFactory().createCache(getConfiguration(0));
      long cacheStartTime = System.currentTimeMillis() - startTime;
      System.out.println("cacheStartTime = " + cacheStartTime);
      for (int i = 0; i < 5012; i++)
      {
         first.put("a/b/"+i, "k","v");
         if (i%1000 == 0) System.out.println(i + " operations executed so far");
      }
      startTime = System.currentTimeMillis();
      second = (CacheSPI) new DefaultCacheFactory().createCache(getConfiguration(2));

      long stateTranferTime = System.currentTimeMillis() - startTime - cacheStartTime;
      for (int i = 0; i < 5012; i+=100)
      {
         second.get("a/b/"+ i, "k").equals("v");
      }
      System.out.println("stateTranferTime = " + stateTranferTime);
   }
}
TOP

Related Classes of org.jboss.cache.loader.JDBCCacheLoaderStateTransferTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.