jt2594838 commented on code in PR #18950:
URL: https://github.com/apache/iotdb/pull/18950#discussion_r4332535922
##########
iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/jvm/JvmThreadMetrics.java:
##########
@@ -104,7 +104,8 @@ public void unbindFrom(AbstractMetricService metricService)
{
}
// VisibleForTesting
- static long getThreadStateCount(ThreadMXBean threadBean, Thread.State state)
{
+ static synchronized long getThreadStateCount(ThreadMXBean threadBean,
Thread.State state) {
Review Comment:
Synchronize the entire cache refresh and lookup because the shared EnumMap
is mutated in place. This class-level lock makes the read atomic with respect
to clear/compute, preventing a concurrent gauge collection from observing a
transient null value and unboxing it as a long. The 10-second sampling behavior
is unchanged; the targeted JUnit regression passes in the reactor test run.
##########
iotdb-core/metrics/interface/src/test/java/org/apache/iotdb/metrics/metricsets/jvm/JvmThreadMetricsTest.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.iotdb.metrics.metricsets.jvm;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.lang.reflect.Field;
+import java.lang.reflect.Proxy;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.LockSupport;
+import java.util.function.Supplier;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JvmThreadMetricsTest {
+
+ @Before
+ @After
+ public void expireCache() throws Exception {
+ Field lastUpdateTime =
JvmThreadMetrics.class.getDeclaredField("lastUpdateTime");
+ lastUpdateTime.setAccessible(true);
+ synchronized (JvmThreadMetrics.class) {
+ lastUpdateTime.setLong(null, 0L);
+ }
+ }
+
+ // Count two live threads, ignore a terminated thread's null info, and
return zero for an absent
+ // state without resampling within the cache interval.
+ @Test
+ public void testThreadStateCountsAndMissingState() {
+ ThreadInfo info =
+
ManagementFactory.getThreadMXBean().getThreadInfo(Thread.currentThread().getId());
+ AtomicInteger samples = new AtomicInteger();
+ ThreadMXBean threadBean =
+ threadBean(
+ () -> {
+ samples.incrementAndGet();
+ return new ThreadInfo[] {info, info, null};
+ });
+
+ assertEquals(2L, JvmThreadMetrics.getThreadStateCount(threadBean,
info.getThreadState()));
+ assertEquals(0L, JvmThreadMetrics.getThreadStateCount(threadBean,
Thread.State.TERMINATED));
+ assertEquals(1, samples.get());
+ }
+
+ // Pause a refresh after clearing the cache, then read the same state
concurrently. Both calls
+ // must return the complete sample rather than a temporarily absent entry.
+ @Test
+ public void testConcurrentReadDuringRefresh() throws Exception {
Review Comment:
This regression pauses the first refresh after the cache is cleared and
starts a concurrent gauge read. The expected count verifies that the reader
waits for the refresh and never observes the intermediate empty EnumMap; the
missing-state case above separately verifies the documented zero default. The
original implementation fails this test with expected 1 but actual 0, while the
synchronized implementation passes.
--
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]