Copilot commented on code in PR #12150:
URL: https://github.com/apache/ozone/pull/12150#discussion_r4680265666
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -212,14 +214,50 @@ private synchronized void refreshBlockInfo(IOException
cause) throws IOException
* @return BlockData.
*/
protected BlockData getBlockData() throws IOException {
+ releaseCachedReplica();
+ final ShortCircuitStreamCache cache = getShortCircuitStreamCache();
+ if (cache != null) {
+ final ShortCircuitStreamCache.Replica replica = cache.acquire(blockID,
length);
+ if (replica != null) {
+ cachedReplica = replica;
+ blockFileInputStream = replica.getStream();
Review Comment:
Interrupting one reader during a `FileChannel.read()` closes the shared
channel, causing unrelated readers of the cached block to fail with
`ClosedChannelException`. Cache invalidation does not recover those readers
because their retries retain the same local chunk streams and closed channel.
Isolate active readers' descriptors or rebuild their local streams with a
usable descriptor (or fall back to gRPC), and add a concurrent-reader
cancellation regression test.
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -212,14 +214,50 @@ private synchronized void refreshBlockInfo(IOException
cause) throws IOException
* @return BlockData.
*/
protected BlockData getBlockData() throws IOException {
+ releaseCachedReplica();
+ final ShortCircuitStreamCache cache = getShortCircuitStreamCache();
+ if (cache != null) {
+ final ShortCircuitStreamCache.Replica replica = cache.acquire(blockID,
length);
+ if (replica != null) {
+ cachedReplica = replica;
+ blockFileInputStream = replica.getStream();
+ return replica.getBlockData();
+ }
+ }
acquireClient();
try {
- return getBlockDataUsingClient();
+ final BlockData data = getBlockDataUsingClient();
+ if (cache != null && blockFileInputStream != null && data.getSize() >=
length) {
+ cachedReplica = cache.put(blockID, data, blockFileInputStream);
+ }
+ return data;
} finally {
releaseClient();
}
}
+ /** @return the short-circuit stream cache if this block may be served from
or added to it, otherwise null. */
+ private ShortCircuitStreamCache getShortCircuitStreamCache() {
+ if (xceiverClientFactory == null || fallbackToGrpc.get() || blockInfo ==
null) {
Review Comment:
Please bypass cache lookup and insertion for under-construction blocks.
Repeated `hsync()` calls within the same block can leave OM's length and BCSID
unchanged, so cached metadata still passes validation and hides newly synced
bytes until expiry. Excluding `blockInfo.isUnderConstruction()` here preserves
the fresh GetBlock used to obtain the current size and chunk list.
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/OzoneClientConfig.java:
##########
@@ -64,6 +64,23 @@ public class OzoneClientConfig {
"temporarily for this period of time(seconds).",
tags = { ConfigTag.CLIENT })
private long shortCircuitReadDisableInterval = 60 * 10;
+
+ @Config(key = SHORT_CIRCUIT_PREFIX + "streams.cache.size",
+ defaultValue = "256",
+ type = ConfigType.INT,
+ description = "Maximum number of local block replicas (block metadata
and open file descriptor) cached by " +
+ "a client for short-circuit read, so that reopening a cached block
does not send GetBlock to the " +
+ "datanode again. Set to 0 to disable the cache. Only used when
short-circuit read is enabled.",
+ tags = { ConfigTag.CLIENT })
+ private int shortCircuitBlockCacheSize = 256;
+
+ @Config(key = SHORT_CIRCUIT_PREFIX + "streams.cache.expiry",
+ defaultValue = "5m",
+ type = ConfigType.TIME,
+ description = "Time after its last access when a local block replica is
removed from the short-circuit " +
+ "block cache.",
Review Comment:
The public setting promises expiration after the last access, but
`ShortCircuitStreamCache` uses `expireAfterWrite`. A frequently accessed
replica therefore expires five minutes after insertion rather than five minutes
after becoming idle. Please align the implementation and documentation: use
access-based expiry for the documented idle timeout, or describe the
insertion-based TTL both here and in the cache Javadoc if that hard freshness
bound is intentional.
##########
hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestShortCircuitStreamCache.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.hdds.scm.storage;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hdds.client.BlockID;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.BlockData;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChecksumData;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChecksumType;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChunkInfo;
+import org.apache.hadoop.hdds.scm.OzoneClientConfig;
+import org.apache.hadoop.hdds.scm.XceiverClientFactory;
+import org.apache.hadoop.hdds.scm.pipeline.MockPipeline;
+import org.apache.ozone.test.GenericTestUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests for {@link ShortCircuitStreamCache}.
+ */
+class TestShortCircuitStreamCache {
+
+ private static final long EXPIRY_MS = TimeUnit.MINUTES.toMillis(5);
+
+ @TempDir
+ private Path dir;
+
+ private static ScheduledExecutorService cleanupExecutor;
+ private static ExecutorService closeExecutor;
+
+ @BeforeAll
+ static void init() {
+ closeExecutor = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new
LinkedBlockingQueue<>(),
+ new ThreadFactoryBuilder().setDaemon(true).build());
+ cleanupExecutor = Executors.newSingleThreadScheduledExecutor(
+ new ThreadFactoryBuilder().setDaemon(true).build());
+ }
+
+ @AfterAll
+ static void cleanup() {
+ closeExecutor.shutdownNow();
+ cleanupExecutor.shutdownNow();
+ }
+
+ @Test
+ void testPut() throws Exception {
+ try (ShortCircuitStreamCache cache = new ShortCircuitStreamCache(10, 1,
closeExecutor, cleanupExecutor)) {
+ ShortCircuitStreamCache.Replica replica = cache.put(blockID(1, 5),
blockData(1, 5, 100), newStream());
+ cache.release(replica);
+ Thread.sleep(10);
+
+ assertNull(cache.acquire(blockID(1, 5), 100));
+ assertTrue(replica.isClosed());
Review Comment:
Expiration only queues descriptor closure on `closeExecutor`; neither the 10
ms sleep nor `acquire()` waits for that task. On a busy runner, the entry can
already be absent while `replica.isClosed()` is still false, making this test
flaky. Wait for closure with `GenericTestUtils.waitFor`, as the other eviction
tests do.
--
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]