-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Circuit-break based on real memory usage #31767
Changes from 4 commits
80644aa
d9b6466
403aae6
6eb4f1c
32977a0
7c4f783
664a7a6
580cf98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.benchmark.indices.breaker; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Threads; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Fork(3) | ||
@Warmup(iterations = 10) | ||
@Measurement(iterations = 10) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Benchmark) | ||
@SuppressWarnings("unused") //invoked by benchmarking framework | ||
public class MemoryStatsBenchmark { | ||
private static final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); | ||
|
||
@Param({"0", "16", "256", "4096"}) | ||
private int tokens; | ||
|
||
@Benchmark | ||
public void baseline() { | ||
Blackhole.consumeCPU(tokens); | ||
} | ||
|
||
@Benchmark | ||
@Threads(1) | ||
public long getMemoryStats_01() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(2) | ||
public long getMemoryStats_02() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(4) | ||
public long getMemoryStats_04() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(8) | ||
public long getMemoryStats_08() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(16) | ||
public long getMemoryStats_16() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(32) | ||
public long getMemoryStats_32() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(64) | ||
public long getMemoryStats_64() { | ||
Blackhole.consumeCPU(tokens); | ||
return memoryMXBean.getHeapMemoryUsage().getUsed(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,11 @@ | |
import org.elasticsearch.common.settings.Setting; | ||
import org.elasticsearch.common.settings.Setting.Property; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
@@ -44,10 +47,24 @@ public class HierarchyCircuitBreakerService extends CircuitBreakerService { | |
|
||
private static final String CHILD_LOGGER_PREFIX = "org.elasticsearch.indices.breaker."; | ||
|
||
private static final MemoryMXBean MEMORY_MX_BEAN = ManagementFactory.getMemoryMXBean(); | ||
|
||
private final ConcurrentMap<String, CircuitBreaker> breakers = new ConcurrentHashMap<>(); | ||
|
||
public static final Setting<Boolean> USE_REAL_MEMORY_USAGE_SETTING = | ||
Setting.boolSetting("indices.breaker.total.use_real_memory", settings -> { | ||
ByteSizeValue maxHeapSize = new ByteSizeValue(MEMORY_MX_BEAN.getHeapMemoryUsage().getMax()); | ||
return Boolean.toString(maxHeapSize.compareTo(new ByteSizeValue(1, ByteSizeUnit.GB)) < 0); | ||
}, Property.NodeScope); | ||
|
||
public static final Setting<ByteSizeValue> TOTAL_CIRCUIT_BREAKER_LIMIT_SETTING = | ||
Setting.memorySizeSetting("indices.breaker.total.limit", "70%", Property.Dynamic, Property.NodeScope); | ||
Setting.memorySizeSetting("indices.breaker.total.limit", settings -> { | ||
if (USE_REAL_MEMORY_USAGE_SETTING.get(settings)) { | ||
return "95%"; | ||
} else { | ||
return "70%"; | ||
} | ||
}, Property.Dynamic, Property.NodeScope); | ||
|
||
public static final Setting<ByteSizeValue> FIELDDATA_CIRCUIT_BREAKER_LIMIT_SETTING = | ||
Setting.memorySizeSetting("indices.breaker.fielddata.limit", "60%", Property.Dynamic, Property.NodeScope); | ||
|
@@ -77,6 +94,7 @@ public class HierarchyCircuitBreakerService extends CircuitBreakerService { | |
public static final Setting<CircuitBreaker.Type> IN_FLIGHT_REQUESTS_CIRCUIT_BREAKER_TYPE_SETTING = | ||
new Setting<>("network.breaker.inflight_requests.type", "memory", CircuitBreaker.Type::parseValue, Property.NodeScope); | ||
|
||
private final boolean trackRealMemoryUsage; | ||
private volatile BreakerSettings parentSettings; | ||
private volatile BreakerSettings fielddataSettings; | ||
private volatile BreakerSettings inFlightRequestsSettings; | ||
|
@@ -120,6 +138,8 @@ public HierarchyCircuitBreakerService(Settings settings, ClusterSettings cluster | |
logger.trace("parent circuit breaker with settings {}", this.parentSettings); | ||
} | ||
|
||
this.trackRealMemoryUsage = USE_REAL_MEMORY_USAGE_SETTING.get(settings); | ||
|
||
registerBreaker(this.requestSettings); | ||
registerBreaker(this.fielddataSettings); | ||
registerBreaker(this.inFlightRequestsSettings); | ||
|
@@ -191,17 +211,15 @@ public CircuitBreaker getBreaker(String name) { | |
|
||
@Override | ||
public AllCircuitBreakerStats stats() { | ||
long parentEstimated = 0; | ||
List<CircuitBreakerStats> allStats = new ArrayList<>(this.breakers.size()); | ||
// Gather the "estimated" count for the parent breaker by adding the | ||
// estimations for each individual breaker | ||
for (CircuitBreaker breaker : this.breakers.values()) { | ||
allStats.add(stats(breaker.getName())); | ||
parentEstimated += breaker.getUsed(); | ||
} | ||
// Manually add the parent breaker settings since they aren't part of the breaker map | ||
allStats.add(new CircuitBreakerStats(CircuitBreaker.PARENT, parentSettings.getLimit(), | ||
parentEstimated, 1.0, parentTripCount.get())); | ||
parentUsed(0L), 1.0, parentTripCount.get())); | ||
return new AllCircuitBreakerStats(allStats.toArray(new CircuitBreakerStats[allStats.size()])); | ||
} | ||
|
||
|
@@ -211,15 +229,28 @@ public CircuitBreakerStats stats(String name) { | |
return new CircuitBreakerStats(breaker.getName(), breaker.getLimit(), breaker.getUsed(), breaker.getOverhead(), breaker.getTrippedCount()); | ||
} | ||
|
||
private long parentUsed(long newBytesReserved) { | ||
if (this.trackRealMemoryUsage) { | ||
return currentMemoryUsage() + newBytesReserved; | ||
} else { | ||
long parentEstimated = 0; | ||
for (CircuitBreaker breaker : this.breakers.values()) { | ||
parentEstimated += breaker.getUsed() * breaker.getOverhead(); | ||
} | ||
return parentEstimated; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because this is for the current strategy which sums up the total memory reserved by all child circuit breakers. As the corresponding child circuit breaker accounts for that amount of memory already, we do not need to do that again in the parent breaker. For the new strategy which is based on real memory usage, we do not rely on the child memory circuit breakers but rather only on current memory usage. Hence, we need to consider the amount of memory that is about to be reserved here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} | ||
|
||
//package private to allow overriding it in tests | ||
long currentMemoryUsage() { | ||
return MEMORY_MX_BEAN.getHeapMemoryUsage().getUsed(); | ||
} | ||
|
||
/** | ||
* Checks whether the parent breaker has been tripped | ||
*/ | ||
public void checkParentLimit(String label) throws CircuitBreakingException { | ||
long totalUsed = 0; | ||
for (CircuitBreaker breaker : this.breakers.values()) { | ||
totalUsed += (breaker.getUsed() * breaker.getOverhead()); | ||
} | ||
|
||
public void checkParentLimit(long newBytesReserved, String label) throws CircuitBreakingException { | ||
long totalUsed = parentUsed(newBytesReserved); | ||
long parentLimit = this.parentSettings.getLimit(); | ||
if (totalUsed > parentLimit) { | ||
this.parentTripCount.incrementAndGet(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we talked about today, I think we may want to think about why this defaults to off and put it in the documentation (or at least mention it in this PR). Right now it comes off as more of "it's off by default for > 1gb heaps for no reason in particular" which seems strange given that we're trying to be as safe as possible by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy to change the default to the real memory circuit breaker for all cases but initially I wanted to be a bit more conservative. I felt that for larger deployments it is ok to stick with the current implementation. I also thought that it might be one thing less to consider in a major version upgrade if we keep the default for now for those deployments.
My idea behind changing it only for lower heap sizes below 1GB deviation between real memory usage and actively tracked memory usage starts to matter more and we want to ensure we push back accordingly. I did not choose this heap size by coincidence but based on our benchmarks. With 1GB heap, Elasticsearch can handle our macrobenchmark suite but it starts to struggle below that point (e.g. for 768MB).
I could ask how the rest of the team feels turning it on in all cases. Wdyt?