Skip to content

Commit ad8911b

Browse files
committed
Add unit tests for indices.recovery.max_bytes_per_sec default values (elastic#83261)
`indices.recovery.max_bytes_per_sec` has a default value that depends on multiple criteria that are well documented but not unit tested. This pull request introduces unit tests that verifies the current behavior so that future changes like elastic#82819 are less likely to break things.
1 parent 15fd693 commit ad8911b

File tree

2 files changed

+251
-6
lines changed

2 files changed

+251
-6
lines changed

server/src/main/java/org/elasticsearch/indices/recovery/RecoverySettings.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,57 @@ public class RecoverySettings {
4646

4747
private static final Logger logger = LogManager.getLogger(RecoverySettings.class);
4848

49+
/**
50+
* Undocumented setting, used to override the total physical available memory in tests
51+
**/
52+
// package private for tests
53+
static final Setting<ByteSizeValue> TOTAL_PHYSICAL_MEMORY_OVERRIDING_TEST_SETTING = Setting.byteSizeSetting(
54+
"recovery_settings.total_physical_memory_override",
55+
settings -> new ByteSizeValue(OsProbe.getInstance().getTotalPhysicalMemorySize()).getStringRep(),
56+
Property.NodeScope
57+
);
58+
59+
/**
60+
* Undocumented setting, used to override the current JVM version in tests
61+
**/
62+
// package private for tests
63+
static final Setting<JavaVersion> JAVA_VERSION_OVERRIDING_TEST_SETTING = new Setting<>(
64+
"recovery_settings.java_version_override",
65+
settings -> JavaVersion.current().toString(),
66+
JavaVersion::parse,
67+
Property.NodeScope
68+
);
69+
70+
public static final ByteSizeValue DEFAULT_MAX_BYTES_PER_SEC = new ByteSizeValue(40L, ByteSizeUnit.MB);
71+
4972
public static final Setting<ByteSizeValue> INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING = Setting.byteSizeSetting(
5073
"indices.recovery.max_bytes_per_sec",
5174
s -> {
52-
final ByteSizeValue defaultMaxBytesPerSec = new ByteSizeValue(40, ByteSizeUnit.MB);
5375
final List<DiscoveryNodeRole> roles = NodeRoleSettings.NODE_ROLES_SETTING.get(s);
5476
final List<DiscoveryNodeRole> dataRoles = roles.stream().filter(DiscoveryNodeRole::canContainData).collect(Collectors.toList());
5577
if (dataRoles.isEmpty()) {
5678
// if the node is not a data node, this value doesn't matter, use the default
57-
return defaultMaxBytesPerSec.getStringRep();
79+
return DEFAULT_MAX_BYTES_PER_SEC.getStringRep();
5880
}
5981
if (dataRoles.stream()
6082
.allMatch(
6183
dn -> dn.equals(DiscoveryNodeRole.DATA_COLD_NODE_ROLE) || dn.equals(DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE)
6284
) == false) {
6385
// the node is not a dedicated cold and/or frozen node, use the default
64-
return defaultMaxBytesPerSec.getStringRep();
86+
return DEFAULT_MAX_BYTES_PER_SEC.getStringRep();
6587
}
6688
/*
6789
* Now we are looking at a node that has a single data role, that data role is the cold data role, and the node does not
6890
* have the master role. In this case, we are going to set the recovery size as a function of the memory size. We are making
6991
* an assumption here that the size of the instance is correlated with I/O resources. That is we are assuming that the
7092
* larger the instance, the more disk and networking capacity it has available.
7193
*/
72-
if (JavaVersion.current().compareTo(JavaVersion.parse("14")) < 0) {
94+
final JavaVersion javaVersion = JAVA_VERSION_OVERRIDING_TEST_SETTING.get(s);
95+
if (javaVersion.compareTo(JavaVersion.parse("14")) < 0) {
7396
// prior to JDK 14, the JDK did not take into consideration container memory limits when reporting total system memory
74-
return defaultMaxBytesPerSec.getStringRep();
97+
return DEFAULT_MAX_BYTES_PER_SEC.getStringRep();
7598
}
76-
final ByteSizeValue totalPhysicalMemory = new ByteSizeValue(OsProbe.getInstance().getTotalPhysicalMemorySize());
99+
final ByteSizeValue totalPhysicalMemory = TOTAL_PHYSICAL_MEMORY_OVERRIDING_TEST_SETTING.get(s);
77100
final ByteSizeValue maxBytesPerSec;
78101
if (totalPhysicalMemory.compareTo(new ByteSizeValue(4, ByteSizeUnit.GB)) <= 0) {
79102
maxBytesPerSec = new ByteSizeValue(40, ByteSizeUnit.MB);
@@ -373,6 +396,10 @@ private void setMaxBytesPerSec(ByteSizeValue maxBytesPerSec) {
373396
}
374397
}
375398

399+
ByteSizeValue getMaxBytesPerSec() {
400+
return maxBytesPerSec;
401+
}
402+
376403
public int getMaxConcurrentFileChunks() {
377404
return maxConcurrentFileChunks;
378405
}

server/src/test/java/org/elasticsearch/indices/recovery/RecoverySettingsTests.java

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@
1010

1111
import org.elasticsearch.common.settings.ClusterSettings;
1212
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.common.unit.ByteSizeUnit;
14+
import org.elasticsearch.common.unit.ByteSizeValue;
15+
import org.elasticsearch.core.Nullable;
1316
import org.elasticsearch.core.Releasable;
17+
import org.elasticsearch.jdk.JavaVersion;
1418
import org.elasticsearch.test.ESTestCase;
1519

20+
import java.util.ArrayList;
21+
import java.util.HashSet;
22+
import java.util.Objects;
23+
import java.util.Set;
24+
25+
import static org.elasticsearch.common.util.set.Sets.newHashSet;
26+
import static org.elasticsearch.indices.recovery.RecoverySettings.DEFAULT_MAX_BYTES_PER_SEC;
27+
import static org.elasticsearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING;
1628
import static org.elasticsearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_MAX_CONCURRENT_SNAPSHOT_FILE_DOWNLOADS;
1729
import static org.elasticsearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_MAX_CONCURRENT_SNAPSHOT_FILE_DOWNLOADS_PER_NODE;
1830
import static org.elasticsearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_USE_SNAPSHOTS_SETTING;
31+
import static org.elasticsearch.indices.recovery.RecoverySettings.JAVA_VERSION_OVERRIDING_TEST_SETTING;
32+
import static org.elasticsearch.indices.recovery.RecoverySettings.TOTAL_PHYSICAL_MEMORY_OVERRIDING_TEST_SETTING;
33+
import static org.elasticsearch.node.NodeRoleSettings.NODE_ROLES_SETTING;
1934
import static org.hamcrest.Matchers.containsString;
35+
import static org.hamcrest.Matchers.equalTo;
2036
import static org.hamcrest.Matchers.is;
2137
import static org.hamcrest.Matchers.notNullValue;
2238
import static org.hamcrest.Matchers.nullValue;
@@ -89,4 +105,206 @@ public void testMaxConcurrentSnapshotFileDownloadsPerNodeIsValidated() {
89105
)
90106
);
91107
}
108+
109+
public void testDefaultMaxBytesPerSecOnNonDataNode() {
110+
assertThat(
111+
"Non-data nodes have a default 40mb rate limit",
112+
nodeRecoverySettings().withRole(randomFrom("master", "ingest", "ml")).withRandomMemory().build().getMaxBytesPerSec(),
113+
equalTo(DEFAULT_MAX_BYTES_PER_SEC)
114+
);
115+
}
116+
117+
public void testMaxBytesPerSecOnNonDataNodeWithIndicesRecoveryMaxBytesPerSec() {
118+
final ByteSizeValue random = randomByteSizeValue();
119+
assertThat(
120+
"Non-data nodes should use the defined rate limit when set",
121+
nodeRecoverySettings().withRole(randomFrom("master", "ingest", "ml"))
122+
.withIndicesRecoveryMaxBytesPerSec(random)
123+
.withRandomMemory()
124+
.build()
125+
.getMaxBytesPerSec(),
126+
equalTo(random)
127+
);
128+
}
129+
130+
public void testDefaultMaxBytesPerSecOnDataNode() {
131+
assertThat(
132+
"Data nodes that are not dedicated to cold/frozen have a default 40mb rate limit",
133+
nodeRecoverySettings().withRole(randomFrom("data", "data_hot", "data_warm", "data_content"))
134+
.withRandomMemory()
135+
.build()
136+
.getMaxBytesPerSec(),
137+
equalTo(DEFAULT_MAX_BYTES_PER_SEC)
138+
);
139+
}
140+
141+
public void testMaxBytesPerSecOnDataNodeWithIndicesRecoveryMaxBytesPerSec() {
142+
final Set<String> roles = new HashSet<>(randomSubsetOf(randomIntBetween(1, 4), "data", "data_hot", "data_warm", "data_content"));
143+
roles.addAll(randomSubsetOf(newHashSet("data_cold", "data_frozen")));
144+
final ByteSizeValue random = randomByteSizeValue();
145+
assertThat(
146+
"Data nodes that are not dedicated to cold/frozen should use the defined rate limit when set",
147+
nodeRecoverySettings().withRoles(roles)
148+
.withIndicesRecoveryMaxBytesPerSec(random)
149+
.withRandomMemory()
150+
.build()
151+
.getMaxBytesPerSec(),
152+
equalTo(random)
153+
);
154+
}
155+
156+
public void testDefaultMaxBytesPerSecOnColdOrFrozenNodeWithOldJvm() {
157+
assertThat(
158+
"Data nodes with only cold/frozen data roles have a default 40mb rate limit on Java version prior to 14",
159+
nodeRecoverySettings().withRoles(
160+
randomFrom(newHashSet("data_cold"), newHashSet("data_frozen"), newHashSet("data_cold", "data_frozen"))
161+
).withJavaVersion(randomFrom("8", "9", "11")).withRandomMemory().build().getMaxBytesPerSec(),
162+
equalTo(DEFAULT_MAX_BYTES_PER_SEC)
163+
);
164+
}
165+
166+
public void testDefaultMaxBytesPerSecOnColdOrFrozenNode() {
167+
final Set<String> dataRoles = randomFrom(
168+
newHashSet("data_cold"),
169+
newHashSet("data_frozen"),
170+
newHashSet("data_cold", "data_frozen")
171+
);
172+
final String recentVersion = JavaVersion.current().compareTo(JavaVersion.parse("14")) < 0 ? "14" : null;
173+
{
174+
assertThat(
175+
"Dedicated cold/frozen data nodes with <= 4GB of RAM have a default 40mb rate limit",
176+
nodeRecoverySettings().withRoles(dataRoles)
177+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(1L, ByteSizeUnit.GB.toBytes(4L))))
178+
.withJavaVersion(recentVersion)
179+
.build()
180+
.getMaxBytesPerSec(),
181+
equalTo(new ByteSizeValue(40, ByteSizeUnit.MB))
182+
);
183+
}
184+
{
185+
assertThat(
186+
"Dedicated cold/frozen data nodes with 4GB < RAM <= 8GB have a default 60mb rate limit",
187+
nodeRecoverySettings().withRoles(dataRoles)
188+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(ByteSizeUnit.GB.toBytes(4L) + 1L, ByteSizeUnit.GB.toBytes(8L))))
189+
.withJavaVersion(recentVersion)
190+
.build()
191+
.getMaxBytesPerSec(),
192+
equalTo(new ByteSizeValue(60, ByteSizeUnit.MB))
193+
);
194+
}
195+
{
196+
assertThat(
197+
"Dedicated cold/frozen data nodes with 8GB < RAM <= 16GB have a default 90mb rate limit",
198+
nodeRecoverySettings().withRoles(dataRoles)
199+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(ByteSizeUnit.GB.toBytes(8L) + 1L, ByteSizeUnit.GB.toBytes(16L))))
200+
.withJavaVersion(recentVersion)
201+
.build()
202+
.getMaxBytesPerSec(),
203+
equalTo(new ByteSizeValue(90, ByteSizeUnit.MB))
204+
);
205+
}
206+
{
207+
assertThat(
208+
"Dedicated cold/frozen data nodes with 16GB < RAM <= 32GB have a default 90mb rate limit",
209+
nodeRecoverySettings().withRoles(dataRoles)
210+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(ByteSizeUnit.GB.toBytes(16L) + 1L, ByteSizeUnit.GB.toBytes(32L))))
211+
.withJavaVersion(recentVersion)
212+
.build()
213+
.getMaxBytesPerSec(),
214+
equalTo(new ByteSizeValue(125, ByteSizeUnit.MB))
215+
);
216+
}
217+
{
218+
assertThat(
219+
"Dedicated cold/frozen data nodes with RAM > 32GB have a default 250mb rate limit",
220+
nodeRecoverySettings().withRoles(dataRoles)
221+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(ByteSizeUnit.GB.toBytes(32L) + 1L, ByteSizeUnit.TB.toBytes(4L))))
222+
.withJavaVersion(recentVersion)
223+
.build()
224+
.getMaxBytesPerSec(),
225+
equalTo(new ByteSizeValue(250, ByteSizeUnit.MB))
226+
);
227+
}
228+
}
229+
230+
public void testMaxBytesPerSecOnColdOrFrozenNodeWithIndicesRecoveryMaxBytesPerSec() {
231+
final ByteSizeValue random = randomByteSizeValue();
232+
assertThat(
233+
"Dedicated cold/frozen data nodes should use the defined rate limit when set",
234+
nodeRecoverySettings().withRoles(
235+
randomFrom(newHashSet("data_cold"), newHashSet("data_frozen"), newHashSet("data_cold", "data_frozen"))
236+
)
237+
.withJavaVersion(JavaVersion.current().compareTo(JavaVersion.parse("14")) < 0 ? "14" : null)
238+
.withMemory(ByteSizeValue.ofBytes(randomLongBetween(1L, ByteSizeUnit.TB.toBytes(4L))))
239+
.withIndicesRecoveryMaxBytesPerSec(random)
240+
.build()
241+
.getMaxBytesPerSec(),
242+
equalTo(random)
243+
);
244+
}
245+
246+
public static ByteSizeValue randomByteSizeValue() {
247+
return new ByteSizeValue(randomLongBetween(0L, Long.MAX_VALUE >> 16));
248+
}
249+
250+
public static ByteSizeValue randomNonZeroByteSizeValue() {
251+
return new ByteSizeValue(randomLongBetween(1L, Long.MAX_VALUE >> 16));
252+
}
253+
254+
static NodeRecoverySettings nodeRecoverySettings() {
255+
return new NodeRecoverySettings();
256+
}
257+
258+
private static class NodeRecoverySettings {
259+
260+
private Set<String> roles;
261+
private ByteSizeValue physicalMemory;
262+
private @Nullable String javaVersion;
263+
private @Nullable ByteSizeValue indicesRecoveryMaxBytesPerSec;
264+
265+
NodeRecoverySettings withRole(String role) {
266+
this.roles = new HashSet<>();
267+
this.roles.add(Objects.requireNonNull(role));
268+
return this;
269+
}
270+
271+
NodeRecoverySettings withRoles(Set<String> roles) {
272+
this.roles = Objects.requireNonNull(roles);
273+
return this;
274+
}
275+
276+
NodeRecoverySettings withMemory(ByteSizeValue physicalMemory) {
277+
this.physicalMemory = Objects.requireNonNull(physicalMemory);
278+
return this;
279+
}
280+
281+
NodeRecoverySettings withRandomMemory() {
282+
return withMemory(ByteSizeValue.ofBytes(randomLongBetween(ByteSizeUnit.GB.toBytes(1L), ByteSizeUnit.TB.toBytes(4L))));
283+
}
284+
285+
NodeRecoverySettings withJavaVersion(String javaVersion) {
286+
this.javaVersion = javaVersion;
287+
return this;
288+
}
289+
290+
NodeRecoverySettings withIndicesRecoveryMaxBytesPerSec(ByteSizeValue indicesRecoveryMaxBytesPerSec) {
291+
this.indicesRecoveryMaxBytesPerSec = Objects.requireNonNull(indicesRecoveryMaxBytesPerSec);
292+
return this;
293+
}
294+
295+
RecoverySettings build() {
296+
final Settings.Builder settings = Settings.builder();
297+
settings.put(TOTAL_PHYSICAL_MEMORY_OVERRIDING_TEST_SETTING.getKey(), Objects.requireNonNull(physicalMemory));
298+
if (roles.isEmpty() == false) {
299+
settings.putList(NODE_ROLES_SETTING.getKey(), new ArrayList<>(roles));
300+
}
301+
if (javaVersion != null) {
302+
settings.put(JAVA_VERSION_OVERRIDING_TEST_SETTING.getKey(), javaVersion);
303+
}
304+
if (indicesRecoveryMaxBytesPerSec != null) {
305+
settings.put(INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), indicesRecoveryMaxBytesPerSec);
306+
}
307+
return new RecoverySettings(settings.build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
308+
}
309+
}
92310
}

0 commit comments

Comments
 (0)