Copilot commented on code in PR #3758:
URL: https://github.com/apache/celeborn/pull/3758#discussion_r3589577295


##########
common/src/main/scala/org/apache/celeborn/common/metrics/sink/JmxSink.scala:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.celeborn.common.metrics.sink
+
+import java.util.Properties
+
+import com.codahale.metrics.MetricRegistry
+import com.codahale.metrics.jmx.JmxReporter
+
+class JmxSink(val property: Properties, val registry: MetricRegistry) extends 
Sink {
+
+  val reporter: JmxReporter = JmxReporter.forRegistry(registry).build()

Review Comment:
   Using `JmxReporter` with default settings publishes into the global JMX 
domain `\"metrics\"`. If multiple Celeborn components/MetricRegistries run in 
the same JVM (or multiple tests run in-process), this can cause MBean name 
collisions and cross-component interference. Consider allowing the JMX domain 
(and optionally the MBeanServer) to be configured via `Properties`, and 
defaulting to a Celeborn-specific domain (or one derived from the metrics 
namespace/instance name) using `JmxReporter.Builder.inDomain(...)`.



##########
service/src/test/scala/org/apache/celeborn/server/common/metrics/sink/JmxSinkSuite.scala:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.celeborn.server.common.metrics.sink
+
+import java.lang.management.ManagementFactory
+import javax.management.ObjectName
+
+import scala.collection.JavaConverters._
+
+import org.apache.celeborn.CelebornFunSuite
+import org.apache.celeborn.common.CelebornConf
+import org.apache.celeborn.common.metrics.MetricsSystem
+import org.apache.celeborn.common.metrics.sink.JmxSink
+import org.apache.celeborn.common.metrics.source.JVMSource
+import org.apache.celeborn.common.network.TestHelper
+
+class JmxSinkSuite extends CelebornFunSuite {
+
+  private def newMetricsSystem(): MetricsSystem = {
+    val celebornConf = new CelebornConf()
+    celebornConf
+      .set(CelebornConf.METRICS_ENABLED.key, "true")
+      .set(
+        CelebornConf.METRICS_CONF.key,
+        TestHelper.getResourceAsAbsolutePath("/metrics-jmx.properties"))
+    val metricsSystem = MetricsSystem.createMetricsSystem("test", celebornConf)
+    metricsSystem.registerSource(new JVMSource(celebornConf, "test"))
+    metricsSystem
+  }
+
+  test("test load jmx sink case") {
+    val metricsSystem = newMetricsSystem()
+    metricsSystem.start(true)
+
+    try {
+      // JmxSink has no dedicated branch in MetricsSystem.registerSinks, so it 
must be
+      // instantiated reflectively via its (Properties, MetricRegistry) 
constructor.
+      assert(metricsSystem.sinks.exists(_.isInstanceOf[JmxSink]))
+    } finally {
+      metricsSystem.stop()
+    }
+  }
+
+  test("test jmx sink registers and unregisters MBeans lifecycle case") {
+    val metricsSystem = newMetricsSystem()
+    val mBeanServer = ManagementFactory.getPlatformMBeanServer
+    // JmxReporter publishes metrics under the "metrics" JMX domain by default.
+    val jmxDomainPattern = new ObjectName("metrics:*")
+
+    val beforeStart = mBeanServer.queryNames(jmxDomainPattern, null).asScala
+
+    // start() runs registerSinks() (reflection-based loading) followed by 
Sink.start(),
+    // which makes the JmxSink's JmxReporter register the registry metrics as 
MBeans.
+    metricsSystem.start(true)
+    val afterStart = mBeanServer.queryNames(jmxDomainPattern, null).asScala
+    val registeredByJmxSink = afterStart -- beforeStart
+
+    metricsSystem.stop()
+    val afterStop = mBeanServer.queryNames(jmxDomainPattern, null).asScala

Review Comment:
   This test does not guarantee `metricsSystem.stop()` runs if `start(true)` or 
the intermediate JMX queries throw, which can leak a running reporter/MBeans 
into other tests and cause flakes. Wrap the start/query/assert sequence in a 
`try` and call `metricsSystem.stop()` in a `finally` to ensure cleanup on 
failures.



-- 
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]

Reply via email to