virajjasani commented on code in PR #7136: URL: https://github.com/apache/hbase/pull/7136#discussion_r2206399653
########## hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestBytesReadServerSideScanMetrics.java: ########## @@ -0,0 +1,896 @@ +/* + * 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.hbase.regionserver; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.NavigableSet; +import java.util.TreeSet; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.function.Consumer; +import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.CellComparator; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.PrivateConstants; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.client.metrics.ScanMetrics; +import org.apache.hadoop.hbase.executor.ExecutorType; +import org.apache.hadoop.hbase.io.hfile.BlockCache; +import org.apache.hadoop.hbase.io.hfile.BlockCacheKey; +import org.apache.hadoop.hbase.io.hfile.BlockType; +import org.apache.hadoop.hbase.io.hfile.CompoundBloomFilter; +import org.apache.hadoop.hbase.io.hfile.FixedFileTrailer; +import org.apache.hadoop.hbase.io.hfile.HFile; +import org.apache.hadoop.hbase.io.hfile.HFileBlock; +import org.apache.hadoop.hbase.io.hfile.HFileBlockIndex; +import org.apache.hadoop.hbase.io.hfile.HFileContext; +import org.apache.hadoop.hbase.io.hfile.LruBlockCache; +import org.apache.hadoop.hbase.io.hfile.NoOpIndexBlockEncoder; +import org.apache.hadoop.hbase.nio.ByteBuff; +import org.apache.hadoop.hbase.testclassification.IOTests; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.util.BloomFilter; +import org.apache.hadoop.hbase.util.BloomFilterUtil; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Category({ IOTests.class, LargeTests.class }) +public class TestBytesReadServerSideScanMetrics { Review Comment: Nice variety of tests! ########## hbase-server/src/main/java/org/apache/hadoop/hbase/monitoring/ThreadLocalServerSideScanMetrics.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.hbase.monitoring; + +import java.util.concurrent.atomic.AtomicLong; +import org.apache.hadoop.hbase.client.metrics.ServerSideScanMetrics; +import org.apache.hadoop.hbase.regionserver.RegionScanner; +import org.apache.hadoop.hbase.regionserver.ScannerContext; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Thread-local storage for server-side scan metrics that captures performance data separately for + * each scan thread. This class works in conjunction with {@link ServerSideScanMetrics} to provide + * comprehensive scan performance monitoring. + * <h3>Purpose and Design</h3> {@link ServerSideScanMetrics} captures scan metrics on the server + * side and passes them back to the client in protocol buffer responses. However, the + * {@link ServerSideScanMetrics} instance is not readily available at deeper layers in HBase where + * HFiles are read and individual HFile blocks are accessed. To avoid the complexity of passing + * {@link ServerSideScanMetrics} instances through method calls across multiple layers, this class + * provides thread-local storage for metrics collection. + * <h3>Thread Safety and HBase Architecture</h3> This class leverages a critical aspect of HBase + * server design: on the server side, the thread that opens a {@link RegionScanner} and calls + * {@link RegionScanner#nextRaw(java.util.List, ScannerContext)} is the same thread that reads HFile + * blocks. This design allows thread-local storage to effectively capture metrics without + * cross-thread synchronization. + * <h3>Special Handling for Parallel Operations</h3> The only deviation from the single-thread model + * occurs when {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} is used for + * parallel store file seeking. In this case, special handling ensures that metrics are captured + * correctly across multiple threads. The + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} captures metrics from + * worker threads and aggregates them back to the main scan thread. Please refer to the javadoc of + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} for detailed information + * about this parallel processing mechanism. + * <h3>Usage Pattern</h3> + * <ol> + * <li>Enable metrics collection: {@link #setScanMetricsEnabled(boolean)}</li> + * <li>Reset counters at scan start: {@link #reset()}</li> + * <li>Increment counters during I/O operations using the various {@code add*} methods</li> + * <li>Populate the main metrics object: + * {@link #populateServerSideScanMetrics(ServerSideScanMetrics)}</li> + * </ol> + * <h3>Thread Safety</h3> This class is thread-safe. Each thread maintains its own set of counters + * through {@link ThreadLocal} storage, ensuring that metrics from different scan operations do not + * interfere with each other. + * @see ServerSideScanMetrics + * @see RegionScanner + * @see org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler + */ [email protected] +public final class ThreadLocalServerSideScanMetrics { + private ThreadLocalServerSideScanMetrics() { + } + + private static final ThreadLocal<Boolean> isScanMetricsEnabled = new ThreadLocal<>() { + @Override + protected Boolean initialValue() { + return false; + } + }; + + private static final ThreadLocal<AtomicLong> bytesReadFromFs = new ThreadLocal<>() { + @Override + protected AtomicLong initialValue() { + return new AtomicLong(0); + } + }; + + private static final ThreadLocal<AtomicLong> bytesReadFromBlockCache = new ThreadLocal<>() { + @Override + protected AtomicLong initialValue() { + return new AtomicLong(0); + } + }; + + private static final ThreadLocal<AtomicLong> bytesReadFromMemstore = new ThreadLocal<>() { + @Override + protected AtomicLong initialValue() { + return new AtomicLong(0); + } + }; + + private static final ThreadLocal<AtomicLong> blockReadOpsCount = new ThreadLocal<>() { + @Override + protected AtomicLong initialValue() { + return new AtomicLong(0); + } + }; + + public static final void setScanMetricsEnabled(boolean enable) { Review Comment: `final` is not required for any of these methods, let's remove it. For above constants, it's fine to have it. ########## hbase-server/src/main/java/org/apache/hadoop/hbase/monitoring/ThreadLocalServerSideScanMetrics.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.hbase.monitoring; + +import java.util.concurrent.atomic.AtomicLong; +import org.apache.hadoop.hbase.client.metrics.ServerSideScanMetrics; +import org.apache.hadoop.hbase.regionserver.RegionScanner; +import org.apache.hadoop.hbase.regionserver.ScannerContext; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Thread-local storage for server-side scan metrics that captures performance data separately for + * each scan thread. This class works in conjunction with {@link ServerSideScanMetrics} to provide + * comprehensive scan performance monitoring. + * <h3>Purpose and Design</h3> {@link ServerSideScanMetrics} captures scan metrics on the server + * side and passes them back to the client in protocol buffer responses. However, the + * {@link ServerSideScanMetrics} instance is not readily available at deeper layers in HBase where + * HFiles are read and individual HFile blocks are accessed. To avoid the complexity of passing + * {@link ServerSideScanMetrics} instances through method calls across multiple layers, this class + * provides thread-local storage for metrics collection. + * <h3>Thread Safety and HBase Architecture</h3> This class leverages a critical aspect of HBase + * server design: on the server side, the thread that opens a {@link RegionScanner} and calls + * {@link RegionScanner#nextRaw(java.util.List, ScannerContext)} is the same thread that reads HFile + * blocks. This design allows thread-local storage to effectively capture metrics without + * cross-thread synchronization. + * <h3>Special Handling for Parallel Operations</h3> The only deviation from the single-thread model + * occurs when {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} is used for + * parallel store file seeking. In this case, special handling ensures that metrics are captured + * correctly across multiple threads. The + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} captures metrics from + * worker threads and aggregates them back to the main scan thread. Please refer to the javadoc of + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} for detailed information + * about this parallel processing mechanism. + * <h3>Usage Pattern</h3> + * <ol> + * <li>Enable metrics collection: {@link #setScanMetricsEnabled(boolean)}</li> + * <li>Reset counters at scan start: {@link #reset()}</li> + * <li>Increment counters during I/O operations using the various {@code add*} methods</li> + * <li>Populate the main metrics object: + * {@link #populateServerSideScanMetrics(ServerSideScanMetrics)}</li> + * </ol> + * <h3>Thread Safety</h3> This class is thread-safe. Each thread maintains its own set of counters + * through {@link ThreadLocal} storage, ensuring that metrics from different scan operations do not + * interfere with each other. + * @see ServerSideScanMetrics + * @see RegionScanner + * @see org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler + */ [email protected] +public final class ThreadLocalServerSideScanMetrics { + private ThreadLocalServerSideScanMetrics() { + } + + private static final ThreadLocal<Boolean> isScanMetricsEnabled = new ThreadLocal<>() { Review Comment: nit: static final constants usually have all upper case e.g. `IS_SCAN_METRICS_ENABLED` ########## hbase-server/src/main/java/org/apache/hadoop/hbase/monitoring/ThreadLocalServerSideScanMetrics.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.hbase.monitoring; + +import java.util.concurrent.atomic.AtomicLong; +import org.apache.hadoop.hbase.client.metrics.ServerSideScanMetrics; +import org.apache.hadoop.hbase.regionserver.RegionScanner; +import org.apache.hadoop.hbase.regionserver.ScannerContext; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Thread-local storage for server-side scan metrics that captures performance data separately for + * each scan thread. This class works in conjunction with {@link ServerSideScanMetrics} to provide + * comprehensive scan performance monitoring. + * <h3>Purpose and Design</h3> {@link ServerSideScanMetrics} captures scan metrics on the server + * side and passes them back to the client in protocol buffer responses. However, the + * {@link ServerSideScanMetrics} instance is not readily available at deeper layers in HBase where + * HFiles are read and individual HFile blocks are accessed. To avoid the complexity of passing + * {@link ServerSideScanMetrics} instances through method calls across multiple layers, this class + * provides thread-local storage for metrics collection. + * <h3>Thread Safety and HBase Architecture</h3> This class leverages a critical aspect of HBase + * server design: on the server side, the thread that opens a {@link RegionScanner} and calls + * {@link RegionScanner#nextRaw(java.util.List, ScannerContext)} is the same thread that reads HFile + * blocks. This design allows thread-local storage to effectively capture metrics without + * cross-thread synchronization. + * <h3>Special Handling for Parallel Operations</h3> The only deviation from the single-thread model + * occurs when {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} is used for + * parallel store file seeking. In this case, special handling ensures that metrics are captured + * correctly across multiple threads. The + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} captures metrics from + * worker threads and aggregates them back to the main scan thread. Please refer to the javadoc of + * {@link org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler} for detailed information + * about this parallel processing mechanism. + * <h3>Usage Pattern</h3> + * <ol> + * <li>Enable metrics collection: {@link #setScanMetricsEnabled(boolean)}</li> + * <li>Reset counters at scan start: {@link #reset()}</li> + * <li>Increment counters during I/O operations using the various {@code add*} methods</li> + * <li>Populate the main metrics object: + * {@link #populateServerSideScanMetrics(ServerSideScanMetrics)}</li> + * </ol> + * <h3>Thread Safety</h3> This class is thread-safe. Each thread maintains its own set of counters + * through {@link ThreadLocal} storage, ensuring that metrics from different scan operations do not + * interfere with each other. + * @see ServerSideScanMetrics + * @see RegionScanner + * @see org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler + */ [email protected] +public final class ThreadLocalServerSideScanMetrics { + private ThreadLocalServerSideScanMetrics() { + } + + private static final ThreadLocal<Boolean> isScanMetricsEnabled = new ThreadLocal<>() { + @Override + protected Boolean initialValue() { + return false; + } + }; + + private static final ThreadLocal<AtomicLong> bytesReadFromFs = new ThreadLocal<>() { + @Override + protected AtomicLong initialValue() { + return new AtomicLong(0); + } + }; Review Comment: nit: let's use `ThreadLocal.withInitial()` with lambda ``` private static final ThreadLocal<Boolean> IS_SCAN_METRICS_ENABLED = ThreadLocal.withInitial(() -> false); private static final ThreadLocal<AtomicLong> BYTES_READ_FROM_FS = ThreadLocal.withInitial(() -> new AtomicLong(0)); ... ``` -- 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]
