-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from ajoymajumdar/develop
Added health indicator for physical memory used on the instance.
- Loading branch information
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
genie-web/src/main/java/com/netflix/genie/web/health/MemoryHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.genie.web.health; | ||
|
||
import com.sun.management.OperatingSystemMXBean; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Health indicator for system memory usage. Also mark the service out-of-service if it crosses the threshold. | ||
* @author amajumdar | ||
* @since 3.0.0 | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class MemoryHealthIndicator implements HealthIndicator { | ||
private static final String FREE_PHYSICAL_MEMORY_SIZE = "freePhysicalMemorySize"; | ||
private static final String TOTAL_PHYSICAL_MEMORY_SIZE = "totalPhysicalMemorySize"; | ||
|
||
private final double maxUsedPhysicalMemoryPercentage; | ||
private final OperatingSystemMXBean operatingSystemMXBean; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param maxUsedPhysicalMemoryPercentage The maximum physical memory threshold | ||
* @param operatingSystemMXBean MX bean for operating system | ||
*/ | ||
@Autowired | ||
public MemoryHealthIndicator( | ||
@Value("${genie.health.memory.usedPhysicalMemoryPercent:90}") | ||
final double maxUsedPhysicalMemoryPercentage, | ||
final OperatingSystemMXBean operatingSystemMXBean | ||
) { | ||
this.maxUsedPhysicalMemoryPercentage = maxUsedPhysicalMemoryPercentage; | ||
this.operatingSystemMXBean = operatingSystemMXBean; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
final double freePhysicalMemorySize = (double) operatingSystemMXBean.getFreePhysicalMemorySize(); | ||
final double totalPhysicalMemorySize = (double) operatingSystemMXBean.getTotalPhysicalMemorySize(); | ||
final double usedPhysicalMemoryPercentage = ((totalPhysicalMemorySize - freePhysicalMemorySize) | ||
/ totalPhysicalMemorySize) * 100; | ||
|
||
if (usedPhysicalMemoryPercentage > maxUsedPhysicalMemoryPercentage) { | ||
log.warn("Physical memory {} crossed the threshold of {}", usedPhysicalMemoryPercentage, | ||
maxUsedPhysicalMemoryPercentage); | ||
return Health | ||
.outOfService() | ||
.withDetail(FREE_PHYSICAL_MEMORY_SIZE, freePhysicalMemorySize) | ||
.withDetail(TOTAL_PHYSICAL_MEMORY_SIZE, totalPhysicalMemorySize) | ||
.build(); | ||
} else { | ||
return Health | ||
.up() | ||
.withDetail(FREE_PHYSICAL_MEMORY_SIZE, freePhysicalMemorySize) | ||
.withDetail(TOTAL_PHYSICAL_MEMORY_SIZE, totalPhysicalMemorySize) | ||
.build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
genie-web/src/test/groovy/com/netflix/genie/web/health/MemoryHealthIndicatorSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.genie.web.health | ||
|
||
import com.sun.management.OperatingSystemMXBean | ||
import org.springframework.boot.actuate.health.Status | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
/** | ||
* Unit tests for MemoryHealthIndicator. | ||
* | ||
* @author amajumdar | ||
* @since 3.0.0 | ||
*/ | ||
@Unroll | ||
class MemoryHealthIndicatorSpec extends Specification{ | ||
OperatingSystemMXBean operatingSystemMXBean | ||
MemoryHealthIndicator memoryHealthIndicator; | ||
|
||
def setup(){ | ||
operatingSystemMXBean = Mock(OperatingSystemMXBean) | ||
memoryHealthIndicator = new MemoryHealthIndicator(80, operatingSystemMXBean) | ||
} | ||
|
||
def 'Health should be #status when free memory is #freeMemory and totla is #totalMemory'(){ | ||
given: | ||
operatingSystemMXBean.getFreePhysicalMemorySize() >> freeMemory | ||
operatingSystemMXBean.getTotalPhysicalMemorySize() >> totalMemory | ||
expect: | ||
memoryHealthIndicator.health().getStatus() == status | ||
where: | ||
freeMemory | totalMemory | status | ||
10.0 | 100.0 | Status.OUT_OF_SERVICE | ||
20.0 | 100.0 | Status.UP | ||
30.0 | 100.0 | Status.UP | ||
30.0 | 50.0 | Status.UP | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
## | ||
|
||
genie: | ||
health: | ||
memory: | ||
usedPhysicalMemoryPercent: 100 | ||
jobs: | ||
dir: | ||
location: "file:///tmp/" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
## | ||
|
||
genie: | ||
health: | ||
memory: | ||
usedPhysicalMemoryPercent: 100 | ||
jobs: | ||
dir: | ||
location: "file:///tmp/" | ||
|