ivanzlenko commented on code in PR #6344: URL: https://github.com/apache/ozone/pull/6344#discussion_r1525781277
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/BucketUtilizationMetrics.java: ########## @@ -0,0 +1,112 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.ozone.om; + +import org.apache.hadoop.hdds.annotation.InterfaceAudience; +import org.apache.hadoop.hdds.utils.db.cache.CacheKey; +import org.apache.hadoop.hdds.utils.db.cache.CacheValue; +import org.apache.hadoop.metrics2.MetricsCollector; +import org.apache.hadoop.metrics2.MetricsInfo; +import org.apache.hadoop.metrics2.MetricsSource; +import org.apache.hadoop.metrics2.MetricsSystem; +import org.apache.hadoop.metrics2.annotation.Metrics; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.om.helpers.OmBucketInfo; + +import java.util.Iterator; +import java.util.Map.Entry; + +/** + * A class for collecting and reporting bucket utilization metrics. + * <p> + * Available metrics: + * <ul> + * <li>Bytes used in bucket. + * <li>Bucket quote in bytes. + * <li>Bucket quota in namespace. + * <li>Bucket available space. Calculated from difference between used bytes in bucket and bucket quota. + * If bucket quote is not set then this metric show -1 as value. + * </ul> + */ [email protected] +@Metrics(about = "Ozone Bucket Utilization Metrics", context = OzoneConsts.OZONE) +public class BucketUtilizationMetrics implements MetricsSource { + + private static final String SOURCE = BucketUtilizationMetrics.class.getSimpleName(); + + private final OMMetadataManager metadataManager; + + public BucketUtilizationMetrics(OMMetadataManager metadataManager) { + this.metadataManager = metadataManager; + } + + public static BucketUtilizationMetrics create(OMMetadataManager metadataManager) { + MetricsSystem ms = DefaultMetricsSystem.instance(); + return ms.register(SOURCE, "Bucket Utilization Metrics", new BucketUtilizationMetrics(metadataManager)); + } + + @Override + public void getMetrics(MetricsCollector collector, boolean all) { + Iterator<Entry<CacheKey<String>, CacheValue<OmBucketInfo>>> bucketIterator = metadataManager.getBucketIterator(); + + while (bucketIterator.hasNext()) { + Entry<CacheKey<String>, CacheValue<OmBucketInfo>> entry = bucketIterator.next(); + OmBucketInfo bucketInfo = entry.getValue().getCacheValue(); + if (bucketInfo == null) { + continue; + } + + long availableSpace = bucketInfo.getQuotaInBytes() - bucketInfo.getUsedBytes(); + + collector.addRecord(SOURCE) + .setContext("Bucket metrics") + .tag(BucketMetricsInfo.VolumeName, bucketInfo.getVolumeName()) + .tag(BucketMetricsInfo.BucketName, bucketInfo.getBucketName()) + .addGauge(BucketMetricsInfo.BucketUsedBytes, bucketInfo.getUsedBytes()) + .addGauge(BucketMetricsInfo.BucketQuotaBytes, bucketInfo.getQuotaInBytes()) + .addGauge(BucketMetricsInfo.BucketQuotaNamespace, bucketInfo.getQuotaInNamespace()) + .addGauge(BucketMetricsInfo.BucketAvailableBytes, availableSpace > 0 ? availableSpace : -1); + } + } + + public void unRegister() { + MetricsSystem ms = DefaultMetricsSystem.instance(); + ms.unregisterSource(SOURCE); + } + + enum BucketMetricsInfo implements MetricsInfo { Review Comment: This class used in tests so it should be package-private -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
