/** * This constructs an optimizer with the disk cacn properties. * <p> * @param attributes * @param tableState We mark the table status as optimizing when this is happening. * @param dataSource access to the database */ public MySQLTableOptimizer( MySQLDiskCacheAttributes attributes, TableState tableState, DataSource dataSource ) { setTableName( attributes.getTableName() ); this.tableState = tableState; this.dataSource = dataSource; }
/** * This factory method should create an instance of the mysqlcache. * <p> * @param rawAttr * @param compositeCacheManager * @param cacheEventLogger * @param elementSerializer * @return MySQLDiskCache * @throws SQLException if the creation of the cache instance fails */ @Override public <K, V> MySQLDiskCache<K, V> createCache( AuxiliaryCacheAttributes rawAttr, ICompositeCacheManager compositeCacheManager, ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) throws SQLException { MySQLDiskCacheAttributes cattr = (MySQLDiskCacheAttributes) rawAttr; TableState tableState = getTableState( cattr.getTableName() ); MySQLDiskCache<K, V> cache = new MySQLDiskCache<K, V>( cattr, tableState, compositeCacheManager ); cache.setCacheEventLogger( cacheEventLogger ); cache.setElementSerializer( elementSerializer ); // create a shrinker if we need it. createShrinkerWhenNeeded( cattr, cache ); scheduleOptimizations( cattr, tableState, cache.getPoolAccess() ); return cache; }
/** * This factory method should create an instance of the mysqlcache. * <p> * @param rawAttr specific cache configuration attributes * @param compositeCacheManager the global cache manager * @param cacheEventLogger a specific logger for cache events * @param elementSerializer a serializer for cache elements * @return MySQLDiskCache the cache instance * @throws SQLException if the cache instance could not be created */ @Override public <K, V> MySQLDiskCache<K, V> createCache( AuxiliaryCacheAttributes rawAttr, ICompositeCacheManager compositeCacheManager, ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) throws SQLException { MySQLDiskCacheAttributes cattr = (MySQLDiskCacheAttributes) rawAttr; TableState tableState = getTableState( cattr.getTableName() ); DataSourceFactory dsFactory = getDataSourceFactory(cattr, compositeCacheManager.getConfigurationProperties()); MySQLDiskCache<K, V> cache = new MySQLDiskCache<K, V>( cattr, dsFactory, tableState, compositeCacheManager ); cache.setCacheEventLogger( cacheEventLogger ); cache.setElementSerializer( elementSerializer ); // create a shrinker if we need it. createShrinkerWhenNeeded( cattr, cache ); scheduleOptimizations( cattr, tableState, cache.getDataSource() ); return cache; }
/** * Run the optimization against live a table. * <p> * @throws Exception */ public void testBasicOptimizationUnknownTable() throws Exception { // SETUP MySQLDiskCacheAttributes attributes = new MySQLDiskCacheAttributes(); attributes.setUserName( "java" ); attributes.setPassword( "letmein" ); attributes.setUrl( "jdbc:mysql://10.19.98.43:3306/flight_option_cache" ); attributes.setDriverClassName( "com.mysql.jdbc.Driver" ); String tableName = "DOESNTEXIST"; attributes.setTableName( tableName ); TableState tableState = new TableState( tableName ); MySQLTableOptimizer optimizer = new MySQLTableOptimizer( attributes, tableState, JDBCDiskCachePoolAccessManager .createPoolAccess( attributes ) ); // DO WORK optimizer.optimizeTable(); } }
MySQLDiskCacheAttributes attributes = new MySQLDiskCacheAttributes(); String tableName = "JCS_TEST"; attributes.setDriverClassName( "org.hsqldb.jdbcDriver" ); attributes.setTableName( tableName ); attributes.setBalkDuringOptimization( true ); MySQLDiskCache<String, String> cache = new MySQLDiskCache<String, String>( attributes, tableState, CompositeCacheManager.getUnconfiguredInstance() ); Object result = cache.processGet( "myKey" ); assertNull( "The result should be null", result );
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and do nothing. * <p> * @param element */ @Override protected void processUpdate( ICacheElement<K, V> element ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return; } } super.processUpdate( element ); }
/** * This takes in a single time and schedules the optimizer to be called at that time every day. * <p> * @param startTime -- HH:MM:SS format * @param optimizer */ protected void scheduleOptimization( Date startTime, MySQLTableOptimizer optimizer ) { if ( log.isInfoEnabled() ) { log.info( "startTime [" + startTime + "] for optimizer " + optimizer ); } // get the runnable from the factory OptimizerTask runnable = new OptimizerTask( optimizer ); Date now = new Date(); long initialDelay = startTime.getTime() - now.getTime(); // have the daemon execute our runnable getScheduledExecutorService().scheduleAtFixedRate(runnable, initialDelay, 86400000L, TimeUnit.MILLISECONDS ); }
/** * This calls optimize on the optimizer. * <p> * @see java.lang.Runnable#run() */ @Override public void run() { if ( optimizer != null ) { boolean success = optimizer.optimizeTable(); if ( log.isInfoEnabled() ) { log.info( "Optimization success status [" + success + "]" ); } } else { log.warn( "OptimizerRunner: The optimizer is null. Could not optimize table." ); } } }
/** * Test the basic JDBC disk cache functionality with a hsql backing. * @throws Exception */ public void testSimpleJDBCPutGetWithHSQL() throws Exception { runTestForRegion( "testCache1", 200 ); }
/** * Run the optimization against live a table. * <p> * @throws Exception */ public void testBasicOptimization() throws Exception { // SETUP MySQLDiskCacheAttributes attributes = new MySQLDiskCacheAttributes(); attributes.setUserName( "java" ); attributes.setPassword( "letmein" ); attributes.setUrl( "jdbc:mysql://10.19.98.43:3306/flight_option_cache" ); attributes.setDriverClassName( "com.mysql.jdbc.Driver" ); String tableName = "JCS_STORE_FLIGHT_OPTION_ITINERARY"; attributes.setTableName( tableName ); TableState tableState = new TableState( tableName ); MySQLTableOptimizer optimizer = new MySQLTableOptimizer( attributes, tableState, JDBCDiskCachePoolAccessManager .createPoolAccess( attributes ) ); // DO WORK optimizer.optimizeTable(); }
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and return null. * <p> * @param pattern used for like query. * @return An object matching key, or null. */ @Override protected Map<K, ICacheElement<K, V>> processGetMatching( String pattern ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return null; } } return super.processGetMatching( pattern ); }
/** * This constructs an optimizer with the disk cacn properties. * <p> * @param attributes * @param tableState We mark the table status as optimizing when this is happening. * @param poolAccess access to the database */ public MySQLTableOptimizer( MySQLDiskCacheAttributes attributes, TableState tableState, JDBCDiskCachePoolAccess poolAccess ) { setTableName( attributes.getTableName() ); this.tableState = tableState; this.poolAccess = poolAccess; }
/** * This takes in a single time and schedules the optimizer to be called at that time every day. * <p> * @param startTime -- HH:MM:SS format * @param optimizer */ protected void scheduleOptimization( Date startTime, MySQLTableOptimizer optimizer ) { if ( log.isInfoEnabled() ) { log.info( "startTime [" + startTime + "] for optimizer " + optimizer ); } // get the runnable from the factory OptimizerTask runnable = new OptimizerTask( optimizer ); Date now = new Date(); long initialDelay = startTime.getTime() - now.getTime(); // have the daemon execute our runnable getScheduledExecutorService().scheduleAtFixedRate(runnable, initialDelay, 86400000L, TimeUnit.MILLISECONDS ); }
/** * Test the basic JDBC disk cache functionality with a hsql backing. * @throws Exception */ @Test public void testSimpleJDBCPutGetWithHSQL() throws Exception { runTestForRegion( "testCache1", 200 ); }
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and return null. * <p> * @param key Key to locate value for. * @return An object matching key, or null. */ @Override protected ICacheElement<K, V> processGet( K key ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return null; } } return super.processGet( key ); }
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and do nothing. * <p> * @param element */ @Override protected void processUpdate( ICacheElement<K, V> element ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return; } } super.processUpdate( element ); }
/** * Removed the expired. (now - create time) > max life seconds * 1000 * <p> * If we are currently optimizing, then this method will balk and do nothing. * <p> * TODO consider blocking and trying again. * <p> * @return the number deleted */ @Override protected int deleteExpired() { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return -1; } } return super.deleteExpired(); } }
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and return null. * <p> * @param key Key to locate value for. * @return An object matching key, or null. */ @Override protected ICacheElement<K, V> processGet( K key ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return null; } } return super.processGet( key ); }
/** * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this * method will balk and return null. * <p> * @param pattern used for like query. * @return An object matching key, or null. */ @Override protected Map<K, ICacheElement<K, V>> processGetMatching( String pattern ) { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return null; } } return super.processGetMatching( pattern ); }
/** * Removed the expired. (now - create time) > max life seconds * 1000 * <p> * If we are currently optimizing, then this method will balk and do nothing. * <p> * TODO consider blocking and trying again. * <p> * @return the number deleted */ @Override protected int deleteExpired() { if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) { if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) { return -1; } } return super.deleteExpired(); } }