Search in sources :

Example 76 with Application

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

the class EurekaEntityFunctions method toApplicationMap.

public static Map<String, Application> toApplicationMap(List<InstanceInfo> instances) {
    Map<String, Application> applicationMap = new HashMap<String, Application>();
    for (InstanceInfo instance : instances) {
        String appName = instance.getAppName();
        Application application = applicationMap.get(appName);
        if (application == null) {
            applicationMap.put(appName, application = new Application(appName));
        }
        application.addInstance(instance);
    }
    return applicationMap;
}
Also used : HashMap(java.util.HashMap) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 77 with Application

use of com.netflix.discovery.shared.Application 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 78 with Application

use of com.netflix.discovery.shared.Application 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<>();
    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 79 with Application

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

the class AbstractInstanceRegistry method getApplicationDeltas.

/**
 * Get the registry information about the delta changes. The deltas are
 * cached for a window specified by
 * {@link EurekaServerConfig#getRetentionTimeInMSInDeltaQueue()}. Subsequent
 * requests for delta information may return the same information and client
 * must make sure this does not adversely affect them.
 *
 * @return all application deltas.
 * @deprecated use {@link #getApplicationDeltasFromMultipleRegions(String[])} instead. This method has a
 * flawed behavior of transparently falling back to a remote region if no instances for an app is available locally.
 * The new behavior is to explicitly specify if you need a remote region.
 */
@Deprecated
public Applications getApplicationDeltas() {
    GET_ALL_CACHE_MISS_DELTA.increment();
    Applications apps = new Applications();
    apps.setVersion(responseCache.getVersionDelta().get());
    Map<String, Application> applicationInstancesMap = new HashMap<String, Application>();
    write.lock();
    try {
        Iterator<RecentlyChangedItem> iter = this.recentlyChangedQueue.iterator();
        logger.debug("The number of elements in the delta queue is : {}", this.recentlyChangedQueue.size());
        while (iter.hasNext()) {
            Lease<InstanceInfo> lease = iter.next().getLeaseInfo();
            InstanceInfo instanceInfo = lease.getHolder();
            logger.debug("The instance id {} is found with status {} and actiontype {}", instanceInfo.getId(), instanceInfo.getStatus().name(), instanceInfo.getActionType().name());
            Application app = applicationInstancesMap.get(instanceInfo.getAppName());
            if (app == null) {
                app = new Application(instanceInfo.getAppName());
                applicationInstancesMap.put(instanceInfo.getAppName(), app);
                apps.addApplication(app);
            }
            app.addInstance(new InstanceInfo(decorateInstanceInfo(lease)));
        }
        boolean disableTransparentFallback = serverConfig.disableTransparentFallbackToOtherRegion();
        if (!disableTransparentFallback) {
            Applications allAppsInLocalRegion = getApplications(false);
            for (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) {
                Applications applications = remoteRegistry.getApplicationDeltas();
                for (Application application : applications.getRegisteredApplications()) {
                    Application appInLocalRegistry = allAppsInLocalRegion.getRegisteredApplications(application.getName());
                    if (appInLocalRegistry == null) {
                        apps.addApplication(application);
                    }
                }
            }
        }
        Applications allApps = getApplications(!disableTransparentFallback);
        apps.setAppsHashCode(allApps.getReconcileHashCode());
        return apps;
    } finally {
        write.unlock();
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 80 with Application

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

the class RemoteRegionRegistry 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()) {
            ++deltaCount;
            if (ActionType.ADDED.equals(instance.getActionType())) {
                Application existingApp = getApplications().getRegisteredApplications(instance.getAppName());
                if (existingApp == null) {
                    getApplications().addApplication(app);
                }
                logger.debug("Added instance {} to the existing apps ", instance.getId());
                getApplications().getRegisteredApplications(instance.getAppName()).addInstance(instance);
            } else if (ActionType.MODIFIED.equals(instance.getActionType())) {
                Application existingApp = getApplications().getRegisteredApplications(instance.getAppName());
                if (existingApp == null) {
                    getApplications().addApplication(app);
                }
                logger.debug("Modified instance {} to the existing apps ", instance.getId());
                getApplications().getRegisteredApplications(instance.getAppName()).addInstance(instance);
            } else if (ActionType.DELETED.equals(instance.getActionType())) {
                Application existingApp = getApplications().getRegisteredApplications(instance.getAppName());
                if (existingApp == null) {
                    getApplications().addApplication(app);
                }
                logger.debug("Deleted instance {} to the existing apps ", instance.getId());
                getApplications().getRegisteredApplications(instance.getAppName()).removeInstance(instance);
            }
        }
    }
    logger.debug("The total number of instances fetched by the delta processor : {}", deltaCount);
}
Also used : Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

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