Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.tez.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class TezRuntimeShutdownHandler {
private static final Logger LOG = LoggerFactory.getLogger(TezRuntimeShutdownHandler.class);

private static final List<Runnable> shutdownTasks = new ArrayList<>();

private TezRuntimeShutdownHandler() {
}

public static void addShutdownTask(Runnable r) {
shutdownTasks.add(r);
}

public static synchronized void shutdown() {
LOG.info("Handling {} shutdown tasks", shutdownTasks.size());
for (Runnable shutdownTask : shutdownTasks) {
shutdownTask.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.tez.runtime.api.impl.ExecutionContextImpl;
import org.apache.tez.runtime.common.objectregistry.ObjectRegistryImpl;
import org.apache.tez.runtime.internals.api.TaskReporterInterface;
import org.apache.tez.util.TezRuntimeShutdownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -390,8 +391,10 @@ public void shutdown() {
LOG.info("Shutting down container {}", containerIdString);
// It's possible that there's pending tasks on the executor. Those should be cancelled.
List<Runnable> pendingRunnables = executor.shutdownNow();
LOG.info("There are {} runnables in shared executor, cancelling those...", pendingRunnables.size());
for (Runnable r : pendingRunnables) {
LOG.info("Cancelling pending runnables during TezChild shutdown for containerId={}", containerIdString);
LOG.info("Cancelling pending runnable ({}) during TezChild shutdown for containerId={}", r.hashCode(),
containerIdString);
((FutureTask)r).cancel(false);
}
if (taskReporter != null) {
Expand All @@ -401,6 +404,8 @@ public void shutdown() {
RPC.stopProxy(umbilical);
}
}
TezRuntimeShutdownHandler.shutdown();
LOG.info("TezChild shutdown finished");
}

public static class ContainerExecutionResult {
Expand Down Expand Up @@ -522,7 +527,8 @@ public static void main(String[] args) throws IOException, InterruptedException,
System.getenv(), pid, new ExecutionContextImpl(System.getenv(Environment.NM_HOST.name())),
credentials, Runtime.getRuntime().maxMemory(), System
.getenv(ApplicationConstants.Environment.USER.toString()), null, true, hadoopShim);
tezChild.run();
ContainerExecutionResult result = tezChild.run();
LOG.info("TezChild is about to exit from main(), run() returned result: {}", result.toString());
}

private void handleError(Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.tez.runtime.library.common.security.SecureShuffleUtils;
import org.apache.tez.runtime.library.common.shuffle.orderedgrouped.ShuffleHeader;
import org.apache.tez.util.StopWatch;
import org.apache.tez.util.TezRuntimeShutdownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -102,6 +103,16 @@ private void initClient(HttpConnectionParams httpConnParams) throws IOException
.build();
DefaultAsyncHttpClientConfig config = builder.build();
httpAsyncClient = new DefaultAsyncHttpClient(config);
TezRuntimeShutdownHandler.addShutdownTask(() -> {
try {
if (httpAsyncClient != null) {
httpAsyncClient.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should httpAsyncClient be set to "null" explicitly as well? Without it, it may create issues during container reuse. i.e httpAsyncClient will not be null, but in closed state & system would try to use it leading to exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @rbalamohan, makes sense, fixed

httpAsyncClient = null;
}
} catch (IOException e) {
LOG.warn("Error while closing async client (this won't block shutdown)", e);
}
});
}
}
}
Expand Down