Commit fcdae6e9 authored by Mingyang Zhu's avatar Mingyang Zhu
Browse files

deprecate the old health check API code and integrate with spring boot...

deprecate the old health check API code and integrate with spring boot actuator health endpoint. Implement a customized health indicator in azure controlled by a parameter.
parent 7723dfb7
......@@ -48,7 +48,7 @@ spec:
cpu: "300m"
readinessProbe:
httpGet:
path: /api/partition/v1/_ah/readiness_check
path: /api/partition/v1/actuator/health
port: 80
volumeMounts:
- name: azure-keyvault
......
......@@ -74,7 +74,7 @@ spec:
- containerPort: 80
readinessProbe:
httpGet:
path: /api/partition/v1/_ah/readiness_check
path: /api/partition/v1/actuator/health
port: 80
volumeMounts:
- name: azure-keyvault
......
......@@ -16,7 +16,7 @@ tags:
- name: health-check
description: Health Check
paths:
/_ah/liveness_check:
/actuator/health:
get:
tags:
- health-check
......@@ -40,7 +40,7 @@ paths:
security:
- JWT:
- global
/_ah/readiness_check:
/actuator/health:
get:
tags:
- health-check
......
......@@ -18,13 +18,13 @@ Partition service is responsible for creating and retrieving the partition speci
## Health Check <a name="checking-service-health"></a>
An endpoint to check if service is up and running.
```
GET api/partition/v1/_ah/liveness_check
GET api/partition/v1/actuator/health
```
<details><summary>curl</summary>
```
curl --request GET \
--url 'https://<base_url>/api/partition/v1/_ah/liveness_check'
--url 'https://<base_url>/api/partition/v1/actuator/health'
```
</details>
......
......@@ -56,6 +56,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
......
// Copyright 2017-2020, Schlumberger
//
// 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.opengroup.osdu.partition.api;
import org.opengroup.osdu.partition.logging.AuditLogger;
import org.opengroup.osdu.partition.provider.interfaces.IHealthCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
@RestController
@RequestMapping(path = "/_ah", produces = "application/json")
public class HealthCheck {
@Autowired
private AuditLogger auditLogger;
@Autowired
private IHealthCheckService healthCheckService;
@GetMapping("/liveness_check")
public ResponseEntity<String> livenessCheck() {
healthCheckService.performLivenessCheck();
ResponseEntity responseEntity = new ResponseEntity<>("Partition service is alive", HttpStatus.OK);
this.auditLogger.readServiceLivenessSuccess(Collections.singletonList(responseEntity.toString()));
return responseEntity;
}
@GetMapping("/readiness_check")
public ResponseEntity<String> readinessCheck() {
healthCheckService.performReadinessCheck();
return new ResponseEntity<>("Partition service is ready", HttpStatus.OK);
}
}
package org.opengroup.osdu.partition.provider.interfaces;
public interface IHealthCheckService {
void performLivenessCheck();
void performReadinessCheck();
}
package org.opengroup.osdu.partition.service;
import org.opengroup.osdu.partition.provider.interfaces.IHealthCheckService;
import org.springframework.stereotype.Service;
@Service
public class DefaultHealthCheckImpl implements IHealthCheckService {
@Override
public void performLivenessCheck() {
}
@Override
public void performReadinessCheck() {
}
}
// Copyright 2017-2020, Schlumberger
//
// 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.opengroup.osdu.partition.api;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.partition.logging.AuditLogger;
import org.opengroup.osdu.partition.provider.interfaces.IHealthCheckService;
import org.springframework.http.HttpStatus;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class HealthCheckTest {
@Mock
private AuditLogger auditLogger;
@Mock
private IHealthCheckService healthCheckService;
@InjectMocks
private HealthCheck sut;
@Test
public void should_returnHttp200_when_checkLiveness() {
assertEquals(HttpStatus.OK, this.sut.livenessCheck().getStatusCode());
}
@Test(expected = Exception.class)
public void should_throwException_when_customizedLivenessCheckFail() {
doThrow(new Exception()).when(healthCheckService).performLivenessCheck();
this.sut.livenessCheck();
}
@Test
public void should_returnHttp200_when_checkReadiness() {
assertEquals(HttpStatus.OK, this.sut.readinessCheck().getStatusCode());
verify(healthCheckService).performReadinessCheck();
}
@Test(expected = Exception.class)
public void should_throwException_when_customizedReadinessCheckFail() {
doThrow(new Exception()).when(healthCheckService).performReadinessCheck();
this.sut.readinessCheck();
}
}
\ No newline at end of file
......@@ -93,10 +93,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
......
......@@ -107,7 +107,7 @@ $ (cd testing/partition-test-core/ && mvn clean install)
$ (cd testing/partition-test-azure/ && mvn clean test)
```
A liveness check can also be performed at `http://localhost:8080/api/partition/v1/_ah/liveness_check`. Other apis can be found on the swagger page
A liveness check can also be performed at `http://localhost:8080/api/partition/v1/actuator/health`. Other apis can be found on the swagger page
## Debugging
......
......@@ -151,7 +151,7 @@ stages:
attempt_counter=0
max_attempts=60
# NOTE that the PARTITION_BASE_URL already has a '/' at the end of it
until [ $(curl -s -o /dev/null -w "%{http_code}" $(PARTITION_BASE_URL)api/partition/v1/_ah/liveness_check) -eq 200 ]; do
until [ $(curl -s -o /dev/null -w "%{http_code}" $(PARTITION_BASE_URL)api/partition/v1/actuator/health) -eq 200 ]; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Service is not available, please check the deployment"
exit 1
......
......@@ -40,7 +40,7 @@ public class AADSecurityConfig extends WebSecurityConfigurerAdapter {
.and()
.authorizeRequests()
.antMatchers("/", "/index.html",
"/_ah/*",
"/actuator/*",
"/api-docs",
"/configuration/ui",
"/swagger-resources/**",
......
......@@ -2,19 +2,19 @@ package org.opengroup.osdu.partition.provider.azure.service;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.partition.model.PartitionInfo;
import org.opengroup.osdu.partition.provider.interfaces.IHealthCheckService;
import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache;
import org.opengroup.osdu.partition.service.DefaultHealthCheckImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
@Primary
@RequiredArgsConstructor
public class HealthCheckServiceImpl extends DefaultHealthCheckImpl {
class CacheServiceHealthIndicatorImpl implements HealthIndicator {
@Autowired
@Qualifier("partitionServiceCache")
......@@ -23,11 +23,15 @@ public class HealthCheckServiceImpl extends DefaultHealthCheckImpl {
@Value("${redis.custom.readiness.check.enabled}")
private boolean redisCustomReadinessCheck;
@Override
public void performReadinessCheck() {
public Health health() {
if (redisCustomReadinessCheck) {
partitionServiceCache.get("dummy-key");
try {
partitionServiceCache.get("dummy-key");
} catch (Exception ex) {
return Health.down().withDetail("Cache service", ex).build();
}
}
return Health.up().build();
}
}
......@@ -4,7 +4,6 @@ logging.level.org.springframework.web=${partition_spring_logging_level:INFO}
JAVA_OPTS=-Dserver.port=80
server.port=8080
springfox.documentation.swagger.v2.path=/api-docs
PARTITION_API=
#logging configuration
logging.transaction.enabled=true
......@@ -37,6 +36,10 @@ redis.ssl.enabled=true
redis.connection.timeout=15
redis.database=${REDIS_DATABASE}
# health check
management.health.azure-key-vault.enabled=false
redis.custom.readiness.check.enabled=false
azure.cryptography.enabled=false
azure.eventgrid.topic.enabled=false
azure.eventgrid.manager.enabled=false
......@@ -42,10 +42,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
......
......@@ -41,10 +41,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
......
......@@ -15,7 +15,7 @@ cache.provider=vm
cache.expiration=1
cache.maxSize=1000
ibm.health-check-uri=/api/partition/v1/_ah/liveness_check,/api/partition/v1/_ah/readiness_check
ibm.health-check-uri=/api/partition/v1/actuator/health,/api/partition/v1/actuator/health
excluded.uris.for.partition-filter=/api/partition/v1/partitions
#Database Configuration
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment