Skip to content

Commit

Permalink
Merge branch 'trunk' into YARN-11225
Browse files Browse the repository at this point in the history
  • Loading branch information
slfan1989 authored Dec 15, 2022
2 parents ef0f580 + 6172c31 commit 38dca6b
Show file tree
Hide file tree
Showing 34 changed files with 1,759 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ private AuditConstants() {
*/
public static final String PARAM_PROCESS = "ps";

/**
* Header: Range for GET request data: {@value}.
*/
public static final String PARAM_RANGE = "rg";

/**
* Task Attempt ID query header: {@value}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.apache.hadoop.fs.impl;

import java.lang.ref.WeakReference;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.metrics2.MetricsCollector;
import org.apache.hadoop.metrics2.MetricsSource;

import static java.util.Objects.requireNonNull;

/**
* A weak referenced metrics source which avoids hanging on to large objects
* if somehow they don't get fully closed/cleaned up.
* The JVM may clean up all objects which are only weakly referenced whenever
* it does a GC, <i>even if there is no memory pressure</i>.
* To avoid these refs being removed, always keep a strong reference around
* somewhere.
*/
@InterfaceAudience.Private
public class WeakRefMetricsSource implements MetricsSource {

/**
* Name to know when unregistering.
*/
private final String name;

/**
* Underlying metrics source.
*/
private final WeakReference<MetricsSource> sourceWeakReference;

/**
* Constructor.
* @param name Name to know when unregistering.
* @param source metrics source
*/
public WeakRefMetricsSource(final String name, final MetricsSource source) {
this.name = name;
this.sourceWeakReference = new WeakReference<>(requireNonNull(source));
}

/**
* If the weak reference is non null, update the metrics.
* @param collector to contain the resulting metrics snapshot
* @param all if true, return all metrics even if unchanged.
*/
@Override
public void getMetrics(final MetricsCollector collector, final boolean all) {
MetricsSource metricsSource = sourceWeakReference.get();
if (metricsSource != null) {
metricsSource.getMetrics(collector, all);
}
}

/**
* Name to know when unregistering.
* @return the name passed in during construction.
*/
public String getName() {
return name;
}

/**
* Get the source, will be null if the reference has been GC'd
* @return the source reference
*/
public MetricsSource getSource() {
return sourceWeakReference.get();
}

@Override
public String toString() {
return "WeakRefMetricsSource{" +
"name='" + name + '\'' +
", sourceWeakReference is " +
(sourceWeakReference.get() == null ? "unset" : "set") +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.apache.hadoop.classification.VisibleForTesting;
import org.apache.hadoop.oncrpc.RpcAcceptedReply.AcceptState;
import org.apache.hadoop.oncrpc.security.VerifierNone;
Expand Down Expand Up @@ -163,8 +164,16 @@ public void stopDaemons() {}
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
RpcInfo info = (RpcInfo) msg;
try {
channelRead(ctx, info);
} finally {
ReferenceCountUtil.release(info.data());
}
}

private void channelRead(ChannelHandlerContext ctx, RpcInfo info)
throws Exception {
RpcCall call = (RpcCall) info.header();

SocketAddress remoteAddress = info.remoteAddress();
if (LOG.isTraceEnabled()) {
LOG.trace(program + " procedure #" + call.getProcedure());
Expand Down Expand Up @@ -256,4 +265,4 @@ public int getPort() {
public int getPortmapUdpTimeoutMillis() {
return portmapUdpTimeoutMillis;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
RpcInfo info = null;
try {
RpcCall callHeader = RpcCall.read(in);
ByteBuf dataBuffer = Unpooled.wrappedBuffer(in.buffer()
.slice());
ByteBuf dataBuffer = buf.slice(b.position(), b.remaining());

info = new RpcInfo(callHeader, dataBuffer, ctx, ctx.channel(),
remoteAddress);
} catch (Exception exc) {
LOG.info("Malformed RPC request from " + remoteAddress);
} finally {
buf.release();
// only release buffer if it is not passed to downstream handler
if (info == null) {
buf.release();
}
}

if (info != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
import org.apache.hadoop.fs.statistics.DurationTracker;
import org.apache.hadoop.fs.statistics.DurationTrackerFactory;
import org.apache.hadoop.fs.statistics.IOStatistics;
import org.apache.hadoop.fs.statistics.IOStatisticsLogging;
import org.apache.hadoop.fs.statistics.IOStatisticsSource;
import org.apache.hadoop.fs.statistics.IOStatisticsContext;
import org.apache.hadoop.fs.statistics.impl.IOStatisticsStore;
Expand Down Expand Up @@ -459,6 +458,13 @@ public void initialize(URI name, Configuration originalConf)
AuditSpan span = null;
try {
LOG.debug("Initializing S3AFileSystem for {}", bucket);
if (LOG.isTraceEnabled()) {
// log a full trace for deep diagnostics of where an object is created,
// for tracking down memory leak issues.
LOG.trace("Filesystem for {} created; fs.s3a.impl.disable.cache = {}",
name, originalConf.getBoolean("fs.s3a.impl.disable.cache", false),
new RuntimeException(super.toString()));
}
// clone the configuration into one with propagated bucket options
Configuration conf = propagateBucketOptions(originalConf, bucket);
// HADOOP-17894. remove references to s3a stores in JCEKS credentials.
Expand Down Expand Up @@ -3999,22 +4005,18 @@ public void close() throws IOException {
}
isClosed = true;
LOG.debug("Filesystem {} is closed", uri);
if (getConf() != null) {
String iostatisticsLoggingLevel =
getConf().getTrimmed(IOSTATISTICS_LOGGING_LEVEL,
IOSTATISTICS_LOGGING_LEVEL_DEFAULT);
logIOStatisticsAtLevel(LOG, iostatisticsLoggingLevel, getIOStatistics());
}
try {
super.close();
} finally {
stopAllServices();
}
// Log IOStatistics at debug.
if (LOG.isDebugEnabled()) {
// robust extract and convert to string
LOG.debug("Statistics for {}: {}", uri,
IOStatisticsLogging.ioStatisticsToPrettyString(getIOStatistics()));
// log IO statistics, including of any file deletion during
// superclass close
if (getConf() != null) {
String iostatisticsLoggingLevel =
getConf().getTrimmed(IOSTATISTICS_LOGGING_LEVEL,
IOSTATISTICS_LOGGING_LEVEL_DEFAULT);
logIOStatisticsAtLevel(LOG, iostatisticsLoggingLevel, getIOStatistics());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.impl.WeakRefMetricsSource;
import org.apache.hadoop.fs.s3a.statistics.BlockOutputStreamStatistics;
import org.apache.hadoop.fs.s3a.statistics.ChangeTrackerStatistics;
import org.apache.hadoop.fs.s3a.statistics.CommitterStatistics;
Expand Down Expand Up @@ -160,7 +161,10 @@ public class S3AInstrumentation implements Closeable, MetricsSource,

private final DurationTrackerFactory durationTrackerFactory;

private String metricsSourceName;
/**
* Weak reference so there's no back reference to the instrumentation.
*/
private WeakRefMetricsSource metricsSourceReference;

private final MetricsRegistry registry =
new MetricsRegistry("s3aFileSystem").setContext(CONTEXT);
Expand Down Expand Up @@ -233,19 +237,33 @@ public S3AInstrumentation(URI name) {
new MetricDurationTrackerFactory());
}

/**
* Get the current metrics system; demand creating.
* @return a metric system, creating if need be.
*/
@VisibleForTesting
public MetricsSystem getMetricsSystem() {
static MetricsSystem getMetricsSystem() {
synchronized (METRICS_SYSTEM_LOCK) {
if (metricsSystem == null) {
metricsSystem = new MetricsSystemImpl();
metricsSystem.init(METRICS_SYSTEM_NAME);
LOG.debug("Metrics system inited {}", metricsSystem);
}
}
return metricsSystem;
}

/**
* Register this instance as a metrics source.
* Does the instrumentation have a metrics system?
* @return true if the metrics system is present.
*/
@VisibleForTesting
static boolean hasMetricSystem() {
return metricsSystem != null;
}

/**
* Register this instance as a metrics source via a weak reference.
* @param name s3a:// URI for the associated FileSystem instance
*/
private void registerAsMetricsSource(URI name) {
Expand All @@ -257,8 +275,9 @@ private void registerAsMetricsSource(URI name) {
number = ++metricsSourceNameCounter;
}
String msName = METRICS_SOURCE_BASENAME + number;
metricsSourceName = msName + "-" + name.getHost();
metricsSystem.register(metricsSourceName, "", this);
String metricsSourceName = msName + "-" + name.getHost();
metricsSourceReference = new WeakRefMetricsSource(metricsSourceName, this);
metricsSystem.register(metricsSourceName, "", metricsSourceReference);
}

/**
Expand Down Expand Up @@ -680,19 +699,42 @@ public void getMetrics(MetricsCollector collector, boolean all) {
registry.snapshot(collector.addRecord(registry.info().name()), true);
}

/**
* if registered with the metrics, return the
* name of the source.
* @return the name of the metrics, or null if this instance is not bonded.
*/
public String getMetricSourceName() {
return metricsSourceReference != null
? metricsSourceReference.getName()
: null;
}

public void close() {
synchronized (METRICS_SYSTEM_LOCK) {
// it is critical to close each quantile, as they start a scheduled
// task in a shared thread pool.
throttleRateQuantile.stop();
metricsSystem.unregisterSource(metricsSourceName);
metricsSourceActiveCounter--;
int activeSources = metricsSourceActiveCounter;
if (activeSources == 0) {
LOG.debug("Shutting down metrics publisher");
metricsSystem.publishMetricsNow();
metricsSystem.shutdown();
metricsSystem = null;
if (metricsSourceReference != null) {
// get the name
String name = metricsSourceReference.getName();
LOG.debug("Unregistering metrics for {}", name);
// then set to null so a second close() is a noop here.
metricsSourceReference = null;
synchronized (METRICS_SYSTEM_LOCK) {
// it is critical to close each quantile, as they start a scheduled
// task in a shared thread pool.
if (metricsSystem == null) {
LOG.debug("there is no metric system to unregister {} from", name);
return;
}
throttleRateQuantile.stop();

metricsSystem.unregisterSource(name);
metricsSourceActiveCounter--;
int activeSources = metricsSourceActiveCounter;
if (activeSources == 0) {
LOG.debug("Shutting down metrics publisher");
metricsSystem.publishMetricsNow();
metricsSystem.shutdown();
metricsSystem = null;
}
}
}
}
Expand Down
Loading

0 comments on commit 38dca6b

Please sign in to comment.