Polish "Migrate size properties to DataSize"

Closes gh-14549
This commit is contained in:
Stephane Nicoll
2018-09-21 14:43:10 +02:00
parent eb9f635004
commit cbae22f0c9
24 changed files with 205 additions and 106 deletions

View File

@@ -33,12 +33,14 @@ import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.core.io.Resource;
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
import org.springframework.util.CollectionUtils;
import org.springframework.util.unit.DataSize;
/**
* Configuration properties for Spring for Apache Kafka.
@@ -247,14 +249,14 @@ public class KafkaProperties {
/**
* Maximum amount of time the server blocks before answering the fetch request if
* there isn't sufficient data to immediately satisfy the requirement given by
* "fetch.min.bytes".
* "fetch-min-size".
*/
private Duration fetchMaxWait;
/**
* Minimum amount of data, in bytes, the server should return for a fetch request.
* Minimum amount of data the server should return for a fetch request.
*/
private Integer fetchMinSize;
private DataSize fetchMinSize;
/**
* Unique string that identifies the consumer group to which this consumer
@@ -339,11 +341,11 @@ public class KafkaProperties {
this.fetchMaxWait = fetchMaxWait;
}
public Integer getFetchMinSize() {
public DataSize getFetchMinSize() {
return this.fetchMinSize;
}
public void setFetchMinSize(Integer fetchMinSize) {
public void setFetchMinSize(DataSize fetchMinSize) {
this.fetchMinSize = fetchMinSize;
}
@@ -406,7 +408,7 @@ public class KafkaProperties {
.to(properties.in(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG));
map.from(this::getFetchMaxWait).asInt(Duration::toMillis)
.to(properties.in(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG));
map.from(this::getFetchMinSize)
map.from(this::getFetchMinSize).asInt(DataSize::toBytes)
.to(properties.in(ConsumerConfig.FETCH_MIN_BYTES_CONFIG));
map.from(this::getGroupId).to(properties.in(ConsumerConfig.GROUP_ID_CONFIG));
map.from(this::getHeartbeatInterval).asInt(Duration::toMillis)
@@ -433,10 +435,10 @@ public class KafkaProperties {
private String acks;
/**
* Default batch size in bytes. A small batch size will make batching less common
* and may reduce throughput (a batch size of zero disables batching entirely).
* Default batch size. A small batch size will make batching less common and may
* reduce throughput (a batch size of zero disables batching entirely).
*/
private Integer batchSize;
private DataSize batchSize;
/**
* Comma-delimited list of host:port pairs to use for establishing the initial
@@ -445,10 +447,10 @@ public class KafkaProperties {
private List<String> bootstrapServers;
/**
* Total bytes of memory the producer can use to buffer records waiting to be sent
* to the server.
* Total memory size the producer can use to buffer records waiting to be sent to
* the server.
*/
private Long bufferMemory;
private DataSize bufferMemory;
/**
* ID to pass to the server when making requests. Used for server-side logging.
@@ -497,11 +499,11 @@ public class KafkaProperties {
this.acks = acks;
}
public Integer getBatchSize() {
public DataSize getBatchSize() {
return this.batchSize;
}
public void setBatchSize(Integer batchSize) {
public void setBatchSize(DataSize batchSize) {
this.batchSize = batchSize;
}
@@ -513,11 +515,11 @@ public class KafkaProperties {
this.bootstrapServers = bootstrapServers;
}
public Long getBufferMemory() {
public DataSize getBufferMemory() {
return this.bufferMemory;
}
public void setBufferMemory(Long bufferMemory) {
public void setBufferMemory(DataSize bufferMemory) {
this.bufferMemory = bufferMemory;
}
@@ -577,11 +579,11 @@ public class KafkaProperties {
Properties properties = new Properties();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(this::getAcks).to(properties.in(ProducerConfig.ACKS_CONFIG));
map.from(this::getBatchSize)
map.from(this::getBatchSize).asInt(DataSize::toBytes)
.to(properties.in(ProducerConfig.BATCH_SIZE_CONFIG));
map.from(this::getBootstrapServers)
.to(properties.in(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG));
map.from(this::getBufferMemory)
map.from(this::getBufferMemory).as(DataSize::toBytes)
.to(properties.in(ProducerConfig.BUFFER_MEMORY_CONFIG));
map.from(this::getClientId)
.to(properties.in(ProducerConfig.CLIENT_ID_CONFIG));
@@ -674,9 +676,9 @@ public class KafkaProperties {
private List<String> bootstrapServers;
/**
* Maximum number of memory bytes to be used for buffering across all threads.
* Maximum memory size to be used for buffering across all threads.
*/
private Integer cacheMaxBytesBuffering;
private DataSize cacheMaxSizeBuffering;
/**
* ID to pass to the server when making requests. Used for server-side logging.
@@ -727,12 +729,26 @@ public class KafkaProperties {
this.bootstrapServers = bootstrapServers;
}
@DeprecatedConfigurationProperty(replacement = "spring.kafka.streams.cache-max-size-buffering")
@Deprecated
public Integer getCacheMaxBytesBuffering() {
return this.cacheMaxBytesBuffering;
return (this.cacheMaxSizeBuffering != null)
? (int) this.cacheMaxSizeBuffering.toBytes() : null;
}
@Deprecated
public void setCacheMaxBytesBuffering(Integer cacheMaxBytesBuffering) {
this.cacheMaxBytesBuffering = cacheMaxBytesBuffering;
DataSize cacheMaxSizeBuffering = (cacheMaxBytesBuffering != null)
? DataSize.ofBytes(cacheMaxBytesBuffering) : null;
setCacheMaxSizeBuffering(cacheMaxSizeBuffering);
}
public DataSize getCacheMaxSizeBuffering() {
return this.cacheMaxSizeBuffering;
}
public void setCacheMaxSizeBuffering(DataSize cacheMaxSizeBuffering) {
this.cacheMaxSizeBuffering = cacheMaxSizeBuffering;
}
public String getClientId() {
@@ -769,7 +785,7 @@ public class KafkaProperties {
map.from(this::getApplicationId).to(properties.in("application.id"));
map.from(this::getBootstrapServers)
.to(properties.in(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG));
map.from(this::getCacheMaxBytesBuffering)
map.from(this::getCacheMaxSizeBuffering).asInt(DataSize::toBytes)
.to(properties.in("cache.max.bytes.buffering"));
map.from(this::getClientId)
.to(properties.in(CommonClientConfigs.CLIENT_ID_CONFIG));

View File

@@ -134,7 +134,8 @@ public class EmbeddedMongoAutoConfiguration {
if (storage != null) {
String databaseDir = storage.getDatabaseDir();
String replSetName = storage.getReplSetName();
int oplogSize = (storage.getOplogSize() != null) ? storage.getOplogSize() : 0;
int oplogSize = (storage.getOplogSize() != null)
? (int) storage.getOplogSize().toMegabytes() : 0;
builder.replication(new Storage(databaseDir, replSetName, oplogSize));
}
Integer configuredPort = this.properties.getPort();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -23,6 +23,9 @@ import java.util.Set;
import de.flapdoodle.embed.mongo.distribution.Feature;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
/**
* Configuration properties for Embedded Mongo.
@@ -70,9 +73,10 @@ public class EmbeddedMongoProperties {
public static class Storage {
/**
* Maximum size of the oplog, in megabytes.
* Maximum size of the oplog.
*/
private Integer oplogSize;
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize oplogSize;
/**
* Name of the replica set.
@@ -84,11 +88,11 @@ public class EmbeddedMongoProperties {
*/
private String databaseDir;
public Integer getOplogSize() {
public DataSize getOplogSize() {
return this.oplogSize;
}
public void setOplogSize(Integer oplogSize) {
public void setOplogSize(DataSize oplogSize) {
this.oplogSize = oplogSize;
}

View File

@@ -57,6 +57,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.util.MimeType;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
/**
@@ -283,8 +284,8 @@ public class ThymeleafAutoConfiguration {
PropertyMapper map = PropertyMapper.get();
map.from(properties::getMediaTypes).whenNonNull()
.to(resolver::setSupportedMediaTypes);
map.from(properties::getMaxChunkSize).when((size) -> size > 0)
.to(resolver::setResponseMaxChunkSizeBytes);
map.from(properties::getMaxChunkSize).asInt(DataSize::toBytes)
.when((size) -> size > 0).to(resolver::setResponseMaxChunkSizeBytes);
map.from(properties::getFullModeViewNames).to(resolver::setFullModeViewNames);
map.from(properties::getChunkedModeViewNames)
.to(resolver::setChunkedModeViewNames);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -23,6 +23,7 @@ import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;
import org.springframework.util.unit.DataSize;
/**
* Properties for Thymeleaf.
@@ -233,10 +234,10 @@ public class ThymeleafProperties {
public static class Reactive {
/**
* Maximum size of data buffers used for writing to the response, in bytes.
* Templates will execute in CHUNKED mode by default if this is set.
* Maximum size of data buffers used for writing to the response. Templates will
* execute in CHUNKED mode by default if this is set.
*/
private int maxChunkSize;
private DataSize maxChunkSize = DataSize.ofBytes(0);
/**
* Media types supported by the view technology.
@@ -263,11 +264,11 @@ public class ThymeleafProperties {
this.mediaTypes = mediaTypes;
}
public int getMaxChunkSize() {
public DataSize getMaxChunkSize() {
return this.maxChunkSize;
}
public void setMaxChunkSize(int maxChunkSize) {
public void setMaxChunkSize(DataSize maxChunkSize) {
this.maxChunkSize = maxChunkSize;
}

View File

@@ -946,7 +946,7 @@ public class ServerProperties {
* Size of each buffer. The default is derived from the maximum amount of memory
* that is available to the JVM.
*/
private DataSize bufferSize = DataSize.ofBytes(0);
private DataSize bufferSize;
/**
* Number of I/O threads to create for the worker. The default is derived from the

View File

@@ -78,8 +78,8 @@ public class JettyWebServerFactoryCustomizer implements
.asInt(DataSize::toBytes)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize));
propertyMapper.from(jettyProperties::getMaxHttpPostSize).whenNonNull()
.asInt(DataSize::toBytes)
propertyMapper.from(jettyProperties::getMaxHttpPostSize).asInt(DataSize::toBytes)
.when(this::isPositive)
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
maxHttpPostSize));
propertyMapper.from(properties::getConnectionTimeout).whenNonNull()

View File

@@ -92,8 +92,8 @@ public class TomcatWebServerFactoryCustomizer implements
propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()
.asInt(DataSize::toBytes)
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
propertyMapper.from(tomcatProperties::getMaxHttpPostSize).whenNonNull()
.asInt(DataSize::toBytes)
propertyMapper.from(tomcatProperties::getMaxHttpPostSize).asInt(DataSize::toBytes)
.when((maxHttpPostSize) -> maxHttpPostSize != 0)
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
maxHttpPostSize));
propertyMapper.from(tomcatProperties::getAccesslog)

View File

@@ -89,8 +89,8 @@ public class UndertowWebServerFactoryCustomizer implements
.asInt(DataSize::toBytes)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize));
propertyMapper.from(undertowProperties::getMaxHttpPostSize).whenNonNull()
.asInt(DataSize::toBytes)
propertyMapper.from(undertowProperties::getMaxHttpPostSize)
.asInt(DataSize::toBytes).when(this::isPositive)
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
maxHttpPostSize));
propertyMapper.from(properties::getConnectionTimeout)

View File

@@ -29,6 +29,12 @@
"level": "error"
}
},
{
"name": "server.compression.min-response-size",
"description": "Minimum \"Content-Length\" value that is required for compression to be performed.",
"type": "org.springframework.util.unit.DataSize",
"defaultValue": "2KB"
},
{
"name": "server.error.include-stacktrace",
"defaultValue": "never"

View File

@@ -100,7 +100,7 @@ public class KafkaAutoConfigurationTests {
"spring.kafka.consumer.enable-auto-commit=false",
"spring.kafka.consumer.fetch-max-wait=456",
"spring.kafka.consumer.properties.fiz.buz=fix.fox",
"spring.kafka.consumer.fetch-min-size=789",
"spring.kafka.consumer.fetch-min-size=1KB",
"spring.kafka.consumer.group-id=bar",
"spring.kafka.consumer.heartbeat-interval=234",
"spring.kafka.consumer.key-deserializer = org.apache.kafka.common.serialization.LongDeserializer",
@@ -143,7 +143,7 @@ public class KafkaAutoConfigurationTests {
assertThat(configs.get(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG))
.isEqualTo(456);
assertThat(configs.get(ConsumerConfig.FETCH_MIN_BYTES_CONFIG))
.isEqualTo(789);
.isEqualTo(1024);
assertThat(configs.get(ConsumerConfig.GROUP_ID_CONFIG))
.isEqualTo("bar");
assertThat(configs.get(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG))
@@ -166,10 +166,10 @@ public class KafkaAutoConfigurationTests {
public void producerProperties() {
this.contextRunner.withPropertyValues("spring.kafka.clientId=cid",
"spring.kafka.properties.foo.bar.baz=qux.fiz.buz",
"spring.kafka.producer.acks=all", "spring.kafka.producer.batch-size=20",
"spring.kafka.producer.acks=all", "spring.kafka.producer.batch-size=2KB",
"spring.kafka.producer.bootstrap-servers=bar:1234", // test
// override
"spring.kafka.producer.buffer-memory=12345",
"spring.kafka.producer.buffer-memory=4KB",
"spring.kafka.producer.compression-type=gzip",
"spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.LongSerializer",
"spring.kafka.producer.retries=2",
@@ -194,11 +194,11 @@ public class KafkaAutoConfigurationTests {
// producer
assertThat(configs.get(ProducerConfig.ACKS_CONFIG)).isEqualTo("all");
assertThat(configs.get(ProducerConfig.BATCH_SIZE_CONFIG))
.isEqualTo(20);
.isEqualTo(2048);
assertThat(configs.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG))
.isEqualTo(Collections.singletonList("bar:1234")); // override
assertThat(configs.get(ProducerConfig.BUFFER_MEMORY_CONFIG))
.isEqualTo(12345L);
.isEqualTo(4096L);
assertThat(configs.get(ProducerConfig.COMPRESSION_TYPE_CONFIG))
.isEqualTo("gzip");
assertThat(configs.get(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG))
@@ -290,7 +290,7 @@ public class KafkaAutoConfigurationTests {
"spring.application.name=appName",
"spring.kafka.properties.foo.bar.baz=qux.fiz.buz",
"spring.kafka.streams.auto-startup=false",
"spring.kafka.streams.cache-max-bytes-buffering=42",
"spring.kafka.streams.cache-max-size-buffering=1KB",
"spring.kafka.streams.client-id=override",
"spring.kafka.streams.properties.fiz.buz=fix.fox",
"spring.kafka.streams.replication-factor=2",
@@ -311,7 +311,7 @@ public class KafkaAutoConfigurationTests {
.isEqualTo("localhost:9092, localhost:9093");
assertThat(
configs.get(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG))
.isEqualTo("42");
.isEqualTo("1024");
assertThat(configs.get(StreamsConfig.CLIENT_ID_CONFIG))
.isEqualTo("override");
assertThat(configs.get(StreamsConfig.REPLICATION_FACTOR_CONFIG))
@@ -347,6 +347,22 @@ public class KafkaAutoConfigurationTests {
});
}
@Test
@Deprecated
public void streamPropertiesWithCustomCacheMaxBytesBuffering() {
this.contextRunner.withUserConfiguration(EnableKafkaStreamsConfiguration.class)
.withPropertyValues("spring.application.name=appName",
"spring.kafka.streams.cache-max-bytes-buffering=42")
.run((context) -> {
Properties configs = context.getBean(
KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME,
KafkaStreamsConfiguration.class).asProperties();
assertThat(
configs.get(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG))
.isEqualTo("42");
});
}
@Test
public void streamsApplicationIdUsesMainApplicationNameByDefault() {
this.contextRunner.withUserConfiguration(EnableKafkaStreamsConfiguration.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -139,6 +139,13 @@ public class EmbeddedMongoAutoConfigurationTests {
@Test
public void customOpLogSizeIsAppliedToConfiguration() {
load("spring.mongodb.embedded.storage.oplogSize=1024KB");
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize())
.isEqualTo(1);
}
@Test
public void customOpLogSizeUsesMegabytesPerDefault() {
load("spring.mongodb.embedded.storage.oplogSize=10");
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize())
.isEqualTo(10);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -116,7 +116,7 @@ public class ThymeleafReactiveAutoConfigurationTests {
@Test
public void overrideMaxChunkSize() {
load(BaseConfiguration.class, "spring.thymeleaf.reactive.maxChunkSize:8192");
load(BaseConfiguration.class, "spring.thymeleaf.reactive.maxChunkSize:8KB");
ThymeleafReactiveViewResolver views = this.context
.getBean(ThymeleafReactiveViewResolver.class);
assertThat(views.getResponseMaxChunkSizeBytes()).isEqualTo(Integer.valueOf(8192));