Mmuzaf commented on code in PR #3267:
URL: https://github.com/apache/cassandra/pull/3267#discussion_r1582196121
##########
src/java/org/apache/cassandra/index/sai/metrics/AbstractMetrics.java:
##########
@@ -57,28 +55,28 @@ public abstract class AbstractMetrics
public void release()
{
- tracked.forEach(Metrics::remove);
- tracked.clear();
+ Metrics.removeIfMatch(fullName -> resolveShortMetricName(fullName,
DefaultNameFactory.GROUP_NAME, TYPE),
+ this::createMetricName, m -> {});
}
- protected CassandraMetricsRegistry.MetricName createMetricName(String name)
+ protected String metricScope()
{
- return createMetricName(name, scope);
+ return scope;
}
- protected CassandraMetricsRegistry.MetricName createMetricName(String
name, String scope)
+ protected CassandraMetricsRegistry.MetricName createMetricName(String name)
{
- String metricScope = keyspace + '.' + table;
- if (index != null)
- {
- metricScope += '.' + index;
- }
- metricScope += '.' + scope + '.' + name;
+ return createMetricName(name, metricScope());
+ }
- CassandraMetricsRegistry.MetricName metricName = new
CassandraMetricsRegistry.MetricName(DefaultNameFactory.GROUP_NAME,
-
TYPE, name, metricScope, createMBeanName(name, scope));
- tracked.add(metricName.getMetricName());
- return metricName;
+ private CassandraMetricsRegistry.MetricName createMetricName(String name,
String scope)
+ {
+ assert name.indexOf('.') == -1 : String.format("Metric name '%s'
should not contain '.'", name);
+ return new
CassandraMetricsRegistry.MetricName(DefaultNameFactory.GROUP_NAME,
+ TYPE,
+ name,
+
MetricRegistry.name(keyspace, table, index, scope, name),
Review Comment:
Some of the metrics are created with `scope = null`, so the
`resolveShortMetricName` method won't work in this case. This is an
implementation bug. Fixed.
##########
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.
Review Comment:
Comment removed.
--
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]