gianm commented on code in PR #19772:
URL: https://github.com/apache/druid/pull/19772#discussion_r3726506364
##########
server/src/main/java/org/apache/druid/metadata/SqlSegmentsMetadataQuery.java:
##########
@@ -1116,6 +1118,70 @@ public List<Interval>
retrieveSomeUnusedSegmentIntervals(String dataSource, int
return
intervals.stream().filter(Objects::nonNull).collect(Collectors.toList());
}
+ /**
+ * Scans upto {@code maxSegmentsToScan} unused segments which are eligible
for
Review Comment:
up to (spelling)
Also, this seems to be a copy of the javadoc for
`IndexerMetadataStorageCoordinator#retrieveSomeUnusedSegmentIntervals`. Can one
reference the other rather than copying the text?
##########
docs/api-reference/data-management-api.md:
##########
@@ -35,15 +35,17 @@ For example, use `http://localhost:8888` for quickstart
deployments.
:::info
- Coordinator APIs for data management are now deprecated. Use new APIs served
by the Overlord instead.
-- Do not use these APIs while an indexing task or kill task is in progress for
the same datasource and interval.
+- The APIs to mark segments as used fail if an indexing task or kill task is
in progress for the same datasource and overlapping interval, to ensure that
there are no accidental data losses or data inconsistencies.
+- Do not use the APIs to mark segments as unused while an indexing task or
kill task is in progress for the same datasource and interval.
Review Comment:
Why the asymmetry? How are people supposed to know if an indexing or kill
task is in progress? (It isn't obvious what the interval is without looking at
the specs, which is tedious if a lot of tasks are running.)
##########
indexing-service/src/main/java/org/apache/druid/indexing/common/task/KillUnusedSegmentsTask.java:
##########
@@ -533,15 +532,20 @@ public boolean isReady(TaskActionClient taskActionClient)
throws Exception
return true;
}
- private TaskLockType determineLockType(boolean useConcurrentLocks)
+ public TaskLockType determineLockType()
{
- TaskLockType actualLockType;
+ final boolean useConcurrentLocks = Boolean.TRUE.equals(
+ getContextValue(
+ Tasks.USE_CONCURRENT_LOCKS,
+ Tasks.DEFAULT_USE_CONCURRENT_LOCKS
+ )
+ );
+
if (useConcurrentLocks) {
- actualLockType = TaskLockType.REPLACE;
+ return TaskLockType.REPLACE;
} else {
- actualLockType = getContextValue(Tasks.TASK_LOCK_TYPE,
TaskLockType.EXCLUSIVE);
+ return getContextValue(Tasks.TASK_LOCK_TYPE, TaskLockType.EXCLUSIVE);
Review Comment:
This should coerce too. As written I believe it will throw
`CastClassException` on real JSON, since it'll try to cast the `String` values
to `TaskLockType`.
##########
indexing-service/src/main/java/org/apache/druid/indexing/overlord/SegmentStatusManager.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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.inject.Inject;
+import org.apache.druid.common.utils.IdUtils;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.InternalServerError;
+import org.apache.druid.indexer.TaskStatus;
+import org.apache.druid.indexing.common.TaskLock;
+import org.apache.druid.indexing.common.TaskLockType;
+import org.apache.druid.indexing.common.TaskToolbox;
+import org.apache.druid.indexing.common.actions.TaskActionClient;
+import org.apache.druid.indexing.common.actions.TaskActionClientFactory;
+import org.apache.druid.indexing.common.actions.TimeChunkLockTryAcquireAction;
+import org.apache.druid.indexing.common.task.AbstractFixedIntervalTask;
+import org.apache.druid.indexing.common.task.Task;
+import org.apache.druid.indexing.common.task.TaskMetrics;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.JodaUtils;
+import org.apache.druid.java.util.common.Stopwatch;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.java.util.emitter.service.ServiceEmitter;
+import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
+import org.apache.druid.query.DruidMetrics;
+import org.apache.druid.timeline.SegmentId;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Performs updates on segments to change their "used" status in the metadata
store.
+ * <p>
+ * Currently, this class exposes methods that are used to acquire an EXCLUSIVE
+ * lock while marking segments in an interval as "used". A similar restriction
+ * may be imposed on marking (non-overshadowed) segments as unused in the
future.
+ *
+ * @see #markAsUsedWithExclusiveLock(String, Interval, UpdateOperation)
+ * Reasons for using EXCLUSIVE locks
+ */
+public class SegmentStatusManager
+{
+ private static final Logger log = new Logger(SegmentStatusManager.class);
+ private static final String TASK_TYPE_MARK_USED = "markSegmentAsUsed";
+
+ private final ServiceEmitter emitter;
+ private final GlobalTaskLockbox taskLockbox;
+ private final TaskActionClientFactory taskActionClientFactory;
+ private final IndexerMetadataStorageCoordinator storageCoordinator;
+
+ @Inject
+ public SegmentStatusManager(
+ GlobalTaskLockbox taskLockbox,
+ IndexerMetadataStorageCoordinator storageCoordinator,
+ TaskActionClientFactory taskActionClientFactory,
+ ServiceEmitter emitter
+ )
+ {
+ this.emitter = emitter;
+ this.taskLockbox = taskLockbox;
+ this.storageCoordinator = storageCoordinator;
+ this.taskActionClientFactory = taskActionClientFactory;
+ }
+
+ /**
+ * Same as {@link
IndexerMetadataStorageCoordinator#markAllNonOvershadowedSegmentsAsUsed(String)}
+ * but with an EXCLUSIVE lock.
+ */
+ public int markAllNonOvershadowedSegmentsAsUsed(String dataSource)
+ {
+ return markAsUsedWithExclusiveLock(
+ dataSource,
+ Intervals.ETERNITY,
+ () ->
storageCoordinator.markAllNonOvershadowedSegmentsAsUsed(dataSource)
+ );
+ }
+
+ /**
+ * Same as {@link
IndexerMetadataStorageCoordinator#markNonOvershadowedSegmentsAsUsed(String,
Interval, List)}
+ * but with an EXCLUSIVE lock.
+ */
+ public int markNonOvershadowedSegmentsAsUsed(
+ String dataSource,
+ Interval interval,
+ @Nullable List<String> versions
+ )
+ {
+ return markAsUsedWithExclusiveLock(
+ dataSource,
+ interval,
+ () -> storageCoordinator.markNonOvershadowedSegmentsAsUsed(dataSource,
interval, versions)
+ );
+ }
+
+ /**
+ * Same as {@link
IndexerMetadataStorageCoordinator#markNonOvershadowedSegmentsAsUsed(String,
Set)}
+ * but with an EXCLUSIVE lock.
+ */
+ public int markNonOvershadowedSegmentsAsUsed(String dataSource,
Set<SegmentId> segmentIds)
+ {
+ return markAsUsedWithExclusiveLock(
+ dataSource,
+
JodaUtils.umbrellaInterval(segmentIds.stream().map(SegmentId::getInterval).toList()),
+ () -> storageCoordinator.markNonOvershadowedSegmentsAsUsed(dataSource,
segmentIds)
+ );
+ }
+
+ /**
+ * Same as {@link
IndexerMetadataStorageCoordinator#markSegmentAsUsed(SegmentId)}
+ * but with an EXCLUSIVE lock.
+ */
+ public int markSegmentAsUsed(SegmentId segmentId)
+ {
+ return markAsUsedWithExclusiveLock(
+ segmentId.getDataSource(),
+ segmentId.getInterval(),
+ () -> storageCoordinator.markSegmentAsUsed(segmentId) ? 1 : 0
+ );
+ }
+
+ /**
+ * Mark segments for a datasource-interval as used while holding an EXCLUSIVE
+ * lock. Most APIs mark non-overshadowed segments as used. So, as soon as
they
+ * are updated, they would become visible to other metadata operations. This
+ * may cause concurrent append jobs to see an inconsistent view of existing
+ * segments during their lifecycle. It may also cause a concurrent kill task
+ * to accidentally delete files of a segment that has just been marked as
used
+ * and then upgraded.
+ *
+ * @return Number of segments updated.
+ */
+ private int markAsUsedWithExclusiveLock(String dataSource, Interval
interval, UpdateOperation operation)
+ {
+ final Stopwatch taskRunTime = Stopwatch.createStarted();
+
+ final String taskId = IdUtils.newTaskId(TASK_TYPE_MARK_USED, dataSource,
interval);
+ final ExclusiveIntervalDummyTask dummyTask
+ = new ExclusiveIntervalDummyTask(taskId, dataSource, interval);
+
+ final TaskActionClient taskActionClient =
taskActionClientFactory.create(dummyTask);
+
+ final ServiceMetricEvent.Builder metricBuilder = new
ServiceMetricEvent.Builder();
+ metricBuilder.setDimension(DruidMetrics.INTERVAL, interval);
+ metricBuilder.setDimension(DruidMetrics.DATASOURCE, dataSource);
+
+ try {
+ // Acquire lock on the interval before performing the update operation
+ taskLockbox.add(dummyTask);
Review Comment:
It seems sketchy to insert a dummy task into the `TaskLockbox`. Certain
operations in the lockbox require the task to exist in storage, such as
`revokeLock`, which I think might get called if a higher-priority task is
launched for the same interval.
Have you traced through the possibilities and determined this dummy-task
approach to be OK?
##########
indexing-service/src/main/java/org/apache/druid/indexing/overlord/config/DefaultTaskConfig.java:
##########
@@ -35,7 +36,15 @@
public class DefaultTaskConfig
{
@JsonProperty
- private final Map<String, Object> context = ImmutableMap.of();
+ private final Map<String, Object> context;
+
+ @JsonCreator
+ public DefaultTaskConfig(
Review Comment:
Why this change? It seems like the end result would be similar.
##########
server/src/main/java/org/apache/druid/indexing/overlord/IndexerMetadataStorageCoordinator.java:
##########
@@ -618,6 +619,34 @@ List<Interval> getUnusedSegmentIntervals(
*/
List<Interval> retrieveSomeUnusedSegmentIntervals(String dataSource, int
limit);
+ /**
+ * Scans upto {@code maxSegmentsToScan} unused segments which are eligible
for
Review Comment:
up to (spelling)
##########
indexing-service/src/main/java/org/apache/druid/indexing/overlord/SegmentStatusManager.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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.inject.Inject;
+import org.apache.druid.common.utils.IdUtils;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.InternalServerError;
+import org.apache.druid.indexer.TaskStatus;
+import org.apache.druid.indexing.common.TaskLock;
+import org.apache.druid.indexing.common.TaskLockType;
+import org.apache.druid.indexing.common.TaskToolbox;
+import org.apache.druid.indexing.common.actions.TaskActionClient;
+import org.apache.druid.indexing.common.actions.TaskActionClientFactory;
+import org.apache.druid.indexing.common.actions.TimeChunkLockTryAcquireAction;
+import org.apache.druid.indexing.common.task.AbstractFixedIntervalTask;
+import org.apache.druid.indexing.common.task.Task;
+import org.apache.druid.indexing.common.task.TaskMetrics;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.JodaUtils;
+import org.apache.druid.java.util.common.Stopwatch;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.java.util.emitter.service.ServiceEmitter;
+import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
+import org.apache.druid.query.DruidMetrics;
+import org.apache.druid.timeline.SegmentId;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Performs updates on segments to change their "used" status in the metadata
store.
+ * <p>
+ * Currently, this class exposes methods that are used to acquire an EXCLUSIVE
+ * lock while marking segments in an interval as "used". A similar restriction
+ * may be imposed on marking (non-overshadowed) segments as unused in the
future.
+ *
+ * @see #markAsUsedWithExclusiveLock(String, Interval, UpdateOperation)
+ * Reasons for using EXCLUSIVE locks
+ */
+public class SegmentStatusManager
+{
+ private static final Logger log = new Logger(SegmentStatusManager.class);
+ private static final String TASK_TYPE_MARK_USED = "markSegmentAsUsed";
+
+ private final ServiceEmitter emitter;
+ private final GlobalTaskLockbox taskLockbox;
+ private final TaskActionClientFactory taskActionClientFactory;
+ private final IndexerMetadataStorageCoordinator storageCoordinator;
+
+ @Inject
+ public SegmentStatusManager(
+ GlobalTaskLockbox taskLockbox,
+ IndexerMetadataStorageCoordinator storageCoordinator,
+ TaskActionClientFactory taskActionClientFactory,
+ ServiceEmitter emitter
+ )
+ {
+ this.emitter = emitter;
+ this.taskLockbox = taskLockbox;
+ this.storageCoordinator = storageCoordinator;
+ this.taskActionClientFactory = taskActionClientFactory;
+ }
+
+ /**
+ * Same as {@link
IndexerMetadataStorageCoordinator#markAllNonOvershadowedSegmentsAsUsed(String)}
+ * but with an EXCLUSIVE lock.
+ */
+ public int markAllNonOvershadowedSegmentsAsUsed(String dataSource)
+ {
+ return markAsUsedWithExclusiveLock(
Review Comment:
Given this requires an exclusive lock on eternity, will it be able to run on
a datasource that is receiving active ingestion? I would think that it would
require pausing ingestion? Seems like it would make it difficult to use.
##########
indexing-service/src/main/java/org/apache/druid/indexing/common/task/KillUnusedSegmentsTask.java:
##########
@@ -533,15 +532,20 @@ public boolean isReady(TaskActionClient taskActionClient)
throws Exception
return true;
}
- private TaskLockType determineLockType(boolean useConcurrentLocks)
+ public TaskLockType determineLockType()
{
- TaskLockType actualLockType;
+ final boolean useConcurrentLocks = Boolean.TRUE.equals(
Review Comment:
This should coerce in the manner of `QueryContexts#getAsBoolean`, so
`"true"` (the string) is treated as true too.
##########
server/src/main/java/org/apache/druid/indexing/overlord/IndexerMetadataStorageCoordinator.java:
##########
@@ -618,6 +619,34 @@ List<Interval> getUnusedSegmentIntervals(
*/
List<Interval> retrieveSomeUnusedSegmentIntervals(String dataSource, int
limit);
+ /**
+ * Scans upto {@code maxSegmentsToScan} unused segments which are eligible
for
+ * kill and returns the unique datasource-interval for the segments scanned.
+ * <p>
+ * This method ensures that if there is any unused segment in any datasource
+ * which was updated earlier than {@code maxUpdatedTime}, then the returned
+ * map is not empty. However, it does NOT guarantee that:
+ * <ul>
+ * <li>the candidates in the returned map would be ordered by datasource or
interval</li>
+ * <li>the result would contain {@code limit} entries when there are more
distinct
Review Comment:
`limit` is not a parameter. Is this meant to refer to `maxResultSize`?
--
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]