Search in sources :

Example 91 with Application

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

the class EurekaEntityFunctionsTest method testCopyInstancesIfNotNullReturnCollectionOfInstanceInfo.

@Test
public void testCopyInstancesIfNotNullReturnCollectionOfInstanceInfo() {
    Application application = createSingleInstanceApp("foo", "foo", InstanceInfo.ActionType.ADDED);
    Assert.assertEquals(1, EurekaEntityFunctions.copyInstances(new ArrayList<>(Arrays.asList(application.getByInstanceId("foo"))), InstanceInfo.ActionType.ADDED).size());
}
Also used : Application(com.netflix.discovery.shared.Application) Test(org.junit.Test)

Example 92 with Application

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

the class EurekaEntityFunctionsTest method testCopyApplicationIfNotNullReturnApplication.

@Test
public void testCopyApplicationIfNotNullReturnApplication() {
    Application application = createSingleInstanceApp("foo", "foo", InstanceInfo.ActionType.ADDED);
    Assert.assertEquals(1, EurekaEntityFunctions.copyApplication(application).size());
}
Also used : Application(com.netflix.discovery.shared.Application) Test(org.junit.Test)

Example 93 with Application

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

the class DiscoveryClient method getInstancesByVipAddressAndAppName.

/**
 * Gets the list of instances matching the given VIP Address and the given
 * application name if both of them are not null. If one of them is null,
 * then that criterion is completely ignored for matching instances.
 *
 * @param vipAddress
 *            - The VIP address to match the instances for.
 * @param appName
 *            - The applicationName to match the instances for.
 * @param secure
 *            - true if it is a secure vip address, false otherwise.
 * @return - The list of {@link InstanceInfo} objects matching the criteria.
 */
@Override
public List<InstanceInfo> getInstancesByVipAddressAndAppName(String vipAddress, String appName, boolean secure) {
    List<InstanceInfo> result = new ArrayList<>();
    if (vipAddress == null && appName == null) {
        throw new IllegalArgumentException("Supplied VIP Address and application name cannot both be null");
    } else if (vipAddress != null && appName == null) {
        return getInstancesByVipAddress(vipAddress, secure);
    } else if (vipAddress == null && appName != null) {
        Application application = getApplication(appName);
        if (application != null) {
            result = application.getInstances();
        }
        return result;
    }
    String instanceVipAddress;
    for (Application app : getApplications().getRegisteredApplications()) {
        for (InstanceInfo instance : app.getInstances()) {
            if (secure) {
                instanceVipAddress = instance.getSecureVipAddress();
            } else {
                instanceVipAddress = instance.getVIPAddress();
            }
            if (instanceVipAddress == null) {
                continue;
            }
            String[] instanceVipAddresses = instanceVipAddress.split(COMMA_STRING);
            // return the instance info for the same
            for (String vipAddressFromList : instanceVipAddresses) {
                if (vipAddress.equalsIgnoreCase(vipAddressFromList.trim()) && appName.equalsIgnoreCase(instance.getAppName())) {
                    result.add(instance);
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application)

Example 94 with Application

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

the class DiscoveryClient method updateDelta.

/**
 * Updates the delta information fetches from the eureka server into the
 * local cache.
 *
 * @param delta
 *            the delta information received from eureka server in the last
 *            poll cycle.
 */
private void updateDelta(Applications delta) {
    int deltaCount = 0;
    for (Application app : delta.getRegisteredApplications()) {
        for (InstanceInfo instance : app.getInstances()) {
            Applications applications = getApplications();
            String instanceRegion = instanceRegionChecker.getInstanceRegion(instance);
            if (!instanceRegionChecker.isLocalRegion(instanceRegion)) {
                Applications remoteApps = remoteRegionVsApps.get(instanceRegion);
                if (null == remoteApps) {
                    remoteApps = new Applications();
                    remoteRegionVsApps.put(instanceRegion, remoteApps);
                }
                applications = remoteApps;
            }
            ++deltaCount;
            if (ActionType.ADDED.equals(instance.getActionType())) {
                Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                if (existingApp == null) {
                    applications.addApplication(app);
                }
                logger.debug("Added instance {} to the existing apps in region {}", instance.getId(), instanceRegion);
                applications.getRegisteredApplications(instance.getAppName()).addInstance(instance);
            } else if (ActionType.MODIFIED.equals(instance.getActionType())) {
                Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                if (existingApp == null) {
                    applications.addApplication(app);
                }
                logger.debug("Modified instance {} to the existing apps ", instance.getId());
                applications.getRegisteredApplications(instance.getAppName()).addInstance(instance);
            } else if (ActionType.DELETED.equals(instance.getActionType())) {
                Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                if (existingApp != null) {
                    logger.debug("Deleted instance {} to the existing apps ", instance.getId());
                    existingApp.removeInstance(instance);
                    /*
                         * We find all instance list from application(The status of instance status is not only the status is UP but also other status)
                         * if instance list is empty, we remove the application.
                         */
                    if (existingApp.getInstancesAsIsFromEureka().isEmpty()) {
                        applications.removeApplication(existingApp);
                    }
                }
            }
        }
    }
    logger.debug("The total number of instances fetched by the delta processor : {}", deltaCount);
    getApplications().setVersion(delta.getVersion());
    getApplications().shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
    for (Applications applications : remoteRegionVsApps.values()) {
        applications.setVersion(delta.getVersion());
        applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 95 with Application

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

the class ApplicationXmlDeserializer method deserialize.

@Override
public Application deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    String name = null;
    List<InstanceInfo> instances = new ArrayList<>();
    while (jp.nextToken() == JsonToken.FIELD_NAME) {
        String fieldName = jp.getCurrentName();
        // to point to value
        jp.nextToken();
        if ("name".equals(fieldName)) {
            name = jp.getValueAsString();
        } else if ("instance".equals(fieldName)) {
            instances.add(jp.readValueAs(InstanceInfo.class));
        } else {
            throw new JsonMappingException("Unexpected field " + fieldName, jp.getCurrentLocation());
        }
    }
    return new Application(name, instances);
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application)

Aggregations

Application (com.netflix.discovery.shared.Application)104 InstanceInfo (com.netflix.appinfo.InstanceInfo)51 Test (org.junit.Test)43 Applications (com.netflix.discovery.shared.Applications)41 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)7 Response (javax.ws.rs.core.Response)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)6 Map (java.util.Map)6 DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)4 Lease (com.netflix.eureka.lease.Lease)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 AmazonInfo (com.netflix.appinfo.AmazonInfo)2 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)2 Pair (com.netflix.discovery.shared.Pair)2