Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0.x] ISPN-13836 NullPointerException in channelConnected callback #10057

Merged
merged 1 commit into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,15 @@ 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
// Reason: protocol that does not send headers around does not need an ID.
// 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 +89,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) || Modifier.isAbstract(protocol.getModifiers())) {
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 attributes available
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 @@ -60,6 +60,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 @@ -1522,7 +1523,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