Search in sources :

Example 31 with Release

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

the class ConfigServiceWithCache method initialize.

@PostConstruct
void initialize() {
    configCache = CacheBuilder.newBuilder().expireAfterAccess(DEFAULT_EXPIRED_AFTER_ACCESS_IN_MINUTES, TimeUnit.MINUTES).build(new CacheLoader<String, ConfigCacheEntry>() {

        @Override
        public ConfigCacheEntry load(String key) throws Exception {
            List<String> namespaceInfo = STRING_SPLITTER.splitToList(key);
            if (namespaceInfo.size() != 3) {
                Tracer.logError(new IllegalArgumentException(String.format("Invalid cache load key %s", key)));
                return nullConfigCacheEntry;
            }
            Transaction transaction = Tracer.newTransaction(TRACER_EVENT_CACHE_LOAD, key);
            try {
                ReleaseMessage latestReleaseMessage = releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(key));
                Release latestRelease = releaseService.findLatestActiveRelease(namespaceInfo.get(0), namespaceInfo.get(1), namespaceInfo.get(2));
                transaction.setStatus(Transaction.SUCCESS);
                long notificationId = latestReleaseMessage == null ? ConfigConsts.NOTIFICATION_ID_PLACEHOLDER : latestReleaseMessage.getId();
                if (notificationId == ConfigConsts.NOTIFICATION_ID_PLACEHOLDER && latestRelease == null) {
                    return nullConfigCacheEntry;
                }
                return new ConfigCacheEntry(notificationId, latestRelease);
            } catch (Throwable ex) {
                transaction.setStatus(ex);
                throw ex;
            } finally {
                transaction.complete();
            }
        }
    });
    configIdCache = CacheBuilder.newBuilder().expireAfterAccess(DEFAULT_EXPIRED_AFTER_ACCESS_IN_MINUTES, TimeUnit.MINUTES).build(new CacheLoader<Long, Optional<Release>>() {

        @Override
        public Optional<Release> load(Long key) throws Exception {
            Transaction transaction = Tracer.newTransaction(TRACER_EVENT_CACHE_LOAD_ID, String.valueOf(key));
            try {
                Release release = releaseService.findActiveOne(key);
                transaction.setStatus(Transaction.SUCCESS);
                return Optional.ofNullable(release);
            } catch (Throwable ex) {
                transaction.setStatus(ex);
                throw ex;
            } finally {
                transaction.complete();
            }
        }
    });
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) CacheLoader(com.google.common.cache.CacheLoader) Release(com.ctrip.framework.apollo.biz.entity.Release) PostConstruct(javax.annotation.PostConstruct)

Example 32 with Release

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

the class ConfigControllerTest method testMergeConfigurations.

@Test
public void testMergeConfigurations() throws Exception {
    Gson gson = new Gson();
    String key1 = "key1";
    String value1 = "value1";
    String anotherValue1 = "anotherValue1";
    String key2 = "key2";
    String value2 = "value2";
    Map<String, String> config = ImmutableMap.of(key1, anotherValue1);
    Map<String, String> anotherConfig = ImmutableMap.of(key1, value1, key2, value2);
    Release releaseWithHighPriority = new Release();
    releaseWithHighPriority.setConfigurations(gson.toJson(config));
    Release releaseWithLowPriority = new Release();
    releaseWithLowPriority.setConfigurations(gson.toJson(anotherConfig));
    Map<String, String> result = configController.mergeReleaseConfigurations(Lists.newArrayList(releaseWithHighPriority, releaseWithLowPriority));
    assertEquals(2, result.keySet().size());
    assertEquals(anotherValue1, result.get(key1));
    assertEquals(value2, result.get(key2));
}
Also used : Gson(com.google.gson.Gson) Release(com.ctrip.framework.apollo.biz.entity.Release) Test(org.junit.Test)

Example 33 with Release

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

the class ConfigController method queryConfig.

@GetMapping(value = "/{appId}/{clusterName}/{namespace:.+}")
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName, @PathVariable String namespace, @RequestParam(value = "dataCenter", required = false) String dataCenter, @RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey, @RequestParam(value = "ip", required = false) String clientIp, @RequestParam(value = "label", required = false) String clientLabel, @RequestParam(value = "messages", required = false) String messagesAsString, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String originalNamespace = namespace;
    // strip out .properties suffix
    namespace = namespaceUtil.filterNamespaceName(namespace);
    // fix the character case issue, such as FX.apollo <-> fx.apollo
    namespace = namespaceUtil.normalizeNamespace(appId, namespace);
    if (Strings.isNullOrEmpty(clientIp)) {
        clientIp = tryToGetClientIp(request);
    }
    ApolloNotificationMessages clientMessages = transformMessages(messagesAsString);
    List<Release> releases = Lists.newLinkedList();
    String appClusterNameLoaded = clusterName;
    if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
        Release currentAppRelease = configService.loadConfig(appId, clientIp, clientLabel, appId, clusterName, namespace, dataCenter, clientMessages);
        if (currentAppRelease != null) {
            releases.add(currentAppRelease);
            // we have cluster search process, so the cluster name might be overridden
            appClusterNameLoaded = currentAppRelease.getClusterName();
        }
    }
    // if namespace does not belong to this appId, should check if there is a public configuration
    if (!namespaceBelongsToAppId(appId, namespace)) {
        Release publicRelease = this.findPublicConfig(appId, clientIp, clientLabel, clusterName, namespace, dataCenter, clientMessages);
        if (Objects.nonNull(publicRelease)) {
            releases.add(publicRelease);
        }
    }
    if (releases.isEmpty()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, String.format("Could not load configurations with appId: %s, clusterName: %s, namespace: %s", appId, clusterName, originalNamespace));
        Tracer.logEvent("Apollo.Config.NotFound", assembleKey(appId, clusterName, originalNamespace, dataCenter));
        return null;
    }
    auditReleases(appId, clusterName, dataCenter, clientIp, releases);
    String mergedReleaseKey = releases.stream().map(Release::getReleaseKey).collect(Collectors.joining(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR));
    if (mergedReleaseKey.equals(clientSideReleaseKey)) {
        // Client side configuration is the same with server side, return 304
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        Tracer.logEvent("Apollo.Config.NotModified", assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
        return null;
    }
    ApolloConfig apolloConfig = new ApolloConfig(appId, appClusterNameLoaded, originalNamespace, mergedReleaseKey);
    apolloConfig.setConfigurations(mergeReleaseConfigurations(releases));
    Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
    return apolloConfig;
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 34 with Release

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

the class ReleaseService method calculateChildNamespaceToPublishConfiguration.

private Map<String, String> calculateChildNamespaceToPublishConfiguration(Map<String, String> parentNamespaceOldConfiguration, Map<String, String> parentNamespaceNewConfiguration, Namespace childNamespace) {
    // first. calculate child namespace modified configs
    Release childNamespaceLatestActiveRelease = findLatestActiveRelease(childNamespace);
    Map<String, String> childNamespaceLatestActiveConfiguration = childNamespaceLatestActiveRelease == null ? null : gson.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
    Map<String, String> childNamespaceModifiedConfiguration = calculateBranchModifiedItemsAccordingToRelease(parentNamespaceOldConfiguration, childNamespaceLatestActiveConfiguration);
    // second. append child namespace modified configs to parent namespace new latest configuration
    return mergeConfiguration(parentNamespaceNewConfiguration, childNamespaceModifiedConfiguration);
}
Also used : Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 35 with Release

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

the class ReleaseService method getNamespaceReleaseConfiguration.

private Map<String, String> getNamespaceReleaseConfiguration(Namespace namespace) {
    Release release = findLatestActiveRelease(namespace);
    Map<String, String> configuration = new HashMap<>();
    if (release != null) {
        configuration = new Gson().fromJson(release.getConfigurations(), GsonType.CONFIG);
    }
    return configuration;
}
Also used : HashMap(java.util.HashMap) Gson(com.google.gson.Gson) 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