From common-commits-return-82203-archive-asf-public=cust-asf.ponee.io@hadoop.apache.org Wed May 2 21:21:43 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 2B33818067C for ; Wed, 2 May 2018 21:21:43 +0200 (CEST) Received: (qmail 3371 invoked by uid 500); 2 May 2018 19:21:37 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 2868 invoked by uid 99); 2 May 2018 19:21:37 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 May 2018 19:21:37 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D0427F687F; Wed, 2 May 2018 19:21:36 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: shv@apache.org To: common-commits@hadoop.apache.org Date: Wed, 02 May 2018 19:21:53 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [18/50] [abbrv] hadoop git commit: HADOOP-15394. Backport PowerShell NodeFencer HADOOP-14309 to branch-2. Contributed by Inigo Goiri. HADOOP-15394. Backport PowerShell NodeFencer HADOOP-14309 to branch-2. Contributed by Inigo Goiri. Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/7a2edb58 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/7a2edb58 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/7a2edb58 Branch: refs/heads/YARN-8200 Commit: 7a2edb588c38a221a3cfaf73d9571691b06c2f37 Parents: 5ec195e Author: Inigo Goiri Authored: Tue Apr 17 11:33:09 2018 -0700 Committer: Inigo Goiri Committed: Tue Apr 17 11:33:09 2018 -0700 ---------------------------------------------------------------------- .../java/org/apache/hadoop/ha/NodeFencer.java | 3 +- .../org/apache/hadoop/ha/PowerShellFencer.java | 155 +++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/7a2edb58/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/NodeFencer.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/NodeFencer.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/NodeFencer.java index 63f6db6..2247a34 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/NodeFencer.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/NodeFencer.java @@ -69,7 +69,8 @@ public class NodeFencer { private static final Map> STANDARD_METHODS = ImmutableMap.>of( "shell", ShellCommandFencer.class, - "sshfence", SshFenceByTcpPort.class); + "sshfence", SshFenceByTcpPort.class, + "powershell", PowerShellFencer.class); private final List methods; http://git-wip-us.apache.org/repos/asf/hadoop/blob/7a2edb58/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/PowerShellFencer.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/PowerShellFencer.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/PowerShellFencer.java new file mode 100644 index 0000000..6de618c --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/PowerShellFencer.java @@ -0,0 +1,155 @@ +/** + * 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.ha; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; + +import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Fencer method that uses PowerShell to remotely connect to a machine and kill + * the required process. This only works in Windows. + * + * The argument passed to this fencer should be a unique string in the + * "CommandLine" attribute for the "java.exe" process. For example, the full + * path for the Namenode: "org.apache.hadoop.hdfs.server.namenode.NameNode". + * The administrator can also shorten the name to "Namenode" if it's unique. + */ +public class PowerShellFencer extends Configured implements FenceMethod { + + private static final Logger LOG = LoggerFactory.getLogger(PowerShellFencer + .class); + + + @Override + public void checkArgs(String argStr) throws BadFencingConfigurationException { + LOG.info("The parameter for the PowerShell fencer is " + argStr); + } + + @Override + public boolean tryFence(HAServiceTarget target, String argsStr) + throws BadFencingConfigurationException { + + String processName = argsStr; + InetSocketAddress serviceAddr = target.getAddress(); + String hostname = serviceAddr.getHostName(); + + // Use PowerShell to kill a remote process + String ps1script = buildPSScript(processName, hostname); + if (ps1script == null) { + LOG.error("Cannot build PowerShell script"); + return false; + } + + // Execute PowerShell script + LOG.info("Executing " + ps1script); + ProcessBuilder builder = new ProcessBuilder("powershell.exe", ps1script); + Process p = null; + try { + p = builder.start(); + p.getOutputStream().close(); + } catch (IOException e) { + LOG.warn("Unable to execute " + ps1script, e); + return false; + } + + // Pump logs to stderr + StreamPumper errPumper = new StreamPumper( + LOG, "fencer", p.getErrorStream(), StreamPumper.StreamType.STDERR); + errPumper.start(); + + StreamPumper outPumper = new StreamPumper( + LOG, "fencer", p.getInputStream(), StreamPumper.StreamType.STDOUT); + outPumper.start(); + + // Waiting for the process to finish + int rc = 0; + try { + rc = p.waitFor(); + errPumper.join(); + outPumper.join(); + } catch (InterruptedException ie) { + LOG.warn("Interrupted while waiting for fencing command: " + ps1script); + return false; + } + + return rc == 0; + } + + /** + * Build a PowerShell script to kill a java.exe process in a remote machine. + * + * @param processName Name of the process to kill. This is an attribute in + * CommandLine. + * @param host Host where the process is. + * @return Path of the PowerShell script. + */ + private String buildPSScript(final String processName, final String host) { + LOG.info( + "Building PowerShell script to kill " + processName + " at " + host); + String ps1script = null; + BufferedWriter writer = null; + try { + File file = File.createTempFile("temp-fence-command", ".ps1"); + file.deleteOnExit(); + FileOutputStream fos = new FileOutputStream(file, false); + OutputStreamWriter osw = + new OutputStreamWriter(fos, StandardCharsets.UTF_8); + writer = new BufferedWriter(osw); + + // Filter to identify the Namenode process + String filter = StringUtils.join(" and ", new String[] { + "Name LIKE '%java.exe%'", + "CommandLine LIKE '%" + processName+ "%'"}); + + // Identify the process + String cmd = "Get-WmiObject Win32_Process"; + cmd += " -Filter \"" + filter + "\""; + // Remote location + cmd += " -Computer " + host; + // Kill it + cmd += " |% { $_.Terminate() }"; + + LOG.info("PowerShell command: " + cmd); + writer.write(cmd); + writer.flush(); + + ps1script = file.getAbsolutePath(); + } catch (IOException ioe) { + LOG.error("Cannot create PowerShell script", ioe); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException ioe) { + LOG.error("Cannot close PowerShell script", ioe); + } + } + } + return ps1script; + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org