com.netflix.eureka
Class PeerAwareInstanceRegistry

java.lang.Object
  extended by com.netflix.eureka.InstanceRegistry
      extended by com.netflix.eureka.PeerAwareInstanceRegistry
All Implemented Interfaces:
com.netflix.discovery.shared.LookupService<java.lang.String>, LeaseManager<com.netflix.appinfo.InstanceInfo>

public class PeerAwareInstanceRegistry
extends InstanceRegistry

Handles replication of all operations to InstanceRegistry to peer Eureka nodes to keep them all in sync.

Primary operations that are replicated are the Registers,Renewals,Cancels,Expirations and Status Changes

When the eureka server starts up it tries to fetch all the registry information from the peer eureka nodes.If for some reason this operation fails, the server does not allow the user to get the registry information for a period specified in EurekaServerConfig.getWaitTimeInMsWhenSyncEmpty().

One important thing to note about renewals.If the renewal drops more than the specified threshold as specified in EurekaServerConfig.getRenewalPercentThreshold() within a period of EurekaServerConfig.getRenewalThresholdUpdateIntervalMs(), eureka perceives this as a danger and stops expiring instances.


Nested Class Summary
static class PeerAwareInstanceRegistry.Action
           
 
Field Summary
 
Fields inherited from class com.netflix.eureka.InstanceRegistry
allKnownRemoteRegions, expectedNumberOfRenewsPerMin, lock, numberOfRenewsPerMinThreshold, overriddenInstanceStatusMap, regionNameVSRemoteRegistry
 
Method Summary
 boolean cancel(java.lang.String appName, java.lang.String id, boolean isReplication)
          Cancels the registration of an instance.
static PeerAwareInstanceRegistry getInstance()
           
 com.netflix.appinfo.InstanceInfo getNextServerFromEureka(java.lang.String virtualHostname, boolean secure)
           
 int getNumOfRenewsPerMinThreshold()
          Gets the threshold for the renewals per minute.
 long getNumOfReplicationsInLastMin()
          Gets the number of renewals in the last minute.
 java.util.List<PeerEurekaNode> getReplicaNodes()
          Gets the list of peer eureka nodes which is the list to replicate information to.
 java.util.List<com.netflix.discovery.shared.Application> getSortedApplications()
          Gets the list of all Applications from the registry in sorted lexical order of Application.getName().
 int isBelowRenewThresold()
          Checks if the number of renewals is lesser than threshold.
 boolean isLeaseExpirationEnabled()
           
 boolean isRegisterable(com.netflix.appinfo.InstanceInfo instanceInfo)
          Checks if an instance is registerable in this region.
 boolean isSelfPreservationModeEnabled()
          Checks to see if the self-preservation mode is enabled.
 void openForTraffic(int count)
           
 void register(com.netflix.appinfo.InstanceInfo info, boolean isReplication)
          Registers the information about the InstanceInfo and replicates this information to all peer eureka nodes.
 boolean renew(java.lang.String appName, java.lang.String id, boolean isReplication)
          Renew the Lease associated w/ the passed in appName and id
 boolean shouldAllowAccess()
          Checks to see if the registry access is allowed or the server is in a situation where it does not all getting registry information.
 void statusUpdate(java.lang.String asgName, ASGResource.ASGStatus newStatus, boolean isReplication)
          Replicate the ASG status updates to peer eureka nodes.
 boolean statusUpdate(java.lang.String appName, java.lang.String id, com.netflix.appinfo.InstanceInfo.InstanceStatus newStatus, java.lang.String lastDirtyTimestamp, boolean isReplication)
          Updates the status of an instance.
 int syncUp()
          Populates the registry information from a peer eureka node.
 
Methods inherited from class com.netflix.eureka.InstanceRegistry
evict, getApplication, getApplication, getApplicationDeltas, getApplicationDeltasFromMultipleRegions, getApplications, getApplications, getApplicationsFromAllRemoteRegions, getApplicationsFromLocalRegionOnly, getApplicationsFromMultipleRegions, getInstanceByAppAndId, getInstanceByAppAndId, getInstancesById, getInstancesById, getLastNCanceledInstances, getLastNRegisteredInstances, getNumberofElementsininstanceCache, getNumOfRenewsInLastMin, initRemoteRegionRegistry, postInit, register, storeOverriddenStatusIfRequired
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static PeerAwareInstanceRegistry getInstance()

syncUp

public int 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.


openForTraffic

public void openForTraffic(int count)

shouldAllowAccess

public boolean shouldAllowAccess()
Checks to see if the registry access is allowed or the server is in a situation where it does not all getting registry information. The server does not return registry information for a period specified in EurekaServerConfig.getWaitTimeInMsWhenSyncEmpty(), if it cannot get the registry information from the peer eureka nodes at start up.

Returns:
false - if the instances count from a replica transfer returned zero and if the wait time has not elapsed, o otherwise returns true

getReplicaNodes

public java.util.List<PeerEurekaNode> getReplicaNodes()
Gets the list of peer eureka nodes which is the list to replicate information to.

Returns:
the list of replica nodes.

cancel

public boolean cancel(java.lang.String appName,
                      java.lang.String id,
                      boolean isReplication)
Description copied from class: InstanceRegistry
Cancels the registration of an instance.

This is normally invoked by a client when it shuts down informing the server to remove the instance from traffic.

Specified by:
cancel in interface LeaseManager<com.netflix.appinfo.InstanceInfo>
Overrides:
cancel in class InstanceRegistry
Parameters:
appName - the application name of the application.
id - the unique identifier of the instance.
isReplication - true if this is a replication event from other nodes, false otherwise.
Returns:
true if the instance was removed from the InstanceRegistry successfully, false otherwise.

register

public void register(com.netflix.appinfo.InstanceInfo info,
                     boolean isReplication)
Registers the information about the InstanceInfo and replicates this information to all peer eureka nodes. If this is replication event from other replica nodes then it is not replicated.

Parameters:
info - the InstanceInfo to be registered and replicated.
isReplication - true if this is a replication event from other replica nodes, false otherwise.

renew

public boolean renew(java.lang.String appName,
                     java.lang.String id,
                     boolean isReplication)
Description copied from interface: LeaseManager
Renew the Lease associated w/ the passed in appName and id

Specified by:
renew in interface LeaseManager<com.netflix.appinfo.InstanceInfo>
Overrides:
renew in class InstanceRegistry
id - - unique id within appName
isReplication - - whether this is a replicated entry from another ds node
Returns:
whether the operation of successful

statusUpdate

public boolean statusUpdate(java.lang.String appName,
                            java.lang.String id,
                            com.netflix.appinfo.InstanceInfo.InstanceStatus newStatus,
                            java.lang.String lastDirtyTimestamp,
                            boolean isReplication)
Description copied from class: InstanceRegistry
Updates the status of an instance. Normally happens to put an instance between InstanceInfo.InstanceStatus.OUT_OF_SERVICE and InstanceInfo.InstanceStatus.UP to put the instance in and out of traffic.

Overrides:
statusUpdate in class InstanceRegistry
Parameters:
appName - the application name of the instance.
id - the unique identifier of the instance.
newStatus - the new InstanceInfo.InstanceStatus.
lastDirtyTimestamp - last timestamp when this instance information was updated.
isReplication - true if this is a replication event from other nodes, false otherwise.
Returns:
true if the status was successfully updated, false otherwise.

statusUpdate

public void statusUpdate(java.lang.String asgName,
                         ASGResource.ASGStatus newStatus,
                         boolean isReplication)
Replicate the ASG status updates to peer eureka nodes. If this event is a replication from other nodes, then it is not replicated to other nodes.

Parameters:
asgName - the asg name for which the status needs to be replicated.
newStatus - the ASGResource.ASGStatus information that needs to be replicated.
isReplication - true if this is a replication event from other nodes, false otherwise.

isLeaseExpirationEnabled

public boolean isLeaseExpirationEnabled()
Specified by:
isLeaseExpirationEnabled in class InstanceRegistry

isSelfPreservationModeEnabled

public boolean isSelfPreservationModeEnabled()
Checks to see if the self-preservation mode is enabled.

The self-preservation mode is enabled if the expected number of renewals per minute InstanceRegistry.getNumOfRenewsInLastMin() is lesser than the expected threshold which is determined by getNumOfRenewsPerMinThreshold() . Eureka perceives this as a danger and stops expiring instances as this is most likely because of a network event. The mode is disabled only when the renewals get back to above the threshold or if the flag EurekaServerConfig.shouldEnableSelfPreservation() is set to false.

Returns:
true if the self-preservation mode is enabled, false otherwise.

getNextServerFromEureka

public com.netflix.appinfo.InstanceInfo getNextServerFromEureka(java.lang.String virtualHostname,
                                                                boolean secure)

getSortedApplications

public java.util.List<com.netflix.discovery.shared.Application> getSortedApplications()
Gets the list of all Applications from the registry in sorted lexical order of Application.getName().

Returns:
the list of Applications in lexical order.

getNumOfReplicationsInLastMin

@Monitor(name="numOfReplicationsInLastMin",
         description="Number of total replications received in the last minute",
         type=GAUGE)
public long getNumOfReplicationsInLastMin()
Gets the number of renewals in the last minute.

Returns:
a long value representing the number of renewals in the last minute.

isBelowRenewThresold

@Monitor(name="isBelowRenewThreshold",
         description="0 = false, 1 = true",
         type=GAUGE)
public int isBelowRenewThresold()
Checks if the number of renewals is lesser than threshold.

Returns:
0 if the renewals are greater than threshold, 1 otherwise.

getNumOfRenewsPerMinThreshold

@Monitor(name="numOfRenewsPerMinThreshold",
         type=GAUGE)
public int getNumOfRenewsPerMinThreshold()
Gets the threshold for the renewals per minute.

Returns:
the integer representing the threshold for the renewals per minute.

isRegisterable

public boolean isRegisterable(com.netflix.appinfo.InstanceInfo instanceInfo)
Checks if an instance is registerable in this region. Instances from other regions are rejected.

Parameters:
instanceInfo - - the instance info information of the instance
Returns:
- true, if it can be registered in this server, false otherwise.