kfaraz commented on code in PR #16768: URL: https://github.com/apache/druid/pull/16768#discussion_r1728367822
########## indexing-service/src/main/java/org/apache/druid/indexing/compact/OverlordCompactionScheduler.java: ########## @@ -0,0 +1,295 @@ +/* + * 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.compact; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Supplier; +import com.google.inject.Inject; +import org.apache.druid.indexing.overlord.TaskMaster; +import org.apache.druid.indexing.overlord.TaskQueryTool; +import org.apache.druid.java.util.common.Stopwatch; +import org.apache.druid.java.util.common.concurrent.ScheduledExecutorFactory; +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.metadata.SegmentsMetadataManager; +import org.apache.druid.rpc.indexing.OverlordClient; +import org.apache.druid.server.compaction.CompactionRunSimulator; +import org.apache.druid.server.compaction.CompactionSimulateResult; +import org.apache.druid.server.compaction.CompactionStatusTracker; +import org.apache.druid.server.coordinator.AutoCompactionSnapshot; +import org.apache.druid.server.coordinator.ClusterCompactionConfig; +import org.apache.druid.server.coordinator.CompactionSupervisorsConfig; +import org.apache.druid.server.coordinator.CoordinatorOverlordServiceConfig; +import org.apache.druid.server.coordinator.DataSourceCompactionConfig; +import org.apache.druid.server.coordinator.DruidCompactionConfig; +import org.apache.druid.server.coordinator.duty.CompactSegments; +import org.apache.druid.server.coordinator.stats.CoordinatorRunStats; +import org.apache.druid.server.coordinator.stats.CoordinatorStat; +import org.apache.druid.server.coordinator.stats.Dimension; +import org.apache.druid.timeline.SegmentTimeline; +import org.joda.time.Duration; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Compaction scheduler that runs on the Overlord if {@link CompactionSupervisorsConfig} + * is enabled. + * <p> + * Usage: + * <ul> + * <li>When an active {@link CompactionSupervisor} starts, it should register + * itself by calling {@link #startCompaction}.</li> + * <li>When a suspended {@link CompactionSupervisor} starts, it should stop + * compaction by calling {@link #stopCompaction}.</li> + * <li>When stopping, any {@link CompactionSupervisor} (active or suspended) + * should call {@link #stopCompaction}.</li> + * </ul> + */ +public class OverlordCompactionScheduler implements CompactionScheduler +{ + private static final Logger log = new Logger(OverlordCompactionScheduler.class); + + private static final long SCHEDULE_PERIOD_SECONDS = 5; + private static final Duration METRIC_EMISSION_PERIOD = Duration.standardMinutes(5); + + private final SegmentsMetadataManager segmentManager; + private final OverlordClient overlordClient; + private final ServiceEmitter emitter; + + private final CompactionSupervisorsConfig schedulerConfig; + private final Supplier<DruidCompactionConfig> compactionConfigSupplier; + private final ConcurrentHashMap<String, DataSourceCompactionConfig> activeDatasourceConfigs; + + /** + * Single-threaded executor to process the compaction queue. + */ + private final ScheduledExecutorService executor; + + private final CompactionStatusTracker statusTracker; + private final AtomicBoolean started = new AtomicBoolean(false); + private final CompactSegments duty; + + /** + * The scheduler should enable/disable polling of segments only if the Overlord + * is running in standalone mode, otherwise this is handled by the DruidCoordinator + * class itself. + */ + private final boolean shouldPollSegments; + + private final Stopwatch sinceStatsEmitted = Stopwatch.createUnstarted(); + + @Inject + public OverlordCompactionScheduler( + TaskMaster taskMaster, + TaskQueryTool taskQueryTool, + SegmentsMetadataManager segmentManager, + Supplier<DruidCompactionConfig> compactionConfigSupplier, + CompactionStatusTracker statusTracker, + CompactionSupervisorsConfig schedulerConfig, + CoordinatorOverlordServiceConfig coordinatorOverlordServiceConfig, + ScheduledExecutorFactory executorFactory, + ServiceEmitter emitter, + ObjectMapper objectMapper + ) + { + this.segmentManager = segmentManager; + this.emitter = emitter; + this.schedulerConfig = schedulerConfig; + this.compactionConfigSupplier = compactionConfigSupplier; + + this.executor = executorFactory.create(1, "CompactionScheduler-%s"); + this.statusTracker = statusTracker; + this.shouldPollSegments = segmentManager != null + && !coordinatorOverlordServiceConfig.isEnabled(); + this.overlordClient = new LocalOverlordClient(taskMaster, taskQueryTool, objectMapper); + this.duty = new CompactSegments(this.statusTracker, overlordClient); + this.activeDatasourceConfigs = new ConcurrentHashMap<>(); + } + + @Override + public void start() + { + if (isEnabled() && started.compareAndSet(false, true)) { + log.info("Starting compaction scheduler."); + initState(); + scheduleOnExecutor(this::checkSchedulingStatus, SCHEDULE_PERIOD_SECONDS); + } + } + + @Override + public void stop() + { + if (isEnabled() && started.compareAndSet(true, false)) { + log.info("Stopping compaction scheduler."); + cleanupState(); + } + } + + @Override + public boolean isRunning() + { + return isEnabled() && started.get(); + } + + @Override + public void startCompaction(String dataSourceName, DataSourceCompactionConfig config) + { + activeDatasourceConfigs.put(dataSourceName, config); Review Comment: added. ########## indexing-service/src/main/java/org/apache/druid/indexing/overlord/http/OverlordCompactionResource.java: ########## @@ -0,0 +1,110 @@ +/* + * 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.http; + +import com.google.inject.Inject; +import com.sun.jersey.spi.container.ResourceFilters; +import org.apache.druid.indexing.compact.CompactionScheduler; +import org.apache.druid.server.coordinator.AutoCompactionSnapshot; +import org.apache.druid.server.coordinator.ClusterCompactionConfig; +import org.apache.druid.server.http.security.StateResourceFilter; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.Collection; +import java.util.Collections; + +/** + * Contains the same logic as {@code CompactionResource} but the APIs are served + * by {@link CompactionScheduler} instead of {@code DruidCoordinator}. + */ +@Path("/druid/indexer/v1/compaction") +public class OverlordCompactionResource Review Comment: done. -- 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]
