-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add feature to automatically remove datasource metadata based on retention period #11227
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
1a830eb
b096991
c2c4c32
92c37ab
2447830
5032ecf
3bda833
5ab2308
411bf44
a3d57e3
f9c912c
50fd55b
118973d
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 |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ | |
| import org.apache.druid.timeline.partition.PartitionChunk; | ||
| import org.apache.druid.timeline.partition.PartitionIds; | ||
| import org.apache.druid.timeline.partition.SingleDimensionShardSpec; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.Interval; | ||
| import org.joda.time.chrono.ISOChronology; | ||
| import org.skife.jdbi.v2.Batch; | ||
|
|
@@ -73,6 +74,7 @@ | |
| import org.skife.jdbi.v2.util.ByteArrayMapper; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.validation.constraints.NotNull; | ||
| import java.io.IOException; | ||
| import java.sql.ResultSet; | ||
| import java.util.ArrayList; | ||
|
|
@@ -1423,4 +1425,39 @@ public boolean insertDataSourceMetadata(String dataSource, DataSourceMetadata me | |
| .execute() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public int removeDataSourceMetadataOlderThan(long timestamp, @NotNull Set<String> excludeDatasources) | ||
| { | ||
| DateTime dateTime = DateTimes.utc(timestamp); | ||
| List<String> datasourcesToDelete = connector.getDBI().withHandle( | ||
| handle -> handle | ||
| .createQuery( | ||
| StringUtils.format( | ||
| "SELECT dataSource FROM %1$s WHERE created_date < '%2$s'", | ||
| dbTables.getDataSourceTable(), | ||
| dateTime.toString() | ||
| ) | ||
| ) | ||
| .mapTo(String.class) | ||
| .list() | ||
| ); | ||
| datasourcesToDelete.removeAll(excludeDatasources); | ||
| return connector.getDBI().withHandle( | ||
|
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. What happens if an exception is thrown while trying to delete the datasources?
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 try catch block |
||
| handle -> { | ||
| final PreparedBatch batch = handle.prepareBatch( | ||
| StringUtils.format( | ||
| "DELETE FROM %1$s WHERE dataSource = :dataSource AND created_date < '%2$s'", | ||
|
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. Why did you choose to build the delete statements one at a time instead of doing a batch delete? I think we could encapsulate the Something like
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. This is to prevent the IN clause being too large
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. Also moved the filtering of the datasources to outside the handle block. |
||
| dbTables.getDataSourceTable(), | ||
| dateTime.toString() | ||
| ) | ||
| ); | ||
| for (String datasource : datasourcesToDelete) { | ||
| batch.bind("dataSource", datasource).add(); | ||
| } | ||
| int[] result = batch.execute(); | ||
| return IntStream.of(result).sum(); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * 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.base.Strings; | ||
| import com.google.inject.Inject; | ||
| import org.apache.druid.indexing.overlord.IndexerMetadataStorageCoordinator; | ||
| import org.apache.druid.indexing.overlord.supervisor.SupervisorSpec; | ||
| 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.MetadataSupervisorManager; | ||
| import org.apache.druid.server.coordinator.DruidCoordinatorConfig; | ||
| import org.apache.druid.server.coordinator.DruidCoordinatorRuntimeParams; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * CoordinatorDuty for automatic deletion of datasource metadata from the datasource table in metadata storage. | ||
| * (Note: datasource metadata only exists for datasource created from supervisor). | ||
| * Note that this class relies on the supervisorSpec.getDataSources names to match with the | ||
| * 'datasource' column of the datasource metadata table. | ||
| */ | ||
| public class KillDatasourceMetadata implements CoordinatorDuty | ||
| { | ||
| private static final Logger log = new Logger(KillDatasourceMetadata.class); | ||
|
|
||
| private final long period; | ||
| private final long retainDuration; | ||
| private long lastKillTime = 0; | ||
|
|
||
| private final IndexerMetadataStorageCoordinator indexerMetadataStorageCoordinator; | ||
| private final MetadataSupervisorManager metadataSupervisorManager; | ||
|
|
||
| @Inject | ||
| public KillDatasourceMetadata( | ||
| DruidCoordinatorConfig config, | ||
| IndexerMetadataStorageCoordinator indexerMetadataStorageCoordinator, | ||
| MetadataSupervisorManager metadataSupervisorManager | ||
| ) | ||
| { | ||
| this.indexerMetadataStorageCoordinator = indexerMetadataStorageCoordinator; | ||
| this.metadataSupervisorManager = metadataSupervisorManager; | ||
| this.period = config.getCoordinatorDatasourceKillPeriod().getMillis(); | ||
| Preconditions.checkArgument( | ||
| this.period >= config.getCoordinatorMetadataStoreManagementPeriod().getMillis(), | ||
| "Coordinator datasource metadata kill period must be >= druid.coordinator.period.metadataStoreManagementPeriod" | ||
| ); | ||
| this.retainDuration = config.getCoordinatorDatasourceKillDurationToRetain().getMillis(); | ||
| Preconditions.checkArgument(this.retainDuration >= 0, "Coordinator datasource metadata kill retainDuration must be >= 0"); | ||
| Preconditions.checkArgument(this.retainDuration < System.currentTimeMillis(), "Coordinator datasource metadata kill retainDuration cannot be greater than current time in ms"); | ||
| log.debug( | ||
| "Datasource Metadata Kill Task scheduling enabled with period [%s], retainDuration [%s]", | ||
| this.period, | ||
| this.retainDuration | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) | ||
| { | ||
| long currentTimeMillis = System.currentTimeMillis(); | ||
| if ((lastKillTime + period) < currentTimeMillis) { | ||
| lastKillTime = currentTimeMillis; | ||
| long timestamp = currentTimeMillis - retainDuration; | ||
| try { | ||
| // Datasource metadata only exists for datasource with supervisor | ||
| // To determine if datasource metadata is still active, we check if the supervisor for that particular datasource | ||
| // is still active or not | ||
| Map<String, SupervisorSpec> allActiveSupervisor = metadataSupervisorManager.getLatestActiveOnly(); | ||
| Set<String> allDatasourceWithActiveSupervisor = allActiveSupervisor.values() | ||
| .stream() | ||
| .map(supervisorSpec -> supervisorSpec.getDataSources()) | ||
| .flatMap(Collection::stream) | ||
| .filter(datasource -> !Strings.isNullOrEmpty(datasource)) | ||
| .collect(Collectors.toSet()); | ||
| // We exclude removing datasource metadata with active supervisor | ||
| int datasourceMetadataRemovedCount = indexerMetadataStorageCoordinator.removeDataSourceMetadataOlderThan( | ||
| timestamp, | ||
| allDatasourceWithActiveSupervisor | ||
| ); | ||
| ServiceEmitter emitter = params.getEmitter(); | ||
| emitter.emit( | ||
| new ServiceMetricEvent.Builder().build( | ||
| "metadata/kill/datasource/count", | ||
| datasourceMetadataRemovedCount | ||
| ) | ||
| ); | ||
| log.info( | ||
| "Finished running KillDatasourceMetadata duty. Removed %,d datasource metadata", | ||
| datasourceMetadataRemovedCount | ||
| ); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Failed to kill datasource metadata"); | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.