Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14834] improvement(plugins): Record @Monitored metrics in the maintenance and idp-basic plugins (#14884)

spa77k5dfa7854260f
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());
+ }
+}