Skip to content

Commit

Permalink
Submit a feature that applies graceful up and down (#4670)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshouye168 authored Jan 13, 2025
1 parent 8cedd07 commit 0668f63
Show file tree
Hide file tree
Showing 35 changed files with 153 additions and 176 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ private void doRun() throws Exception {
triggerEvent(EventType.BEFORE_REGISTRY);
registrationManager.run();
discoveryManager.run();
// ensure can invoke services in AFTER_REGISTRY
registrationManager.updateMicroserviceInstanceStatus(MicroserviceInstanceStatus.UP);
status = SCBStatus.UP;
triggerEvent(EventType.AFTER_REGISTRY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private VersionedCache calcAvailableInstance(String application, String serviceN
continue;
}
if (instance.getHistoryStatus() == HistoryStatus.HISTORY
&& instance.getMicroserviceInstanceStatus() == MicroserviceInstanceStatus.UP
&& instance.getStatus() == MicroserviceInstanceStatus.UP
&& instance.getPingStatus() == PingStatus.OK
&& instance.getIsolationStatus() == IsolationStatus.NORMAL) {
result.add(instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ public interface MicroserviceInstance {
default String getServiceId() {
return "";
}

/**
* Service status(Required): status of this microservice.
*
*/
MicroserviceInstanceStatus getStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,4 @@
* Microserivce instance registration object.
*/
public interface RegistrationInstance extends MicroserviceInstance {
/**
* Initial status when registering(Optional). This method is
* implementation specific and may be not used.
*/
MicroserviceInstanceStatus getInitialStatus();

/**
* Ready status when microservice instance is ready for service(Optional).This method is
* implementation specific and may be not used.
*/
MicroserviceInstanceStatus getReadyStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ public void init(DiscoveryContext context, DiscoveryTreeNode parent) {

for (StatefulDiscoveryInstance instance : instances) {
if (HistoryStatus.CURRENT == instance.getHistoryStatus() &&
MicroserviceInstanceStatus.UP == instance.getMicroserviceInstanceStatus() &&
MicroserviceInstanceStatus.UP == instance.getStatus() &&
PingStatus.OK == instance.getPingStatus() &&
IsolationStatus.NORMAL == instance.getIsolationStatus()) {
level0.add(instance);
continue;
}
if (HistoryStatus.CURRENT == instance.getHistoryStatus() &&
MicroserviceInstanceStatus.UP == instance.getMicroserviceInstanceStatus() &&
MicroserviceInstanceStatus.UP == instance.getStatus() &&
PingStatus.UNKNOWN == instance.getPingStatus() &&
IsolationStatus.NORMAL == instance.getIsolationStatus()) {
level1.add(instance);
continue;
}
if (HistoryStatus.HISTORY == instance.getHistoryStatus() &&
MicroserviceInstanceStatus.UP == instance.getMicroserviceInstanceStatus() &&
MicroserviceInstanceStatus.UP == instance.getStatus() &&
PingStatus.OK == instance.getPingStatus() &&
IsolationStatus.NORMAL == instance.getIsolationStatus()) {
level2.add(instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public enum HistoryStatus {

private final DiscoveryInstance discoveryInstance;

private MicroserviceInstanceStatus microserviceInstanceStatus = MicroserviceInstanceStatus.UP;

private IsolationStatus isolationStatus = IsolationStatus.NORMAL;

private long isolatedTime;
Expand All @@ -64,15 +62,6 @@ public StatefulDiscoveryInstance(DiscoveryInstance discoveryInstance) {
this.discoveryInstance = discoveryInstance;
}

public MicroserviceInstanceStatus getMicroserviceInstanceStatus() {
return microserviceInstanceStatus;
}

public void setMicroserviceInstanceStatus(
MicroserviceInstanceStatus microserviceInstanceStatus) {
this.microserviceInstanceStatus = microserviceInstanceStatus;
}

public IsolationStatus getIsolationStatus() {
return isolationStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ public void test_all_group_correct_init() {
List<StatefulDiscoveryInstance> instances = new ArrayList<>();
StatefulDiscoveryInstance instance1 = Mockito.mock(StatefulDiscoveryInstance.class);
Mockito.when(instance1.getHistoryStatus()).thenReturn(HistoryStatus.CURRENT);
Mockito.when(instance1.getMicroserviceInstanceStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance1.getStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance1.getPingStatus()).thenReturn(PingStatus.OK);
Mockito.when(instance1.getIsolationStatus()).thenReturn(IsolationStatus.NORMAL);
StatefulDiscoveryInstance instance2 = Mockito.mock(StatefulDiscoveryInstance.class);
Mockito.when(instance2.getHistoryStatus()).thenReturn(HistoryStatus.CURRENT);
Mockito.when(instance2.getMicroserviceInstanceStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance2.getStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance2.getPingStatus()).thenReturn(PingStatus.UNKNOWN);
Mockito.when(instance2.getIsolationStatus()).thenReturn(IsolationStatus.NORMAL);
StatefulDiscoveryInstance instance3 = Mockito.mock(StatefulDiscoveryInstance.class);
Mockito.when(instance3.getHistoryStatus()).thenReturn(HistoryStatus.HISTORY);
Mockito.when(instance3.getMicroserviceInstanceStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance3.getStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance3.getPingStatus()).thenReturn(PingStatus.OK);
Mockito.when(instance3.getIsolationStatus()).thenReturn(IsolationStatus.NORMAL);
StatefulDiscoveryInstance instance4 = Mockito.mock(StatefulDiscoveryInstance.class);
Mockito.when(instance4.getHistoryStatus()).thenReturn(HistoryStatus.CURRENT);
Mockito.when(instance4.getMicroserviceInstanceStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance4.getStatus()).thenReturn(MicroserviceInstanceStatus.UP);
Mockito.when(instance4.getPingStatus()).thenReturn(PingStatus.FAIL);
Mockito.when(instance4.getIsolationStatus()).thenReturn(IsolationStatus.NORMAL);
instances.addAll(Arrays.asList(instance1, instance2, instance3, instance4));
Expand Down
20 changes: 10 additions & 10 deletions foundations/foundation-registry/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
under the License.
-->
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
</Loggers>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
4 changes: 2 additions & 2 deletions service-registry/registry-consul/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.servicecomb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.servicecomb.registry.consul;

import com.google.common.net.HostAndPort;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.DataCenterProperties;
import org.apache.servicecomb.registry.RegistrationId;
Expand Down Expand Up @@ -54,7 +55,8 @@ public ConsulDiscoveryProperties consulDiscoveryProperties() {
@ConditionalOnBean(value = {ConsulProperties.class, ConsulDiscoveryProperties.class})
@ConditionalOnMissingBean
public Consul consulClient(ConsulProperties consulProperties, ConsulDiscoveryProperties consulDiscoveryProperties) {
Consul.Builder builder = Consul.builder().withHostAndPort(HostAndPort.fromParts(consulProperties.getHost(), consulProperties.getPort()));
Consul.Builder builder = Consul.builder()
.withHostAndPort(HostAndPort.fromParts(consulProperties.getHost(), consulProperties.getPort()));
if (StringUtils.isNotBlank(consulDiscoveryProperties.getAclToken())) {
builder.withAclToken(consulDiscoveryProperties.getAclToken());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;

import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.registry.api.Discovery;
import org.apache.servicecomb.registry.consul.config.ConsulDiscoveryProperties;
Expand Down Expand Up @@ -111,7 +113,8 @@ public void run() {
LOGGER.info("ConsulDiscovery run");
String serviceName = BootStrapProperties.readServiceName(environment);
HealthClient healthClient = consulClient.healthClient();
svHealth = ServiceHealthCache.newCache(healthClient, serviceName, true, Options.BLANK_QUERY_OPTIONS, consulDiscoveryProperties.getWatchSeconds());
svHealth = ServiceHealthCache.newCache(healthClient, serviceName, true, Options.BLANK_QUERY_OPTIONS,
consulDiscoveryProperties.getWatchSeconds());
svHealth.addListener((Map<ServiceHealthKey, ServiceHealth> newValues) -> instanceChangedListener.onInstanceChanged(
name(), BootStrapProperties.readApplication(environment), serviceName, getInstances(serviceName)));
svHealth.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@

public class ConsulDiscoveryInstance extends ConsulInstance implements DiscoveryInstance {

public ConsulDiscoveryInstance(ConsulInstance consulInstance) {
super(consulInstance);
}
public ConsulDiscoveryInstance(ConsulInstance consulInstance) {
super(consulInstance);
}

@Override
public MicroserviceInstanceStatus getStatus() {
return MicroserviceInstanceStatus.UP;
}
@Override
public MicroserviceInstanceStatus getStatus() {
return MicroserviceInstanceStatus.UP;
}

@Override
public String getRegistryName() {
return ConsulConst.CONSUL_REGISTRY_NAME;
}
@Override
public String getRegistryName() {
return ConsulConst.CONSUL_REGISTRY_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.servicecomb.registry.api.DataCenterInfo;
import org.apache.servicecomb.registry.api.MicroserviceInstance;
import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -144,7 +145,7 @@ public String getVersion() {

@Override
public DataCenterInfo getDataCenterInfo() {
return dataCenterInfo;
return dataCenterInfo == null ? new DataCenterInfo() : dataCenterInfo;
}

@Override
Expand Down Expand Up @@ -188,4 +189,9 @@ public String getInstanceId() {
public String getServiceId() {
return serviceId;
}

@Override
public MicroserviceInstanceStatus getStatus() {
return MicroserviceInstanceStatus.UP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import jakarta.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.config.BootStrapProperties;
import org.apache.servicecomb.config.DataCenterProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,12 @@

package org.apache.servicecomb.registry.consul;

import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus;

import org.apache.servicecomb.registry.api.RegistrationInstance;

public class ConsulRegistrationInstance extends ConsulInstance implements RegistrationInstance {

public ConsulRegistrationInstance(ConsulInstance instance) {
super(instance);
}

@Override
public MicroserviceInstanceStatus getInitialStatus() {
return MicroserviceInstanceStatus.STARTING;
}

@Override
public MicroserviceInstanceStatus getReadyStatus() {
return MicroserviceInstanceStatus.UP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.servicecomb.registry.consul.config;

import jakarta.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.servicecomb.registry.api.DataCenterInfo;
import org.apache.servicecomb.registry.api.MicroserviceInstance;
import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus;

public class EtcdInstance implements MicroserviceInstance {
private String serviceId;
Expand Down Expand Up @@ -143,7 +144,7 @@ public String getVersion() {

@Override
public DataCenterInfo getDataCenterInfo() {
return dataCenterInfo;
return dataCenterInfo == null ? new DataCenterInfo() : dataCenterInfo;
}

@Override
Expand Down Expand Up @@ -188,6 +189,11 @@ public String getServiceId() {
return serviceId;
}

@Override
public MicroserviceInstanceStatus getStatus() {
return MicroserviceInstanceStatus.UP;
}

@Override
public String toString() {
return "EtcdInstance{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,10 @@
*/
package org.apache.servicecomb.registry.etcd;

import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus;
import org.apache.servicecomb.registry.api.RegistrationInstance;

public class EtcdRegistrationInstance extends EtcdInstance implements RegistrationInstance {
public EtcdRegistrationInstance(EtcdInstance instance) {
super(instance);
}

@Override
public MicroserviceInstanceStatus getInitialStatus() {
return MicroserviceInstanceStatus.STARTING;
}

@Override
public MicroserviceInstanceStatus getReadyStatus() {
return MicroserviceInstanceStatus.UP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ public String getInstanceId() {
}

@Override
public MicroserviceInstanceStatus getInitialStatus() {
return MicroserviceInstanceStatus.STARTING;
}

@Override
public MicroserviceInstanceStatus getReadyStatus() {
public MicroserviceInstanceStatus getStatus() {
return MicroserviceInstanceStatus.UP;
}

Expand Down
Loading

0 comments on commit 0668f63

Please sign in to comment.