kevinrr888 commented on code in PR #4527: URL: https://github.com/apache/accumulo/pull/4527#discussion_r1591269719
########## server/base/src/main/java/org/apache/accumulo/server/metrics/NoOpDistributionSummary.java: ########## @@ -0,0 +1,78 @@ +/* + * 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 + * + * https://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.accumulo.server.metrics; + +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import io.micrometer.core.instrument.distribution.HistogramSnapshot; + +/** + * Provides a default DistributionSummary that does not do anything. This can be used to prevent NPE + * if metrics have not been initialized when a DistributionSummary is expected. + * <p> + * Normally DistributionSummaries are created using a builder that takes a registry. + * + * <pre> + * DistributionSummary distSum; + * ... + * public void registerMetrics(MeterRegistry registry) { + * ... + * distSum = DistributionSummary.builder("metric name") .description("...").register(registry); + * ... + * } + * </pre> + * + * Until the registration is called, the instance variable is null. If code then tries to update the + * metric, a NPE is thrown. Using this class to provide a default value prevents this from occurring + * and on registration, it is replaced with a valid instance. + */ +public class NoOpDistributionSummary implements DistributionSummary { Review Comment: Would it be good to log when operating on `NoOpDistributionSummary` since it might be expected that something like `tm.addIdle(time)` has an effect but doesn't ########## server/base/src/test/java/org/apache/accumulo/server/metrics/ThriftMetricsTest.java: ########## @@ -0,0 +1,59 @@ +/* + * 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 + * + * https://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.accumulo.server.metrics; + +import static org.apache.accumulo.core.metrics.MetricsProducer.METRICS_THRIFT_EXECUTE; +import static org.apache.accumulo.core.metrics.MetricsProducer.METRICS_THRIFT_IDLE; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; + +class ThriftMetricsTest { + private final static Logger LOG = LoggerFactory.getLogger(ThriftMetricsTest.class); + + @Test + void testNoNPE() { + ThriftMetrics tm = new ThriftMetrics(); + assertDoesNotThrow(() -> tm.addIdle(1)); + assertDoesNotThrow(() -> tm.addExecute(1)); + } + + @Test + void registerMetrics() { + MeterRegistry registry = new SimpleMeterRegistry(); + ThriftMetrics tm = new ThriftMetrics(); + tm.registerMetrics(registry); + tm.addExecute(1000); + tm.addIdle(1000); + + registry.forEachMeter(m -> { + LOG.trace("meter: {}", m.getId()); + assertTrue(m instanceof DistributionSummary); Review Comment: ```suggestion assertTrue(m instanceof DistributionSummary); assertFalse(m instanceof NoOpDistributionSummary); ``` could check that it is not the default DistSum ########## server/tserver/src/main/java/org/apache/accumulo/tserver/metrics/TabletServerUpdateMetrics.java: ########## @@ -35,7 +36,7 @@ public class TabletServerUpdateMetrics implements MetricsProducer { private Timer commitPrepStat; private Timer walogWriteTimeStat; private Timer commitTimeStat; - private DistributionSummary mutationArraySizeStat; + private DistributionSummary mutationArraySizeStat = new NoOpDistributionSummary();; Review Comment: ```suggestion private DistributionSummary mutationArraySizeStat = new NoOpDistributionSummary(); ``` Unnecessary semi colon ########## server/base/src/main/java/org/apache/accumulo/server/metrics/NoOpDistributionSummary.java: ########## @@ -0,0 +1,78 @@ +/* + * 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 + * + * https://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.accumulo.server.metrics; + +import io.micrometer.core.instrument.DistributionSummary; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import io.micrometer.core.instrument.distribution.HistogramSnapshot; + +/** + * Provides a default DistributionSummary that does not do anything. This can be used to prevent NPE + * if metrics have not been initialized when a DistributionSummary is expected. + * <p> + * Normally DistributionSummaries are created using a builder that takes a registry. + * + * <pre> + * DistributionSummary distSum; + * ... + * public void registerMetrics(MeterRegistry registry) { + * ... + * distSum = DistributionSummary.builder("metric name") .description("...").register(registry); Review Comment: ```suggestion * distSum = DistributionSummary.builder("metric name").description("...").register(registry); ``` Just have an accidental space here -- 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]
