Skip to content

Commit

Permalink
rename + comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Andyz26 committed Nov 16, 2023
1 parent 165a529 commit 94d2c5c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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

}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,11 +17,6 @@
package io.mantisrx.common.properties;

public interface MantisPropertiesLoader {

void initalize();

void shutdown();

String getStringValue(String name, String defaultVal);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
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) {
Expand All @@ -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);
}
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 94d2c5c

Please sign in to comment.