mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
NodeUsageCollector migrate from using finalize() to Cleaner
This commit is contained in:
1 parent
4d21fbfcf8
commit
e1b9bf62a3
7 files changed
+143
-104
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
package io.ebean.bean;
|
package io.ebean.bean;
|
||||||
|
|
||||||
|
import java.lang.ref.Cleaner;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -8,120 +9,116 @@ import java.util.Set;
|
|||||||
* Collects profile information for a bean (or reference/proxy bean) at a given node.
|
* Collects profile information for a bean (or reference/proxy bean) at a given node.
|
||||||
* <p>
|
* <p>
|
||||||
* The node identifies the location of the bean in the object graph.
|
* The node identifies the location of the bean in the object graph.
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* It has to use a weak reference so as to ensure that it does not stop the
|
|
||||||
* associated bean from being garbage collected.
|
|
||||||
* </p>
|
|
||||||
*/
|
*/
|
||||||
public final class NodeUsageCollector {
|
public final class NodeUsageCollector {
|
||||||
|
|
||||||
/**
|
private final static Cleaner cleaner = Cleaner.create();
|
||||||
* The point in the object graph for a specific query and call stack point.
|
|
||||||
*/
|
|
||||||
private final ObjectGraphNode node;
|
|
||||||
|
|
||||||
/**
|
public static final class State implements Runnable {
|
||||||
* Weak to allow garbage collection.
|
private final WeakReference<NodeUsageListener> managerRef;
|
||||||
*/
|
/**
|
||||||
private final WeakReference<NodeUsageListener> managerRef;
|
* The properties used at this profile point.
|
||||||
|
*/
|
||||||
|
private final Set<String> used = new LinkedHashSet<>();
|
||||||
|
/**
|
||||||
|
* The point in the object graph for a specific query and call stack point.
|
||||||
|
*/
|
||||||
|
private final ObjectGraphNode node;
|
||||||
|
/**
|
||||||
|
* set to true if the bean is modified (setter called)
|
||||||
|
*/
|
||||||
|
private boolean modified;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The properties used at this profile point.
|
* The property that cause a reference to lazy load.
|
||||||
*/
|
*/
|
||||||
private final Set<String> used = new LinkedHashSet<>();
|
private String loadProperty;
|
||||||
|
|
||||||
/**
|
private State(ObjectGraphNode node, WeakReference<NodeUsageListener> managerRef) {
|
||||||
* set to true if the bean is modified (setter called)
|
this.node = node;
|
||||||
*/
|
this.managerRef = managerRef;
|
||||||
private boolean modified;
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* The property that cause a reference to lazy load.
|
public String toString() {
|
||||||
*/
|
return node + " read:" + used + " modified:" + modified;
|
||||||
private String loadProperty;
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
NodeUsageListener manager = managerRef.get();
|
||||||
|
if (manager != null) {
|
||||||
|
manager.collectNodeUsage(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if no properties where used.
|
||||||
|
*/
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return used.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the associated node which identifies the location in the object
|
||||||
|
* graph of the bean/reference.
|
||||||
|
*/
|
||||||
|
public ObjectGraphNode node() {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the set of used properties.
|
||||||
|
*/
|
||||||
|
public Set<String> used() {
|
||||||
|
return used;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the bean was modified by a setter.
|
||||||
|
*/
|
||||||
|
public boolean isModified() {
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final State state;
|
||||||
|
|
||||||
public NodeUsageCollector(ObjectGraphNode node, WeakReference<NodeUsageListener> managerRef) {
|
public NodeUsageCollector(ObjectGraphNode node, WeakReference<NodeUsageListener> managerRef) {
|
||||||
this.node = node;
|
this.state = new State(node, managerRef);
|
||||||
// weak to allow garbage collection.
|
cleaner.register(this, state);
|
||||||
this.managerRef = managerRef;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the underlying state.
|
||||||
|
*/
|
||||||
|
public State state() {
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The bean has been modified by a setter method.
|
* The bean has been modified by a setter method.
|
||||||
*/
|
*/
|
||||||
public void setModified() {
|
public void setModified() {
|
||||||
modified = true;
|
state.modified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the name of a property that has been used.
|
* Add the name of a property that has been used.
|
||||||
*/
|
*/
|
||||||
public void addUsed(String property) {
|
public void addUsed(String property) {
|
||||||
used.add(property);
|
state.used.add(property);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The property that invoked a lazy load.
|
* The property that invoked a lazy load.
|
||||||
*/
|
*/
|
||||||
public void setLoadProperty(String loadProperty) {
|
public void setLoadProperty(String loadProperty) {
|
||||||
this.loadProperty = loadProperty;
|
state.loadProperty = loadProperty;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Publish the usage info to the manager.
|
|
||||||
*/
|
|
||||||
private void publishUsageInfo() {
|
|
||||||
NodeUsageListener manager = managerRef.get();
|
|
||||||
if (manager != null) {
|
|
||||||
manager.collectNodeUsage(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* publish the collected usage information when garbage collection occurs.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void finalize() throws Throwable {
|
|
||||||
publishUsageInfo();
|
|
||||||
super.finalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the associated node which identifies the location in the object
|
|
||||||
* graph of the bean/reference.
|
|
||||||
*/
|
|
||||||
public ObjectGraphNode getNode() {
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true if no properties where used.
|
|
||||||
*/
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return used.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the set of used properties.
|
|
||||||
*/
|
|
||||||
public Set<String> getUsed() {
|
|
||||||
return used;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true if the bean was modified by a setter.
|
|
||||||
*/
|
|
||||||
public boolean isModified() {
|
|
||||||
return modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLoadProperty() {
|
|
||||||
return loadProperty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return node + " read:" + used + " modified:" + modified;
|
return state.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ public interface NodeUsageListener {
|
|||||||
* <p>
|
* <p>
|
||||||
* This is the properties that are used for a given bean in the object graph.
|
* This is the properties that are used for a given bean in the object graph.
|
||||||
* This information is used by autoTune to tune queries.
|
* This information is used by autoTune to tune queries.
|
||||||
* </p>
|
|
||||||
*/
|
*/
|
||||||
void collectNodeUsage(NodeUsageCollector collector);
|
void collectNodeUsage(NodeUsageCollector.State state);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package io.ebean.bean;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class NodeUsageCollectorTest {
|
||||||
|
|
||||||
|
private final Listener listener = new Listener();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run this manually as we make explicit GC call here.
|
||||||
|
*/
|
||||||
|
@Disabled
|
||||||
|
@Test
|
||||||
|
void test() throws InterruptedException {
|
||||||
|
WeakReference<NodeUsageListener> profilingListenerRef = new WeakReference<>(listener);
|
||||||
|
|
||||||
|
ObjectGraphNode node = new ObjectGraphNode((ObjectGraphOrigin)null, "foo");
|
||||||
|
NodeUsageCollector c = new NodeUsageCollector(node, profilingListenerRef);
|
||||||
|
c.addUsed("a");
|
||||||
|
c.addUsed("b");
|
||||||
|
c = null;
|
||||||
|
|
||||||
|
System.gc();
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
assertThat(listener.collectCount).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Listener implements NodeUsageListener {
|
||||||
|
|
||||||
|
int collectCount;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void collectNodeUsage(NodeUsageCollector.State collector) {
|
||||||
|
collectCount++;
|
||||||
|
System.out.println("collectNodeUsage " + collector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -91,8 +91,8 @@ public class ProfileManager implements ProfilingListener {
|
|||||||
* is called on the bean.
|
* is called on the bean.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void collectNodeUsage(NodeUsageCollector usageCollector) {
|
public void collectNodeUsage(NodeUsageCollector.State usageCollector) {
|
||||||
ProfileOrigin profileOrigin = getProfileOrigin(usageCollector.getNode().getOriginQueryPoint());
|
ProfileOrigin profileOrigin = getProfileOrigin(usageCollector.node().getOriginQueryPoint());
|
||||||
profileOrigin.collectUsageInfo(usageCollector);
|
profileOrigin.collectUsageInfo(usageCollector);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -151,9 +151,9 @@ public class ProfileOrigin {
|
|||||||
/**
|
/**
|
||||||
* Collect the usage information for from a instance for this node.
|
* Collect the usage information for from a instance for this node.
|
||||||
*/
|
*/
|
||||||
public void collectUsageInfo(NodeUsageCollector profile) {
|
public void collectUsageInfo(NodeUsageCollector.State profile) {
|
||||||
if (!profile.isEmpty()) {
|
if (!profile.isEmpty()) {
|
||||||
getNodeStats(profile.getNode().getPath()).collectUsageInfo(profile);
|
getNodeStats(profile.node().getPath()).collectUsageInfo(profile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -103,11 +103,10 @@ public class ProfileOriginNodeUsage {
|
|||||||
/**
|
/**
|
||||||
* Collect usage from a node.
|
* Collect usage from a node.
|
||||||
*/
|
*/
|
||||||
protected void collectUsageInfo(NodeUsageCollector profile) {
|
protected void collectUsageInfo(NodeUsageCollector.State profile) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
Set<String> used = profile.getUsed();
|
Set<String> used = profile.used();
|
||||||
|
|
||||||
profileCount++;
|
profileCount++;
|
||||||
if (!used.isEmpty()) {
|
if (!used.isEmpty()) {
|
||||||
profileUsedCount++;
|
profileUsedCount++;
|
||||||
|
|||||||
+11
-11
@@ -23,7 +23,7 @@ public class ProfileOriginTest extends BaseTestCase {
|
|||||||
c.addUsed("name");
|
c.addUsed("name");
|
||||||
|
|
||||||
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
OrmQueryDetail detail = po.buildDetail(desc);
|
OrmQueryDetail detail = po.buildDetail(desc);
|
||||||
|
|
||||||
@@ -38,11 +38,11 @@ public class ProfileOriginTest extends BaseTestCase {
|
|||||||
c.addUsed("name");
|
c.addUsed("name");
|
||||||
|
|
||||||
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
c = node(null);
|
c = node(null);
|
||||||
c.addUsed("orderDate");
|
c.addUsed("orderDate");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
OrmQueryDetail detail = po.buildDetail(desc);
|
OrmQueryDetail detail = po.buildDetail(desc);
|
||||||
|
|
||||||
@@ -56,11 +56,11 @@ public class ProfileOriginTest extends BaseTestCase {
|
|||||||
c.addUsed("id");
|
c.addUsed("id");
|
||||||
|
|
||||||
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
ProfileOrigin po = new ProfileOrigin(null, false, 1, 1);
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
c = node(null);
|
c = node(null);
|
||||||
c.addUsed("orderDate");
|
c.addUsed("orderDate");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
OrmQueryDetail detail = po.buildDetail(desc);
|
OrmQueryDetail detail = po.buildDetail(desc);
|
||||||
|
|
||||||
@@ -75,15 +75,15 @@ public class ProfileOriginTest extends BaseTestCase {
|
|||||||
NodeUsageCollector c = node(null);
|
NodeUsageCollector c = node(null);
|
||||||
c.addUsed("orderDate");
|
c.addUsed("orderDate");
|
||||||
c.addUsed("customer");
|
c.addUsed("customer");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
c = node("customer");
|
c = node("customer");
|
||||||
c.addUsed("billingAddress");
|
c.addUsed("billingAddress");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
c = node("customer.billingAddress");
|
c = node("customer.billingAddress");
|
||||||
c.addUsed("id");
|
c.addUsed("id");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
OrmQueryDetail detail = po.buildDetail(desc);
|
OrmQueryDetail detail = po.buildDetail(desc);
|
||||||
|
|
||||||
@@ -100,20 +100,20 @@ public class ProfileOriginTest extends BaseTestCase {
|
|||||||
|
|
||||||
NodeUsageCollector c = node(null);
|
NodeUsageCollector c = node(null);
|
||||||
c.addUsed("customer");
|
c.addUsed("customer");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
c = node("customer");
|
c = node("customer");
|
||||||
c.addUsed("id");
|
c.addUsed("id");
|
||||||
c.addUsed("name");
|
c.addUsed("name");
|
||||||
c.addUsed("note");
|
c.addUsed("note");
|
||||||
c.addUsed("billingAddress");
|
c.addUsed("billingAddress");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
//fetch details.product (id,name)
|
//fetch details.product (id,name)
|
||||||
c = node("customer.billingAddress");
|
c = node("customer.billingAddress");
|
||||||
c.addUsed("id");
|
c.addUsed("id");
|
||||||
c.addUsed("line1");
|
c.addUsed("line1");
|
||||||
po.collectUsageInfo(c);
|
po.collectUsageInfo(c.state());
|
||||||
|
|
||||||
OrmQueryDetail detail = po.buildDetail(desc);
|
OrmQueryDetail detail = po.buildDetail(desc);
|
||||||
assertThat(detail.asString()).isEqualTo("fetch customer (name,note) fetch customer.billingAddress (line1)");
|
assertThat(detail.asString()).isEqualTo("fetch customer (name,note) fetch customer.billingAddress (line1)");
|
||||||
|
|||||||
Reference in new issue
Block a user