architjainjain commented on code in PR #6501:
URL: https://github.com/apache/hive/pull/6501#discussion_r3718949736


##########
ql/src/test/org/apache/hadoop/hive/ql/exec/tez/monitoring/TestTezJobMonitorQueueMetrics.java:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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.hive.ql.exec.tez.monitoring;
+
+import 
org.apache.hadoop.hive.ql.exec.tez.monitoring.yarnqueue.NoOpQueueMetricsCollector;
+import 
org.apache.hadoop.hive.ql.exec.tez.monitoring.yarnqueue.QueueMetricsCollector;
+import 
org.apache.hadoop.hive.ql.exec.tez.monitoring.yarnqueue.YarnQueueMetricsCollector;
+
+import java.lang.reflect.Field;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConfForTest;
+import org.apache.hadoop.hive.ql.Context;
+import org.apache.hadoop.hive.ql.exec.tez.TezSession;
+import org.apache.hadoop.hive.ql.log.PerfLogger;
+import org.apache.hadoop.hive.ql.plan.BaseWork;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hadoop.yarn.client.api.YarnClient;
+import org.apache.tez.common.counters.TezCounters;
+import org.apache.tez.dag.api.DAG;
+import org.apache.tez.dag.api.client.DAGClient;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test cases for TezJobMonitor queue metrics initialization.
+ */
+public class TestTezJobMonitorQueueMetrics {
+
+  @Mock
+  private TezSession mockSession;
+  @Mock
+  private DAGClient mockDagClient;
+  @Mock
+  private DAG mockDag;
+  @Mock
+  private Context mockContext;
+  @Mock
+  private PerfLogger mockPerfLogger;
+  @Mock
+  private YarnClient mockYarnClient;
+  @Mock
+  private TezCounters mockCounters;
+
+  private HiveConf hiveConf;
+  private List<BaseWork> topSortedWorks;
+  private SessionState sessionState;
+  private AutoCloseable mockCloseable;
+
+  @Before
+  public void setUp() {
+    mockCloseable = MockitoAnnotations.openMocks(this);
+    hiveConf = new HiveConfForTest(TestTezJobMonitorQueueMetrics.class);
+    hiveConf.set("hive.security.authorization.manager",
+        
"org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdConfOnlyAuthorizerFactory");
+    sessionState = SessionState.start(hiveConf);
+    topSortedWorks = new ArrayList<>();
+    when(mockDag.getName()).thenReturn("test-dag-1");
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    if (mockCloseable != null) {
+      mockCloseable.close();
+    }
+    if (sessionState != null) {
+      sessionState.close();
+    }
+  }
+
+  @Test
+  public void testMetricsCollectorDisabledByDefault() throws Exception {
+    when(mockSession.getYarnClient()).thenReturn(null); // YarnClient should 
not be initialized
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor should be created", monitor);
+    // When metrics are disabled (interval=0), getYarnClient() is never called 
because
+    // the check happens before attempting to retrieve the YarnClient
+    verify(mockSession, never()).getYarnClient();
+    verify(mockYarnClient, never()).getQueueInfo(anyString());
+  }
+
+  @Test
+  public void testMetricsCollectorEnabledWithInterval() {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
10, TimeUnit.SECONDS);
+
+    when(mockSession.getYarnClient()).thenReturn(mockYarnClient);
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor should be created", monitor);
+    verify(mockSession, atLeastOnce()).getYarnClient();
+  }
+
+  @Test
+  public void testMetricsCollectorDisabledWithZeroInterval() throws Exception {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
0, TimeUnit.SECONDS);
+
+    when(mockSession.getYarnClient()).thenReturn(null); // YarnClient should 
not be initialized
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor should be created", monitor);
+    // When metrics are disabled (interval=0), getYarnClient() is never called
+    verify(mockSession, never()).getYarnClient();
+    verify(mockYarnClient, never()).getQueueInfo(anyString());
+  }
+
+  @Test
+  public void testMetricsCollectorDisabledWithNegativeInterval() throws 
Exception {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
-1, TimeUnit.SECONDS);
+
+    when(mockSession.getYarnClient()).thenReturn(null); // YarnClient should 
not be initialized
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor should be created", monitor);
+    // When metrics are disabled (interval<0), getYarnClient() is never called
+    verify(mockSession, never()).getYarnClient();
+    verify(mockYarnClient, never()).getQueueInfo(anyString());
+  }
+
+  @Test
+  public void testMetricsCollectorWithSmallInterval() {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
500, TimeUnit.MILLISECONDS);
+
+    when(mockSession.getYarnClient()).thenReturn(mockYarnClient);
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor should be created with adjusted interval", monitor);
+  }
+
+  @Test
+  public void testMetricsCollectorWithCustomQueue() {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
15, TimeUnit.SECONDS);
+
+    when(mockSession.getYarnClient()).thenReturn(mockYarnClient);
+    when(mockSession.getQueueName()).thenReturn("production.analytics");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    verify(mockSession, atLeastOnce()).getQueueName();
+    assertNotNull("Monitor should be created with custom queue", monitor);
+  }
+
+  /**
+   * Metrics enabled with a null YarnClient: monitor must still be created and 
must
+   * reach the YarnClient gate (verify getYarnClient called), but must NOT call
+   * getQueueName (nothing to resolve without a client).
+   */
+  @Test
+  public void testMetricsCollectorWithNullYarnClient() {
+    
hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TEZ_QUEUE_METRICS_REFRESH_INTERVAL, 
10, TimeUnit.SECONDS);
+    when(mockSession.getYarnClient()).thenReturn(null);
+    when(mockSession.getQueueName()).thenReturn("default");
+
+    TezJobMonitor monitor =
+        new TezJobMonitor(mockSession, topSortedWorks, mockDagClient, 
hiveConf, mockDag, mockContext, mockCounters,
+            mockPerfLogger);
+
+    assertNotNull("Monitor must be created when YarnClient is null", monitor);
+    verify(mockSession, atLeastOnce()).getYarnClient();
+  }
+
+  /**
+   * Metrics enabled with a null queue name: monitor must be created and the 
code
+   * must reach both the YarnClient and queue-name gates.
+   */
+  @Test
+  public void testMetricsCollectorWithNullQueueName() {

Review Comment:
   The test verifies that when getQueueName() returns null, the monitor doesn't 
fail — it silently defaults to "default" and still creates a working 
YarnQueueMetricsCollector. The assertions confirm:
   collector.getClass() → YarnQueueMetricsCollector — a real collector was 
created, not a no-op fallback
   collector.isEnabled() → true — metrics collection is still active
   collector.getQueueName() → "default" — the null queue name was correctly 
substituted with the default



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