-
Notifications
You must be signed in to change notification settings - Fork 374
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
[CELEBORN-1815] Support UnpooledByteBufAllocator #3043
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 |
---|---|---|
|
@@ -23,19 +23,17 @@ | |
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import io.netty.buffer.PoolArenaMetric; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.buffer.PooledByteBufAllocatorMetric; | ||
import io.netty.buffer.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.celeborn.common.metrics.source.AbstractSource; | ||
|
||
/** A Netty memory metrics class to collect metrics from Netty PooledByteBufAllocator. */ | ||
/** A Netty memory metrics class to collect metrics from Netty ByteBufAllocator. */ | ||
public class NettyMemoryMetrics { | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
private final PooledByteBufAllocator pooledAllocator; | ||
private final ByteBufAllocator allocator; | ||
|
||
private final boolean verboseMetricsEnabled; | ||
|
||
|
@@ -69,71 +67,87 @@ public class NettyMemoryMetrics { | |
} | ||
|
||
public NettyMemoryMetrics( | ||
PooledByteBufAllocator pooledAllocator, | ||
ByteBufAllocator allocator, | ||
String metricPrefix, | ||
boolean verboseMetricsEnabled, | ||
AbstractSource source, | ||
Map<String, String> labels) { | ||
this.pooledAllocator = pooledAllocator; | ||
this.allocator = allocator; | ||
this.metricPrefix = metricPrefix; | ||
this.verboseMetricsEnabled = verboseMetricsEnabled; | ||
this.source = source; | ||
this.labels = labels; | ||
|
||
registerMetrics(this.pooledAllocator); | ||
registerMetrics(); | ||
} | ||
|
||
private void registerMetrics(PooledByteBufAllocator allocator) { | ||
PooledByteBufAllocatorMetric pooledAllocatorMetric = allocator.metric(); | ||
|
||
private void registerMetrics() { | ||
// Register general metrics. | ||
if (source != null) { | ||
logger.debug("setup netty metrics"); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedHeapMemory"), | ||
labels, | ||
pooledAllocatorMetric::usedHeapMemory); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedDirectMemory"), | ||
labels, | ||
pooledAllocatorMetric::usedDirectMemory); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numHeapArenas"), | ||
labels, | ||
pooledAllocatorMetric::numHeapArenas); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numDirectArenas"), | ||
labels, | ||
pooledAllocatorMetric::numDirectArenas); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "tinyCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::tinyCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "smallCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::smallCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "normalCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::normalCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numThreadLocalCaches"), | ||
labels, | ||
pooledAllocatorMetric::numThreadLocalCaches); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "chunkSize"), labels, pooledAllocatorMetric::chunkSize); | ||
if (verboseMetricsEnabled) { | ||
int directArenaIndex = 0; | ||
for (PoolArenaMetric metric : pooledAllocatorMetric.directArenas()) { | ||
registerArenaMetric(metric, "directArena" + directArenaIndex); | ||
directArenaIndex++; | ||
} | ||
if (allocator instanceof UnpooledByteBufAllocator) { | ||
logger.debug("Setup netty metrics for UnpooledByteBufAllocator"); | ||
ByteBufAllocatorMetric unpooledMetric = ((UnpooledByteBufAllocator) allocator).metric(); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedHeapMemory"), | ||
labels, | ||
unpooledMetric::usedHeapMemory); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedDirectMemory"), | ||
labels, | ||
unpooledMetric::usedDirectMemory); | ||
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. UnpooledByteBufAllocator only supports these two metrics |
||
} else if (allocator instanceof PooledByteBufAllocator) { | ||
logger.debug("Setup netty metrics for PooledByteBufAllocator"); | ||
PooledByteBufAllocatorMetric pooledAllocatorMetric = | ||
((PooledByteBufAllocator) allocator).metric(); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedHeapMemory"), | ||
labels, | ||
pooledAllocatorMetric::usedHeapMemory); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "usedDirectMemory"), | ||
labels, | ||
pooledAllocatorMetric::usedDirectMemory); | ||
|
||
int heapArenaIndex = 0; | ||
for (PoolArenaMetric metric : pooledAllocatorMetric.heapArenas()) { | ||
registerArenaMetric(metric, "heapArena" + heapArenaIndex); | ||
heapArenaIndex++; | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numHeapArenas"), | ||
labels, | ||
pooledAllocatorMetric::numHeapArenas); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numDirectArenas"), | ||
labels, | ||
pooledAllocatorMetric::numDirectArenas); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "tinyCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::tinyCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "smallCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::smallCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "normalCacheSize"), | ||
labels, | ||
pooledAllocatorMetric::normalCacheSize); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "numThreadLocalCaches"), | ||
labels, | ||
pooledAllocatorMetric::numThreadLocalCaches); | ||
source.addGauge( | ||
MetricRegistry.name(metricPrefix, "chunkSize"), | ||
labels, | ||
pooledAllocatorMetric::chunkSize); | ||
if (verboseMetricsEnabled) { | ||
int directArenaIndex = 0; | ||
for (PoolArenaMetric metric : pooledAllocatorMetric.directArenas()) { | ||
registerArenaMetric(metric, "directArena" + directArenaIndex); | ||
directArenaIndex++; | ||
} | ||
|
||
int heapArenaIndex = 0; | ||
for (PoolArenaMetric metric : pooledAllocatorMetric.heapArenas()) { | ||
registerArenaMetric(metric, "heapArena" + heapArenaIndex); | ||
heapArenaIndex++; | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,9 @@ | |
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
import io.netty.buffer.ByteBufAllocator; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.buffer.UnpooledByteBufAllocator; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.ServerChannel; | ||
|
@@ -42,9 +44,8 @@ | |
|
||
/** Utilities for creating various Netty constructs based on whether we're using EPOLL or NIO. */ | ||
public class NettyUtils { | ||
private static final PooledByteBufAllocator[] _sharedPooledByteBufAllocator = | ||
new PooledByteBufAllocator[2]; | ||
private static ConcurrentHashMap<String, Integer> allocatorsIndex = | ||
private static final ByteBufAllocator[] _sharedByteBufAllocator = new ByteBufAllocator[2]; | ||
private static final ConcurrentHashMap<String, Integer> allocatorsIndex = | ||
JavaUtils.newConcurrentHashMap(); | ||
/** Creates a new ThreadFactory which prefixes each thread with the given name. */ | ||
public static ThreadFactory createThreadFactory(String threadPoolPrefix) { | ||
|
@@ -98,58 +99,69 @@ public static String getRemoteAddress(Channel channel) { | |
} | ||
|
||
/** | ||
* Create a pooled ByteBuf allocator but disables the thread-local cache. Thread-local caches are | ||
* disabled for TransportClients because the ByteBufs are allocated by the event loop thread, but | ||
* released by the executor thread rather than the event loop thread. Those thread-local caches | ||
* actually delay the recycling of buffers, leading to larger memory usage. | ||
* Create a ByteBufAllocator that respects the parameters | ||
* | ||
* @param pooled If true, create a PooledByteBufAllocator, otherwise UnpooledByteBufAllocator | ||
* @param allowDirectBufs If true and platform supports, allocate ByteBuf in direct memory, | ||
* otherwise in heap memory. | ||
* @param allowCache If true, enable thread-local cache, it only take effect for | ||
* PooledByteBufAllocator. | ||
* @param numCores Number of heap/direct arenas, 0 means use number of cpu cores, it only take | ||
* effect for PooledByteBufAllocator. | ||
*/ | ||
private static PooledByteBufAllocator createPooledByteBufAllocator( | ||
boolean allowDirectBufs, boolean allowCache, int numCores) { | ||
if (numCores == 0) { | ||
numCores = Runtime.getRuntime().availableProcessors(); | ||
private static ByteBufAllocator createByteBufAllocator( | ||
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. The method comments should also be updated 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. update: move the existing comment to the caller side, add new comments to explain each parameters |
||
boolean pooled, boolean allowDirectBufs, boolean allowCache, int numCores) { | ||
if (pooled) { | ||
if (numCores == 0) { | ||
numCores = Runtime.getRuntime().availableProcessors(); | ||
} | ||
return new PooledByteBufAllocator( | ||
allowDirectBufs && PlatformDependent.directBufferPreferred(), | ||
Math.min(PooledByteBufAllocator.defaultNumHeapArena(), numCores), | ||
Math.min(PooledByteBufAllocator.defaultNumDirectArena(), allowDirectBufs ? numCores : 0), | ||
PooledByteBufAllocator.defaultPageSize(), | ||
PooledByteBufAllocator.defaultMaxOrder(), | ||
allowCache ? PooledByteBufAllocator.defaultSmallCacheSize() : 0, | ||
allowCache ? PooledByteBufAllocator.defaultNormalCacheSize() : 0, | ||
allowCache && PooledByteBufAllocator.defaultUseCacheForAllThreads()); | ||
} else { | ||
return new UnpooledByteBufAllocator( | ||
allowDirectBufs && PlatformDependent.directBufferPreferred()); | ||
} | ||
return new PooledByteBufAllocator( | ||
allowDirectBufs && PlatformDependent.directBufferPreferred(), | ||
Math.min(PooledByteBufAllocator.defaultNumHeapArena(), numCores), | ||
Math.min(PooledByteBufAllocator.defaultNumDirectArena(), allowDirectBufs ? numCores : 0), | ||
PooledByteBufAllocator.defaultPageSize(), | ||
PooledByteBufAllocator.defaultMaxOrder(), | ||
allowCache ? PooledByteBufAllocator.defaultSmallCacheSize() : 0, | ||
allowCache ? PooledByteBufAllocator.defaultNormalCacheSize() : 0, | ||
allowCache && PooledByteBufAllocator.defaultUseCacheForAllThreads()); | ||
} | ||
|
||
/** | ||
* Returns the lazily created shared pooled ByteBuf allocator for the specified allowCache | ||
* parameter value. | ||
*/ | ||
public static synchronized PooledByteBufAllocator getSharedPooledByteBufAllocator( | ||
public static synchronized ByteBufAllocator getSharedByteBufAllocator( | ||
CelebornConf conf, AbstractSource source, boolean allowCache) { | ||
final int index = allowCache ? 0 : 1; | ||
if (_sharedPooledByteBufAllocator[index] == null) { | ||
_sharedPooledByteBufAllocator[index] = | ||
createPooledByteBufAllocator(true, allowCache, conf.networkAllocatorArenas()); | ||
if (_sharedByteBufAllocator[index] == null) { | ||
_sharedByteBufAllocator[index] = | ||
createByteBufAllocator( | ||
conf.networkMemoryAllocatorPooled(), true, allowCache, conf.networkAllocatorArenas()); | ||
if (source != null) { | ||
new NettyMemoryMetrics( | ||
_sharedPooledByteBufAllocator[index], | ||
_sharedByteBufAllocator[index], | ||
"shared-pool-" + index, | ||
conf.networkAllocatorVerboseMetric(), | ||
source, | ||
Collections.emptyMap()); | ||
} | ||
} | ||
return _sharedPooledByteBufAllocator[index]; | ||
return _sharedByteBufAllocator[index]; | ||
} | ||
|
||
public static PooledByteBufAllocator getPooledByteBufAllocator( | ||
public static ByteBufAllocator getByteBufAllocator( | ||
TransportConf conf, AbstractSource source, boolean allowCache) { | ||
return getPooledByteBufAllocator(conf, source, allowCache, 0); | ||
return getByteBufAllocator(conf, source, allowCache, 0); | ||
} | ||
|
||
public static PooledByteBufAllocator getPooledByteBufAllocator( | ||
public static ByteBufAllocator getByteBufAllocator( | ||
TransportConf conf, AbstractSource source, boolean allowCache, int coreNum) { | ||
if (conf.getCelebornConf().networkShareMemoryAllocator()) { | ||
return getSharedPooledByteBufAllocator( | ||
return getSharedByteBufAllocator( | ||
conf.getCelebornConf(), | ||
source, | ||
allowCache && conf.getCelebornConf().networkMemoryAllocatorAllowCache()); | ||
|
@@ -160,8 +172,12 @@ public static PooledByteBufAllocator getPooledByteBufAllocator( | |
} else { | ||
arenas = conf.getCelebornConf().networkAllocatorArenas(); | ||
} | ||
PooledByteBufAllocator allocator = | ||
createPooledByteBufAllocator(conf.preferDirectBufs(), allowCache, arenas); | ||
ByteBufAllocator allocator = | ||
createByteBufAllocator( | ||
conf.getCelebornConf().networkMemoryAllocatorPooled(), | ||
conf.preferDirectBufs(), | ||
allowCache, | ||
arenas); | ||
if (source != null) { | ||
String poolName = "default-netty-pool"; | ||
Map<String, String> labels = new HashMap<>(); | ||
|
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.
Interesting... Flink client overrides it to forcibly use UnpooledByteBufAllocator
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.
This is related to this PR. The Flink client's memory is quite limited.
#1324
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.
@FMX Thanks for the information. BTW, it's better to leave some brief comments to explain the special logic