-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add feature to automatically remove compaction configurations for inactive datasources #11232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a791327
add auto cleanup
maytasm 8154096
add auto cleanup
maytasm ae50951
Merge remote-tracking branch 'upstream/master' into IMPLY-6570
maytasm dec9549
add auto cleanup
maytasm a17bd55
add tests
maytasm 6a497b6
add tests
maytasm 0726707
use retryutils
maytasm fbd755a
use retryutils
maytasm 49ee419
use retryutils
maytasm 22fb520
address comments
maytasm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/apache/druid/java/util/RetryableException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.java.util; | ||
|
|
||
| import com.google.common.base.Predicate; | ||
| import org.apache.druid.java.util.common.RetryUtils; | ||
|
|
||
| /** | ||
| * This Exception class can be use with {@link RetryUtils}. | ||
| * The method {@link RetryUtils#retry(RetryUtils.Task, Predicate, int)} retry condition (Predicate argument) | ||
| * requires an exception to be thrown and applying the predicate to the thrown exception. | ||
| * For cases where the task method does not throw an exception but still needs retrying, | ||
| * the method can throw this RetryableException so that the RetryUtils can then retry the task | ||
| */ | ||
| public class RetryableException extends Exception | ||
| { | ||
| public RetryableException(Throwable t) | ||
| { | ||
| super(t); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
server/src/main/java/org/apache/druid/server/coordinator/duty/KillCompactionConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * 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.server.coordinator.duty; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.inject.Inject; | ||
| import org.apache.druid.audit.AuditInfo; | ||
| import org.apache.druid.common.config.ConfigManager; | ||
| import org.apache.druid.common.config.JacksonConfigManager; | ||
| import org.apache.druid.java.util.RetryableException; | ||
| import org.apache.druid.java.util.common.RetryUtils; | ||
| 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.SqlSegmentsMetadataManager; | ||
| import org.apache.druid.server.coordinator.CoordinatorCompactionConfig; | ||
| import org.apache.druid.server.coordinator.DataSourceCompactionConfig; | ||
| import org.apache.druid.server.coordinator.DruidCoordinatorConfig; | ||
| import org.apache.druid.server.coordinator.DruidCoordinatorRuntimeParams; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * CoordinatorDuty for automatic deletion of compaction configurations from the config table in metadata storage. | ||
| * Note that this will delete compaction configuration for inactive datasources | ||
| * (datasource with no used and unused segments) immediately. | ||
| */ | ||
| public class KillCompactionConfig implements CoordinatorDuty | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. javadocs please
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| { | ||
| private static final Logger log = new Logger(KillCompactionConfig.class); | ||
| private static final int UPDATE_NUM_RETRY = 5; | ||
|
|
||
| static final String COUNT_METRIC = "metadata/kill/compaction/count"; | ||
|
|
||
| private final long period; | ||
| private long lastKillTime = 0; | ||
|
|
||
| private final JacksonConfigManager jacksonConfigManager; | ||
| private final SqlSegmentsMetadataManager sqlSegmentsMetadataManager; | ||
|
|
||
| @Inject | ||
| public KillCompactionConfig( | ||
| DruidCoordinatorConfig config, | ||
| SqlSegmentsMetadataManager sqlSegmentsMetadataManager, | ||
| JacksonConfigManager jacksonConfigManager | ||
| ) | ||
| { | ||
| this.sqlSegmentsMetadataManager = sqlSegmentsMetadataManager; | ||
| this.jacksonConfigManager = jacksonConfigManager; | ||
| this.period = config.getCoordinatorCompactionKillPeriod().getMillis(); | ||
| Preconditions.checkArgument( | ||
| this.period >= config.getCoordinatorMetadataStoreManagementPeriod().getMillis(), | ||
| "Coordinator compaction configuration kill period must be >= druid.coordinator.period.metadataStoreManagementPeriod" | ||
| ); | ||
| log.debug( | ||
| "Compaction Configuration Kill Task scheduling enabled with period [%s]", | ||
| this.period | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) | ||
| { | ||
| long currentTimeMillis = System.currentTimeMillis(); | ||
| if ((lastKillTime + period) < currentTimeMillis) { | ||
| lastKillTime = currentTimeMillis; | ||
| try { | ||
| RetryUtils.retry( | ||
| () -> { | ||
| CoordinatorCompactionConfig current = CoordinatorCompactionConfig.current(jacksonConfigManager); | ||
| // If current compaction config is empty then there is nothing to do | ||
| if (CoordinatorCompactionConfig.empty().equals(current)) { | ||
| log.info( | ||
| "Finished running KillCompactionConfig duty. Nothing to do as compaction config is already empty."); | ||
| emitMetric(params.getEmitter(), 0); | ||
| return ConfigManager.SetResult.ok(); | ||
| } | ||
|
|
||
| // Get all active datasources | ||
| // Note that we get all active datasources after getting compaction config to prevent race condition if new | ||
| // datasource and config are added. | ||
| Set<String> activeDatasources = sqlSegmentsMetadataManager.retrieveAllDataSourceNames(); | ||
| final Map<String, DataSourceCompactionConfig> updated = current | ||
| .getCompactionConfigs() | ||
| .stream() | ||
| .filter(dataSourceCompactionConfig -> activeDatasources.contains(dataSourceCompactionConfig.getDataSource())) | ||
| .collect(Collectors.toMap(DataSourceCompactionConfig::getDataSource, Function.identity())); | ||
|
|
||
| // Calculate number of compaction configs to remove for logging | ||
| int compactionConfigRemoved = current.getCompactionConfigs().size() - updated.size(); | ||
|
|
||
| ConfigManager.SetResult result = jacksonConfigManager.set( | ||
| CoordinatorCompactionConfig.CONFIG_KEY, | ||
| // Do database insert without swap if the current config is empty as this means the config may be null in the database | ||
| current, | ||
| CoordinatorCompactionConfig.from(current, ImmutableList.copyOf(updated.values())), | ||
| new AuditInfo( | ||
| "KillCompactionConfig", | ||
| "CoordinatorDuty for automatic deletion of compaction config", | ||
| "" | ||
| ) | ||
| ); | ||
| if (result.isOk()) { | ||
| log.info( | ||
| "Finished running KillCompactionConfig duty. Removed %,d compaction configs", | ||
| compactionConfigRemoved | ||
| ); | ||
| emitMetric(params.getEmitter(), compactionConfigRemoved); | ||
| } else if (result.isRetryable()) { | ||
| // Failed but is retryable | ||
| log.debug("Retrying KillCompactionConfig duty"); | ||
| throw new RetryableException(result.getException()); | ||
| } else { | ||
| // Failed and not retryable | ||
| log.error(result.getException(), "Failed to kill compaction configurations"); | ||
| emitMetric(params.getEmitter(), 0); | ||
| } | ||
| return result; | ||
| }, | ||
| e -> e instanceof RetryableException, | ||
| UPDATE_NUM_RETRY | ||
| ); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Failed to kill compaction configurations"); | ||
| emitMetric(params.getEmitter(), 0); | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| private void emitMetric(ServiceEmitter emitter, int compactionConfigRemoved) | ||
| { | ||
| emitter.emit( | ||
| new ServiceMetricEvent.Builder().build( | ||
| COUNT_METRIC, | ||
| compactionConfigRemoved | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,6 +180,7 @@ public void setUp() throws Exception | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| new Duration("PT0s") | ||
| ); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,7 @@ public void setUp() throws Exception | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| new Duration("PT0s") | ||
| ); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,7 @@ public class HttpLoadQueuePeonTest | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| Duration.ZERO | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ public LoadQueuePeonTester() | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| new Duration("PT1s") | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you choose to put this class in this package?
I didn't find an equivalent class in the
java.utilpackage.I think for now it might be better to move this to a private class in
KillCompactionConfigwhich appears to be the only place where this is usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be useful for people using the RetryUtils (which is also in org.apache.druid.java.util). RetryUtils.retry retry condition requires an exception to be thrown and applying a predicate to the thrown exception. For cases where the task method does not throw an exception, the method can throw this
RetryableExceptionso that the RetryUtils can then retryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is what I suspected, but the package naming threw me off. Can you please add javadocs explaining this use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done