saurabhd336 commented on code in PR #10359: URL: https://github.com/apache/pinot/pull/10359#discussion_r1130914572
########## pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/ZkBasedTableRebalanceObserver.java: ########## @@ -0,0 +1,168 @@ +/** + * 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.pinot.controller.helix.core.rebalance; + +import com.google.common.base.Preconditions; +import java.util.Map; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; +import org.apache.pinot.spi.utils.CommonConstants; +/** + * <code>ZkBasedTableRebalanceObserver</code> observes rebalance progress and tracks rebalance status, + * stats in Zookeeper. This will be used to show the progress of rebalance to users via rebalanceStatus API. + */ +public class ZkBasedTableRebalanceObserver implements TableRebalanceObserver { + private final String _tableNameWithType; + private final String _rebalanceJobId; + private final ZkHelixPropertyStore<ZNRecord> _propertyStore; + TableRebalanceProgressStats _tableRebalanceProgressStats; + + public ZkBasedTableRebalanceObserver(String tableNameWithType, + String rebalanceJobId, + ZkHelixPropertyStore<ZNRecord> propertyStore) { + Preconditions.checkState(tableNameWithType != null, "Table name cannot be null"); + Preconditions.checkState(rebalanceJobId != null, "rebalanceId cannot be null"); + Preconditions.checkState(propertyStore != null, "propertyStore cannot be null"); + _tableNameWithType = tableNameWithType; + _rebalanceJobId = rebalanceJobId; + _propertyStore = propertyStore; + _tableRebalanceProgressStats = new TableRebalanceProgressStats(); + } + + @Override + public void onTrigger(Trigger trigger, Map<String, Map<String, String>> currentState, + Map<String, Map<String, String>> targetState) { + switch (trigger) { + case START_TRIGGER: + updateOnStart(currentState, targetState); + break; + case IDEAL_STATE_CHANGE_TRIGGER: + _tableRebalanceProgressStats.setCurrentToTargetConvergence(getDifferenceBetweenTableRebalanceStates( + targetState, currentState)); + break; + case EXTERNAL_VIEW_TO_IDEAL_STATE_CONVERGENCE_TRIGGER: + _tableRebalanceProgressStats.setExternalViewToIdealStateConvergence( + getDifferenceBetweenTableRebalanceStates(targetState, currentState)); + break; + default: + throw new IllegalArgumentException("Unimplemented trigger: " + trigger); + } + PinotHelixResourceManager.addRebalanceResultToZk(_tableNameWithType, _rebalanceJobId, Review Comment: Writing updated stats to ZK on each "EXTERNAL_VIEW_TO_IDEAL_STATE_CONVERGENCE_TRIGGER" might be too expensive? The lifecycle of a ZkBasedTableRebalanceObserver instance is as long as the rebalance operation is in progress right? Could we persist state to ZK only onSuccess and onError? And everything in between, can be answered by the in memory _tableRebalanceProgressStats that is part of the observer class? I.e. At the start of rebalance we maintain an inmemory map of rebalanceJobId -> observer and the API leverages that to return the job status? Once either success / error, we can persist the info into ZK? At the very least, we should avoid writing to ZK if the externalViewToIdealStateConvergence has not changed at all. ########## pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/ZkBasedTableRebalanceObserver.java: ########## @@ -0,0 +1,168 @@ +/** + * 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.pinot.controller.helix.core.rebalance; + +import com.google.common.base.Preconditions; +import java.util.Map; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; +import org.apache.pinot.spi.utils.CommonConstants; +/** + * <code>ZkBasedTableRebalanceObserver</code> observes rebalance progress and tracks rebalance status, + * stats in Zookeeper. This will be used to show the progress of rebalance to users via rebalanceStatus API. + */ +public class ZkBasedTableRebalanceObserver implements TableRebalanceObserver { + private final String _tableNameWithType; + private final String _rebalanceJobId; + private final ZkHelixPropertyStore<ZNRecord> _propertyStore; + TableRebalanceProgressStats _tableRebalanceProgressStats; + + public ZkBasedTableRebalanceObserver(String tableNameWithType, + String rebalanceJobId, + ZkHelixPropertyStore<ZNRecord> propertyStore) { + Preconditions.checkState(tableNameWithType != null, "Table name cannot be null"); + Preconditions.checkState(rebalanceJobId != null, "rebalanceId cannot be null"); + Preconditions.checkState(propertyStore != null, "propertyStore cannot be null"); + _tableNameWithType = tableNameWithType; + _rebalanceJobId = rebalanceJobId; + _propertyStore = propertyStore; + _tableRebalanceProgressStats = new TableRebalanceProgressStats(); + } + + @Override + public void onTrigger(Trigger trigger, Map<String, Map<String, String>> currentState, + Map<String, Map<String, String>> targetState) { + switch (trigger) { + case START_TRIGGER: + updateOnStart(currentState, targetState); + break; + case IDEAL_STATE_CHANGE_TRIGGER: + _tableRebalanceProgressStats.setCurrentToTargetConvergence(getDifferenceBetweenTableRebalanceStates( + targetState, currentState)); + break; + case EXTERNAL_VIEW_TO_IDEAL_STATE_CONVERGENCE_TRIGGER: + _tableRebalanceProgressStats.setExternalViewToIdealStateConvergence( + getDifferenceBetweenTableRebalanceStates(targetState, currentState)); + break; + default: + throw new IllegalArgumentException("Unimplemented trigger: " + trigger); + } + PinotHelixResourceManager.addRebalanceResultToZk(_tableNameWithType, _rebalanceJobId, Review Comment: cc: @Jackie-Jiang ? -- 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. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org