You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/05/27 12:49:51 UTC

[GitHub] [shardingsphere] kezhenxu94 commented on a change in pull request #5812: Add load all instance states api for UI # control-panel-cluster

kezhenxu94 commented on a change in pull request #5812:
URL: https://github.com/apache/shardingsphere/pull/5812#discussion_r431091096



##########
File path: shardingsphere-ui/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/ClusterServiceImpl.java
##########
@@ -0,0 +1,64 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.cluster.state.InstanceState;
+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 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<>();
+        try {
+            instanceIds.forEach(each -> {
+                instanceStateMap.put(each, loadInstanceState(each));
+            });
+        } catch (Exception ex) {
+            log.error("Load all instance states error", ex);
+        }

Review comment:
       1. `try-catch` outside of the `forEach` is considered as a bad idea, if an item throws, all the following items are skipped
   2. Simply swallowing the exception is also a bad idea IMO, if I were you, I'll consider the instance state as something like "Unknown" if it throws




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org