Unverified Commit 29cbffa2 authored by luoshangjie's avatar luoshangjie Committed by GitHub

[Feature] Add the functionality to automatically retrieve the latest highly…

[Feature] Add the functionality to automatically retrieve the latest highly available JobManager address (#3392) Co-authored-by: 's avatarluoshangjie <luoshangjie@yfpharmacy.com>
parent e7bf47f8
......@@ -21,6 +21,7 @@ package org.dinky.job.handler;
import org.dinky.api.FlinkAPI;
import org.dinky.assertion.Asserts;
import org.dinky.cluster.FlinkClusterInfo;
import org.dinky.context.SpringContextUtils;
import org.dinky.data.constant.FlinkRestResultConstant;
import org.dinky.data.dto.ClusterConfigurationDTO;
......@@ -42,6 +43,8 @@ import org.dinky.gateway.config.GatewayConfig;
import org.dinky.gateway.exception.NotSupportGetStatusException;
import org.dinky.gateway.model.FlinkClusterConfig;
import org.dinky.job.JobConfig;
import org.dinky.service.ClusterInstanceService;
import org.dinky.service.HistoryService;
import org.dinky.service.JobHistoryService;
import org.dinky.service.JobInstanceService;
import org.dinky.utils.JsonUtils;
......@@ -70,10 +73,14 @@ public class JobRefreshHandler {
private static final JobInstanceService jobInstanceService;
private static final JobHistoryService jobHistoryService;
private static final ClusterInstanceService clusterInstanceService;
private static final HistoryService historyService;
static {
jobInstanceService = SpringContextUtils.getBean("jobInstanceServiceImpl", JobInstanceService.class);
jobHistoryService = SpringContextUtils.getBean("jobHistoryServiceImpl", JobHistoryService.class);
clusterInstanceService = SpringContextUtils.getBean("clusterInstanceServiceImpl", ClusterInstanceService.class);
historyService = SpringContextUtils.getBean("historyServiceImpl", HistoryService.class);
}
/**
......@@ -102,6 +109,9 @@ public class JobRefreshHandler {
jobInstanceService.updateById(jobInstance);
return true;
}
checkAndRefreshCluster(jobInfoDetail);
// Update the value of JobData from the flink api while ignoring the null value to prevent
// some other configuration from being overwritten
BeanUtil.copyProperties(
......@@ -308,4 +318,48 @@ public class JobRefreshHandler {
Gateway.build(jobConfig.getGatewayConfig()).onJobFinishCallback(jobInstance.getStatus());
}
}
/**
* In a YARN cluster with HA mode enabled,
* if the jobManagerHost cannot be connected,
* attempt to retrieve the latest address of the jobManagerHost from ZK
*
* @param jobInfoDetail The job info detail.
* @return The job status.
*/
private static void checkAndRefreshCluster(JobInfoDetail jobInfoDetail) {
if (!GatewayType.isDeployYarnCluster(jobInfoDetail.getClusterInstance().getType())) {
return;
}
FlinkClusterInfo flinkClusterInfo = clusterInstanceService.checkHeartBeat(
jobInfoDetail.getClusterInstance().getHosts(),
jobInfoDetail.getClusterInstance().getJobManagerHost());
if (!flinkClusterInfo.isEffective()) {
ClusterConfigurationDTO clusterCfg = jobInfoDetail.getClusterConfiguration();
ClusterInstance clusterInstance = jobInfoDetail.getClusterInstance();
if (!Asserts.isNull(clusterCfg)) {
String appId = jobInfoDetail.getClusterInstance().getName();
GatewayConfig gatewayConfig = GatewayConfig.build(clusterCfg.getConfig());
gatewayConfig.getClusterConfig().setAppId(appId);
gatewayConfig
.getFlinkConfig()
.setJobName(jobInfoDetail.getInstance().getName());
Gateway gateway = Gateway.build(gatewayConfig);
String latestJobManageHost = gateway.getLatestJobManageHost(appId, clusterInstance.getJobManagerHost());
if (Asserts.isNotNull(latestJobManageHost)) {
clusterInstance.setHosts(latestJobManageHost);
clusterInstance.setJobManagerHost(latestJobManageHost);
clusterInstanceService.updateById(clusterInstance);
if (Asserts.isNotNull(jobInfoDetail.getHistory())) {
jobInfoDetail.getHistory().setJobManagerAddress(latestJobManageHost);
historyService.updateById(jobInfoDetail.getHistory());
}
}
}
}
}
}
......@@ -83,6 +83,10 @@ public enum GatewayType {
return get(type).isDeployCluster();
}
public static boolean isDeployYarnCluster(String type) {
return get(type).isDeployYarnCluster();
}
/**
* 是否在本地构建 job graph , 用于校验是否提交到集群
* @return true: 本地构建 jobgraph
......@@ -112,6 +116,16 @@ public enum GatewayType {
}
}
public boolean isDeployYarnCluster() {
switch (this) {
case YARN_APPLICATION:
case YARN_PER_JOB:
return true;
default:
return false;
}
}
public boolean isApplicationMode() {
switch (this) {
case YARN_APPLICATION:
......
/*
*
* 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.data.enums;
/**
* @author luoshangjie
* @projectName dinky
* @description:
* @date 2024/4/13 12:51
*/
public enum HaModeEnum {
/**
* no highly available
*/
NONE,
/**
* ZOOKEEPER highly available
*/
ZOOKEEPER,
/**
* KUBERNETES highly available
*/
KUBERNETES
}
......@@ -236,4 +236,9 @@ public abstract class AbstractGateway implements Gateway {
public boolean onJobFinishCallback(String status) {
return true;
}
@Override
public String getLatestJobManageHost(String appId, String oldJobManagerHost) {
throw new NotSupportGetStatusException("Does not support obtaining the latest JobManager host address");
}
}
......@@ -90,4 +90,6 @@ public interface Gateway {
boolean onJobFinishCallback(String status);
GatewayResult deployCluster(FlinkUdfPathContextHolder udfPathContextHolder);
String getLatestJobManageHost(String appId, String oldJobManagerHost);
}
......@@ -38,13 +38,16 @@ import org.dinky.gateway.result.YarnResult;
import org.dinky.utils.FlinkJsonUtil;
import org.dinky.utils.ThreadUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.client.deployment.ClusterRetrieveException;
import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.configuration.ConfigConstants;
import org.apache.flink.configuration.CoreOptions;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.configuration.HighAvailabilityOptions;
import org.apache.flink.configuration.SecurityOptions;
import org.apache.flink.runtime.jobmanager.HighAvailabilityMode;
import org.apache.flink.runtime.messages.webmonitor.JobDetails;
import org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails;
import org.apache.flink.runtime.rest.messages.JobsOverviewHeaders;
......@@ -66,9 +69,12 @@ import org.apache.hadoop.yarn.api.records.YarnApplicationState;
import org.apache.hadoop.yarn.client.api.YarnClient;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.zookeeper.ZooKeeper;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -418,4 +424,85 @@ public abstract class YarnGateway extends AbstractGateway {
public boolean close() {
return FileUtil.del(tmpConfDir);
}
@Override
public String getLatestJobManageHost(String appId, String oldJobManagerHost) {
initConfig();
HighAvailabilityMode highAvailabilityMode = HighAvailabilityMode.fromConfig(configuration);
if (HighAvailabilityMode.ZOOKEEPER == highAvailabilityMode) {
configuration.setString(HighAvailabilityOptions.HA_CLUSTER_ID, appId);
String zkQuorum = configuration.getValue(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM);
if (zkQuorum == null || StringUtils.isBlank(zkQuorum)) {
throw new RuntimeException("No valid ZooKeeper quorum has been specified. "
+ "You can specify the quorum via the configuration key '"
+ HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM.key()
+ "'.");
}
int sessionTimeout = configuration.getInteger(HighAvailabilityOptions.ZOOKEEPER_SESSION_TIMEOUT);
String root = configuration.getValue(HighAvailabilityOptions.HA_ZOOKEEPER_ROOT);
String namespace = configuration.getValue(HighAvailabilityOptions.HA_CLUSTER_ID);
ZooKeeper zooKeeper = null;
try {
zooKeeper = new ZooKeeper(zkQuorum, sessionTimeout, watchedEvent -> {});
String path = generateZookeeperPath(root, namespace, "leader", "rest_server", "connection_info");
byte[] data = zooKeeper.getData(path, false, null);
if (data != null && data.length > 0) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bais);
final String leaderAddress = ois.readUTF();
if (Asserts.isNotNullString(leaderAddress)) {
String hosts = leaderAddress.substring(7);
if (!oldJobManagerHost.equals(hosts)) {
return hosts;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (Asserts.isNotNull(zooKeeper)) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
return null;
}
/** Creates a ZooKeeper path of the form "/a/b/.../z". */
private static String generateZookeeperPath(String... paths) {
final String result = Arrays.stream(paths)
.map(YarnGateway::trimSlashes)
.filter(s -> !s.isEmpty())
.collect(Collectors.joining("/", "/", ""));
return result;
}
private static String trimSlashes(String input) {
int left = 0;
int right = input.length() - 1;
while (left <= right && input.charAt(left) == '/') {
left++;
}
while (right >= left && input.charAt(right) == '/') {
right--;
}
if (left <= right) {
return input.substring(left, right + 1);
} else {
return "";
}
}
}
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