Unverified Commit 71f6bb92 authored by gaoyan's avatar gaoyan Committed by GitHub

[Fix]Fix yarn application issue (#3025)

parent ad2b82f5
......@@ -48,10 +48,12 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.core.execution.JobClient;
import org.apache.flink.python.PythonOptions;
import org.apache.flink.runtime.jobgraph.SavepointConfigOptions;
import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;
import org.apache.flink.streaming.api.graph.StreamGraph;
import org.apache.flink.table.api.TableResult;
import java.io.File;
import java.io.FileOutputStream;
......@@ -70,6 +72,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
......@@ -129,15 +132,21 @@ public class Submitter {
String[] statements =
SqlUtil.getStatements(sql, SystemConfiguration.getInstances().getSqlSeparator());
Optional<JobClient> jobClient = Optional.empty();
try {
if (Dialect.FLINK_JAR == appTask.getDialect()) {
executeJarJob(appTask.getType(), executor, statements);
jobClient = executeJarJob(appTask.getType(), executor, statements);
} else {
executeJob(executor, statements);
jobClient = executeJob(executor, statements);
}
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(Submitter.executor, config.getTaskId());
if (jobClient.isPresent()) {
FlinkAppUtil.monitorFlinkTask(jobClient.get(), config.getTaskId());
} else {
log.error("jobClient is empty, can not monitor job");
// FlinkAppUtil.monitorFlinkTask(Submitter.executor, config.getTaskId());
}
}
}
......@@ -251,7 +260,9 @@ public class Submitter {
}
@SneakyThrows
public static void executeJarJob(String type, Executor executor, String[] statements) {
public static Optional<JobClient> executeJarJob(String type, Executor executor, String[] statements) {
Optional<JobClient> jobClient = Optional.empty();
for (int i = 0; i < statements.length; i++) {
String sqlStatement = executor.pretreatStatement(statements[i]);
if (ExecuteJarParseStrategy.INSTANCE.match(sqlStatement)) {
......@@ -269,7 +280,8 @@ public class Submitter {
streamGraph.setSavepointRestoreSettings(SavepointRestoreSettings.forPath(
savePointPath, configuration.get(SavepointConfigOptions.SAVEPOINT_IGNORE_UNCLAIMED_STATE)));
}
executor.getStreamExecutionEnvironment().executeAsync(streamGraph);
JobClient client = executor.getStreamExecutionEnvironment().executeAsync(streamGraph);
jobClient = Optional.of(client);
break;
}
if (Operations.getOperationType(sqlStatement) == SqlType.ADD) {
......@@ -280,9 +292,11 @@ public class Submitter {
}
}
}
return jobClient;
}
public static void executeJob(Executor executor, String[] statements) {
public static Optional<JobClient> executeJob(Executor executor, String[] statements) {
Optional<JobClient> jobClient = Optional.empty();
ExecutorConfig executorConfig = executor.getExecutorConfig();
List<StatementParam> ddl = new ArrayList<>();
......@@ -326,12 +340,15 @@ public class Submitter {
}
}
log.info("Executing FlinkSQL statement set: {}", String.join(FlinkSQLConstant.SEPARATOR, inserts));
executor.executeStatementSet(inserts);
TableResult tableResult = executor.executeStatementSet(inserts);
jobClient = tableResult.getJobClient();
log.info("Execution succeeded.");
} else {
// UseStatementSet defaults to true, where the logic is never executed
StatementParam item = trans.get(0);
log.info("Executing FlinkSQL: {}", item.getValue());
executor.executeSql(item.getValue());
TableResult tableResult = executor.executeSql(item.getValue());
jobClient = tableResult.getJobClient();
log.info("Execution succeeded.");
}
}
......@@ -350,12 +367,14 @@ public class Submitter {
"The FlinkSQL statement set is being executed: {}",
String.join(FlinkSQLConstant.SEPARATOR, executes));
try {
executor.executeAsync(executorConfig.getJobName());
JobClient client = executor.executeAsync(executorConfig.getJobName());
jobClient = Optional.of(client);
log.info("The execution was successful");
} catch (Exception e) {
log.error("Execution failed, {}", e.getMessage(), e);
}
}
log.info("{} The task is successfully submitted", LocalDateTime.now());
return jobClient;
}
}
......@@ -28,6 +28,7 @@ import org.apache.flink.client.deployment.StandaloneClusterId;
import org.apache.flink.client.program.rest.RestClusterClient;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.core.execution.JobClient;
import org.apache.flink.runtime.client.JobStatusMessage;
import java.util.Collection;
......@@ -75,6 +76,28 @@ public class FlinkAppUtil {
}
}
public static void monitorFlinkTask(JobClient jobClient, int taskId) {
boolean isRun = true;
String jobId = jobClient.getJobID().toHexString();
try {
while (isRun) {
String jobStatus = jobClient.getJobStatus().get().toString();
JobStatus status = JobStatus.get(jobStatus);
if (status.isDone()) {
sendHook(taskId, jobId, 0);
log.info("refesh job status finished, status is {}", status);
isRun = false;
}
Thread.sleep(5000);
}
} catch (Exception e) {
// If an exception is thrown, it will cause the k8s pod to trigger a restart,
// resulting in an inability to exit normally
log.error("refesh status failed:", e);
}
}
/**
* The sendHook method is used to send a Hook request.
* This method sends an HTTP request to notify a specific address about the completion status of a task.
......@@ -82,9 +105,9 @@ public class FlinkAppUtil {
* If sending the request fails, it will be retried up to 30 times with a 1-second interval between each retry.
* If the retry limit is exceeded, an exception is thrown.
*/
private static void sendHook(int taskId, String jobId, int reTryCount) throws InterruptedException {
private static void sendHook(int taskId, String jobId, int reTryCount) {
String dinkyAddr = SystemConfiguration.getInstances().getDinkyAddr().getValue();
try {
String dinkyAddr = SystemConfiguration.getInstances().getDinkyAddr().getValue();
String url = StrFormatter.format(
"http://{}/api/jobInstance/hookJobDone?taskId={}&jobId={}", dinkyAddr, taskId, jobId);
String resultStr = HttpUtil.get(url);
......@@ -97,10 +120,14 @@ public class FlinkAppUtil {
} catch (Exception e) {
if (reTryCount < 30) {
log.error("send hook failed,retry later taskId:{},jobId:{},{}", taskId, jobId, e.getMessage());
Thread.sleep(1000);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
sendHook(taskId, jobId, reTryCount + 1);
} else {
throw new RuntimeException("Hook Job Done failed, The retry limit is exceeded: " + e.getMessage());
log.error("Hook Job Done failed to {}, The retry limit is exceeded: {}", dinkyAddr, e.getMessage());
}
}
}
......
......@@ -42,7 +42,6 @@ import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.configuration.CoreOptions;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.configuration.SecurityOptions;
import org.apache.flink.runtime.messages.webmonitor.JobDetails;
import org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails;
......@@ -105,10 +104,8 @@ public abstract class YarnGateway extends AbstractGateway {
private void initConfig() {
final ClusterConfig clusterConfig = config.getClusterConfig();
configuration = GlobalConfiguration.loadConfiguration(clusterConfig.getFlinkConfigPath());
if (!configuration.contains(RestOptions.PORT)) {
configuration.set(RestOptions.PORT, RestOptions.PORT.defaultValue());
}
configuration = GlobalConfiguration.loadConfiguration(
clusterConfig.getFlinkConfigPath().trim());
configuration.set(CoreOptions.CLASSLOADER_RESOLVE_ORDER, "parent-first");
final FlinkConfig flinkConfig = config.getFlinkConfig();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment