devmadhuu commented on code in PR #12071: URL: https://github.com/apache/ozone/pull/12071#discussion_r4664094048
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OMStoragePolicyMetrics.java: ########## @@ -0,0 +1,165 @@ +/* + * 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.hadoop.ozone.om; + +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.commons.lang3.tuple.Triple; +import org.apache.hadoop.hdds.client.ContainerBlockID; +import org.apache.hadoop.hdds.client.OzoneStoragePolicy; +import org.apache.hadoop.hdds.client.StorageTier; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.metrics2.MetricsCollector; +import org.apache.hadoop.metrics2.MetricsInfo; +import org.apache.hadoop.metrics2.MetricsSource; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.metrics2.lib.Interns; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup; +import org.apache.hadoop.ozone.util.MetricUtil; + +/** + * Committed logical bytes attributed to each block's storage-policy allocation outcome. Bytes from a key + * spanning multiple tiers are counted separately per tier. These are per-OM counters with the same commit + * boundary as {@code OMMetrics.TotalDataCommitted}, not current physical-volume usage. + */ +public final class OMStoragePolicyMetrics implements MetricsSource { + private static final String SOURCE_NAME = OMStoragePolicyMetrics.class.getSimpleName(); + private static final MetricsInfo POLICY = Interns.info("Policy", "Recorded key storage policy"); + private static final MetricsInfo REQUESTED_TIER = Interns.info("RequestedTier", "Policy creation tier"); + private static final MetricsInfo ACTUAL_TIER = Interns.info("ActualTier", "SCM-selected tiers of committed blocks"); + private static final MetricsInfo OUTCOME = Interns.info("Outcome", "Preferred, fallback or unknown allocation"); + private static final MetricsInfo COMMITTED_BYTES = Interns.info("CommittedBytes", "Committed logical key bytes"); + + private final ConcurrentMap<Triple<OzoneStoragePolicy, Tier, Outcome>, AtomicLong> committedBytes = + new ConcurrentHashMap<>(); + private String sourceName; + + // These values bound metric cardinality; key, bucket and volume names are never retained or exported. + private enum Tier { + SSD, DISK, ARCHIVE, UNKNOWN + } + + private enum Outcome { + PREFERRED, FALLBACK, UNKNOWN + } + + void register(ConfigurationSource conf) { + sourceName = MetricUtil.getMetricsSourceName(SOURCE_NAME, + conf == null ? null : conf.get(OMConfigKeys.OZONE_OM_INTERNAL_SERVICE_ID), + conf == null ? null : conf.get(OMConfigKeys.OZONE_OM_NODE_ID_KEY)); + DefaultMetricsSystem.instance().register(sourceName, "OM storage policy commit metrics", this); Review Comment: Good point. In production register is only reached through `OMMetrics.create()`, which `OzoneManager` calls once, and shutdown `unregisters` it first. I've still made it idempotent per instance: register returns early if this instance is already registered (the name is recorded only after a successful registration), and unregister clears it, so repeated calls and re-registration are safe. Two different instances with the same name still fail as before. I added `testRepeatedRegisterAndUnregisterAreSafe`, which uses a started metrics system: it fails with "already exists" on the old code and passes now. ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCommitRequest.java: ########## @@ -256,6 +257,8 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, Execut keyName, writerClientId); omKeyInfo = omMetadataManager.getOpenKeyTable(getBucketLayout()).get(dbOpenKey); + // Builder copies below replace the block locations. Retain allocation evidence for successful-commit metrics. + allocatedKeyInfo = omKeyInfo; Review Comment: No copy is needed. After this line `omKeyInfo` is only reassigned to new objects: `withMetadataMutations` and `toBuilder().build()` both build a new `OmKeyInfo`, and toBuilder copies the location groups. `updateLocationInfoList` runs on that new object's groups. The only in-place change to the original is `setModificationTime`, which the metric doesn't read. And`allocatedKeyInfo` is never put into the cache or into a response, So `allocatedKeyInfo` keeps the allocated block tiers and fallback flags, and `copyObject()` would add a full key copy per commit only for metrics. I've updated the comment to explain this. -- 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]
