Samrat002 commented on code in PR #28427: URL: https://github.com/apache/flink/pull/28427#discussion_r3610967319
########## flink-core/src/test/java/org/apache/flink/core/fs/FileSystemAttachMetricsTest.java: ########## @@ -0,0 +1,213 @@ +/* + * 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.flink.core.fs; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.core.plugin.MetricsAware; +import org.apache.flink.core.plugin.TestingPluginManager; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +/** + * Tests for {@link FileSystem#attachMetrics(MetricGroup)} and the {@link MetricsAware} two-phase + * init contract. + * + * <p>The headline case is {@link #attachMetricsReachesPluginLoadedMetricsAwareFactory()}: plugin + * file systems are registered wrapped in a {@link PluginFileSystemFactory}, which does <em>not</em> + * itself implement {@link MetricsAware}. {@code attachMetrics} must unwrap the proxy to reach the + * real factory, otherwise the metric group is silently never delivered and no metrics are ever + * emitted. + */ +class FileSystemAttachMetricsTest { + + @AfterEach + void resetFileSystems() { + // Restore the default, plugin-less factory registry so other tests are unaffected. + FileSystem.initialize(new Configuration(), null); + } + + @Test + void attachMetricsReachesPluginLoadedMetricsAwareFactory() { + RecordingMetricsAwareFactory factory = new RecordingMetricsAwareFactory("metrics-test-fs"); + initializeWithPlugins(factory); + + RecordingMetricGroup processGroup = new RecordingMetricGroup(); + FileSystem.attachMetrics(processGroup); + + // Unwrapped through PluginFileSystemFactory and invoked exactly once. + assertThat(factory.setMetricGroupCalls).hasValue(1); + // The group handed to the factory is the "filesystem" child of the process group, not the + // process group itself. + assertThat(processGroup.childGroupNames).containsExactly("filesystem"); + assertThat(factory.receivedGroup.get()).isNotNull().isNotSameAs(processGroup); + } + + @Test + void attachMetricsSkipsFactoriesThatAreNotMetricsAware() { Review Comment: Kept the explicit test name and the inline note that one plain non-MetricsAware factory is sufficient because the behaviour under test is just the instanceof filter. -- 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]
