/
HeartbeatHandler.java
121 lines (107 loc) · 4.75 KB
/
HeartbeatHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 lombok.extern.slf4j.Slf4j;
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.
*/
@Slf4j
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 | ExecutionException ex) {
log.error("Heart beat report error", ex);
}
});
ClusterFacade.getInstance().reportHeartbeat(new HeartbeatResponse(heartbeatResultMap));
}
private void closeExecutor(final ExecutorService executorService) {
if (null != executorService && !executorService.isShutdown()) {
executorService.shutdown();
}
}
private static final class HeartbeatHandlerHolder {
public static final HeartbeatHandler INSTANCE = new HeartbeatHandler();
}
}