Search in sources :

Example 81 with Application

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

the class AbstractInstanceRegistry method getApplication.

/**
 * Get application information.
 *
 * @param appName The name of the application
 * @param includeRemoteRegion true, if we need to include applications from remote regions
 *                            as indicated by the region {@link URL} by this property
 *                            {@link EurekaServerConfig#getRemoteRegionUrls()}, false otherwise
 * @return the application
 */
@Override
public Application getApplication(String appName, boolean includeRemoteRegion) {
    Application app = null;
    Map<String, Lease<InstanceInfo>> leaseMap = registry.get(appName);
    if (leaseMap != null && leaseMap.size() > 0) {
        for (Entry<String, Lease<InstanceInfo>> entry : leaseMap.entrySet()) {
            if (app == null) {
                app = new Application(appName);
            }
            app.addInstance(decorateInstanceInfo(entry.getValue()));
        }
    } else if (includeRemoteRegion) {
        for (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) {
            Application application = remoteRegistry.getApplication(appName);
            if (application != null) {
                return application;
            }
        }
    }
    return app;
}
Also used : Lease(com.netflix.eureka.lease.Lease) Application(com.netflix.discovery.shared.Application)

Example 82 with Application

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

the class AbstractInstanceRegistry method getInstancesById.

/**
 * @deprecated Try {@link #getInstanceByAppAndId(String, String, boolean)} instead.
 *
 * Get the list of instances by its unique id.
 *
 * @param id the unique id of the instance
 * @param includeRemoteRegions true, if we need to include applications from remote regions
 *                             as indicated by the region {@link URL} by this property
 *                             {@link EurekaServerConfig#getRemoteRegionUrls()}, false otherwise
 * @return list of InstanceInfo objects.
 */
@Deprecated
public List<InstanceInfo> getInstancesById(String id, boolean includeRemoteRegions) {
    List<InstanceInfo> list = new ArrayList<>();
    for (Iterator<Entry<String, Map<String, Lease<InstanceInfo>>>> iter = registry.entrySet().iterator(); iter.hasNext(); ) {
        Map<String, Lease<InstanceInfo>> leaseMap = iter.next().getValue();
        if (leaseMap != null) {
            Lease<InstanceInfo> lease = leaseMap.get(id);
            if (lease == null || (isLeaseExpirationEnabled() && lease.isExpired())) {
                continue;
            }
            if (list == Collections.EMPTY_LIST) {
                list = new ArrayList<>();
            }
            list.add(decorateInstanceInfo(lease));
        }
    }
    if (list.isEmpty() && includeRemoteRegions) {
        for (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) {
            for (Application application : remoteRegistry.getApplications().getRegisteredApplications()) {
                InstanceInfo instanceInfo = application.getByInstanceId(id);
                if (instanceInfo != null) {
                    list.add(instanceInfo);
                    return list;
                }
            }
        }
    }
    return list;
}
Also used : Entry(java.util.Map.Entry) Lease(com.netflix.eureka.lease.Lease) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application)

Example 83 with Application

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

the class AbstractInstanceRegistry method getApplications.

/**
 * Get the registry information about all {@link Applications}.
 *
 * @param includeRemoteRegion true, if we need to include applications from remote regions
 *                            as indicated by the region {@link URL} by this property
 *                            {@link EurekaServerConfig#getRemoteRegionUrls()}, false otherwise
 * @return applications
 *
 * @deprecated Use {@link #getApplicationsFromMultipleRegions(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 getApplications(boolean includeRemoteRegion) {
    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 (RemoteRegionRegistry remoteRegistry : this.regionNameVSRemoteRegistry.values()) {
            Applications applications = remoteRegistry.getApplications();
            for (Application application : applications.getRegisteredApplications()) {
                Application appInLocalRegistry = apps.getRegisteredApplications(application.getName());
                if (appInLocalRegistry == null) {
                    apps.addApplication(application);
                }
            }
        }
    }
    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 84 with Application

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

the class PeerAwareInstanceRegistryImpl method primeAwsReplicas.

/**
 * Prime connections for Aws replicas.
 * <p>
 * Sometimes when the eureka servers comes up, AWS firewall may not allow
 * the network connections immediately. This will cause the outbound
 * connections to fail, but the inbound connections continue to work. What
 * this means is the clients would have switched to this node (after EIP
 * binding) and so the other eureka nodes will expire all instances that
 * have been switched because of the lack of outgoing heartbeats from this
 * instance.
 * </p>
 * <p>
 * The best protection in this scenario is to block and wait until we are
 * able to ping all eureka nodes successfully atleast once. Until then we
 * won't open up the traffic.
 * </p>
 */
private void primeAwsReplicas(ApplicationInfoManager applicationInfoManager) {
    boolean areAllPeerNodesPrimed = false;
    while (!areAllPeerNodesPrimed) {
        String peerHostName = null;
        try {
            Application eurekaApps = this.getApplication(applicationInfoManager.getInfo().getAppName(), false);
            if (eurekaApps == null) {
                areAllPeerNodesPrimed = true;
                logger.info("No peers needed to prime.");
                return;
            }
            for (PeerEurekaNode node : peerEurekaNodes.getPeerEurekaNodes()) {
                for (InstanceInfo peerInstanceInfo : eurekaApps.getInstances()) {
                    LeaseInfo leaseInfo = peerInstanceInfo.getLeaseInfo();
                    // If the lease is expired - do not worry about priming
                    if (System.currentTimeMillis() > (leaseInfo.getRenewalTimestamp() + (leaseInfo.getDurationInSecs() * 1000)) + (2 * 60 * 1000)) {
                        continue;
                    }
                    peerHostName = peerInstanceInfo.getHostName();
                    logger.info("Trying to send heartbeat for the eureka server at {} to make sure the " + "network channels are open", peerHostName);
                    // the other instances may be legitimately down
                    if (peerHostName.equalsIgnoreCase(new URI(node.getServiceUrl()).getHost())) {
                        node.heartbeat(peerInstanceInfo.getAppName(), peerInstanceInfo.getId(), peerInstanceInfo, null, true);
                    }
                }
            }
            areAllPeerNodesPrimed = true;
        } catch (Throwable e) {
            logger.error("Could not contact {}", peerHostName, e);
            try {
                Thread.sleep(PRIME_PEER_NODES_RETRY_MS);
            } catch (InterruptedException e1) {
                logger.warn("Interrupted while priming : ", e1);
                areAllPeerNodesPrimed = true;
            }
        }
    }
}
Also used : PeerEurekaNode(com.netflix.eureka.cluster.PeerEurekaNode) LeaseInfo(com.netflix.appinfo.LeaseInfo) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo) URI(java.net.URI)

Example 85 with Application

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

the class PeerAwareInstanceRegistryImpl method syncUp.

/**
 * Populates the registry information from a peer eureka node. This
 * operation fails over to other nodes until the list is exhausted if the
 * communication fails.
 */
@Override
public int syncUp() {
    // Copy entire entry from neighboring DS node
    int count = 0;
    for (int i = 0; ((i < serverConfig.getRegistrySyncRetries()) && (count == 0)); i++) {
        if (i > 0) {
            try {
                Thread.sleep(serverConfig.getRegistrySyncRetryWaitMs());
            } catch (InterruptedException e) {
                logger.warn("Interrupted during registry transfer..");
                break;
            }
        }
        Applications apps = eurekaClient.getApplications();
        for (Application app : apps.getRegisteredApplications()) {
            for (InstanceInfo instance : app.getInstances()) {
                try {
                    if (isRegisterable(instance)) {
                        register(instance, instance.getLeaseInfo().getDurationInSecs(), true);
                        count++;
                    }
                } catch (Throwable t) {
                    logger.error("During DS init copy", t);
                }
            }
        }
    }
    return count;
}
Also used : Applications(com.netflix.discovery.shared.Applications) 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