Search in sources :

Example 16 with Release

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

the class ReleaseService method rollback.

@Transactional
public Release rollback(long releaseId, String operator) {
    Release release = findOne(releaseId);
    if (release == null) {
        throw new NotFoundException("release not found");
    }
    if (release.isAbandoned()) {
        throw new BadRequestException("release is not active");
    }
    String appId = release.getAppId();
    String clusterName = release.getClusterName();
    String namespaceName = release.getNamespaceName();
    PageRequest page = PageRequest.of(0, 2);
    List<Release> twoLatestActiveReleases = findActiveReleases(appId, clusterName, namespaceName, page);
    if (twoLatestActiveReleases == null || twoLatestActiveReleases.size() < 2) {
        throw new BadRequestException(String.format("Can't rollback namespace(appId=%s, clusterName=%s, namespaceName=%s) because there is only one active release", appId, clusterName, namespaceName));
    }
    release.setAbandoned(true);
    release.setDataChangeLastModifiedBy(operator);
    releaseRepository.save(release);
    releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, clusterName, twoLatestActiveReleases.get(1).getId(), release.getId(), ReleaseOperation.ROLLBACK, null, operator);
    // publish child namespace if namespace has child
    rollbackChildNamespace(appId, clusterName, namespaceName, twoLatestActiveReleases, operator);
    return release;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with Release

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

the class NamespaceService method isNamespaceNotPublished.

private boolean isNamespaceNotPublished(Namespace namespace) {
    Release latestRelease = releaseService.findLatestActiveRelease(namespace);
    long namespaceId = namespace.getId();
    if (latestRelease == null) {
        Item lastItem = itemService.findLastOne(namespaceId);
        return lastItem != null;
    }
    Date lastPublishTime = latestRelease.getDataChangeLastModifiedTime();
    List<Item> itemsModifiedAfterLastPublish = itemService.findItemsModifiedAfterDate(namespaceId, lastPublishTime);
    if (CollectionUtils.isEmpty(itemsModifiedAfterLastPublish)) {
        return false;
    }
    Map<String, String> publishedConfiguration = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
    for (Item item : itemsModifiedAfterLastPublish) {
        if (!Objects.equals(item.getValue(), publishedConfiguration.get(item.getKey()))) {
            return true;
        }
    }
    return false;
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) Release(com.ctrip.framework.apollo.biz.entity.Release) Date(java.util.Date)

Example 18 with Release

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

the class NamespaceService method findPublicNamespaceForAssociatedNamespace.

public Namespace findPublicNamespaceForAssociatedNamespace(String clusterName, String namespaceName) {
    AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
    if (appNamespace == null) {
        throw new BadRequestException("namespace not exist");
    }
    String appId = appNamespace.getAppId();
    Namespace namespace = findOne(appId, clusterName, namespaceName);
    // default cluster's namespace
    if (Objects.equals(clusterName, ConfigConsts.CLUSTER_NAME_DEFAULT)) {
        return namespace;
    }
    // return default cluster's namespace
    if (namespace == null) {
        return findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
    }
    // custom cluster's namespace exist and has published.
    // return custom cluster's namespace
    Release latestActiveRelease = releaseService.findLatestActiveRelease(namespace);
    if (latestActiveRelease != null) {
        return namespace;
    }
    Namespace defaultNamespace = findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
    // return custom cluster's namespace
    if (defaultNamespace == null) {
        return namespace;
    }
    // custom cluster's namespace exist but never published.
    // and default cluster's namespace exist and has published.
    // return default cluster's namespace
    Release defaultNamespaceLatestActiveRelease = releaseService.findLatestActiveRelease(defaultNamespace);
    if (defaultNamespaceLatestActiveRelease != null) {
        return defaultNamespace;
    }
    // return custom cluster's namespace
    return namespace;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 19 with Release

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

the class NamespaceUnlockAspect method isModified.

boolean isModified(Namespace namespace) {
    Release release = releaseService.findLatestActiveRelease(namespace);
    List<Item> items = itemService.findItemsWithoutOrdered(namespace.getId());
    if (release == null) {
        return hasNormalItems(items);
    }
    Map<String, String> releasedConfiguration = GSON.fromJson(release.getConfigurations(), GsonType.CONFIG);
    Map<String, String> configurationFromItems = generateConfigurationFromItems(namespace, items);
    MapDifference<String, String> difference = Maps.difference(releasedConfiguration, configurationFromItems);
    return !difference.areEqual();
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 20 with Release

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

the class NamespaceUnlockAspect method generateConfigurationFromItems.

private Map<String, String> generateConfigurationFromItems(Namespace namespace, List<Item> namespaceItems) {
    Map<String, String> configurationFromItems = Maps.newHashMap();
    Namespace parentNamespace = namespaceService.findParentNamespace(namespace);
    // parent namespace
    if (parentNamespace == null) {
        generateMapFromItems(namespaceItems, configurationFromItems);
    } else {
        // child namespace
        Release parentRelease = releaseService.findLatestActiveRelease(parentNamespace);
        if (parentRelease != null) {
            configurationFromItems = GSON.fromJson(parentRelease.getConfigurations(), GsonType.CONFIG);
        }
        generateMapFromItems(namespaceItems, configurationFromItems);
    }
    return configurationFromItems;
}
Also used : Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Release(com.ctrip.framework.apollo.biz.entity.Release)

Aggregations

Release (com.ctrip.framework.apollo.biz.entity.Release)71 Test (org.junit.Test)36 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)22 Item (com.ctrip.framework.apollo.biz.entity.Item)12 GrayReleaseRule (com.ctrip.framework.apollo.biz.entity.GrayReleaseRule)9 Transactional (org.springframework.transaction.annotation.Transactional)9 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)7 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)7 ReleaseHistory (com.ctrip.framework.apollo.biz.entity.ReleaseHistory)7 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)7 Sql (org.springframework.test.context.jdbc.Sql)7 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)5 Instance (com.ctrip.framework.apollo.biz.entity.Instance)4 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)4 Date (java.util.Date)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)3 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 PageRequest (org.springframework.data.domain.PageRequest)3