From 30d697d490d2dd3aa6df7b261598b57d35ebf770 Mon Sep 17 00:00:00 2001 From: rbygrave Date: Sat, 9 May 2015 01:06:00 +1200 Subject: [PATCH] No effective change - change newline char --- .../server/autofetch/AutoFetchManager.java | 488 +++++++++--------- .../autofetch/AutoFetchManagerFactory.java | 190 +++---- .../DefaultAutoFetchManagerLogging.java | 134 ++--- 3 files changed, 406 insertions(+), 406 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManager.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManager.java index 9cc6836e5..c5e5a4c3f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManager.java @@ -1,244 +1,244 @@ -package com.avaje.ebeaninternal.server.autofetch; - -import java.util.Iterator; - -import com.avaje.ebean.Query; -import com.avaje.ebean.bean.NodeUsageListener; -import com.avaje.ebean.bean.ObjectGraphNode; -import com.avaje.ebean.config.AutofetchMode; -import com.avaje.ebean.config.ServerConfig; -import com.avaje.ebeaninternal.api.SpiEbeanServer; -import com.avaje.ebeaninternal.api.SpiQuery; - -/** - * Collects and manages the the profile information. - *

- * The profile information is periodically converted into "tuned query details" - - * which is used to automatically tune the queries that use autoFetch. - *

- *

- * The "tuned query details" effectively are part of the query that has the - * select() and join() information (but not the where clause, order by, limits - * etc). These are applied to the query when tuneQuery() is called. - *

- */ -public interface AutoFetchManager extends NodeUsageListener { - - /** - * Set the owning ebean server. - */ - public void setOwner(SpiEbeanServer server, ServerConfig serverConfig); - - /** - * Clear the query execution statistics. - */ - public void clearQueryStatistics(); - - /** - * Clear all the tuned query info. - *

- * Should only need do this for testing and playing around. - *

- */ - public int clearTunedQueryInfo(); - - /** - * Clear all the profiling information. - *

- * This means the profiling information will need to be re-gathered. - *

- *

- * Should only need do this for testing and playing around. - *

- */ - public int clearProfilingInfo(); - - /** - * On shutdown fire garbage collection and collect statistics. Note that - * usually we add a little delay (100 milliseconds) to give the garbage - * collector plenty of time to do its thing and collect the profile - * information. - */ - public void shutdown(); - - /** - * Return the current tuned fetch information for a given queryPoint key. - */ - public TunedQueryInfo getTunedQueryInfo(String queryPointKey); - - /** - * Return the current Statistics for a given queryPoint key. - */ - public Statistics getStatistics(String queryPointKey); - - /** - * Iterate the tuned fetch info. - *

- * This should be a read only iteration. - *

- */ - public Iterator iterateTunedQueryInfo(); - - /** - * Iterate the node usage statistics. - *

- * This should be a read only iteration. - *

- */ - public Iterator iterateStatistics(); - - /** - * Return true if profiling is enabled. - */ - public boolean isProfiling(); - - /** - * Set to true to enable profiling. - *

- * We rely on garbage collection to collect the profiling information. This - * means there is a unknown delay between when a query is executed and when - * we actually collect the usage profile information. - *

- *

- * Due to this garbage collection delay, when turning off profiling while - * the application is running you should consider calling - * collectUsageViaGC() BEFORE setProfiling(false). This hints to - * the JVM to perform garbage collection, and hopefully collects the - * profiling information. - *

- */ - public void setProfiling(boolean enable); - - /** - * Return true if automatic query tuning is enabled. - */ - public boolean isQueryTuning(); - - /** - * Set to true to enable automatic query tuning. - */ - public void setQueryTuning(boolean enable); - - /** - * This controls whether autoFetch is used when it has not been explicitly - * set on a query via {@link Query#setAutoFetch(boolean)}. - */ - public AutofetchMode getMode(); - - /** - * Set the auto fetch mode used when a query has not had - * {@link Query#setAutoFetch(boolean)}. - */ - public void setMode(AutofetchMode Mode); - - /** - * Return the profiling rate (int between 0 and 100). - */ - public double getProfilingRate(); - - /** - * Set the profiling rate (int between 0 and 100). - */ - public void setProfilingRate(double rate); - - /** - * Return the max number of queries profiled (per query point). - *

- * The number of queries profiled is collected per query point. Once a query - * point has profiled this number of queries it does not profile any more. - *

- */ - public int getProfilingBase(); - - /** - * Set a max number of queries to profile per query point. - *

- * This number should provide a level of confidence that no more profiling - * is required for this query point. - *

- */ - public void setProfilingBase(int profilingMax); - - /** - * Return the minimum number of queries profiled before autoFetch will start - * automatically tuning the queries. - *

- * This could be one which means start autoFetch tuning after the first - * profiling information is collected. - *

- */ - public int getProfilingMin(); - - /** - * Set the minimum number of queries profiled per query point before - * autoFetch will automatically tune the queries. - *

- * Increasing this number will mean more profiling is collected before - * autoFetch starts tuning the query. - *

- */ - public void setProfilingMin(int autoFetchMinThreshold); - - /** - * Fire a garbage collection (hint to the JVM). Assuming garbage collection - * fires this will gather the usage profiling information. - */ - public String collectUsageViaGC(long waitMillis); - - /** - * This will take the current profiling information and update the "tuned - * query detail". - *

- * This is done periodically and can also be manually invoked. - *

- *

- * This returns a string summary of the updates that occurred. - *

- */ - public String updateTunedQueryInfo(); - - /** - * Called when a query thinks it should be automatically tuned by autoFetch. - *

- * This internally checks that autoFetch is enabled, there is a "tuned query - * detail" to tune the query with and that the autoFetchMinThreshold has - * been reached. - *

- *

- * This will also determine if the query should be profiled. - *

- */ - public boolean tuneQuery(SpiQuery query); - - /** - * Collect query profiling information. - *

- * This is for the original query as well as any subsequent lazy loading - * queries that are required as the object graph is traversed. - *

- * - * @param node - * the node path in the object graph. - * @param beans - * the number of beans loaded by the query. - * @param micros - * the query executing time in microseconds - */ - public void collectQueryInfo(ObjectGraphNode node, long beans, long micros); - - - /** - * Return the number of queries tuned by AutoFetch. - */ - public int getTotalTunedQueryCount(); - - /** - * Return the size of the TuneQuery map. - */ - public int getTotalTunedQuerySize(); - - /** - * Return the size of the profile map. - */ - public int getTotalProfileSize(); -} +package com.avaje.ebeaninternal.server.autofetch; + +import java.util.Iterator; + +import com.avaje.ebean.Query; +import com.avaje.ebean.bean.NodeUsageListener; +import com.avaje.ebean.bean.ObjectGraphNode; +import com.avaje.ebean.config.AutofetchMode; +import com.avaje.ebean.config.ServerConfig; +import com.avaje.ebeaninternal.api.SpiEbeanServer; +import com.avaje.ebeaninternal.api.SpiQuery; + +/** + * Collects and manages the the profile information. + *

+ * The profile information is periodically converted into "tuned query details" - + * which is used to automatically tune the queries that use autoFetch. + *

+ *

+ * The "tuned query details" effectively are part of the query that has the + * select() and join() information (but not the where clause, order by, limits + * etc). These are applied to the query when tuneQuery() is called. + *

+ */ +public interface AutoFetchManager extends NodeUsageListener { + + /** + * Set the owning ebean server. + */ + public void setOwner(SpiEbeanServer server, ServerConfig serverConfig); + + /** + * Clear the query execution statistics. + */ + public void clearQueryStatistics(); + + /** + * Clear all the tuned query info. + *

+ * Should only need do this for testing and playing around. + *

+ */ + public int clearTunedQueryInfo(); + + /** + * Clear all the profiling information. + *

+ * This means the profiling information will need to be re-gathered. + *

+ *

+ * Should only need do this for testing and playing around. + *

+ */ + public int clearProfilingInfo(); + + /** + * On shutdown fire garbage collection and collect statistics. Note that + * usually we add a little delay (100 milliseconds) to give the garbage + * collector plenty of time to do its thing and collect the profile + * information. + */ + public void shutdown(); + + /** + * Return the current tuned fetch information for a given queryPoint key. + */ + public TunedQueryInfo getTunedQueryInfo(String queryPointKey); + + /** + * Return the current Statistics for a given queryPoint key. + */ + public Statistics getStatistics(String queryPointKey); + + /** + * Iterate the tuned fetch info. + *

+ * This should be a read only iteration. + *

+ */ + public Iterator iterateTunedQueryInfo(); + + /** + * Iterate the node usage statistics. + *

+ * This should be a read only iteration. + *

+ */ + public Iterator iterateStatistics(); + + /** + * Return true if profiling is enabled. + */ + public boolean isProfiling(); + + /** + * Set to true to enable profiling. + *

+ * We rely on garbage collection to collect the profiling information. This + * means there is a unknown delay between when a query is executed and when + * we actually collect the usage profile information. + *

+ *

+ * Due to this garbage collection delay, when turning off profiling while + * the application is running you should consider calling + * collectUsageViaGC() BEFORE setProfiling(false). This hints to + * the JVM to perform garbage collection, and hopefully collects the + * profiling information. + *

+ */ + public void setProfiling(boolean enable); + + /** + * Return true if automatic query tuning is enabled. + */ + public boolean isQueryTuning(); + + /** + * Set to true to enable automatic query tuning. + */ + public void setQueryTuning(boolean enable); + + /** + * This controls whether autoFetch is used when it has not been explicitly + * set on a query via {@link Query#setAutoFetch(boolean)}. + */ + public AutofetchMode getMode(); + + /** + * Set the auto fetch mode used when a query has not had + * {@link Query#setAutoFetch(boolean)}. + */ + public void setMode(AutofetchMode Mode); + + /** + * Return the profiling rate (int between 0 and 100). + */ + public double getProfilingRate(); + + /** + * Set the profiling rate (int between 0 and 100). + */ + public void setProfilingRate(double rate); + + /** + * Return the max number of queries profiled (per query point). + *

+ * The number of queries profiled is collected per query point. Once a query + * point has profiled this number of queries it does not profile any more. + *

+ */ + public int getProfilingBase(); + + /** + * Set a max number of queries to profile per query point. + *

+ * This number should provide a level of confidence that no more profiling + * is required for this query point. + *

+ */ + public void setProfilingBase(int profilingMax); + + /** + * Return the minimum number of queries profiled before autoFetch will start + * automatically tuning the queries. + *

+ * This could be one which means start autoFetch tuning after the first + * profiling information is collected. + *

+ */ + public int getProfilingMin(); + + /** + * Set the minimum number of queries profiled per query point before + * autoFetch will automatically tune the queries. + *

+ * Increasing this number will mean more profiling is collected before + * autoFetch starts tuning the query. + *

+ */ + public void setProfilingMin(int autoFetchMinThreshold); + + /** + * Fire a garbage collection (hint to the JVM). Assuming garbage collection + * fires this will gather the usage profiling information. + */ + public String collectUsageViaGC(long waitMillis); + + /** + * This will take the current profiling information and update the "tuned + * query detail". + *

+ * This is done periodically and can also be manually invoked. + *

+ *

+ * This returns a string summary of the updates that occurred. + *

+ */ + public String updateTunedQueryInfo(); + + /** + * Called when a query thinks it should be automatically tuned by autoFetch. + *

+ * This internally checks that autoFetch is enabled, there is a "tuned query + * detail" to tune the query with and that the autoFetchMinThreshold has + * been reached. + *

+ *

+ * This will also determine if the query should be profiled. + *

+ */ + public boolean tuneQuery(SpiQuery query); + + /** + * Collect query profiling information. + *

+ * This is for the original query as well as any subsequent lazy loading + * queries that are required as the object graph is traversed. + *

+ * + * @param node + * the node path in the object graph. + * @param beans + * the number of beans loaded by the query. + * @param micros + * the query executing time in microseconds + */ + public void collectQueryInfo(ObjectGraphNode node, long beans, long micros); + + + /** + * Return the number of queries tuned by AutoFetch. + */ + public int getTotalTunedQueryCount(); + + /** + * Return the size of the TuneQuery map. + */ + public int getTotalTunedQuerySize(); + + /** + * Return the size of the profile map. + */ + public int getTotalProfileSize(); +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManagerFactory.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManagerFactory.java index 030be2be1..8cb8b2e79 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManagerFactory.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/AutoFetchManagerFactory.java @@ -1,95 +1,95 @@ -package com.avaje.ebeaninternal.server.autofetch; - -import com.avaje.ebean.config.ServerConfig; -import com.avaje.ebeaninternal.api.SpiEbeanServer; -import com.avaje.ebeaninternal.server.resource.ResourceManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.persistence.PersistenceException; -import java.io.File; -import java.io.FileInputStream; -import java.io.ObjectInputStream; - -public class AutoFetchManagerFactory { - - private static final Logger logger = LoggerFactory.getLogger(AutoFetchManagerFactory.class); - - public static AutoFetchManager create(SpiEbeanServer server, ServerConfig serverConfig, ResourceManager resourceManager) { - - AutoFetchManagerFactory me = new AutoFetchManagerFactory(); - return me.createAutoFetchManager(server, serverConfig, resourceManager); - } - - private AutoFetchManager createAutoFetchManager(SpiEbeanServer server, ServerConfig serverConfig, ResourceManager resourceManager){ - - AutoFetchManager manager = createAutoFetchManager(server.getName(), resourceManager); - manager.setOwner(server, serverConfig); - - return manager; - } - - private AutoFetchManager createAutoFetchManager(String serverName, ResourceManager resourceManager) { - - File autoFetchFile = getAutoFetchFile(serverName, resourceManager); - - AutoFetchManager autoFetchManager = null; - - boolean readFile = !"false".equalsIgnoreCase(System.getProperty("autofetch.readfromfile")); - if (readFile) { - autoFetchManager = deserializeAutoFetch(autoFetchFile); - } - - if (autoFetchManager == null) { - // not deserialized from file so create as empty - // It will be populated automatically by querying the - // database meta data - autoFetchManager = new DefaultAutoFetchManager(autoFetchFile.getAbsolutePath()); - } - - return autoFetchManager; - } - - private AutoFetchManager deserializeAutoFetch(File autoFetchFile) { - try { - - if (!autoFetchFile.exists()) { - return null; - } - FileInputStream fi = new FileInputStream(autoFetchFile); - ObjectInputStream ois = new ObjectInputStream(fi); - AutoFetchManager profListener = (AutoFetchManager) ois.readObject(); - ois.close(); - - logger.info("AutoFetch deserialized from file ["+autoFetchFile.getAbsolutePath()+"]"); - - return profListener; - - } catch (Exception ex) { - logger.error("Error loading autofetch file "+autoFetchFile.getAbsolutePath(), ex); - return null; - } - } - - /** - * Return the file name of the autoFetch meta data. - */ - private File getAutoFetchFile(String serverName, ResourceManager resourceManager) { - - String fileName = ".ebean."+serverName+".autofetch"; - - File dir = resourceManager.getAutofetchDirectory(); - - if (!dir.exists()) { - // automatically create the directory if it does not exist. - // this is probably a fairly reasonable thing to do - if (!dir.mkdirs()) { - String m = "Unable to create directory [" + dir + "] for autofetch file ["+ fileName + "]"; - throw new PersistenceException(m); - } - } - - return new File(dir, fileName); - } - -} +package com.avaje.ebeaninternal.server.autofetch; + +import com.avaje.ebean.config.ServerConfig; +import com.avaje.ebeaninternal.api.SpiEbeanServer; +import com.avaje.ebeaninternal.server.resource.ResourceManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.persistence.PersistenceException; +import java.io.File; +import java.io.FileInputStream; +import java.io.ObjectInputStream; + +public class AutoFetchManagerFactory { + + private static final Logger logger = LoggerFactory.getLogger(AutoFetchManagerFactory.class); + + public static AutoFetchManager create(SpiEbeanServer server, ServerConfig serverConfig, ResourceManager resourceManager) { + + AutoFetchManagerFactory me = new AutoFetchManagerFactory(); + return me.createAutoFetchManager(server, serverConfig, resourceManager); + } + + private AutoFetchManager createAutoFetchManager(SpiEbeanServer server, ServerConfig serverConfig, ResourceManager resourceManager){ + + AutoFetchManager manager = createAutoFetchManager(server.getName(), resourceManager); + manager.setOwner(server, serverConfig); + + return manager; + } + + private AutoFetchManager createAutoFetchManager(String serverName, ResourceManager resourceManager) { + + File autoFetchFile = getAutoFetchFile(serverName, resourceManager); + + AutoFetchManager autoFetchManager = null; + + boolean readFile = !"false".equalsIgnoreCase(System.getProperty("autofetch.readfromfile")); + if (readFile) { + autoFetchManager = deserializeAutoFetch(autoFetchFile); + } + + if (autoFetchManager == null) { + // not deserialized from file so create as empty + // It will be populated automatically by querying the + // database meta data + autoFetchManager = new DefaultAutoFetchManager(autoFetchFile.getAbsolutePath()); + } + + return autoFetchManager; + } + + private AutoFetchManager deserializeAutoFetch(File autoFetchFile) { + try { + + if (!autoFetchFile.exists()) { + return null; + } + FileInputStream fi = new FileInputStream(autoFetchFile); + ObjectInputStream ois = new ObjectInputStream(fi); + AutoFetchManager profListener = (AutoFetchManager) ois.readObject(); + ois.close(); + + logger.info("AutoFetch deserialized from file ["+autoFetchFile.getAbsolutePath()+"]"); + + return profListener; + + } catch (Exception ex) { + logger.error("Error loading autofetch file "+autoFetchFile.getAbsolutePath(), ex); + return null; + } + } + + /** + * Return the file name of the autoFetch meta data. + */ + private File getAutoFetchFile(String serverName, ResourceManager resourceManager) { + + String fileName = ".ebean."+serverName+".autofetch"; + + File dir = resourceManager.getAutofetchDirectory(); + + if (!dir.exists()) { + // automatically create the directory if it does not exist. + // this is probably a fairly reasonable thing to do + if (!dir.mkdirs()) { + String m = "Unable to create directory [" + dir + "] for autofetch file ["+ fileName + "]"; + throw new PersistenceException(m); + } + } + + return new File(dir, fileName); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/DefaultAutoFetchManagerLogging.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/DefaultAutoFetchManagerLogging.java index d95e12fb2..6823b33ef 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/DefaultAutoFetchManagerLogging.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/DefaultAutoFetchManagerLogging.java @@ -1,67 +1,67 @@ -package com.avaje.ebeaninternal.server.autofetch; - -import com.avaje.ebean.config.ServerConfig; -import com.avaje.ebeaninternal.api.SpiEbeanServer; -import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.TimeUnit; - -/** - * Handles the logging aspects for the DefaultAutoFetchListener. - *

- * Note that java util logging loggers generally should not be serialised and - * that is one of the main reasons for pulling out the logging to this class. - *

- */ -public class DefaultAutoFetchManagerLogging { - - private static final Logger logger = LoggerFactory.getLogger(DefaultAutoFetchManagerLogging.class); - - private final DefaultAutoFetchManager manager; - - private final int updateFreqInSecs; - - public DefaultAutoFetchManagerLogging(ServerConfig serverConfig, DefaultAutoFetchManager profileListener) { - - this.manager = profileListener; - this.updateFreqInSecs = serverConfig.getAutofetchConfig().getProfileUpdateFrequency(); - } - - public void init(SpiEbeanServer ebeanServer) { - ebeanServer.getBackgroundExecutor().executePeriodically(new UpdateProfile(), updateFreqInSecs, TimeUnit.SECONDS); - } - - private final class UpdateProfile implements Runnable { - public void run() { - manager.updateTunedQueryInfo(); - } - } - - public void logInfo(String msg, Throwable e) { - logger.info(msg, e); - } - - public void logError(String msg, Throwable e) { - logger.error(msg, e); - } - - public void logSummary(String summaryInfo) { - - String msg = "\"Summary\",\""+summaryInfo+"\",,,,"; - logger.debug(msg); - } - - public void logChanged(TunedQueryInfo tunedFetch, OrmQueryDetail newQueryDetail) { - - String msg = tunedFetch.getLogOutput(newQueryDetail); - logger.debug(msg); - } - - public void logNew(TunedQueryInfo tunedFetch) { - - String msg = tunedFetch.getLogOutput(null); - logger.debug(msg); - } -} +package com.avaje.ebeaninternal.server.autofetch; + +import com.avaje.ebean.config.ServerConfig; +import com.avaje.ebeaninternal.api.SpiEbeanServer; +import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.TimeUnit; + +/** + * Handles the logging aspects for the DefaultAutoFetchListener. + *

+ * Note that java util logging loggers generally should not be serialised and + * that is one of the main reasons for pulling out the logging to this class. + *

+ */ +public class DefaultAutoFetchManagerLogging { + + private static final Logger logger = LoggerFactory.getLogger(DefaultAutoFetchManagerLogging.class); + + private final DefaultAutoFetchManager manager; + + private final int updateFreqInSecs; + + public DefaultAutoFetchManagerLogging(ServerConfig serverConfig, DefaultAutoFetchManager profileListener) { + + this.manager = profileListener; + this.updateFreqInSecs = serverConfig.getAutofetchConfig().getProfileUpdateFrequency(); + } + + public void init(SpiEbeanServer ebeanServer) { + ebeanServer.getBackgroundExecutor().executePeriodically(new UpdateProfile(), updateFreqInSecs, TimeUnit.SECONDS); + } + + private final class UpdateProfile implements Runnable { + public void run() { + manager.updateTunedQueryInfo(); + } + } + + public void logInfo(String msg, Throwable e) { + logger.info(msg, e); + } + + public void logError(String msg, Throwable e) { + logger.error(msg, e); + } + + public void logSummary(String summaryInfo) { + + String msg = "\"Summary\",\""+summaryInfo+"\",,,,"; + logger.debug(msg); + } + + public void logChanged(TunedQueryInfo tunedFetch, OrmQueryDetail newQueryDetail) { + + String msg = tunedFetch.getLogOutput(newQueryDetail); + logger.debug(msg); + } + + public void logNew(TunedQueryInfo tunedFetch) { + + String msg = tunedFetch.getLogOutput(null); + logger.debug(msg); + } +}