Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
Signed-off-by: zane-neo <zaniu@amazon.com>
  • Loading branch information
zane-neo committed Jul 11, 2024
1 parent e65c49a commit f775ee4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.opensearch.ml.breaker;

import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE;

import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedActionException;
Expand All @@ -15,8 +17,6 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.ml.common.exception.MLException;

import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE;

/**
* A circuit breaker for disk usage.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_JVM_HEAP_MEM_THRESHOLD;

import java.util.Optional;

import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings;
import org.opensearch.monitor.jvm.JvmService;

import java.util.Optional;

/**
* A circuit breaker for memory usage.
*/
Expand All @@ -32,17 +32,23 @@ public MemoryCircuitBreaker(short threshold, JvmService jvmService) {
}

public MemoryCircuitBreaker(Settings settings, ClusterService clusterService, JvmService jvmService) {
super(Optional.ofNullable(ML_COMMONS_JVM_HEAP_MEM_THRESHOLD.get(settings)).map(Integer::shortValue).orElse(DEFAULT_JVM_HEAP_USAGE_THRESHOLD));
super(
Optional
.ofNullable(ML_COMMONS_JVM_HEAP_MEM_THRESHOLD.get(settings))
.map(Integer::shortValue)
.orElse(DEFAULT_JVM_HEAP_USAGE_THRESHOLD)
);
this.jvmService = jvmService;
clusterService.getClusterSettings().addSettingsUpdateConsumer(ML_COMMONS_JVM_HEAP_MEM_THRESHOLD, it -> super.setThreshold(it.shortValue()));
clusterService
.getClusterSettings()
.addSettingsUpdateConsumer(ML_COMMONS_JVM_HEAP_MEM_THRESHOLD, it -> super.setThreshold(it.shortValue()));
}

@Override
public String getName() {
return ML_MEMORY_CB;
}


@Override
public boolean isOpen() {
return getThreshold() < 100 && jvmService.stats().getMem().getHeapUsedPercent() > getThreshold();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_NATIVE_MEM_THRESHOLD;

import java.util.Optional;

import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings;
import org.opensearch.monitor.os.OsService;

import java.util.Optional;

/**
* A circuit breaker for native memory usage.
*/
Expand All @@ -22,9 +22,16 @@ public class NativeMemoryCircuitBreaker extends ThresholdCircuitBreaker<Short> {
private final OsService osService;

public NativeMemoryCircuitBreaker(OsService osService, Settings settings, ClusterService clusterService) {
super(Optional.ofNullable(ML_COMMONS_NATIVE_MEM_THRESHOLD.get(settings)).map(Integer::shortValue).orElse(DEFAULT_NATIVE_MEM_USAGE_THRESHOLD));
super(
Optional
.ofNullable(ML_COMMONS_NATIVE_MEM_THRESHOLD.get(settings))
.map(Integer::shortValue)
.orElse(DEFAULT_NATIVE_MEM_USAGE_THRESHOLD)
);
this.osService = osService;
clusterService.getClusterSettings().addSettingsUpdateConsumer(ML_COMMONS_NATIVE_MEM_THRESHOLD, it -> super.setThreshold(it.shortValue()));
clusterService
.getClusterSettings()
.addSettingsUpdateConsumer(ML_COMMONS_NATIVE_MEM_THRESHOLD, it -> super.setThreshold(it.shortValue()));
}

public NativeMemoryCircuitBreaker(Integer threshold, OsService osService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ private MLCommonsSettings() {}
.intSetting("plugins.ml_commons.jvm_heap_memory_threshold", 85, 0, 100, Setting.Property.NodeScope, Setting.Property.Dynamic);

public static final Setting<Integer> ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE = Setting
.intSetting("plugins.ml_commons.disk_free_space_min_value", 5, 0, Integer.MAX_VALUE, Setting.Property.NodeScope, Setting.Property.Dynamic);
.intSetting(
"plugins.ml_commons.disk_free_space_min_value",
5,
0,
Integer.MAX_VALUE,
Setting.Property.NodeScope,
Setting.Property.Dynamic
);

public static final Setting<String> ML_COMMONS_EXCLUDE_NODE_NAMES = Setting
.simpleString("plugins.ml_commons.exclude_nodes._name", Setting.Property.NodeScope, Setting.Property.Dynamic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

package org.opensearch.ml.breaker;

import static org.mockito.Mockito.when;
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE;

import java.io.File;
import java.util.HashSet;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,13 +23,6 @@
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;

import java.io.File;
import java.util.HashSet;
import java.util.List;

import static org.mockito.Mockito.when;
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE;

public class DiskCircuitBreakerTests {
@Mock
ClusterService clusterService;
Expand All @@ -33,28 +33,40 @@ public class DiskCircuitBreakerTests {
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, new HashSet<>(List.of(
ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE))));
when(clusterService.getClusterSettings())
.thenReturn(new ClusterSettings(Settings.EMPTY, new HashSet<>(List.of(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE))));
}

@Test
public void test_isOpen_whenDiskFreeSpaceIsHigherThanMinValue_breakerIsNotOpen() {
CircuitBreaker breaker = new DiskCircuitBreaker(Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(), clusterService, file);
CircuitBreaker breaker = new DiskCircuitBreaker(
Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(),
clusterService,
file
);
when(file.getFreeSpace()).thenReturn(5 * 1024 * 1024 * 1024L);
Assert.assertFalse(breaker.isOpen());
}

@Test
public void test_isOpen_whenDiskFreeSpaceIsLessThanMinValue_breakerIsOpen() {
CircuitBreaker breaker = new DiskCircuitBreaker(Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(), clusterService, file);
CircuitBreaker breaker = new DiskCircuitBreaker(
Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(),
clusterService,
file
);
when(file.getFreeSpace()).thenReturn(4 * 1024 * 1024 * 1024L);
Assert.assertTrue(breaker.isOpen());
}

@Test
public void test_isOpen_whenDiskFreeSpaceConfiguredToZero_breakerIsNotOpen() {
CircuitBreaker breaker = new DiskCircuitBreaker(Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(), clusterService, file);
when(file.getFreeSpace()).thenReturn((long)(Math.random() * 1024 * 1024 * 1024 * 1024L));
CircuitBreaker breaker = new DiskCircuitBreaker(
Settings.builder().put(ML_COMMONS_DISK_FREE_SPACE_MIN_VALUE.getKey(), 5).build(),
clusterService,
file
);
when(file.getFreeSpace()).thenReturn((long) (Math.random() * 1024 * 1024 * 1024 * 1024L));
Assert.assertFalse(breaker.isOpen());
}

Expand Down

0 comments on commit f775ee4

Please sign in to comment.