Skip to content

Commit

Permalink
Add health check retry. (#237)
Browse files Browse the repository at this point in the history
Add health check retry.
  • Loading branch information
straybirdzls authored and QilongZhang committed Oct 10, 2018
1 parent aa1c001 commit 5119a25
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ public class HealthCheckConfigurationConstants {
public static final String SOFABOOT_STRICT_COMPONENT_HEALTH_CHECK = "com.alipay.sofa.healthcheck.strict.component.check";
public static final String SOFABOOT_STRICT_COMPONENT_HEALTH_CHECK_UNDERLINE = "com_alipay_sofa_healthcheck_strict_component_check";

// 组件健康检查次数配置项
public static final String SOFABOOT_COMPONENT_CHECK_RETRY_COUNT = "com.alipay.sofa.healthcheck.component.check.retry.count";
// 组件健康检查默认次数
public static final int SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_COUNT = 20;
// 组件健康检查间隔配置项
public static final String SOFABOOT_COMPONENT_CHECK_RETRY_INTERVAL = "com.alipay.sofa.healthcheck.component.check.retry.interval";
// 组件健康检查默认间隔
public static final long SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_INTERVAL = 1000;

// 模块健康检查次数配置项
public static final String SOFABOOT_MODULE_CHECK_RETRY_COUNT = "com.alipay.sofa.healthcheck.module.check.retry.count";
// 模块健康检查默认次数
public static final int SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_COUNT = 0;
// 模块健康检查间隔配置项
public static final String SOFABOOT_MODULE_CHECK_RETRY_INTERVAL = "com.alipay.sofa.healthcheck.module.check.retry.interval";
// 模块健康检查默认间隔
public static final long SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_INTERVAL = 1000;

//日志
public static final String SOFABOOT_HEALTH_LOG_SPACE = "com.alipay.sofa.healthcheck";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package com.alipay.sofa.isle.spring.health;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.healthcheck.core.HealthChecker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;

/**
Expand All @@ -25,6 +27,15 @@
* @author xuanbei 18/5/6
*/
public class SofaModuleHealthChecker extends AbstractModuleHealthChecker implements HealthChecker {

@Value("${" + HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_COUNT + ":"
+ HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_COUNT + "}")
private int retryCount;

@Value("${" + HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_INTERVAL + ":"
+ HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_INTERVAL + "}")
private long retryInterval;

@Override
public Health isHealthy() {
return doHealthCheck();
Expand All @@ -37,12 +48,12 @@ public String getComponentName() {

@Override
public int getRetryCount() {
return 0;
return retryCount;
}

@Override
public long getRetryTimeInterval() {
return 0;
return retryInterval;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.isle.integration;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.healthcheck.core.HealthChecker;
import com.alipay.sofa.isle.ApplicationRuntimeModel;
import com.alipay.sofa.isle.constants.SofaModuleFrameworkConstants;
Expand Down Expand Up @@ -99,8 +100,12 @@ public void testHealthChecker() {
.getBean("sofaModuleHealthChecker");
Assert.assertTrue(healthChecker.isHealthy().getStatus().equals(Status.UP));
Assert.assertEquals("SOFABoot Modules", healthChecker.getComponentName());
Assert.assertEquals(0, healthChecker.getRetryCount());
Assert.assertEquals(0, healthChecker.getRetryTimeInterval());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_COUNT,
healthChecker.getRetryCount());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_INTERVAL,
healthChecker.getRetryTimeInterval());
Assert.assertEquals(true, healthChecker.isStrictCheck());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.isle.spring.health;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.isle.spring.configuration.SofaModuleAutoConfiguration;
import com.alipay.sofa.runtime.spring.configuration.SofaRuntimeAutoConfiguration;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author abby.zh
* @since 2.4.10
*/
public class SofaModuleHealthCheckerTest {

private final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

@After
public void closeContext() {
this.applicationContext.close();
}

@Test
public void testDefaultConfig() {
this.applicationContext.register(SofaModuleAutoConfiguration.class);
this.applicationContext.refresh();
SofaModuleHealthChecker sofaModuleHealthChecker = applicationContext
.getBean(SofaModuleHealthChecker.class);
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_COUNT,
sofaModuleHealthChecker.getRetryCount());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_DEFAULT_INTERVAL,
sofaModuleHealthChecker.getRetryTimeInterval());
}

@Test
public void testCustomConfig() {
int customRetryCount = 10;
int customRetryInterval = 30;
this.applicationContext.register(SofaModuleAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.applicationContext,
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_COUNT + "="
+ customRetryCount);
EnvironmentTestUtils.addEnvironment(this.applicationContext,
HealthCheckConfigurationConstants.SOFABOOT_MODULE_CHECK_RETRY_INTERVAL + "="
+ customRetryInterval);
this.applicationContext.register(SofaRuntimeAutoConfiguration.class);
this.applicationContext.refresh();
SofaModuleHealthChecker sofaModuleHealthChecker = applicationContext
.getBean(SofaModuleHealthChecker.class);
Assert.assertEquals(customRetryCount, sofaModuleHealthChecker.getRetryCount());
Assert.assertEquals(customRetryInterval, sofaModuleHealthChecker.getRetryTimeInterval());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/
package com.alipay.sofa.runtime.spring.health;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.healthcheck.core.HealthChecker;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;

/**
Expand All @@ -27,6 +29,16 @@
*/
public class SofaComponentHealthChecker extends AbstractComponentHealthChecker implements
HealthChecker {

@Value("${" + HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_COUNT + ":"
+ HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_COUNT + "}")
private int retryCount;

@Value("${" + HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_INTERVAL + ":"
+ HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_INTERVAL
+ "}")
private int retryInterval;

public SofaComponentHealthChecker(SofaRuntimeContext sofaRuntimeContext) {
super(sofaRuntimeContext);
}
Expand All @@ -43,12 +55,12 @@ public String getComponentName() {

@Override
public int getRetryCount() {
return 0;
return retryCount;
}

@Override
public long getRetryTimeInterval() {
return 0;
return retryInterval;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.runtime.integration;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.healthcheck.core.HealthChecker;
import com.alipay.sofa.runtime.beans.service.SampleService;
import com.alipay.sofa.runtime.integration.aop.SampleServiceAspect;
Expand Down Expand Up @@ -67,8 +68,12 @@ public void testHealthChecker() {
HealthChecker healthChecker = (HealthChecker) context.getBean("sofaComponentHealthChecker");
Assert.assertTrue(healthChecker.isHealthy().getStatus().equals(Status.UP));
Assert.assertEquals("RUNTIME-COMPONENT", healthChecker.getComponentName());
Assert.assertEquals(0, healthChecker.getRetryCount());
Assert.assertEquals(0, healthChecker.getRetryTimeInterval());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_COUNT,
healthChecker.getRetryCount());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_INTERVAL,
healthChecker.getRetryTimeInterval());
Assert.assertEquals(true, healthChecker.isStrictCheck());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.runtime.spring.health;

import com.alipay.sofa.healthcheck.configuration.HealthCheckConfigurationConstants;
import com.alipay.sofa.runtime.spring.configuration.SofaRuntimeAutoConfiguration;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author abby.zh
* @since 2.4.10
*/
public class SofaComponentHealthCheckerTest {

private final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

@After
public void closeContext() {
this.applicationContext.close();
}

@Test
public void testDefaultConfig() {
this.applicationContext.register(SofaRuntimeAutoConfiguration.class);
this.applicationContext.refresh();
SofaComponentHealthChecker sofaComponentHealthChecker = applicationContext
.getBean(SofaComponentHealthChecker.class);
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_COUNT,
sofaComponentHealthChecker.getRetryCount());
Assert.assertEquals(
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_DEFAULT_INTERVAL,
sofaComponentHealthChecker.getRetryTimeInterval());
}

@Test
public void testCustomConfig() {
int customRetryCount = 10;
int customRetryInterval = 30;
this.applicationContext.register(SofaRuntimeAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.applicationContext,
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_COUNT + "="
+ customRetryCount);
EnvironmentTestUtils.addEnvironment(this.applicationContext,
HealthCheckConfigurationConstants.SOFABOOT_COMPONENT_CHECK_RETRY_INTERVAL + "="
+ customRetryInterval);
this.applicationContext.register(SofaRuntimeAutoConfiguration.class);
this.applicationContext.refresh();
SofaComponentHealthChecker sofaComponentHealthChecker = applicationContext
.getBean(SofaComponentHealthChecker.class);
Assert.assertEquals(customRetryCount, sofaComponentHealthChecker.getRetryCount());
Assert.assertEquals(customRetryInterval, sofaComponentHealthChecker.getRetryTimeInterval());
}

}

0 comments on commit 5119a25

Please sign in to comment.