Search in sources :

Example 46 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method alreadyBound.

public boolean alreadyBound() throws MalformedURLException {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2Service = getEC2Service();
    List<InstanceNetworkInterface> instanceNetworkInterfaces = instanceData(myInstanceId, ec2Service).getNetworkInterfaces();
    List<String> candidateIPs = getCandidateIps();
    for (String ip : candidateIPs) {
        for (InstanceNetworkInterface ini : instanceNetworkInterfaces) {
            if (ip.equals(ini.getPrivateIpAddress())) {
                logger.info("My instance {} seems to be already associated with the ip {}", myInstanceId, ip);
                return true;
            }
        }
    }
    return false;
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 47 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method getCandidateIps.

/**
     * Based on shouldUseDnsForFetchingServiceUrls configuration, either retrieves candidates from dns records or from
     * configuration properties.
     *
     *
     */
public List<String> getCandidateIps() throws MalformedURLException {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.availabilityZone);
    Collection<String> candidates = clientConfig.shouldUseDnsForFetchingServiceUrls() ? getIPsForZoneFromDNS(myZone) : getIPsForZoneFromConfig(myZone);
    if (candidates == null || candidates.size() == 0) {
        throw new RuntimeException("Could not get any ips from the pool for zone :" + myZone);
    }
    List<String> ips = Lists.newArrayList();
    for (String candidate : candidates) {
        String host = new URL(candidate).getHost();
        if (InetAddresses.isInetAddress(host)) {
            ips.add(host);
        } else {
            // ip-172-31-55-172.ec2.internal -> ip-172-31-55-172
            String firstPartOfHost = Splitter.on(".").splitToList(host).get(0);
            // ip-172-31-55-172 -> [172,31,55,172]
            List<String> noIpPrefix = Splitter.on("-").splitToList(firstPartOfHost).subList(1, 5);
            // [172,31,55,172] -> 172.31.55.172
            String ip = Joiner.on(".").join(noIpPrefix);
            if (InetAddresses.isInetAddress(ip)) {
                ips.add(ip);
            } else {
                throw new IllegalArgumentException("Illegal internal hostname " + host + " translated to '" + ip + "'");
            }
        }
    }
    return ips;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) URL(java.net.URL)

Example 48 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class PeerAwareInstanceRegistryImpl method updateRenewalThreshold.

/**
     * Updates the <em>renewal threshold</em> based on the current number of
     * renewals. The threshold is a percentage as specified in
     * {@link EurekaServerConfig#getRenewalPercentThreshold()} of renewals
     * received per minute {@link #getNumOfRenewsInLastMin()}.
     */
private void updateRenewalThreshold() {
    try {
        Applications apps = eurekaClient.getApplications();
        int count = 0;
        for (Application app : apps.getRegisteredApplications()) {
            for (InstanceInfo instance : app.getInstances()) {
                if (this.isRegisterable(instance)) {
                    ++count;
                }
            }
        }
        synchronized (lock) {
            // current expected threshold of if the self preservation is disabled.
            if ((count * 2) > (serverConfig.getRenewalPercentThreshold() * numberOfRenewsPerMinThreshold) || (!this.isSelfPreservationModeEnabled())) {
                this.expectedNumberOfRenewsPerMin = count * 2;
                this.numberOfRenewsPerMinThreshold = (int) ((count * 2) * serverConfig.getRenewalPercentThreshold());
            }
        }
        logger.info("Current renewal threshold is : {}", numberOfRenewsPerMinThreshold);
    } catch (Throwable e) {
        logger.error("Cannot update renewal threshold", e);
    }
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 49 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class PeerAwareInstanceRegistryImpl method replicateInstanceActionsToPeers.

/**
     * Replicates all instance changes to peer eureka nodes except for
     * replication traffic to this node.
     *
     */
private void replicateInstanceActionsToPeers(Action action, String appName, String id, InstanceInfo info, InstanceStatus newStatus, PeerEurekaNode node) {
    try {
        InstanceInfo infoFromRegistry = null;
        CurrentRequestVersion.set(Version.V2);
        switch(action) {
            case Cancel:
                node.cancel(appName, id);
                break;
            case Heartbeat:
                InstanceStatus overriddenStatus = overriddenInstanceStatusMap.get(id);
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.heartbeat(appName, id, infoFromRegistry, overriddenStatus, false);
                break;
            case Register:
                node.register(info);
                break;
            case StatusUpdate:
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.statusUpdate(appName, id, newStatus, infoFromRegistry);
                break;
            case DeleteStatusOverride:
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.deleteStatusOverride(appName, id, infoFromRegistry);
                break;
        }
    } catch (Throwable t) {
        logger.error("Cannot replicate information to {} for action {}", node.getServiceUrl(), action.name(), t);
    }
}
Also used : InstanceStatus(com.netflix.appinfo.InstanceInfo.InstanceStatus) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 50 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class ResponseCacheImpl method getApplicationsForVip.

private static Applications getApplicationsForVip(Key key, AbstractInstanceRegistry registry) {
    Object[] args = { key.getEntityType(), key.getName(), key.getVersion(), key.getType() };
    logger.debug("Retrieving applications from registry for key : {} {} {} {}", args);
    Applications toReturn = new Applications();
    Applications applications = registry.getApplications();
    for (Application application : applications.getRegisteredApplications()) {
        Application appToAdd = null;
        for (InstanceInfo instanceInfo : application.getInstances()) {
            String vipAddress;
            if (Key.EntityType.VIP.equals(key.getEntityType())) {
                vipAddress = instanceInfo.getVIPAddress();
            } else if (Key.EntityType.SVIP.equals(key.getEntityType())) {
                vipAddress = instanceInfo.getSecureVipAddress();
            } else {
                // should not happen, but just in case.
                continue;
            }
            if (null != vipAddress) {
                String[] vipAddresses = vipAddress.split(",");
                Arrays.sort(vipAddresses);
                if (Arrays.binarySearch(vipAddresses, key.getName()) >= 0) {
                    if (null == appToAdd) {
                        appToAdd = new Application(application.getName());
                        toReturn.addApplication(appToAdd);
                    }
                    appToAdd.addInstance(instanceInfo);
                }
            }
        }
    }
    toReturn.setAppsHashCode(toReturn.getReconcileHashCode());
    args = new Object[] { key.getEntityType(), key.getName(), key.getVersion(), key.getType(), toReturn.getReconcileHashCode() };
    logger.debug("Retrieved applications from registry for key : {} {} {} {}, reconcile hashcode: {}", args);
    return toReturn;
}
Also used : Applications(com.netflix.discovery.shared.Applications) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

InstanceInfo (com.netflix.appinfo.InstanceInfo)192 Test (org.junit.Test)74 Application (com.netflix.discovery.shared.Application)35 Applications (com.netflix.discovery.shared.Applications)22 AmazonInfo (com.netflix.appinfo.AmazonInfo)21 ArrayList (java.util.ArrayList)19 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)11 Before (org.junit.Before)11 DiscoveryClient (com.netflix.discovery.DiscoveryClient)10 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)10 Lease (com.netflix.eureka.lease.Lease)9 HashMap (java.util.HashMap)8 Response (javax.ws.rs.core.Response)8 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)7 EncoderWrapper (com.netflix.discovery.converters.wrappers.EncoderWrapper)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)6 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6