This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 5dfa785426 [#14834] improvement(plugins): Record @Monitored metrics in
the maintenance and idp-basic plugins (#14884)
5dfa785426 is described below
commit 5dfa7854260f0ba18c245b71d6775f950438355a
Author: spa77k <[email protected]>
AuthorDate: Fri Nov 13 20:28:00 2026 +0900
[#14834] improvement(plugins): Record @Monitored metrics in the maintenance
and idp-basic plugins (#14884)
### What changes were proposed in this pull request?
- `plugins/maintenance` and `plugins/idp-basic` now apply
`libs.plugins.aspectj.post.compile.weaving`, depend on `aspectjrt`, and
weave `MethodMonitorAspect` from `core` into their own classes.
- `core` builds an extra `aspects` jar (classifier `aspects`, exposed
through an `aspects` configuration) that holds only the unshaded
`MethodMonitorAspect` class. The plugins weave from this jar.
- The plugins add it through their own `monitoredAspect` configuration
to the main source set's aspect path, not through the plugin's `aspect`
configuration. `aspect` is extended by `implementation`, so it would put
the aspect jar on the runtime classpath (it ended up in the maintenance
plugin's copied libs), where `core` already provides the aspect.
- New tests in `core` and in each plugin replace the aspect's metrics
system with a fresh one, call monitored methods, and assert the
`<baseMetricName>.total` / `.success` / `.failure` counts.
- Not changed: the optional rename of the `ViewMetaService.updateView`
metric (`updateViewByIdentifier`), to keep existing metric names.
### Why are the changes needed?
`MethodMonitorAspect` is applied by AspectJ post-compile weaving, and
only `core` applied that plugin. The 34 `@Monitored` annotations in
`plugins/maintenance` and `plugins/idp-basic` were no-ops and never
recorded metrics.
`core` cannot be used directly as the aspect path. Its published jar is
the shadow jar that relocates `org.antlr.v4.runtime`, and relocation
rewrites the constant pools of its woven classes. ajc then fails with
`ClassCastException` (`ConstantNameAndType cannot be cast to
ConstantUtf8`) while reading their AspectJ attributes. This is why the
separate `aspects` jar is needed.
Fix #14834
### Does this PR introduce _any_ user-facing change?
The 34 `@Monitored` methods in the two plugins now record metrics in the
relational store metrics source, so new metrics appear. No API or
configuration changes.
### How was this patch tested?
- New tests: `TestMetaServiceMetrics` in `core` (`listMetalakes`
success, `getMetalakeIdByName` failure on a missing metalake),
`TestIdpMetaServiceMetrics` in `idp-basic` (`getIdpUserByUsername`
success and failure, `getIdpGroupByName`), and
`TestMaintenanceMetaServiceMetrics` in `maintenance`
(`insertTableSnapshotMetrics`, `getTableSnapshotMetricsById`). They
pass. The `core` test was run locally in the `coreH2Test` lane only.
- Removing the weaving from `idp-basic` makes its new test fail.
- `javap` on the built plugin jars shows all 34 monitored methods woven
(calls to `MethodMonitorAspect.aspectOf`).
- `:plugins:idp-basic:build` (121 tests) and
`:plugins:maintenance:build` (89 tests) passed.
- `:core:spotlessCheck` passed.
---
core/build.gradle.kts | 15 ++++
.../gravitino/metrics/MethodMonitorAspect.java | 26 ++++++-
.../relational/service/TestMetaServiceMetrics.java | 74 ++++++++++++++++++++
plugins/idp-basic/build.gradle.kts | 17 +++++
.../storage/service/TestIdpMetaServiceMetrics.java | 80 ++++++++++++++++++++++
plugins/maintenance/build.gradle.kts | 17 +++++
.../service/TestMaintenanceMetaServiceMetrics.java | 72 +++++++++++++++++++
7 files changed, 298 insertions(+), 3 deletions(-)
diff --git a/core/build.gradle.kts b/core/build.gradle.kts
index 459efb988d..8519d0e95f 100644
--- a/core/build.gradle.kts
+++ b/core/build.gradle.kts
@@ -109,12 +109,27 @@ val testJar by tasks.registering(Jar::class) {
from(sourceSets["test"].output)
}
+// Modules weave MethodMonitorAspect into their own @Monitored methods from
this jar. The shaded
+// jar cannot be used as the aspect path: relocation rewrites the constant
pool of woven classes and
+// breaks their AspectJ attributes, which makes ajc fail. The jar is written
outside build/libs so
+// that the distribution, which copies every jar in build/libs, does not ship
a second copy of the
+// aspect.
+val aspectJar by tasks.registering(Jar::class) {
+ archiveClassifier.set("aspects")
+ destinationDirectory.set(layout.buildDirectory.dir("aspects"))
+ from(sourceSets["main"].output) {
+ include("org/apache/gravitino/metrics/MethodMonitorAspect.class")
+ }
+}
+
configurations {
create("testArtifacts")
+ create("aspects")
}
artifacts {
add("testArtifacts", testJar)
+ add("aspects", aspectJar)
}
// Core's tests run in four lanes: coreUnitTest, plus
coreH2Test/coreMySQLTest/corePostgreSQLTest.
diff --git
a/core/src/main/java/org/apache/gravitino/metrics/MethodMonitorAspect.java
b/core/src/main/java/org/apache/gravitino/metrics/MethodMonitorAspect.java
index 7e081cd82b..6d383409f8 100644
--- a/core/src/main/java/org/apache/gravitino/metrics/MethodMonitorAspect.java
+++ b/core/src/main/java/org/apache/gravitino/metrics/MethodMonitorAspect.java
@@ -21,6 +21,8 @@ package org.apache.gravitino.metrics;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
+import com.google.common.annotations.VisibleForTesting;
+import javax.annotation.Nullable;
import org.apache.gravitino.GravitinoEnv;
import org.apache.gravitino.metrics.source.MetricsSource;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -34,7 +36,7 @@ import org.slf4j.LoggerFactory;
public class MethodMonitorAspect {
private static final Logger LOG =
LoggerFactory.getLogger(MethodMonitorAspect.class);
- private MetricsSystem metricsSystem;
+ private volatile MetricsSystem metricsSystem;
public MethodMonitorAspect() {
this.metricsSystem = GravitinoEnv.getInstance().metricsSystem();
@@ -46,16 +48,34 @@ public class MethodMonitorAspect {
}
}
+ /**
+ * Replaces the metrics system used by this aspect. The aspect is a
singleton that reads the
+ * metrics system only once, when it is created, so tests use this to
install their own. Do not
+ * call it from production code.
+ *
+ * @param metricsSystem the metrics system to use, or null to disable
monitoring
+ * @return the metrics system used before the call, which may be null, so
the caller can restore
+ * it
+ */
+ @VisibleForTesting
+ @Nullable
+ public MetricsSystem replaceMetricsSystem(@Nullable MetricsSystem
metricsSystem) {
+ MetricsSystem previous = this.metricsSystem;
+ this.metricsSystem = metricsSystem;
+ return previous;
+ }
+
@Pointcut("execution(@org.apache.gravitino.metrics.Monitored * *(..))")
public void monitoredMethods() {}
@Around("monitoredMethods() && @annotation(monitored)")
public Object monitorMethod(ProceedingJoinPoint pjp, Monitored monitored)
throws Throwable {
- if (metricsSystem == null) {
+ MetricsSystem currentMetricsSystem = metricsSystem;
+ if (currentMetricsSystem == null) {
return pjp.proceed();
}
- MetricsSource metricsSource =
metricsSystem.getMetricsSource(monitored.metricsSource());
+ MetricsSource metricsSource =
currentMetricsSystem.getMetricsSource(monitored.metricsSource());
if (metricsSource == null) {
LOG.warn(
"MetricsSource {} is not registered in MetricsSystem, skip
monitoring for method {}",
diff --git
a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestMetaServiceMetrics.java
b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestMetaServiceMetrics.java
new file mode 100644
index 0000000000..de577eb766
--- /dev/null
+++
b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestMetaServiceMetrics.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.storage.relational.service;
+
+import com.codahale.metrics.MetricRegistry;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.metrics.MethodMonitorAspect;
+import org.apache.gravitino.metrics.MetricsSystem;
+import org.apache.gravitino.metrics.source.MetricsSource;
+import org.apache.gravitino.storage.relational.TestJDBCBackend;
+import org.aspectj.lang.Aspects;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+/** Verifies that {@code @Monitored} methods in core are woven and record
metrics. */
+class TestMetaServiceMetrics extends TestJDBCBackend {
+
+ private MethodMonitorAspect aspect;
+ private MetricsSystem originalMetricsSystem;
+ private MetricsSource metricsSource;
+
+ @BeforeEach
+ void installMetricsSystem() {
+ // The aspect reads the metrics system once when it is created, so replace
it for this test.
+ aspect = Aspects.aspectOf(MethodMonitorAspect.class);
+ MetricsSystem metricsSystem = new MetricsSystem();
+ originalMetricsSystem = aspect.replaceMetricsSystem(metricsSystem);
+ metricsSource = new
MetricsSource(MetricsSource.GRAVITINO_RELATIONAL_STORE_METRIC_NAME) {};
+ metricsSystem.register(metricsSource);
+ }
+
+ @AfterEach
+ void restoreMetricsSystem() {
+ aspect.replaceMetricsSystem(originalMetricsSystem);
+ }
+
+ @TestTemplate
+ void testMonitoredMethodsRecordMetrics() {
+ MetalakeMetaService service = MetalakeMetaService.getInstance();
+ service.listMetalakes();
+ Assertions.assertThrows(
+ NoSuchEntityException.class, () ->
service.getMetalakeIdByName("missing_metalake"));
+
+ assertMetrics("listMetalakes", 1, 1, 0);
+ assertMetrics("getMetalakeIdByName", 1, 0, 1);
+ }
+
+ private void assertMetrics(String baseMetricName, long total, long success,
long failure) {
+ Assertions.assertEquals(
+ total, metricsSource.getTimer(MetricRegistry.name(baseMetricName,
"total")).getCount());
+ Assertions.assertEquals(
+ success, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"success")).getCount());
+ Assertions.assertEquals(
+ failure, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"failure")).getCount());
+ }
+}
diff --git a/plugins/idp-basic/build.gradle.kts
b/plugins/idp-basic/build.gradle.kts
index 626a78d809..860ec4c953 100644
--- a/plugins/idp-basic/build.gradle.kts
+++ b/plugins/idp-basic/build.gradle.kts
@@ -17,20 +17,37 @@
* under the License.
*/
+import io.freefair.gradle.plugins.aspectj.WeavingSourceSet
+
plugins {
`maven-publish`
id("java")
id("idea")
+ alias(libs.plugins.aspectj.post.compile.weaving)
+}
+
+// Weave MethodMonitorAspect from core so @Monitored methods in this plugin
record metrics. The
+// `aspect` configuration is not used because it would also put the aspect jar
on the runtime
+// classpath, where core already provides the aspect.
+val monitoredAspect by configurations.creating {
+ isCanBeConsumed = false
+}
+
+sourceSets.main {
+ WeavingSourceSet.getAspectPath(this).from(monitoredAspect)
}
dependencies {
annotationProcessor(libs.lombok)
+ monitoredAspect(project(":core", "aspects"))
+
implementation(project(":api"))
implementation(project(":server-common"))
implementation(project(":common"))
implementation(project(":core"))
+ implementation(libs.aspectj.aspectjrt)
implementation(libs.bundles.jersey)
implementation(libs.commons.lang3)
implementation(libs.guava)
diff --git
a/plugins/idp-basic/src/test/java/org/apache/gravitino/idp/storage/service/TestIdpMetaServiceMetrics.java
b/plugins/idp-basic/src/test/java/org/apache/gravitino/idp/storage/service/TestIdpMetaServiceMetrics.java
new file mode 100644
index 0000000000..cc210c7f98
--- /dev/null
+++
b/plugins/idp-basic/src/test/java/org/apache/gravitino/idp/storage/service/TestIdpMetaServiceMetrics.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.idp.storage.service;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import com.codahale.metrics.MetricRegistry;
+import java.io.IOException;
+import org.apache.gravitino.exceptions.NotFoundException;
+import org.apache.gravitino.metrics.MethodMonitorAspect;
+import org.apache.gravitino.metrics.MetricsSystem;
+import org.apache.gravitino.metrics.source.MetricsSource;
+import org.aspectj.lang.Aspects;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** Verifies that {@code @Monitored} methods in this plugin are woven and
record metrics. */
+class TestIdpMetaServiceMetrics extends AbstractIdpMetaServiceTest {
+
+ private MethodMonitorAspect aspect;
+ private MetricsSystem originalMetricsSystem;
+ private MetricsSource metricsSource;
+
+ @BeforeEach
+ void installMetricsSystem() {
+ // The aspect reads the metrics system once when it is created, so replace
it for this test.
+ aspect = Aspects.aspectOf(MethodMonitorAspect.class);
+ MetricsSystem metricsSystem = new MetricsSystem();
+ originalMetricsSystem = aspect.replaceMetricsSystem(metricsSystem);
+ metricsSource = new
MetricsSource(MetricsSource.GRAVITINO_RELATIONAL_STORE_METRIC_NAME) {};
+ metricsSystem.register(metricsSource);
+ }
+
+ @AfterEach
+ void restoreMetricsSystem() {
+ aspect.replaceMetricsSystem(originalMetricsSystem);
+ }
+
+ @Test
+ void testMonitoredMethodsRecordMetrics() throws IOException {
+ init("h2");
+ insertUsers(1);
+ insertGroups(1);
+
+ IdpUserMetaService userMetaService = IdpUserMetaService.getInstance();
+ userMetaService.getIdpUserByUsername("user1");
+ assertThrows(NotFoundException.class, () ->
userMetaService.getIdpUserByUsername("missing"));
+ assertMetrics("getIdpUserByUsername", 2, 1, 1);
+
+ IdpGroupMetaService.getInstance().getIdpGroupByName("group1");
+ assertMetrics("getIdpGroupByName", 1, 1, 0);
+ }
+
+ private void assertMetrics(String baseMetricName, long total, long success,
long failure) {
+ assertEquals(
+ total, metricsSource.getTimer(MetricRegistry.name(baseMetricName,
"total")).getCount());
+ assertEquals(
+ success, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"success")).getCount());
+ assertEquals(
+ failure, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"failure")).getCount());
+ }
+}
diff --git a/plugins/maintenance/build.gradle.kts
b/plugins/maintenance/build.gradle.kts
index c7464ca27f..7b6e257431 100644
--- a/plugins/maintenance/build.gradle.kts
+++ b/plugins/maintenance/build.gradle.kts
@@ -17,15 +17,31 @@
* under the License.
*/
+import io.freefair.gradle.plugins.aspectj.WeavingSourceSet
+
plugins {
`maven-publish`
id("java")
id("idea")
+ alias(libs.plugins.aspectj.post.compile.weaving)
+}
+
+// Weave MethodMonitorAspect from core so @Monitored methods in this plugin
record metrics. The
+// `aspect` configuration is not used because it would also put the aspect jar
on the runtime
+// classpath, where core already provides the aspect.
+val monitoredAspect by configurations.creating {
+ isCanBeConsumed = false
+}
+
+sourceSets.main {
+ WeavingSourceSet.getAspectPath(this).from(monitoredAspect)
}
dependencies {
annotationProcessor(libs.lombok)
+ monitoredAspect(project(":core", "aspects"))
+
implementation(project(":api"))
implementation(project(":common"))
implementation(project(":core"))
@@ -33,6 +49,7 @@ dependencies {
implementation(project(":maintenance:optimizer-api"))
implementation(project(":server-common"))
+ implementation(libs.aspectj.aspectjrt)
implementation(libs.bundles.jersey)
implementation(libs.commons.lang3)
implementation(libs.db.scheduler)
diff --git
a/plugins/maintenance/src/test/java/org/apache/gravitino/maintenance/storage/service/TestMaintenanceMetaServiceMetrics.java
b/plugins/maintenance/src/test/java/org/apache/gravitino/maintenance/storage/service/TestMaintenanceMetaServiceMetrics.java
new file mode 100644
index 0000000000..ef5f037271
--- /dev/null
+++
b/plugins/maintenance/src/test/java/org/apache/gravitino/maintenance/storage/service/TestMaintenanceMetaServiceMetrics.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.maintenance.storage.service;
+
+import com.codahale.metrics.MetricRegistry;
+import org.apache.gravitino.metrics.MethodMonitorAspect;
+import org.apache.gravitino.metrics.MetricsSystem;
+import org.apache.gravitino.metrics.source.MetricsSource;
+import org.apache.gravitino.storage.relational.TestJDBCBackend;
+import org.aspectj.lang.Aspects;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+/** Verifies that {@code @Monitored} methods in this plugin are woven and
record metrics. */
+class TestMaintenanceMetaServiceMetrics extends TestJDBCBackend {
+
+ private MethodMonitorAspect aspect;
+ private MetricsSystem originalMetricsSystem;
+ private MetricsSource metricsSource;
+
+ @BeforeEach
+ void installMetricsSystem() {
+ // The aspect reads the metrics system once when it is created, so replace
it for this test.
+ aspect = Aspects.aspectOf(MethodMonitorAspect.class);
+ MetricsSystem metricsSystem = new MetricsSystem();
+ originalMetricsSystem = aspect.replaceMetricsSystem(metricsSystem);
+ metricsSource = new
MetricsSource(MetricsSource.GRAVITINO_RELATIONAL_STORE_METRIC_NAME) {};
+ metricsSystem.register(metricsSource);
+ }
+
+ @AfterEach
+ void restoreMetricsSystem() {
+ aspect.replaceMetricsSystem(originalMetricsSystem);
+ }
+
+ @TestTemplate
+ void testMonitoredMethodsRecordMetrics() {
+ TableSnapshotMetricsMetaService service =
TableSnapshotMetricsMetaService.getInstance();
+ long metricsId = service.insertMetrics(1L, 2L, 99L, "{\"mse\":1}");
+ service.getMetrics(metricsId);
+
+ assertMetrics("insertTableSnapshotMetrics", 1, 1, 0);
+ assertMetrics("getTableSnapshotMetricsById", 1, 1, 0);
+ }
+
+ private void assertMetrics(String baseMetricName, long total, long success,
long failure) {
+ Assertions.assertEquals(
+ total, metricsSource.getTimer(MetricRegistry.name(baseMetricName,
"total")).getCount());
+ Assertions.assertEquals(
+ success, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"success")).getCount());
+ Assertions.assertEquals(
+ failure, metricsSource.getMeter(MetricRegistry.name(baseMetricName,
"failure")).getCount());
+ }
+}