github-advanced-security[bot] commented on code in PR #17390: URL: https://github.com/apache/druid/pull/17390#discussion_r1810928390
########## indexing-service/src/main/java/org/apache/druid/indexing/overlord/GlobalTaskLockbox.java: ########## @@ -0,0 +1,401 @@ +/* + * 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.druid.indexing.overlord; + +import com.google.common.annotations.VisibleForTesting; +import com.google.inject.Inject; +import org.apache.druid.indexing.common.LockGranularity; +import org.apache.druid.indexing.common.TaskLock; +import org.apache.druid.indexing.common.actions.SegmentAllocateAction; +import org.apache.druid.indexing.common.actions.SegmentAllocateRequest; +import org.apache.druid.indexing.common.actions.SegmentAllocateResult; +import org.apache.druid.indexing.common.task.Task; +import org.apache.druid.java.util.common.Pair; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.metadata.LockFilterPolicy; +import org.apache.druid.metadata.ReplaceTaskLock; +import org.joda.time.DateTime; +import org.joda.time.Interval; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Optional; +import java.util.Set; +import java.util.SortedMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Remembers which activeTasks have locked which intervals or which segments. Tasks are permitted to lock an interval + * or a segment if no other task outside their group has locked an overlapping interval for the same datasource or + * the same segments. Note that TaskLockbox is also responsible for allocating segmentIds when a task requests to lock + * a new segment. Task lock might involve version assignment. + * + * - When a task locks an interval or a new segment, it is assigned a new version string that it can use to publish + * segments. + * - When a task locks a existing segment, it doesn't need to be assigned a new version. + * + * Note that tasks of higher priorities can revoke locks of tasks of lower priorities. + */ +public class GlobalTaskLockbox +{ + private static final Logger log = new Logger(GlobalTaskLockbox.class); + + private final TaskStorage taskStorage; + private final IndexerMetadataStorageCoordinator metadataStorageCoordinator; + private final ReentrantReadWriteLock globalLock = new ReentrantReadWriteLock(true); + private final ConcurrentHashMap<String, TaskLockbox> datasourceLocks = new ConcurrentHashMap<>(); + + @Inject + public GlobalTaskLockbox( + TaskStorage taskStorage, + IndexerMetadataStorageCoordinator metadataStorageCoordinator + ) + { + this.taskStorage = taskStorage; + this.metadataStorageCoordinator = metadataStorageCoordinator; + } + + private TaskLockbox getDatasourceLockbox(Task task) + { + return getDatasourceLockbox(task.getDataSource()); + } + + private TaskLockbox getDatasourceLockbox(String datasource) + { + return datasourceLocks.computeIfAbsent( + datasource, + ds -> new TaskLockbox(ds, new DatasourceLock(), taskStorage, metadataStorageCoordinator) + ); + } + + private class DatasourceLock extends ReentrantLock Review Comment: ## Serializable inner class of non-serializable class Serializable inner class of non-serializable class [GlobalTaskLockbox](1). Consider making the class static or implementing readObject() and writeObject(). [Show more details](https://github.com/apache/druid/security/code-scanning/8426) -- 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]
