Search in sources :

Example 6 with GrayReleaseRule

use of com.ctrip.framework.apollo.biz.entity.GrayReleaseRule in project apollo by ctripcorp.

the class ReleaseCreationTest method testPublishMasterNamespaceAndBranchHasNotItems.

/**
 *               Master     |      Branch
 *           ------------------------------                                      Master    |    Branch
 *     Items      k1=v1     |                                                 ----------------------------
 *                k2=v2     |                                                     k1=v1    |    k1=v1
 *                k3=v3                            publish master                 k2=v2    |    k2=v2
 *           ------------------------------        ===========>>      Result      k3=v3    |    k3=v3
 *    Release               |
 *                          |
 *                          |
 */
@Test
@Sql(scripts = "/sql/release-creation-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testPublishMasterNamespaceAndBranchHasNotItems() {
    long parentNamespaceId = 101;
    String parentClusterName = "default1";
    long childNamespaceId = 102;
    String childClusterName = "child-cluster1";
    Namespace parentNamespace = instanceNamespace(parentNamespaceId, parentClusterName);
    releaseService.publish(parentNamespace, "", "", operator, false);
    Release latestParentNamespaceRelease = releaseService.findLatestActiveRelease(parentNamespace);
    // assert parent namespace
    Assert.assertNotNull(latestParentNamespaceRelease);
    Map<String, String> parentNamespaceConfiguration = parseConfiguration(latestParentNamespaceRelease.getConfigurations());
    Assert.assertEquals(3, parentNamespaceConfiguration.size());
    Assert.assertEquals("v1", parentNamespaceConfiguration.get("k1"));
    Assert.assertEquals("v2", parentNamespaceConfiguration.get("k2"));
    Assert.assertEquals("v3", parentNamespaceConfiguration.get("k3"));
    // assert child namespace
    Namespace childNamespace = instanceNamespace(childNamespaceId, childClusterName);
    Release latestChildNamespaceRelease = releaseService.findLatestActiveRelease(childNamespace);
    // assert parent namespace
    Assert.assertNotNull(latestChildNamespaceRelease);
    Map<String, String> childNamespaceConfiguration = parseConfiguration(latestChildNamespaceRelease.getConfigurations());
    Assert.assertEquals(3, childNamespaceConfiguration.size());
    Assert.assertEquals("v1", childNamespaceConfiguration.get("k1"));
    Assert.assertEquals("v2", childNamespaceConfiguration.get("k2"));
    Assert.assertEquals("v3", childNamespaceConfiguration.get("k3"));
    GrayReleaseRule rule = namespaceBranchService.findBranchGrayRules(testApp, parentClusterName, testNamespace, childClusterName);
    Assert.assertNotNull(rule);
    Assert.assertEquals(1, rule.getBranchStatus());
    Assert.assertEquals(Long.valueOf(latestChildNamespaceRelease.getId()), rule.getReleaseId());
    // assert release history
    Page<ReleaseHistory> releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, parentClusterName, testNamespace, pageable);
    ReleaseHistory masterReleaseHistory = releaseHistories.getContent().get(1);
    ReleaseHistory branchReleaseHistory = releaseHistories.getContent().get(0);
    Assert.assertEquals(2, releaseHistories.getTotalElements());
    Assert.assertEquals(ReleaseOperation.NORMAL_RELEASE, masterReleaseHistory.getOperation());
    Assert.assertEquals(latestParentNamespaceRelease.getId(), masterReleaseHistory.getReleaseId());
    Assert.assertEquals(0, masterReleaseHistory.getPreviousReleaseId());
    Assert.assertEquals(ReleaseOperation.MASTER_NORMAL_RELEASE_MERGE_TO_GRAY, branchReleaseHistory.getOperation());
    Assert.assertEquals(latestChildNamespaceRelease.getId(), branchReleaseHistory.getReleaseId());
    Assert.assertTrue(branchReleaseHistory.getOperationContext().contains(String.format("\"baseReleaseId\":%d", latestParentNamespaceRelease.getId())));
    Assert.assertTrue(branchReleaseHistory.getOperationContext().contains(rule.getRules()));
}
Also used : ReleaseHistory(com.ctrip.framework.apollo.biz.entity.ReleaseHistory) GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release) Test(org.junit.Test) AbstractIntegrationTest(com.ctrip.framework.apollo.biz.AbstractIntegrationTest) Sql(org.springframework.test.context.jdbc.Sql)

Example 7 with GrayReleaseRule

use of com.ctrip.framework.apollo.biz.entity.GrayReleaseRule in project apollo by ctripcorp.

the class NamespaceBranchService method deleteBranch.

@Transactional
public void deleteBranch(String appId, String clusterName, String namespaceName, String branchName, int branchStatus, String operator) {
    Cluster toDeleteCluster = clusterService.findOne(appId, branchName);
    if (toDeleteCluster == null) {
        return;
    }
    Release latestBranchRelease = releaseService.findLatestActiveRelease(appId, branchName, namespaceName);
    long latestBranchReleaseId = latestBranchRelease != null ? latestBranchRelease.getId() : 0;
    // update branch rules
    GrayReleaseRule deleteRule = new GrayReleaseRule();
    deleteRule.setRules("[]");
    deleteRule.setAppId(appId);
    deleteRule.setClusterName(clusterName);
    deleteRule.setNamespaceName(namespaceName);
    deleteRule.setBranchName(branchName);
    deleteRule.setBranchStatus(branchStatus);
    deleteRule.setDataChangeLastModifiedBy(operator);
    deleteRule.setDataChangeCreatedBy(operator);
    doUpdateBranchGrayRules(appId, clusterName, namespaceName, branchName, deleteRule, false, -1);
    // delete branch cluster
    clusterService.delete(toDeleteCluster.getId(), operator);
    int releaseOperation = branchStatus == NamespaceBranchStatus.MERGED ? ReleaseOperation.GRAY_RELEASE_DELETED_AFTER_MERGE : ReleaseOperation.ABANDON_GRAY_RELEASE;
    releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, branchName, latestBranchReleaseId, latestBranchReleaseId, releaseOperation, null, operator);
    auditService.audit("Branch", toDeleteCluster.getId(), Audit.OP.DELETE, operator);
}
Also used : Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with GrayReleaseRule

use of com.ctrip.framework.apollo.biz.entity.GrayReleaseRule in project apollo by ctripcorp.

the class GrayReleaseRulesHolderTest method assembleGrayReleaseRule.

private GrayReleaseRule assembleGrayReleaseRule(String appId, String clusterName, String namespaceName, List<GrayReleaseRuleItemDTO> ruleItems, long releaseId, int branchStatus) {
    GrayReleaseRule rule = new GrayReleaseRule();
    rule.setId(idCounter.incrementAndGet());
    rule.setAppId(appId);
    rule.setClusterName(clusterName);
    rule.setNamespaceName(namespaceName);
    rule.setBranchName("someBranch");
    rule.setRules(GSON.toJson(ruleItems));
    rule.setReleaseId(releaseId);
    rule.setBranchStatus(branchStatus);
    return rule;
}
Also used : GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)

Example 9 with GrayReleaseRule

use of com.ctrip.framework.apollo.biz.entity.GrayReleaseRule in project apollo by ctripcorp.

the class GrayReleaseRulesHolder method mergeGrayReleaseRules.

private void mergeGrayReleaseRules(List<GrayReleaseRule> grayReleaseRules) {
    if (CollectionUtils.isEmpty(grayReleaseRules)) {
        return;
    }
    for (GrayReleaseRule grayReleaseRule : grayReleaseRules) {
        if (grayReleaseRule.getReleaseId() == null || grayReleaseRule.getReleaseId() == 0) {
            // filter rules with no release id, i.e. never released
            continue;
        }
        String key = assembleGrayReleaseRuleKey(grayReleaseRule.getAppId(), grayReleaseRule.getClusterName(), grayReleaseRule.getNamespaceName());
        // create a new list to avoid ConcurrentModificationException
        List<GrayReleaseRuleCache> rules = Lists.newArrayList(grayReleaseRuleCache.get(key));
        GrayReleaseRuleCache oldRule = null;
        for (GrayReleaseRuleCache ruleCache : rules) {
            if (ruleCache.getBranchName().equals(grayReleaseRule.getBranchName())) {
                oldRule = ruleCache;
                break;
            }
        }
        // if old rule is null and new rule's branch status is not active, ignore
        if (oldRule == null && grayReleaseRule.getBranchStatus() != NamespaceBranchStatus.ACTIVE) {
            continue;
        }
        // use id comparison to avoid synchronization
        if (oldRule == null || grayReleaseRule.getId() > oldRule.getRuleId()) {
            addCache(key, transformRuleToRuleCache(grayReleaseRule));
            if (oldRule != null) {
                removeCache(key, oldRule);
            }
        } else {
            if (oldRule.getBranchStatus() == NamespaceBranchStatus.ACTIVE) {
                // update load version
                oldRule.setLoadVersion(loadVersion.get());
            } else if ((loadVersion.get() - oldRule.getLoadVersion()) > 1) {
                // remove outdated inactive branch rule after 2 update cycles
                removeCache(key, oldRule);
            }
        }
    }
}
Also used : GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)

Example 10 with GrayReleaseRule

use of com.ctrip.framework.apollo.biz.entity.GrayReleaseRule in project apollo by ctripcorp.

the class NamespaceBranchServiceTest method testDeleteBranch.

@Test
@Sql(scripts = "/sql/namespace-branch-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testDeleteBranch() {
    GrayReleaseRule rule = instanceGrayReleaseRule();
    namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, rule);
    namespaceBranchService.deleteBranch(testApp, testCluster, testNamespace, testBranchName, NamespaceBranchStatus.DELETED, operator);
    Namespace branch = namespaceBranchService.findBranch(testApp, testCluster, testNamespace);
    Assert.assertNull(branch);
    GrayReleaseRule latestRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName);
    Assert.assertNotNull(latestRule);
    Assert.assertEquals(NamespaceBranchStatus.DELETED, latestRule.getBranchStatus());
    Assert.assertEquals("[]", latestRule.getRules());
    Page<ReleaseHistory> releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, testNamespace, pageable);
    ReleaseHistory firstReleaseHistory = releaseHistories.getContent().get(1);
    ReleaseHistory secondReleaseHistory = releaseHistories.getContent().get(0);
    Assert.assertEquals(2, releaseHistories.getTotalElements());
    Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, firstReleaseHistory.getOperation());
    Assert.assertEquals(ReleaseOperation.ABANDON_GRAY_RELEASE, secondReleaseHistory.getOperation());
}
Also used : ReleaseHistory(com.ctrip.framework.apollo.biz.entity.ReleaseHistory) GrayReleaseRule(com.ctrip.framework.apollo.biz.entity.GrayReleaseRule) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Test(org.junit.Test) AbstractIntegrationTest(com.ctrip.framework.apollo.biz.AbstractIntegrationTest) Sql(org.springframework.test.context.jdbc.Sql)

Aggregations

GrayReleaseRule (com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)21 Test (org.junit.Test)11 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)10 Sql (org.springframework.test.context.jdbc.Sql)10 Release (com.ctrip.framework.apollo.biz.entity.Release)9 ReleaseHistory (com.ctrip.framework.apollo.biz.entity.ReleaseHistory)8 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)6 Transactional (org.springframework.transaction.annotation.Transactional)3 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)1 GrayReleaseRuleDTO (com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1