HadoopUtils.java 20.8 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.common.utils;
L
ligang 已提交
18

Q
qiaozhanwei 已提交
19 20 21
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.ExecutionStatus;
import org.apache.dolphinscheduler.common.enums.ResUploadType;
L
ligang 已提交
22 23 24 25 26
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
L
lgcareer 已提交
27
import org.apache.hadoop.fs.*;
L
ligang 已提交
28
import org.apache.hadoop.fs.FileSystem;
journey2018's avatar
journey2018 已提交
29
import org.apache.hadoop.security.UserGroupInformation;
L
ligang 已提交
30 31 32 33 34
import org.apache.hadoop.yarn.client.cli.RMAdminCLI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
L
add  
lgcareer 已提交
35
import java.security.PrivilegedExceptionAction;
L
ligang 已提交
36
import java.util.List;
37
import java.util.Map;
L
ligang 已提交
38 39 40 41 42 43 44 45 46 47 48
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * hadoop utils
 * single instance
 */
public class HadoopUtils implements Closeable {

    private static final Logger logger = LoggerFactory.getLogger(HadoopUtils.class);

L
lgcareer 已提交
49
    private static String hdfsUser = PropertyUtils.getString(Constants.HDFS_ROOT_USER);
L
ligang 已提交
50 51 52 53
    private static volatile HadoopUtils instance = new HadoopUtils();
    private static volatile Configuration configuration;
    private static FileSystem fs;

L
lgcareer 已提交
54

L
ligang 已提交
55
    private HadoopUtils(){
L
lgcareer 已提交
56 57 58
        if(StringUtils.isEmpty(hdfsUser)){
            hdfsUser = PropertyUtils.getString(Constants.HDFS_ROOT_USER);
        }
L
ligang 已提交
59
        init();
L
lgcareer 已提交
60
        initHdfsPath();
L
ligang 已提交
61 62 63
    }

    public static HadoopUtils getInstance(){
64 65 66 67
        // if kerberos startup , renew HadoopUtils
        if (CommonUtils.getKerberosStartupState()){
            return new HadoopUtils();
        }
L
ligang 已提交
68 69 70
        return instance;
    }

L
add  
lgcareer 已提交
71
    /**
72
     * init dolphinscheduler root path in hdfs
L
add  
lgcareer 已提交
73 74
     */
    private void initHdfsPath(){
Q
qiaozhanwei 已提交
75
        String hdfsPath = PropertyUtils.getString(Constants.DATA_STORE_2_HDFS_BASEPATH);
L
add  
lgcareer 已提交
76
        Path path = new Path(hdfsPath);
L
lgcareer 已提交
77

L
add  
lgcareer 已提交
78
        try {
L
lgcareer 已提交
79 80 81
            if (!fs.exists(path)) {
                fs.mkdirs(path);
            }
L
add  
lgcareer 已提交
82 83 84 85 86
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

L
lgcareer 已提交
87

L
ligang 已提交
88 89 90 91 92 93 94 95 96
    /**
     * init hadoop configuration
     */
    private void init() {
        if (configuration == null) {
            synchronized (HadoopUtils.class) {
                if (configuration == null) {
                    try {
                        configuration = new Configuration();
journey2018's avatar
journey2018 已提交
97

journey2018's avatar
journey2018 已提交
98 99 100 101
                        String resUploadStartupType = PropertyUtils.getString(Constants.RES_UPLOAD_STARTUP_TYPE);
                        ResUploadType resUploadType = ResUploadType.valueOf(resUploadStartupType);

                        if (resUploadType == ResUploadType.HDFS){
Q
qiaozhanwei 已提交
102
                            if (PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE)){
journey2018's avatar
journey2018 已提交
103
                                System.setProperty(Constants.JAVA_SECURITY_KRB5_CONF,
Q
qiaozhanwei 已提交
104
                                        PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH));
journey2018's avatar
journey2018 已提交
105 106
                                configuration.set(Constants.HADOOP_SECURITY_AUTHENTICATION,"kerberos");
                                UserGroupInformation.setConfiguration(configuration);
Q
qiaozhanwei 已提交
107 108
                                UserGroupInformation.loginUserFromKeytab(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME),
                                        PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH));
journey2018's avatar
journey2018 已提交
109
                            }
journey2018's avatar
journey2018 已提交
110

Q
qiaozhanwei 已提交
111
                            String defaultFS = configuration.get(Constants.FS_DEFAULTFS);
journey2018's avatar
journey2018 已提交
112 113 114
                            //first get key from core-site.xml hdfs-site.xml ,if null ,then try to get from properties file
                            // the default is the local file system
                            if(defaultFS.startsWith("file")){
Q
qiaozhanwei 已提交
115
                                String defaultFSProp = PropertyUtils.getString(Constants.FS_DEFAULTFS);
journey2018's avatar
journey2018 已提交
116
                                if(StringUtils.isNotBlank(defaultFSProp)){
Q
qiaozhanwei 已提交
117 118
                                    Map<String, String> fsRelatedProps = PropertyUtils.getPrefixedProperties("fs.");
                                    configuration.set(Constants.FS_DEFAULTFS,defaultFSProp);
119
                                    fsRelatedProps.forEach((key, value) -> configuration.set(key, value));
journey2018's avatar
journey2018 已提交
120
                                }else{
121
                                    logger.error("property:{} can not to be empty, please set!", Constants.FS_DEFAULTFS );
122 123 124
                                    throw new RuntimeException(
                                        String.format("property: %s can not to be empty, please set!", Constants.FS_DEFAULTFS)
                                        );
journey2018's avatar
journey2018 已提交
125
                                }
L
ligang 已提交
126
                            }else{
Q
qiaozhanwei 已提交
127
                                logger.info("get property:{} -> {}, from core-site.xml hdfs-site.xml ", Constants.FS_DEFAULTFS, defaultFS);
L
ligang 已提交
128 129
                            }

journey2018's avatar
journey2018 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                            if (fs == null) {
                                if(StringUtils.isNotEmpty(hdfsUser)){
                                    //UserGroupInformation ugi = UserGroupInformation.createProxyUser(hdfsUser,UserGroupInformation.getLoginUser());
                                    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(hdfsUser);
                                    ugi.doAs(new PrivilegedExceptionAction<Boolean>() {
                                        @Override
                                        public Boolean run() throws Exception {
                                            fs = FileSystem.get(configuration);
                                            return true;
                                        }
                                    });
                                }else{
                                    logger.warn("hdfs.root.user is not set value!");
                                    fs = FileSystem.get(configuration);
                                }
L
lgcareer 已提交
145
                            }
journey2018's avatar
journey2018 已提交
146
                        }else if (resUploadType == ResUploadType.S3){
Q
qiaozhanwei 已提交
147 148 149 150
                            configuration.set(Constants.FS_DEFAULTFS, PropertyUtils.getString(Constants.FS_DEFAULTFS));
                            configuration.set(Constants.FS_S3A_ENDPOINT, PropertyUtils.getString(Constants.FS_S3A_ENDPOINT));
                            configuration.set(Constants.FS_S3A_ACCESS_KEY, PropertyUtils.getString(Constants.FS_S3A_ACCESS_KEY));
                            configuration.set(Constants.FS_S3A_SECRET_KEY, PropertyUtils.getString(Constants.FS_S3A_SECRET_KEY));
journey2018's avatar
journey2018 已提交
151
                            fs = FileSystem.get(configuration);
L
ligang 已提交
152
                        }
journey2018's avatar
journey2018 已提交
153 154


Q
qiaozhanwei 已提交
155 156
                        String rmHaIds = PropertyUtils.getString(Constants.YARN_RESOURCEMANAGER_HA_RM_IDS);
                        String appAddress = PropertyUtils.getString(Constants.YARN_APPLICATION_STATUS_ADDRESS);
L
ligang 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                        if (!StringUtils.isEmpty(rmHaIds)) {
                            appAddress = getAppAddress(appAddress, rmHaIds);
                            logger.info("appAddress : {}", appAddress);
                        }
                        configuration.set(Constants.YARN_APPLICATION_STATUS_ADDRESS, appAddress);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }

                }
            }
        }
    }

    /**
     * @return Configuration
     */
    public Configuration getConfiguration() {
        return configuration;
    }

    /**
     * get application url
     *
D
dailidong 已提交
181 182
     * @param applicationId application id
     * @return url of application
L
ligang 已提交
183 184
     */
    public String getApplicationUrl(String applicationId) {
Q
qiaozhanwei 已提交
185
        return String.format(configuration.get(Constants.YARN_APPLICATION_STATUS_ADDRESS), applicationId);
L
ligang 已提交
186 187 188 189 190 191
    }

    /**
     * cat file on hdfs
     *
     * @param hdfsFilePath  hdfs file path
D
dailidong 已提交
192 193
     * @return byte[] byte array
     * @throws IOException errors
L
ligang 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
     */
    public byte[] catFile(String hdfsFilePath) throws IOException {

        if(StringUtils.isBlank(hdfsFilePath)){
            logger.error("hdfs file path:{} is blank",hdfsFilePath);
            return null;
        }

        FSDataInputStream fsDataInputStream = fs.open(new Path(hdfsFilePath));
        return IOUtils.toByteArray(fsDataInputStream);
    }



    /**
     * cat file on hdfs
     *
     * @param hdfsFilePath  hdfs file path
     * @param skipLineNums  skip line numbers
     * @param limit         read how many lines
D
dailidong 已提交
214 215
     * @return content of file
     * @throws IOException errors
L
ligang 已提交
216 217 218
     */
    public List<String> catFile(String hdfsFilePath, int skipLineNums, int limit) throws IOException {

journey2018's avatar
journey2018 已提交
219
        if (StringUtils.isBlank(hdfsFilePath)){
L
ligang 已提交
220 221 222 223
            logger.error("hdfs file path:{} is blank",hdfsFilePath);
            return null;
        }

224 225 226 227 228 229
        try (FSDataInputStream in = fs.open(new Path(hdfsFilePath))){
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            Stream<String> stream = br.lines().skip(skipLineNums).limit(limit);
            return stream.collect(Collectors.toList());
        }
        
L
ligang 已提交
230 231 232 233 234 235 236 237
    }

    /**
     * make the given file and all non-existent parents into
     * directories. Has the semantics of Unix 'mkdir -p'.
     * Existence of the directory hierarchy is not an error.
     *
     * @param hdfsPath path to create
D
dailidong 已提交
238 239
     * @return mkdir result
     * @throws IOException errors
L
ligang 已提交
240 241 242 243 244 245 246 247 248 249 250 251
     */
    public boolean mkdir(String hdfsPath) throws IOException {
        return fs.mkdirs(new Path(hdfsPath));
    }

    /**
     * copy files between FileSystems
     *
     * @param srcPath      source hdfs path
     * @param dstPath      destination hdfs path
     * @param deleteSource whether to delete the src
     * @param overwrite    whether to overwrite an existing file
D
dailidong 已提交
252 253
     * @return if success or not
     * @throws IOException errors
L
ligang 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
     */
    public boolean copy(String srcPath, String dstPath, boolean deleteSource, boolean overwrite) throws IOException {
        return FileUtil.copy(fs, new Path(srcPath), fs, new Path(dstPath), deleteSource, overwrite, fs.getConf());
    }

    /**
     * the src file is on the local disk.  Add it to FS at
     * the given dst name.

     * @param srcFile       local file
     * @param dstHdfsPath   destination hdfs path
     * @param deleteSource  whether to delete the src
     * @param overwrite     whether to overwrite an existing file
D
dailidong 已提交
267 268
     * @return if success or not
     * @throws IOException errors
L
ligang 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
     */
    public boolean copyLocalToHdfs(String srcFile, String dstHdfsPath, boolean deleteSource, boolean overwrite) throws IOException {
        Path srcPath = new Path(srcFile);
        Path dstPath= new Path(dstHdfsPath);

        fs.copyFromLocalFile(deleteSource, overwrite, srcPath, dstPath);

        return true;
    }

    /**
     * copy hdfs file to local
     *
     * @param srcHdfsFilePath   source hdfs file path
     * @param dstFile           destination file
     * @param deleteSource      delete source
     * @param overwrite         overwrite
D
dailidong 已提交
286 287
     * @return result of copy hdfs file to local
     * @throws IOException errors
L
ligang 已提交
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
     */
    public boolean copyHdfsToLocal(String srcHdfsFilePath, String dstFile, boolean deleteSource, boolean overwrite) throws IOException {
        Path srcPath = new Path(srcHdfsFilePath);
        File dstPath = new File(dstFile);

        if (dstPath.exists()) {
            if (dstPath.isFile()) {
                if (overwrite) {
                    dstPath.delete();
                }
            } else {
                logger.error("destination file must be a file");
            }
        }

        if(!dstPath.getParentFile().exists()){
            dstPath.getParentFile().mkdirs();
        }

        return FileUtil.copy(fs, srcPath, dstPath, deleteSource, fs.getConf());
    }

    /**
     *
     * delete a file
     *
     * @param hdfsFilePath the path to delete.
     * @param recursive if path is a directory and set to
     * true, the directory is deleted else throws an exception. In
     * case of a file the recursive can be set to either true or false.
     * @return  true if delete is successful else false.
D
dailidong 已提交
319
     * @throws IOException errors
L
ligang 已提交
320 321 322 323 324 325 326 327 328
     */
    public boolean delete(String hdfsFilePath, boolean recursive) throws IOException {
        return fs.delete(new Path(hdfsFilePath), recursive);
    }

    /**
     * check if exists
     *
     * @param hdfsFilePath source file path
D
dailidong 已提交
329 330
     * @return result of exists or not
     * @throws IOException errors
L
ligang 已提交
331 332 333 334 335
     */
    public boolean exists(String hdfsFilePath) throws IOException {
        return fs.exists(new Path(hdfsFilePath));
    }

L
ligang 已提交
336 337 338
    /**
     * Gets a list of files in the directory
     *
D
dailidong 已提交
339 340 341
     * @param filePath file path
     * @return {@link FileStatus} file status
     * @throws Exception errors
L
ligang 已提交
342 343 344 345 346 347 348 349 350 351
     */
    public FileStatus[] listFileStatus(String filePath)throws Exception{
        try {
            return fs.listStatus(new Path(filePath));
        } catch (IOException e) {
            logger.error("Get file list exception", e);
            throw new Exception("Get file list exception", e);
        }
    }

L
ligang 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    /**
     * Renames Path src to Path dst.  Can take place on local fs
     * or remote DFS.
     * @param src path to be renamed
     * @param dst new path after rename
     * @throws IOException on failure
     * @return true if rename is successful
     */
    public boolean rename(String src, String dst) throws IOException {
        return fs.rename(new Path(src), new Path(dst));
    }


    /**
     * get the state of an application
     *
D
dailidong 已提交
368
     * @param applicationId application id
L
ligang 已提交
369
     * @return the return may be null or there may be other parse exceptions
D
dailidong 已提交
370
     * @throws JSONException json exception
L
ligang 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384
     */
    public ExecutionStatus getApplicationStatus(String applicationId) throws JSONException {
        if (StringUtils.isEmpty(applicationId)) {
            return null;
        }

        String applicationUrl = getApplicationUrl(applicationId);

        String responseContent = HttpUtils.get(applicationUrl);

        JSONObject jsonObject = JSONObject.parseObject(responseContent);
        String result = jsonObject.getJSONObject("app").getString("finalStatus");

        switch (result) {
Q
qiaozhanwei 已提交
385
            case Constants.ACCEPTED:
L
ligang 已提交
386
                return ExecutionStatus.SUBMITTED_SUCCESS;
Q
qiaozhanwei 已提交
387
            case Constants.SUCCEEDED:
L
ligang 已提交
388
                return ExecutionStatus.SUCCESS;
Q
qiaozhanwei 已提交
389 390 391 392
            case Constants.NEW:
            case Constants.NEW_SAVING:
            case Constants.SUBMITTED:
            case Constants.FAILED:
L
ligang 已提交
393
                return ExecutionStatus.FAILURE;
Q
qiaozhanwei 已提交
394
            case Constants.KILLED:
L
ligang 已提交
395 396
                return ExecutionStatus.KILL;

Q
qiaozhanwei 已提交
397
            case Constants.RUNNING:
L
ligang 已提交
398 399 400 401 402 403 404 405 406 407
            default:
                return ExecutionStatus.RUNNING_EXEUTION;
        }
    }

    /**
     *
     * @return data hdfs path
     */
    public static String getHdfsDataBasePath() {
Q
qiaozhanwei 已提交
408
        String basePath = PropertyUtils.getString(Constants.DATA_STORE_2_HDFS_BASEPATH);
409 410 411 412 413 414
        if ("/".equals(basePath)) {
            // if basepath is configured to /,  the generated url may be  //default/resources (with extra leading /)
            return "";
        } else {
            return basePath;
        }
L
ligang 已提交
415 416 417 418 419 420 421 422
    }

    /**
     * hdfs resource dir
     *
     * @param tenantCode tenant code
     * @return hdfs resource dir
     */
423
    public static String getHdfsResDir(String tenantCode) {
L
ligang 已提交
424 425 426 427
        return String.format("%s/resources", getHdfsTenantDir(tenantCode));
    }

    /**
428 429 430
     * hdfs user dir
     *
     * @param tenantCode tenant code
D
dailidong 已提交
431
     * @param userId user id
432 433 434 435 436 437 438 439
     * @return hdfs resource dir
     */
    public static String getHdfsUserDir(String tenantCode,int userId) {
        return String.format("%s/home/%d", getHdfsTenantDir(tenantCode),userId);
    }

    /**
     * hdfs udf dir
L
ligang 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
     *
     * @param tenantCode tenant code
     * @return get udf dir on hdfs
     */
    public static String getHdfsUdfDir(String tenantCode) {
        return String.format("%s/udfs", getHdfsTenantDir(tenantCode));
    }

    /**
     * get absolute path and name for file on hdfs
     *
     * @param tenantCode tenant code
     * @param filename   file name
     * @return get absolute path and name for file on hdfs
     */
    public static String getHdfsFilename(String tenantCode, String filename) {
456
        return String.format("%s/%s", getHdfsResDir(tenantCode), filename);
L
ligang 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470
    }

    /**
     * get absolute path and name for udf file on hdfs
     *
     * @param tenantCode tenant code
     * @param filename   file name
     * @return get absolute path and name for udf file on hdfs
     */
    public static String getHdfsUdfFilename(String tenantCode, String filename) {
        return String.format("%s/%s", getHdfsUdfDir(tenantCode), filename);
    }

    /**
D
dailidong 已提交
471
     * @param tenantCode tenant code
L
ligang 已提交
472 473
     * @return file directory of tenants on hdfs
     */
474
    public static String getHdfsTenantDir(String tenantCode) {
475
        return String.format("%s/%s", getHdfsDataBasePath(), tenantCode);
L
ligang 已提交
476 477 478 479 480 481
    }


    /**
     * getAppAddress
     *
D
dailidong 已提交
482 483 484
     * @param appAddress app address
     * @param rmHa resource manager ha
     * @return app address
L
ligang 已提交
485 486 487 488 489 490
     */
    public static String getAppAddress(String appAddress, String rmHa) {

        //get active ResourceManager
        String activeRM = YarnHAAdminUtils.getAcitveRMName(rmHa);

Q
qiaozhanwei 已提交
491
        String[] split1 = appAddress.split(Constants.DOUBLE_SLASH);
L
ligang 已提交
492 493 494 495 496

        if (split1.length != 2) {
            return null;
        }

Q
qiaozhanwei 已提交
497 498
        String start = split1[0] + Constants.DOUBLE_SLASH;
        String[] split2 = split1[1].split(Constants.COLON);
L
ligang 已提交
499 500 501 502 503

        if (split2.length != 2) {
            return null;
        }

Q
qiaozhanwei 已提交
504
        String end = Constants.COLON + split2[1];
L
ligang 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

        return start + activeRM + end;
    }


    @Override
    public void close() throws IOException {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                logger.error("Close HadoopUtils instance failed", e);
                throw new IOException("Close HadoopUtils instance failed", e);
            }
        }
    }


    /**
     * yarn ha admin utils
     */
    private static final class YarnHAAdminUtils extends RMAdminCLI {

        private static final Logger logger = LoggerFactory.getLogger(YarnHAAdminUtils.class);

        /**
         * get active resourcemanager
         *
         * @param rmIds
         * @return
         */
        public static String getAcitveRMName(String rmIds) {

Q
qiaozhanwei 已提交
538
            String[] rmIdArr = rmIds.split(Constants.COMMA);
L
ligang 已提交
539

Q
qiaozhanwei 已提交
540
            int activeResourceManagerPort = PropertyUtils.getInt(Constants.HADOOP_RESOURCE_MANAGER_HTTPADDRESS_PORT, 8088);
L
ligang 已提交
541 542 543 544 545 546 547 548 549 550

            String yarnUrl = "http://%s:" + activeResourceManagerPort + "/ws/v1/cluster/info";

            String state = null;
            try {
                /**
                 * send http get request to rm1
                 */
                state = getRMState(String.format(yarnUrl, rmIdArr[0]));

Q
qiaozhanwei 已提交
551
                if (Constants.HADOOP_RM_STATE_ACTIVE.equals(state)) {
L
ligang 已提交
552
                    return rmIdArr[0];
Q
qiaozhanwei 已提交
553
                } else if (Constants.HADOOP_RM_STATE_STANDBY.equals(state)) {
L
ligang 已提交
554
                    state = getRMState(String.format(yarnUrl, rmIdArr[1]));
Q
qiaozhanwei 已提交
555
                    if (Constants.HADOOP_RM_STATE_ACTIVE.equals(state)) {
L
ligang 已提交
556 557 558 559 560 561 562
                        return rmIdArr[1];
                    }
                } else {
                    return null;
                }
            } catch (Exception e) {
                state = getRMState(String.format(yarnUrl, rmIdArr[1]));
Q
qiaozhanwei 已提交
563
                if (Constants.HADOOP_RM_STATE_ACTIVE.equals(state)) {
L
ligang 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
                    return rmIdArr[0];
                }
            }
            return null;
        }


        /**
         * get ResourceManager state
         *
         * @param url
         * @return
         */
        public static String getRMState(String url) {

            String retStr = HttpUtils.get(url);

            if (StringUtils.isEmpty(retStr)) {
                return null;
            }
            //to json
            JSONObject jsonObject = JSON.parseObject(retStr);

            //get ResourceManager state
            String state = jsonObject.getJSONObject("clusterInfo").getString("haState");
            return state;
        }

    }
}