Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dw DataSize for validation #3642

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.dropwizard.validation.MaxDataSize

gibt es schon

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups, den habe ich übersehen. Danke!


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;
}
}
Loading