From 88d98de7ae8f34d117ed23f2268c688cb0722264 Mon Sep 17 00:00:00 2001 From: nicu marasoiu Date: Fri, 13 Mar 2015 12:37:38 +0200 Subject: [PATCH] refactor Ttl scheduler with joda; use ConfProperties fully --- pom.xml | 7 ++- spring-cloud-consul-discovery/pom.xml | 6 ++- .../cloud/consul/discovery/TtlScheduler.java | 45 ++++++++++++------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 2b7b2ef1..3132beac 100644 --- a/pom.xml +++ b/pom.xml @@ -118,7 +118,7 @@ com.ecwid.consul consul-api - 1.0 + 1.0.8 javax.servlet @@ -177,6 +177,11 @@ ribbon-httpclient ${ribbon.version} + + joda-time + joda-time + 2.7 + org.projectlombok lombok diff --git a/spring-cloud-consul-discovery/pom.xml b/spring-cloud-consul-discovery/pom.xml index df57be82..a6695ec4 100644 --- a/spring-cloud-consul-discovery/pom.xml +++ b/spring-cloud-consul-discovery/pom.xml @@ -47,6 +47,10 @@ spring-boot-starter-test test - + + joda-time + joda-time + + diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java index e743c0ca..1fdc96a8 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java @@ -5,9 +5,15 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.PostConstruct; +import javax.validation.constraints.DecimalMax; +import javax.validation.constraints.DecimalMin; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; +import org.joda.time.Period; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -20,34 +26,39 @@ import com.ecwid.consul.v1.agent.model.NewService; * Created by nicu on 11.03.2015. */ @Slf4j -@ConfigurationProperties +@ConfigurationProperties(prefix = "consul.heartbeat") public class TtlScheduler { - private final Map serviceHeartbeats = new ConcurrentHashMap<>(); + public static final DateTime EXPIRED_DATE = new DateTime(0); + private final Map serviceHeartbeats = new ConcurrentHashMap<>(); private final AtomicBoolean heartbeatingNow = new AtomicBoolean(); - @Value("${consul.ttl:3}") - private volatile int ttl; + @Min(1) + @Max(10) + private volatile int ttl = 3; - @Value("${consul.heartbeatIntervalRatio:0.66}") - private volatile float heartbeatIntervalRatio; + @DecimalMin("0.1") + @DecimalMax("0.9") + private volatile double intervalRatio = 2.0/3.0; - private volatile int heartbeatInterval; + private volatile Period heartbeatInterval; @Autowired private ConsulClient client; @PostConstruct - public void computeHeartbeatInterval() { - // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now - heartbeatInterval = Math.round(Math.max(ttl - 1, Math.min(ttl * heartbeatIntervalRatio, 1))); - } + public void computeHeartbeatInterval() { + // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser + // priority), no sooner than 1s from now + heartbeatInterval = new Period(Math.round(1000 * Math.max(ttl - 1, + Math.min(ttl * intervalRatio, 1)))); + } /** * Add a service to the checks loop. */ public void add(final NewService service) { - serviceHeartbeats.put(service.getId(), 0L); + serviceHeartbeats.put(service.getId(), EXPIRED_DATE); } public void remove(String serviceId) { @@ -58,15 +69,15 @@ public class TtlScheduler { return ttl; } - @Scheduled(initialDelay = 0, fixedRate = 100) + @Scheduled(initialDelay = 0, fixedRateString = "${consul.heartbeat.fixedRate:201000}") private void heartbeatServices() { if (heartbeatingNow.compareAndSet(false, true)) { for (String serviceId : serviceHeartbeats.keySet()) { - long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId); - if (latestHeartbeatDoneForService + heartbeatInterval <= System - .currentTimeMillis()) { + DateTime latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId); + if (latestHeartbeatDoneForService.plus(heartbeatInterval).isBefore( + new DateTime())) { client.agentCheckPass(serviceId); - serviceHeartbeats.put(serviceId, System.currentTimeMillis()); + serviceHeartbeats.put(serviceId, new DateTime()); } } }