This is an automated email from the ASF dual-hosted git repository.

kirs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 9491566  [Improvement][core] Add DeployMode enum (#1404)
9491566 is described below

commit 94915666540fb94c164816ad20787a978f2a9842
Author: Wenjun Ruan <[email protected]>
AuthorDate: Sun Mar 6 22:49:16 2022 +0800

    [Improvement][core] Add DeployMode enum (#1404)
---
 .../org/apache/seatunnel/common/config/Common.java |  8 +++--
 .../apache/seatunnel/common/config/DeployMode.java | 35 ++++++++++++++++++++++
 .../main/java/org/apache/seatunnel/Seatunnel.java  | 14 +++++----
 .../seatunnel/config/command/CommandLineArgs.java  |  4 ++-
 .../seatunnel/config/command/CommandSparkArgs.java |  4 ++-
 .../config/command/CommandSparkArgsTest.java       |  4 ++-
 6 files changed, 58 insertions(+), 11 deletions(-)

diff --git 
a/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/Common.java 
b/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/Common.java
index 023ed27..b8cc45d 100644
--- 
a/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/Common.java
+++ 
b/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/Common.java
@@ -23,10 +23,12 @@ import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 public class Common {
 
-    private static final List<String> ALLOWED_MODES = Arrays.asList("client", 
"cluster");
+    private static final List<String> ALLOWED_MODES = 
Arrays.stream(DeployMode.values())
+        .map(DeployMode::getName).collect(Collectors.toList());
 
     private static Optional<String> MODE = Optional.empty();
 
@@ -58,14 +60,14 @@ public class Common {
      * When running seatunnel in --master yarn or --master mesos, you can put 
plugins related files in plugins dir.
      */
     public static Path appRootDir() {
-        if (MODE.equals(Optional.of("client"))) {
+        if (MODE.equals(Optional.of(DeployMode.CLIENT.getName()))) {
             try {
                 String path = 
Common.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
                 return Paths.get(path).getParent().getParent().getParent();
             } catch (URISyntaxException e) {
                 throw new RuntimeException(e);
             }
-        } else if (MODE.equals(Optional.of("cluster"))) {
+        } else if (MODE.equals(Optional.of(DeployMode.CLUSTER.getName()))) {
             return Paths.get("");
         } else {
             throw new IllegalStateException("MODE not support : " + 
MODE.orElse("null"));
diff --git 
a/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/DeployMode.java
 
b/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/DeployMode.java
new file mode 100644
index 0000000..44beef8
--- /dev/null
+++ 
b/seatunnel-common/src/main/java/org/apache/seatunnel/common/config/DeployMode.java
@@ -0,0 +1,35 @@
+/*
+ * 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.seatunnel.common.config;
+
+public enum DeployMode {
+    CLIENT("client"),
+    CLUSTER("cluster"),
+    ;
+
+    private final String name;
+
+    DeployMode(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+}
diff --git 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/Seatunnel.java
 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/Seatunnel.java
index a566653..c11905b 100644
--- 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/Seatunnel.java
+++ 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/Seatunnel.java
@@ -24,6 +24,7 @@ import org.apache.seatunnel.common.Constants;
 import org.apache.seatunnel.common.config.CheckResult;
 import org.apache.seatunnel.common.config.Common;
 import org.apache.seatunnel.common.config.ConfigRuntimeException;
+import org.apache.seatunnel.common.config.DeployMode;
 import org.apache.seatunnel.config.ConfigBuilder;
 import org.apache.seatunnel.config.command.CommandLineArgs;
 import org.apache.seatunnel.env.Execution;
@@ -50,10 +51,13 @@ public class Seatunnel {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(Seatunnel.class);
 
     public static void run(CommandLineArgs commandLineArgs, Engine engine) 
throws Exception {
-        Common.setDeployMode(commandLineArgs.getDeployMode());
+        if (!Common.setDeployMode(commandLineArgs.getDeployMode())) {
+            throw new IllegalArgumentException(
+                String.format("Deploy mode: %s is Illegal", 
commandLineArgs.getDeployMode()));
+        }
+
         String configFilePath = getConfigFilePath(commandLineArgs, engine);
-        boolean testConfig = commandLineArgs.isTestConfig();
-        if (testConfig) {
+        if (commandLineArgs.isTestConfig()) {
             new ConfigBuilder(configFilePath, engine).checkConfig();
             LOGGER.info("config OK !");
         } else {
@@ -77,7 +81,7 @@ public class Seatunnel {
                 break;
             case SPARK:
                 final Optional<String> mode = Common.getDeployMode();
-                if (mode.isPresent() && "cluster".equals(mode.get())) {
+                if (mode.isPresent() && 
DeployMode.CLUSTER.getName().equals(mode.get())) {
                     path = 
Paths.get(cmdArgs.getConfigFile()).getFileName().toString();
                 } else {
                     path = cmdArgs.getConfigFile();
@@ -124,7 +128,7 @@ public class Seatunnel {
 
     private static void deployModeCheck() {
         final Optional<String> mode = Common.getDeployMode();
-        if (mode.isPresent() && "cluster".equals(mode.get())) {
+        if (mode.isPresent() && 
DeployMode.CLUSTER.getName().equals(mode.get())) {
 
             LOGGER.info("preparing cluster mode work dir files...");
             File workDir = new File(".");
diff --git 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
index 5e9513b..1fe0ced 100644
--- 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
+++ 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
@@ -17,11 +17,13 @@
 
 package org.apache.seatunnel.config.command;
 
+import org.apache.seatunnel.common.config.DeployMode;
+
 import java.util.List;
 
 public class CommandLineArgs {
 
-    private String deployMode = "client";
+    private String deployMode = DeployMode.CLIENT.getName();
     private final String configFile;
     private final boolean testConfig;
     private List<String> variables;
diff --git 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandSparkArgs.java
 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandSparkArgs.java
index 35d7c16..c847590 100644
--- 
a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandSparkArgs.java
+++ 
b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandSparkArgs.java
@@ -17,6 +17,8 @@
 
 package org.apache.seatunnel.config.command;
 
+import org.apache.seatunnel.common.config.DeployMode;
+
 import com.beust.jcommander.Parameter;
 
 import java.util.List;
@@ -32,7 +34,7 @@ public class CommandSparkArgs {
         description = "spark deploy mode",
         required = true,
         validateWith = 
org.apache.seatunnel.config.command.DeployModeValidator.class)
-    private String deployMode = "client";
+    private String deployMode = DeployMode.CLIENT.getName();
 
     @Parameter(names = {"-m", "--master"},
         description = "spark master",
diff --git 
a/seatunnel-core/seatunnel-core-base/src/test/java/org/apache/seatunnel/config/command/CommandSparkArgsTest.java
 
b/seatunnel-core/seatunnel-core-base/src/test/java/org/apache/seatunnel/config/command/CommandSparkArgsTest.java
index 0548f2e..f9af86f 100644
--- 
a/seatunnel-core/seatunnel-core-base/src/test/java/org/apache/seatunnel/config/command/CommandSparkArgsTest.java
+++ 
b/seatunnel-core/seatunnel-core-base/src/test/java/org/apache/seatunnel/config/command/CommandSparkArgsTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.seatunnel.config.command;
 
+import org.apache.seatunnel.common.config.DeployMode;
+
 import com.beust.jcommander.JCommander;
 import org.junit.Assert;
 import org.junit.Test;
@@ -34,7 +36,7 @@ public class CommandSparkArgsTest {
             .build()
             .parse(args);
         Assert.assertEquals("app.conf", sparkArgs.getConfigFile());
-        Assert.assertEquals("client", sparkArgs.getDeployMode());
+        Assert.assertEquals(DeployMode.CLIENT.getName(), 
sparkArgs.getDeployMode());
         Assert.assertEquals("yarn", sparkArgs.getMaster());
         Assert.assertEquals(Arrays.asList("city=shijiazhuang", "name=Tom"), 
sparkArgs.getVariables());
     }

Reply via email to