-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Heap Memory Based Worker Flag to stop processing new split when in low memory #20946
Changes from all commits
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 |
---|---|---|
|
@@ -174,12 +174,15 @@ public class TaskExecutor | |
// shared between SplitRunners | ||
private final CounterStat globalCpuTimeMicros = new CounterStat(); | ||
private final CounterStat globalScheduledTimeMicros = new CounterStat(); | ||
private final CounterStat splitSkippedDueToMemoryPressure = new CounterStat(); | ||
|
||
private final TimeStat blockedQuantaWallTime = new TimeStat(MICROSECONDS); | ||
private final TimeStat unblockedQuantaWallTime = new TimeStat(MICROSECONDS); | ||
|
||
private volatile boolean closed; | ||
|
||
private volatile boolean lowMemory; | ||
|
||
@Inject | ||
public TaskExecutor(TaskManagerConfig config, EmbedVersion embedVersion, MultilevelSplitQueue splitQueue) | ||
{ | ||
|
@@ -483,6 +486,13 @@ private void splitFinished(PrioritizedSplitRunner split) | |
|
||
private synchronized void scheduleTaskIfNecessary(TaskHandle taskHandle) | ||
{ | ||
// Worker skip processing split if jvm heap usage crosses configured threhold | ||
// Helps reduce memory pressure on the worker and avoid OOMs | ||
if (isLowMemory()) { | ||
log.debug("Skip task scheduling due to low memory"); | ||
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. Would it be better to add this logging in the isLowMemory method so that relevant extra stats can be logged as well there, providing more insight into that decision. Also maybe the counter update there as well to DRY out this code 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. Both places where we are checking low memory, put a different logging to help understand which flow is being skipped. This will help in debugging issues in the future and that's why it's a |
||
splitSkippedDueToMemoryPressure.update(1); | ||
return; | ||
} | ||
// if task has less than the minimum guaranteed splits running, | ||
// immediately schedule a new split for this task. This assures | ||
// that a task gets its fair amount of consideration (you have to | ||
|
@@ -498,6 +508,14 @@ private synchronized void scheduleTaskIfNecessary(TaskHandle taskHandle) | |
|
||
private synchronized void addNewEntrants() | ||
{ | ||
// Worker skip processing split if jvm heap usage crosses configured threhold | ||
// Helps reduce memory pressure on the worker and avoid OOMs | ||
if (isLowMemory()) { | ||
log.debug("Skip polling next split worker due to low memory"); | ||
splitSkippedDueToMemoryPressure.update(1); | ||
return; | ||
} | ||
|
||
// Ignore intermediate splits when checking minimumNumberOfDrivers. | ||
// Otherwise with (for example) minimumNumberOfDrivers = 100, 200 intermediate splits | ||
// and 100 leaf splits, depending on order of appearing splits, number of | ||
|
@@ -904,6 +922,13 @@ public CounterStat getGlobalCpuTimeMicros() | |
return globalCpuTimeMicros; | ||
} | ||
|
||
@Managed | ||
@Nested | ||
public CounterStat getSplitSkippedDueToMemoryPressure() | ||
{ | ||
return splitSkippedDueToMemoryPressure; | ||
} | ||
|
||
private synchronized int getRunningTasksForLevel(int level) | ||
{ | ||
int count = 0; | ||
|
@@ -1032,4 +1057,14 @@ public ThreadPoolExecutorMBean getProcessorExecutor() | |
{ | ||
return executorMBean; | ||
} | ||
|
||
public void setLowMemory(boolean lowMemory) | ||
{ | ||
this.lowMemory = lowMemory; | ||
} | ||
|
||
public boolean isLowMemory() | ||
{ | ||
return this.lowMemory; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.facebook.presto.memory; | ||
|
||
import com.facebook.airlift.log.Logger; | ||
import com.facebook.presto.execution.TaskManagerConfig; | ||
import com.facebook.presto.execution.executor.TaskExecutor; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
import javax.inject.Inject; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.lang.management.MemoryUsage; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.facebook.airlift.concurrent.Threads.daemonThreadsNamed; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.concurrent.Executors.newScheduledThreadPool; | ||
|
||
public class LowMemoryMonitor | ||
{ | ||
private static final Logger log = Logger.get(LowMemoryMonitor.class); | ||
private final ScheduledExecutorService lowMemoryExecutor = newScheduledThreadPool(1, daemonThreadsNamed("low-memory-monitor-executor")); | ||
private final TaskExecutor taskExecutor; | ||
private final double threshold; | ||
private static final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); | ||
|
||
@Inject | ||
public LowMemoryMonitor(TaskExecutor taskExecutor, TaskManagerConfig taskManagerConfig) | ||
{ | ||
this.taskExecutor = requireNonNull(taskExecutor, "taskExecutor is null"); | ||
this.threshold = taskManagerConfig.getMemoryBasedSlowDownThreshold(); | ||
} | ||
|
||
@PostConstruct | ||
public void start() | ||
{ | ||
if (threshold < 1.0) { | ||
lowMemoryExecutor.scheduleWithFixedDelay(() -> checkLowMemory(), 1, 1, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
@PreDestroy | ||
public void stop() | ||
{ | ||
lowMemoryExecutor.shutdown(); | ||
} | ||
|
||
private void checkLowMemory() | ||
{ | ||
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); | ||
|
||
long usedMemory = memoryUsage.getUsed(); | ||
long maxMemory = memoryUsage.getMax(); | ||
long memoryThreshold = (long) (maxMemory * threshold); | ||
|
||
if (usedMemory > memoryThreshold) { | ||
if (!taskExecutor.isLowMemory()) { | ||
log.debug("Enabling Low Memory: Used: %s Max: %s Threshold: %s", usedMemory, maxMemory, memoryThreshold); | ||
taskExecutor.setLowMemory(true); | ||
} | ||
} | ||
else { | ||
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. nit: else and if can be merged. |
||
if (taskExecutor.isLowMemory()) { | ||
log.debug("Disabling Low Memory: Used: %s Max: %s Threshold: %s", usedMemory, maxMemory, memoryThreshold); | ||
taskExecutor.setLowMemory(false); | ||
} | ||
} | ||
} | ||
} |
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.
Please add a comment saying it should only be updated from 1 thread from the lowMemoryExecutor