diff --git a/eureka-client/src/main/java/com/netflix/discovery/DiscoveryClient.java b/eureka-client/src/main/java/com/netflix/discovery/DiscoveryClient.java index 841106fc5..de74a4a1d 100644 --- a/eureka-client/src/main/java/com/netflix/discovery/DiscoveryClient.java +++ b/eureka-client/src/main/java/com/netflix/discovery/DiscoveryClient.java @@ -106,6 +106,7 @@ * * @author Karthik Ranganathan, Greg Kim * @author Spencer Gibb + * @author Olga Maciaszek-Sharma * */ @Singleton @@ -149,6 +150,7 @@ public class DiscoveryClient implements EurekaClient { private final PreRegistrationHandler preRegistrationHandler; private final AtomicReference localRegionApps = new AtomicReference<>(); private final Lock fetchRegistryUpdateLock = new ReentrantLock(); + private final ReentrantLock instanceStatusUpdateLock = new ReentrantLock(); // monotonically increasing generation counter to ensure stale threads do not reset registry to an older version private final AtomicLong fetchRegistryGeneration; private final ApplicationInfoManager applicationInfoManager; @@ -1381,15 +1383,26 @@ void refreshInstanceInfo() { applicationInfoManager.refreshLeaseInfoIfRequired(); InstanceStatus status; - try { - status = getHealthCheckHandler().getStatus(instanceInfo.getStatus()); - } catch (Exception e) { - logger.warn("Exception from healthcheckHandler.getStatus, setting status to DOWN", e); - status = InstanceStatus.DOWN; - } + if (instanceStatusUpdateLock.tryLock()) { + try { + try { + status = getHealthCheckHandler().getStatus(instanceInfo.getStatus()); + } + catch (Exception e) { + logger.warn("Exception from healthcheckHandler.getStatus, setting status to DOWN", e); + status = InstanceStatus.DOWN; + } - if (null != status) { - applicationInfoManager.setInstanceStatus(status); + if (null != status) { + applicationInfoManager.setInstanceStatus(status); + } + } + finally { + instanceStatusUpdateLock.unlock(); + } + } + else { + logger.warn("Cannot acquire update lock, aborting instance status refresh"); } }