Search in sources :

Example 21 with Application

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

the class AbstractInstanceRegistry method getApplicationsFromMultipleRegions.

/**
     * This method will return applications with instances from all passed remote regions as well as the current region.
     * Thus, this gives a union view of instances from multiple regions. <br/>
     * The application instances for which this union will be done can be restricted to the names returned by
     * {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} for every region. In case, there is no whitelist
     * defined for a region, this method will also look for a global whitelist by passing <code>null</code> to the
     * method {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} <br/>
     * If you are not selectively requesting for a remote region, use {@link #getApplicationsFromAllRemoteRegions()}
     * or {@link #getApplicationsFromLocalRegionOnly()}
     *
     * @param remoteRegions The remote regions for which the instances are to be queried. The instances may be limited
     *                      by a whitelist as explained above. If <code>null</code> or empty no remote regions are
     *                      included.
     *
     * @return The applications with instances from the passed remote regions as well as local region. The instances
     * from remote regions can be only for certain whitelisted apps as explained above.
     */
public Applications getApplicationsFromMultipleRegions(String[] remoteRegions) {
    boolean includeRemoteRegion = null != remoteRegions && remoteRegions.length != 0;
    logger.debug("Fetching applications registry with remote regions: {}, Regions argument {}", includeRemoteRegion, Arrays.toString(remoteRegions));
    if (includeRemoteRegion) {
        GET_ALL_WITH_REMOTE_REGIONS_CACHE_MISS.increment();
    } else {
        GET_ALL_CACHE_MISS.increment();
    }
    Applications apps = new Applications();
    apps.setVersion(1L);
    for (Entry<String, Map<String, Lease<InstanceInfo>>> entry : registry.entrySet()) {
        Application app = null;
        if (entry.getValue() != null) {
            for (Entry<String, Lease<InstanceInfo>> stringLeaseEntry : entry.getValue().entrySet()) {
                Lease<InstanceInfo> lease = stringLeaseEntry.getValue();
                if (app == null) {
                    app = new Application(lease.getHolder().getAppName());
                }
                app.addInstance(decorateInstanceInfo(lease));
            }
        }
        if (app != null) {
            apps.addApplication(app);
        }
    }
    if (includeRemoteRegion) {
        for (String remoteRegion : remoteRegions) {
            RemoteRegionRegistry remoteRegistry = regionNameVSRemoteRegistry.get(remoteRegion);
            if (null != remoteRegistry) {
                Applications remoteApps = remoteRegistry.getApplications();
                for (Application application : remoteApps.getRegisteredApplications()) {
                    if (shouldFetchFromRemoteRegistry(application.getName(), remoteRegion)) {
                        logger.info("Application {}  fetched from the remote region {}", application.getName(), remoteRegion);
                        Application appInstanceTillNow = apps.getRegisteredApplications(application.getName());
                        if (appInstanceTillNow == null) {
                            appInstanceTillNow = new Application(application.getName());
                            apps.addApplication(appInstanceTillNow);
                        }
                        for (InstanceInfo instanceInfo : application.getInstances()) {
                            appInstanceTillNow.addInstance(instanceInfo);
                        }
                    } else {
                        logger.debug("Application {} not fetched from the remote region {} as there exists a " + "whitelist and this app is not in the whitelist.", application.getName(), remoteRegion);
                    }
                }
            } else {
                logger.warn("No remote registry available for the remote region {}", remoteRegion);
            }
        }
    }
    apps.setAppsHashCode(apps.getReconcileHashCode());
    return apps;
}
Also used : Applications(com.netflix.discovery.shared.Applications) Lease(com.netflix.eureka.lease.Lease) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application)

Example 22 with Application

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

the class InstanceRegistryTest method testGetAppsFromBothRegions.

@Test
public void testGetAppsFromBothRegions() throws Exception {
    registerInstanceLocally(createRemoteInstance(LOCAL_REGION_INSTANCE_2_HOSTNAME));
    registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME));
    Applications apps = registry.getApplicationsFromAllRemoteRegions();
    List<Application> registeredApplications = apps.getRegisteredApplications();
    Assert.assertEquals("Apps size from both regions do not match", 2, registeredApplications.size());
    Application locaApplication = null;
    Application remApplication = null;
    for (Application registeredApplication : registeredApplications) {
        if (registeredApplication.getName().equalsIgnoreCase(LOCAL_REGION_APP_NAME)) {
            locaApplication = registeredApplication;
        }
        if (registeredApplication.getName().equalsIgnoreCase(REMOTE_REGION_APP_NAME)) {
            remApplication = registeredApplication;
        }
    }
    Assert.assertNotNull("Did not find local registry app", locaApplication);
    Assert.assertEquals("Local registry app instance count not as expected.", 1, locaApplication.getInstances().size());
    Assert.assertNotNull("Did not find remote registry app", remApplication);
    Assert.assertEquals("Remote registry app instance count not as expected.", 2, remApplication.getInstances().size());
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) Test(org.junit.Test)

Example 23 with Application

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

the class InstanceRegistryTest method testGetAppsDeltaFromAllRemoteRegions.

@Test
public void testGetAppsDeltaFromAllRemoteRegions() throws Exception {
    /// local delta
    registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_2_HOSTNAME));
    waitForDeltaToBeRetrieved();
    Applications appDelta = registry.getApplicationDeltasFromMultipleRegions(null);
    List<Application> registeredApplications = appDelta.getRegisteredApplications();
    Assert.assertEquals("Apps size from remote regions do not match", 2, registeredApplications.size());
    Application localApplication = null;
    Application remApplication = null;
    for (Application registeredApplication : registeredApplications) {
        if (registeredApplication.getName().equalsIgnoreCase(LOCAL_REGION_APP_NAME)) {
            localApplication = registeredApplication;
        }
        if (registeredApplication.getName().equalsIgnoreCase(REMOTE_REGION_APP_NAME)) {
            remApplication = registeredApplication;
        }
    }
    Assert.assertNotNull("Did not find local registry app in delta.", localApplication);
    Assert.assertEquals("Local registry app instance count in delta not as expected.", 1, localApplication.getInstances().size());
    Assert.assertNotNull("Did not find remote registry app in delta", remApplication);
    Assert.assertEquals("Remote registry app instance count  in delta not as expected.", 1, remApplication.getInstances().size());
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) Test(org.junit.Test)

Example 24 with Application

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

the class InstanceRegistryTest method testGetAppsFromLocalRegionOnly.

@Test
public void testGetAppsFromLocalRegionOnly() throws Exception {
    registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME));
    Applications apps = registry.getApplicationsFromLocalRegionOnly();
    List<Application> registeredApplications = apps.getRegisteredApplications();
    Assert.assertEquals("Apps size from local region do not match", 1, registeredApplications.size());
    Application app = registeredApplications.iterator().next();
    Assert.assertEquals("Added app did not return from local registry", LOCAL_REGION_APP_NAME, app.getName());
    Assert.assertEquals("Returned app did not have the instance", 1, app.getInstances().size());
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) Test(org.junit.Test)

Example 25 with Application

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

the class AbstractVIPResourceTest method testMiniVipGet.

@Test
public void testMiniVipGet() throws Exception {
    Response response = resource.getVipResponse(Version.V2.name(), vipName, MediaType.APPLICATION_JSON, EurekaAccept.compact, Key.EntityType.VIP);
    String json = String.valueOf(response.getEntity());
    DecoderWrapper decoder = CodecWrappers.getDecoder(CodecWrappers.LegacyJacksonJson.class);
    Applications decodedApps = decoder.decode(json, Applications.class);
    Application decodedApp = decodedApps.getRegisteredApplications(testApplication.getName());
    // assert false as one is mini, so should NOT equal
    assertThat(EurekaEntityComparators.equal(testApplication, decodedApp), is(false));
    for (InstanceInfo instanceInfo : testApplication.getInstances()) {
        InstanceInfo decodedInfo = decodedApp.getByInstanceId(instanceInfo.getId());
        assertThat(EurekaEntityComparators.equalMini(instanceInfo, decodedInfo), is(true));
    }
}
Also used : Response(javax.ws.rs.core.Response) DecoderWrapper(com.netflix.discovery.converters.wrappers.DecoderWrapper) Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo) CodecWrappers(com.netflix.discovery.converters.wrappers.CodecWrappers) Test(org.junit.Test)

Aggregations

Application (com.netflix.discovery.shared.Application)60 InstanceInfo (com.netflix.appinfo.InstanceInfo)35 Applications (com.netflix.discovery.shared.Applications)25 Test (org.junit.Test)18 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)7 HashMap (java.util.HashMap)7 Response (javax.ws.rs.core.Response)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)6 Lease (com.netflix.eureka.lease.Lease)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 EurekaHttpResponseBuilder (com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder)2 PeerEurekaNode (com.netflix.eureka.cluster.PeerEurekaNode)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)1