From 0121741e2a1da966f255e2752a90ff32434d9a26 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 19 Feb 2018 11:59:24 -0600 Subject: [PATCH] ARTEMIS-872 fix potential negative space calc --- .../activemq/artemis/core/config/impl/Validators.java | 10 ++++++++++ .../core/deployers/impl/FileConfigurationParser.java | 2 +- .../artemis/core/server/ActiveMQMessageBundle.java | 2 ++ .../artemis/core/server/files/FileStoreMonitor.java | 10 +++++++++- .../artemis/core/server/impl/ActiveMQServerImpl.java | 10 ++++++---- .../artemis/core/config/impl/ValidatorsTest.java | 10 ++++++++++ docs/user-manual/en/configuration-index.md | 2 +- 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/Validators.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/Validators.java index bef4d1ba72..2f28e49716 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/Validators.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/Validators.java @@ -76,6 +76,16 @@ public final class Validators { } }; + public static final Validator PERCENTAGE_OR_MINUS_ONE = new Validator() { + @Override + public void validate(final String name, final Object value) { + Number val = (Number) value; + if (val == null || ((val.intValue() < 0 || val.intValue() > 100) && val.intValue() != -1)) { + throw ActiveMQMessageBundle.BUNDLE.notPercentOrMinusOne(name, val); + } + } + }; + public static final Validator GE_ZERO = new Validator() { @Override public void validate(final String name, final Object value) { diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java index b5352ba2cd..e6440106c6 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java @@ -360,7 +360,7 @@ public final class FileConfigurationParser extends XMLConfigurationUtil { config.setGlobalMaxSize(globalMaxSize); } - config.setMaxDiskUsage(getInteger(e, MAX_DISK_USAGE, config.getMaxDiskUsage(), Validators.PERCENTAGE)); + config.setMaxDiskUsage(getInteger(e, MAX_DISK_USAGE, config.getMaxDiskUsage(), Validators.PERCENTAGE_OR_MINUS_ONE)); config.setDiskScanPeriod(getInteger(e, DISK_SCAN_PERIOD, config.getDiskScanPeriod(), Validators.MINUS_ONE_OR_GT_ZERO)); diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java index f502d00dda..d923ddcdee 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java @@ -436,4 +436,6 @@ public interface ActiveMQMessageBundle { @Message(id = 119213, value = "User: {0} does not have permission=''{1}'' for queue {2} on address {3}", format = Message.Format.MESSAGE_FORMAT) ActiveMQSecurityException userNoPermissionsQueue(String username, CheckType checkType, String squeue, String saddress); + @Message(id = 119214, value = "{0} must be a valid percentage value between 0 and 100 or -1 (actual value: {1})", format = Message.Format.MESSAGE_FORMAT) + IllegalArgumentException notPercentOrMinusOne(String name, Number val); } diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileStoreMonitor.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileStoreMonitor.java index 2c3d28c748..ad591172d4 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileStoreMonitor.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileStoreMonitor.java @@ -137,7 +137,15 @@ public class FileStoreMonitor extends ActiveMQScheduledComponent { } protected double calculateUsage(FileStore store) throws IOException { - return 1.0 - (double) store.getUsableSpace() / (double) store.getTotalSpace(); + return 1.0 - (double) store.getUsableSpace() / getTotalSpace(store); + } + + private double getTotalSpace(FileStore store) throws IOException { + double totalSpace = (double) store.getTotalSpace(); + if (totalSpace < 0) { + totalSpace = Long.MAX_VALUE; + } + return totalSpace; } public interface Callback { diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java index 2f6cb7f745..7352d3873a 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java @@ -2403,10 +2403,12 @@ public class ActiveMQServerImpl implements ActiveMQServer { postOffice.startExpiryScanner(); } - try { - injectMonitor(new FileStoreMonitor(getScheduledPool(), executorFactory.getExecutor(), configuration.getDiskScanPeriod(), TimeUnit.MILLISECONDS, configuration.getMaxDiskUsage() / 100f, shutdownOnCriticalIO)); - } catch (Exception e) { - ActiveMQServerLogger.LOGGER.unableToInjectMonitor(e); + if (configuration.getMaxDiskUsage() != -1) { + try { + injectMonitor(new FileStoreMonitor(getScheduledPool(), executorFactory.getExecutor(), configuration.getDiskScanPeriod(), TimeUnit.MILLISECONDS, configuration.getMaxDiskUsage() / 100f, shutdownOnCriticalIO)); + } catch (Exception e) { + ActiveMQServerLogger.LOGGER.unableToInjectMonitor(e); + } } } diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ValidatorsTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ValidatorsTest.java index 5d4c1e8298..354827a8b1 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ValidatorsTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ValidatorsTest.java @@ -110,6 +110,16 @@ public class ValidatorsTest extends Assert { ValidatorsTest.failure(Validators.PERCENTAGE, null); } + @Test + public void testPERCENTAGE_OR_MINUS_ONE() { + ValidatorsTest.success(Validators.PERCENTAGE_OR_MINUS_ONE, 99); + ValidatorsTest.success(Validators.PERCENTAGE_OR_MINUS_ONE, 100); + ValidatorsTest.success(Validators.PERCENTAGE_OR_MINUS_ONE, 0); + ValidatorsTest.success(Validators.PERCENTAGE_OR_MINUS_ONE, -1); + ValidatorsTest.failure(Validators.PERCENTAGE_OR_MINUS_ONE, 101); + ValidatorsTest.failure(Validators.PERCENTAGE_OR_MINUS_ONE, null); + } + // Package protected --------------------------------------------- // Protected ----------------------------------------------------- diff --git a/docs/user-manual/en/configuration-index.md b/docs/user-manual/en/configuration-index.md index 7b26737dab..f962d2194e 100644 --- a/docs/user-manual/en/configuration-index.md +++ b/docs/user-manual/en/configuration-index.md @@ -86,7 +86,7 @@ Name | Description [management-notification-address](management.md "Configuring The Core Management Notification Address") | the name of the address that consumers bind to receive management notifications. Default=activemq.notifications [mask-password](masking-passwords.md "Masking Passwords") | This option controls whether passwords in server configuration need be masked. If set to "true" the passwords are masked. Default=false [max-saved-replicated-journals-size](ha.md#data-replication) | This specifies how many times a replicated backup server can restart after moving its files on start. Once there are this number of backup journal files the server will stop permanently after if fails back. -1 Means no Limit, 0 don't keep a copy at all, Default=2 -[max-disk-usage](paging.md#max-disk-usage) | The max percentage of data we should use from disks. The System will block while the disk is full. Default=100 +[max-disk-usage](paging.md#max-disk-usage) | The max percentage of data we should use from disks. The System will block while the disk is full. Disable by setting -1. Default=100 [memory-measure-interval](perf-tuning.md) | frequency to sample JVM memory in ms (or -1 to disable memory sampling). Default=-1 [memory-warning-threshold](perf-tuning.md) | Percentage of available memory which will trigger a warning log. Default=25 [message-counter-enabled](management.md "Configuring Message Counters") | true means that message counters are enabled. Default=false