Search in sources :

Example 71 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class AwsAsgUtil method getASGAccount.

/**
     * Get the AWS account id where an ASG is created.
     * Warning: This is expensive as it loops through all instances currently registered.
     *
     * @param asgName The name of the ASG
     * @return the account id
     */
private String getASGAccount(String asgName) {
    Applications apps = registry.getApplicationsFromLocalRegionOnly();
    for (Application app : apps.getRegisteredApplications()) {
        for (InstanceInfo instanceInfo : app.getInstances()) {
            String thisAsgName = instanceInfo.getASGName();
            if (thisAsgName != null && thisAsgName.equals(asgName)) {
                String localAccountId = getAccountId(instanceInfo, null);
                if (localAccountId != null) {
                    return localAccountId;
                }
            }
        }
    }
    logger.info("Couldn't get the ASG account for {}, using the default accountId instead", asgName);
    return accountId;
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 72 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class AwsAsgUtil method getCacheKeys.

/**
     * Get the cacheKeys of all the ASG to which query AWS for.
     *
     * <p>
     * The names are obtained from the {@link com.netflix.eureka.registry.InstanceRegistry} which is then
     * used for querying the AWS.
     * </p>
     *
     * @return the set of ASG cacheKeys (asgName + accountId).
     */
private Set<CacheKey> getCacheKeys() {
    Set<CacheKey> cacheKeys = new HashSet<CacheKey>();
    Applications apps = registry.getApplicationsFromLocalRegionOnly();
    for (Application app : apps.getRegisteredApplications()) {
        for (InstanceInfo instanceInfo : app.getInstances()) {
            String localAccountId = getAccountId(instanceInfo, accountId);
            String asgName = instanceInfo.getASGName();
            if (asgName != null) {
                CacheKey key = new CacheKey(localAccountId, asgName);
                cacheKeys.add(key);
            }
        }
    }
    return cacheKeys;
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo) HashSet(java.util.HashSet)

Example 73 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class EurekaHttpClientsTest method testCanonicalClient.

@Test
public void testCanonicalClient() throws Exception {
    Applications apps = instanceGen.toApplications();
    when(writeRequestHandler.getApplications()).thenReturn(anEurekaHttpResponse(302, Applications.class).headers("Location", readServerURI + "/v2/apps").build());
    when(readRequestHandler.getApplications()).thenReturn(anEurekaHttpResponse(200, apps).headers(HttpHeaders.CONTENT_TYPE, "application/json").build());
    EurekaHttpClient eurekaHttpClient = clientFactory.newClient();
    EurekaHttpResponse<Applications> result = eurekaHttpClient.getApplications();
    assertThat(result.getStatusCode(), is(equalTo(200)));
    assertThat(EurekaEntityComparators.equal(result.getEntity(), apps), is(true));
}
Also used : Applications(com.netflix.discovery.shared.Applications) Test(org.junit.Test)

Example 74 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class EurekaHttpClientsTest method testCompositeBootstrapResolver.

@Test
public void testCompositeBootstrapResolver() throws Exception {
    Applications applications = InstanceInfoGenerator.newBuilder(5, "eurekaWrite", "someOther").build().toApplications();
    Applications applications2 = InstanceInfoGenerator.newBuilder(2, "eurekaWrite", "someOther").build().toApplications();
    String vipAddress = applications.getRegisteredApplications("eurekaWrite").getInstances().get(0).getVIPAddress();
    // setup client config to use fixed root ips for testing
    when(clientConfig.shouldUseDnsForFetchingServiceUrls()).thenReturn(false);
    // can use anything here
    when(clientConfig.getEurekaServerServiceUrls(anyString())).thenReturn(Arrays.asList("http://foo:0"));
    when(clientConfig.getRegion()).thenReturn("us-east-1");
    when(transportConfig.getWriteClusterVip()).thenReturn(vipAddress);
    when(transportConfig.getAsyncExecutorThreadPoolSize()).thenReturn(4);
    when(transportConfig.getAsyncResolverRefreshIntervalMs()).thenReturn(400);
    when(transportConfig.getAsyncResolverWarmUpTimeoutMs()).thenReturn(400);
    ApplicationsResolver.ApplicationsSource applicationsSource = mock(ApplicationsResolver.ApplicationsSource.class);
    when(applicationsSource.getApplications(anyInt(), eq(TimeUnit.SECONDS))).thenReturn(// first time
    null).thenReturn(// second time
    applications).thenReturn(// subsequent times
    null);
    EurekaHttpClient mockHttpClient = mock(EurekaHttpClient.class);
    when(mockHttpClient.getVip(eq(vipAddress))).thenReturn(anEurekaHttpResponse(200, applications).build()).thenReturn(// contains diff number of servers
    anEurekaHttpResponse(200, applications2).build());
    TransportClientFactory transportClientFactory = mock(TransportClientFactory.class);
    when(transportClientFactory.newClient(any(EurekaEndpoint.class))).thenReturn(mockHttpClient);
    ClosableResolver<AwsEndpoint> resolver = null;
    try {
        resolver = EurekaHttpClients.compositeBootstrapResolver(clientConfig, transportConfig, transportClientFactory, applicationInfoManager.getInfo(), applicationsSource);
        List endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications.getInstancesByVirtualHostName(vipAddress).size()));
        // wait for the second cycle that hits the app source
        verify(applicationsSource, timeout(3000).times(2)).getApplications(anyInt(), eq(TimeUnit.SECONDS));
        endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications.getInstancesByVirtualHostName(vipAddress).size()));
        // wait for the third cycle that triggers the mock http client (which is the third resolver cycle)
        // for the third cycle we have mocked the application resolver to return null data so should fall back
        // to calling the remote resolver again (which should return applications2)
        verify(mockHttpClient, timeout(3000).times(3)).getVip(anyString());
        endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications2.getInstancesByVirtualHostName(vipAddress).size()));
    } finally {
        if (resolver != null) {
            resolver.shutdown();
        }
    }
}
Also used : ApplicationsResolver(com.netflix.discovery.shared.resolver.aws.ApplicationsResolver) Applications(com.netflix.discovery.shared.Applications) AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) EurekaEndpoint(com.netflix.discovery.shared.resolver.EurekaEndpoint) Test(org.junit.Test)

Example 75 with Applications

use of com.netflix.discovery.shared.Applications in project eureka by Netflix.

the class RemoteRegionRegistry method reconcileAndLogDifference.

/**
     * Reconciles the delta information fetched to see if the hashcodes match.
     *
     * @param delta - the delta information fetched previously for reconcililation.
     * @param reconcileHashCode - the hashcode for comparison.
     * @return - response
     * @throws Throwable
     */
private boolean reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable {
    logger.warn("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry", reconcileHashCode, delta.getAppsHashCode());
    Applications serverApps = this.fetchRemoteRegistry(false);
    Map<String, List<String>> reconcileDiffMap = getApplications().getReconcileMapDiff(serverApps);
    String reconcileString = "";
    for (Map.Entry<String, List<String>> mapEntry : reconcileDiffMap.entrySet()) {
        reconcileString = reconcileString + mapEntry.getKey() + ": ";
        for (String displayString : mapEntry.getValue()) {
            reconcileString = reconcileString + displayString;
        }
        reconcileString = reconcileString + "\n";
    }
    logger.warn("The reconcile string is {}", reconcileString);
    applications.set(serverApps);
    applicationsDelta.set(serverApps);
    logger.warn("The Reconcile hashcodes after complete sync up, client : {}, server : {}.", getApplications().getReconcileHashCode(), delta.getAppsHashCode());
    return true;
}
Also used : Applications(com.netflix.discovery.shared.Applications) List(java.util.List) Map(java.util.Map)

Aggregations

Applications (com.netflix.discovery.shared.Applications)85 Test (org.junit.Test)43 Application (com.netflix.discovery.shared.Application)25 InstanceInfo (com.netflix.appinfo.InstanceInfo)22 EurekaEntityFunctions.toApplications (com.netflix.discovery.util.EurekaEntityFunctions.toApplications)11 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)10 EurekaEntityFunctions.copyApplications (com.netflix.discovery.util.EurekaEntityFunctions.copyApplications)8 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)6 List (java.util.List)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)5 InstanceInfoGenerator (com.netflix.discovery.util.InstanceInfoGenerator)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Response (javax.ws.rs.core.Response)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Map (java.util.Map)3