Search in sources :

Example 1 with BizState

use of com.alipay.sofa.ark.spi.model.BizState in project sofa-ark by alipay.

the class OperationTransformer method doTransformToBizOperation.

public static List<BizOperation> doTransformToBizOperation(String config, Map<String, Map<String, BizState>> currentBizState) throws IllegalStateException {
    List<BizOperation> bizOperations = new ArrayList<>();
    Map<String, Map<String, BizState>> expectedBizState = new HashMap<>();
    Map<String, Map<String, BizState>> progressBizState = cloneBizStateMap(currentBizState);
    ArrayList<String> configOperation = adjustOperationOrder(config);
    for (String operation : configOperation) {
        int idx = operation.indexOf(Constants.QUESTION_MARK_SPLIT);
        String[] configInfo = (idx == -1) ? operation.split(Constants.STRING_COLON) : operation.substring(0, idx).split(Constants.STRING_COLON);
        String bizName = configInfo[0];
        String bizVersion = configInfo[1];
        String stateStr = configInfo[2];
        String parameterStr = (idx == -1) ? Constants.EMPTY_STR : operation.substring(idx + 1);
        BizState bizState = BizState.of(stateStr);
        Map<String, String> parameters = parseParameter(parameterStr);
        if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).get(bizVersion) == bizState) {
            continue;
        } else if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).get(bizVersion) != null && expectedBizState.get(bizName).get(bizVersion) != bizState) {
            throw new IllegalStateException(String.format("Don't specify same biz with different bizState, config is %s.", config));
        } else if (expectedBizState.get(bizName) != null && expectedBizState.get(bizName).containsValue(BizState.ACTIVATED) && bizState == BizState.ACTIVATED) {
            throw new IllegalStateException(String.format("Don't allow multi biz with same bizName to be active, config is %s. ", config));
        }
        if (bizState == BizState.ACTIVATED) {
            if (progressBizState.get(bizName) != null && progressBizState.get(bizName).get(bizVersion) != null) {
                // switch operation
                if (progressBizState.get(bizName).get(bizVersion).equals(BizState.DEACTIVATED)) {
                    BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.SWITCH).setParameters(parameters);
                    bizOperations.add(bizOperation);
                    transformBizState(progressBizState, BizOperation.OperationType.SWITCH, bizName, bizVersion);
                }
            } else {
                // install operation
                BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.INSTALL).setParameters(parameters);
                bizOperations.add(bizOperation);
                if (progressBizState.get(bizName) != null && progressBizState.get(bizName).containsValue(BizState.ACTIVATED)) {
                    // add switch
                    bizOperations.add(BizOperation.createBizOperation().setOperationType(BizOperation.OperationType.SWITCH).setBizName(bizName).setBizVersion(bizVersion));
                    transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
                    transformBizState(progressBizState, BizOperation.OperationType.SWITCH, bizName, bizVersion);
                } else {
                    transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
                }
            }
        } else {
            if (progressBizState.get(bizName) != null && progressBizState.get(bizName).get(bizVersion) == null && progressBizState.get(bizName).containsValue(BizState.ACTIVATED)) {
                BizOperation bizOperation = BizOperation.createBizOperation().setBizName(bizName).setBizVersion(bizVersion).setOperationType(BizOperation.OperationType.INSTALL).setParameters(parameters);
                bizOperations.add(bizOperation);
                transformBizState(progressBizState, BizOperation.OperationType.INSTALL, bizName, bizVersion);
            } else if (progressBizState.get(bizName) == null || !BizState.DEACTIVATED.equals(progressBizState.get(bizName).get(bizVersion))) {
                throw new IllegalStateException(String.format("Biz(%s:%s) cant be transform to %s, config is %s.", bizName, bizVersion, bizState, config));
            }
        }
        if (expectedBizState.get(bizName) == null) {
            expectedBizState.put(bizName, new HashMap<String, BizState>());
        }
        expectedBizState.get(bizName).put(bizVersion, bizState);
    }
    for (String bizName : currentBizState.keySet()) {
        for (String bizVersion : currentBizState.get(bizName).keySet()) {
            if (expectedBizState.get(bizName) == null || !expectedBizState.get(bizName).containsKey(bizVersion)) {
                bizOperations.add(BizOperation.createBizOperation().setOperationType(BizOperation.OperationType.UNINSTALL).setBizName(bizName).setBizVersion(bizVersion));
                transformBizState(progressBizState, BizOperation.OperationType.UNINSTALL, bizName, bizVersion);
            }
        }
    }
    // double check
    if (!checkBizState(expectedBizState, progressBizState)) {
        throw new IllegalStateException(String.format("Failed to transform biz operation, config is %s.", config));
    }
    return bizOperations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BizState(com.alipay.sofa.ark.spi.model.BizState) BizOperation(com.alipay.sofa.ark.spi.model.BizOperation) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with BizState

use of com.alipay.sofa.ark.spi.model.BizState in project sofa-ark by alipay.

the class OperationTransformer method adjustOperationOrder.

/**
 * prefer to handle activated state
 *
 * @param config
 */
public static ArrayList<String> adjustOperationOrder(String config) {
    ArrayList<String> activatedStateConfig = new ArrayList<>();
    ArrayList<String> deactivatedStateConfig = new ArrayList<>();
    for (String configOperation : config.split(Constants.STRING_SEMICOLON)) {
        if (StringUtils.isEmpty(configOperation)) {
            continue;
        }
        int idx = configOperation.indexOf(Constants.QUESTION_MARK_SPLIT);
        String[] configInfo = (idx == -1) ? configOperation.split(Constants.STRING_COLON) : configOperation.substring(0, idx).split(Constants.STRING_COLON);
        BizState bizState = BizState.of(configInfo[2]);
        if (BizState.ACTIVATED.equals(bizState)) {
            activatedStateConfig.add(configOperation);
        } else {
            deactivatedStateConfig.add(configOperation);
        }
    }
    activatedStateConfig.addAll(deactivatedStateConfig);
    return activatedStateConfig;
}
Also used : ArrayList(java.util.ArrayList) BizState(com.alipay.sofa.ark.spi.model.BizState)

Example 3 with BizState

use of com.alipay.sofa.ark.spi.model.BizState in project sofa-ark by alipay.

the class BizManagerServiceTest method testBizGet.

@Test
public void testBizGet() {
    Biz biz = bizManagerService.getBizByIdentity("test-biz:1.0.0");
    Assert.assertNotNull(biz);
    Set<String> stringSet = bizManagerService.getAllBizNames();
    Assert.assertTrue(stringSet.size() == 1);
    Assert.assertTrue(stringSet.contains("test-biz"));
    biz = bizManagerService.getActiveBiz("test-biz");
    Assert.assertNull(biz);
    BizState bizState = bizManagerService.getBizState("test-biz:1.0.0");
    Assert.assertTrue(bizState == BizState.RESOLVED);
    bizState = bizManagerService.getBizState("test-biz", "1.0.0");
    Assert.assertTrue(bizState == BizState.RESOLVED);
    bizState = bizManagerService.getBizState("ss", "xx");
    Assert.assertTrue(bizState == BizState.UNRESOLVED);
    biz = new BizModel().setBizName("test-biz").setBizVersion("1.0.1").setBizState(BizState.RESOLVED).setPriority("10");
    bizManagerService.registerBiz(biz);
    List<Biz> bizList = bizManagerService.getBizInOrder();
    Assert.assertTrue(bizList.size() == 2);
    Assert.assertTrue(bizList.get(0).getBizVersion().equals("1.0.1"));
    Assert.assertTrue(bizList.get(1).getBizVersion().equals("1.0.0"));
    biz = bizManagerService.getActiveBiz("test-biz");
    Assert.assertNull(biz);
    bizManagerService.activeBiz("test-biz", "1.0.1");
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.1") == BizState.RESOLVED);
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.0") == BizState.RESOLVED);
    biz = bizManagerService.getBiz("test-biz", "1.0.1");
    ((BizModel) biz).setBizState(BizState.DEACTIVATED);
    bizManagerService.activeBiz("test-biz", "1.0.1");
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.1") == BizState.ACTIVATED);
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.0") == BizState.RESOLVED);
    bizManagerService.activeBiz("test-biz", "1.0.0");
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.1") == BizState.ACTIVATED);
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.0") == BizState.RESOLVED);
    biz = bizManagerService.getBiz("test-biz", "1.0.0");
    ((BizModel) biz).setBizState(BizState.DEACTIVATED);
    bizManagerService.activeBiz("test-biz", "1.0.0");
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.0") == BizState.ACTIVATED);
    Assert.assertTrue(bizManagerService.getBizState("test-biz", "1.0.1") == BizState.DEACTIVATED);
}
Also used : Biz(com.alipay.sofa.ark.spi.model.Biz) BizModel(com.alipay.sofa.ark.container.model.BizModel) BizState(com.alipay.sofa.ark.spi.model.BizState) BaseTest(com.alipay.sofa.ark.container.BaseTest) Test(org.junit.Test)

Aggregations

BizState (com.alipay.sofa.ark.spi.model.BizState)3 ArrayList (java.util.ArrayList)2 BaseTest (com.alipay.sofa.ark.container.BaseTest)1 BizModel (com.alipay.sofa.ark.container.model.BizModel)1 Biz (com.alipay.sofa.ark.spi.model.Biz)1 BizOperation (com.alipay.sofa.ark.spi.model.BizOperation)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1