#815 - ENH: When loading ebean-autotune.xml also look in resources (if not found as file)

This commit is contained in:
Robin Bygrave
2016-08-24 22:19:46 +12:00
parent 2b7c204072
commit 157dfcfeb6
5 changed files with 87 additions and 13 deletions
@@ -19,8 +19,7 @@ public class AutoTuneXmlReader {
/**
* Read and return a Profiling from an xml file.
*/
public Autotune read(File file) {
public static Autotune read(File file) {
try {
return readFile(file);
} catch (IOException e) {
@@ -28,7 +27,7 @@ public class AutoTuneXmlReader {
}
}
protected Autotune readFile(File file) throws IOException {
protected static Autotune readFile(File file) throws IOException {
if (!file.exists()) {
return new Autotune();
}
@@ -43,8 +42,7 @@ public class AutoTuneXmlReader {
/**
* Read and return a Profiling from an xml document.
*/
public Autotune read(InputStream is) {
public static Autotune read(InputStream is) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Autotune.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
@@ -12,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
/**
@@ -101,19 +102,26 @@ public class DefaultAutoTuneService implements AutoTuneService {
*/
private void loadTuningFile() {
File file = new File(tuningFile);
if (!file.exists()) {
logger.warn("AutoTune file {} not found - no initial automatic query tuning", file.getAbsolutePath());
if (file.exists()) {
loadAutoTuneProfiling(AutoTuneXmlReader.read(file));
} 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.put(origin);
// look for autotune as a resource
InputStream stream = getClass().getResourceAsStream("/" + tuningFile);
if (stream != null) {
loadAutoTuneProfiling(AutoTuneXmlReader.read(stream));
} else {
logger.warn("AutoTune file {} not found - no initial automatic query tuning", tuningFile);
}
}
}
private void loadAutoTuneProfiling(Autotune profiling) {
logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size());
for (Origin origin : profiling.getOrigin()) {
queryTuner.put(origin);
}
}
/**
* Collect profiling, check for new/diff to existing tuning and apply changes.
*/
@@ -0,0 +1,31 @@
package com.avaje.ebeaninternal.server.autotune.service;
import com.avaje.ebeaninternal.server.autotune.model.Autotune;
import org.junit.Test;
import java.io.File;
import java.io.InputStream;
import static org.assertj.core.api.Assertions.assertThat;
public class AutoTuneXmlReaderTest {
@Test
public void read_file() throws Exception {
File testFile = new File("src/test/resources/autotune/test-autotune.xml");
Autotune tuneInfo = AutoTuneXmlReader.read(testFile);
assertThat(tuneInfo.getOrigin()).isNotEmpty();
}
@Test
public void read_inputStream() throws Exception {
InputStream is = getClass().getResourceAsStream("/autotune/test-autotune.xml");
Autotune tuneInfo = AutoTuneXmlReader.read(is);
assertThat(tuneInfo.getOrigin()).isNotEmpty();
}
}
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<autotune xmlns="http://ebean-orm.github.io/xml/ns/autotune">
<origin key="DWeHD4.B0dP9Z.DKG8lq" beanType="com.avaje.tests.model.basic.Order" detail="select (status,shipDate) " original="select (status, orderDate, shipDate) ">
<callStack>com.avaje.tests.query.autotune.TestAutoTuneProfiling.findById(TestAutoTuneProfiling.java:62)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.useOrderDate(TestAutoTuneProfiling.java:66)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:52)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
</callStack>
</origin>
<origin key="DWeHD4.BZfmOY.Yd8Hf" beanType="com.avaje.tests.model.basic.Order" detail=" fetch customer (id) fetch details (orderQty,shipQty,unitPrice) fetch details.product (id,name) " original="select (id) fetch details ">
<callStack>com.avaje.tests.query.autotune.TestAutoTuneProfiling.useLotUntuned(TestAutoTuneProfiling.java:95)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:55)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
</callStack>
</origin>
<origin key="DWeHD4.l6b4M.BYWlSr" beanType="com.avaje.tests.model.basic.Order" detail=" fetch customer (name) " original="">
<callStack>com.avaje.tests.model.basic.Order._ebean_get_customer(Order.java:6)
com.avaje.tests.model.basic.Order.getCustomer(Order.java:228)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.useOrderDateCustomerName(TestAutoTuneProfiling.java:74)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:53)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:25)
</callStack>
</origin>
<origin key="DVf2Cn.StLsZ.BCru36" beanType="com.avaje.tests.model.basic.OrderDetail" detail=" fetch product (name) " original="">
<callStack>com.avaje.tests.model.basic.OrderDetail._ebean_get_product(OrderDetail.java:6)
com.avaje.tests.model.basic.OrderDetail.getProduct(OrderDetail.java:150)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.useLotUntuned(TestAutoTuneProfiling.java:96)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.execute(TestAutoTuneProfiling.java:55)
com.avaje.tests.query.autotune.TestAutoTuneProfiling.test(TestAutoTuneProfiling.java:32)
</callStack>
</origin>
</autotune>
+2
View File
@@ -73,6 +73,8 @@
</root>
<!--<logger name="com.avaje.ebeaninternal" level="INFO"/>-->
<logger name="com.avaje.ebean" level="INFO"/>
<logger name="org.avaje.ebean" level="INFO"/>