fix heartbeatInterval

This commit is contained in:
Spencer Gibb
2015-03-19 20:07:08 -06:00
parent b77cbbb603
commit 41c8575385
4 changed files with 72 additions and 8 deletions

View File

@@ -24,11 +24,13 @@ import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.joda.time.Period;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "consul.heartbeat")
@Data
@CommonsLog
public class HeartbeatProperties {
@Min(1)
private int ttlValue = 30;
@@ -43,14 +45,22 @@ public class HeartbeatProperties {
private Period heartbeatInterval;
@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 = new Period(Math.round(1000 * Math.max(ttlValue - 1,
Math.min(ttlValue * intervalRatio, 1))));
public void init() {
this.heartbeatInterval = computeHearbeatInterval();
log.debug("Computed heartbeatInterval: " + heartbeatInterval);
}
public String getTtl() {
protected Period computeHearbeatInterval() {
// heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser
// priority), no sooner than 1s from now
double interval = ttlValue * intervalRatio;
double max = Math.max(interval, 1);
int ttlMinus1 = ttlValue - 1;
double min = Math.min(ttlMinus1, max);
return new Period(Math.round(1000 * min));
}
public String getTtl() {
return ttlValue + ttlUnit;
}
}

View File

@@ -67,7 +67,7 @@ public class TtlScheduler {
checkId = "service:" + checkId;
}
client.agentCheckPass(checkId);
log.info("Sending consul heartbeat for: " + serviceId);
log.debug("Sending consul heartbeat for: " + serviceId);
serviceHeartbeats.put(serviceId, DateTime.now());
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.discovery;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import org.joda.time.Period;
import org.junit.Test;
/**
* @author Spencer Gibb
*/
public class HeartbeatPropertiesTests {
@Test
public void computeHeartbeatIntervalWorks() {
HeartbeatProperties properties = new HeartbeatProperties();
Period period = properties.computeHearbeatInterval();
assertThat(period, is(notNullValue()));
assertThat(period.getSeconds(), is(20));
}
@Test
public void computeShortHeartbeat() {
HeartbeatProperties properties = new HeartbeatProperties();
properties.setTtlValue(2);
Period period = properties.computeHearbeatInterval();
assertThat(period, is(notNullValue()));
assertThat(period.getSeconds(), is(1));
}
}

View File

@@ -15,4 +15,8 @@ endpoints:
restart:
enabled: true
shutdown:
enabled: true
enabled: true
logging:
level:
org.springframework.cloud.consul: DEBUG