Search in sources :

Example 26 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification 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 27 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification 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)

Example 28 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class NotificationControllerV2 method handleMessage.

@Override
public void handleMessage(ReleaseMessage message, String channel) {
    logger.info("message received - channel: {}, message: {}", channel, message);
    String content = message.getMessage();
    Tracer.logEvent("Apollo.LongPoll.Messages", content);
    if (!Topics.APOLLO_RELEASE_TOPIC.equals(channel) || Strings.isNullOrEmpty(content)) {
        return;
    }
    String changedNamespace = retrieveNamespaceFromReleaseMessage.apply(content);
    if (Strings.isNullOrEmpty(changedNamespace)) {
        logger.error("message format invalid - {}", content);
        return;
    }
    if (!deferredResults.containsKey(content)) {
        return;
    }
    // create a new list to avoid ConcurrentModificationException
    List<DeferredResultWrapper> results = Lists.newArrayList(deferredResults.get(content));
    ApolloConfigNotification configNotification = new ApolloConfigNotification(changedNamespace, message.getId());
    configNotification.addMessage(content, message.getId());
    // do async notification if too many clients
    if (results.size() > bizConfig.releaseMessageNotificationBatch()) {
        largeNotificationBatchExecutorService.submit(() -> {
            logger.debug("Async notify {} clients for key {} with batch {}", results.size(), content, bizConfig.releaseMessageNotificationBatch());
            for (int i = 0; i < results.size(); i++) {
                if (i > 0 && i % bizConfig.releaseMessageNotificationBatch() == 0) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(bizConfig.releaseMessageNotificationBatchIntervalInMilli());
                    } catch (InterruptedException e) {
                    // ignore
                    }
                }
                logger.debug("Async notify {}", results.get(i));
                results.get(i).setResult(configNotification);
            }
        });
        return;
    }
    logger.debug("Notify {} clients for key {}", results.size(), content);
    for (DeferredResultWrapper result : results) {
        result.setResult(configNotification);
    }
    logger.debug("Notification completed");
}
Also used : DeferredResultWrapper(com.ctrip.framework.apollo.configservice.wrapper.DeferredResultWrapper) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Example 29 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project apollo by ctripcorp.

the class NotificationControllerV2 method getApolloConfigNotifications.

private List<ApolloConfigNotification> getApolloConfigNotifications(Set<String> namespaces, Map<String, Long> clientSideNotifications, Multimap<String, String> watchedKeysMap, List<ReleaseMessage> latestReleaseMessages) {
    List<ApolloConfigNotification> newNotifications = Lists.newArrayList();
    if (!CollectionUtils.isEmpty(latestReleaseMessages)) {
        Map<String, Long> latestNotifications = Maps.newHashMap();
        for (ReleaseMessage releaseMessage : latestReleaseMessages) {
            latestNotifications.put(releaseMessage.getMessage(), releaseMessage.getId());
        }
        for (String namespace : namespaces) {
            long clientSideId = clientSideNotifications.get(namespace);
            long latestId = ConfigConsts.NOTIFICATION_ID_PLACEHOLDER;
            Collection<String> namespaceWatchedKeys = watchedKeysMap.get(namespace);
            for (String namespaceWatchedKey : namespaceWatchedKeys) {
                long namespaceNotificationId = latestNotifications.getOrDefault(namespaceWatchedKey, ConfigConsts.NOTIFICATION_ID_PLACEHOLDER);
                if (namespaceNotificationId > latestId) {
                    latestId = namespaceNotificationId;
                }
            }
            if (latestId > clientSideId) {
                ApolloConfigNotification notification = new ApolloConfigNotification(namespace, latestId);
                namespaceWatchedKeys.stream().filter(latestNotifications::containsKey).forEach(namespaceWatchedKey -> notification.addMessage(namespaceWatchedKey, latestNotifications.get(namespaceWatchedKey)));
                newNotifications.add(notification);
            }
        }
    }
    return newNotifications;
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Example 30 with ApolloConfigNotification

use of com.ctrip.framework.apollo.core.dto.ApolloConfigNotification in project dubbo by alibaba.

the class EmbeddedApolloJunit5 method mockLongPollBody.

private String mockLongPollBody(String notificationsStr) {
    List<ApolloConfigNotification> oldNotifications = GSON.fromJson(notificationsStr, notificationType);
    List<ApolloConfigNotification> newNotifications = new ArrayList<>();
    for (ApolloConfigNotification notification : oldNotifications) {
        newNotifications.add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1));
    }
    return GSON.toJson(newNotifications);
}
Also used : ArrayList(java.util.ArrayList) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)

Aggregations

ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)53 Test (org.junit.Test)41 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)28 Sql (org.springframework.test.context.jdbc.Sql)26 List (java.util.List)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)9 ResponseEntity (org.springframework.http.ResponseEntity)8 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)5 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)5 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)3 Config (com.ctrip.framework.apollo.Config)3 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)3 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)3 Map (java.util.Map)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 DeferredResultWrapper (com.ctrip.framework.apollo.configservice.wrapper.DeferredResultWrapper)2