Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.healthcheck.impl;

import com.alipay.sofa.boot.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
Expand Down Expand Up @@ -57,10 +58,13 @@ public Health isHealthy() {
Health.Builder builder = new Health.Builder();
for (ComponentInfo componentInfo : sofaRuntimeContext.getComponentManager().getComponents()) {
HealthResult healthy = componentInfo.isHealthy();
if (healthy.isHealthy()) {
builder.withDetail(healthy.getHealthName(), "passed");
} else {
String healthReport = healthy.getHealthReport();
if (StringUtils.hasText(healthReport)) {
builder.withDetail(healthy.getHealthName(), healthy.getHealthReport());
} else {
builder.withDetail(healthy.getHealthName(), "passed");
}
if (!healthy.isHealthy()) {
allPassed = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.rpc.boot.test.misc;

import com.alipay.sofa.healthcheck.impl.ComponentHealthChecker;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

/**
* @author <a href="mailto:[email protected]">guaner.zzx</a>
* Created on 2019/12/19
*/
@SpringBootApplication
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ComponentHealthCheckerTest.class, properties = { "timeout=10000" })
@ImportResource("/spring/service_reference.xml")
public class ComponentHealthCheckerTest {
@Autowired
private ApplicationContext applicationContext;

@Test
public void componentHealthCheckerTest() {
ComponentHealthChecker componentHealthChecker = applicationContext
.getBean(ComponentHealthChecker.class);
Health health = componentHealthChecker.isHealthy();
Map<String, Object> details = health.getDetails();
for (String key : details.keySet()) {
Assert.assertTrue(((String) details.get(key)).contains("bolt"));
}
}

@Configuration
static class ComponentHealthCheckerTestConfiguration {
@Bean
public ComponentHealthChecker sofaComponentHealthChecker(SofaRuntimeContext sofaRuntimeContext) {
return new ComponentHealthChecker(sofaRuntimeContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.boot.test.xsd;
package com.alipay.sofa.rpc.boot.test.misc;

/**
* @author <a href="mailto:[email protected]">guaner.zzx</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.boot.test.xsd;
package com.alipay.sofa.rpc.boot.test.misc;

/**
* @author <a href="mailto:[email protected]">guaner.zzx</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.boot.test.xsd;
package com.alipay.sofa.rpc.boot.test.misc;

import com.alipay.sofa.rpc.boot.runtime.binding.BoltBinding;
import com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding;
import com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingType;
import com.alipay.sofa.runtime.api.component.ComponentName;
import com.alipay.sofa.runtime.service.component.Reference;
import com.alipay.sofa.runtime.service.component.ReferenceComponent;
import com.alipay.sofa.runtime.service.component.ServiceComponent;
import com.alipay.sofa.runtime.spi.component.ComponentInfo;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -34,16 +31,14 @@
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Collection;

/**
* @author <a href="mailto:[email protected]">guaner.zzx</a>
* Created on 2019/12/18
*/
@SpringBootApplication
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XsdTimeoutTest.class, properties = { "timeout=10000" })
@ImportResource("/spring/xsd.xml")
@ImportResource("/spring/service_reference.xml")
public class XsdTimeoutTest {
@Autowired
WhateverInterface whatever;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
xmlns:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd">
<bean id="whateverBean" class="com.alipay.sofa.rpc.boot.test.xsd.WhateverClass" />
<sofa:service ref="whateverBean" interface="com.alipay.sofa.rpc.boot.test.xsd.WhateverInterface">
<bean id="whateverBean" class="com.alipay.sofa.rpc.boot.test.misc.WhateverClass" />
<sofa:service ref="whateverBean" interface="com.alipay.sofa.rpc.boot.test.misc.WhateverInterface">
<sofa:binding.bolt>
<sofa:global-attrs timeout="${timeout}" />
</sofa:binding.bolt>
</sofa:service>

<sofa:reference id="whatever" jvm-first="false" interface="com.alipay.sofa.rpc.boot.test.xsd.WhateverInterface">
<sofa:reference id="whatever" jvm-first="false" interface="com.alipay.sofa.rpc.boot.test.misc.WhateverInterface">
<sofa:binding.bolt>
<sofa:global-attrs timeout="${timeout}" />
</sofa:binding.bolt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class JvmBinding extends AbstractBinding {

private JvmBindingParam jvmBindingParam = new JvmBindingParam();

private HealthResult healthResult = new HealthResult(getName());

public JvmBinding() {
}

Expand Down Expand Up @@ -92,7 +94,6 @@ public int getBindingHashCode() {

@Override
public HealthResult healthCheck() {
HealthResult healthResult = new HealthResult(getName());
healthResult.setHealthy(isHealthy);
return healthResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Object doInvoke(MethodInvocation invocation) throws Throwable {
}
}

if ((targetObj == null || ((targetObj instanceof Proxy) && binding.hasBackupProxy()))) {
if (targetObj == null || ((targetObj instanceof Proxy) && binding.hasBackupProxy())) {
targetObj = binding.getBackupProxy();
SofaLogger.debug("<<{0}.{1} backup proxy invoke.", getInterfaceName().getName(),
invocation.getMethod().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ public Map<String, Property> getProperties() {

@Override
public HealthResult isHealthy() {
if (!isActivated()) {
return super.isHealthy();
}

HealthResult result = new HealthResult(componentName.getRawName());
List<HealthResult> bindingHealth = new ArrayList<>();

Expand Down Expand Up @@ -122,14 +118,9 @@ public HealthResult isHealthy() {
if (failedBindingHealth.size() == 0) {
result.setHealthy(true);
} else {
StringBuilder healthReport = new StringBuilder("|");
for (HealthResult healthResult : failedBindingHealth) {
healthReport.append(healthResult.getHealthName()).append("#")
.append(healthResult.getHealthReport());
}
result.setHealthReport(healthReport.substring(1, healthReport.length()));
result.setHealthy(false);
}
result.setHealthReport(aggregateBindingHealth(reference.getBindings()));

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private void activateBinding() {
getContext());
} catch (Throwable t) {
allPassed = false;
binding.setHealthy(false);
SofaLogger.error(t, " <<Out binding [{0}] for [{1}] occur exception.",
binding.getBindingType(), service);
continue;
Expand Down Expand Up @@ -306,17 +307,14 @@ public Service getService() {

@Override
public HealthResult isHealthy() {
if (!isActivated()) {
return super.isHealthy();
}

HealthResult healthResult = new HealthResult(componentName.getRawName());
if (this.e == null) {
healthResult.setHealthy(true);
} else {
healthResult.setHealthy(false);
healthResult.setHealthReport(e.getMessage());
}
healthResult.setHealthReport(aggregateBindingHealth(service.getBindings()));

return healthResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import com.alipay.sofa.runtime.api.component.ComponentLifeCycle;
import com.alipay.sofa.runtime.api.component.ComponentName;
import com.alipay.sofa.runtime.model.ComponentStatus;
import com.alipay.sofa.runtime.spi.binding.Binding;
import com.alipay.sofa.runtime.spi.health.HealthResult;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* abstract component implementation
*
Expand Down Expand Up @@ -170,6 +175,20 @@ public HealthResult isHealthy() {
return healthResult;
}

protected String aggregateBindingHealth(Collection<Binding> bindings) {
List<String> healthResult = new ArrayList<>();
for (Binding binding : bindings) {
HealthResult result = binding.healthCheck();
String report = "["
+ result.getHealthName()
+ ","
+ (result.getHealthReport() == null ? (result.isHealthy() ? "passed"
: "failed") : result.getHealthReport()) + "]";
healthResult.add(report);
}
return String.join(" ", healthResult);
}

@Override
public boolean canBeDuplicate() {
return true;
Expand Down