From dad73b76c07025264d8daf1d42436f8ed6395995 Mon Sep 17 00:00:00 2001 From: jokercurry <84573424+smallzhongfeng@users.noreply.github.com> Date: Sat, 4 Feb 2023 10:29:19 +0800 Subject: [PATCH] YARN-11419. Remove redundant exception capture in NMClientAsyncImpl and improve readability in ContainerShellWebSocket, etc (#5309) Co-authored-by: smallzhongfeng <982458633@qq.com> Reviewed-by: Shilun Fan Signed-off-by: Shilun Fan --- .../client/api/ContainerShellWebSocket.java | 4 ++-- .../api/async/impl/NMClientAsyncImpl.java | 20 +++---------------- .../api/impl/SharedCacheClientImpl.java | 8 +------- .../yarn/client/util/YarnClientUtils.java | 2 +- 4 files changed, 7 insertions(+), 27 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/ContainerShellWebSocket.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/ContainerShellWebSocket.java index efcc2ea0aed..66a901fc36a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/ContainerShellWebSocket.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/ContainerShellWebSocket.java @@ -20,7 +20,7 @@ package org.apache.hadoop.yarn.client.api; import java.io.IOException; import java.io.OutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -62,7 +62,7 @@ public class ContainerShellWebSocket { session.getRemote().flush(); sttySet = true; } - terminal.output().write(message.getBytes(Charset.forName("UTF-8"))); + terminal.output().write(message.getBytes(StandardCharsets.UTF_8)); terminal.output().flush(); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/async/impl/NMClientAsyncImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/async/impl/NMClientAsyncImpl.java index 3215aa299a4..eb5b9b227fb 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/async/impl/NMClientAsyncImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/async/impl/NMClientAsyncImpl.java @@ -18,7 +18,6 @@ package org.apache.hadoop.yarn.client.api.async.impl; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.EnumSet; import java.util.HashSet; @@ -51,7 +50,6 @@ import org.apache.hadoop.yarn.client.api.impl.NMClientImpl; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.event.AbstractEvent; import org.apache.hadoop.yarn.event.EventHandler; -import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.ipc.RPCUtil; import org.apache.hadoop.yarn.state.InvalidStateTransitionException; import org.apache.hadoop.yarn.state.MultipleArcTransition; @@ -636,12 +634,8 @@ public class NMClientAsyncImpl extends NMClientAsync { + "Container " + containerId, thr); } return ContainerState.RUNNING; - } catch (YarnException e) { + } catch (Throwable e) { return onExceptionRaised(container, event, e); - } catch (IOException e) { - return onExceptionRaised(container, event, e); - } catch (Throwable t) { - return onExceptionRaised(container, event, t); } } @@ -854,12 +848,8 @@ public class NMClientAsyncImpl extends NMClientAsync { + "Container " + event.getContainerId(), thr); } return ContainerState.DONE; - } catch (YarnException e) { + } catch (Throwable e) { return onExceptionRaised(container, event, e); - } catch (IOException e) { - return onExceptionRaised(container, event, e); - } catch (Throwable t) { - return onExceptionRaised(container, event, t); } } @@ -966,12 +956,8 @@ public class NMClientAsyncImpl extends NMClientAsync { "Unchecked exception is thrown from onContainerStatusReceived" + " for Container " + event.getContainerId(), thr); } - } catch (YarnException e) { + } catch (Throwable e) { onExceptionRaised(containerId, e); - } catch (IOException e) { - onExceptionRaised(containerId, e); - } catch (Throwable t) { - onExceptionRaised(containerId, t); } } else { StatefulContainer container = containers.get(containerId); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/SharedCacheClientImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/SharedCacheClientImpl.java index cce6ae8df61..103080a8502 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/SharedCacheClientImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/SharedCacheClientImpl.java @@ -158,14 +158,8 @@ public class SharedCacheClientImpl extends SharedCacheClient { public String getFileChecksum(Path sourceFile) throws IOException { FileSystem fs = sourceFile.getFileSystem(this.conf); - FSDataInputStream in = null; - try { - in = fs.open(sourceFile); + try (FSDataInputStream in = fs.open(sourceFile)) { return this.checksum.computeChecksum(in); - } finally { - if (in != null) { - in.close(); - } } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/YarnClientUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/YarnClientUtils.java index 041152d7df8..049dbd7962c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/YarnClientUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/YarnClientUtils.java @@ -145,7 +145,7 @@ public abstract class YarnClientUtils { // Now we only support one property, which is exclusive, so check if // key = exclusive and value = {true/false} - if (key.equals("exclusive") + if ("exclusive".equals(key) && ImmutableSet.of("true", "false").contains(value)) { exclusive = Boolean.parseBoolean(value); } else {