diff --git a/mantis-common/src/main/java/io/mantisrx/common/properties/DefaultMantisPropertiesLoader.java b/mantis-common/src/main/java/io/mantisrx/common/properties/DefaultMantisPropertiesLoader.java index 57aff03e5..465de0c5d 100644 --- a/mantis-common/src/main/java/io/mantisrx/common/properties/DefaultMantisPropertiesLoader.java +++ b/mantis-common/src/main/java/io/mantisrx/common/properties/DefaultMantisPropertiesLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Netflix, Inc. + * Copyright 2023 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,16 +42,4 @@ public String getStringValue(String name, String defaultVal) { } return defaultVal; } - - @Override - public void initalize() { - // TODO Auto-generated method stub - - } - - @Override - public void shutdown() { - // TODO Auto-generated method stub - - } } diff --git a/mantis-common/src/main/java/io/mantisrx/common/properties/MantisPropertiesLoader.java b/mantis-common/src/main/java/io/mantisrx/common/properties/MantisPropertiesLoader.java index cd75bc1d1..8da5964c7 100644 --- a/mantis-common/src/main/java/io/mantisrx/common/properties/MantisPropertiesLoader.java +++ b/mantis-common/src/main/java/io/mantisrx/common/properties/MantisPropertiesLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Netflix, Inc. + * Copyright 2023 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +17,6 @@ package io.mantisrx.common.properties; public interface MantisPropertiesLoader { - - void initalize(); - - void shutdown(); - String getStringValue(String name, String defaultVal); } diff --git a/mantis-common/src/main/java/io/mantisrx/common/properties/DynamicProperty.java b/mantis-common/src/main/java/io/mantisrx/config/dynamic/DynamicProperty.java similarity index 63% rename from mantis-common/src/main/java/io/mantisrx/common/properties/DynamicProperty.java rename to mantis-common/src/main/java/io/mantisrx/config/dynamic/DynamicProperty.java index 3956067b5..281cef229 100644 --- a/mantis-common/src/main/java/io/mantisrx/common/properties/DynamicProperty.java +++ b/mantis-common/src/main/java/io/mantisrx/config/dynamic/DynamicProperty.java @@ -14,19 +14,24 @@ * limitations under the License. */ -package io.mantisrx.common.properties; +package io.mantisrx.config.dynamic; +import io.mantisrx.common.properties.MantisPropertiesLoader; import java.time.Clock; +import java.time.Duration; import java.time.Instant; +import java.util.Objects; +import lombok.extern.slf4j.Slf4j; +@Slf4j public abstract class DynamicProperty { - public static final String DYNAMICPROPERTY_REFRESH_SECONDS_KEY = "mantis.dynamicproperty.refreshSecs"; + public static final String DYNAMIC_PROPERTY_REFRESH_SECONDS_KEY = "mantis.config.dynamic.refreshSecs"; protected final MantisPropertiesLoader propertiesLoader; protected final String propertyName; protected final T defaultValue; protected T lastValue; protected Instant lastRefreshTime; - private final long refreshDuration; + private final Duration refreshDuration; private final Clock clock; public DynamicProperty(MantisPropertiesLoader propertiesLoader, String propertyName, T defaultValue, Clock clock) { @@ -39,10 +44,9 @@ public DynamicProperty(MantisPropertiesLoader propertiesLoader, String propertyN try { - this.refreshDuration = Long.parseLong( - propertiesLoader.getStringValue(DYNAMICPROPERTY_REFRESH_SECONDS_KEY, "30")); - } - catch (NumberFormatException ex) { + this.refreshDuration = Duration.ofSeconds(Long.parseLong( + propertiesLoader.getStringValue(DYNAMIC_PROPERTY_REFRESH_SECONDS_KEY, "30"))); + } catch (NumberFormatException ex) { throw new RuntimeException("invalid refresh secs for dynamic property: " + propertyName); } } @@ -56,9 +60,22 @@ protected String getStringValue() { return this.propertiesLoader.getStringValue(this.propertyName, this.lastValue.toString()); } - protected boolean shouldRefresh() { - return this.clock.instant().isAfter(this.lastRefreshTime.plusSeconds(this.refreshDuration)); + private boolean shouldRefresh() { + return this.clock.instant().isAfter(this.lastRefreshTime.plus(this.refreshDuration)); } - public abstract T getValue(); + protected abstract T convertFromString(String newStrVal); + + public T getValue() { + if (shouldRefresh()) { + String newStrVal = this.getStringValue(); + T newVal = convertFromString(newStrVal); + if (!Objects.equals(this.lastValue, newVal)) { + log.info("[DP: {}] value changed from {} to {}", this.propertyName, this.lastValue, newVal); + } + this.lastValue = newVal; + } + + return this.lastValue; + } } diff --git a/mantis-common/src/main/java/io/mantisrx/common/properties/LongDynamicProperty.java b/mantis-common/src/main/java/io/mantisrx/config/dynamic/LongDynamicProperty.java similarity index 65% rename from mantis-common/src/main/java/io/mantisrx/common/properties/LongDynamicProperty.java rename to mantis-common/src/main/java/io/mantisrx/config/dynamic/LongDynamicProperty.java index 84d349bbf..ef4b1bb5c 100644 --- a/mantis-common/src/main/java/io/mantisrx/common/properties/LongDynamicProperty.java +++ b/mantis-common/src/main/java/io/mantisrx/config/dynamic/LongDynamicProperty.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package io.mantisrx.common.properties; +package io.mantisrx.config.dynamic; +import io.mantisrx.common.properties.MantisPropertiesLoader; import java.time.Clock; import lombok.extern.slf4j.Slf4j; @@ -29,21 +30,13 @@ public LongDynamicProperty(MantisPropertiesLoader propertiesLoader, String prope Clock clock) { super(propertiesLoader, propertyName, defaultValue, clock); } + @Override - public Long getValue() { - if (shouldRefresh()) { - String strV = this.getStringValue(); - try { - long newVal = Long.parseLong(strV); - if (this.lastValue != newVal) { - log.info("[DP: {}] value changed from {} to {}", this.propertyName, this.lastValue, newVal); - } - this.lastValue = newVal; - } catch (NumberFormatException e) { - throw new RuntimeException("invalid long value: " + strV); - } + protected Long convertFromString(String newStrVal) { + try { + return Long.parseLong(newStrVal); + } catch (NumberFormatException e) { + throw new RuntimeException("invalid long value: " + newStrVal); } - - return this.lastValue; } } diff --git a/mantis-common/src/main/java/io/mantisrx/common/properties/StringDynamicProperty.java b/mantis-common/src/main/java/io/mantisrx/config/dynamic/StringDynamicProperty.java similarity index 66% rename from mantis-common/src/main/java/io/mantisrx/common/properties/StringDynamicProperty.java rename to mantis-common/src/main/java/io/mantisrx/config/dynamic/StringDynamicProperty.java index 86444b241..e8b5c6e9c 100644 --- a/mantis-common/src/main/java/io/mantisrx/common/properties/StringDynamicProperty.java +++ b/mantis-common/src/main/java/io/mantisrx/config/dynamic/StringDynamicProperty.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package io.mantisrx.common.properties; +package io.mantisrx.config.dynamic; +import io.mantisrx.common.properties.MantisPropertiesLoader; import java.time.Clock; import lombok.extern.slf4j.Slf4j; @@ -26,20 +27,14 @@ public StringDynamicProperty(MantisPropertiesLoader propertiesLoader, String pro super(propertiesLoader, propertyName, defaultValue); } - public StringDynamicProperty(MantisPropertiesLoader propertiesLoader, String propertyName, String defaultValue, + public StringDynamicProperty( + MantisPropertiesLoader propertiesLoader, String propertyName, String defaultValue, Clock clock) { super(propertiesLoader, propertyName, defaultValue, clock); } - @Override - public String getValue() { - if (shouldRefresh()) { - String newVal = this.getStringValue(); - if (this.lastValue != newVal) { - log.info("[DP: {}] value changed from {} to {}", this.propertyName, this.lastValue, newVal); - } - this.lastValue = newVal; - } - return this.lastValue; + @Override + protected String convertFromString(String newStrVal) { + return newStrVal; } } diff --git a/mantis-common/src/test/java/io/mantisrx/common/properties/DynamicPropertyTest.java b/mantis-common/src/test/java/io/mantisrx/common/properties/DynamicPropertyTest.java index 58ff9cd92..825f68566 100644 --- a/mantis-common/src/test/java/io/mantisrx/common/properties/DynamicPropertyTest.java +++ b/mantis-common/src/test/java/io/mantisrx/common/properties/DynamicPropertyTest.java @@ -22,6 +22,9 @@ import static org.mockito.Mockito.when; import io.mantisrx.common.util.DelegateClock; +import io.mantisrx.config.dynamic.DynamicProperty; +import io.mantisrx.config.dynamic.LongDynamicProperty; +import io.mantisrx.config.dynamic.StringDynamicProperty; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -38,7 +41,7 @@ public void testStringDynamicProperty() throws Exception { final String defaultVal1 = "defaultVal1"; final String val2 = "val2"; final String val3 = "val3"; - when(mockloader.getStringValue(eq(DynamicProperty.DYNAMICPROPERTY_REFRESH_SECONDS_KEY), ArgumentMatchers.any())) + when(mockloader.getStringValue(eq(DynamicProperty.DYNAMIC_PROPERTY_REFRESH_SECONDS_KEY), ArgumentMatchers.any())) .thenReturn("10"); when(mockloader.getStringValue(key1, defaultVal1)).thenReturn(defaultVal1); @@ -71,7 +74,7 @@ public void testAckInstance() throws Exception { final Long defaultVal1 = 1L; final Long val2 = 2L; final Long val3 = 3L; - when(mockloader.getStringValue(eq(DynamicProperty.DYNAMICPROPERTY_REFRESH_SECONDS_KEY), ArgumentMatchers.any())) + when(mockloader.getStringValue(eq(DynamicProperty.DYNAMIC_PROPERTY_REFRESH_SECONDS_KEY), ArgumentMatchers.any())) .thenReturn("10"); when(mockloader.getStringValue(key1, defaultVal1.toString())).thenReturn(defaultVal1.toString()); diff --git a/mantis-control-plane/mantis-control-plane-core/src/main/java/io/mantisrx/server/core/utils/ConfigUtils.java b/mantis-control-plane/mantis-control-plane-core/src/main/java/io/mantisrx/server/core/utils/ConfigUtils.java index e11536451..dd541a8cc 100644 --- a/mantis-control-plane/mantis-control-plane-core/src/main/java/io/mantisrx/server/core/utils/ConfigUtils.java +++ b/mantis-control-plane/mantis-control-plane-core/src/main/java/io/mantisrx/server/core/utils/ConfigUtils.java @@ -16,9 +16,9 @@ package io.mantisrx.server.core.utils; -import io.mantisrx.common.properties.LongDynamicProperty; import io.mantisrx.common.properties.MantisPropertiesLoader; -import io.mantisrx.common.properties.StringDynamicProperty; +import io.mantisrx.config.dynamic.LongDynamicProperty; +import io.mantisrx.config.dynamic.StringDynamicProperty; import io.mantisrx.server.core.CoreConfiguration; import java.lang.reflect.Method; import org.skife.config.Config; diff --git a/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/ResourceManagerGatewayCxn.java b/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/ResourceManagerGatewayCxn.java index 5ab3ade13..31d329edd 100644 --- a/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/ResourceManagerGatewayCxn.java +++ b/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/ResourceManagerGatewayCxn.java @@ -25,7 +25,7 @@ import io.mantisrx.common.metrics.Counter; import io.mantisrx.common.metrics.Metrics; import io.mantisrx.common.metrics.spectator.MetricGroupId; -import io.mantisrx.common.properties.LongDynamicProperty; +import io.mantisrx.config.dynamic.LongDynamicProperty; import io.mantisrx.server.agent.utils.DurableBooleanState; import io.mantisrx.server.agent.utils.ExponentialBackoffAbstractScheduledService; import io.mantisrx.server.master.resourcecluster.ResourceClusterGateway; diff --git a/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/TaskExecutor.java b/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/TaskExecutor.java index d1ecd6bb9..be3ec4cf2 100644 --- a/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/TaskExecutor.java +++ b/mantis-server/mantis-server-agent/src/main/java/io/mantisrx/server/agent/TaskExecutor.java @@ -23,8 +23,8 @@ import io.mantisrx.common.WorkerPorts; import io.mantisrx.common.metrics.netty.MantisNettyEventsListenerFactory; import io.mantisrx.common.properties.DefaultMantisPropertiesLoader; -import io.mantisrx.common.properties.LongDynamicProperty; import io.mantisrx.common.properties.MantisPropertiesLoader; +import io.mantisrx.config.dynamic.LongDynamicProperty; import io.mantisrx.runtime.MachineDefinition; import io.mantisrx.runtime.MantisJobState; import io.mantisrx.runtime.loader.ClassLoaderHandle; diff --git a/mantis-server/mantis-server-agent/src/test/java/io/mantisrx/server/agent/ResourceManagerGatewayCxnTest.java b/mantis-server/mantis-server-agent/src/test/java/io/mantisrx/server/agent/ResourceManagerGatewayCxnTest.java index 5568c9e96..f4f6b0a84 100644 --- a/mantis-server/mantis-server-agent/src/test/java/io/mantisrx/server/agent/ResourceManagerGatewayCxnTest.java +++ b/mantis-server/mantis-server-agent/src/test/java/io/mantisrx/server/agent/ResourceManagerGatewayCxnTest.java @@ -28,8 +28,8 @@ import com.spotify.futures.CompletableFutures; import io.mantisrx.common.WorkerPorts; import io.mantisrx.common.properties.DefaultMantisPropertiesLoader; -import io.mantisrx.common.properties.LongDynamicProperty; import io.mantisrx.common.properties.MantisPropertiesLoader; +import io.mantisrx.config.dynamic.LongDynamicProperty; import io.mantisrx.runtime.MachineDefinition; import io.mantisrx.server.agent.utils.DurableBooleanState; import io.mantisrx.server.master.resourcecluster.ClusterID;