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 04:01:01 UTC

[GitHub] [shardingsphere] menghaoranss opened a new pull request #5795: Proxy handle heart beat event #control-panel-cluster

menghaoranss opened a new pull request #5795:
URL: https://github.com/apache/shardingsphere/pull/5795


   For #5666.
   
   Changes proposed in this pull request:
   - proxy handle heart beat event & report heart beat 
   
   
   


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



[GitHub] [shardingsphere] kimmking merged pull request #5795: Proxy handle heart beat event #control-panel-cluster

Posted by GitBox <gi...@apache.org>.
kimmking merged pull request #5795:
URL: https://github.com/apache/shardingsphere/pull/5795


   


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



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

Posted by GitBox <gi...@apache.org>.
kimmking commented on a change in pull request #5795:
URL: https://github.com/apache/shardingsphere/pull/5795#discussion_r430139199



##########
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:
       log ex

##########
File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ShardingSphereSchemas.java
##########
@@ -142,4 +146,14 @@ public synchronized void renew(final SchemaAddedEvent schemaAddedEvent) throws S
     public synchronized void renew(final SchemaDeletedEvent schemaDeletedEvent) {
         schemas.remove(schemaDeletedEvent.getShardingSchemaName());
     }
+    
+    /**
+     * Heart beat detect.
+     *
+     * @param event heart beat detect notice event
+     */
+    @Subscribe
+    public synchronized void heartBeat(final HeartbeatDetectNoticeEvent event) {

Review comment:
       heartbeat




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



[GitHub] [shardingsphere] codecov-commenter commented on pull request #5795: Proxy handle heart beat event #control-panel-cluster

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on pull request #5795:
URL: https://github.com/apache/shardingsphere/pull/5795#issuecomment-633822172


   # [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=h1) Report
   > Merging [#5795](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=desc) into [master](https://codecov.io/gh/apache/shardingsphere/commit/20576455846b82a7c754d9f67434d64dd480260d&el=desc) will **decrease** coverage by `0.23%`.
   > The diff coverage is `4.70%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/shardingsphere/pull/5795/graphs/tree.svg?width=650&height=150&src=pr&token=ZvlXpWa7so)](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=tree)
   
   ```diff
   @@             Coverage Diff              @@
   ##             master    #5795      +/-   ##
   ============================================
   - Coverage     52.88%   52.64%   -0.24%     
     Complexity      433      433              
   ============================================
     Files          1181     1182       +1     
     Lines         20743    20832      +89     
     Branches       3732     3744      +12     
   ============================================
   - Hits          10970    10968       -2     
   - Misses         9109     9199      +90     
   - Partials        664      665       +1     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=tree) | Coverage Δ | Complexity Δ | |
   |---|---|---|---|
   | [...e/shardingsphere/cluster/facade/ClusterFacade.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtY29udHJvbC1wYW5lbC9zaGFyZGluZ3NwaGVyZS1jbHVzdGVyL3NoYXJkaW5nc3BoZXJlLWNsdXN0ZXItZmFjYWRlL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zaGFyZGluZ3NwaGVyZS9jbHVzdGVyL2ZhY2FkZS9DbHVzdGVyRmFjYWRlLmphdmE=) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (ø)` | |
   | [...ngsphere/cluster/heartbeat/task/HeartbeatTask.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtY29udHJvbC1wYW5lbC9zaGFyZGluZ3NwaGVyZS1jbHVzdGVyL3NoYXJkaW5nc3BoZXJlLWNsdXN0ZXItaGVhcnRiZWF0L3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zaGFyZGluZ3NwaGVyZS9jbHVzdGVyL2hlYXJ0YmVhdC90YXNrL0hlYXJ0YmVhdFRhc2suamF2YQ==) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (ø)` | |
   | [...proxy/backend/cluster/AbstractHeartbeatDetect.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC9jbHVzdGVyL0Fic3RyYWN0SGVhcnRiZWF0RGV0ZWN0LmphdmE=) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (?)` | |
   | [...gsphere/proxy/backend/cluster/HeartbeatDetect.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC9jbHVzdGVyL0hlYXJ0YmVhdERldGVjdC5qYXZh) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (?)` | |
   | [...sphere/proxy/backend/cluster/HeartbeatHandler.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC9jbHVzdGVyL0hlYXJ0YmVhdEhhbmRsZXIuamF2YQ==) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (?)` | |
   | [...ava/org/apache/shardingsphere/proxy/Bootstrap.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYm9vdHN0cmFwL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zaGFyZGluZ3NwaGVyZS9wcm94eS9Cb290c3RyYXAuamF2YQ==) | `0.00% <0.00%> (ø)` | `0.00 <0.00> (ø)` | |
   | [...re/proxy/backend/schema/ShardingSphereSchemas.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC9zY2hlbWEvU2hhcmRpbmdTcGhlcmVTY2hlbWFzLmphdmE=) | `24.13% <33.33%> (-2.79%)` | `0.00 <0.00> (ø)` | |
   | [...hestration/core/registrycenter/RegistryCenter.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtY29udHJvbC1wYW5lbC9zaGFyZGluZ3NwaGVyZS1vcmNoZXN0cmF0aW9uL3NoYXJkaW5nc3BoZXJlLW9yY2hlc3RyYXRpb24tY29yZS9zaGFyZGluZ3NwaGVyZS1vcmNoZXN0cmF0aW9uLWNvcmUtcmVnaXN0cnljZW50ZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL29yY2hlc3RyYXRpb24vY29yZS9yZWdpc3RyeWNlbnRlci9SZWdpc3RyeUNlbnRlci5qYXZh) | `100.00% <100.00%> (ø)` | `0.00 <0.00> (ø)` | |
   | [.../core/registrycenter/RegistryCenterNodeStatus.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtY29udHJvbC1wYW5lbC9zaGFyZGluZ3NwaGVyZS1vcmNoZXN0cmF0aW9uL3NoYXJkaW5nc3BoZXJlLW9yY2hlc3RyYXRpb24tY29yZS9zaGFyZGluZ3NwaGVyZS1vcmNoZXN0cmF0aW9uLWNvcmUtcmVnaXN0cnljZW50ZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL3NoYXJkaW5nc3BoZXJlL29yY2hlc3RyYXRpb24vY29yZS9yZWdpc3RyeWNlbnRlci9SZWdpc3RyeUNlbnRlck5vZGVTdGF0dXMuamF2YQ==) | `100.00% <100.00%> (ø)` | `0.00 <0.00> (ø)` | |
   | [...roxy/backend/text/admin/UnicastBackendHandler.java](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC90ZXh0L2FkbWluL1VuaWNhc3RCYWNrZW5kSGFuZGxlci5qYXZh) | `66.66% <0.00%> (-8.34%)` | `0.00% <0.00%> (ø%)` | |
   | ... and [25 more](https://codecov.io/gh/apache/shardingsphere/pull/5795/diff?src=pr&el=tree-more) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=continue).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=footer). Last update [2057645...e91cd87](https://codecov.io/gh/apache/shardingsphere/pull/5795?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
   


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



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

Posted by GitBox <gi...@apache.org>.
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