xyuanlu commented on a change in pull request #1043:
URL: https://github.com/apache/helix/pull/1043#discussion_r438505561
##########
File path:
helix-core/src/main/java/org/apache/helix/controller/rebalancer/topology/Topology.java
##########
@@ -81,62 +75,43 @@ public Topology(final List<String> allNodes, final
List<String> liveNodes,
throw new HelixException(String.format("Config for instances %s is not
found!",
_allInstances.removeAll(_instanceConfigMap.keySet())));
}
-
_clusterConfig = clusterConfig;
- _types = new LinkedHashSet<>();
- _topologyAwareEnabled = clusterConfig.isTopologyAwareEnabled();
- if (_topologyAwareEnabled) {
+ if (clusterConfig.isTopologyAwareEnabled()) {
String topologyDef = _clusterConfig.getTopology();
if (topologyDef != null) {
// Customized cluster topology definition is configured.
- String[] types = topologyDef.trim().split("/");
- for (int i = 0; i < types.length; i++) {
- if (types[i].length() != 0) {
- _types.add(types[i]);
+ String[] topologyStr = topologyDef.trim().split("/");
+ int lastValidTypeIdx = topologyStr.length - 1;
+ while(lastValidTypeIdx >= 0) {
+ if (topologyStr[lastValidTypeIdx].length() != 0) {
+ break;
}
+ --lastValidTypeIdx;
}
- if (_types.size() == 0) {
- logger.error("Invalid cluster topology definition " + topologyDef);
+ if (lastValidTypeIdx < 0) {
throw new HelixException("Invalid cluster topology definition " +
topologyDef);
- } else {
- String lastType = null;
- for (String type : _types) {
- _defaultDomainPathValues.put(type, "Helix_default_" + type);
- lastType = type;
- }
- _endNodeType = lastType;
- _faultZoneType = clusterConfig.getFaultZoneType();
- if (_faultZoneType == null) {
- _faultZoneType = _endNodeType;
- }
- if (!_types.contains(_faultZoneType)) {
- throw new HelixException(String
- .format("Invalid fault zone type %s, not present in topology
definition %s.",
- _faultZoneType, topologyDef));
- }
- _useDefaultTopologyDef = false;
+ }
+ _endNodeType = topologyStr[lastValidTypeIdx];
+ _faultZoneType = clusterConfig.getFaultZoneType();
+ if (_faultZoneType == null) {
+ _faultZoneType = _endNodeType;
+ }
+ if (Arrays.stream(topologyStr).noneMatch(type ->
type.equals(_faultZoneType))) {
Review comment:
Please correct me if I am wrong.
Comparing to previous approach (set. contains()), the stream.noneMatch()
does introduced a o(n)worst case complexity.
However, since there is no new LinkedHashSet created, it saved a O(n) time
for constructing the LinkedHashSet and a O(n) space complexity for the object
itself.
It is better to pass the already parsed array to computeInstanceTopologyMap
so we do not compute again in line 274. However, I think we could just use the
original array topologyStr, no need to introduce another LinkedHashSet.
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]