architjainjain commented on code in PR #6501: URL: https://github.com/apache/hive/pull/6501#discussion_r3713422955
########## ql/src/java/org/apache/hadoop/hive/ql/exec/tez/monitoring/yarnqueue/QueueMetricsState.java: ########## @@ -0,0 +1,287 @@ +/* + * 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.yarnqueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.OptionalLong; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Holds all runtime state for YARN queue metrics collection on a single queue. + * One instance exists per active queue name in the JVM, stored in {@link QueueMetricsCache}. + * <p> + * Owns all per-queue logic: session interval registration, refresh task scheduling, + * thundering herd prevention, and circuit breaker. All fields are private — callers + * interact only through methods. + * <p> + * Ownership model: + * <ul> + * <li>{@code intervalCounts}, {@code minRefreshIntervalMs}, {@code activeSessionCount} + * — owned by {@link #registerInterval}/{@link #deregisterInterval} via lock-free atomics</li> + * <li>{@code refreshTask}, {@code taskCurrentRefreshIntervalMs} + * — owned by {@link #ensureTaskScheduled} under {@code synchronized(this)}</li> + * <li>{@code snapshot}, {@code lastWriteTime} — written by the refresh thread, read by + * TezProgressMonitor; {@code volatile} for visibility without synchronization</li> + * </ul> + */ +public class QueueMetricsState { + private static final Logger LOG = LoggerFactory.getLogger(QueueMetricsState.class); + + private static final int MAX_CONSECUTIVE_FAILURES = 5; + private static final int CIRCUIT_BREAKER_PROBE_INTERVAL = 10; + + // Metrics data (written by refresh thread, read by TezProgressMonitor) + private final AtomicReference<QueueMetricsSnapshot> snapshot; + private volatile long lastWriteTime; + + // Session interval tracking (lock-free atomics) + private final AtomicLong minRefreshIntervalMs; + private final ConcurrentHashMap<Long, AtomicInteger> intervalCounts = new ConcurrentHashMap<>(); + private final AtomicInteger activeSessionCount = new AtomicInteger(0); + + // Refresh task (owned by ensureTaskScheduled under synchronized(this)) + private final AtomicReference<ScheduledFuture<?>> refreshTask = new AtomicReference<>(null); + private final AtomicLong taskCurrentRefreshIntervalMs; + + // Thundering herd guard + private final AtomicBoolean isRefreshing = new AtomicBoolean(false); + + // Circuit breaker + private final AtomicInteger consecutiveFailures = new AtomicInteger(0); + private final AtomicInteger circuitBreakerSkipCount = new AtomicInteger(0); + + QueueMetricsState(QueueMetricsSnapshot snapshot, long refreshIntervalMs) { + this.snapshot = new AtomicReference<>(snapshot); + this.lastWriteTime = 0L; // epoch = "never written" — ensures first fetch fires immediately + this.minRefreshIntervalMs = new AtomicLong(refreshIntervalMs); + this.taskCurrentRefreshIntervalMs = new AtomicLong(refreshIntervalMs); + } + + /** + * Returns the latest snapshot, or null if not yet fetched. + */ + public QueueMetricsSnapshot getSnapshot() { + return snapshot.get(); + } + + /** + * Returns ms since last successful RM write. Large value on first call (lastWriteTime=0). + */ + public long getAgeMs() { + return System.currentTimeMillis() - lastWriteTime; Review Comment: done -- 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]
