1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
| package org.apache.ambari.server.controller.internal;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import com.google.inject.Inject; import org.apache.ambari.server.StaticallyInject; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.YarnRequest; import org.apache.ambari.server.controller.YarnResponse; import org.apache.ambari.server.controller.spi.*; import org.apache.ambari.server.orm.dao.HostComponentStateDAO; import org.apache.ambari.server.orm.entities.HostComponentStateEntity; import org.apache.ambari.server.utils.ShellCommandUtil; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.*; import java.nio.charset.Charset; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set;
/** * Yarn资源 * * @author Erik * @date 2020/12/7 */ @StaticallyInject public class YarnResourceProvider extends AbstractControllerResourceProvider {
private static final Logger LOG = LoggerFactory.getLogger(YarnResourceProvider.class);
@Inject protected static HostComponentStateDAO hostComponentStateDAO;
public static final String YARN_RESOURCE_CATEGORY = "yarn";
/** * Yarn application info */ public static final String APPLICATION_ID_PROPERTY_ID = "applicationId"; public static final String APPLICATION_NAME_PROPERTY_ID = "applicationName"; public static final String APPLICATION_TYPE_PROPERTY_ID = "applicationType"; public static final String USER_PROPERTY_ID = "user"; public static final String QUEUE_PROPERTY_ID = "queue"; public static final String STATE_PROPERTY_ID = "state"; public static final String FINAL_STATE_PROPERTY_ID = "finalState"; public static final String PROGRESS_PROPERTY_ID = "progress"; public static final String TRACKINGURL_PROPERTY_ID = "trackingUrl"; public static final String LOG_INFO_PROPERTY_ID = "logInfo";
public static final String YARN_APPLICATION_ID_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + APPLICATION_ID_PROPERTY_ID; public static final String YARN_APPLICATION_NAME_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + APPLICATION_NAME_PROPERTY_ID; public static final String YARN_APPLICATION_TYPE_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + APPLICATION_TYPE_PROPERTY_ID; public static final String YARN_USER_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + USER_PROPERTY_ID; public static final String YARN_QUEUE_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + QUEUE_PROPERTY_ID; public static final String YARN_STATE_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + STATE_PROPERTY_ID; public static final String YARN_FINAL_STATE_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + FINAL_STATE_PROPERTY_ID; public static final String YARN_PROGRESS_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + PROGRESS_PROPERTY_ID; public static final String YARN_TRACKINGURL_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + TRACKINGURL_PROPERTY_ID; public static final String YARN_LOG_INFO_PROPERTY_ID = YARN_RESOURCE_CATEGORY + "/" + LOG_INFO_PROPERTY_ID;
/** * The key property ids for a Yarn resource. */ private static Map<Resource.Type, String> keyPropertyIds = ImmutableMap.<Resource.Type, String>builder() .put(Resource.Type.Yarn, YARN_APPLICATION_ID_PROPERTY_ID) .build();
/** * The property ids for a Yarn resource. */ public static Set<String> propertyIds = Sets.newHashSet( YARN_APPLICATION_ID_PROPERTY_ID, YARN_APPLICATION_NAME_PROPERTY_ID, YARN_APPLICATION_TYPE_PROPERTY_ID, YARN_USER_PROPERTY_ID, YARN_QUEUE_PROPERTY_ID, YARN_STATE_PROPERTY_ID, YARN_FINAL_STATE_PROPERTY_ID, YARN_PROGRESS_PROPERTY_ID, YARN_TRACKINGURL_PROPERTY_ID, YARN_LOG_INFO_PROPERTY_ID);
public YarnResourceProvider(AmbariManagementController managementController) { super(Resource.Type.Yarn, propertyIds, keyPropertyIds, managementController); }
@Override protected Set<String> getPKPropertyIds() { return new HashSet<>(keyPropertyIds.values()); }
@Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
LOG.info("Into YarnResourceProvider:getResource."); final Set<YarnRequest> requests = new HashSet<>();
if (predicate == null) { LOG.info("Predicate is null."); requests.add(getRequest(null)); } else { LOG.info("Predicate not null."); for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { YarnRequest req = getRequest(propertyMap); requests.add(req); } }
// 获取 Yarn 资源 Set<YarnResponse> responses = getYarnResources(requests);
// 构建返回数据 Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<>();
for (YarnResponse yarnResponse : responses) { ResourceImpl resource = new ResourceImpl(Resource.Type.Yarn);
setResourceProperty(resource, YARN_APPLICATION_ID_PROPERTY_ID, yarnResponse.getApplicationId(), requestedIds);
setResourceProperty(resource, YARN_APPLICATION_NAME_PROPERTY_ID, yarnResponse.getApplicationName(), requestedIds);
setResourceProperty(resource, YARN_APPLICATION_TYPE_PROPERTY_ID, yarnResponse.getApplicationType(), requestedIds);
setResourceProperty(resource, YARN_USER_PROPERTY_ID, yarnResponse.getUser(), requestedIds);
setResourceProperty(resource, YARN_QUEUE_PROPERTY_ID, yarnResponse.getQueue(), requestedIds);
setResourceProperty(resource, YARN_STATE_PROPERTY_ID, yarnResponse.getState(), requestedIds);
setResourceProperty(resource, YARN_FINAL_STATE_PROPERTY_ID, yarnResponse.getFinalState(), requestedIds);
setResourceProperty(resource, YARN_PROGRESS_PROPERTY_ID, yarnResponse.getProgress(), requestedIds);
setResourceProperty(resource, YARN_LOG_INFO_PROPERTY_ID, yarnResponse.getLogInfo(), requestedIds);
resources.add(resource); }
return resources; }
/** * 获取 Yarn 资源信息 * * @param requests requests * @return Set<YarnResponse> */ private Set<YarnResponse> getYarnResources(Set<YarnRequest> requests) throws NoSuchResourceException { Set<YarnResponse> responsesSet = new HashSet<>();
// 获取 resourceManager 所在的节点 String hostName = getHostName();
for (YarnRequest request : requests) { String applicationId = request.getApplicationId();
/** * 如果 applicationId 为空,则返回所有 application 信息 * 如果 applicationId 不为空,则查询日志 */ if (null == applicationId) { LOG.info("applicationId is null.");
// 执行语句 String[] remoteAppListCmdStr = new String[]{"ssh", hostName, "yarn", "application", "-list", "-appStates", "ALL"}; printCmd(remoteAppListCmdStr);
String stdout = null; try {
ShellCommandUtil.Result cmdResult = ShellCommandUtil.runCommand(remoteAppListCmdStr);
// 判断结果码 if (cmdResult.getExitCode() == 0) { stdout = cmdResult.getStdout();
if (stdout != null) {
// 解析输出结果 responsesSet = parseApplicationInfo(stdout); } else { LOG.error("Run command stdout is empty!"); } } else { LOG.error("Run command error,exitCode is:{}.", cmdResult.getExitCode()); throw new SystemException("Run command error!"); } } catch (Exception e) { LOG.error("Run command error - Executor exception ({})", e.getMessage()); }
} else { LOG.info("ApplicationId is:{}.", applicationId);
try { String appLogCmdStr = "su hdfs -c \'yarn logs -applicationId " + applicationId + "\'"; String[] remoteAppLogCmdStr = new String[]{"ssh", hostName, appLogCmdStr}; printCmd(remoteAppLogCmdStr);
LOG.info("Start execute shell cmd."); ShellCommandUtil.Result cmdResult = ShellCommandUtil.runCommand(remoteAppLogCmdStr); LOG.info("Execute finish.");
LOG.info("cmdResult exitCode:{}", cmdResult.getExitCode()); if (cmdResult.getExitCode() == 0) { String stdout = cmdResult.getStdout();
YarnResponse yarnResponse = new YarnResponse(); yarnResponse.setLogInfo(stdout); yarnResponse.setApplicationId(applicationId); responsesSet.add(yarnResponse); } else { LOG.error("Run command error,exitCode is:{}.", cmdResult.getExitCode()); throw new SystemException("Run command error!"); }
} catch (Exception e) { LOG.error("Run command error - Executor exception ({})", e.getMessage()); } } }
return responsesSet; }
/** * 获取 resourcemanager 节点 */ private String getHostName() throws NoSuchResourceException { String hostName = null; String serviceName = "YARN"; String componentName = "RESOURCEMANAGER";
List<HostComponentStateEntity> hostComponentStateEntities = hostComponentStateDAO.findByServiceAndComponent(serviceName, componentName); HostComponentStateEntity hostComponentStateEntity = hostComponentStateEntities.get(0); if (hostComponentStateEntity != null) { hostName = hostComponentStateEntity.getHostName(); } else { throw new NoSuchResourceException("Not find resourceManager in this cluster!"); }
return hostName; }
/** * 解析 application 信息 * * @param stdout 输出信息 */ private Set<YarnResponse> parseApplicationInfo(String stdout) { Set<YarnResponse> yarnResponseSet = new HashSet<>(); // 将字符串转换为字节流 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stdout.getBytes(Charset.forName("utf8"))); BufferedReader br = new BufferedReader(new InputStreamReader(byteArrayInputStream, Charset.forName("utf8")));
/** * 逐行读取 * 只解析以“application_”开头的行 */ try { String line; while ((line = br.readLine()) != null) {
if (line.startsWith("application_")) {
String[] property = line.split("\t");
YarnResponse yarnResponse = new YarnResponse();
yarnResponse.setApplicationId(property[0].trim()); yarnResponse.setApplicationName(property[1].trim()); yarnResponse.setApplicationType(property[2].trim()); yarnResponse.setUser(property[3].trim()); yarnResponse.setQueue(property[4].trim()); yarnResponse.setState(property[5].trim()); yarnResponse.setFinalState(property[6].trim()); yarnResponse.setProgress(property[7].trim()); yarnResponse.setTrackingUrl(property[8].trim());
yarnResponseSet.add(yarnResponse); } } br.close(); } catch (IOException e) { e.printStackTrace(); }
return yarnResponseSet; }
/** * 打印执行命令 * * @param cmd 命令字符串 */ private void printCmd(String[] cmd) {
String cmdStr = ""; for (String str : cmd) { cmdStr = cmdStr + str + " "; }
LOG.info("Cmd str is:{}.", cmdStr); }
private YarnRequest getRequest(Map<String, Object> properties) { YarnRequest yarnRequest = new YarnRequest(null); if (properties == null) { return yarnRequest; }
Object object = properties.get(YARN_APPLICATION_ID_PROPERTY_ID); if (object != null) { String applicationId = properties.get(YARN_APPLICATION_ID_PROPERTY_ID).toString(); LOG.info("Request:applicationId:{}", applicationId);
yarnRequest = new YarnRequest(applicationId); }
return yarnRequest; }
}
|