MonitorServiceImpl.java 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.apache.dolphinscheduler.api.service.impl;

import static org.apache.dolphinscheduler.common.utils.Preconditions.checkNotNull;

import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.MonitorService;
24
import org.apache.dolphinscheduler.api.utils.RegistryMonitor;
25
import org.apache.dolphinscheduler.common.Constants;
26
import org.apache.dolphinscheduler.common.enums.NodeType;
27 28 29 30 31 32
import org.apache.dolphinscheduler.common.model.Server;
import org.apache.dolphinscheduler.common.model.WorkerServerModel;
import org.apache.dolphinscheduler.dao.MonitorDBDao;
import org.apache.dolphinscheduler.dao.entity.MonitorRecord;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord;
33
import org.apache.dolphinscheduler.service.registry.RegistryClient;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Sets;

/**
 * monitor service impl
 */
@Service
50
public class MonitorServiceImpl extends BaseServiceImpl implements MonitorService {
51 52

    @Autowired
53 54 55 56
    private RegistryMonitor registryMonitor;

    @Autowired
    private RegistryClient registryClient;
57 58 59 60 61 62 63 64 65 66

    @Autowired
    private MonitorDBDao monitorDBDao;

    /**
     * query database state
     *
     * @param loginUser login user
     * @return data base state
     */
67
    @Override
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    public Map<String,Object> queryDatabaseState(User loginUser) {
        Map<String, Object> result = new HashMap<>();

        List<MonitorRecord> monitorRecordList = monitorDBDao.queryDatabaseState();

        result.put(Constants.DATA_LIST, monitorRecordList);
        putMsg(result, Status.SUCCESS);

        return result;

    }

    /**
     * query master list
     *
     * @param loginUser login user
     * @return master information list
     */
86
    @Override
87 88 89 90
    public Map<String,Object> queryMaster(User loginUser) {

        Map<String, Object> result = new HashMap<>();

91
        List<Server> masterServers = getServerListFromRegistry(true);
92 93 94 95 96 97 98 99 100 101 102 103
        result.put(Constants.DATA_LIST, masterServers);
        putMsg(result,Status.SUCCESS);

        return result;
    }

    /**
     * query zookeeper state
     *
     * @param loginUser login user
     * @return zookeeper information list
     */
104
    @Override
105 106 107
    public Map<String,Object> queryZookeeperState(User loginUser) {
        Map<String, Object> result = new HashMap<>();

108
        List<ZookeeperRecord> zookeeperRecordList = registryMonitor.zookeeperInfoList();
109 110 111 112 113 114 115 116 117 118 119 120 121 122

        result.put(Constants.DATA_LIST, zookeeperRecordList);
        putMsg(result, Status.SUCCESS);

        return result;

    }

    /**
     * query worker list
     *
     * @param loginUser login user
     * @return worker information list
     */
123
    @Override
124 125 126
    public Map<String,Object> queryWorker(User loginUser) {

        Map<String, Object> result = new HashMap<>();
127
        List<WorkerServerModel> workerServers = getServerListFromRegistry(false)
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
                .stream()
                .map((Server server) -> {
                    WorkerServerModel model = new WorkerServerModel();
                    model.setId(server.getId());
                    model.setHost(server.getHost());
                    model.setPort(server.getPort());
                    model.setZkDirectories(Sets.newHashSet(server.getZkDirectory()));
                    model.setResInfo(server.getResInfo());
                    model.setCreateTime(server.getCreateTime());
                    model.setLastHeartbeatTime(server.getLastHeartbeatTime());
                    return model;
                })
                .collect(Collectors.toList());

        Map<String, WorkerServerModel> workerHostPortServerMapping = workerServers
                .stream()
                .collect(Collectors.toMap(
                    (WorkerServerModel worker) -> {
                        String[] s = worker.getZkDirectories().iterator().next().split("/");
                        return s[s.length - 1];
                    }
                    , Function.identity()
                    , (WorkerServerModel oldOne, WorkerServerModel newOne) -> {
                        oldOne.getZkDirectories().addAll(newOne.getZkDirectories());
                        return oldOne;
                    }));

        result.put(Constants.DATA_LIST, workerHostPortServerMapping.values());
        putMsg(result,Status.SUCCESS);

        return result;
    }

161
    @Override
162
    public List<Server> getServerListFromRegistry(boolean isMaster) {
163

164 165 166
        checkNotNull(registryMonitor);
        NodeType nodeType = isMaster ? NodeType.MASTER : NodeType.WORKER;
        return registryClient.getServerList(nodeType);
167 168 169
    }

}