wardlican commented on code in PR #3922: URL: https://github.com/apache/amoro/pull/3922#discussion_r2929912898
########## amoro-ams/src/main/java/org/apache/amoro/server/AmsAssignService.java: ########## @@ -0,0 +1,515 @@ +/* + * 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.amoro.server; + +import org.apache.amoro.client.AmsServerInfo; +import org.apache.amoro.config.Configurations; +import org.apache.amoro.exception.BucketAssignStoreException; +import org.apache.amoro.server.ha.HighAvailabilityContainer; +import org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting; +import org.apache.amoro.shade.guava32.com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Service for assigning bucket IDs to AMS nodes in master-slave mode. Periodically detects node + * changes and redistributes bucket IDs evenly. + */ +public class AmsAssignService { + + private static final Logger LOG = LoggerFactory.getLogger(AmsAssignService.class); + + private final ScheduledExecutorService assignScheduler = + Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder() + .setNameFormat("ams-assign-scheduler-%d") + .setDaemon(true) + .build()); + + private final HighAvailabilityContainer haContainer; + private final BucketAssignStore assignStore; + private final Configurations serviceConfig; + private final int bucketIdTotalCount; + private final long nodeOfflineTimeoutMs; + private final long assignIntervalSeconds; + private volatile boolean running = false; + + boolean isRunning() { + return running; + } + + public AmsAssignService(HighAvailabilityContainer haContainer, Configurations serviceConfig) { + this.haContainer = haContainer; + this.serviceConfig = serviceConfig; + this.bucketIdTotalCount = + serviceConfig.getInteger(AmoroManagementConf.HA_BUCKET_ID_TOTAL_COUNT); + this.nodeOfflineTimeoutMs = + serviceConfig.get(AmoroManagementConf.HA_NODE_OFFLINE_TIMEOUT).toMillis(); + this.assignIntervalSeconds = + serviceConfig.get(AmoroManagementConf.HA_ASSIGN_INTERVAL).getSeconds(); + this.assignStore = BucketAssignStoreFactory.create(haContainer, serviceConfig); + } + + /** + * Start the assignment service. Only works in master-slave mode and when current node is leader. + */ + public void start() { + if (!serviceConfig.getBoolean(AmoroManagementConf.USE_MASTER_SLAVE_MODE)) { + LOG.info("Master-slave mode is not enabled, skip starting bucket assignment service"); + return; + } + if (running) { + LOG.warn("Bucket assignment service is already running"); + return; + } + running = true; + assignScheduler.scheduleWithFixedDelay( + this::doAssign, 10, assignIntervalSeconds, TimeUnit.SECONDS); + LOG.info("Bucket assignment service started with interval: {} seconds", assignIntervalSeconds); + } + + /** Stop the assignment service. */ + public void stop() { + if (!running) { + return; + } + running = false; + assignScheduler.shutdown(); + try { + if (!assignScheduler.awaitTermination(5, TimeUnit.SECONDS)) { + assignScheduler.shutdownNow(); + } + } catch (InterruptedException e) { + assignScheduler.shutdownNow(); + Thread.currentThread().interrupt(); + } + LOG.info("Bucket assignment service stopped"); + } + + @VisibleForTesting + public void doAssign() { + try { + if (!haContainer.hasLeadership()) { + LOG.debug("Current node is not leader, skip bucket assignment"); + return; + } + + List<AmsServerInfo> aliveNodes = haContainer.getAliveNodes(); + if (aliveNodes.isEmpty()) { + LOG.debug("No alive nodes found, skip bucket assignment"); + return; + } + + Map<AmsServerInfo, List<String>> currentAssignments = assignStore.getAllAssignments(); + Map<String, AmsServerInfo> aliveNodeMap = buildAliveNodeMap(aliveNodes); + NormalizedAssignments normalized = + normalizeCurrentAssignments(currentAssignments, aliveNodeMap); + NodeChangeResult change = + detectNodeChanges(aliveNodes, currentAssignments, aliveNodeMap, normalized.assignedNodes); + + if (!change.needReassign()) { + refreshLastUpdateTime(aliveNodes); + return; + } + + LOG.info( + "Detected node changes - New nodes: {}, Offline nodes: {}, Performing incremental reassignment...", + change.newNodes.size(), + change.offlineNodes.size()); + + List<String> bucketsToRedistribute = + handleOfflineNodes(change.offlineNodes, currentAssignments); + List<String> allBuckets = generateBucketIds(); + Map<AmsServerInfo, List<String>> newAssignments = + buildNewAssignments(aliveNodes, change.offlineNodes, normalized.assignments); + rebalance(aliveNodes, change.newNodes, bucketsToRedistribute, allBuckets, newAssignments); + persistAssignments(newAssignments); + } catch (Exception e) { + LOG.error("Error during bucket assignment", e); + } + } + + /** + * Redistribute buckets incrementally to alive nodes using round-robin. This minimizes bucket + * migration by only redistributing buckets from offline nodes. + * + * @param aliveNodes List of alive nodes + * @param bucketsToRedistribute Buckets to redistribute (from offline nodes) + * @param currentAssignments Current assignments map (will be modified) + */ + private void redistributeBucketsIncrementally( + List<AmsServerInfo> aliveNodes, + List<String> bucketsToRedistribute, + Map<AmsServerInfo, List<String>> currentAssignments) { + if (aliveNodes.isEmpty() || bucketsToRedistribute.isEmpty()) { + return; + } + + // Distribute buckets using round-robin to minimize migration + int nodeIndex = 0; + for (String bucketId : bucketsToRedistribute) { + AmsServerInfo node = aliveNodes.get(nodeIndex % aliveNodes.size()); + currentAssignments.get(node).add(bucketId); + nodeIndex++; + } + } + + /** + * Balance buckets for new nodes by taking buckets from existing nodes. This minimizes migration + * by only moving necessary buckets to new nodes. + * + * @param aliveNodes All alive nodes + * @param newNodes Newly added nodes + * @param currentAssignments Current assignments map (will be modified) + * @param targetBucketsPerNode Target number of buckets per node + * @param remainder Remainder when dividing total buckets by node count + */ + private void balanceBucketsForNewNodes( + List<AmsServerInfo> aliveNodes, + Set<AmsServerInfo> newNodes, + Map<AmsServerInfo, List<String>> currentAssignments, + int targetBucketsPerNode, + int remainder) { + if (newNodes.isEmpty()) { + return; + } + + // Calculate how many buckets each new node should get + int bucketsPerNewNode = targetBucketsPerNode; + int newNodeIndex = 0; + for (AmsServerInfo newNode : newNodes) { + // First 'remainder' nodes get one extra bucket + int targetForNewNode = bucketsPerNewNode + (newNodeIndex < remainder ? 1 : 0); + int currentCount = currentAssignments.get(newNode).size(); + int needed = targetForNewNode - currentCount; + + if (needed > 0) { + // Collect buckets from existing nodes (prefer nodes with more buckets) + List<String> bucketsToMove = + collectBucketsFromExistingNodes(aliveNodes, newNodes, currentAssignments, needed); + currentAssignments.get(newNode).addAll(bucketsToMove); + LOG.info( + "Moved {} buckets to new node {} (target: {})", + bucketsToMove.size(), + newNode, + targetForNewNode); + } + newNodeIndex++; + } + } + + /** + * Collect buckets from existing nodes to balance for new nodes. Prefer taking from nodes that + * have more buckets than target. + * + * @param aliveNodes All alive nodes + * @param newNodes New nodes (excluded from source) + * @param currentAssignments Current assignments + * @param needed Number of buckets needed + * @return List of bucket IDs to move + */ + private List<String> collectBucketsFromExistingNodes( + List<AmsServerInfo> aliveNodes, + Set<AmsServerInfo> newNodes, + Map<AmsServerInfo, List<String>> currentAssignments, + int needed) { + List<String> bucketsToMove = new ArrayList<>(); + List<AmsServerInfo> existingNodes = new ArrayList<>(); + for (AmsServerInfo node : aliveNodes) { + if (!newNodes.contains(node)) { + existingNodes.add(node); + } + } + + if (existingNodes.isEmpty()) { + return bucketsToMove; + } + + // Sort existing nodes by current bucket count (descending) + // This ensures we take from nodes with more buckets first + existingNodes.sort( + (n1, n2) -> { + int count1 = currentAssignments.get(n1).size(); + int count2 = currentAssignments.get(n2).size(); + return Integer.compare(count2, count1); + }); + + // Collect buckets from existing nodes using round-robin + int nodeIndex = 0; + int collected = 0; + while (collected < needed && !existingNodes.isEmpty()) { + AmsServerInfo sourceNode = existingNodes.get(nodeIndex % existingNodes.size()); + List<String> sourceBuckets = currentAssignments.get(sourceNode); + if (!sourceBuckets.isEmpty()) { + // Take one bucket from this node + String bucketToMove = sourceBuckets.remove(0); + bucketsToMove.add(bucketToMove); + collected++; + LOG.debug("Moving bucket {} from node {} to new node", bucketToMove, sourceNode); + } else { + // This node has no more buckets, remove it from consideration + existingNodes.remove(sourceNode); + if (existingNodes.isEmpty()) { + break; + } + nodeIndex = nodeIndex % existingNodes.size(); + continue; + } + nodeIndex++; + } + + return bucketsToMove; + } + + private List<String> generateBucketIds() { + List<String> bucketIds = new ArrayList<>(); + for (int i = 1; i <= bucketIdTotalCount; i++) { + bucketIds.add(String.valueOf(i)); + } + return bucketIds; + } + + /** + * Get node key for matching nodes. Uses host:thriftBindPort format, consistent with + * ZkBucketAssignStore.getNodeKey(). + */ + private String getNodeKey(AmsServerInfo nodeInfo) { + return nodeInfo.getHost() + ":" + nodeInfo.getThriftBindPort(); Review Comment: There won't be this problem. This is mainly used to distinguish different AMS nodes. The IP address and Thriftport are already sufficient to uniquely identify different nodes, so there's no need to add a restport. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
