Search in sources :

Example 6 with LeaseInfo

use of com.netflix.appinfo.LeaseInfo 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)

Aggregations

LeaseInfo (com.netflix.appinfo.LeaseInfo)6 InstanceInfo (com.netflix.appinfo.InstanceInfo)3 AmazonInfo (com.netflix.appinfo.AmazonInfo)2 Builder (com.netflix.appinfo.InstanceInfo.Builder)2 Application (com.netflix.discovery.shared.Application)1 PeerEurekaNode (com.netflix.eureka.cluster.PeerEurekaNode)1 URI (java.net.URI)1 Before (org.junit.Before)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 EurekaInstanceRegisteredEvent (org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent)1