From 373598ab26c21812d56c7d17ec4d8b38ce3ab329 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 10 Sep 2015 10:02:17 +1200 Subject: [PATCH] AutoTune update - config properties for queryTuningFile and profilingFile + logging adjustment --- .../avaje/ebean/config/AutoTuneConfig.java | 35 ++++++++++ .../service/DefaultAutoTuneService.java | 66 ++++++++++++++----- src/test/resources/ebean.properties | 3 + 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/avaje/ebean/config/AutoTuneConfig.java b/src/main/java/com/avaje/ebean/config/AutoTuneConfig.java index e95fe2395..6eab25e8c 100644 --- a/src/main/java/com/avaje/ebean/config/AutoTuneConfig.java +++ b/src/main/java/com/avaje/ebean/config/AutoTuneConfig.java @@ -7,12 +7,16 @@ public class AutoTuneConfig { private AutoTuneMode mode = AutoTuneMode.DEFAULT_ON; + private String queryTuningFile = "ebean-autotune.xml"; + private boolean queryTuning; private boolean queryTuningAddVersion; private boolean profiling; + private String profilingFile = "ebean-profiling"; + private int profilingBase = 5; private double profilingRate = 0.01; @@ -24,6 +28,34 @@ public class AutoTuneConfig { public AutoTuneConfig() { } + /** + * Return the name of the file that holds the query tuning information. + */ + public String getQueryTuningFile() { + return queryTuningFile; + } + + /** + * Set the name of the file that holds the query tuning information. + */ + public void setQueryTuningFile(String queryTuningFile) { + this.queryTuningFile = queryTuningFile; + } + + /** + * Return the name of the file that profiling information is written to. + */ + public String getProfilingFile() { + return profilingFile; + } + + /** + * Set the name of the file that profiling information is written to. + */ + public void setProfilingFile(String profilingFile) { + this.profilingFile = profilingFile; + } + /** * Return the mode used when autoTune has not been explicit defined on a * query. @@ -172,6 +204,8 @@ public class AutoTuneConfig { queryTuning = p.getBoolean("autoTune.queryTuning", queryTuning); queryTuningAddVersion = p.getBoolean("autoTune.queryTuningAddVersion", queryTuningAddVersion); + queryTuningFile = p.get("autoTune.queryTuningFile", queryTuningFile); + skipCollectionOnShutdown = p.getBoolean("autoTune.skipCollectionOnShutdown", skipCollectionOnShutdown); mode = p.getEnum(AutoTuneMode.class, "autoTune.mode", mode); @@ -179,5 +213,6 @@ public class AutoTuneConfig { profiling = p.getBoolean("autoTune.profiling", profiling); profilingBase = p.getInt("autoTune.profilingBase", profilingBase); profilingRate = p.getDouble("autoTune.profilingRate", profilingRate); + profilingFile = p.get("autoTune.profilingFile", profilingFile); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/DefaultAutoTuneService.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/DefaultAutoTuneService.java index d2d7242d5..12600ad6d 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/DefaultAutoTuneService.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/DefaultAutoTuneService.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * Implementation of the AutoTuneService which is comprised of profiling and query tuning. @@ -41,16 +42,25 @@ public class DefaultAutoTuneService implements AutoTuneService { private final boolean profiling; + private final boolean queryTuning; + + private final String tuningFile; + + private final String profilingFile; + private final String serverName; public DefaultAutoTuneService(SpiEbeanServer server, ServerConfig serverConfig) { AutoTuneConfig config = serverConfig.getAutoTuneConfig(); + this.queryTuning = config.isQueryTuning(); this.profiling = config.isProfiling(); + this.tuningFile = config.getQueryTuningFile(); + this.profilingFile = config.getProfilingFile(); + this.serverName = server.getName(); this.profileManager = new ProfileManager(config, server); this.queryTuner = new BaseQueryTuner(config, server, profileManager); - this.serverName = server.getName(); this.skipCollectionOnShutdown = config.isSkipCollectionOnShutdown(); this.defaultGarbageCollectionWait = (long) config.getGarbageCollectionWait(); } @@ -61,11 +71,19 @@ public class DefaultAutoTuneService implements AutoTuneService { @Override public void startup() { - File file = new File("ebean-autotune.xml"); - AutoTuneXmlReader reader = new AutoTuneXmlReader(); - Autotune profiling = reader.read(file); - for (Origin origin : profiling.getOrigin()) { - queryTuner.load(origin.getKey(), createTunedQueryInfo(origin)); + if (queryTuning) { + File file = new File(tuningFile); + if (!file.exists()) { + logger.warn("AutoTune file {} not found - no automatic tuning will be applied", file.getAbsolutePath()); + + } else { + AutoTuneXmlReader reader = new AutoTuneXmlReader(); + Autotune profiling = reader.read(file); + logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size()); + for (Origin origin : profiling.getOrigin()) { + queryTuner.load(origin.getKey(), createTunedQueryInfo(origin)); + } + } } } @@ -83,20 +101,32 @@ public class DefaultAutoTuneService implements AutoTuneService { List entries = autoTuneCollection.getEntries(); + // count "new" and "diff" profiling entries + AtomicInteger newCounter = new AtomicInteger(); + AtomicInteger diffCounter = new AtomicInteger(); + for (AutoTuneCollection.Entry entry : entries) { - saveProfilingEntry(document, entry); + saveProfilingEntry(document, entry, newCounter, diffCounter); } - sortDocument(document); + int totalNew = newCounter.get(); + int totalDiff = diffCounter.get(); + if (totalNew == 0 && totalDiff == 0) { + logger.debug("No new or diff entries for profiling server:{}", serverName); - SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss"); - String now = df.format(new Date()); + } else { + sortDocument(document); - File file = new File("ebean-profiling"+"-"+serverName+"-"+now+".xml"); - AutoTuneXmlWriter writer = new AutoTuneXmlWriter(); - writer.write(document, file); + SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss"); + String now = df.format(new Date()); - logger.debug("profiling entries:{}", entries.size()); + // write the file with serverName and now suffix as we can output the profiling many times + File file = new File(profilingFile + "-" + serverName + "-" + now + ".xml"); + AutoTuneXmlWriter writer = new AutoTuneXmlWriter(); + writer.write(document, file); + + logger.debug("writing new:{} diff:{} profiling entries for server:{}", totalNew, totalDiff, serverName); + } } /** @@ -114,6 +144,9 @@ public class DefaultAutoTuneService implements AutoTuneService { } } + /** + * Comparator sort by bean type then key. + */ class OriginNameKeySort implements Comparator { @Override @@ -126,7 +159,7 @@ public class DefaultAutoTuneService implements AutoTuneService { } } - private void saveProfilingEntry(Autotune document, AutoTuneCollection.Entry entry) { + private void saveProfilingEntry(Autotune document, AutoTuneCollection.Entry entry, AtomicInteger newCount, AtomicInteger diffCount) { ObjectGraphOrigin point = entry.getOrigin(); OrmQueryDetail profileDetail = entry.getDetail(); @@ -135,6 +168,7 @@ public class DefaultAutoTuneService implements AutoTuneService { OrmQueryDetail tuneDetail = queryTuner.get(point.getKey()); if (tuneDetail == null) { // New entry + newCount.incrementAndGet(); ProfileNew profileNew = document.getProfileNew(); if (profileNew == null) { profileNew = new ProfileNew(); @@ -146,6 +180,7 @@ public class DefaultAutoTuneService implements AutoTuneService { } else if (!tuneDetail.isAutoTuneEqual(profileDetail)) { // Diff entry + diffCount.incrementAndGet(); Origin origin = createOrigin(entry, point); origin.setOriginal(tuneDetail.toString()); ProfileDiff diff = document.getProfileDiff(); @@ -219,7 +254,6 @@ public class DefaultAutoTuneService implements AutoTuneService { } } - /** * Auto tune the query and enable profiling. */ diff --git a/src/test/resources/ebean.properties b/src/test/resources/ebean.properties index 12018186d..3205cad75 100644 --- a/src/test/resources/ebean.properties +++ b/src/test/resources/ebean.properties @@ -12,7 +12,10 @@ ebean.encryptKeyManager=com.avaje.tests.basic.encrypt.BasicEncyptKeyManager ebean.autotune.querytuning=true +#ebean.autotune.queryTuningFile=${HOME}/config/tuning.xml + ebean.autotune.profiling=true +#ebean.autotune.profilingFile=/tmp/eb-prof ebean.ddl.generate=true