-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support zone based virtual topology assignment algorithm #2986
Open
MarkGaox
wants to merge
4
commits into
apache:master
Choose a base branch
from
MarkGaox:markgaox/support-zone-based-virtual-topology
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...n/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.apache.helix.cloud.topology; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import java.util.TreeSet; | ||
|
||
import static org.apache.helix.util.VirtualTopologyUtil.computeVirtualGroupId; | ||
|
||
/** | ||
* A strategy that densely assign virtual groups with input instance list, it doesn't move to the next one until | ||
* the current one is filled. | ||
* Given that instances.size = instancesPerGroup * numGroups + residuals, | ||
* we break [residuals] into the first few groups, as a result each virtual group will have | ||
* either [instancesPerGroup] or [instancesPerGroup + 1] instances. | ||
*/ | ||
public class FaultZoneBasedVirtualGroupAssignmentAlgorithm implements VirtualGroupAssignmentAlgorithm { | ||
|
||
private static final FaultZoneBasedVirtualGroupAssignmentAlgorithm _instance = new FaultZoneBasedVirtualGroupAssignmentAlgorithm(); | ||
|
||
private FaultZoneBasedVirtualGroupAssignmentAlgorithm() { } | ||
|
||
public static FaultZoneBasedVirtualGroupAssignmentAlgorithm getInstance() { | ||
return _instance; | ||
} | ||
|
||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. signature |
||
public Map<String, Set<String>> computeAssignment(int numGroups, String virtualGroupName, | ||
Map<String, Set<String>> zoneMapping) { | ||
TreeSet<String> sortdFaultZones = new TreeSet<>(zoneMapping.keySet()); | ||
int faultZonesPerGroup = sortdFaultZones.size() / numGroups; | ||
int residuals = sortdFaultZones.size() % numGroups; | ||
|
||
Map<String, Set<String>> assignment = new TreeMap<>(); | ||
int virtualGroupIndex = 0; | ||
int curFaultZoneNum = 0; | ||
|
||
for (String faultZone : sortdFaultZones) { | ||
String groupId = computeVirtualGroupId(virtualGroupIndex, virtualGroupName); | ||
if (curFaultZoneNum == 0) { | ||
assignment.put(groupId, new HashSet<>()); | ||
} | ||
assignment.get(groupId).addAll(zoneMapping.get(faultZone)); | ||
curFaultZoneNum++; | ||
if (curFaultZoneNum == faultZonesPerGroup + (virtualGroupIndex < residuals ? 1 : 0)) { | ||
virtualGroupIndex++; | ||
curFaultZoneNum = 0; | ||
} | ||
} | ||
|
||
return assignment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
helix-core/src/main/java/org/apache/helix/util/VirtualTopologyUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.apache.helix.util; | ||
|
||
import io.netty.util.internal.StringUtil; | ||
import org.apache.helix.cloud.constants.VirtualTopologyGroupConstants; | ||
|
||
public class VirtualTopologyUtil { | ||
public static String computeVirtualGroupId(int groupIndex, String virtualGroupName) { | ||
return virtualGroupName + VirtualTopologyGroupConstants.GROUP_NAME_SPLITTER + groupIndex; | ||
} | ||
|
||
/** | ||
* Ensures the provided fault zone type string ends with | ||
* the virtual fault zone type suffix. | ||
* | ||
* @param oldFaultZoneType The original fault zone type. Must not be null or empty. | ||
* @return The fault zone type string with the virtual fault zone type appended if necessary. | ||
* @throws IllegalArgumentException if {@code oldFaultZoneType} is null or empty | ||
*/ | ||
public static String computeVirtualFaultZoneTypeKey(String oldFaultZoneType) { | ||
if (StringUtil.isNullOrEmpty(oldFaultZoneType)) { | ||
throw new IllegalArgumentException("The old fault zone type is null or empty"); | ||
} | ||
|
||
String suffix = VirtualTopologyGroupConstants.GROUP_NAME_SPLITTER | ||
+ VirtualTopologyGroupConstants.VIRTUAL_FAULT_ZONE_TYPE; | ||
|
||
// If already ends with splitter + VIRTUAL_FAULT_ZONE_TYPE, return as-is | ||
if (oldFaultZoneType.endsWith(suffix)) { | ||
return oldFaultZoneType; | ||
} | ||
|
||
// Otherwise, remove any existing suffix parts beyond the first splitter, if needed | ||
String[] segments = oldFaultZoneType.split(VirtualTopologyGroupConstants.GROUP_NAME_SPLITTER); | ||
String baseName = segments[0]; | ||
|
||
return baseName + suffix; | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...org/apache/helix/cloud/virtualTopologyGroup/TestFaultZoneBasedVirtualGroupAssignment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.apache.helix.cloud.virtualTopologyGroup; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.helix.cloud.topology.FaultZoneBasedVirtualGroupAssignmentAlgorithm; | ||
import org.apache.helix.cloud.topology.VirtualGroupAssignmentAlgorithm; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.apache.helix.util.VirtualTopologyUtil.computeVirtualGroupId; | ||
|
||
public class TestFaultZoneBasedVirtualGroupAssignment { | ||
|
||
private static final String GROUP_NAME = "test_virtual_group"; | ||
private static final int ZONE_NUMBER = 20; | ||
private static final int INSTANCES_PER_ZONE = 5; | ||
private Map<String, Set<String>> _zoneMapping = new HashMap<>(); | ||
|
||
@BeforeTest | ||
public void prepare() { | ||
_zoneMapping = new HashMap<>(); | ||
int instanceIdx = 0; | ||
for (int i = 0; i < ZONE_NUMBER; i++) { | ||
String zone = "zone_" + i; | ||
_zoneMapping.computeIfAbsent(zone, k -> new HashSet<>()); | ||
for (int j = 0; j < INSTANCES_PER_ZONE; j++) { | ||
String instance = "instance_" + instanceIdx++; | ||
_zoneMapping.get(zone).add(instance); | ||
} | ||
} | ||
} | ||
|
||
@Test(dataProvider = "getMappingTests") | ||
public void testAssignmentScheme(int numGroups, Map<String, Set<String>> expected, | ||
VirtualGroupAssignmentAlgorithm algorithm) { | ||
Assert.assertEquals(algorithm.computeAssignment(numGroups, GROUP_NAME, _zoneMapping), expected); | ||
} | ||
|
||
@DataProvider | ||
public Object[][] getMappingTests() { | ||
VirtualGroupAssignmentAlgorithm algorithm = FaultZoneBasedVirtualGroupAssignmentAlgorithm.getInstance(); | ||
|
||
// Ordered zones: | ||
// zone_0, zone_1, zone_10, zone_11, zone_12, zone_13, zone_14, zone_15, zone_16, zone_17, | ||
// zone_18, zone_19, zone_2, zone_3, zone_4, zone_5, zone_6, zone_7, zone_8, zone_9 | ||
|
||
// Expected mapping with 4 virtual groups: | ||
// 0 -> zone_0, zone_1, zone_10, zone_11, zone_12 | ||
// 1 -> zone_13, zone_14, zone_15, zone_16, zone_17 | ||
// 2 -> zone_18, zone_19, zone_2, zone_3, zone_4 | ||
// 3 -> zone_5, zone_6, zone_7, zone_8, zone_9 | ||
Map<String, Set<String>> virtualMapping = new HashMap<>(); | ||
|
||
virtualMapping.put(computeVirtualGroupId(0, GROUP_NAME), new HashSet<>()); | ||
virtualMapping.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_0")); | ||
virtualMapping.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_1")); | ||
virtualMapping.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_10")); | ||
virtualMapping.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_11")); | ||
virtualMapping.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_12")); | ||
|
||
virtualMapping.put(computeVirtualGroupId(1, GROUP_NAME), new HashSet<>()); | ||
virtualMapping.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_13")); | ||
virtualMapping.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_14")); | ||
virtualMapping.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_15")); | ||
virtualMapping.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_16")); | ||
virtualMapping.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_17")); | ||
|
||
virtualMapping.put(computeVirtualGroupId(2, GROUP_NAME), new HashSet<>()); | ||
virtualMapping.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_18")); | ||
virtualMapping.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_19")); | ||
virtualMapping.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_2")); | ||
virtualMapping.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_3")); | ||
virtualMapping.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_4")); | ||
|
||
virtualMapping.put(computeVirtualGroupId(3, GROUP_NAME), new HashSet<>()); | ||
virtualMapping.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_5")); | ||
virtualMapping.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_6")); | ||
virtualMapping.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_7")); | ||
virtualMapping.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_8")); | ||
virtualMapping.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_9")); | ||
|
||
// Expected mapping with 7 virtual groups: | ||
// 0 -> zone_0, zone_1, zone_10 | ||
// 1 -> zone_11, zone_12, zone_13 | ||
// 2 -> zone_14, zone_15, zone_16 | ||
// 3 -> zone_17, zone_18, zone_19 | ||
// 4 -> zone_2, zone_3, zone_4 | ||
// 5 -> zone_5, zone_6, zone_7 | ||
// 6 -> zone_8, zone_9 | ||
Map<String, Set<String>> virtualMapping2 = new HashMap<>(); | ||
virtualMapping2.put(computeVirtualGroupId(0, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_0")); | ||
virtualMapping2.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_1")); | ||
virtualMapping2.get(computeVirtualGroupId(0, GROUP_NAME)).addAll(_zoneMapping.get("zone_10")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(1, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_11")); | ||
virtualMapping2.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_12")); | ||
virtualMapping2.get(computeVirtualGroupId(1, GROUP_NAME)).addAll(_zoneMapping.get("zone_13")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(2, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_14")); | ||
virtualMapping2.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_15")); | ||
virtualMapping2.get(computeVirtualGroupId(2, GROUP_NAME)).addAll(_zoneMapping.get("zone_16")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(3, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_17")); | ||
virtualMapping2.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_18")); | ||
virtualMapping2.get(computeVirtualGroupId(3, GROUP_NAME)).addAll(_zoneMapping.get("zone_19")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(4, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(4, GROUP_NAME)).addAll(_zoneMapping.get("zone_2")); | ||
virtualMapping2.get(computeVirtualGroupId(4, GROUP_NAME)).addAll(_zoneMapping.get("zone_3")); | ||
virtualMapping2.get(computeVirtualGroupId(4, GROUP_NAME)).addAll(_zoneMapping.get("zone_4")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(5, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(5, GROUP_NAME)).addAll(_zoneMapping.get("zone_5")); | ||
virtualMapping2.get(computeVirtualGroupId(5, GROUP_NAME)).addAll(_zoneMapping.get("zone_6")); | ||
virtualMapping2.get(computeVirtualGroupId(5, GROUP_NAME)).addAll(_zoneMapping.get("zone_7")); | ||
|
||
virtualMapping2.put(computeVirtualGroupId(6, GROUP_NAME), new HashSet<>()); | ||
virtualMapping2.get(computeVirtualGroupId(6, GROUP_NAME)).addAll(_zoneMapping.get("zone_8")); | ||
virtualMapping2.get(computeVirtualGroupId(6, GROUP_NAME)).addAll(_zoneMapping.get("zone_9")); | ||
|
||
return new Object[][]{{4, virtualMapping, algorithm}, {7, virtualMapping2, algorithm}}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe ASSIGNMENT_ALGORITHM_TYPE?