Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculation of available job capacity returned in HealthIndicator #503

Merged
merged 1 commit into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ public Health health() {
.withDetail(NUMBER_RUNNING_JOBS_KEY, this.jobMetricsService.getNumActiveJobs())
.withDetail(USED_MEMORY_KEY, usedMemory)
.withDetail(AVAILABLE_MEMORY, availableMemory)
.withDetail(AVAILABLE_DEFAULT_JOB_CAPACITY, availableMemory % defaultJobMemory)
.withDetail(AVAILABLE_MAX_JOB_CAPACITY, availableMemory % maxJobMemory)
.withDetail(
AVAILABLE_DEFAULT_JOB_CAPACITY,
(availableMemory >= 0 && defaultJobMemory > 0) ? (availableMemory / defaultJobMemory) : 0)
.withDetail(
AVAILABLE_MAX_JOB_CAPACITY,
(availableMemory >= 0 && maxJobMemory > 0) ? (availableMemory / maxJobMemory) : 0)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.mockito.Mockito;
import org.springframework.boot.actuate.health.Status;

import java.util.Map;

/**
* Unit tests for GenieHealthIndicator.
*
Expand All @@ -43,14 +45,15 @@ public class GenieMemoryHealthIndicatorUnitTests {

private GenieMemoryHealthIndicator genieMemoryHealthIndicator;
private JobMetricsService jobMetricsService;
private JobsProperties jobsProperties;

/**
* Setup for the tests.
*/
@Before
public void setup() {
this.jobMetricsService = Mockito.mock(JobMetricsService.class);
final JobsProperties jobsProperties = new JobsProperties();
jobsProperties = new JobsProperties();
jobsProperties.getMemory().setDefaultJobMemory(DEFAULT_JOB_MEMORY);
jobsProperties.getMemory().setMaxSystemMemory(MAX_SYSTEM_MEMORY);
jobsProperties.getMemory().setMaxJobMemory(MAX_JOB_MEMORY);
Expand All @@ -70,4 +73,28 @@ public void canGetHealth() {
Assert.assertThat(this.genieMemoryHealthIndicator.health().getStatus(), Matchers.is(Status.UP));
Assert.assertThat(this.genieMemoryHealthIndicator.health().getStatus(), Matchers.is(Status.OUT_OF_SERVICE));
}

/**
* Test to make sure invalid property values for job memory default/max don't result in exceptions.
*/
@Test
public void canGetHealthWithInvalidJobSettings() {
jobsProperties.getMemory().setDefaultJobMemory(0);
jobsProperties.getMemory().setMaxJobMemory(0);
final Map<String, Object> healthDetails = genieMemoryHealthIndicator.health().getDetails();
Assert.assertEquals(0, healthDetails.get("availableDefaultJobCapacity"));
Assert.assertEquals(0, healthDetails.get("availableMaxJobCapacity"));
}

/**
* Test to make at full capacity we don't show a negative capacity.
*/
@Test
public void nonNegativeJobsCapacity() {
Mockito.when(this.jobMetricsService.getUsedMemory()).thenReturn(MAX_SYSTEM_MEMORY + 100);
final Map<String, Object> healthDetails = genieMemoryHealthIndicator.health().getDetails();
Assert.assertEquals(0, healthDetails.get("availableDefaultJobCapacity"));
Assert.assertEquals(0, healthDetails.get("availableMaxJobCapacity"));
}

}