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

[TH2-2484] Change default "messageRecursionLimit" #134

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (3.26.5)
# th2 common library (Java) (3.27.0)

## Usage

Expand Down Expand Up @@ -79,7 +79,7 @@ The `CommonFactory` reads a RabbitMQ configuration from the rabbitMQ.json file.
* minConnectionRecoveryTimeout - this option defines a minimal interval in milliseconds between reconnect attempts, with its default value set to 10000. Common factory increases the reconnect interval values from minConnectionRecoveryTimeout to maxConnectionRecoveryTimeout.
* maxConnectionRecoveryTimeout - this option defines a maximum interval in milliseconds between reconnect attempts, with its default value set to 60000. Common factory increases the reconnect interval values from minConnectionRecoveryTimeout to maxConnectionRecoveryTimeout.
* prefetchCount - this option is the maximum number of messages that the server will deliver, with its value set to 0 if unlimited, the default value is set to 10.
* messageRecursionLimit - an integer number denotes how deep nested protobuf message might be, default = 100
* messageRecursionLimit - an integer number denotes how deep nested protobuf message might be, default = 500

```json
{
Expand All @@ -95,7 +95,7 @@ The `CommonFactory` reads a RabbitMQ configuration from the rabbitMQ.json file.
"minConnectionRecoveryTimeout": 10000,
"maxConnectionRecoveryTimeout": 60000,
"prefetchCount": 10,
"messageRecursionLimit": 100
"messageRecursionLimit": 500
}
```

Expand Down Expand Up @@ -251,6 +251,9 @@ NOTES:

## Release notes

### 3.27.0
+ Updated `messageRecursionLimit` default value from `100` to `500`

### 3.26.5
+ Migrated `grpc-common` version from `3.7.0` to `3.8.0`
+ Added `time_precision` and `decimal_precision` parameters to `RootComparisonSettings`
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ test {
}

dependencies {
api platform('com.exactpro.th2:bom:3.0.0')
api platform('com.exactpro.th2:bom:3.1.0')
api "com.exactpro.th2:cradle-core:${cradleVersion}"
api 'com.exactpro.th2:grpc-common:3.8.0'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

release_version=3.26.5
release_version=3.27.0
viktor-235 marked this conversation as resolved.
Show resolved Hide resolved

description = 'th2 common library (Java)'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package com.exactpro.th2.common.schema.message.impl.rabbitmq.connection;

import com.exactpro.th2.common.metrics.HealthMetrics;
import com.exactpro.th2.common.schema.exception.CommonFactoryException;
import com.exactpro.th2.common.schema.message.SubscriberMonitor;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.ConnectionManagerConfiguration;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.RabbitMQConfiguration;
import com.exactpro.th2.common.schema.util.ProtobufUtils;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BlockedListener;
Expand Down Expand Up @@ -96,6 +98,12 @@ public ConnectionManager(@NotNull RabbitMQConfiguration rabbitMQConfiguration, @
Objects.requireNonNull(rabbitMQConfiguration, "RabbitMQ configuration cannot be null");
this.configuration = Objects.requireNonNull(connectionManagerConfiguration, "Connection manager configuration can not be null");

try {
ProtobufUtils.changeRecursionLimit(configuration.getMessageRecursionLimit());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new CommonFactoryException("Failed to change GRPC messageRecursionLimit", e);
}

String subscriberNameTmp = ObjectUtils.defaultIfNull(connectionManagerConfiguration.getSubscriberName(), rabbitMQConfiguration.getSubscriberName());
if (StringUtils.isBlank(subscriberNameTmp)) {
subscriberName = "rabbit_mq_subscriber." + System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021-2021 Exactpro (Exactpro Systems Limited)
*
* 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.exactpro.th2.common.schema.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import com.google.protobuf.CodedInputStream;
import com.google.protobuf.util.JsonFormat;

public class ProtobufUtils {

public static void changeRecursionLimit(int value) throws NoSuchFieldException, IllegalAccessException {
changeCodedInputStreamLimit(value);
changeJsonFormatLimit(value);
}

public static void changeCodedInputStreamLimit(int value) throws NoSuchFieldException, IllegalAccessException {
modifyPrivateInt(CodedInputStream.class, "defaultRecursionLimit", value);
viktor-235 marked this conversation as resolved.
Show resolved Hide resolved
}

public static void changeJsonFormatLimit(int value) throws NoSuchFieldException, IllegalAccessException {
modifyPrivateInt(JsonFormat.Parser.class, "DEFAULT_RECURSION_LIMIT", value);
}

private static void modifyPrivateInt(Class<?> clazz, String fieldName, int value) throws NoSuchFieldException, IllegalAccessException {
Field recursionLimitField = clazz.getDeclaredField(fieldName);
recursionLimitField.setAccessible(true);

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(recursionLimitField, recursionLimitField.getModifiers() & ~Modifier.FINAL);
recursionLimitField.set(null, value);
recursionLimitField.setAccessible(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ data class ConnectionManagerConfiguration(
var minConnectionRecoveryTimeout: Int = 10000,
var maxConnectionRecoveryTimeout: Int = 60000,
val prefetchCount: Int = 10,
val messageRecursionLimit: Int = 100
val messageRecursionLimit: Int = 500
) : Configuration()
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021-2021 Exactpro (Exactpro Systems Limited)
*
* 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.exactpro.th2.common.schema.util;

import java.lang.reflect.Field;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.google.protobuf.CodedInputStream;
import com.google.protobuf.util.JsonFormat;

class ProtobufUtilsTest {

@Test
void testChangeCodedInputStreamLimit() throws NoSuchFieldException, IllegalAccessException {
int limitToTest = 500;
ProtobufUtils.changeCodedInputStreamLimit(limitToTest);
Integer changedLimit = getField(CodedInputStream.class, "defaultRecursionLimit");
Assertions.assertEquals(limitToTest, changedLimit);
}

@Test
void testChangeJsonFormatRecursionLimit() throws NoSuchFieldException, IllegalAccessException {
int limitToTest = 600;
ProtobufUtils.changeJsonFormatLimit(limitToTest);
Integer changedLimit = getField(JsonFormat.Parser.class, "DEFAULT_RECURSION_LIMIT");
Assertions.assertEquals(limitToTest, changedLimit);
}

static <T> T getField(Class<?> clazz, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return (T)field.get(null);
}

}