kaisun2000 commented on a change in pull request #1532: URL: https://github.com/apache/helix/pull/1532#discussion_r555418251
########## File path: helix-core/src/main/java/org/apache/helix/controller/stages/PerReplicaThrottleStage.java ########## @@ -0,0 +1,982 @@ +package org.apache.helix.controller.stages; + +/* + * 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.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.helix.HelixDefinedState; +import org.apache.helix.HelixException; +import org.apache.helix.HelixManager; +import org.apache.helix.api.config.StateTransitionThrottleConfig; +import org.apache.helix.controller.LogUtil; +import org.apache.helix.controller.common.PartitionStateMap; +import org.apache.helix.controller.common.ResourcesStateMap; +import org.apache.helix.controller.dataproviders.ResourceControllerDataProvider; +import org.apache.helix.controller.pipeline.AbstractBaseStage; +import org.apache.helix.controller.pipeline.StageException; +import org.apache.helix.model.BuiltInStateModelDefinitions; +import org.apache.helix.model.ClusterConfig; +import org.apache.helix.model.IdealState; +import org.apache.helix.model.MaintenanceSignal; +import org.apache.helix.model.Message; +import org.apache.helix.model.Partition; +import org.apache.helix.model.Resource; +import org.apache.helix.model.StateModelDefinition; +import org.apache.helix.monitoring.mbeans.ClusterStatusMonitor; +import org.apache.helix.monitoring.mbeans.ResourceMonitor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class PerReplicaThrottleStage extends AbstractBaseStage { + private static final Logger logger = + LoggerFactory.getLogger(PerReplicaThrottleStage.class.getName()); + + private boolean isEmitThrottledMsg = false; + + public PerReplicaThrottleStage() { + this(false); + } + + protected PerReplicaThrottleStage(boolean enableEmitThrottledMsg) { + isEmitThrottledMsg = enableEmitThrottledMsg; + } + + @Override + public void process(ClusterEvent event) throws Exception { + _eventId = event.getEventId(); + + CurrentStateOutput currentStateOutput = event.getAttribute(AttributeName.CURRENT_STATE.name()); + + MessageOutput selectedMessages = event.getAttribute(AttributeName.MESSAGES_SELECTED.name()); + LogUtil.logDebug(logger, _eventId, String.format("selectedMessages is: %s", selectedMessages)); + + Map<String, Resource> resourceToRebalance = + event.getAttribute(AttributeName.RESOURCES_TO_REBALANCE.name()); + ResourceControllerDataProvider cache = + event.getAttribute(AttributeName.ControllerDataProvider.name()); + + if (currentStateOutput == null || selectedMessages == null || resourceToRebalance == null + || cache == null) { + throw new StageException(String.format("Missing attributes in event: %s. " + + "Requires CURRENT_STATE (%s) |BEST_POSSIBLE_STATE (%s) |RESOURCES (%s) |DataCache (%s)", + event, currentStateOutput, selectedMessages, resourceToRebalance, cache)); + } + + ResourcesStateMap retracedResourceStateMap = new ResourcesStateMap(); + List<Message> throttledRecoveryMsg = new ArrayList<>(); + List<Message> throttledLoadMsg = new ArrayList<>(); + MessageOutput output = + compute(event, resourceToRebalance, currentStateOutput, selectedMessages, retracedResourceStateMap, throttledRecoveryMsg, throttledLoadMsg); + + if (logger.isDebugEnabled()) { + LogUtil.logDebug(logger, _eventId, String.format("output is")); + for (String resource : resourceToRebalance.keySet()) { + if (output.getResourceMessages(resource) != null) { + LogUtil.logDebug(logger, _eventId, String.format("resource: %s", resource)); + Map<Partition, List<Message>> partitionListMap = output.getResourceMessages(resource); + for (Partition partition : partitionListMap.keySet()) { + for (Message msg : partitionListMap.get(partition)) { + LogUtil.logDebug(logger, _eventId, String + .format("\tresource: %s, partition: %s, msg: %s", resource, partition, msg)); + } + } + } + } + } + event.addAttribute(AttributeName.PER_REPLICA_OUTPUT_MESSAGES.name(), output); + LogUtil.logDebug(logger,_eventId, String.format("retraceResourceStateMap is: %s", retracedResourceStateMap)); + event.addAttribute(AttributeName.PER_REPLICA_RETRACED_STATES.name(), retracedResourceStateMap); + + if (isEmitThrottledMsg) { + event.addAttribute(AttributeName.PER_REPLICA_THROTTLED_RECOVERY_MESSAGES.name(), throttledRecoveryMsg); + event.addAttribute(AttributeName.PER_REPLICA_THROTTLED_LOAD_MESSAGES.name(), throttledLoadMsg); + } + + // Make sure no instance has more replicas/partitions assigned than maxPartitionPerInstance. If + // it does, pause the rebalance and put the cluster on maintenance mode + int maxPartitionPerInstance = cache.getClusterConfig().getMaxPartitionsPerInstance(); + if (maxPartitionPerInstance > 0) { + validateMaxPartitionsPerInstance(retracedResourceStateMap, maxPartitionPerInstance, cache, event); + } + } + + /** + * Go through every instance in the assignment and check that each instance does NOT have more + * replicas for partitions assigned to it than maxPartitionsPerInstance. If the assignment + * violates this, put the cluster on maintenance mode. + * @param retracedResourceStateMap + * @param maxPartitionPerInstance + */ + private void validateMaxPartitionsPerInstance(ResourcesStateMap retracedResourceStateMap, Review comment: In both places, this function are the last one in the file. ---------------------------------------------------------------- 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]
