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 03:27:12 UTC

[GitHub] [shardingsphere] menghaoranss commented on a change in pull request #5795: Proxy handle heart beat event #control-panel-cluster

menghaoranss commented on a change in pull request #5795:
URL: https://github.com/apache/shardingsphere/pull/5795#discussion_r430141942



##########
File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/cluster/HeartbeatHandler.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.proxy.backend.cluster;
+
+import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.cluster.configuration.config.HeartbeatConfiguration;
+
+import org.apache.shardingsphere.cluster.facade.ClusterFacade;
+import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResponse;
+import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResult;
+import org.apache.shardingsphere.proxy.backend.schema.ShardingSphereSchema;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.Collection;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+
+/**
+ * Heart beat handler.
+ */
+public final class HeartbeatHandler {
+    
+    private HeartbeatConfiguration configuration;
+    
+    /**
+     * Init heart beat handler.
+     *
+     * @param configuration heart beat configuration
+     */
+    public void init(final HeartbeatConfiguration configuration) {
+        Preconditions.checkNotNull(configuration, "heart beat configuration can not be null.");
+        this.configuration = configuration;
+    }
+    
+    /**
+     * Get heart beat handler instance.
+     *
+     * @return heart beat handler instance
+     */
+    public static HeartbeatHandler getInstance() {
+        return HeartbeatHandlerHolder.INSTANCE;
+    }
+    
+    /**
+     * Handle heart beat detect event.
+     *
+     * @param schemas ShardingSphere schemas
+     */
+    public void handle(final Map<String, ShardingSphereSchema> schemas) {
+        ExecutorService executorService = Executors.newFixedThreadPool(countDataSource(schemas));
+        List<FutureTask<Map<String, HeartbeatResult>>> futureTasks = new ArrayList<>();
+        schemas.values().forEach(value -> value.getBackendDataSource().getDataSources().entrySet().forEach(entry -> {
+            FutureTask<Map<String, HeartbeatResult>> futureTask = new FutureTask<>(new HeartbeatDetect(value.getName(), entry.getKey(),
+                    entry.getValue(), configuration));
+            futureTasks.add(futureTask);
+            executorService.submit(futureTask);
+        }));
+        reportHeartbeat(futureTasks);
+        closeExecutor(executorService);
+    }
+    
+    private Integer countDataSource(final Map<String, ShardingSphereSchema> schemas) {
+        return Long.valueOf(schemas.values().stream().
+                collect(Collectors.summarizingInt(entry -> entry.getBackendDataSource().
+                        getDataSources().keySet().size())).getSum()).intValue();
+    }
+    
+    private void reportHeartbeat(final List<FutureTask<Map<String, HeartbeatResult>>> futureTasks) {
+        Map<String, Collection<HeartbeatResult>> heartbeatResultMap = new HashMap<>();
+        futureTasks.stream().forEach(each -> {
+            try {
+                each.get().entrySet().forEach(entry -> {
+                    if (Objects.isNull(heartbeatResultMap.get(entry.getKey()))) {
+                        heartbeatResultMap.put(entry.getKey(), new ArrayList<>(Arrays.asList(entry.getValue())));
+                    } else {
+                        heartbeatResultMap.get(entry.getKey()).add(entry.getValue());
+                    }
+                });
+            } catch (InterruptedException ex) {
+            

Review comment:
       got it.




----------------------------------------------------------------
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