-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis Health Indicator for Spring Actuator
- Loading branch information
Showing
5 changed files
with
179 additions
and
2 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
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
59 changes: 59 additions & 0 deletions
59
redis-lock/src/main/java/com/netflix/conductor/redislock/config/RedisHealthIndicator.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,59 @@ | ||
/* | ||
* Copyright 2025 Conductor Authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.conductor.redislock.config; | ||
|
||
import org.redisson.api.RedissonClient; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.redisson.api.redisnode.RedisNodes.*; | ||
|
||
@Component | ||
@ConditionalOnProperty(name = "management.health.redis.enabled", havingValue = "true") | ||
public class RedisHealthIndicator implements HealthIndicator { | ||
private final RedissonClient redisClient; | ||
private final RedisLockProperties redisProperties; | ||
|
||
public RedisHealthIndicator(RedissonClient redisClient, RedisLockProperties redisProperties) { | ||
this.redisClient = redisClient; | ||
this.redisProperties = redisProperties; | ||
} | ||
|
||
@Override | ||
public Health health() { | ||
return isHealth() ? Health.up().build() : Health.down().build(); | ||
} | ||
|
||
private boolean isHealth() { | ||
switch (redisProperties.getServerType()) { | ||
case SINGLE -> { | ||
return redisClient.getRedisNodes(SINGLE).pingAll(5, SECONDS); | ||
} | ||
|
||
case CLUSTER -> { | ||
return redisClient.getRedisNodes(CLUSTER).pingAll(5, SECONDS); | ||
} | ||
|
||
case SENTINEL -> { | ||
return redisClient.getRedisNodes(SENTINEL_MASTER_SLAVE).pingAll(5, SECONDS); | ||
} | ||
|
||
default -> { | ||
return false; | ||
} | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...s-lock/src/test/java/com/netflix/conductor/redislock/config/RedisHealthIndicatorTest.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,111 @@ | ||
/* | ||
* Copyright 2025 Conductor Authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.conductor.redislock.config; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.redisnode.RedissonClusterNodes; | ||
import org.redisson.redisnode.RedissonSentinelMasterSlaveNodes; | ||
import org.redisson.redisnode.RedissonSingleNode; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static com.netflix.conductor.redislock.config.RedisLockProperties.REDIS_SERVER_TYPE.*; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class RedisHealthIndicatorTest { | ||
|
||
@Mock private RedissonClient redissonClient; | ||
|
||
@Test | ||
public void shouldReturnAsHealthWhenServerTypeIsSingle() { | ||
// Given a Redisson client | ||
var redisProperties = new RedisLockProperties(); | ||
redisProperties.setServerType(SINGLE); | ||
|
||
// And its mocks | ||
var redisNode = Mockito.mock(RedissonSingleNode.class); | ||
when(redissonClient.getRedisNodes(any())).thenReturn(redisNode); | ||
when(redisNode.pingAll(anyLong(), any(TimeUnit.class))).thenReturn(true); | ||
|
||
// When execute a health indicator | ||
var actualHealth = new RedisHealthIndicator(redissonClient, redisProperties); | ||
|
||
// Then should return as health | ||
assertThat(actualHealth.health()).isEqualTo(Health.up().build()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAsHealthWhenServerTypeIsCluster() { | ||
// Given a Redisson client | ||
var redisProperties = new RedisLockProperties(); | ||
redisProperties.setServerType(CLUSTER); | ||
|
||
// And its mocks | ||
var redisNode = Mockito.mock(RedissonClusterNodes.class); | ||
when(redissonClient.getRedisNodes(any())).thenReturn(redisNode); | ||
when(redisNode.pingAll(anyLong(), any(TimeUnit.class))).thenReturn(true); | ||
|
||
// When execute a health indicator | ||
var actualHealth = new RedisHealthIndicator(redissonClient, redisProperties); | ||
|
||
// Then should return as health | ||
assertThat(actualHealth.health()).isEqualTo(Health.up().build()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAsHealthWhenServerTypeIsSentinel() { | ||
// Given a Redisson client | ||
var redisProperties = new RedisLockProperties(); | ||
redisProperties.setServerType(SENTINEL); | ||
|
||
// And its mocks | ||
var redisNode = Mockito.mock(RedissonSentinelMasterSlaveNodes.class); | ||
when(redissonClient.getRedisNodes(any())).thenReturn(redisNode); | ||
when(redisNode.pingAll(anyLong(), any(TimeUnit.class))).thenReturn(true); | ||
|
||
// When execute a health indicator | ||
var actualHealth = new RedisHealthIndicator(redissonClient, redisProperties); | ||
|
||
// Then should return as health | ||
assertThat(actualHealth.health()).isEqualTo(Health.up().build()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAsUnhealthyWhenAnyServerIsDown() { | ||
// Given a Redisson client | ||
var redisProperties = new RedisLockProperties(); | ||
redisProperties.setServerType(SINGLE); | ||
|
||
// And its mocks | ||
var redisNode = Mockito.mock(RedissonSingleNode.class); | ||
when(redissonClient.getRedisNodes(any())).thenReturn(redisNode); | ||
when(redisNode.pingAll(anyLong(), any(TimeUnit.class))).thenReturn(false); | ||
|
||
// When execute a health indicator | ||
var actualHealth = new RedisHealthIndicator(redissonClient, redisProperties); | ||
|
||
// Then should return as unhealthy | ||
assertThat(actualHealth.health()).isEqualTo(Health.down().build()); | ||
} | ||
} |