| 1 |
package org.apache.commons.jcs3.auxiliary.disk.jdbc.mysql; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.sql.SQLException; |
| 23 |
import java.text.ParseException; |
| 24 |
import java.util.Date; |
| 25 |
import java.util.concurrent.TimeUnit; |
| 26 |
|
| 27 |
import javax.sql.DataSource; |
| 28 |
|
| 29 |
import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; |
| 30 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheFactory; |
| 31 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.TableState; |
| 32 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory; |
| 33 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.mysql.util.ScheduleParser; |
| 34 |
import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager; |
| 35 |
import org.apache.commons.jcs3.engine.behavior.IElementSerializer; |
| 36 |
import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; |
| 37 |
import org.apache.commons.jcs3.log.Log; |
| 38 |
import org.apache.commons.jcs3.log.LogManager; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class MySQLDiskCacheFactory |
| 44 |
extends JDBCDiskCacheFactory |
| 45 |
{ |
| 46 |
|
| 47 |
private static final Log log = LogManager.getLog( MySQLDiskCacheFactory.class ); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public <K, V> MySQLDiskCache<K, V> createCache( final AuxiliaryCacheAttributes rawAttr, |
| 61 |
final ICompositeCacheManager compositeCacheManager, |
| 62 |
final ICacheEventLogger cacheEventLogger, final IElementSerializer elementSerializer ) |
| 63 |
throws SQLException |
| 64 |
{ |
| 65 |
final MySQLDiskCacheAttributes cattr = (MySQLDiskCacheAttributes) rawAttr; |
| 66 |
final TableState tableState = getTableState( cattr.getTableName() ); |
| 67 |
final DataSourceFactory dsFactory = getDataSourceFactory(cattr, compositeCacheManager.getConfigurationProperties()); |
| 68 |
|
| 69 |
final MySQLDiskCache<K, V> cache = new MySQLDiskCache<>( cattr, dsFactory, tableState); |
| 70 |
cache.setCacheEventLogger( cacheEventLogger ); |
| 71 |
cache.setElementSerializer( elementSerializer ); |
| 72 |
|
| 73 |
|
| 74 |
createShrinkerWhenNeeded( cattr, cache ); |
| 75 |
scheduleOptimizations( cattr, tableState, cache.getDataSource() ); |
| 76 |
|
| 77 |
return cache; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected void scheduleOptimizations( final MySQLDiskCacheAttributes attributes, final TableState tableState, final DataSource ds ) |
| 89 |
{ |
| 90 |
if ( attributes != null ) |
| 91 |
{ |
| 92 |
if ( attributes.getOptimizationSchedule() != null ) |
| 93 |
{ |
| 94 |
log.info( "Will try to configure optimization for table [{0}] on schedule [{1}]", |
| 95 |
attributes::getTableName, attributes::getOptimizationSchedule); |
| 96 |
|
| 97 |
final MySQLTableOptimizer optimizer = new MySQLTableOptimizer( attributes, tableState, ds ); |
| 98 |
|
| 99 |
|
| 100 |
try |
| 101 |
{ |
| 102 |
|
| 103 |
final Date[] dates = ScheduleParser.createDatesForSchedule( attributes.getOptimizationSchedule() ); |
| 104 |
for (final Date date : dates) { |
| 105 |
this.scheduleOptimization( date, optimizer ); |
| 106 |
} |
| 107 |
} |
| 108 |
catch ( final ParseException e ) |
| 109 |
{ |
| 110 |
log.warn( "Problem creating optimization schedule for table [{0}]", |
| 111 |
attributes.getTableName(), e ); |
| 112 |
} |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
log.info( "Optimization is not configured for table [{0}]", |
| 117 |
attributes.getTableName()); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected void scheduleOptimization( final Date startTime, final MySQLTableOptimizer optimizer ) |
| 129 |
{ |
| 130 |
log.info( "startTime [{0}] for optimizer {1}", startTime, optimizer ); |
| 131 |
|
| 132 |
final Date now = new Date(); |
| 133 |
final long initialDelay = startTime.getTime() - now.getTime(); |
| 134 |
|
| 135 |
|
| 136 |
getScheduledExecutorService().scheduleAtFixedRate(() -> optimizeTable(optimizer), |
| 137 |
initialDelay, 86400L, TimeUnit.SECONDS ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private static void optimizeTable(final MySQLTableOptimizer optimizer) |
| 144 |
{ |
| 145 |
if ( optimizer != null ) |
| 146 |
{ |
| 147 |
final boolean success = optimizer.optimizeTable(); |
| 148 |
log.info( "Optimization success status [{0}]", success ); |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
log.warn( "OptimizerRunner: The optimizer is null. Could not optimize table." ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |