Skip to content

Commit

Permalink
chore: Add Configuration support for GRPC messages (#15472)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Moran <[email protected]>
  • Loading branch information
thomas-swirlds-labs authored Oct 7, 2024
1 parent 7b3b0d6 commit b1f1e05
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,49 @@
* unless both are 0. Must be a value between 0 and 65535, inclusive.
* @param workflowsPort Deprecated
* @param workflowsTlsPort Deprecated
* @param maxMessageSize The maximum message size in bytes that the server can receive. Must be non-negative.
* @param maxResponseSize The maximum message size in bytes that the server can send in the response.
* Must be non-negative. Defaults to 4MB.
* @param noopMarshallerMaxMessageSize The maximum message size in bytes that the server can receive when using a no-op
* serialization strategy. Must be non-negative. Defaults to 4MB.
*/
@ConfigData("grpc")
public record GrpcConfig(
@ConfigProperty(defaultValue = "50211") @Min(0) @Max(65535) @NodeProperty int port,
@ConfigProperty(defaultValue = "50212") @Min(0) @Max(65535) @NodeProperty int tlsPort,
@ConfigProperty(defaultValue = "60211") @Min(0) @Max(65535) @NodeProperty int workflowsPort,
@ConfigProperty(defaultValue = "60212") @Min(0) @Max(65535) @NodeProperty int workflowsTlsPort) {
@ConfigProperty(defaultValue = "60212") @Min(0) @Max(65535) @NodeProperty int workflowsTlsPort,
@ConfigProperty(defaultValue = "4194304") @Max(4194304) @Min(0) int maxMessageSize,
@ConfigProperty(defaultValue = "4194304") @Max(4194304) @Min(0) int maxResponseSize,
@ConfigProperty(defaultValue = "4194304") @Max(4194304) @Min(0) int noopMarshallerMaxMessageSize) {

public GrpcConfig {
if (port == tlsPort && port != 0) {
validateFieldRange(port, 0, 65535, "port");
validateFieldRange(tlsPort, 0, 65535, "tlsPort");
validateFieldRange(workflowsPort, 0, 65535, "workflowsPort");
validateFieldRange(workflowsTlsPort, 0, 65535, "workflowsTlsPort");
validateFieldRange(maxMessageSize, 0, 4194304, "maxMessageSize");
validateFieldRange(maxResponseSize, 0, 4194304, "maxResponseSize");
validateFieldRange(noopMarshallerMaxMessageSize, 0, 4194304, "noopMarshallerMaxMessageSize");
validateUniquePorts(port, tlsPort);
validateUniqueWorkflowsPorts(workflowsPort, workflowsTlsPort);
}

private void validateFieldRange(int value, int minValue, int maxValue, String fieldName) {
if (value < minValue || value > maxValue) {
throw new IllegalArgumentException(
"grpc." + fieldName + " must be between " + minValue + " and " + maxValue);
}
}

private void validateUniquePorts(int port1, int port2) {
if (port1 == port2 && port1 != 0) {
throw new IllegalArgumentException("grpc.port and grpc.tlsPort must be different");
}
}

if (workflowsPort == workflowsTlsPort && workflowsPort != 0) {
private void validateUniqueWorkflowsPorts(int port1, int port2) {
if (port1 == port2 && port1 != 0) {
throw new IllegalArgumentException("grpc.workflowsPort and grpc.workflowsTlsPort must be different");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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 com.hedera.node.config.data;

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

import org.junit.jupiter.api.Test;

class GrpcConfigTest {

@Test
void testValidConfiguration() {
// Test valid configuration
GrpcConfig config = new GrpcConfig(50211, 50212, 60211, 60212, 4194304, 4194304, 4194304);

assertThat(config).isNotNull();
assertThat(config.port()).isEqualTo(50211);
assertThat(config.tlsPort()).isEqualTo(50212);
assertThat(config.workflowsPort()).isEqualTo(60211);
assertThat(config.workflowsTlsPort()).isEqualTo(60212);
assertThat(config.maxMessageSize()).isEqualTo(4194304);
assertThat(config.maxResponseSize()).isEqualTo(4194304);
assertThat(config.noopMarshallerMaxMessageSize()).isEqualTo(4194304);
}

@Test
void testInvalidPortAndTlsPort() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(50212, 50212, 50212, 60212, 4194304, 4194304, 4194304);
});
assertThat(throwable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.port and grpc.tlsPort must be different");
}

@Test
void testInvalidWorkflowsPortAndTlsPort() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(50211, 50212, 60212, 60212, 4194304, 4194304, 4194304);
});
assertThat(throwable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.workflowsPort and grpc.workflowsTlsPort must be different");
}

@Test
void testValidZeroPorts() {
GrpcConfig config = new GrpcConfig(0, 0, 60211, 60212, 4194304, 4194304, 4194304);

assertThat(config).isNotNull();
assertThat(config.port()).isEqualTo(0);
assertThat(config.tlsPort()).isEqualTo(0);
assertThat(config.workflowsPort()).isEqualTo(60211);
assertThat(config.workflowsTlsPort()).isEqualTo(60212);
assertThat(config.maxMessageSize()).isEqualTo(4194304);
assertThat(config.maxResponseSize()).isEqualTo(4194304);
assertThat(config.noopMarshallerMaxMessageSize()).isEqualTo(4194304);
}

@Test
void testInvalidMinValue() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(-1, 50212, 60211, 60212, 4194304, 5194304, 7194304);
});

assertThat(throwable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.port must be between 0 and 65535");
}

@Test
void testInvalidMaxValue() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(65536, 50212, 60211, 60212, 4194304, 5194304, 7194304);
});

assertThat(throwable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.port must be between 0 and 65535");
}

@Test
void testMaxMessageSizeMaxValue() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(50211, 50212, 60211, 60212, 4194305, 4194304, 4194304);
});

assertThat(throwable)
.isNotNull()
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.maxMessageSize must be between 0 and 4194304");
}

@Test
void testMaxResponseSizeMaxValue() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(50211, 50212, 60211, 60212, 4194304, 4194305, 4194304);
});

assertThat(throwable)
.isNotNull()
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.maxResponseSize must be between 0 and 4194304");
}

@Test
void testNoopMarshallerMaxMessageSizeMaxValue() {
Throwable throwable = catchThrowable(() -> {
new GrpcConfig(50211, 50212, 60211, 60212, 4194304, 4194304, 4194305);
});

assertThat(throwable)
.isNotNull()
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("grpc.noopMarshallerMaxMessageSize must be between 0 and 4194304");
}
}

0 comments on commit b1f1e05

Please sign in to comment.