Skip to content

Commit

Permalink
Use lock for instance status update.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Dec 24, 2024
1 parent f4c8ed1 commit ca2d613
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
*
* @author Karthik Ranganathan, Greg Kim
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*
*/
@Singleton
Expand Down Expand Up @@ -149,6 +150,7 @@ public class DiscoveryClient implements EurekaClient {
private final PreRegistrationHandler preRegistrationHandler;
private final AtomicReference<Applications> 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;
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit ca2d613

Please sign in to comment.