Unverified Commit 14d35e84 authored by ZackYoung's avatar ZackYoung Committed by GitHub

[Optimize] [Gateway] optimize yarn app submit (#2824)

* optimize_yarn_app_submit * optimize_yarn_app_submit * optimize_yarn_app_submit
parent dd28f44f
......@@ -134,6 +134,7 @@ import org.slf4j.LoggerFactory;
/** The descriptor with deployment information for deploying a Flink cluster on Yarn. */
public class YarnClusterDescriptor implements ClusterDescriptor<ApplicationId> {
private static final Logger LOG = LoggerFactory.getLogger(YarnClusterDescriptor.class);
public static final String pathSeparator = ":";
......
/*
*
* 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.dinky.utils;
import org.apache.flink.runtime.rest.messages.MessageHeaders;
import org.apache.flink.runtime.rest.messages.ResponseBody;
import org.apache.flink.runtime.rest.util.RestMapperUtils;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collection;
public class FlinkJsonUtil {
private static final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
public static <T extends ResponseBody> T toBean(String json, MessageHeaders<?, T, ?> messageHeaders) {
final JavaType responseType;
final Collection<Class<?>> typeParameters = messageHeaders.getResponseTypeParameters();
if (typeParameters.isEmpty()) {
responseType = objectMapper.constructType(messageHeaders.getResponseClass());
} else {
responseType = objectMapper
.getTypeFactory()
.constructParametricType(
messageHeaders.getResponseClass(),
typeParameters.toArray(new Class<?>[typeParameters.size()]));
}
try {
JsonParser jsonParser = objectMapper.treeAsTokens(objectMapper.readTree(json));
JavaType javaType = objectMapper.constructType(responseType);
return objectMapper.readValue(jsonParser, javaType);
} catch (Exception e) {
throw new RuntimeException("json to flink bean exception.", e);
}
}
}
/*
*
* 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.dinky.utils;
import org.apache.flink.runtime.rest.messages.JobsOverviewHeaders;
import org.junit.Test;
public class FlinkJsonUtilTest {
@Test
public void toBeanTest() {
// If successful, the validation is successful
FlinkJsonUtil.toBean(
"{\"jobs\":[{\"jid\":\"495c4e271dc64a4b7e78b8b1eb8d75f5\",\"name\":\"datagen\",\"state\":\"RUNNING\",\"start-time\":1703649915151,\"end-time\":-1,\"duration\":78437474,\"last-modification\":1703649915340,\"tasks\":{\"total\":6,\"created\":0,\"scheduled\":0,\"deploying\":0,\"running\":6,\"finished\":0,\"canceling\":0,\"canceled\":0,\"failed\":0,\"reconciling\":0,\"initializing\":0}},{\"jid\":\"214bbbe27dec4272f18fc3f14e9522b6\",\"name\":\"datagen-1\",\"state\":\"RUNNING\",\"start-time\":1703654026497,\"end-time\":-1,\"duration\":74326128,\"last-modification\":1703654026613,\"tasks\":{\"total\":2,\"created\":0,\"scheduled\":0,\"deploying\":0,\"running\":2,\"finished\":0,\"canceling\":0,\"canceled\":0,\"failed\":0,\"reconciling\":0,\"initializing\":0}}]}",
JobsOverviewHeaders.getInstance());
}
}
......@@ -26,6 +26,7 @@ import org.dinky.gateway.config.AppConfig;
import org.dinky.gateway.enums.GatewayType;
import org.dinky.gateway.result.GatewayResult;
import org.dinky.gateway.result.YarnResult;
import org.dinky.utils.FlinkJsonUtil;
import org.dinky.utils.LogUtil;
import org.apache.flink.client.deployment.ClusterSpecification;
......@@ -33,17 +34,23 @@ import org.apache.flink.client.deployment.application.ApplicationConfiguration;
import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.client.program.ClusterClientProvider;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.runtime.client.JobStatusMessage;
import org.apache.flink.runtime.messages.webmonitor.JobDetails;
import org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails;
import org.apache.flink.runtime.rest.messages.JobsOverviewHeaders;
import org.apache.flink.yarn.YarnClusterDescriptor;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.ApplicationReport;
import org.apache.hadoop.yarn.api.records.YarnApplicationState;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
/**
* YarnApplicationGateway
*
......@@ -77,34 +84,53 @@ public class YarnApplicationGateway extends YarnGateway {
new ApplicationConfiguration(userJarParas, appConfig.getUserJarMainAppClass());
YarnResult result = YarnResult.build(getType());
String webUrl;
try (YarnClusterDescriptor yarnClusterDescriptor = createYarnClusterDescriptorWithJar(udfPathContextHolder)) {
ClusterClientProvider<ApplicationId> clusterClientProvider = yarnClusterDescriptor.deployApplicationCluster(
clusterSpecificationBuilder.createClusterSpecification(), applicationConfiguration);
ClusterClient<ApplicationId> clusterClient = clusterClientProvider.getClusterClient();
Collection<JobStatusMessage> jobStatusMessages =
clusterClient.listJobs().get();
int counts = SystemConfiguration.getInstances().getJobIdWait();
while (jobStatusMessages.size() == 0 && counts > 0) {
while (yarnClient.getApplicationReport(clusterClient.getClusterId()).getYarnApplicationState()
== YarnApplicationState.ACCEPTED
&& counts-- > 0) {
Thread.sleep(1000);
counts--;
jobStatusMessages = clusterClient.listJobs().get();
if (jobStatusMessages.size() > 0) {
}
ApplicationReport applicationReport = yarnClient.getApplicationReport(clusterClient.getClusterId());
if (applicationReport.getYarnApplicationState() != YarnApplicationState.RUNNING) {
throw new RuntimeException("Yarn application state is not running, please check yarn cluster status.");
}
webUrl = applicationReport.getOriginalTrackingUrl();
final List<JobDetails> jobDetailsList = new ArrayList<>();
while (jobDetailsList.isEmpty() && counts-- > 0) {
Thread.sleep(1000);
String url = ReUtil.replaceAll(
yarnClient
.getApplicationReport(clusterClient.getClusterId())
.getTrackingUrl()
+ JobsOverviewHeaders.URL,
"/+",
"/");
String json = HttpUtil.get(url);
MultipleJobsDetails jobsDetails = FlinkJsonUtil.toBean(json, JobsOverviewHeaders.getInstance());
jobDetailsList.addAll(jobsDetails.getJobs());
if (!jobDetailsList.isEmpty()) {
break;
}
}
if (jobStatusMessages.size() > 0) {
if (!jobDetailsList.isEmpty()) {
List<String> jobIds = new ArrayList<>();
for (JobStatusMessage jobStatusMessage : jobStatusMessages) {
jobIds.add(jobStatusMessage.getJobId().toHexString());
for (JobDetails jobDetails : jobDetailsList) {
jobIds.add(jobDetails.getJobId().toHexString());
}
result.setJids(jobIds);
}
ApplicationId applicationId = clusterClient.getClusterId();
result.setId(applicationId.toString());
result.setWebURL(clusterClient.getWebInterfaceURL());
result.setWebURL(webUrl);
result.success();
} catch (Exception e) {
result.fail(LogUtil.getError(e));
......
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