From 9632279c2f6587207a99548e135198d5e838ea5a Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 10 Sep 2015 22:36:12 +1200 Subject: [PATCH] AutoTune update - clean up for query ignore --- .../autofetch/service/BaseQueryTuner.java | 52 +++++++++++++------ .../autofetch/service/ProfileManager.java | 24 ++++++--- .../com/avaje/tests/query/TestRowCount.java | 3 ++ 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/BaseQueryTuner.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/BaseQueryTuner.java index f3614d4f8..2e349dc29 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/BaseQueryTuner.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/BaseQueryTuner.java @@ -20,7 +20,7 @@ public class BaseQueryTuner { private final boolean queryTuning; - private boolean profiling; + private final boolean profiling; private final AutoTuneMode mode; @@ -29,12 +29,14 @@ public class BaseQueryTuner { */ private final Map tunedQueryInfoMap = new ConcurrentHashMap(); - private final SpiEbeanServer server; private final ProfilingListener profilingListener; - boolean fullProfiling = true; + /** + * Flag set true when there is no profiling or query tuning. + */ + private final boolean skipAll; public BaseQueryTuner(AutoTuneConfig config, SpiEbeanServer server, ProfilingListener profilingListener) { this.server = server; @@ -42,6 +44,7 @@ public class BaseQueryTuner { this.mode = config.getMode(); this.queryTuning = config.isQueryTuning(); this.profiling = config.isProfiling(); + this.skipAll = !queryTuning && !profiling; } /** @@ -65,15 +68,13 @@ public class BaseQueryTuner { */ public boolean tuneQuery(SpiQuery query) { - if (!queryTuning && !profiling) { + if (skipAll || !tunableQuery(query)) { return false; } - if (!useAutoTune(query)) { - // not tuning this query but maybe profiling - if (fullProfiling) { - CallStack stack = server.createCallStack(); - profiling(query, stack); + if (!useTuning(query)) { + if (profiling) { + profiling(query, server.createCallStack()); } return false; } @@ -104,6 +105,31 @@ public class BaseQueryTuner { return false; } + /** + * Return false for row count, find ids, subQuery, delete and Versions queries. + *

+ * These queries are not applicable for autoTune in that they don't have a select/fetch (fetch group). + *

+ *

+ * We also exclude queries that are explicitly set to load the L2 bean cache as we want full beans + * in that case. + *

+ */ + private boolean tunableQuery(SpiQuery query) { + SpiQuery.Type type = query.getType(); + switch (type) { + case ROWCOUNT: + case ID_LIST: + case DELETE: + case SUBQUERY: + return false; + default: + // not using autoTune when explicitly loading the l2 bean cache + // or when using Versions query + return !query.isLoadBeanCache() && SpiQuery.TemporalMode.VERSIONS != query.getTemporalMode(); + } + } + private void profiling(SpiQuery query, CallStack stack) { // create a query point to identify the query @@ -117,13 +143,7 @@ public class BaseQueryTuner { /** * Return true if we should try to tune this query. */ - private boolean useAutoTune(SpiQuery query) { - - if (query.isLoadBeanCache()) { - // when loading the cache don't tune the query - // as we want full objects loaded into the cache - return false; - } + private boolean useTuning(SpiQuery query) { Boolean autoFetch = query.isAutofetch(); if (autoFetch != null) { diff --git a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/ProfileManager.java b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/ProfileManager.java index 4ef3b464a..b079ed6b8 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/ProfileManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/autofetch/service/ProfileManager.java @@ -9,19 +9,15 @@ import com.avaje.ebeaninternal.api.SpiQuery; import com.avaje.ebeaninternal.server.autofetch.AutoTuneCollection; import com.avaje.ebeaninternal.server.autofetch.ProfilingListener; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * + * Manages the collection of object graph usage profiling. */ public class ProfileManager implements ProfilingListener { - private static final Logger logger = LoggerFactory.getLogger(ProfileManager.class); - private final boolean queryTuningAddVersion; /** @@ -53,15 +49,27 @@ public class ProfileManager implements ProfilingListener { ProfileOrigin profileOrigin = profileMap.get(origin.getOriginQueryPoint().getKey()); if (profileOrigin == null) { - profileOrigin = new ProfileOrigin(origin.getOriginQueryPoint(), queryTuningAddVersion, profilingBase, profilingRate); - profileOrigin.setOriginalQuery(query.getDetail().toString()); - profileMap.put(origin.getOriginQueryPoint().getKey(), profileOrigin); + profileMap.put(origin.getOriginQueryPoint().getKey(), createProfileOrigin(origin, query)); return true; } else { return profileOrigin.isProfile(); } } + /** + * Create the profile origin noting the query detail currently being used. + *

+ * For new profiling entries it is useful to compare the profiling against the current + * query detail that is specified in the code (as the query might already be manually optimised). + *

+ */ + private ProfileOrigin createProfileOrigin(ObjectGraphNode origin, SpiQuery query) { + ProfileOrigin profileOrigin = new ProfileOrigin(origin.getOriginQueryPoint(), queryTuningAddVersion, profilingBase, profilingRate); + // set the current query detail (fetch group) so that we can compare against profiling for new entries + profileOrigin.setOriginalQuery(query.getDetail().toString()); + return profileOrigin; + } + /** * Gather query execution statistics. This could either be the originating * query in which case the parentNode will be null, or a lazy loading query diff --git a/src/test/java/com/avaje/tests/query/TestRowCount.java b/src/test/java/com/avaje/tests/query/TestRowCount.java index 3f6ddb969..790d5cb56 100644 --- a/src/test/java/com/avaje/tests/query/TestRowCount.java +++ b/src/test/java/com/avaje/tests/query/TestRowCount.java @@ -29,6 +29,9 @@ public class TestRowCount extends BaseTestCase { List list = query.findList(); System.out.println(list); + for (Order order : list) { + order.getStatus(); + } Assert.assertEquals("same rc to ids.size() ", rc, ids.size()); Assert.assertEquals("same rc to list.size()", rc, list.size());