Skip to content

Commit

Permalink
Add API to Delete Stale Definition Metadata (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
raveeram authored and ajoymajumdar committed Jun 13, 2019
1 parent 29ca257 commit 3e999d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ default void softDeleteDataMetadata(String userId, List<String> uris) {
default void deleteDefinitionMetadata(List<QualifiedName> names) {
}

/**
* Delete definition metadata older than the given timestamp for names
* that match the given pattern.
*
* @param qualifiedNamePattern The pattern to match against qualified names.
* @param lastUpdated The lastUpdated timestamp to use as a cut-off.
*/
default void deleteStaleDefinitionMetadata(String qualifiedNamePattern, Date lastUpdated) {
}

/**
* Delete definition metadata and soft delete data metadata.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
Expand Down Expand Up @@ -176,6 +177,25 @@ public void deleteDefinitionMetadata(
}
}

@Override
public void deleteStaleDefinitionMetadata(
@NonNull final String qualifiedNamePattern,
@NonNull final Date lastUpdated) {
if (qualifiedNamePattern == null || lastUpdated == null) {
return;
}

try {
jdbcTemplate.update(SQL.DELETE_DEFINITION_METADATA_STALE, new Object[]{qualifiedNamePattern, lastUpdated},
new int[]{Types.VARCHAR, Types.TIMESTAMP});
} catch (Exception e) {
final String message = String.format("Failed to delete stale definition metadata for pattern %s",
qualifiedNamePattern);
log.error(message, e);
throw new UserMetadataServiceException(message, e);
}
}

@Override
public void deleteMetadata(final String userId, final List<HasMetadata> holders) {
try {
Expand Down Expand Up @@ -885,6 +905,8 @@ protected static class SQL {
"delete from data_metadata where id in (%s)";
static final String DELETE_DEFINITION_METADATA =
"delete from definition_metadata where name in (%s)";
static final String DELETE_DEFINITION_METADATA_STALE =
"delete from definition_metadata where name like ? and last_updated < ?";
static final String DELETE_PARTITION_DEFINITION_METADATA =
"delete from partition_definition_metadata where name in (%s)";
static final String GET_DATA_METADATA =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.google.common.collect.Lists
import com.netflix.metacat.common.QualifiedName
import com.netflix.metacat.testdata.provider.DataDtoProvider

import java.time.Instant

/**
* Tests for MysqlUserMetadataService.
* TODO: Need to move this to integration-test
Expand Down Expand Up @@ -98,5 +100,10 @@ class MysqlUserMetadataServiceSpec extends BaseSpec {
mysqlUserMetadataService.deleteDataMetadata(uris)
then:
mysqlUserMetadataService.getDeletedDataMetadataUris(new Date(), 0, 10).size() == 0
when:
mysqlUserMetadataService.saveMetadata(userName, table, true)
mysqlUserMetadataService.deleteStaleDefinitionMetadata("prod%", Date.from(Instant.now().plusSeconds(500)))
then:
!mysqlUserMetadataService.getDefinitionMetadata(qualifiedName).isPresent()
}
}

0 comments on commit 3e999d0

Please sign in to comment.