Skip to content

Commit

Permalink
ISPN-13836 NullPointerException in channelConnected callback
Browse files Browse the repository at this point in the history
  • Loading branch information
pruivo committed Apr 19, 2022
1 parent 524226f commit 008f99b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Expand Up @@ -20,6 +20,7 @@
import org.infinispan.external.JGroupsProtocolComponent;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.protocols.RED;
import org.jgroups.stack.Protocol;
import org.kohsuke.MetaInfServices;

Expand Down Expand Up @@ -70,19 +71,14 @@ public void generate(PrintWriter w, String packageName, String className) throws
w.println(" static {");
w.println(" List<AttributeMetadata> attributes;");
for (short id = 0; id < 256; id++) {
Class protocol = ClassConfigurator.getProtocol(id);
if (protocol != null && Protocol.class.isAssignableFrom(protocol)) {
w.println(" attributes = new ArrayList<>();");
for (Method method : protocol.getMethods()) {
ManagedAttribute annotation = method.getAnnotation(ManagedAttribute.class);
if (annotation != null && !Modifier.isStatic(method.getModifiers()) && method.getParameterCount() == 0 && isNumber(method.getReturnType())) {
w.printf(" attributes.add(new AttributeMetadata(\"%s\", \"%s\", false, false, \"%s\",\n" +
" false, (Function<%s, ?>) %s::%s, null));%n",
method.getName(), annotation.description().replace('"', '\''), method.getReturnType().getName(), protocol.getName(), protocol.getName(), method.getName());
}
}
w.printf(" PROTOCOL_METADATA.put(%s.class, attributes);%n", protocol.getName());
}
Class<?> protocol = ClassConfigurator.getProtocol(id);
addProtocol(protocol, w);
}

// RED protocol does not have an ID (maybe a bug?)
// Add it manually if an ID is not found.
if (ClassConfigurator.getProtocolId(RED.class) == 0) {
addProtocol(RED.class, w);
}

w.println(" }");
Expand All @@ -92,6 +88,30 @@ public void generate(PrintWriter w, String packageName, String className) throws
w.close();
}

private static void addProtocol(Class<?> protocol, PrintWriter w) {
if (protocol == null || !Protocol.class.isAssignableFrom(protocol)) {
return;
}
boolean hasAttributes = false;

for (Method method : protocol.getMethods()) {
ManagedAttribute annotation = method.getAnnotation(ManagedAttribute.class);
if (annotation != null && !Modifier.isStatic(method.getModifiers()) && method.getParameterCount() == 0 && isNumber(method.getReturnType())) {
if (!hasAttributes) {
w.println(" attributes = new ArrayList<>();");
hasAttributes = true;
}
w.printf(" attributes.add(new AttributeMetadata(\"%s\", \"%s\", false, false, \"%s\",\n" +
" false, (Function<%s, ?>) %s::%s, null));%n",
method.getName(), annotation.description().replace('"', '\''), method.getReturnType().getName(), protocol.getName(), protocol.getName(), method.getName());
}
}
if (hasAttributes) {
// only put the protocol in PROTOCOL_METADATA if we have something to store there
w.printf(" PROTOCOL_METADATA.put(%s.class, attributes);%n", protocol.getName());
}
}

private static boolean isNumber(Class<?> type) {
return short.class == type || byte.class == type || long.class == type || int.class == type || float.class == type || double.class == type || Number.class.isAssignableFrom(type);
}
Expand Down
Expand Up @@ -59,6 +59,7 @@
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.factories.impl.ComponentRef;
import org.infinispan.factories.impl.MBeanMetadata;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.jmx.CacheManagerJmxRegistration;
Expand Down Expand Up @@ -1523,7 +1524,10 @@ public void channelConnected(JChannel channel) {
}
Set<Object> metrics = new HashSet<>();
for (Protocol protocol : c.getProtocolStack().getProtocols()) {
metrics.addAll(mc.registerMetrics(protocol, JGroupsMetricsMetadata.PROTOCOL_METADATA.get(protocol.getClass()), METRICS_PREFIX + name + '_' + protocol.getName().toLowerCase() + '_', null, nodeName));
Collection<MBeanMetadata.AttributeMetadata> attributes = JGroupsMetricsMetadata.PROTOCOL_METADATA.get(protocol.getClass());
if (attributes != null && !attributes.isEmpty()) {
metrics.addAll(mc.registerMetrics(protocol, attributes, METRICS_PREFIX + name + '_' + protocol.getName().toLowerCase() + '_', null, nodeName));
}
}
return metrics;
});
Expand Down

0 comments on commit 008f99b

Please sign in to comment.