Mmuzaf commented on code in PR #3267:
URL: https://github.com/apache/cassandra/pull/3267#discussion_r1582279651
##########
src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java:
##########
@@ -444,30 +410,71 @@ void remove(ThreadPoolMetrics metrics)
public <T extends Metric> T register(MetricName name, T metric,
MetricName... aliases)
{
- setAliases(ArrayUtils.addAll(new MetricName[]{name}, aliases));
T metricLoc = register(name, metric);
Stream.of(aliases).forEach(n -> register(n, metricLoc));
return metricLoc;
}
+ /**
+ * Removes all metrics that match the given predicate.
+ *
+ * @param resolver a function that resolves a short metric name from a
full metric name,
+ * or @{code null} if the metric should not be removed
+ * @param factory a function that creates a metric name from a short
metric name.
+ * @param onRemoved a callback that is called for each removed metric.
+ */
+ public void removeIfMatch(MetricNameResolver resolver,
+ Function<String, MetricName> factory,
+ Consumer<MetricName> onRemoved)
+ {
+ removeMatching((full, metric) -> {
+ String shortName = resolver.resolve(full);
+ if (shortName == null)
+ return false;
+
+ MetricName metricName = factory.apply(shortName);
+ boolean remove = metricName.getMetricName().equals(full);
+
+ if (remove)
+ {
+ unregisterMBean(metricName.getMBeanName(),
MBeanWrapper.instance);
+ onRemoved.accept(metricName);
+ }
+ return remove;
+ });
+ }
+
+ /**
+ * Default implementation of the {@link MetricNameResolver} that resolves
a short metric name from a full metric name,
+ * assuming that the full metric name doesn't contain dots. Returns {@code
null} if the full metric name doesn't match
+ * the provided group and type.
+ *
+ * @param fullName full metric name
+ * @param group metric group
+ * @param type metric type
+ * @return short metric name or {@code null} if the full metric name
doesn't match the group and type.
+ */
+ public static @Nullable String resolveShortMetricName(String fullName,
String group, String type)
+ {
+ String prefix = name(group, type);
+ if (fullName.startsWith(prefix))
+ return fullName.substring(prefix.length() + 1,
fullName.indexOf('.', prefix.length() + 1));
+
+ return null;
+ }
+
public void remove(MetricName name)
{
// Aliases are removed in onMetricRemoved by metrics listener.
- remove(name.getMetricName());
+ boolean success = remove(name.getMetricName());
+ if (success)
Review Comment:
`!success` should never happen in general, but I've noticed that because of
the hierarchical structure of some metrics and the lifecycle we have, we can
try to release a metric more than once.
The `mBeanWrapper.unregisterMBean(name, MBeanWrapper.OnException.IGNORE);`
indicates that some of the exceptions about missing requested metrics are
simply ignored due to the fact that the requested metric is already released.
So I'd keep it as is for safety, but we can change it and see how many tests
will fail.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]