Backport PR #15925 to 8.12: Set Netty's maxOrder options to previous default (#15928)

Updates Netty's configuration of maxOrder to a previously proven value, if not already customised by the user.

Adds a step to the JvmOption parsing tool, which is used to compose the JVM options string to pass down to Logstash at startup.
The added step rework the parsed options to set the allocator max order -Dio.netty.allocator.maxOrder=11 so that the maximum pooled buffer is up to 16MB and not 4MB.
This option is added iff it's not yet specified by the user

(cherry picked from commit 52ce3ff8f6)

Co-authored-by: Andrea Selva <selva.andre@gmail.com>
This commit is contained in:
github-actions[bot] 2024-02-09 15:01:39 +01:00 committed by GitHub
parent 1cca6bcb2c
commit 2a5c4c906f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 4 deletions

View file

@ -31,11 +31,11 @@ buildscript {
}
}
project.sourceCompatibility = JavaVersion.VERSION_1_8
project.targetCompatibility = JavaVersion.VERSION_1_8
project.sourceCompatibility = JavaVersion.VERSION_11
project.targetCompatibility = JavaVersion.VERSION_11
dependencies {
testImplementation "junit:junit:4.12"
testImplementation "junit:junit:4.13.1"
}
javadoc {

View file

@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
@ -180,7 +181,26 @@ public class JvmOptionsParser {
// Set mandatory JVM options
jvmOptionsContent.addAll(getMandatoryJvmOptions(javaMajorVersion));
System.out.println(String.join(" ", jvmOptionsContent));
final Set<String> jvmFinalOptions = nettyMaxOrderDefaultTo11(jvmOptionsContent);
System.out.println(String.join(" ", jvmFinalOptions));
}
/**
* Inplace method that verifies if Netty's maxOrder option is already set, else configure it to have
* the default value of 11.
*
* @param options the collection of options to examine.
* @return the collection of input option eventually with Netty maxOrder added.
* */
private Set<String> nettyMaxOrderDefaultTo11(Set<String> options) {
boolean maxOrderAlreadyContained = options.stream().anyMatch(s -> s.startsWith("-Dio.netty.allocator.maxOrder"));
if (maxOrderAlreadyContained) {
return options;
}
final Set<String> acc = new HashSet<>(options);
acc.add("-Dio.netty.allocator.maxOrder=11");
return acc;
}
/**

View file

@ -8,11 +8,15 @@ import org.junit.rules.TemporaryFolder;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.function.Consumer;
import static org.junit.Assert.*;
@ -123,6 +127,38 @@ public class JvmOptionsParserTest {
assertEquals("anotherInvalidOption", res.getInvalidLines().get(4));
}
@Test
public void testNettyMaxOrderRuleAppliesIfNotAlreadyDefinedExplicitlyByUser() throws IOException {
File optionsFile = writeIntoTempOptionsFile(writer -> writer.println("-Dsome.other.netty.property=123"));
JvmOptionsParser.handleJvmOptions(new String[] {"/path/to/ls_home", optionsFile.toString()}, "-Dcli.opts=something");
// Verify
final String output = outputStreamCaptor.toString();
assertTrue("Existing properties other than Netty's maxOrder ar preserved", output.contains("-Dsome.other.netty.property=123"));
assertTrue("Netty's maxOrder MUST be forcibly defined to the expected default", output.contains("-Dio.netty.allocator.maxOrder=11"));
}
@Test
public void testNettyMaxOrderRuleDoNotAppliesIfAlreadyDefinedExplicitlyByUser() throws IOException {
File optionsFile = writeIntoTempOptionsFile(writer -> writer.println("-Dio.netty.allocator.maxOrder=10"));
JvmOptionsParser.handleJvmOptions(new String[] {"/path/to/ls_home", optionsFile.toString()}, "-Dcli.opts=something");
// Verify
final String output = outputStreamCaptor.toString();
assertTrue("Netty's maxOrder MUST be forcibly defined to the expected default", output.contains("-Dio.netty.allocator.maxOrder=10"));
}
private File writeIntoTempOptionsFile(Consumer<PrintWriter> writer) throws IOException {
File optionsFile = temp.newFile("jvm.options");
PrintWriter optionsWriter = new PrintWriter(new FileWriter(optionsFile));
writer.accept(optionsWriter);
optionsWriter.close();
return optionsFile;
}
private void verifyOptions(String message, String expected, JvmOptionsParser.ParseResult res) {
assertEquals(message, expected, String.join(System.lineSeparator(), res.getJvmOptions()));
}