Search in sources :

Example 11 with ReleaseMessage

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

the class ReleaseMessageService method findLatestReleaseMessagesGroupByMessages.

public List<ReleaseMessage> findLatestReleaseMessagesGroupByMessages(Collection<String> messages) {
    if (CollectionUtils.isEmpty(messages)) {
        return Collections.emptyList();
    }
    List<Object[]> result = releaseMessageRepository.findLatestReleaseMessagesGroupByMessages(messages);
    List<ReleaseMessage> releaseMessages = Lists.newArrayList();
    for (Object[] o : result) {
        try {
            ReleaseMessage releaseMessage = new ReleaseMessage((String) o[0]);
            releaseMessage.setId((Long) o[1]);
            releaseMessages.add(releaseMessage);
        } catch (Exception ex) {
            Tracer.logError("Parsing LatestReleaseMessagesGroupByMessages failed", ex);
        }
    }
    return releaseMessages;
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage)

Example 12 with ReleaseMessage

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

the class GrayReleaseRulesHolderTest method assembleReleaseMessage.

private ReleaseMessage assembleReleaseMessage(String appId, String clusterName, String namespaceName) {
    String message = STRING_JOINER.join(appId, clusterName, namespaceName);
    ReleaseMessage releaseMessage = new ReleaseMessage(message);
    return releaseMessage;
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage)

Example 13 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage 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 14 with ReleaseMessage

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

the class NotificationControllerTest method testPollNotificationWithDefaultNamespaceWithNotificationIdOutDated.

@Test
public void testPollNotificationWithDefaultNamespaceWithNotificationIdOutDated() throws Exception {
    long notificationId = someNotificationId + 1;
    ReleaseMessage someReleaseMessage = mock(ReleaseMessage.class);
    String someWatchKey = "someKey";
    Set<String> watchKeys = Sets.newHashSet(someWatchKey);
    when(watchKeysUtil.assembleAllWatchKeys(someAppId, someCluster, defaultNamespace, someDataCenter)).thenReturn(watchKeys);
    when(someReleaseMessage.getId()).thenReturn(notificationId);
    when(releaseMessageService.findLatestReleaseMessageForMessages(watchKeys)).thenReturn(someReleaseMessage);
    DeferredResult<ResponseEntity<ApolloConfigNotification>> deferredResult = controller.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter, someNotificationId, someClientIp);
    ResponseEntity<ApolloConfigNotification> result = (ResponseEntity<ApolloConfigNotification>) deferredResult.getResult();
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals(defaultNamespace, result.getBody().getNamespaceName());
    assertEquals(notificationId, result.getBody().getNotificationId());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Example 15 with ReleaseMessage

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

the class NotificationControllerTest method testPollNotificationWithDefaultNamespaceAndHandleMessage.

@Test
public void testPollNotificationWithDefaultNamespaceAndHandleMessage() throws Exception {
    String someWatchKey = "someKey";
    String anotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(someAppId, someCluster, defaultNamespace);
    Set<String> watchKeys = Sets.newHashSet(someWatchKey, anotherWatchKey);
    when(watchKeysUtil.assembleAllWatchKeys(someAppId, someCluster, defaultNamespace, someDataCenter)).thenReturn(watchKeys);
    DeferredResult<ResponseEntity<ApolloConfigNotification>> deferredResult = controller.pollNotification(someAppId, someCluster, defaultNamespace, someDataCenter, someNotificationId, someClientIp);
    long someId = 1;
    ReleaseMessage someReleaseMessage = new ReleaseMessage(anotherWatchKey);
    someReleaseMessage.setId(someId);
    controller.handleMessage(someReleaseMessage, Topics.APOLLO_RELEASE_TOPIC);
    ResponseEntity<ApolloConfigNotification> response = (ResponseEntity<ApolloConfigNotification>) deferredResult.getResult();
    ApolloConfigNotification notification = response.getBody();
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(defaultNamespace, notification.getNamespaceName());
    assertEquals(someId, notification.getNotificationId());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Aggregations

ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)30 Test (org.junit.Test)16 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)9 ResponseEntity (org.springframework.http.ResponseEntity)7 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)3 Release (com.ctrip.framework.apollo.biz.entity.Release)3 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BizConfig (com.ctrip.framework.apollo.biz.config.BizConfig)2 ReleaseMessageRepository (com.ctrip.framework.apollo.biz.repository.ReleaseMessageRepository)2 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)2 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 Awaitility (org.awaitility.Awaitility)2 Awaitility.await (org.awaitility.Awaitility.await)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertSame (org.junit.Assert.assertSame)2