|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.net.InetSocketAddress;
|
21 | 21 | import java.util.HashMap;
|
| 22 | +import java.util.HashSet; |
22 | 23 | import java.util.List;
|
23 | 24 | import java.util.Map;
|
24 | 25 | import java.util.ArrayList;
|
@@ -167,47 +168,38 @@ public Result<String> createGroup(String namespace, String vGroup, String cluste
|
167 | 168 | return new Result<>(String.valueOf(closeableHttpResponse.getStatusLine().getStatusCode()),
|
168 | 169 | "add vGroup in new cluster failed");
|
169 | 170 | }
|
| 171 | + LOGGER.info("namespace: {} add vGroup: {} in new cluster: {} successfully!", namespace, vGroup, clusterName); |
170 | 172 | } catch (IOException e) {
|
171 | 173 | LOGGER.warn("add vGroup in new cluster failed");
|
172 | 174 | return new Result<>("500", "add vGroup in new cluster failed");
|
173 | 175 | }
|
174 | 176 | }
|
175 |
| - addGroup(namespace,clusterName,unitName,vGroup); |
176 | 177 | return new Result<>("200", "add vGroup successfully!");
|
177 | 178 | }
|
178 | 179 |
|
179 |
| - public Result<String> removeGroup(String namespace, String clusterName,String vGroup, String unitName) { |
180 |
| - List<Cluster> clusterList = getClusterListByVgroup(vGroup, namespace); |
181 |
| - for (Cluster cluster : clusterList) { |
182 |
| - if (!StringUtils.equals(clusterName, cluster.getClusterName())) { |
183 |
| - continue; |
184 |
| - } |
185 |
| - if (cluster.getUnitData() != null && cluster.getUnitData().size() > 0) { |
186 |
| - Unit unit = cluster.getUnitData().get(0); |
187 |
| - if (unit != null && unit.getNamingInstanceList() != null && unit.getNamingInstanceList().size() > 0) { |
188 |
| - Node node = unit.getNamingInstanceList().get(0); |
189 |
| - String httpUrl = NamingServerConstants.HTTP_PREFIX + node.getControl().getHost() |
190 |
| - + NamingServerConstants.IP_PORT_SPLIT_CHAR + node.getControl().getPort() |
191 |
| - + NamingServerConstants.HTTP_REMOVE_GROUP_SUFFIX; |
192 |
| - HashMap<String, String> params = new HashMap<>(); |
193 |
| - params.put(CONSTANT_GROUP, vGroup); |
194 |
| - params.put(NamingServerConstants.CONSTANT_UNIT, unitName); |
195 |
| - Map<String, String> header = new HashMap<>(); |
196 |
| - header.put(HTTP.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); |
197 |
| - try (CloseableHttpResponse closeableHttpResponse = |
198 |
| - HttpClientUtil.doGet(httpUrl, params, header, 3000)) { |
199 |
| - if (closeableHttpResponse == null |
200 |
| - || closeableHttpResponse.getStatusLine().getStatusCode() != 200) { |
201 |
| - LOGGER.warn("remove vGroup in old cluster failed"); |
202 |
| - return new Result<>(String.valueOf(closeableHttpResponse.getStatusLine().getStatusCode()), |
203 |
| - "removing vGroup " + vGroup + " in old cluster " + cluster + " failed"); |
204 |
| - } |
205 |
| - } catch (IOException e) { |
206 |
| - LOGGER.warn("handle removing vGroup in old cluster failed"); |
207 |
| - return new Result<>("500", |
208 |
| - "handle removing vGroup " + vGroup + " in old cluster " + cluster + " failed"); |
209 |
| - } |
| 180 | + public Result<String> removeGroup(Unit unit, String vGroup, String clusterName, String namespace, String unitName) { |
| 181 | + if (unit != null && !CollectionUtils.isEmpty(unit.getNamingInstanceList())) { |
| 182 | + Node node = unit.getNamingInstanceList().get(0); |
| 183 | + String httpUrl = NamingServerConstants.HTTP_PREFIX + node.getControl().getHost() |
| 184 | + + NamingServerConstants.IP_PORT_SPLIT_CHAR + node.getControl().getPort() |
| 185 | + + NamingServerConstants.HTTP_REMOVE_GROUP_SUFFIX; |
| 186 | + HashMap<String, String> params = new HashMap<>(); |
| 187 | + params.put(CONSTANT_GROUP, vGroup); |
| 188 | + params.put(NamingServerConstants.CONSTANT_UNIT, unitName); |
| 189 | + Map<String, String> header = new HashMap<>(); |
| 190 | + header.put(HTTP.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); |
| 191 | + try (CloseableHttpResponse closeableHttpResponse = HttpClientUtil.doGet(httpUrl, params, header, 3000)) { |
| 192 | + if (closeableHttpResponse == null || closeableHttpResponse.getStatusLine().getStatusCode() != 200) { |
| 193 | + LOGGER.warn("remove vGroup in old cluster failed"); |
| 194 | + return new Result<>(String.valueOf(closeableHttpResponse.getStatusLine().getStatusCode()), |
| 195 | + "removing vGroup " + vGroup + " in old cluster " + clusterName + " failed"); |
210 | 196 | }
|
| 197 | + LOGGER.info("namespace: {} remove vGroup: {} in new cluster: {} successfully!", namespace, vGroup, |
| 198 | + clusterName); |
| 199 | + } catch (IOException e) { |
| 200 | + LOGGER.warn("handle removing vGroup in old cluster failed"); |
| 201 | + return new Result<>("500", |
| 202 | + "handle removing vGroup " + vGroup + " in old cluster " + clusterName + " failed"); |
211 | 203 | }
|
212 | 204 | }
|
213 | 205 | return new Result<>("200", "remove group in old cluster successfully!");
|
@@ -248,20 +240,21 @@ public boolean registerInstance(NamingServerNode node, String namespace, String
|
248 | 240 | ClusterData clusterData = clusterDataHashMap.computeIfAbsent(clusterName,
|
249 | 241 | key -> new ClusterData(clusterName, (String)node.getMetadata().get("cluster-type")));
|
250 | 242 | boolean hasChanged = clusterData.registerInstance(node, unitName);
|
| 243 | + Object mappingObj = node.getMetadata().get(CONSTANT_GROUP); |
251 | 244 | // if extended metadata includes vgroup mapping relationship, add it in clusterData
|
252 |
| - Optional.ofNullable(node.getMetadata().get(CONSTANT_GROUP)).ifPresent(mappingObj -> { |
253 |
| - if (mappingObj instanceof Map) { |
254 |
| - Map<String, String> vGroups = (Map<String, String>) mappingObj; |
| 245 | + if (mappingObj instanceof Map) { |
| 246 | + Map<String, String> vGroups = (Map<String, String>)mappingObj; |
| 247 | + if (!CollectionUtils.isEmpty(vGroups)) { |
255 | 248 | vGroups.forEach((k, v) -> {
|
256 | 249 | // In non-raft mode, a unit is one-to-one with a node, and the unitName is stored on the node.
|
257 | 250 | // In raft mode, the unitName is equal to the raft-group, so the node's unitName cannot be used.
|
258 | 251 | addGroup(namespace, clusterName, StringUtils.isBlank(v) ? unitName : v, k);
|
259 | 252 | if (hasChanged) {
|
260 |
| - notifyClusterChange(k,namespace, clusterName, unitName,node.getTerm()); |
| 253 | + notifyClusterChange(k, namespace, clusterName, unitName, node.getTerm()); |
261 | 254 | }
|
262 | 255 | });
|
263 | 256 | }
|
264 |
| - }); |
| 257 | + } |
265 | 258 | instanceLiveTable.put(
|
266 | 259 | new InetSocketAddress(node.getTransaction().getHost(), node.getTransaction().getPort()),
|
267 | 260 | System.currentTimeMillis());
|
@@ -367,24 +360,26 @@ public void instanceHeartBeatCheck() {
|
367 | 360 | }
|
368 | 361 |
|
369 | 362 | public Result<String> changeGroup(String namespace, String vGroup, String clusterName, String unitName) {
|
370 |
| - ConcurrentMap<String, NamespaceBO> namespaceMap = |
371 |
| - new ConcurrentHashMap<>(vGroupMap.get(vGroup)); |
| 363 | + ConcurrentMap<String, NamespaceBO> namespaceMap = new ConcurrentHashMap<>(vGroupMap.get(vGroup)); |
| 364 | + Set<String> currentNamespaces = namespaceMap.keySet(); |
| 365 | + Map<String, Set<String>> namespaceClusters = new HashMap<>(); |
| 366 | + for (String currentNamespace : currentNamespaces) { |
| 367 | + namespaceClusters.put(currentNamespace, |
| 368 | + new HashSet<>(namespaceMap.get(currentNamespace).getClusterMap().keySet())); |
| 369 | + } |
372 | 370 | createGroup(namespace, vGroup, clusterName, unitName);
|
373 | 371 | AtomicReference<Result<String>> result = new AtomicReference<>();
|
374 |
| - namespaceMap.forEach((currentNamespace, namespaceBO) -> namespaceBO.getClusterMap().forEach((currentCluster, clusterBO) -> { |
375 |
| - for (String currentUnitName : clusterBO.getUnitNames()) { |
376 |
| - if (StringUtils.isBlank(unitName)) { |
377 |
| - if (StringUtils.equalsIgnoreCase(clusterName, currentCluster)) { |
378 |
| - continue; |
379 |
| - } |
380 |
| - result.set(removeGroup(currentNamespace, currentCluster, vGroup, unitName)); |
381 |
| - } else { |
382 |
| - if (!StringUtils.equalsIgnoreCase(unitName, currentUnitName)) { |
383 |
| - result.set(removeGroup(currentNamespace, currentCluster, vGroup, unitName)); |
384 |
| - } |
385 |
| - } |
| 372 | + namespaceClusters.forEach((oldNamespace, clusters) -> { |
| 373 | + for (String cluster : clusters) { |
| 374 | + Optional.ofNullable(namespaceClusterDataMap.get(oldNamespace)) |
| 375 | + .flatMap(map -> Optional.ofNullable(map.get(cluster))).ifPresent(clusterData -> { |
| 376 | + if (!CollectionUtils.isEmpty(clusterData.getUnitData())) { |
| 377 | + clusterData.getUnitData().forEach((unit, unitData) -> result |
| 378 | + .set(removeGroup(unitData, vGroup, cluster, oldNamespace, unitName))); |
| 379 | + } |
| 380 | + }); |
386 | 381 | }
|
387 |
| - })); |
| 382 | + }); |
388 | 383 | return Optional.ofNullable(result.get()).orElseGet(() -> new Result<>("200", "change vGroup successfully!"));
|
389 | 384 | }
|
390 | 385 |
|
|
0 commit comments