Skip to content

Commit

Permalink
Dump additional objects
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Apr 25, 2019
1 parent 6290f21 commit 501d940
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
15 changes: 9 additions & 6 deletions src/org/jgroups/jmx/ResourceDMBean.java
Expand Up @@ -9,9 +9,12 @@
import org.jgroups.util.Util;

import javax.management.*;
import java.lang.reflect.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -172,11 +175,11 @@ public static boolean isIsMethod(Method method) {


public static void dumpStats(Object obj, final Map<String,Object> map, Log log) {
Consumer<Field> field_func=f -> {
BiConsumer<Field,Object> field_func=(f,o) -> {
String attr_name=null;
try {
f.setAccessible(true);
Object value=f.get(obj);
Object value=f.get(o);
attr_name=Util.getNameFromAnnotation(f);
if(attr_name != null && !attr_name.trim().isEmpty())
attr_name=attr_name.trim();
Expand All @@ -188,12 +191,12 @@ public static void dumpStats(Object obj, final Map<String,Object> map, Log log)
log.warn("Could not retrieve value of attribute (field) " + attr_name, e);
}
};
Consumer<Method> getter_func=m -> {
BiConsumer<Method,Object> getter_func=(m,o) -> {
String method_name=null;
if(!ResourceDMBean.isGetMethod(m))
return;
try {
Object value=m.invoke(obj);
Object value=m.invoke(o);
method_name=Util.getNameFromAnnotation(m);
if(method_name != null && !method_name.trim().isEmpty())
method_name=method_name.trim();
Expand Down
3 changes: 2 additions & 1 deletion src/org/jgroups/protocols/TP.java
Expand Up @@ -991,7 +991,7 @@ public String bundlerStats() {

@ManagedOperation(description="Creates and sets a new bundler. Type has to be either a bundler_type or the fully " +
"qualified classname of a Bundler impl. Stops the current bundler (if running)")
public void bundler(String type) {
public <T extends TP> T bundler(String type) {
Bundler new_bundler=createBundler(type);
String old_bundler_class=null;
if(bundler != null) {
Expand All @@ -1004,6 +1004,7 @@ public void bundler(String type) {
bundler_type=type;
if(old_bundler_class != null)
log.debug("%s: replaced bundler %s with %s", local_addr, old_bundler_class, bundler.getClass().getName());
return (T)this;
}


Expand Down
25 changes: 16 additions & 9 deletions src/org/jgroups/stack/NonReflectiveProbeHandler.java
Expand Up @@ -5,14 +5,15 @@
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.ManagedOperation;
import org.jgroups.annotations.Property;
import org.jgroups.jmx.AdditionalJmxObjects;
import org.jgroups.jmx.ResourceDMBean;
import org.jgroups.util.Util;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

/**
Expand All @@ -35,7 +36,6 @@ public class NonReflectiveProbeHandler extends JChannelProbeHandler {
protected static final Predicate<AccessibleObject> FILTER=obj -> obj.isAnnotationPresent(ManagedAttribute.class) ||
(obj.isAnnotationPresent(Property.class) && obj.getAnnotation(Property.class).exposeAsManagedAttribute()) ||
obj.isAnnotationPresent(ManagedOperation.class);
// (obj instanceof Method && ResourceDMBean.isSetMethod((Method)obj));

public NonReflectiveProbeHandler(JChannel ch) {
super(ch);
Expand All @@ -50,27 +50,35 @@ public NonReflectiveProbeHandler initialize(Collection<Protocol> prots) {
String prot_name=prot.getName();
Map<String,ResourceDMBean.Accessor> m=attrs.computeIfAbsent(prot_name, k -> new TreeMap<>());

Consumer<Field> field_func=field -> m.put(field.getName(), new ResourceDMBean.FieldAccessor(field, prot));
BiConsumer<Field,Object> field_func=(f,o) -> m.put(f.getName(), new ResourceDMBean.FieldAccessor(f, o));

Consumer<Method> method_func=method -> { // getter
BiConsumer<Method,Object> method_func=(method,obj) -> { // getter
if(method.isAnnotationPresent(ManagedOperation.class)) {
Map<String,ResourceDMBean.MethodAccessor> tmp=operations.computeIfAbsent(prot_name, k -> new TreeMap<>());
tmp.put(method.getName(), new ResourceDMBean.MethodAccessor(method, prot));
tmp.put(method.getName(), new ResourceDMBean.MethodAccessor(method, obj));
}
else if(ResourceDMBean.isGetMethod(method)) {
String method_name=Util.getNameFromAnnotation(method);
String attributeName=Util.methodNameToAttributeName(method_name);
m.put(attributeName, new ResourceDMBean.MethodAccessor(method, prot));
m.put(attributeName, new ResourceDMBean.MethodAccessor(method, obj));
}
else if(ResourceDMBean.isSetMethod(method)) { // setter
Map<String,ResourceDMBean.Accessor> tmp=setters.computeIfAbsent(prot_name, k -> new TreeMap<>());
String method_name=Util.getNameFromAnnotation(method);
String attributeName=Util.methodNameToAttributeName(method_name);
tmp.put(attributeName, new ResourceDMBean.MethodAccessor(method, prot));
tmp.put(attributeName, new ResourceDMBean.MethodAccessor(method, obj));
}
};
Util.forAllFieldsAndMethods(prot, FILTER, field_func, method_func);

Util.forAllFieldsAndMethods(prot, FILTER, field_func, method_func);
if(prot instanceof AdditionalJmxObjects) {
Object[] objects=((AdditionalJmxObjects)prot).getJmxObjects();
if(objects != null) {
for(Object obj: objects)
if(obj != null)
Util.forAllFieldsAndMethods(obj, FILTER, field_func, method_func);
}
}
}
return this;
}
Expand Down Expand Up @@ -104,7 +112,6 @@ protected Map<String,Map<String,Object>> dumpAttrsAllProtocols() {
Map<String,Map<String,Object>> retval=new HashMap<>();
for(Map.Entry<String,Map<String,ResourceDMBean.Accessor>> e: attrs.entrySet()) {
String protocol_name=e.getKey();
System.out.printf("--> %s\n", protocol_name);
Map<String,ResourceDMBean.Accessor> val=e.getValue();
Map<String,Object> map=new TreeMap<>();
for(Map.Entry<String,ResourceDMBean.Accessor> en: val.entrySet()) {
Expand Down
21 changes: 5 additions & 16 deletions src/org/jgroups/util/Util.java
Expand Up @@ -4,7 +4,6 @@
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.Property;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.jmx.AdditionalJmxObjects;
import org.jgroups.jmx.JmxConfigurator;
import org.jgroups.logging.Log;
import org.jgroups.protocols.*;
Expand Down Expand Up @@ -34,7 +33,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -2837,30 +2836,20 @@ public static Field[] getAllDeclaredFieldsWithAnnotations(final Class clazz, Cla
* @param method_func The function to be applied to all found methods. No-op if null.
*/
public static void forAllFieldsAndMethods(Object obj, Predicate<? super AccessibleObject> filter,
Consumer<Field> field_func, Consumer<Method> method_func) {
BiConsumer<Field,Object> field_func, BiConsumer<Method,Object> method_func) {
Objects.requireNonNull(obj, "target object cannot be null");
if(field_func != null) {
for(Class<?> clazz=obj.getClass(); clazz != null; clazz=clazz.getSuperclass()) {
Stream.of(clazz.getDeclaredFields()).filter(f -> filter != null && filter.test(f)).forEach(field_func);
Stream.of(clazz.getDeclaredFields()).filter(f -> filter != null && filter.test(f))
.forEach(f -> field_func.accept(f, obj));
}
}
if(method_func != null) {
Stream.of(obj.getClass().getMethods())
.filter(m -> filter != null && filter.test(m)).forEach(method_func);
}

if(obj instanceof AdditionalJmxObjects) {
Object[] objects=((AdditionalJmxObjects)obj).getJmxObjects();
for(Object o: objects) {
if(o != null)
forAllFieldsAndMethods(o, filter, field_func, method_func);
}
.filter(m -> filter != null && filter.test(m)).forEach(m -> method_func.accept(m, obj));
}
}

public static void forAllSetters(Object obj, Predicate<? super AccessibleObject> filter, Consumer<Method> setter_func) {

}

public static String getNameFromAnnotation(AccessibleObject obj) {
ManagedAttribute attr_annotation=obj.getAnnotation(ManagedAttribute.class);
Expand Down
2 changes: 1 addition & 1 deletion tests/perf/org/jgroups/tests/perf/ProgrammaticUPerf2.java
Expand Up @@ -110,6 +110,7 @@ public void viewAccepted(View new_view) {
.setDiagnosticsEnabled(true)
.diagEnableUdp(false) // todo: enable when MulticastSocket works
.diagEnableTcp(true),
// .bundler("no-bundler"),
new TCPPING().initialHosts(Collections.singletonList(new InetSocketAddress(bind_address, 7800))),
new MERGE3(),
new FD_SOCK(),
Expand All @@ -127,7 +128,6 @@ public void viewAccepted(View new_view) {
disp=new RpcDispatcher(channel, null).setMembershipListener(ml)
.setMethodInvoker(ProgrammaticUPerf2::invoke).setMarshaller(new UPerfMarshaller());
h=new NonReflectiveProbeHandler(channel).initialize(channel.getProtocolStack().getProtocols());
// System.out.printf("\nHANDLER:\n%s\n", h.dump());
}
catch(Exception e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 501d940

Please sign in to comment.