Skip to content

Commit 00b5a27

Browse files
pingsutwjojochuang
authored andcommitted
SUBMARINE-72. Kill and destroy the job through the submarine client (#1090) Contributed by kevin su.
1 parent 22d7d1f commit 00b5a27

File tree

7 files changed

+217
-17
lines changed

7 files changed

+217
-17
lines changed

hadoop-submarine/hadoop-submarine-core/src/main/java/org/apache/hadoop/yarn/submarine/client/cli/Cli.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
package org.apache.hadoop.yarn.submarine.client.cli;
1616

17+
import java.util.Arrays;
18+
1719
import org.apache.hadoop.conf.Configuration;
20+
import org.apache.hadoop.yarn.conf.YarnConfiguration;
1821
import org.apache.hadoop.yarn.submarine.client.cli.runjob.RunJobCli;
1922
import org.apache.hadoop.yarn.submarine.common.ClientContext;
20-
import org.apache.hadoop.yarn.conf.YarnConfiguration;
2123
import org.apache.hadoop.yarn.submarine.runtimes.RuntimeFactory;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
2426

25-
import java.util.Arrays;
26-
2727
public class Cli {
2828
private static final Logger LOG =
2929
LoggerFactory.getLogger(Cli.class);
@@ -35,7 +35,7 @@ private static void printHelp() {
3535
helpMsg.append(" job \n");
3636
helpMsg.append(" run : run a job, please see 'job run --help' for usage \n");
3737
helpMsg.append(" show : get status of job, please see 'job show --help' for usage \n");
38-
38+
helpMsg.append(" kill : kill a job, please see 'job kill --help' for usage \n");
3939
System.out.println(helpMsg.toString());
4040
}
4141

@@ -92,6 +92,8 @@ public static void main(String[] args) throws Exception {
9292
new RunJobCli(clientContext).run(moduleArgs);
9393
} else if (subCmd.equals(CliConstants.SHOW)) {
9494
new ShowJobCli(clientContext).run(moduleArgs);
95+
} else if (subCmd.equals(CliConstants.KILL)) {
96+
new KillJobCli(clientContext).run(moduleArgs);
9597
} else {
9698
printHelp();
9799
throw new IllegalArgumentException("Unknown option for job");

hadoop-submarine/hadoop-submarine-core/src/main/java/org/apache/hadoop/yarn/submarine/client/cli/CliConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* NOTE: use lowercase + "_" for the option name
1919
*/
2020
public class CliConstants {
21+
public static final String KILL = "kill";
2122
public static final String RUN = "run";
2223
public static final String SERVE = "serve";
2324
public static final String LIST = "list";

hadoop-submarine/hadoop-submarine-core/src/main/java/org/apache/hadoop/yarn/submarine/client/cli/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
* Represents a Submarine command.
2121
*/
2222
public enum Command {
23-
RUN_JOB, SHOW_JOB
23+
RUN_JOB, SHOW_JOB, KILL_JOB
2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License. See accompanying LICENSE file.
13+
*/
14+
15+
package org.apache.hadoop.yarn.submarine.client.cli;
16+
17+
import static org.apache.hadoop.yarn.client.api.AppAdminClient.DEFAULT_TYPE;
18+
19+
import java.io.IOException;
20+
21+
import org.apache.commons.cli.CommandLine;
22+
import org.apache.commons.cli.GnuParser;
23+
import org.apache.commons.cli.HelpFormatter;
24+
import org.apache.commons.cli.Options;
25+
import org.apache.commons.cli.ParseException;
26+
import org.apache.hadoop.yarn.client.api.AppAdminClient;
27+
import org.apache.hadoop.yarn.exceptions.YarnException;
28+
import org.apache.hadoop.yarn.submarine.client.cli.param.KillJobParameters;
29+
import org.apache.hadoop.yarn.submarine.client.cli.param.ParametersHolder;
30+
import org.apache.hadoop.yarn.submarine.common.ClientContext;
31+
import org.apache.hadoop.yarn.submarine.common.exception.SubmarineException;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
34+
35+
import com.google.common.annotations.VisibleForTesting;
36+
37+
public class KillJobCli extends AbstractCli {
38+
private static final Logger LOG = LoggerFactory.getLogger(ShowJobCli.class);
39+
40+
private Options options;
41+
private ParametersHolder parametersHolder;
42+
43+
public KillJobCli(ClientContext cliContext) {
44+
super(cliContext);
45+
options = generateOptions();
46+
}
47+
48+
public void printUsages() {
49+
new HelpFormatter().printHelp("job kill", options);
50+
}
51+
52+
private Options generateOptions() {
53+
Options options = new Options();
54+
options.addOption(CliConstants.NAME, true, "Name of the job");
55+
options.addOption("h", "help", false, "Print help");
56+
return options;
57+
}
58+
59+
private void parseCommandLineAndGetKillJobParameters(String[] args)
60+
throws IOException, YarnException {
61+
// Do parsing
62+
GnuParser parser = new GnuParser();
63+
CommandLine cli;
64+
try {
65+
cli = parser.parse(options, args);
66+
parametersHolder =
67+
ParametersHolder.createWithCmdLine(cli, Command.KILL_JOB);
68+
parametersHolder.updateParameters(clientContext);
69+
} catch (ParseException e) {
70+
LOG.error(("Error parsing command-line options: " + e.getMessage()));
71+
printUsages();
72+
}
73+
}
74+
75+
@VisibleForTesting
76+
protected boolean killJob() throws IOException, YarnException {
77+
String jobName = getParameters().getName();
78+
AppAdminClient appAdminClient = AppAdminClient
79+
.createAppAdminClient(DEFAULT_TYPE, clientContext.getYarnConfig());
80+
81+
if (appAdminClient.actionStop(jobName) != 0) {
82+
LOG.error("appAdminClient fail to stop application");
83+
return false;
84+
}
85+
if (appAdminClient.actionDestroy(jobName) != 0) {
86+
LOG.error("appAdminClient fail to destroy application");
87+
return false;
88+
}
89+
90+
appAdminClient.stop();
91+
return true;
92+
}
93+
94+
@VisibleForTesting
95+
public KillJobParameters getParameters() {
96+
return (KillJobParameters) parametersHolder.getParameters();
97+
}
98+
99+
@Override
100+
public int run(String[] args) throws ParseException, IOException,
101+
YarnException, InterruptedException, SubmarineException {
102+
if (CliUtils.argsForHelp(args)) {
103+
printUsages();
104+
return 0;
105+
}
106+
parseCommandLineAndGetKillJobParameters(args);
107+
if (killJob() == true) {
108+
LOG.info("Kill job successfully !");
109+
}
110+
return 0;
111+
}
112+
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License. See accompanying LICENSE file.
13+
*/
14+
15+
package org.apache.hadoop.yarn.submarine.client.cli.param;
16+
17+
public class KillJobParameters extends BaseParameters {
18+
19+
}

hadoop-submarine/hadoop-submarine-core/src/main/java/org/apache/hadoop/yarn/submarine/client/cli/param/ParametersHolder.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616

1717
package org.apache.hadoop.yarn.submarine.client.cli.param;
1818

19-
import com.google.common.collect.ImmutableSet;
20-
import com.google.common.collect.Lists;
21-
import com.google.common.collect.Maps;
19+
import static org.apache.hadoop.yarn.submarine.client.cli.runjob.RunJobCli.YAML_PARSE_FAILED;
20+
21+
import java.io.IOException;
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Set;
27+
import java.util.stream.Collectors;
28+
2229
import org.apache.commons.cli.CommandLine;
2330
import org.apache.commons.cli.ParseException;
2431
import org.apache.hadoop.yarn.exceptions.YarnException;
@@ -39,15 +46,9 @@
3946
import org.slf4j.Logger;
4047
import org.slf4j.LoggerFactory;
4148

42-
import java.io.IOException;
43-
import java.util.Arrays;
44-
import java.util.Collections;
45-
import java.util.List;
46-
import java.util.Map;
47-
import java.util.Set;
48-
import java.util.stream.Collectors;
49-
50-
import static org.apache.hadoop.yarn.submarine.client.cli.runjob.RunJobCli.YAML_PARSE_FAILED;
49+
import com.google.common.collect.ImmutableSet;
50+
import com.google.common.collect.Lists;
51+
import com.google.common.collect.Maps;
5152

5253
/**
5354
* This class acts as a wrapper of {@code CommandLine} values along with
@@ -104,6 +105,8 @@ private BaseParameters createParameters() {
104105
}
105106
} else if (command == Command.SHOW_JOB) {
106107
return new ShowJobParameters();
108+
} else if (command == Command.KILL_JOB) {
109+
return new KillJobParameters();
107110
} else {
108111
throw new UnsupportedOperationException(SUPPORTED_COMMANDS_MESSAGE);
109112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.yarn.submarine.client.cli;
20+
21+
import java.io.IOException;
22+
23+
import org.apache.commons.cli.ParseException;
24+
import org.apache.hadoop.yarn.exceptions.YarnException;
25+
import org.apache.hadoop.yarn.submarine.client.cli.param.KillJobParameters;
26+
import org.apache.hadoop.yarn.submarine.common.MockClientContext;
27+
import org.apache.hadoop.yarn.submarine.common.conf.SubmarineLogs;
28+
import org.apache.hadoop.yarn.submarine.common.exception.SubmarineException;
29+
import org.junit.Assert;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
33+
public class TestKillJobCliParsing {
34+
@Before
35+
public void before() {
36+
SubmarineLogs.verboseOff();
37+
}
38+
39+
@Test
40+
public void testPrintHelp() {
41+
MockClientContext mockClientContext = new MockClientContext();
42+
KillJobCli killJobCli = new KillJobCli(mockClientContext);
43+
killJobCli.printUsages();
44+
}
45+
46+
@Test
47+
public void testKillJob()
48+
throws InterruptedException, SubmarineException, YarnException,
49+
ParseException, IOException {
50+
MockClientContext mockClientContext = new MockClientContext();
51+
KillJobCli killJobCli = new KillJobCli(mockClientContext) {
52+
@Override
53+
protected boolean killJob() {
54+
// do nothing
55+
return false;
56+
}
57+
};
58+
killJobCli.run(new String[] { "--name", "my-job" });
59+
KillJobParameters parameters = killJobCli.getParameters();
60+
Assert.assertEquals(parameters.getName(), "my-job");
61+
}
62+
}

0 commit comments

Comments
 (0)