Skip to content

Commit

Permalink
Add a function that can remove the corresponding components when modu…
Browse files Browse the repository at this point in the history
…les install failure (#1012)
  • Loading branch information
crazysaltfish authored Sep 14, 2022
1 parent 591daf8 commit 71e390b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
*/
package com.alipay.sofa.isle.spring.config;

import static com.alipay.sofa.isle.spring.config.SofaModuleProperties.PREFIX;

import org.springframework.boot.context.properties.ConfigurationProperties;

import static com.alipay.sofa.isle.spring.config.SofaModuleProperties.PREFIX;

/**
* Properties
*
* @author xuanbei 18/5/6
*/
@ConfigurationProperties(prefix = PREFIX)
public class SofaModuleProperties {
static final String PREFIX = "com.alipay.sofa.boot";
static final String PREFIX = "com.alipay.sofa.boot";

private String activeProfiles;
private long beanLoadCost = 100;
private boolean allowBeanDefinitionOverriding = false;
private boolean moduleStartUpParallel = true;
private boolean publishEventToParent = false;
private boolean enableIsle = true;
private boolean allowModuleOverriding = false;
private boolean ignoreModuleInstallFailure = false;
private float parallelRefreshCoreCountFactor = 5.0f;
private long parallelRefreshTimeout = 60;
private long parallelRefreshCheckPeriod = 30;
private long beanLoadCost = 100;
private boolean allowBeanDefinitionOverriding = false;
private boolean moduleStartUpParallel = true;
private boolean publishEventToParent = false;
private boolean enableIsle = true;
private boolean allowModuleOverriding = false;
private boolean ignoreModuleInstallFailure = false;
private boolean unregisterComponentWhenModuleInstallFailure = true;
private float parallelRefreshCoreCountFactor = 5.0f;
private long parallelRefreshTimeout = 60;
private long parallelRefreshCheckPeriod = 30;

public String getActiveProfiles() {
return activeProfiles;
Expand Down Expand Up @@ -128,4 +129,12 @@ public long getParallelRefreshCheckPeriod() {
public void setParallelRefreshCheckPeriod(long parallelRefreshCheckPeriod) {
this.parallelRefreshCheckPeriod = parallelRefreshCheckPeriod;
}

public boolean isUnregisterComponentWhenModuleInstallFailure() {
return unregisterComponentWhenModuleInstallFailure;
}

public void setUnregisterComponentWhenModuleInstallFailure(boolean unregisterComponentWhenModuleInstallFailure) {
this.unregisterComponentWhenModuleInstallFailure = unregisterComponentWhenModuleInstallFailure;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import com.alipay.sofa.isle.loader.DynamicSpringContextLoader;
import com.alipay.sofa.isle.loader.SpringContextLoader;
import com.alipay.sofa.isle.spring.config.SofaModuleProperties;
import com.alipay.sofa.runtime.api.ServiceRuntimeException;
import com.alipay.sofa.runtime.api.component.ComponentName;
import com.alipay.sofa.runtime.log.SofaLogger;
import com.alipay.sofa.runtime.spi.component.ComponentInfo;
import com.alipay.sofa.runtime.spi.component.ComponentManager;
import com.alipay.sofa.runtime.spi.component.Implementation;
import com.alipay.sofa.runtime.spi.util.ComponentNameFactory;
import com.alipay.sofa.runtime.spring.SpringContextComponent;
Expand All @@ -40,13 +42,9 @@
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -329,6 +327,7 @@ protected void doRefreshSpringContext(DeploymentDescriptor deployment,
} catch (Throwable t) {
SofaLogger.error(ErrorCode.convert("01-11002", deployment.getName()), t);
application.addFailed(deployment);
unRegisterComponent(application, ctx);
} finally {
deployment.deployFinish();
}
Expand All @@ -339,6 +338,23 @@ protected void doRefreshSpringContext(DeploymentDescriptor deployment,
}
}

private void unRegisterComponent(ApplicationRuntimeModel application,
ConfigurableApplicationContext ctx) {
if (sofaModuleProperties.isUnregisterComponentWhenModuleInstallFailure()) {
ComponentManager componentManager = application.getSofaRuntimeContext()
.getComponentManager();
Collection<ComponentInfo> componentInfos = componentManager
.getComponentInfosByApplicationContext(ctx);
for (ComponentInfo componentInfo : componentInfos) {
try {
componentManager.unregister(componentInfo);
} catch (ServiceRuntimeException e) {
SofaLogger.error(ErrorCode.convert("01-03001", componentInfo.getName()), e);
}
}
}
}

private void publishContextAsSofaComponent(DeploymentDescriptor deployment,
ApplicationRuntimeModel application,
ApplicationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
import com.alipay.sofa.runtime.spi.component.ComponentInfo;
import com.alipay.sofa.runtime.spi.component.ComponentManager;
import com.alipay.sofa.runtime.spring.SpringContextComponent;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -242,6 +239,19 @@ public void resolvePendingResolveComponent(ComponentName componentName) {
}
}

@Override
public Collection<ComponentInfo> getComponentInfosByApplicationContext(ApplicationContext application) {
List<ComponentInfo> componentInfos = new ArrayList<>();

for (ComponentInfo componentInfo : registry.values()) {
if (Objects.equals(application, componentInfo.getApplicationContext())) {
componentInfos.add(componentInfo);
}
}

return componentInfos;
}

private void typeRegistry(ComponentInfo componentInfo) {
ComponentName name = componentInfo.getName();
if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.sofa.runtime.api.ServiceRuntimeException;
import com.alipay.sofa.runtime.api.component.ComponentName;
import com.alipay.sofa.runtime.model.ComponentType;
import org.springframework.context.ApplicationContext;

import java.util.Collection;

Expand Down Expand Up @@ -113,4 +114,12 @@ public interface ComponentManager {
* @param componentName component name
*/
void resolvePendingResolveComponent(ComponentName componentName);

/**
* get components by component application
*
* @param application component application
* @return components
*/
Collection<ComponentInfo> getComponentInfosByApplicationContext(ApplicationContext application);
}

0 comments on commit 71e390b

Please sign in to comment.