Skip to content

Commit

Permalink
feat: support multiple version module as activated (#1018)
Browse files Browse the repository at this point in the history
* feat: support multiple version module as activated
  • Loading branch information
gaosaroma authored Oct 24, 2024
1 parent 9ce12e6 commit 7c40ec7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
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 @@ -348,27 +349,34 @@ private void doStart(String[] args, Map<String, String> envs) throws Throwable {
} 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static com.alipay.sofa.ark.spi.constant.Constants.AUTO_UNINSTALL_WHEN_FAILED_ENABLE;
import static com.alipay.sofa.ark.spi.constant.Constants.CONFIG_BIZ_URL;
import static com.alipay.sofa.ark.spi.constant.Constants.EMBED_ENABLE;
import static com.alipay.sofa.ark.spi.constant.Constants.ACTIVATE_MULTI_BIZ_VERSION_ENABLE;
import static com.alipay.sofa.ark.spi.model.BizOperation.OperationType.CHECK;
import static com.alipay.sofa.ark.spi.model.BizOperation.OperationType.INSTALL;
import static com.alipay.sofa.ark.spi.model.BizOperation.OperationType.SWITCH;
Expand All @@ -70,6 +71,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -89,6 +91,8 @@ public class ArkClientTest extends BaseTest {
private URL bizUrl2;
// bizName=biz-demo, bizVersion=3.0.0
private URL bizUrl3;
// bizName=biz-demo, bizVersion=4.0.0
private URL bizUrl4;

@Before
public void before() {
Expand All @@ -99,6 +103,8 @@ public void before() {
bizUrl2 = this.getClass().getClassLoader().getResource("sample-ark-2.0.0-ark-biz.jar");
// bizName=biz-demo, bizVersion=3.0.0
bizUrl3 = this.getClass().getClassLoader().getResource("sample-ark-3.0.0-ark-biz.jar");
// bizName=biz-demo, bizVersion=4.0.0
bizUrl4 = this.getClass().getClassLoader().getResource("sample-ark-4.0.0-ark-biz.jar");
}

@Test
Expand Down Expand Up @@ -150,6 +156,17 @@ public void testInstallBiz() throws Throwable {
assertEquals(SUCCESS, response.getCode());
bizInfo = response.getBizInfos().iterator().next();
assertEquals(ACTIVATED, bizInfo.getBizState());

// test install biz with same bizName and different bizVersion and keep old module state
setProperty(ACTIVATE_MULTI_BIZ_VERSION_ENABLE, "true");
File bizFile4 = createBizSaveFile("biz-demo", "4.0.0");
copyInputStreamToFile(bizUrl4.openStream(), bizFile4);
response = installBiz(bizFile4);
assertEquals(SUCCESS, response.getCode());
BizManagerService bizManagerService = arkServiceContainer
.getService(BizManagerService.class);
assertSame(bizManagerService.getBiz("biz-demo", "3.0.0").getBizState(), ACTIVATED);
setProperty(ACTIVATE_MULTI_BIZ_VERSION_ENABLE, "");
}

@Test
Expand Down Expand Up @@ -197,12 +214,12 @@ public void testCheckBiz() throws Throwable {
// test check all biz
ClientResponse response = checkBiz();
assertEquals(SUCCESS, response.getCode());
assertEquals(3, response.getBizInfos().size());
assertEquals(4, response.getBizInfos().size());

// test check specified bizName
response = checkBiz("biz-demo");
assertEquals(SUCCESS, response.getCode());
assertEquals(3, response.getBizInfos().size());
assertEquals(4, response.getBizInfos().size());

// test check specified bizName and version
response = checkBiz("biz-demo", "2.0.0");
Expand All @@ -214,6 +231,10 @@ public void testCheckBiz() throws Throwable {

response = checkBiz("biz-demo", "4.0.0");
assertEquals(SUCCESS, response.getCode());
assertEquals(1, response.getBizInfos().size());

response = checkBiz("biz-demo", "5.0.0");
assertEquals(SUCCESS, response.getCode());
assertEquals(0, response.getBizInfos().size());
}

Expand All @@ -228,7 +249,7 @@ public void testUninstallBiz() throws Throwable {
// test check all biz
response = checkBiz();
assertEquals(SUCCESS, response.getCode());
assertEquals(2, response.getBizInfos().size());
assertEquals(3, response.getBizInfos().size());
}

@Test
Expand All @@ -242,7 +263,7 @@ public void testUninstallBizWhenIncludeLib() throws Throwable {
// test check all biz
response = checkBiz();
assertEquals(SUCCESS, response.getCode());
assertEquals(2, response.getBizInfos().size());
assertEquals(3, response.getBizInfos().size());
}

@Test
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ public class Constants {
* uninstall the biz if it starts up failed
*/
public final static String AUTO_UNINSTALL_WHEN_FAILED_ENABLE = "sofa.ark.auto.uninstall.when.failed.enable";

/**
* support multiple version biz as activated
*/
public final static String ACTIVATE_MULTI_BIZ_VERSION_ENABLE = "sofa.ark.activate.multi.biz.version.enable";

/**
* auto remove the biz instance in BizManagerService if it stops failed
*/
Expand Down

0 comments on commit 7c40ec7

Please sign in to comment.