somandal commented on code in PR #16857: URL: https://github.com/apache/pinot/pull/16857#discussion_r2369968117
########## pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/DistributedTaskLockManager.java: ########## @@ -0,0 +1,563 @@ +/** + * 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.minion; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.annotation.Nullable; +import org.apache.helix.AccessOption; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.pinot.common.metadata.ZKMetadataProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Manages distributed locks for minion task generation using ZooKeeper ephemeral sequential nodes. + * Uses ephemeral nodes that automatically disappear when the controller session ends. + * This approach provides automatic cleanup and is suitable for long-running task generation. + * Locks are held until explicitly released or the controller session terminates. + * Locks are at the table level, to ensure that only one type of task can be generated per table at any given time. + */ +public class DistributedTaskLockManager { + private static final Logger LOGGER = LoggerFactory.getLogger(DistributedTaskLockManager.class); + + // Lock and state paths are constructed using ZKMetadataProvider + private static final String LOCK_SUFFIX = "-Lock"; + private static final String STATE_SUFFIX = "-State"; + private static final String LOCK_OWNER_KEY = "lockOwner"; + private static final String LOCK_PATH_KEY = "lockPath"; + private static final String LOCK_UUID_KEY = "lockUuid"; + private static final String LOCK_TIMESTAMP_MILLIS_KEY = "lockTimestampMillis"; + private static final String TASK_GENERATION_STATUS_KEY = "status"; + private static final String TASK_GENERATION_START_TIME_MILLIS_KEY = "startTimeMillis"; + private static final String TASK_GENERATION_COMPLETION_TIME_MILLIS_KEY = "completionTimeMillis"; + private static final long STALE_THRESHOLD_MILLIS = 24 * 60 * 60 * 1000L; // 24 hours; + + // Task generation states + private enum Status { + // IN_PROGRESS if the task generation is currently in progress; + // COMPLETED if the task generation completed successfully; + // FAILED if the task generation failed. + IN_PROGRESS, COMPLETED, FAILED + } + + // Define a custom comparator to compare strings of format '<controllerName>-lock-<sequenceNumber>' and sort them by + // the sequence number at the end + private static final Comparator<String> TASK_LOCK_SEQUENCE_ID_COMPARATOR = (s1, s2) -> { + // Regex to find the trailing sequence of digits + Pattern p = Pattern.compile("\\d+$"); + + // Extract the number from the first string + Matcher m1 = p.matcher(s1); + long num1 = m1.find() ? Long.parseLong(m1.group()) : 0; + + // Extract the number from the second string + Matcher m2 = p.matcher(s2); + long num2 = m2.find() ? Long.parseLong(m2.group()) : 0; + + return Long.compare(num1, num2); + }; + + private final ZkHelixPropertyStore<ZNRecord> _propertyStore; + private final String _controllerInstanceId; + + public DistributedTaskLockManager(ZkHelixPropertyStore<ZNRecord> propertyStore, String controllerInstanceId) { + _propertyStore = propertyStore; + _controllerInstanceId = controllerInstanceId; + + // Ensure base paths exist + ensureBasePaths(); + } + + /** + * Attempts to acquire a distributed lock for task generation using session-based locking. + * The lock is held until explicitly released or the controller session ends. + * The lock is created at the table level + * + * @param tableName the table name (can be null for all-table operations) + * @return TaskLock object if successful, null if lock could not be acquired + */ + @Nullable + public TaskLock acquireLock(@Nullable String tableName) { + String tableNameForPath = (tableName != null) ? tableName : "ALL_TABLES"; Review Comment: removed this ALL_TABLES part -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
