Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix seatunnel-issue-6386 and windows specific starter cmd script bug #6735

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.core.starter.utils;

import org.apache.commons.lang3.SystemUtils;

public class SystemUtil {

public static String GetOsType() {
String os_type = "";

if (SystemUtils.IS_OS_WINDOWS) {
os_type = "Windows";
} else if (SystemUtils.IS_OS_MAC) {
os_type = "Mac";
} else if (SystemUtils.IS_OS_LINUX) {
os_type = "Linux";
} else if (SystemUtils.IS_OS_SOLARIS) {
os_type = "Solaris";
} else {
os_type = "Unknown";
}

return os_type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ cd /d "%PRG_DIR%" || (
exit /b 1
)

set "APP_DIR=%~dp0"
set "currentDir=%~dp0"
set "currentDir=%currentDir:~0,-1%"
for %%i in ("%currentDir%") do set "APP_DIR=%%~dpi"
set "APP_DIR=%APP_DIR:~0,-1%"
rem set "APP_DIR=%~dp0"
set "CONF_DIR=%APP_DIR%\config"
set "APP_JAR=%APP_DIR%\starter\seatunnel-flink-13-starter.jar"
set "APP_MAIN=org.apache.seatunnel.core.starter.flink.FlinkStarter"
Expand All @@ -51,20 +55,21 @@ if exist "%CONF_DIR%\log4j2.properties" (
)

set "CLASS_PATH=%APP_DIR%\starter\logging\*;%APP_JAR%"
set "full_java_cmd=java %JAVA_OPTS% -cp %CLASS_PATH% %APP_MAIN% %args%"

for /f "delims=" %%i in ('java %JAVA_OPTS% -cp %CLASS_PATH% %APP_MAIN% %args%') do (
set "CMD=%%i"
setlocal disabledelayedexpansion
for /f "delims=" %%i in ('echo !full_java_cmd!') do (
rem set "CMD=%%i"
rem setlocal disabledelayedexpansion
if !errorlevel! equ 234 (
echo !CMD!
echo %full_java_cmd%
endlocal
exit /b 0
) else if !errorlevel! equ 0 (
echo Execute SeaTunnel Flink Job: !CMD!
echo Execute SeaTunnel Flink Job: %full_java_cmd%
endlocal
call !CMD!
call %full_java_cmd%
) else (
echo !CMD!
echo %full_java_cmd%
endlocal
exit /b !errorlevel!
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ cd /d "%PRG_DIR%" || (
exit /b 1
)

set "APP_DIR=%~dp0"
set "currentDir=%~dp0"
set "currentDir=%currentDir:~0,-1%"
for %%i in ("%currentDir%") do set "APP_DIR=%%~dpi"
set "APP_DIR=%APP_DIR:~0,-1%"
rem set "APP_DIR=%~dp0"
set "CONF_DIR=%APP_DIR%\config"
set "APP_JAR=%APP_DIR%\starter\seatunnel-flink-15-starter.jar"
set "APP_MAIN=org.apache.seatunnel.core.starter.flink.FlinkStarter"
Expand All @@ -51,20 +55,21 @@ if exist "%CONF_DIR%\log4j2.properties" (
)

set "CLASS_PATH=%APP_DIR%\starter\logging\*;%APP_JAR%"
set "full_java_cmd=java %JAVA_OPTS% -cp %CLASS_PATH% %APP_MAIN% %args%"

for /f "delims=" %%i in ('java %JAVA_OPTS% -cp %CLASS_PATH% %APP_MAIN% %args%') do (
set "CMD=%%i"
setlocal disabledelayedexpansion
for /f "delims=" %%i in ('echo !full_java_cmd!') do (
rem set "CMD=%%i"
rem setlocal disabledelayedexpansion
if !errorlevel! equ 234 (
echo !CMD!
echo %full_java_cmd%
endlocal
exit /b 0
) else if !errorlevel! equ 0 (
echo Execute SeaTunnel Flink Job: !CMD!
echo Execute SeaTunnel Flink Job: %full_java_cmd%
endlocal
call !CMD!
call %full_java_cmd%
) else (
echo !CMD!
echo %full_java_cmd%
endlocal
exit /b !errorlevel!
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.seatunnel.core.starter.enums.EngineType;
import org.apache.seatunnel.core.starter.flink.args.FlinkCommandArgs;
import org.apache.seatunnel.core.starter.utils.CommandLineUtils;
import org.apache.seatunnel.core.starter.utils.SystemUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -44,6 +45,7 @@ public class FlinkStarter implements Starter {
this.appJar = Common.appStarterDir().resolve(APP_JAR_NAME).toString();
}

@SuppressWarnings("checkstyle:RegexpSingleline")
public static void main(String[] args) {
FlinkStarter flinkStarter = new FlinkStarter(args);
System.out.println(String.join(" ", flinkStarter.buildCommands()));
Expand All @@ -52,8 +54,46 @@ public static void main(String[] args) {
@Override
public List<String> buildCommands() {
List<String> command = new ArrayList<>();
String local_os_type = "";

SystemUtil my_system_util = new SystemUtil();
local_os_type = my_system_util.GetOsType();
// debug
// System.out.println("OS type:"+local_os_type);
Comment on lines +61 to +62
Copy link
Member

Choose a reason for hiding this comment

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

@GangLiCN Please delete these useless codes


String cmd_flink = "";

/* Nothe that "flink.cmd” or "flink.bat" can be retrieved from
lower version of flink (e.g.1.0.9),We do not check if this
file exists on the box, user needs to make sure this file
exists or not.
*/
switch (local_os_type.toLowerCase()) {
case "windows":
cmd_flink = "%FLINK_HOME%/bin/flink.bat";
break;
case "linux":
cmd_flink = "${FLINK_HOME}/bin/flink";
break;
case "solaris":
cmd_flink = "${FLINK_HOME}/bin/flink";
break;
case "mac":
cmd_flink = "${FLINK_HOME}/bin/flink";
break;
case "unknown":
cmd_flink = "error";
break;
}

// set start command
command.add("${FLINK_HOME}/bin/flink");
if (!(cmd_flink.equals("error"))) {
command.add(cmd_flink);
} else {
System.out.println("Error: Can not determine OS type, abort run !");
System.exit(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

System.exit(-1) is not recommended. The code causes some strange security problems; Throw an exception if there is a problem

}

// set deploy mode, run or run-application
command.add(flinkCommandArgs.getDeployMode().getDeployMode());
// set submitted target master
Expand Down Expand Up @@ -91,6 +131,8 @@ public List<String> buildCommands() {
.filter(Objects::nonNull)
.map(String::trim)
.forEach(variable -> command.add("-D" + variable));
// debug
// System.out.println("Whole command string:" + command.toString());
Comment on lines +134 to +135
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

return command;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.core.starter.flink;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class TestFlinkJobCommandReturnV13 {
@Test
public void testSparkJobCommandReturnV13() throws Exception {
String flink_job_command = null;
String flink_job_command_return = null;
String flink_job_command_return_error = null;
String flink_home_dir = null;
String flink_submit_job_cmd = null;
String seatunnel_home_dir = null;
String seatunnel_submit_flink_job_jar_list = null;
String seatunnel_submit_flink_job_cmd = null;
String seatunnel_submit_flink_job_cmd_paras = null;
String seatunnel_submit_flink_job_cmd_full_path = null;

StringBuilder sb_cmd_final = new StringBuilder();
String seatunnel_submit_flink_job_cmd_final = null;

final String separator = "/";
Properties prop = new Properties();

try {
InputStream in =
this.getClass()
.getClassLoader()
.getResourceAsStream("flink13-job-command.properties");
if (in == null) {
throw new FileNotFoundException(
"Resource file not found. Make sure the file exists in src/test/resources.");
}
prop.load(in);

flink_home_dir = prop.getProperty("FLINK13_HOME");
flink_submit_job_cmd = prop.getProperty("FLINK13_SUBMIT_JOB_CMD");
seatunnel_home_dir = prop.getProperty("SEATUNNEL_HOME");
seatunnel_submit_flink_job_jar_list =
prop.getProperty("SEATUNNEL_SUBMIT_FLINK13_JOB_JAR_LIST");
seatunnel_submit_flink_job_cmd = prop.getProperty("SEATUNNEL_SUBMIT_FLINK13_JOB_CMD");

sb_cmd_final.append(seatunnel_home_dir);
sb_cmd_final.append(separator);
sb_cmd_final.append("bin");
sb_cmd_final.append(separator);
sb_cmd_final.append(seatunnel_submit_flink_job_cmd);
sb_cmd_final.append(" --config ");
sb_cmd_final.append(seatunnel_home_dir);
sb_cmd_final.append(separator);
sb_cmd_final.append("/config/v2.streaming.conf.template");

seatunnel_submit_flink_job_cmd_final = sb_cmd_final.toString();
// debug
// System.out.println("Final command:" + seatunnel_submit_flink_job_cmd_final);

Process process = Runtime.getRuntime().exec(seatunnel_submit_flink_job_cmd_final);
process.waitFor(); // wait for the command to finish

// Read the stdout of command
flink_job_command_return = readStream(process.getInputStream());

// Read the stderror of command
flink_job_command_return_error = readStream(process.getErrorStream());
Assertions.assertNotNull(flink_job_command_return);
Assertions.assertNull(flink_job_command_return_error);

/* if (flink_job_command_return_error == null) {
System.out.println("Seatunnel flink job submutted successfully.");
} else {
System.out.println("Seatunnel flink job submutted failed, error messages is:");
System.out.println(flink_job_command_return_error);
}
*/

process.destroy();
// debug
System.out.println(
"Auto closed [start-seatunnel-flink-13-connector-v2.cmd] successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}

private static String readStream(InputStream inputStream) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
StringBuilder sb = new StringBuilder();
String str_final = "";

while ((line = reader.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
str_final = sb.toString();
if (!(str_final.isEmpty())) {
return str_final;
} else {
return null;
}
}
}
}