Skip to content

Commit

Permalink
Use dw DataSize for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thoniTUB committed Dec 17, 2024
1 parent 85fa37d commit 8d8ca04
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.core.Configuration;
import io.dropwizard.util.DataSize;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.PortRange;
import lombok.Getter;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class ClusterConfig extends Configuration {
*/
@Max(Integer.MAX_VALUE - 4)
@Min(64) // not practical
private int maxIoBufferSizeBytes = Integer.MAX_VALUE - 4;
private DataSize maxIoBufferSize = DataSize.bytes(Integer.MAX_VALUE - 4);

/**
* Defines the starting buffer allocation size. Larger can reduce reallocations, but can cause a greater memory demand.
Expand All @@ -60,7 +61,7 @@ public class ClusterConfig extends Configuration {
*/
@Max(Integer.MAX_VALUE - 4)
@Min(64) // Mina's default
private int initialIoBufferSizeBytes = 8192; // 8kb
private DataSize initialIoBufferSize = DataSize.bytes(8192); // 8kb

/**
* @see com.bakdata.conquery.models.messages.namespaces.specific.CollectColumnValuesJob
Expand Down Expand Up @@ -90,8 +91,8 @@ public NioSocketConnector getClusterConnector(ObjectMapper om, IoHandler ioHandl
final NioSocketConnector connector = new NioSocketConnector();

JacksonProtocolEncoder encoder = new JacksonProtocolEncoder(om.writerFor(NetworkMessage.class));
encoder.setMaxObjectSize(maxIoBufferSizeBytes);
encoder.setInitialBufferCapacityBytes(initialIoBufferSizeBytes);
encoder.setMaxObjectSize(Math.toIntExact(maxIoBufferSize.toBytes()));
encoder.setInitialBufferCapacityBytes(Math.toIntExact(initialIoBufferSize.toBytes()));

ProtocolCodecFilter codecFilter = new ProtocolCodecFilter(
encoder,
Expand All @@ -115,8 +116,8 @@ public NioSocketAcceptor getClusterAcceptor(ObjectMapper om, IoHandler ioHandler


JacksonProtocolEncoder encoder = new JacksonProtocolEncoder(om.writerFor(NetworkMessage.class));
encoder.setMaxObjectSize(maxIoBufferSizeBytes);
encoder.setInitialBufferCapacityBytes(initialIoBufferSizeBytes);
encoder.setMaxObjectSize(Math.toIntExact(maxIoBufferSize.toBytes()));
encoder.setInitialBufferCapacityBytes(Math.toIntExact(initialIoBufferSize.toBytes()));

ProtocolCodecFilter codecFilter = new ProtocolCodecFilter(
encoder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bakdata.conquery.util.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Max;

import com.google.auto.service.AutoService;
import io.dropwizard.util.DataSize;

@AutoService(ConstraintValidator.class)
public class DataSizeMaxValidator implements ConstraintValidator<Max, DataSize> {

private long maxBytes;

@Override
public void initialize(Max constraintAnnotation) {
maxBytes = constraintAnnotation.value();
}

@Override
public boolean isValid(DataSize value, ConstraintValidatorContext context) {
return value.toBytes() <= maxBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bakdata.conquery.util.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.Min;

import com.google.auto.service.AutoService;
import io.dropwizard.util.DataSize;

@AutoService(ConstraintValidator.class)
public class DataSizeMinValidator implements ConstraintValidator<Min, DataSize> {

private long minBytes;

@Override
public void initialize(Min constraintAnnotation) {
minBytes = constraintAnnotation.value();
}

@Override
public boolean isValid(DataSize value, ConstraintValidatorContext context) {
return value.toBytes() >= minBytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class MinaStackTest {
public static void beforeAll() throws IOException {

CLUSTER_CONFIG.setPort(0);
CLUSTER_CONFIG.setMaxIoBufferSizeBytes(toIntExact(DataSize.mebibytes(10).toBytes()));
CLUSTER_CONFIG.setMaxIoBufferSize(DataSize.mebibytes(10));

// This enables the Chunking filter, which triggers for messages > 1 MebiByte
CLUSTER_CONFIG.getMina().setSendBufferSize(toIntExact(DataSize.mebibytes(1).toBytes()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.bakdata.conquery.util.validation;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Set;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

import io.dropwizard.jersey.validation.Validators;
import io.dropwizard.util.DataSize;
import lombok.Data;
import org.junit.jupiter.api.Test;

public class DataSizeValidationTest {

Validator VALIDATOR = Validators.newValidator();


@Test
void inBounds() {
Container container = new Container(DataSize.bytes(5));

Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container);

assertThat(validate).isEmpty();

}

@Test
void onMaxBound() {
Container container = new Container(DataSize.bytes(6));

Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container);

assertThat(validate).isEmpty();

}

@Test
void onMinBound() {
Container container = new Container(DataSize.bytes(3));

Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container);

assertThat(validate).isEmpty();

}

@Test
void maxedOut() {
Container container = new Container(DataSize.bytes(7));

Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container);

assertThat(validate).hasSize(1);

}

@Test
void minedOut() {
Container container = new Container(DataSize.bytes(2));

Set<ConstraintViolation<Container>> validate = VALIDATOR.validate(container);

assertThat(validate).hasSize(1);

}

@Data
private static class Container {
@Min(3)
@Max(6)
private final DataSize size;
}
}

0 comments on commit 8d8ca04

Please sign in to comment.