From 639df8b44d3780e9b2b19e77bf1360d0115ae8ea Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 26 Feb 2025 11:04:54 +0800 Subject: [PATCH 1/2] Add deprecated com.google.common.io.Files#write to forbiddenApis --- codestyle/druid-forbidden-apis.txt | 1 + .../java/org/apache/druid/server/log/FileRequestLoggerTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/codestyle/druid-forbidden-apis.txt b/codestyle/druid-forbidden-apis.txt index c53007f76e37..26650fe522ee 100644 --- a/codestyle/druid-forbidden-apis.txt +++ b/codestyle/druid-forbidden-apis.txt @@ -24,6 +24,7 @@ com.google.common.collect.Sets#newLinkedHashSet() @ Create java.util.LinkedHashS com.google.common.collect.Sets#newTreeSet() @ Create java.util.TreeSet directly com.google.common.collect.Sets#newTreeSet(java.util.Comparator) @ Create java.util.TreeSet directly com.google.common.io.Files#createTempDir() @ Use org.apache.druid.java.util.common.FileUtils.createTempDir() +com.google.common.io.Files#write(java.lang.CharSequence, java.io.File, java.nio.charset.Charset) @ Use com.google.common.io.Files.asCharSink(to, charset).write(from) instead. java.io.File#mkdirs() @ Use org.apache.druid.java.util.common.FileUtils.mkdirp instead java.io.File#toURL() @ Use java.io.File#toURI() and java.net.URI#toURL() instead java.lang.String#matches(java.lang.String) @ Use startsWith(), endsWith(), contains(), or compile and cache a Pattern explicitly diff --git a/server/src/test/java/org/apache/druid/server/log/FileRequestLoggerTest.java b/server/src/test/java/org/apache/druid/server/log/FileRequestLoggerTest.java index d0668d7df02b..abbb7b956006 100644 --- a/server/src/test/java/org/apache/druid/server/log/FileRequestLoggerTest.java +++ b/server/src/test/java/org/apache/druid/server/log/FileRequestLoggerTest.java @@ -94,7 +94,7 @@ public void testLogRemove() throws Exception String logString = dateTime + "\t" + HOST + "\t" + "logString"; File oldLogFile = new File(logDir, "2000-01-01.log"); - com.google.common.io.Files.write("testOldLogContent", oldLogFile, StandardCharsets.UTF_8); + com.google.common.io.Files.asCharSink(oldLogFile, StandardCharsets.UTF_8).write("testOldLogContent"); oldLogFile.setLastModified(new Date(0).getTime()); FileRequestLogger fileRequestLogger = new FileRequestLogger( objectMapper, From a295a2384108dac5b1461914fd37dec7c65457e2 Mon Sep 17 00:00:00 2001 From: GWphua Date: Thu, 27 Feb 2025 11:14:26 +0800 Subject: [PATCH 2/2] Replace deprecated Files.write() --- .../indexing/common/tasklogs/HdfsTaskLogsTest.java | 13 ++++++------- .../indexer/hadoop/DatasourceInputFormatTest.java | 2 +- .../indexing/common/tasklogs/FileTaskLogsTest.java | 12 ++++++------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/extensions-core/hdfs-storage/src/test/java/org/apache/druid/indexing/common/tasklogs/HdfsTaskLogsTest.java b/extensions-core/hdfs-storage/src/test/java/org/apache/druid/indexing/common/tasklogs/HdfsTaskLogsTest.java index 9d0273d1a95f..d5b32aa57eb0 100644 --- a/extensions-core/hdfs-storage/src/test/java/org/apache/druid/indexing/common/tasklogs/HdfsTaskLogsTest.java +++ b/extensions-core/hdfs-storage/src/test/java/org/apache/druid/indexing/common/tasklogs/HdfsTaskLogsTest.java @@ -50,7 +50,7 @@ public void testStream() throws Exception final File tmpDir = tempFolder.newFolder(); final File logDir = new File(tmpDir, "logs"); final File logFile = new File(tmpDir, "log"); - Files.write("blah", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("blah"); final TaskLogs taskLogs = new HdfsTaskLogs(new HdfsTaskLogsConfig(logDir.toString()), new Configuration()); taskLogs.pushTaskLog("foo", logFile); @@ -69,11 +69,11 @@ public void testOverwrite() throws Exception final File logFile = new File(tmpDir, "log"); final TaskLogs taskLogs = new HdfsTaskLogs(new HdfsTaskLogsConfig(logDir.toString()), new Configuration()); - Files.write("blah", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("blah"); taskLogs.pushTaskLog("foo", logFile); Assert.assertEquals("blah", readLog(taskLogs, "foo", 0)); - Files.write("blah blah", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("blah blah"); taskLogs.pushTaskLog("foo", logFile); Assert.assertEquals("blah blah", readLog(taskLogs, "foo", 0)); } @@ -86,8 +86,7 @@ public void test_taskStatus() throws Exception final File statusFile = new File(tmpDir, "status.json"); final TaskLogs taskLogs = new HdfsTaskLogs(new HdfsTaskLogsConfig(logDir.toString()), new Configuration()); - - Files.write("{}", statusFile, StandardCharsets.UTF_8); + Files.asCharSink(statusFile, StandardCharsets.UTF_8).write("{}"); taskLogs.pushTaskStatus("id", statusFile); Assert.assertEquals( "{}", @@ -107,7 +106,7 @@ public void testKill() throws Exception final TaskLogs taskLogs = new HdfsTaskLogs(new HdfsTaskLogsConfig(logDir.toString()), new Configuration()); - Files.write("log1content", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("log1content"); taskLogs.pushTaskLog("log1", logFile); Assert.assertEquals("log1content", readLog(taskLogs, "log1", 0)); @@ -118,7 +117,7 @@ public void testKill() throws Exception long time = (System.currentTimeMillis() / 1000) * 1000; Assert.assertTrue(fs.getFileStatus(new Path(logDirPath, "log1")).getModificationTime() < time); - Files.write("log2content", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("log2content"); taskLogs.pushTaskLog("log2", logFile); Assert.assertEquals("log2content", readLog(taskLogs, "log2", 0)); Assert.assertTrue(fs.getFileStatus(new Path(logDirPath, "log2")).getModificationTime() >= time); diff --git a/indexing-hadoop/src/test/java/org/apache/druid/indexer/hadoop/DatasourceInputFormatTest.java b/indexing-hadoop/src/test/java/org/apache/druid/indexer/hadoop/DatasourceInputFormatTest.java index 562ba7f19abc..fe92a7fa569b 100644 --- a/indexing-hadoop/src/test/java/org/apache/druid/indexer/hadoop/DatasourceInputFormatTest.java +++ b/indexing-hadoop/src/test/java/org/apache/druid/indexer/hadoop/DatasourceInputFormatTest.java @@ -357,7 +357,7 @@ public void testGetSplitsUsingDefaultSupplier() throws Exception { // Use the builtin supplier, reading from the local filesystem, rather than testFormatter. final File tmpFile = temporaryFolder.newFile("something:with:colons"); - Files.write("dummy", tmpFile, StandardCharsets.UTF_8); + Files.asCharSink(tmpFile, StandardCharsets.UTF_8).write("dummy"); final ImmutableList mySegments = ImmutableList.of( WindowedDataSegment.of( diff --git a/indexing-service/src/test/java/org/apache/druid/indexing/common/tasklogs/FileTaskLogsTest.java b/indexing-service/src/test/java/org/apache/druid/indexing/common/tasklogs/FileTaskLogsTest.java index 046d666b2a3e..20e809b0334b 100644 --- a/indexing-service/src/test/java/org/apache/druid/indexing/common/tasklogs/FileTaskLogsTest.java +++ b/indexing-service/src/test/java/org/apache/druid/indexing/common/tasklogs/FileTaskLogsTest.java @@ -58,7 +58,7 @@ public void testSimple() throws Exception try { final File logDir = new File(tmpDir, "druid/logs"); final File logFile = new File(tmpDir, "log"); - Files.write("blah", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("blah"); final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir)); taskLogs.pushTaskLog("foo", logFile); @@ -85,7 +85,7 @@ public void testSimpleReport() throws Exception final String taskId = "myTask"; final TestTaskReport testReport = new TestTaskReport(taskId); final String testReportString = mapper.writeValueAsString(TaskReport.buildTaskReports(testReport)); - Files.write(testReportString, reportFile, StandardCharsets.UTF_8); + Files.asCharSink(reportFile, StandardCharsets.UTF_8).write(testReportString); final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir)); taskLogs.pushTaskReports("foo", reportFile); @@ -107,7 +107,7 @@ public void testSimpleStatus() throws Exception final String taskId = "myTask"; final TaskStatus taskStatus = TaskStatus.success(taskId); final String taskStatusString = mapper.writeValueAsString(taskStatus); - Files.write(taskStatusString, statusFile, StandardCharsets.UTF_8); + Files.asCharSink(statusFile, StandardCharsets.UTF_8).write(taskStatusString); final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir)); taskLogs.pushTaskStatus(taskId, statusFile); @@ -124,7 +124,7 @@ public void testPushTaskLogDirCreationFails() throws Exception final File tmpDir = temporaryFolder.newFolder(); final File logDir = new File(tmpDir, "druid/logs"); final File logFile = new File(tmpDir, "log"); - Files.write("blah", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("blah"); if (!tmpDir.setWritable(false)) { throw new RuntimeException("failed to make tmp dir read-only"); @@ -145,7 +145,7 @@ public void testKill() throws Exception final File logFile = new File(tmpDir, "log"); final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir)); - Files.write("log1content", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("log1content"); taskLogs.pushTaskLog("log1", logFile); Assert.assertEquals("log1content", readLog(taskLogs, "log1", 0)); @@ -156,7 +156,7 @@ public void testKill() throws Exception long time = (System.currentTimeMillis() / 1000) * 1000; Assert.assertTrue(new File(logDir, "log1.log").lastModified() < time); - Files.write("log2content", logFile, StandardCharsets.UTF_8); + Files.asCharSink(logFile, StandardCharsets.UTF_8).write("log2content"); taskLogs.pushTaskLog("log2", logFile); Assert.assertEquals("log2content", readLog(taskLogs, "log2", 0)); Assert.assertTrue(new File(logDir, "log2.log").lastModified() >= time);