Search in sources :

Example 1 with HttpResponse

use of com.ctrip.framework.apollo.util.http.HttpResponse in project apollo by ctripcorp.

the class RemoteConfigLongPollServiceTest method testSubmitLongPollNamespaceWithMessagesUpdated.

@Test
public void testSubmitLongPollNamespaceWithMessagesUpdated() throws Exception {
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    notificationMessages.put(someKey, someNotificationId);
    ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            return pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> onNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            onNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    onNotified.get(5000, TimeUnit.MILLISECONDS);
    // reset to 304
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
    final ArgumentCaptor<ApolloNotificationMessages> captor = ArgumentCaptor.forClass(ApolloNotificationMessages.class);
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    ApolloNotificationMessages captured = captor.getValue();
    assertEquals(1, captured.getDetails().size());
    assertEquals(someNotificationId, captured.get(someKey).longValue());
    final SettableFuture<Boolean> anotherOnNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            anotherOnNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    String anotherKey = "anotherKey";
    long anotherNotificationId = 2;
    notificationMessages.put(anotherKey, anotherNotificationId);
    // send notifications
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    anotherOnNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someRepository, times(2)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    captured = captor.getValue();
    assertEquals(2, captured.getDetails().size());
    assertEquals(someNotificationId, captured.get(someKey).longValue());
    assertEquals(anotherNotificationId, captured.get(anotherKey).longValue());
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Example 2 with HttpResponse

use of com.ctrip.framework.apollo.util.http.HttpResponse in project apollo by ctripcorp.

the class RemoteConfigLongPollServiceTest method testSubmitLongPollMultipleNamespaces.

@Test
public void testSubmitLongPollMultipleNamespaces() throws Exception {
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    RemoteConfigRepository anotherRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    final String anotherNamespace = "anotherNamespace";
    final ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    final ApolloConfigNotification anotherNotification = mock(ApolloConfigNotification.class);
    when(anotherNotification.getNamespaceName()).thenReturn(anotherNamespace);
    final SettableFuture<Boolean> submitAnotherNamespaceStart = SettableFuture.create();
    final SettableFuture<Boolean> submitAnotherNamespaceFinish = SettableFuture.create();
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        final AtomicInteger counter = new AtomicInteger();

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            // the first time
            if (counter.incrementAndGet() == 1) {
                HttpRequest request = invocation.getArgument(0, HttpRequest.class);
                assertTrue(request.getUrl().contains("notifications="));
                assertTrue(request.getUrl().contains(someNamespace));
                submitAnotherNamespaceStart.set(true);
                when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
                when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
            } else if (submitAnotherNamespaceFinish.get()) {
                HttpRequest request = invocation.getArgument(0, HttpRequest.class);
                assertTrue(request.getUrl().contains("notifications="));
                assertTrue(request.getUrl().contains(someNamespace));
                assertTrue(request.getUrl().contains(anotherNamespace));
                when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
                when(pollResponse.getBody()).thenReturn(Lists.newArrayList(anotherNotification));
            } else {
                when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
                when(pollResponse.getBody()).thenReturn(null);
            }
            return pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> onAnotherRepositoryNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            onAnotherRepositoryNotified.set(true);
            return null;
        }
    }).when(anotherRepository).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    submitAnotherNamespaceStart.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.submit(anotherNamespace, anotherRepository);
    submitAnotherNamespaceFinish.set(true);
    onAnotherRepositoryNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someRepository, times(1)).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class));
    verify(anotherRepository, times(1)).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class));
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Example 3 with HttpResponse

use of com.ctrip.framework.apollo.util.http.HttpResponse in project apollo by ctripcorp.

the class RemoteConfigRepositoryTest method testLoadConfigWithAccessKeySecret.

@Test
public void testLoadConfigWithAccessKeySecret() throws Exception {
    someSecret = "someSecret";
    String someKey = "someKey";
    String someValue = "someValue";
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
    when(someResponse.getStatusCode()).thenReturn(200);
    when(someResponse.getBody()).thenReturn(someApolloConfig);
    doAnswer(new Answer<HttpResponse<ApolloConfig>>() {

        @Override
        public HttpResponse<ApolloConfig> answer(InvocationOnMock invocation) throws Throwable {
            HttpRequest request = invocation.getArgument(0, HttpRequest.class);
            Map<String, String> headers = request.getHeaders();
            assertNotNull(headers);
            assertTrue(headers.containsKey(Signature.HTTP_HEADER_TIMESTAMP));
            assertTrue(headers.containsKey(HttpHeaders.AUTHORIZATION));
            return someResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), any(Class.class));
    RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
    Properties config = remoteConfigRepository.getConfig();
    assertEquals(configurations, config);
    assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType());
    remoteConfigLongPollService.stopLongPollingRefresh();
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 4 with HttpResponse

use of com.ctrip.framework.apollo.util.http.HttpResponse in project apollo by ctripcorp.

the class RemoteConfigRepository method loadApolloConfig.

private ApolloConfig loadApolloConfig() {
    if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
        // wait at most 5 seconds
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
        }
    }
    String appId = m_configUtil.getAppId();
    String cluster = m_configUtil.getCluster();
    String dataCenter = m_configUtil.getDataCenter();
    String secret = m_configUtil.getAccessKeySecret();
    Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace));
    int maxRetries = m_configNeedForceRefresh.get() ? 2 : 1;
    // 0 means no sleep
    long onErrorSleepTime = 0;
    Throwable exception = null;
    List<ServiceDTO> configServices = getConfigServices();
    String url = null;
    retryLoopLabel: for (int i = 0; i < maxRetries; i++) {
        List<ServiceDTO> randomConfigServices = Lists.newLinkedList(configServices);
        Collections.shuffle(randomConfigServices);
        // Access the server which notifies the client first
        if (m_longPollServiceDto.get() != null) {
            randomConfigServices.add(0, m_longPollServiceDto.getAndSet(null));
        }
        for (ServiceDTO configService : randomConfigServices) {
            if (onErrorSleepTime > 0) {
                logger.warn("Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
                try {
                    m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime);
                } catch (InterruptedException e) {
                // ignore
                }
            }
            url = assembleQueryConfigUrl(configService.getHomepageUrl(), appId, cluster, m_namespace, dataCenter, m_remoteMessages.get(), m_configCache.get());
            logger.debug("Loading config from {}", url);
            HttpRequest request = new HttpRequest(url);
            if (!StringUtils.isBlank(secret)) {
                Map<String, String> headers = Signature.buildHttpHeaders(url, appId, secret);
                request.setHeaders(headers);
            }
            Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "queryConfig");
            transaction.addData("Url", url);
            try {
                HttpResponse<ApolloConfig> response = m_httpClient.doGet(request, ApolloConfig.class);
                m_configNeedForceRefresh.set(false);
                m_loadConfigFailSchedulePolicy.success();
                transaction.addData("StatusCode", response.getStatusCode());
                transaction.setStatus(Transaction.SUCCESS);
                if (response.getStatusCode() == 304) {
                    logger.debug("Config server responds with 304 HTTP status code.");
                    return m_configCache.get();
                }
                ApolloConfig result = response.getBody();
                logger.debug("Loaded config for {}: {}", m_namespace, result);
                return result;
            } catch (ApolloConfigStatusCodeException ex) {
                ApolloConfigStatusCodeException statusCodeException = ex;
                // config not found
                if (ex.getStatusCode() == 404) {
                    String message = String.format("Could not find config for namespace - appId: %s, cluster: %s, namespace: %s, " + "please check whether the configs are released in Apollo!", appId, cluster, m_namespace);
                    statusCodeException = new ApolloConfigStatusCodeException(ex.getStatusCode(), message);
                }
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(statusCodeException));
                transaction.setStatus(statusCodeException);
                exception = statusCodeException;
                if (ex.getStatusCode() == 404) {
                    break retryLoopLabel;
                }
            } catch (Throwable ex) {
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
                transaction.setStatus(ex);
                exception = ex;
            } finally {
                transaction.complete();
            }
            // if force refresh, do normal sleep, if normal config load, do exponential sleep
            onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() : m_loadConfigFailSchedulePolicy.fail();
        }
    }
    String message = String.format("Load Apollo Config failed - appId: %s, cluster: %s, namespace: %s, url: %s", appId, cluster, m_namespace, url);
    throw new ApolloConfigException(message, exception);
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) List(java.util.List) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) Map(java.util.Map) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 5 with HttpResponse

use of com.ctrip.framework.apollo.util.http.HttpResponse in project apollo by ctripcorp.

the class RemoteConfigLongPollServiceTest method testSubmitLongPollNamespaceWithAccessKeySecret.

@Test
public void testSubmitLongPollNamespaceWithAccessKeySecret() throws Exception {
    someSecret = "someSecret";
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    notificationMessages.put(someKey, someNotificationId);
    ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            HttpRequest request = invocation.getArgument(0, HttpRequest.class);
            Map<String, String> headers = request.getHeaders();
            assertNotNull(headers);
            assertTrue(headers.containsKey(Signature.HTTP_HEADER_TIMESTAMP));
            assertTrue(headers.containsKey(HttpHeaders.AUTHORIZATION));
            return pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> onNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            onNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    onNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)7 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)7 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)6 Test (org.junit.Test)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)5 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)5 Map (java.util.Map)3 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)1 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)1 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)1 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 List (java.util.List)1 Properties (java.util.Properties)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1