-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add feature to automatically remove rules based on retention period #11164
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| import org.skife.jdbi.v2.StatementContext; | ||
| import org.skife.jdbi.v2.TransactionCallback; | ||
| import org.skife.jdbi.v2.TransactionStatus; | ||
| import org.skife.jdbi.v2.Update; | ||
| import org.skife.jdbi.v2.tweak.HandleCallback; | ||
| import org.skife.jdbi.v2.tweak.ResultSetMapper; | ||
|
|
||
|
|
@@ -389,6 +390,8 @@ public Void inTransaction(Handle handle, TransactionStatus transactionStatus) th | |
| .build(), | ||
| handle | ||
| ); | ||
| // Note that the method removeRulesForEmptyDatasourcesOlderThan depends on the version field | ||
| // to be a timestamp | ||
| String version = auditTime.toString(); | ||
| handle.createStatement( | ||
| StringUtils.format( | ||
|
|
@@ -421,8 +424,40 @@ public Void inTransaction(Handle handle, TransactionStatus transactionStatus) th | |
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public int removeRulesForEmptyDatasourcesOlderThan(long timestamp) | ||
| { | ||
| // Note that this DELETE SQL depends on the version field to be a timestamp. Hence, this | ||
| // method depends on overrideRule method to set version to timestamp when the rule entry is created | ||
| DateTime dateTime = DateTimes.utc(timestamp); | ||
| synchronized (lock) { | ||
| return dbi.withHandle( | ||
| handle -> { | ||
| Update sql = handle.createStatement( | ||
| // Note that this query could be expensive when the segments table is large | ||
| // However, since currently this query is run very infrequent (by default once a day by the KillRules Coordinator duty) | ||
| // and the inner query on segment table is a READ (no locking), it is keep this way. | ||
| StringUtils.format( | ||
| "DELETE FROM %1$s WHERE datasource NOT IN (SELECT DISTINCT datasource from %2$s) and datasource!=:default_rule and version < :date_time", | ||
|
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. After this change, the
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. Also, this query could be expensive when the segments table is large. I think maybe it's better to implement the join in this class. For example, this method can iterate all datasources in the rules table and check whether there is any entry in the segments table for each datasource. Then, you can use a much cheaper query for the existence check such as
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. Added comment on
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. Regarding the query
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. It makes sense 👍 |
||
| getRulesTable(), | ||
| getSegmentsTable() | ||
| ) | ||
| ); | ||
| return sql.bind("default_rule", config.getDefaultRule()) | ||
| .bind("date_time", dateTime.toString()) | ||
| .execute(); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private String getRulesTable() | ||
| { | ||
| return dbTables.getRulesTable(); | ||
| } | ||
|
|
||
| private String getSegmentsTable() | ||
| { | ||
| return dbTables.getSegmentsTable(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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.inject.Inject; | ||
| 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.server.coordinator.DruidCoordinatorConfig; | ||
| import org.apache.druid.server.coordinator.DruidCoordinatorRuntimeParams; | ||
|
|
||
| public class KillRules implements CoordinatorDuty | ||
| { | ||
| private static final Logger log = new Logger(KillRules.class); | ||
|
|
||
| private final long period; | ||
| private final long retainDuration; | ||
| private long lastKillTime = 0; | ||
|
|
||
| @Inject | ||
| public KillRules( | ||
| DruidCoordinatorConfig config | ||
| ) | ||
| { | ||
| this.period = config.getCoordinatorRuleKillPeriod().getMillis(); | ||
| Preconditions.checkArgument( | ||
| this.period >= config.getCoordinatorMetadataStoreManagementPeriod().getMillis(), | ||
| "coordinator rule kill period must be >= druid.coordinator.period.metadataStoreManagementPeriod" | ||
| ); | ||
| this.retainDuration = config.getCoordinatorRuleKillDurationToRetain().getMillis(); | ||
| Preconditions.checkArgument(this.retainDuration >= 0, "coordinator rule kill retainDuration must be >= 0"); | ||
| log.debug( | ||
| "Rule Kill Task scheduling enabled with period [%s], retainDuration [%s]", | ||
| this.period, | ||
| this.retainDuration | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) | ||
| { | ||
| if ((lastKillTime + period) < System.currentTimeMillis()) { | ||
| lastKillTime = System.currentTimeMillis(); | ||
|
|
||
| long timestamp = System.currentTimeMillis() - retainDuration; | ||
| int ruleRemoved = params.getDatabaseRuleManager().removeRulesForEmptyDatasourcesOlderThan(timestamp); | ||
| ServiceEmitter emitter = params.getEmitter(); | ||
| emitter.emit( | ||
| new ServiceMetricEvent.Builder().build( | ||
| "metadata/kill/rule/count", | ||
| ruleRemoved | ||
| ) | ||
| ); | ||
| log.info("Finished running KillRules duty. Removed %,d rule", ruleRemoved); | ||
| } | ||
| return params; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,8 @@ public void setUp() throws Exception | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| new Duration("PT0s") | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,6 +146,8 @@ public void setUp() throws Exception | |
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| 10, | ||
| new Duration("PT0s") | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
We should update this to say that this will only work as expected if the version is a timestamp in utc
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.
Will add in a followup PR