adoroszlai commented on code in PR #4140:
URL: https://github.com/apache/ozone/pull/4140#discussion_r1093096165
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOzoneManagerHAWithData.java:
##########
@@ -104,6 +106,43 @@ public void testMultipartUpload() throws Exception {
testMultipartUploadWithOneOmNodeDown();
}
+ @Test
+ public void testOMHAMetrics()
+ throws InterruptedException, TimeoutException {
+ waitForLeaderToBeReady();
+
+ OzoneManager leaderOM = getCluster().getOMLeader();
+ OzoneManager randomOM = getCluster().getOzoneManager(1);
+
+ // Get OMHAMetrics
+ OMHAMetrics omhaMetrics = randomOM.getOmhaMetrics();
+
+ if (randomOM.getOMNodeId()
+ .equals(leaderOM.getOMNodeId())) {
+ Assertions.assertEquals(1L,
+ omhaMetrics.getOmhaInfoOzoneManagerHALeaderState());
+ } else {
+ Assertions.assertEquals(0L,
+ omhaMetrics.getOmhaInfoOzoneManagerHALeaderState());
+ }
+ }
Review Comment:
Please expand the test case (or add another one) to cover change of
leadership.
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOzoneManagerHAWithData.java:
##########
@@ -104,6 +106,43 @@ public void testMultipartUpload() throws Exception {
testMultipartUploadWithOneOmNodeDown();
}
+ @Test
+ public void testOMHAMetrics()
+ throws InterruptedException, TimeoutException {
+ waitForLeaderToBeReady();
+
+ OzoneManager leaderOM = getCluster().getOMLeader();
+ OzoneManager randomOM = getCluster().getOzoneManager(1);
+
+ // Get OMHAMetrics
+ OMHAMetrics omhaMetrics = randomOM.getOmhaMetrics();
+
+ if (randomOM.getOMNodeId()
+ .equals(leaderOM.getOMNodeId())) {
+ Assertions.assertEquals(1L,
+ omhaMetrics.getOmhaInfoOzoneManagerHALeaderState());
+ } else {
+ Assertions.assertEquals(0L,
+ omhaMetrics.getOmhaInfoOzoneManagerHALeaderState());
+ }
Review Comment:
Instead of selecting a "random" node and having only a single assertion, the
test could iterate all OMs and verify value in each.
Also, please reduce duplication:
```suggestion
int expected;
if (randomOM.getOMNodeId()
.equals(leaderOM.getOMNodeId())) {
expected = 1;
} else {
expected = 0;
}
Assertions.assertEquals(expected,
omhaMetrics.getOmhaInfoOzoneManagerHALeaderState());
```
(Please feel free to replace the `if-else` with `?:`.)
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java:
##########
@@ -1816,6 +1818,20 @@ public void updatePeerList(List<String> newPeers) {
}
}
}
+ String leaderId = "";
+ if (isRatisEnabled) {
+ RaftPeer leader = null;
+ try {
+ leader = omRatisServer.getLeader();
+ } catch (IOException ex) {
+ LOG.error("IOException while getting the " +
+ "Ratis server leader.", ex);
+ }
+ if (Objects.nonNull(leader)) {
+ leaderId += leader.getId().toString();
Review Comment:
```suggestion
leaderId = leader.getId().toString();
```
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ha/OMHAMetrics.java:
##########
@@ -0,0 +1,148 @@
+/**
+ * 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.ha;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsInfo;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.metrics2.lib.MetricsRegistry;
+import org.apache.hadoop.ozone.OzoneConsts;
+
+/**
+ * Class to maintain metrics and info related to OM HA.
+ */
+@Metrics(about = "OzoneManager HA Metrics", context = OzoneConsts.OZONE)
+public final class OMHAMetrics implements MetricsSource {
+
+ private enum OMHAMetricsInfo implements MetricsInfo {
+
+ OzoneManagerHALeaderState("Leader active state " +
+ "of OzoneManager node (1 leader, 0 follower)"),
+ NodeId("OM node Id");
+
+ private final String description;
+
+ OMHAMetricsInfo(String description) {
+ this.description = description;
+ }
+
+ @Override
+ public String description() {
+ return description;
+ }
+ }
+
+ /**
+ * Private nested class to hold
+ * the values of OMHAMetricsInfo.
+ */
+ private static final class OMHAInfo {
+
+ private long ozoneManagerHALeaderState;
+ private String nodeId;
+
+ OMHAInfo() {
+ this.ozoneManagerHALeaderState = 0L;
+ this.nodeId = "";
+ }
+
+ public long getOzoneManagerHALeaderState() {
+ return ozoneManagerHALeaderState;
+ }
+
+ public void setOzoneManagerHALeaderState(long ozoneManagerHALeaderState) {
+ this.ozoneManagerHALeaderState = ozoneManagerHALeaderState;
+ }
+
+ public String getNodeId() {
+ return nodeId;
+ }
+
+ public void setNodeId(String nodeId) {
+ this.nodeId = nodeId;
+ }
+ }
+
+ public static final String SOURCE_NAME =
+ OMHAMetrics.class.getSimpleName();
+ private final OMHAInfo omhaInfo = new OMHAInfo();
+ private MetricsRegistry metricsRegistry;
+
+ private String currNodeId;
+ private String leaderId;
+
+ public OMHAMetrics(String currNodeId, String leaderId) {
+ this.currNodeId = currNodeId;
+ this.leaderId = leaderId;
+ this.metricsRegistry = new MetricsRegistry(SOURCE_NAME);
+ }
+
+ /**
+ * Create and return OMHAMetrics instance.
+ * @return OMHAMetrics
+ */
+ public static synchronized OMHAMetrics create(
+ String nodeId, String leaderId) {
+ OMHAMetrics metrics = new OMHAMetrics(nodeId, leaderId);
+ return DefaultMetricsSystem.instance()
+ .register(SOURCE_NAME, "Metrics for OM HA", metrics);
+ }
+
+ /**
+ * Unregister the metrics instance.
+ */
+ public static void unRegister() {
+ DefaultMetricsSystem.instance().unregisterSource(SOURCE_NAME);
+ }
+
+ @Override
+ public synchronized void getMetrics(MetricsCollector collector, boolean all)
{
+
+ MetricsRecordBuilder recordBuilder = collector.addRecord(SOURCE_NAME);
+
+ if (currNodeId.equals(leaderId)) {
+ omhaInfo.setNodeId(currNodeId);
+ omhaInfo.setOzoneManagerHALeaderState(1);
+
+ recordBuilder
+ .tag(OMHAMetricsInfo.NodeId, currNodeId)
+ .addGauge(OMHAMetricsInfo.OzoneManagerHALeaderState, 1);
+ } else {
+ omhaInfo.setNodeId(currNodeId);
+ omhaInfo.setOzoneManagerHALeaderState(0);
+
+ recordBuilder
+ .tag(OMHAMetricsInfo.NodeId, currNodeId)
+ .addGauge(OMHAMetricsInfo.OzoneManagerHALeaderState, 0);
+ }
Review Comment:
Please reduce duplication. The only difference between the branches is the
leader state value (0 or 1).
```suggestion
int state = currNodeId.equals(leaderId) ? 1 : 0;
omhaInfo.setNodeId(currNodeId);
omhaInfo.setOzoneManagerHALeaderState(state);
recordBuilder
.tag(OMHAMetricsInfo.NodeId, currNodeId)
.addGauge(OMHAMetricsInfo.OzoneManagerHALeaderState, state);
```
--
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]