Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into feat_plugin_manage
Browse files Browse the repository at this point in the history
# Conflicts:
#	sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/model/BizModel.java
#	sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/api/ArkClientTest.java
#	sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/biz/BizFactoryServiceTest.java
#	sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/constant/Constants.java
  • Loading branch information
yuanyuancin committed Jan 3, 2025
2 parents 792c726 + 41fe026 commit f6eb235
Show file tree
Hide file tree
Showing 56 changed files with 4,582 additions and 130 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ sofa-ark-parent/support/ark-plugin-maven-plugin/null.ark.plugin.bak
sofa-ark-parent/support/ark-plugin-maven-plugin/xxx.ark.plugin
sofa-ark-parent/core/common/C/\temp dir\b\c/test.txt
sofa-ark-parent/core/common/C:\\temp dir\\b\\c/test.txt

.gradle/
build/
gradle/
gradlew
gradlew.bat
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</modules>

<properties>
<sofa.ark.version>2.2.15-SNAPSHOT</sofa.ark.version>
<sofa.ark.version.old>2.2.14</sofa.ark.version.old>
<sofa.ark.version>2.2.16</sofa.ark.version>
<sofa.ark.version.old>2.2.15</sofa.ark.version.old>
<project.encoding>UTF-8</project.encoding>
<java.version>1.8</java.version>
<license.maven.plugin>3.0</license.maven.plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public void run() {
}
}));
prepareArkConfig();
// don't remove this log print, add to init log space first before initialize ArkLogger
ArkLoggerFactory.getDefaultLogger().info("Ark container starting...");
reInitializeArkLogger();
arkServiceContainer.start();
Pipeline pipeline = arkServiceContainer.getService(Pipeline.class);
Expand Down Expand Up @@ -248,8 +250,8 @@ public List<URL> getProfileConfFiles(String... profiles) {
* @throws ArkRuntimeException
*/
public void reInitializeArkLogger() throws ArkRuntimeException {
for (Map.Entry<SpaceId, SpaceInfo> entry : MultiAppLoggerSpaceManager.getSpacesMap()
.entrySet()) {
for (Map.Entry<SpaceId, SpaceInfo> entry : ((Map<SpaceId, SpaceInfo>) MultiAppLoggerSpaceManager
.getSpacesMap()).entrySet()) {
SpaceId spaceId = entry.getKey();
SpaceInfo spaceInfo = entry.getValue();
if (!ArkLoggerFactory.SOFA_ARK_LOGGER_SPACE.equals(spaceId.getSpaceName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
import com.alipay.sofa.ark.exception.ArkRuntimeException;
import com.alipay.sofa.ark.loader.jar.JarUtils;
import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.spi.event.biz.AfterBizFailedEvent;
import com.alipay.sofa.ark.spi.event.biz.AfterBizStartupEvent;
import com.alipay.sofa.ark.spi.event.biz.AfterBizStartupFailedEvent;
import com.alipay.sofa.ark.spi.event.biz.AfterBizStopEvent;
import com.alipay.sofa.ark.spi.event.biz.AfterBizStopFailedEvent;
import com.alipay.sofa.ark.spi.event.biz.BeforeBizRecycleEvent;
import com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent;
import com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent;
Expand All @@ -44,6 +45,8 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -58,6 +61,8 @@
import java.util.concurrent.CopyOnWriteArrayList;

import static com.alipay.sofa.ark.spi.constant.Constants.BIZ_TEMP_WORK_DIR_RECYCLE_FILE_SUFFIX;
import static com.alipay.sofa.ark.spi.constant.Constants.ACTIVATE_MULTI_BIZ_VERSION_ENABLE;
import static com.alipay.sofa.ark.spi.constant.Constants.REMOVE_BIZ_INSTANCE_AFTER_STOP_FAILED;
import static org.apache.commons.io.FileUtils.deleteQuietly;

/**
Expand Down Expand Up @@ -333,6 +338,18 @@ public void start(String[] args, Map<String, String> envs) throws Throwable {

private void doStart(String[] args, Map<String, String> envs) throws Throwable {
AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED");

// support specify mainClass by env
if (envs != null) {
String mainClassFromEnv = envs.get(Constants.BIZ_MAIN_CLASS);
if (mainClassFromEnv != null) {
mainClass = mainClassFromEnv;
ArkLoggerFactory.getDefaultLogger().info(
"Ark biz {} will start with main class {} from envs", getIdentity(),
mainClassFromEnv);
}
}

if (mainClass == null) {
throw new ArkRuntimeException(String.format("biz: %s has no main method", getBizName()));
}
Expand All @@ -353,33 +370,40 @@ private void doStart(String[] args, Map<String, String> envs) throws Throwable {
getIdentity(), (System.currentTimeMillis() - start));
}
} catch (Throwable e) {
setBizState(BizState.BROKEN, StateChangeReason.FAILED, e.getMessage());
eventAdminService.sendEvent(new AfterBizFailedEvent(this, e));
setBizState(BizState.BROKEN, StateChangeReason.INSTALL_FAILED, getStackTraceAsString(e));
eventAdminService.sendEvent(new AfterBizStartupFailedEvent(this, e));
throw e;
} finally {
ClassLoaderUtils.popContextClassLoader(oldClassLoader);
}

BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(
BizManagerService.class);

// case0: active the first module as activated
if (bizManagerService.getActiveBiz(bizName) == null) {
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
return;
}

// case1: support multiple version biz as activated: always activate the new version and keep the old module activated
boolean activateMultiBizVersion = Boolean.parseBoolean(ArkConfigs.getStringValue(
ACTIVATE_MULTI_BIZ_VERSION_ENABLE, "false"));
if (activateMultiBizVersion) {
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
return;
}

// case2: always activate the new version and deactivate the old module according to ACTIVATE_NEW_MODULE config
if (Boolean.getBoolean(Constants.ACTIVATE_NEW_MODULE)) {
Biz currentActiveBiz = bizManagerService.getActiveBiz(bizName);
if (currentActiveBiz == null) {
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
} else {
((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED,
StateChangeReason.SWITCHED,
String.format("switch to new biz %s", getIdentity()));
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED,
String.format("switch from old biz: %s", currentActiveBiz.getIdentity()));
}
((BizModel) currentActiveBiz).setBizState(BizState.DEACTIVATED,
StateChangeReason.SWITCHED, String.format("switch to new biz %s", getIdentity()));
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED,
String.format("switch from old biz: %s", currentActiveBiz.getIdentity()));
} else {
if (bizManagerService.getActiveBiz(bizName) == null) {
setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
} else {
setBizState(BizState.DEACTIVATED, StateChangeReason.STARTED,
"start but is deactivated");
}
// case3: always deactivate the new version and keep old module activated according to ACTIVATE_NEW_MODULE config
setBizState(BizState.DEACTIVATED, StateChangeReason.STARTED, "start but is deactivated");
}
}

Expand All @@ -398,39 +422,54 @@ public void stop() {
}
EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(
EventAdminService.class);

boolean isStopFailed = false;
long start = System.currentTimeMillis();
try {
// this can trigger uninstall handler
long start = System.currentTimeMillis();
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} stops.", getIdentity());
eventAdminService.sendEvent(new BeforeBizStopEvent(this));
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} stopped in {} ms", getIdentity(),
(System.currentTimeMillis() - start));
} catch (Throwable t) {
// handle stop failed
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} stop failed in {} ms",
getIdentity(), (System.currentTimeMillis() - start));
isStopFailed = true;
setBizState(BizState.BROKEN, StateChangeReason.UN_INSTALL_FAILED);
eventAdminService.sendEvent(new AfterBizStopFailedEvent(this, t));
throw t;
} finally {
BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer()
.getService(BizManagerService.class);
bizManagerService.unRegisterBiz(bizName, bizVersion);
setBizState(BizState.UNRESOLVED, StateChangeReason.STOPPED);
eventAdminService.sendEvent(new BeforeBizRecycleEvent(this));
urls = null;
denyImportPackages = null;
denyImportClasses = null;
denyImportResources = null;
// close classloader
if (classLoader instanceof AbstractClasspathClassLoader) {
try {
((AbstractClasspathClassLoader) classLoader).close();
((AbstractClasspathClassLoader) classLoader).clearCache();
} catch (IOException e) {
ArkLoggerFactory.getDefaultLogger().warn(
"Ark biz {} close biz classloader fail", getIdentity());
boolean removeInstanceAfterStopFailed = Boolean.parseBoolean(ArkConfigs.getStringValue(
REMOVE_BIZ_INSTANCE_AFTER_STOP_FAILED, "true"));
// 只有成功后才清理, 或者失败后允许清理的情况,失败后如果不允许情况则不执行清理

if (!isStopFailed || (isStopFailed && removeInstanceAfterStopFailed)) {
BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer()
.getService(BizManagerService.class);
bizManagerService.unRegisterBiz(bizName, bizVersion);
setBizState(BizState.STOPPED, StateChangeReason.STOPPED);
eventAdminService.sendEvent(new BeforeBizRecycleEvent(this));
urls = null;
denyImportPackages = null;
denyImportClasses = null;
denyImportResources = null;
// close classloader
if (classLoader instanceof AbstractClasspathClassLoader) {
try {
((AbstractClasspathClassLoader) classLoader).close();
((AbstractClasspathClassLoader) classLoader).clearCache();
} catch (IOException e) {
ArkLoggerFactory.getDefaultLogger().warn(
"Ark biz {} close biz classloader fail", getIdentity());
}
}
eventAdminService.sendEvent(new AfterBizStopEvent(this));
eventAdminService.unRegister(classLoader);
classLoader = null;
recycleBizTempWorkDir(bizTempWorkDir);
bizTempWorkDir = null;
}

eventAdminService.sendEvent(new AfterBizStopEvent(this));
eventAdminService.unRegister(classLoader);
classLoader = null;
recycleBizTempWorkDir(bizTempWorkDir);
bizTempWorkDir = null;
ClassLoaderUtils.popContextClassLoader(oldClassLoader);
}
}
Expand Down Expand Up @@ -631,6 +670,13 @@ private static String markBizTempWorkDirRecycled(File bizTempWorkDir) throws IOE
return targetPath;
}

private static String getStackTraceAsString(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
return sw.toString();
}

/* export class and classloader relationship cache */
private ConcurrentHashMap<String, Plugin> exportClassAndClassLoaderMap = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, Plugin> exportNodeAndClassLoaderMap = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,7 @@
import java.util.jar.Attributes;
import java.util.stream.Stream;

import static com.alipay.sofa.ark.spi.constant.Constants.ARK_BIZ_NAME;
import static com.alipay.sofa.ark.spi.constant.Constants.ARK_BIZ_VERSION;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_CLASSES;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_PACKAGES;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_RESOURCES;
import static com.alipay.sofa.ark.spi.constant.Constants.INJECT_EXPORT_PACKAGES;
import static com.alipay.sofa.ark.spi.constant.Constants.INJECT_PLUGIN_DEPENDENCIES;
import static com.alipay.sofa.ark.spi.constant.Constants.MAIN_CLASS_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.MASTER_BIZ;
import static com.alipay.sofa.ark.spi.constant.Constants.PRIORITY_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.START_CLASS_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.WEB_CONTEXT_PATH;
import static com.alipay.sofa.ark.spi.constant.Constants.DECLARED_LIBRARIES;
import static com.alipay.sofa.ark.spi.constant.Constants.*;

/**
* {@link BizFactoryService}
Expand Down Expand Up @@ -187,7 +175,9 @@ public Biz createEmbedMasterBiz(ClassLoader masterClassLoader) {

private BizArchive prepareBizArchive(File file) throws IOException {
BizArchive bizArchive;
if (ArkConfigs.isEmbedEnable()) {
boolean unpackBizWhenInstall = Boolean.parseBoolean(ArkConfigs.getStringValue(
UNPACK_BIZ_WHEN_INSTALL, "true"));
if (ArkConfigs.isEmbedEnable() && unpackBizWhenInstall) {
File unpackFile = FileUtils.file(file.getAbsolutePath() + "-unpack");
if (!unpackFile.exists()) {
unpackFile = FileUtils.unzip(file, file.getAbsolutePath() + "-unpack");
Expand Down
Loading

0 comments on commit f6eb235

Please sign in to comment.