Source:
<code>public int cleanUpOrphanConnectorTaskLocks(boolean cleanUpOrphanTaskLocksInConnector) { <START> if (!_isLeader || !_orphanLockCleanupFuture.isDone()) { <END> return 0; } Set<DatastreamTask> validTasks = getDatastreamTasks(); List<String> allConnectors = getAllConnectors(); int orphanCount = 0; for (String connector : allConnectors) { Map<String, Set<String>> locksByTaskPrefix = getAllConnectorTaskLocks(connector); Set<String> validTaskNamesSet = validTasks.stream() .filter(x -> x.getConnectorType().equals(connector)) .map(DatastreamTask::getDatastreamTaskName) .collect(Collectors.toSet()); List<String> orphanLockList = new ArrayList<>(); locksByTaskPrefix.forEach((taskPrefix, taskList) -> { AtomicBoolean deleteTaskPrefixNode = new AtomicBoolean(true); taskList.forEach(taskName -> { if (!validTaskNamesSet.contains(taskName)) { orphanLockList.add(KeyBuilder.datastreamTaskLock(_cluster, connector, taskPrefix, taskName)); } else { deleteTaskPrefixNode.set(false); } }); if (deleteTaskPrefixNode.get()) { orphanLockList.add(KeyBuilder.datastreamTaskLockPrefix(_cluster, connector, taskPrefix)); } }); if (orphanLockList.size() > 0) { LOG.warn("Found orphan task locks: in connector: {}", orphanLockList, connector); if (cleanUpOrphanTaskLocksInConnector) { _finalOrphanLockList.addAll(orphanLockList); } orphanCount += orphanLockList.size(); } } if (cleanUpOrphanTaskLocksInConnector) { _orphanLockCleanupFuture = _scheduledExecutorServiceOrphanLockCleanup.schedule(this::cleanUpOrphanLocks, _debounceTimerMs, TimeUnit.MILLISECONDS); } return orphanCount; }</code><technical_language>curious, I noticed spawn a separate thread handle orphan lock cleanup, do for cleaning orphan tasks. reason for that? done separate threads? Also, add a log indicating this is skipped</technical_language>

Target:
public int cleanUpOrphanConnectorTaskLocks(boolean cleanUpOrphanTaskLocksInConnector) { if (!_isLeader || !_orphanLockCleanupFuture.isDone()) { LOG.info("Skipping cleanUpOrphanConnectorTaskLocks. isLeader: lock cleanup scheduled: {}", _isLeader, _orphanLockCleanupFuture.isDone()); return 0; } LOG.info("cleanUpOrphanConnectorTaskLocks called. clean up: {}", cleanUpOrphanTaskLocksInConnector); Map<String, Set<String>> validTaskNamesConnectorMap = getDatastreamTaskNamesGroupedByConnector(); List<String> allConnectors = getAllConnectors(); int orphanCount = 0; for (String connector : allConnectors) { Map<String, Set<String>> locksByTaskPrefix = getAllConnectorTaskLocks(connector); Set<String> validTaskNamesSet = validTaskNamesConnectorMap.getOrDefault(connector, Collections.emptySet()); List<String> orphanLockList = new ArrayList<>(); locksByTaskPrefix.forEach((taskPrefix, taskList) -> { AtomicBoolean deleteTaskPrefixNode = new AtomicBoolean(true); taskList.forEach(taskName -> { if (!validTaskNamesSet.contains(taskName)) { orphanLockList.add(KeyBuilder.datastreamTaskLock(_cluster, connector, taskPrefix, taskName)); } else { deleteTaskPrefixNode.set(false); } }); if (deleteTaskPrefixNode.get()) { orphanLockList.add(KeyBuilder.datastreamTaskLockPrefix(_cluster, connector, taskPrefix)); } }); if (orphanLockList.size() > 0) { LOG.warn("Found orphan task locks: in connector: {}", orphanLockList, connector); if (cleanUpOrphanTaskLocksInConnector) { _finalOrphanLockList.addAll(orphanLockList); } orphanCount += orphanLockList.size(); } } if (cleanUpOrphanTaskLocksInConnector && _finalOrphanLockList.size() > 0) { _orphanLockCleanupFuture = _scheduledExecutorServiceOrphanLockCleanup.schedule(this::cleanUpOrphanLocks, _debounceTimerMs, TimeUnit.MILLISECONDS); } return orphanCount; }