rakeshadr commented on code in PR #12079:
URL: https://github.com/apache/ozone/pull/12079#discussion_r4706848593


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/pipeline/SCMPipelineMetrics.java:
##########
@@ -106,6 +127,62 @@ public void getMetrics(MetricsCollector collector, boolean 
all) {
     pipelineCreationLatencyNs.snapshot(recordBuilder, true);
     numBlocksAllocated
         .forEach((pid, metric) -> metric.snapshot(recordBuilder, true));
+    tierPlacementFailures.forEach((tier, counters) -> 
counters.forEach((reason, counter) ->
+        collector.addRecord(SOURCE_NAME)
+            .tag(REQUESTED_TIER, tier)
+            .tag(REASON, reason.label)
+            .addCounter(TIER_PLACEMENT_FAILURES, counter.value())));
+  }
+
+  private void addTierPlacementCounters(String tier) {
+    Map<PlacementFailureReason, MutableCounterLong> counters = new 
EnumMap<>(PlacementFailureReason.class);
+    for (PlacementFailureReason reason : PlacementFailureReason.values()) {
+      counters.put(reason, new MutableCounterLong(TIER_PLACEMENT_FAILURES, 0));
+    }
+    tierPlacementFailures.put(tier, counters);
+  }
+
+  /**
+   * Counts one unsuccessful RATIS/standalone tier allocation after all 
provider retries. A later allocation on a
+   * fallback tier may still succeed. This does not count background pipeline 
creation or change aggregate counters.
+   * Missing/empty tiers are UNKNOWN. Reasons use SCM result codes, never 
exception messages; no_space includes
+   * insufficient eligible storage of the requested type, not just full 
volumes; no_suitable_node includes pipeline
+   * limits. Unclassified failures are other.
+   *
+   * @param tier tier of this allocation attempt, including a fallback tier 
when it is attempted
+   * @param failure last unsuccessful creation/wait operation, or null when no 
structured failure is available
+   */
+  void incTierPlacementFailure(StorageTier tier, IOException failure) {
+    String requestedTier = tier == null || tier == StorageTier.EMPTY ? 
UNKNOWN_TIER : tier.name();
+    
tierPlacementFailures.get(requestedTier).get(PlacementFailureReason.fromException(failure)).incr();
+  }
+
+  private enum PlacementFailureReason {
+    NO_SPACE("no_space"),
+    NO_SUITABLE_NODE("no_suitable_node"),
+    OTHER("other");
+
+    private final String label;
+
+    PlacementFailureReason(String label) {
+      this.label = label;
+    }
+
+    private static PlacementFailureReason fromException(IOException failure) {
+      if (failure instanceof SCMException) {
+        SCMException.ResultCodes code = ((SCMException) failure).getResult();
+        if (code == SCMException.ResultCodes.FAILED_TO_FIND_NODES_WITH_SPACE
+            || code == 
SCMException.ResultCodes.FAILED_TO_FIND_CONTAINER_WITH_SPACE) {
+          return NO_SPACE;
+        }
+        if (code == SCMException.ResultCodes.FAILED_TO_FIND_HEALTHY_NODES
+            || code == SCMException.ResultCodes.FAILED_TO_FIND_SUITABLE_NODE
+            || code == SCMException.ResultCodes.FAILED_TO_FIND_NODE_IN_POOL) {

Review Comment:
   How about adding FAILED_TO_FIND_ACTIVE_PIPELINE under NO_SUITABLE_NODE?
   
   no active pipeline means no suitable node group could form. This is a 
placement constraint failure.
   



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/pipeline/SCMPipelineMetrics.java:
##########
@@ -44,6 +50,12 @@ public final class SCMPipelineMetrics implements 
MetricsSource {
 
   private static final String SOURCE_NAME =
       SCMPipelineMetrics.class.getSimpleName();
+  private static final String UNKNOWN_TIER = "UNKNOWN";
+  private static final MetricsInfo REQUESTED_TIER = 
Interns.info("RequestedTier", "Requested allocation tier");
+  private static final MetricsInfo REASON = Interns.info("Reason", "Tier 
allocation failure category");
+  private static final MetricsInfo TIER_PLACEMENT_FAILURES = 
Interns.info("TierPlacementFailures",
+      "Failed RATIS/standalone tier allocations after retries; no_space 
includes storage-type eligibility failures; "

Review Comment:
   `"storage-type eligibility failures;" `
   
   can we change to `"storage-type capacity scarcity failures;" `or 
`"storage-type capacity shortages" `or anything better



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/pipeline/SCMPipelineMetrics.java:
##########
@@ -59,11 +71,20 @@ public final class SCMPipelineMetrics implements 
MetricsSource {
   private @Metric MutableCounterLong numPipelineContainSameDatanodes;
   private @Metric MutableRate pipelineCreationLatencyNs;
   private final Map<PipelineID, MutableCounterLong> numBlocksAllocated;
+  // Series are allocated once; concurrent updates only change the thread-safe 
counters.
+  private final Map<String, Map<PlacementFailureReason, MutableCounterLong>> 
tierPlacementFailures =

Review Comment:
   Since there will be many more metrics for troubleshooting and efficiency 
measurement etc, please raise a separate jira sub-task for **`storage_policy`** 
metrics documentation, if not created it.



##########
hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/pipeline/TestSCMTierPlacementMetrics.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.hdds.scm.pipeline;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+import org.apache.hadoop.hdds.client.StorageTier;
+import org.apache.hadoop.hdds.scm.exceptions.SCMException;
+import org.apache.hadoop.hdds.scm.exceptions.SCMException.ResultCodes;
+import org.apache.hadoop.hdds.server.http.PrometheusMetricsSink;
+import org.apache.hadoop.metrics2.AbstractMetric;
+import org.apache.hadoop.metrics2.MetricType;
+import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl;
+import org.apache.hadoop.metrics2.impl.MetricsRecordImpl;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Verifies the published tier-allocation counters alongside existing pipeline 
metrics.
+ */
+class TestSCMTierPlacementMetrics {
+  private SCMPipelineMetrics metrics;
+
+  @BeforeEach
+  void setUp() {
+    SCMPipelineMetrics.unRegister();
+    metrics = SCMPipelineMetrics.create();
+  }
+
+  @AfterEach
+  void tearDown() {
+    SCMPipelineMetrics.unRegister();
+  }
+
+  @ParameterizedTest
+  @MethodSource("failureReasons")
+  void classifiesResultCodesWithoutParsingMessages(IOException failure, String 
reason) throws Exception {
+    metrics.incTierPlacementFailure(StorageTier.SSD, failure);
+    String output = prometheusOutput();
+    assertThat(output).contains(series("SSD", reason, 1));
+    assertEquals(1, totalFailures());
+  }
+
+  static Stream<Arguments> failureReasons() {
+    return Stream.of(
+        Arguments.of(new 
SCMException(ResultCodes.FAILED_TO_FIND_NODES_WITH_SPACE), "no_space"),
+        Arguments.of(new 
SCMException(ResultCodes.FAILED_TO_FIND_CONTAINER_WITH_SPACE), "no_space"),

Review Comment:
   The test hardcodes "no_space", "no_suitable_node", "other" strings. Use the 
enum's label instead.
   
    In SCMPipelineMetrics.java, you can change private enum to package-private:
   `enum PlacementFailureReason {`
   



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

Reply via email to