|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.qlangtech.plugins.incr.flink.launch; |
| 20 | + |
| 21 | +import com.qlangtech.tis.coredefine.module.action.TargetResName; |
| 22 | +import com.qlangtech.tis.lang.TisException; |
| 23 | +import org.apache.flink.client.program.rest.RestClusterClient; |
| 24 | +import org.apache.flink.runtime.rest.handler.legacy.messages.ClusterOverviewWithVersion; |
| 25 | +import org.apache.flink.runtime.rest.messages.EmptyMessageParameters; |
| 26 | +import org.apache.flink.runtime.rest.messages.EmptyRequestBody; |
| 27 | +import org.apache.flink.runtime.rest.messages.taskmanager.TaskManagerInfo; |
| 28 | +import org.apache.flink.runtime.rest.messages.taskmanager.TaskManagersHeaders; |
| 29 | +import org.apache.flink.runtime.rest.messages.taskmanager.TaskManagersInfo; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +/** |
| 38 | + * Flink集群Slot校验工具类 |
| 39 | + * 用于在部署Flink Job之前校验集群是否有足够的可用slot |
| 40 | + * |
| 41 | + * @author: 百岁(baisui@qlangtech.com) |
| 42 | + * @create: 2025-11-26 |
| 43 | + **/ |
| 44 | +public class FlinkSlotValidator { |
| 45 | + private static final Logger logger = LoggerFactory.getLogger(FlinkSlotValidator.class); |
| 46 | + |
| 47 | + /** |
| 48 | + * 校验Flink集群是否有足够的可用slot |
| 49 | + * |
| 50 | + * @param restClient RestClusterClient实例 |
| 51 | + * @param requiredSlots 所需的slot数量(等于配置的parallelism) |
| 52 | + * @param collection 任务目标资源名称 |
| 53 | + * @throws TisException 当可用slot不足时抛出异常 |
| 54 | + */ |
| 55 | + public static void validateAvailableSlots( |
| 56 | + RestClusterClient<?> restClient, |
| 57 | + int requiredSlots, |
| 58 | + TargetResName collection) throws TisException { |
| 59 | + |
| 60 | + if (requiredSlots <= 0) { |
| 61 | + throw new IllegalArgumentException("Required slots must be greater than 0, but got: " + requiredSlots); |
| 62 | + } |
| 63 | + |
| 64 | + String clusterUrl = restClient.getWebInterfaceURL(); |
| 65 | + |
| 66 | + try { |
| 67 | + logger.info("Starting slot validation for job: {}, required slots: {}, cluster: {}", |
| 68 | + collection.getName(), requiredSlots, clusterUrl); |
| 69 | + |
| 70 | + // 使用Flink原生API获取集群概览信息 |
| 71 | + CompletableFuture<ClusterOverviewWithVersion> overviewFuture = restClient.sendRequest( |
| 72 | + org.apache.flink.runtime.rest.messages.ClusterOverviewHeaders.getInstance(), |
| 73 | + EmptyMessageParameters.getInstance(), |
| 74 | + EmptyRequestBody.getInstance() |
| 75 | + ); |
| 76 | + ClusterOverviewWithVersion overview = overviewFuture.get(10, TimeUnit.SECONDS); |
| 77 | + |
| 78 | + // 使用Flink原生API获取TaskManager详细信息 |
| 79 | + CompletableFuture<TaskManagersInfo> taskManagersFuture = restClient.sendRequest( |
| 80 | + TaskManagersHeaders.getInstance(), |
| 81 | + EmptyMessageParameters.getInstance(), |
| 82 | + EmptyRequestBody.getInstance() |
| 83 | + ); |
| 84 | + TaskManagersInfo taskManagersInfo = taskManagersFuture.get(10, TimeUnit.SECONDS); |
| 85 | + |
| 86 | + // 计算可用slot总数 |
| 87 | + int totalSlots = overview.getNumSlotsTotal(); |
| 88 | + int availableSlots = overview.getNumSlotsAvailable(); |
| 89 | + int occupiedSlots = totalSlots - availableSlots; |
| 90 | + int taskManagerCount = overview.getNumTaskManagersConnected(); |
| 91 | + |
| 92 | + logger.info("Cluster slot status - Total: {}, Available: {}, Occupied: {}, TaskManagers: {}", |
| 93 | + totalSlots, availableSlots, occupiedSlots, taskManagerCount); |
| 94 | + |
| 95 | + // 校验可用slot是否足够 |
| 96 | + if (availableSlots < requiredSlots) { |
| 97 | + String errorMessage = buildInsufficientSlotsErrorMessage( |
| 98 | + collection, |
| 99 | + requiredSlots, |
| 100 | + availableSlots, |
| 101 | + totalSlots, |
| 102 | + occupiedSlots, |
| 103 | + taskManagerCount, |
| 104 | + taskManagersInfo, |
| 105 | + clusterUrl |
| 106 | + ); |
| 107 | + |
| 108 | + // logger.error(errorMessage); |
| 109 | + throw TisException.create(errorMessage); |
| 110 | + } |
| 111 | + |
| 112 | + logger.info("Slot validation passed. Available slots ({}) >= Required slots ({})", |
| 113 | + availableSlots, requiredSlots); |
| 114 | + |
| 115 | + } catch (TisException e) { |
| 116 | + throw e; |
| 117 | + } catch (Exception e) { |
| 118 | + String errorMsg = "Failed to validate available slots for job: " + collection.getName() |
| 119 | + + ", cluster address: " + clusterUrl; |
| 120 | + logger.error(errorMsg, e); |
| 121 | + throw TisException.create(errorMsg, e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * 构建slot不足时的详细错误信息 |
| 127 | + */ |
| 128 | + private static String buildInsufficientSlotsErrorMessage( |
| 129 | + TargetResName collection, |
| 130 | + int requiredSlots, |
| 131 | + int availableSlots, |
| 132 | + int totalSlots, |
| 133 | + int occupiedSlots, |
| 134 | + int taskManagerCount, |
| 135 | + TaskManagersInfo taskManagersInfo, |
| 136 | + String clusterUrl) { |
| 137 | + |
| 138 | + StringBuilder errorMsg = new StringBuilder(); |
| 139 | + errorMsg.append("Insufficient available slots in Flink cluster for job: ").append(collection.getName()).append("\n"); |
| 140 | + errorMsg.append("========================================\n"); |
| 141 | + errorMsg.append("Required slots: ").append(requiredSlots).append("\n"); |
| 142 | + errorMsg.append("Available slots: ").append(availableSlots).append("\n"); |
| 143 | + errorMsg.append("Total slots: ").append(totalSlots).append("\n"); |
| 144 | + errorMsg.append("Occupied slots: ").append(occupiedSlots).append("\n"); |
| 145 | + errorMsg.append("TaskManager count: ").append(taskManagerCount).append("\n"); |
| 146 | + |
| 147 | + // 添加TaskManager详细信息 |
| 148 | + if (taskManagersInfo != null && taskManagersInfo.getTaskManagerInfos() != null) { |
| 149 | + Collection<TaskManagerInfo> taskManagers = taskManagersInfo.getTaskManagerInfos(); |
| 150 | + if (!taskManagers.isEmpty()) { |
| 151 | + errorMsg.append("\nTaskManager Details:\n"); |
| 152 | + int index = 1; |
| 153 | + for (TaskManagerInfo tmInfo : taskManagers) { |
| 154 | + errorMsg.append(" [").append(index++).append("] "); |
| 155 | + errorMsg.append("ID: ").append(tmInfo.getResourceId().getResourceIdString()); |
| 156 | + errorMsg.append(", Slots: ").append(tmInfo.getNumberSlots()); |
| 157 | + errorMsg.append(", Free: ").append(tmInfo.getNumberAvailableSlots()); |
| 158 | + errorMsg.append("\n"); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + errorMsg.append("\nCluster Address: ").append(clusterUrl).append("\n"); |
| 164 | + errorMsg.append("========================================\n"); |
| 165 | + errorMsg.append("Suggestions:\n"); |
| 166 | + errorMsg.append(" 1. Increase the number of TaskManager instances\n"); |
| 167 | + errorMsg.append(" 2. Increase slots per TaskManager (taskmanager.numberOfTaskSlots)\n"); |
| 168 | + errorMsg.append(" 3. Reduce the parallelism from ").append(requiredSlots) |
| 169 | + .append(" to ").append(availableSlots).append(" or less\n"); |
| 170 | + errorMsg.append(" 4. Wait for running jobs to complete and release slots\n"); |
| 171 | + |
| 172 | + return errorMsg.toString(); |
| 173 | + } |
| 174 | +} |
0 commit comments