Optimize disable proxy instance with UI # control-panel-cluster (#5883)

* optimize registry disable proxy instance

* disabled proxy with UI

* for checkstyle

* revert code

* modify return type is boolean
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/DataSourceState.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/DataSourceState.java
new file mode 100644
index 0000000..3b69943
--- /dev/null
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/DataSourceState.java
@@ -0,0 +1,34 @@
+/*
+ * 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.orchestration.core.registrycenter.instance;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNodeStatus;
+
+/**
+ * Data source state.
+ */
+@Getter
+@Setter
+public final class DataSourceState {
+    
+    private RegistryCenterNodeStatus state;
+    
+    private Long lastConnect;
+}
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/InstanceState.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/InstanceState.java
new file mode 100644
index 0000000..d5696b8
--- /dev/null
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/instance/InstanceState.java
@@ -0,0 +1,40 @@
+/*
+ * 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.orchestration.core.registrycenter.instance;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNodeStatus;
+
+import java.util.Map;
+
+/**
+ * Instance state.
+ */
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+public final class InstanceState {
+    
+    private RegistryCenterNodeStatus state;
+    
+    private Map<String, DataSourceState> dataSources;
+}
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListener.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListener.java
index ff003ad..4f4a99a 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListener.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/main/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListener.java
@@ -17,10 +17,13 @@
 
 package org.apache.shardingsphere.orchestration.core.registrycenter.listener;
 
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.orchestration.center.RegistryCenterRepository;
 import org.apache.shardingsphere.orchestration.center.listener.DataChangedEvent;
 import org.apache.shardingsphere.orchestration.core.common.listener.PostShardingCenterRepositoryEventListener;
 import org.apache.shardingsphere.orchestration.core.registrycenter.event.CircuitStateChangedEvent;
+import org.apache.shardingsphere.orchestration.core.registrycenter.instance.InstanceState;
 import org.apache.shardingsphere.orchestration.core.registrycenter.instance.OrchestrationInstance;
 import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNode;
 import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNodeStatus;
@@ -38,6 +41,14 @@
     
     @Override
     protected CircuitStateChangedEvent createShardingOrchestrationEvent(final DataChangedEvent event) {
-        return new CircuitStateChangedEvent(RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(event.getValue()));
+        return new CircuitStateChangedEvent(isCircuitBreak(event.getValue()));
+    }
+    
+    private boolean isCircuitBreak(final String value) {
+        if (!Strings.isNullOrEmpty(value)) {
+            return RegistryCenterNodeStatus.DISABLED.toString()
+                    .equalsIgnoreCase(YamlEngine.unmarshal(value, InstanceState.class).getState().toString());
+        }
+        return false;
     }
 }
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/test/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListenerTest.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/test/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListenerTest.java
index 2ef841d..e30a3a7 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/test/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListenerTest.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-registrycenter/src/test/java/org/apache/shardingsphere/orchestration/core/registrycenter/listener/InstanceStateChangedListenerTest.java
@@ -50,6 +50,7 @@
     
     @Test
     public void assertCreateShardingOrchestrationEventWhenDisabled() {
-        assertTrue(instanceStateChangedListener.createShardingOrchestrationEvent(new DataChangedEvent("test/test_ds", RegistryCenterNodeStatus.DISABLED.name(), ChangedType.UPDATED)).isCircuitBreak());
+        assertTrue(instanceStateChangedListener.createShardingOrchestrationEvent(new DataChangedEvent("test/test_ds",
+                "state: " + RegistryCenterNodeStatus.DISABLED.name(), ChangedType.UPDATED)).isCircuitBreak());
     }
 }
diff --git a/shardingsphere-ui/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/OrchestrationServiceImpl.java b/shardingsphere-ui/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/OrchestrationServiceImpl.java
index 47964f1..f2e500f 100644
--- a/shardingsphere-ui/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/OrchestrationServiceImpl.java
+++ b/shardingsphere-ui/shardingsphere-ui-backend/src/main/java/org/apache/shardingsphere/ui/servcie/impl/OrchestrationServiceImpl.java
@@ -17,9 +17,12 @@
 
 package org.apache.shardingsphere.ui.servcie.impl;
 
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.masterslave.api.config.MasterSlaveDataSourceConfiguration;
 import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
 import org.apache.shardingsphere.orchestration.core.registrycenter.RegistryCenterNodeStatus;
+import org.apache.shardingsphere.orchestration.core.registrycenter.instance.InstanceState;
 import org.apache.shardingsphere.ui.common.dto.InstanceDTO;
 import org.apache.shardingsphere.ui.common.dto.SlaveDataSourceDTO;
 import org.apache.shardingsphere.ui.servcie.OrchestrationService;
@@ -34,6 +37,7 @@
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -53,16 +57,17 @@
         List<String> instanceIds = registryCenterService.getActivatedRegistryCenter().getChildrenKeys(getInstancesNodeFullRootPath());
         Collection<InstanceDTO> result = new ArrayList<>(instanceIds.size());
         for (String instanceId : instanceIds) {
-            String value = registryCenterService.getActivatedRegistryCenter().get(registryCenterService.getActivatedStateNode().getInstancesNodeFullPath(instanceId));
-            result.add(new InstanceDTO(instanceId, !RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(value)));
+            result.add(new InstanceDTO(instanceId, !RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(getInstanceStatus(instanceId))));
         }
         return result;
     }
     
     @Override
     public void updateInstanceStatus(final String instanceId, final boolean enabled) {
-        String value = enabled ? "" : RegistryCenterNodeStatus.DISABLED.toString();
-        registryCenterService.getActivatedRegistryCenter().persist(registryCenterService.getActivatedStateNode().getInstancesNodeFullPath(instanceId), value);
+        RegistryCenterNodeStatus value = enabled ? RegistryCenterNodeStatus.ONLINE : RegistryCenterNodeStatus.DISABLED;
+        InstanceState instanceState = Optional.ofNullable(loadInstanceState(instanceId)).orElse(new InstanceState());
+        instanceState.setState(value);
+        registryCenterService.getActivatedRegistryCenter().persist(registryCenterService.getActivatedStateNode().getInstancesNodeFullPath(instanceId), YamlEngine.marshal(instanceState));
     }
     
     @Override
@@ -70,12 +75,10 @@
         Collection<SlaveDataSourceDTO> result = new ArrayList<>();
         for (String schemaName : shardingSchemaService.getAllSchemaNames()) {
             String configData = shardingSchemaService.getRuleConfiguration(schemaName);
-            if (!configData.contains("encryptors:\n")) {
-                if (configData.contains("tables:\n")) {
-                    handleShardingRuleConfiguration(result, configData, schemaName);
-                } else {
-                    handleMasterSlaveRuleConfiguration(result, configData, schemaName);
-                }
+            if (configData.contains("!SHARDING")) {
+                handleShardingRuleConfiguration(result, configData, schemaName);
+            } else if (configData.contains("!MASTER_SLAVE")) {
+                handleMasterSlaveRuleConfiguration(result, configData, schemaName);
             }
         }
         return result;
@@ -132,4 +135,14 @@
         }
         return result;
     }
+    
+    private String getInstanceStatus(final String instanceId) {
+        InstanceState instanceState = loadInstanceState(instanceId);
+        return null == instanceState ? "" : instanceState.getState().toString();
+    }
+    
+    private InstanceState loadInstanceState(final String instanceId) {
+        String value = registryCenterService.getActivatedRegistryCenter().get(registryCenterService.getActivatedStateNode().getInstancesNodeFullPath(instanceId));
+        return Strings.isNullOrEmpty(value) ? null : YamlEngine.unmarshal(value, InstanceState.class);
+    }
 }