diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/DfsClientConf.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/DfsClientConf.java index 00b4c1827eaae4..23241505ba863c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/DfsClientConf.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/DfsClientConf.java @@ -841,6 +841,8 @@ public ShortCircuitConf(Configuration conf) { shortCircuitStreamsCacheSize = conf.getInt( Read.ShortCircuit.STREAMS_CACHE_SIZE_KEY, Read.ShortCircuit.STREAMS_CACHE_SIZE_DEFAULT); + Preconditions.checkArgument(shortCircuitStreamsCacheSize >= 0, + Read.ShortCircuit.STREAMS_CACHE_SIZE_KEY + " can't be negative."); shortCircuitStreamsCacheExpiryMs = conf.getLong( Read.ShortCircuit.STREAMS_CACHE_EXPIRY_MS_KEY, Read.ShortCircuit.STREAMS_CACHE_EXPIRY_MS_DEFAULT); diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/client/impl/TestDfsClientConf.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/client/impl/TestDfsClientConf.java new file mode 100644 index 00000000000000..a10f0164d65f0e --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/client/impl/TestDfsClientConf.java @@ -0,0 +1,53 @@ +/** + * 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.hdfs.client.impl; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TestDfsClientConf { + + @Test + public void testNegativeShortCircuitStreamsCacheSize() { + Configuration conf = new Configuration(); + conf.setInt( + HdfsClientConfigKeys.Read.ShortCircuit.STREAMS_CACHE_SIZE_KEY, -1); + + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> new DfsClientConf(conf)); + assertEquals( + HdfsClientConfigKeys.Read.ShortCircuit.STREAMS_CACHE_SIZE_KEY + + " can't be negative.", + exception.getMessage()); + } + + @Test + public void testZeroShortCircuitStreamsCacheSize() { + Configuration conf = new Configuration(); + conf.setInt( + HdfsClientConfigKeys.Read.ShortCircuit.STREAMS_CACHE_SIZE_KEY, 0); + + DfsClientConf dfsClientConf = new DfsClientConf(conf); + assertEquals(0, dfsClientConf.getShortCircuitConf() + .getShortCircuitStreamsCacheSize()); + } +}