未验证 提交 5e4fa1a7 编写于 作者: H Haoran Meng 提交者: GitHub

Add load all instance states api for UI # control-panel-cluster (#5812)

* Add load all instance states api for UI

* remove redundant code

* optimize code about load state failed
上级 160f8cdd
......@@ -22,5 +22,5 @@ package org.apache.shardingsphere.cluster.state.enums;
*/
public enum NodeState {
ONLINE, INTERRUPT, OFFLINE, DISABLED;
ONLINE, INTERRUPT, OFFLINE, DISABLED, UNKNOWN;
}
......@@ -56,7 +56,11 @@
<artifactId>shardingsphere-orchestration-center-zookeeper-curator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-cluster-facade</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
......
/*
* 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.shardingsphere.ui.servcie;
import org.apache.shardingsphere.cluster.state.InstanceState;
import java.util.Map;
/**
* Cluster service.
*/
public interface ClusterService {
/**
* Load all instance states.
*
* @return all instance states
*/
Map<String, InstanceState> loadAllInstanceStates();
}
/*
* 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.shardingsphere.ui.servcie.impl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.cluster.state.InstanceState;
import org.apache.shardingsphere.cluster.state.enums.NodeState;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.ui.servcie.ClusterService;
import org.apache.shardingsphere.ui.servcie.RegistryCenterService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Implementation of cluster service.
*/
@Service
@Slf4j
public final class ClusterServiceImpl implements ClusterService {
@Resource
private RegistryCenterService registryCenterService;
@Override
public Map<String, InstanceState> loadAllInstanceStates() {
List<String> instanceIds = registryCenterService.getActivatedRegistryCenter()
.getChildrenKeys(registryCenterService.getActivatedStateNode().getInstanceNodeRootPath());
Map<String, InstanceState> instanceStateMap = new HashMap<>();
instanceIds.forEach(each -> instanceStateMap.put(each, loadInstanceState(each)));
return instanceStateMap;
}
private InstanceState loadInstanceState(final String instanceId) {
String instanceStateData = registryCenterService.getActivatedRegistryCenter()
.get(registryCenterService.getActivatedStateNode().getInstancesNodeFullPath(instanceId));
if (StringUtils.isEmpty(instanceStateData)) {
log.error("can not load instance '{}' state data", instanceId);
return new InstanceState(NodeState.UNKNOWN, new HashMap<>());
}
return YamlEngine.unmarshal(instanceStateData, InstanceState.class);
}
}
/*
* 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.shardingsphere.ui.web.controller;
import org.apache.shardingsphere.cluster.state.InstanceState;
import org.apache.shardingsphere.ui.servcie.ClusterService;
import org.apache.shardingsphere.ui.web.response.ResponseResult;
import org.apache.shardingsphere.ui.web.response.ResponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* RESTful API of cluster state.
*/
@RestController
@RequestMapping("/api/cluster-state")
public final class ClusterStateController {
@Autowired
private ClusterService clusterService;
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseResult<Map<String, InstanceState>> loadAllInstanceStates() {
return ResponseResultUtil.build(clusterService.loadAllInstanceStates());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册