From 2d61334fd98189b8eee79e480cbde0306245703a Mon Sep 17 00:00:00 2001 From: tian bao <2011xuesong@gmail.com> Date: Mon, 28 Oct 2024 14:56:34 +0800 Subject: [PATCH 1/4] support custom hosts file size --- src/main/java/org/xbill/DNS/hosts/HostsFileParser.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java index 2e6ac420..ea1c9943 100644 --- a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java +++ b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java @@ -29,7 +29,8 @@ */ @Slf4j public final class HostsFileParser { - private static final int MAX_FULL_CACHE_FILE_SIZE_BYTES = 16384; + private static final int MAX_FULL_CACHE_FILE_SIZE_BYTES = Integer.parseInt( + System.getProperty("dnsjava.hostsfile.max_size", "16384")); private final Map hostsCache = new HashMap<>(); private final Path path; From 99afd48d79f9ba93e52270a66a29500bd3d82d3e Mon Sep 17 00:00:00 2001 From: tian bao <2011xuesong@gmail.com> Date: Thu, 14 Nov 2024 23:45:45 +0800 Subject: [PATCH 2/4] add tests and document --- README.adoc | 6 ++++++ .../java/org/xbill/DNS/hosts/HostsFileParser.java | 2 +- .../java/org/xbill/DNS/hosts/HostsFileParserTest.java | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 11e92d5b..9f5509f6 100644 --- a/README.adoc +++ b/README.adoc @@ -103,6 +103,12 @@ Do NOT use it. |true |false +.2+|dnsjava.hostsfile.max_size +3+|Set the size of the hosts file to be loaded at a time,in bytes. +|Integer +|16384 +|1000000 + .2+|dnsjava.nio.selector_timeout 3+|Set selector timeout in milliseconds. Default/Max 1000, Min 1. |Integer diff --git a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java index ea1c9943..9c57e211 100644 --- a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java +++ b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java @@ -29,7 +29,7 @@ */ @Slf4j public final class HostsFileParser { - private static final int MAX_FULL_CACHE_FILE_SIZE_BYTES = Integer.parseInt( + private final int MAX_FULL_CACHE_FILE_SIZE_BYTES = Integer.parseInt( System.getProperty("dnsjava.hostsfile.max_size", "16384")); private final Map hostsCache = new HashMap<>(); diff --git a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java index a9ae9d37..55f02404 100644 --- a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java +++ b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java @@ -171,6 +171,17 @@ void testBigFileIsNotCompletelyCachedA() throws IOException { assertEquals(1, hostsFileParser.cacheSize()); } + @Test + void testBigFileCompletelyCachedA() throws IOException { + System.setProperty("dnsjava.hostsfile.max_size", 1024 * 1024 * 1024 + ""); + HostsFileParser hostsFileParser = generateLargeHostsFile("testBigFileCompletelyCachedA"); + hostsFileParser + .getAddressForHost(Name.fromConstantString("localhost-10."), Type.A) + .orElseThrow(() -> new IllegalStateException("Host entry not found")); + assertEquals(1280, hostsFileParser.cacheSize()); + System.clearProperty("dnsjava.hostsfile.max_size"); + } + @Test void testBigFileIsNotCompletelyCachedAAAA() throws IOException { HostsFileParser hostsFileParser = From 801299cae19fa82422058ee6552c5ab8009b176e Mon Sep 17 00:00:00 2001 From: tian bao <2011xuesong@gmail.com> Date: Mon, 25 Nov 2024 23:05:03 +0800 Subject: [PATCH 3/4] changer for review --- README.adoc | 2 +- .../java/org/xbill/DNS/hosts/HostsFileParser.java | 2 +- .../org/xbill/DNS/hosts/HostsFileParserTest.java | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.adoc b/README.adoc index 9f5509f6..ec3c0ed2 100644 --- a/README.adoc +++ b/README.adoc @@ -104,7 +104,7 @@ Do NOT use it. |false .2+|dnsjava.hostsfile.max_size -3+|Set the size of the hosts file to be loaded at a time,in bytes. +3+|Set the size of the hosts file to be loaded at a time,in bytes. |Integer |16384 |1000000 diff --git a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java index 9c57e211..4bf614a3 100644 --- a/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java +++ b/src/main/java/org/xbill/DNS/hosts/HostsFileParser.java @@ -30,7 +30,7 @@ @Slf4j public final class HostsFileParser { private final int MAX_FULL_CACHE_FILE_SIZE_BYTES = Integer.parseInt( - System.getProperty("dnsjava.hostsfile.max_size", "16384")); + System.getProperty("dnsjava.hostsfile.max_size_bytes", "16384")); private final Map hostsCache = new HashMap<>(); private final Path path; diff --git a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java index 55f02404..0426f2de 100644 --- a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java +++ b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java @@ -173,13 +173,15 @@ void testBigFileIsNotCompletelyCachedA() throws IOException { @Test void testBigFileCompletelyCachedA() throws IOException { - System.setProperty("dnsjava.hostsfile.max_size", 1024 * 1024 * 1024 + ""); + System.setProperty("dnsjava.hostsfile.max_size_bytes", 1024 * 1024 * 1024 + ""); HostsFileParser hostsFileParser = generateLargeHostsFile("testBigFileCompletelyCachedA"); - hostsFileParser - .getAddressForHost(Name.fromConstantString("localhost-10."), Type.A) - .orElseThrow(() -> new IllegalStateException("Host entry not found")); - assertEquals(1280, hostsFileParser.cacheSize()); - System.clearProperty("dnsjava.hostsfile.max_size"); + try { + hostsFileParser.getAddressForHost(Name.fromConstantString("localhost-10."), Type.A) + .orElseThrow(() -> new IllegalStateException("Host entry not found")); + assertEquals(1280, hostsFileParser.cacheSize()); + } finally { + System.clearProperty("dnsjava.hostsfile.max_size_bytes"); + } } @Test From d06041919fff829264f71f4468f04001dcbcdbbc Mon Sep 17 00:00:00 2001 From: tian bao <2011xuesong@gmail.com> Date: Tue, 26 Nov 2024 01:55:21 +0800 Subject: [PATCH 4/4] change again for review --- README.adoc | 4 ++-- src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.adoc b/README.adoc index ec3c0ed2..6c999969 100644 --- a/README.adoc +++ b/README.adoc @@ -103,8 +103,8 @@ Do NOT use it. |true |false -.2+|dnsjava.hostsfile.max_size -3+|Set the size of the hosts file to be loaded at a time,in bytes. +.2+|dnsjava.hostsfile.max_size_bytes +3+|Set the size of the hosts file to be loaded at a time, in bytes. |Integer |16384 |1000000 diff --git a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java index 0426f2de..10ffef41 100644 --- a/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java +++ b/src/test/java/org/xbill/DNS/hosts/HostsFileParserTest.java @@ -173,9 +173,9 @@ void testBigFileIsNotCompletelyCachedA() throws IOException { @Test void testBigFileCompletelyCachedA() throws IOException { - System.setProperty("dnsjava.hostsfile.max_size_bytes", 1024 * 1024 * 1024 + ""); - HostsFileParser hostsFileParser = generateLargeHostsFile("testBigFileCompletelyCachedA"); try { + System.setProperty("dnsjava.hostsfile.max_size_bytes", 1024 * 1024 * 1024 + ""); + HostsFileParser hostsFileParser = generateLargeHostsFile("testBigFileCompletelyCachedA"); hostsFileParser.getAddressForHost(Name.fromConstantString("localhost-10."), Type.A) .orElseThrow(() -> new IllegalStateException("Host entry not found")); assertEquals(1280, hostsFileParser.cacheSize());