From c911f2030f35e319ca680ba66f043c4c1ad1de3f Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 18 Sep 2018 22:46:35 -0700 Subject: [PATCH] Add Spring Boot HealthIndicator for Apache Geode DiskStores. --- .../GeodeDiskStoresHealthIndicator.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeDiskStoresHealthIndicator.java diff --git a/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeDiskStoresHealthIndicator.java b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeDiskStoresHealthIndicator.java new file mode 100644 index 00000000..70c1571b --- /dev/null +++ b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeDiskStoresHealthIndicator.java @@ -0,0 +1,139 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.geode.boot.actuate; + +import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import org.apache.geode.cache.DiskStore; +import org.apache.shiro.util.Assert; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.context.ApplicationContext; +import org.springframework.geode.boot.actuate.health.AbstractGeodeHealthIndicator; + +/** + * The {@link GeodeDiskStoresHealthIndicator} class is a Spring Boot {@link HealthIndicator} providing details about + * the health of Apache Geode {@link DiskStore DiskStores}. + * + * @author John Blum + * @see org.apache.geode.cache.DiskStore + * @see org.springframework.boot.actuate.health.Health + * @see org.springframework.boot.actuate.health.HealthIndicator + * @see org.springframework.context.ApplicationContext + * @see org.springframework.geode.boot.actuate.health.AbstractGeodeHealthIndicator + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public class GeodeDiskStoresHealthIndicator extends AbstractGeodeHealthIndicator { + + private final ApplicationContext applicationContext; + + /** + * Default constructor to construct an uninitialized instance of {@link GeodeDiskStoresHealthIndicator}, + * which will not provide any health information. + */ + public GeodeDiskStoresHealthIndicator() { + super("Disk Stores health check failed"); + this.applicationContext = null; + } + + /** + * Constructs an instance of the {@link GeodeDiskStoresHealthIndicator} initialized with a reference to + * the {@link ApplicationContext} instance. + * + * @param applicationContext reference to the Spring {@link ApplicationContext}. + * @throws IllegalArgumentException if {@link ApplicationContext} is {@literal null}. + * @see org.springframework.context.ApplicationContext + */ + public GeodeDiskStoresHealthIndicator(ApplicationContext applicationContext) { + + super("Disk Stores health check enabled"); + + Assert.notNull(applicationContext, "ApplicationContext is required"); + + this.applicationContext = applicationContext; + } + + /** + * Returns an {@link Optional} reference to the Spring {@link ApplicationContext}. + * + * @return an {@link Optional} reference to the Spring {@link ApplicationContext}. + * @see org.springframework.context.ApplicationContext + * @see java.util.Optional + */ + protected Optional getApplicationContext() { + return Optional.ofNullable(this.applicationContext); + } + + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + + if (getApplicationContext().isPresent()) { + + Map diskStores = getApplicationContext() + .map(it -> it.getBeansOfType(DiskStore.class)) + .orElseGet(Collections::emptyMap); + + builder.withDetail("geode.disk-store.count", diskStores.size()); + + diskStores.values().forEach(diskStore -> { + + String diskStoreName = diskStore.getName(); + + builder.withDetail(diskStoreKey(diskStoreName, "allow-force-compaction"), diskStore.getAllowForceCompaction()) + .withDetail(diskStoreKey(diskStoreName, "auto-compact"), diskStore.getAutoCompact()) + .withDetail(diskStoreKey(diskStoreName, "compaction-threshold"), diskStore.getCompactionThreshold()) + .withDetail(diskStoreKey(diskStoreName, "disk-directories"), toFilePathnamesString(diskStore.getDiskDirs())) + .withDetail(diskStoreKey(diskStoreName, "disk-directory-sizes"), Arrays.toString(diskStore.getDiskDirSizes())) + .withDetail(diskStoreKey(diskStoreName, "disk-usage-critical-percentage"), diskStore.getDiskUsageCriticalPercentage()) + .withDetail(diskStoreKey(diskStoreName, "disk-usage-warning-percentage"), diskStore.getDiskUsageWarningPercentage()) + .withDetail(diskStoreKey(diskStoreName, "max-oplog-size"), diskStore.getMaxOplogSize()) + .withDetail(diskStoreKey(diskStoreName, "queue-size"), diskStore.getQueueSize()) + .withDetail(diskStoreKey(diskStoreName, "time-interval"), diskStore.getTimeInterval()) + .withDetail(diskStoreKey(diskStoreName, "uuid"), diskStore.getDiskStoreUUID().toString()) + .withDetail(diskStoreKey(diskStoreName, "write-buffer-size"), diskStore.getWriteBufferSize()); + }); + + builder.up(); + + return; + } + + builder.unknown(); + } + + private String toFilePathnamesString(File... files) { + + return Arrays.toString(Arrays.stream(nullSafeArray(files, File.class)) + .filter(Objects::nonNull) + .map(File::getAbsolutePath) + .collect(Collectors.toSet()) + .toArray()); + } + + private String diskStoreKey(String diskStoreName, String suffix) { + return String.format("geode.disk-store.%1$s.%2$s", diskStoreName, suffix); + } +}