From cdcc70c8fe05b3d7add42e88014ff0f558426f70 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 28 Sep 2015 16:58:19 +0200 Subject: [PATCH] Allow to change property sonar.ce.maxLogsPerTask without restarting server --- .../server/computation/log/CeLogging.java | 17 +++++-------- .../server/computation/log/CeLoggingTest.java | 24 +++++++++++-------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java b/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java index 8df51dd2335..6b90db47fdc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java @@ -57,22 +57,13 @@ public class CeLogging { public static final String MAX_LOGS_PROPERTY = "sonar.ce.maxLogsPerTask"; private final File logsDir; - private final int maxLogs; + private final Settings settings; public CeLogging(Settings settings) { String dataDir = settings.getString(ProcessProperties.PATH_DATA); checkArgument(dataDir != null, "Property %s is not set", ProcessProperties.PATH_DATA); this.logsDir = logsDirFromDataDir(new File(dataDir)); - this.maxLogs = settings.getInt(MAX_LOGS_PROPERTY); - if (maxLogs < 0) { - throw new IllegalArgumentException(format("Property %s must be positive. Got: %d", MAX_LOGS_PROPERTY, maxLogs)); - } - } - - @VisibleForTesting - CeLogging(File logsDir, int maxLogs) { - this.logsDir = logsDir; - this.maxLogs = maxLogs; + this.settings = settings; } /** @@ -114,6 +105,10 @@ public class CeLogging { @VisibleForTesting void purgeDir(File dir) { if (dir.exists()) { + int maxLogs = settings.getInt(MAX_LOGS_PROPERTY); + if (maxLogs < 0) { + throw new IllegalArgumentException(format("Property %s must be positive. Got: %d", MAX_LOGS_PROPERTY, maxLogs)); + } List logFiles = newArrayList(FileUtils.listFiles(dir, FileFilterUtils.fileFileFilter(), FileFilterUtils.falseFileFilter())); if (logFiles.size() > maxLogs) { Collections.sort(logFiles, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/log/CeLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/log/CeLoggingTest.java index 3ef849ba270..d2bcbfe2cd4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/log/CeLoggingTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/log/CeLoggingTest.java @@ -51,8 +51,7 @@ public class CeLoggingTest { @Test public void getFile() throws IOException { File dataDir = temp.newFolder(); - Settings settings = new Settings(); - settings.setProperty(ProcessProperties.PATH_DATA, dataDir.getAbsolutePath()); + Settings settings = newSettings(dataDir, 10); CeLogging underTest = new CeLogging(settings); LogFileRef ref = new LogFileRef("TYPE1", "TASK1", "COMPONENT1"); @@ -75,7 +74,7 @@ public class CeLoggingTest { @Test public void use_MDC_to_store_path_to_in_progress_task_logs() throws IOException { - CeLogging underTest = new CeLogging(temp.newFolder(), 5); + CeLogging underTest = new CeLogging(newSettings(temp.newFolder(), 5)); CeTask task = new CeTask.Builder().setType("TYPE1").setUuid("U1").build(); underTest.initForTask(task); @@ -97,7 +96,7 @@ public class CeLoggingTest { assertThat(dir.listFiles()).hasSize(5); // keep 3 files in each dir - CeLogging underTest = new CeLogging(dir, 3); + CeLogging underTest = new CeLogging(newSettings(dir, 3)); underTest.purgeDir(dir); assertThat(dir.listFiles()).hasSize(3); @@ -110,7 +109,7 @@ public class CeLoggingTest { File dir = temp.newFolder(); FileUtils.touch(new File(dir, "U1.log")); - CeLogging underTest = new CeLogging(dir, 5); + CeLogging underTest = new CeLogging(newSettings(dir, 5)); underTest.purgeDir(dir); assertThat(dir.listFiles()).extracting("name").containsOnly("U1.log"); @@ -121,7 +120,7 @@ public class CeLoggingTest { File dir = temp.newFolder(); FileUtils.touch(new File(dir, "U1.log")); - CeLogging underTest = new CeLogging(dir, 0); + CeLogging underTest = new CeLogging(newSettings(temp.newFolder(), 0)); underTest.purgeDir(dir); assertThat(dir.listFiles()).isEmpty(); @@ -132,10 +131,9 @@ public class CeLoggingTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Property sonar.ce.maxLogsPerTask must be positive. Got: -1"); - Settings settings = new Settings(); - settings.setProperty(ProcessProperties.PATH_DATA, temp.newFolder().getAbsolutePath()); - settings.setProperty(CeLogging.MAX_LOGS_PROPERTY, -1); - new CeLogging(settings); + Settings settings = newSettings(temp.newFolder(), -1); + CeLogging logging = new CeLogging(settings); + logging.purgeDir(temp.newFolder()); } @Test @@ -151,4 +149,10 @@ public class CeLoggingTest { assertThat(siftingAppender.getDiscriminator().getKey()).isEqualTo(CeLogging.MDC_LOG_PATH); } + private static Settings newSettings(File dataDir, int maxLogs) { + Settings settings = new Settings(); + settings.setProperty(ProcessProperties.PATH_DATA, dataDir.getAbsolutePath()); + settings.setProperty(CeLogging.MAX_LOGS_PROPERTY, maxLogs); + return settings; + } } -- 2.39.5