Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingjian Wu committed Jun 5, 2024
1 parent 4509fd8 commit e2ef102
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.netflix.metacat.common.server.usermetadata;

import com.netflix.metacat.common.dto.TableDto;

import java.util.Optional;

/**
* Default Table UUID Provider.
*
* @author yingjianw
*/
public class DefaultTableUUIDProvider implements TableUUIDProvider {
@Override
public Optional<String> getUUID(final TableDto tableDto) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.netflix.metacat.common.server.usermetadata;

import com.netflix.metacat.common.dto.TableDto;

import java.util.Optional;

/**
* Interface for TableUUIDPorvider.
* @author yingjianw
*/
public interface TableUUIDProvider {
/**
* Given a tableDto, get the corresponding table uuid if applicable.
* @param tableDto dto for table
* @return the uuid of the table
**/
Optional<String> getUUID(TableDto tableDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import com.netflix.metacat.common.server.usermetadata.ParentChildRelMetadataServ
import com.netflix.metacat.metadata.mysql.MySqlParentChildRelMetaDataService
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.datasource.DriverManagerDataSource
import org.springframework.retry.backoff.NoBackOffPolicy
import org.springframework.retry.policy.SimpleRetryPolicy
import org.springframework.retry.support.RetryTemplate
import spock.lang.Shared
import spock.lang.Specification

Expand All @@ -18,8 +15,6 @@ class ParentChildRelMetadataServiceSpec extends Specification{
private ParentChildRelMetadataService service;
@Shared
private JdbcTemplate jdbcTemplate;
@Shared
private RetryTemplate retryTemplate;

@Shared
private String catalog = "prodhive"
Expand All @@ -37,17 +32,8 @@ class ParentChildRelMetadataServiceSpec extends Specification{
dataSource.setUsername(username)
dataSource.setPassword(password)

retryTemplate = new RetryTemplate()

// Configure no backoff policy
retryTemplate.setBackOffPolicy(new NoBackOffPolicy())

// Configure retry policy with no retries
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1) // 1 attempt means no retries
retryTemplate.setRetryPolicy(retryPolicy)

jdbcTemplate = new JdbcTemplate(dataSource)
service = new MySqlParentChildRelMetaDataService(jdbcTemplate, retryTemplate)
service = new MySqlParentChildRelMetaDataService(jdbcTemplate)
}

def cleanup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
import com.netflix.metacat.common.server.usermetadata.LookupService;
import com.netflix.metacat.common.server.usermetadata.TagService;
import com.netflix.metacat.common.server.usermetadata.UserMetadataService;
import com.netflix.metacat.common.server.util.ThreadServiceManager;
import com.netflix.metacat.common.server.usermetadata.ParentChildRelMetadataService;
import com.netflix.metacat.common.server.usermetadata.TableUUIDProvider;
import com.netflix.metacat.common.server.util.ThreadServiceManager;
import com.netflix.metacat.main.manager.CatalogManager;
import com.netflix.metacat.main.manager.ConnectorManager;
import com.netflix.metacat.main.manager.PluginManager;
Expand Down Expand Up @@ -230,6 +231,7 @@ public DatabaseService databaseService(
* @param authorizationService authorization Service
* @param ownerValidationService owner validation service
* @param parentChildRelMetadataService parentChildRelMetadataService
* @param tableUUIDProvider provider that provides the uuid for a table
*
* @return The table service bean
*/
Expand All @@ -247,7 +249,8 @@ public TableService tableService(
final ConverterUtil converterUtil,
final AuthorizationService authorizationService,
final OwnerValidationService ownerValidationService,
final ParentChildRelMetadataService parentChildRelMetadataService) {
final ParentChildRelMetadataService parentChildRelMetadataService,
final TableUUIDProvider tableUUIDProvider) {
return new TableServiceImpl(
connectorManager,
connectorTableServiceProxy,
Expand All @@ -261,7 +264,8 @@ public TableService tableService(
converterUtil,
authorizationService,
ownerValidationService,
parentChildRelMetadataService
parentChildRelMetadataService,
tableUUIDProvider
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@
import com.netflix.metacat.common.server.events.MetacatUpdateIcebergTablePostEvent;
import com.netflix.metacat.common.server.events.MetacatUpdateTablePostEvent;
import com.netflix.metacat.common.server.events.MetacatUpdateTablePreEvent;
import com.netflix.metacat.common.server.monitoring.Metrics;
import com.netflix.metacat.common.server.model.ChildInfo;
import com.netflix.metacat.common.server.model.ParentInfo;
import com.netflix.metacat.common.server.monitoring.Metrics;
import com.netflix.metacat.common.server.properties.Config;
import com.netflix.metacat.common.server.spi.MetacatCatalogConfig;
import com.netflix.metacat.common.server.usermetadata.AuthorizationService;
import com.netflix.metacat.common.server.usermetadata.ParentChildRelMetadataService;
import com.netflix.metacat.common.server.usermetadata.GetMetadataInterceptorParameters;
import com.netflix.metacat.common.server.usermetadata.MetacatOperation;
import com.netflix.metacat.common.server.usermetadata.TagService;
import com.netflix.metacat.common.server.usermetadata.UserMetadataService;
import com.netflix.metacat.common.server.usermetadata.ParentChildRelMetadataService;
import com.netflix.metacat.common.server.usermetadata.TableUUIDProvider;
import com.netflix.metacat.common.server.util.MetacatContextManager;
import com.netflix.metacat.common.server.util.MetacatUtils;
import com.netflix.metacat.main.manager.ConnectorManager;
Expand Down Expand Up @@ -95,6 +96,7 @@ public class TableServiceImpl implements TableService {
private final AuthorizationService authorizationService;
private final OwnerValidationService ownerValidationService;
private final ParentChildRelMetadataService parentChildRelMetadataService;
private final TableUUIDProvider tableUUIDProvider;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -323,14 +325,6 @@ private void tag(final QualifiedName name, final ObjectNode definitionMetadata)
}
}

private Optional<String> getUUID(final TableDto tableDto) {
final Map<String, String> m = tableDto.getMetadata();
if (m != null && m.containsKey("uuid")) {
return Optional.of(m.get("uuid"));
}
return Optional.empty();
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -371,7 +365,7 @@ public TableDto deleteAndReturn(final QualifiedName name, final boolean isMView)
}
}

final Optional<String> uuid = getUUID(tableDto);
final Optional<String> uuid = tableUUIDProvider.getUUID(tableDto);
final Set<ChildInfo> childInfos = parentChildRelMetadataService.getChildren(name, uuid);
if (childInfos != null && !childInfos.isEmpty()) {
throw new RuntimeException("Fail to drop " + name + " because it still has child table");
Expand Down Expand Up @@ -491,7 +485,7 @@ public Optional<TableDto> get(final QualifiedName name, final GetTableServicePar
: userMetadataService.getDefinitionMetadataWithInterceptor(name,
GetMetadataInterceptorParameters.builder().hasMetadata(tableInternal).build());
// Always get the source of truth for parent child relation from the parentChildRelMetadataService
final Optional<String> uuid = getUUID(table);
final Optional<String> uuid = tableUUIDProvider.getUUID(table);
final Set<ParentInfo> parentInfo = parentChildRelMetadataService.getParents(name, uuid);
final Set<ChildInfo> childInfos = parentChildRelMetadataService.getChildren(name, uuid);
final ObjectNode parentChildRelObjectNode = createParentChildObjectNode(parentInfo, childInfos);
Expand Down Expand Up @@ -565,7 +559,7 @@ public void rename(
eventBus.post(new MetacatRenameTablePreEvent(oldName, metacatRequestContext, this, newName));

// Before rename, first rename its parent child relation
final Optional<String> uuid = getUUID(oldTable);
final Optional<String> uuid = tableUUIDProvider.getUUID(oldTable);
parentChildRelMetadataService.rename(oldName, newName, uuid);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.netflix.metacat.common.server.model.ParentInfo
import com.netflix.metacat.common.server.properties.Config
import com.netflix.metacat.common.server.spi.MetacatCatalogConfig
import com.netflix.metacat.common.server.usermetadata.DefaultAuthorizationService
import com.netflix.metacat.common.server.usermetadata.TableUUIDProvider
import com.netflix.metacat.common.server.usermetadata.TagService
import com.netflix.metacat.common.server.usermetadata.UserMetadataService
import com.netflix.metacat.common.server.util.MetacatContextManager
Expand Down Expand Up @@ -84,6 +85,7 @@ class TableServiceImplSpec extends Specification {
def authorizationService
def ownerValidationService
def parentChildRelSvc
def tableUUIDProvider

def service
def setup() {
Expand All @@ -101,10 +103,12 @@ class TableServiceImplSpec extends Specification {
authorizationService = new DefaultAuthorizationService(config)
ownerValidationService = Mock(OwnerValidationService)
parentChildRelSvc = Mock(ParentChildRelMetadataService)
tableUUIDProvider = Mock(TableUUIDProvider)

service = new TableServiceImpl(connectorManager, connectorTableServiceProxy, databaseService, tagService,
usermetadataService, new MetacatJsonLocator(),
eventBus, registry, config, converterUtil, authorizationService, ownerValidationService, parentChildRelSvc)
eventBus, registry, config, converterUtil, authorizationService, ownerValidationService, parentChildRelSvc,
tableUUIDProvider)
}

def testTableGet() {
Expand Down Expand Up @@ -316,13 +320,20 @@ class TableServiceImplSpec extends Specification {
def "Test Drop - Clone Table Fail to drop parent child relation"() {
given:
def name = QualifiedName.ofTable("clone", "clone", "child")
// parentChildRelSvc.getParents(_,_) >> [new ParentInfo("parent", "clone", "parent_uuid")] as Set
// parentChildRelSvc.getChildren(_,_) >> [new ChildInfo("child", "clone", "child_uuid")] as Set

when:
service.delete(name)
then:
1 * parentChildRelSvc.getParents(_, _) >> {[new ParentInfo("parent", "clone", "parent_uuid")] as Set}
2 * parentChildRelSvc.getChildren(_, _) >> {[new ChildInfo("child", "clone", "child_uuid")] as Set}
1 * config.getNoTableDeleteOnTags() >> []
thrown(RuntimeException)

when:
service.delete(name)
then:
1 * parentChildRelSvc.getParents(_, _)
2 * parentChildRelSvc.getChildren(_, _)
1 * config.getNoTableDeleteOnTags() >> []
1 * connectorTableServiceProxy.delete(_) >> {throw new RuntimeException("Fail to drop")}
0 * parentChildRelSvc.drop(_, _)
Expand Down Expand Up @@ -370,7 +381,7 @@ class TableServiceImplSpec extends Specification {
connectorManager, connectorTableServiceProxy, databaseService, tagService,
usermetadataService, new MetacatJsonLocator(),
eventBus, new DefaultRegistry(), config, converterUtil, authorizationService,
new DefaultOwnerValidationService(new NoopRegistry()), parentChildRelSvc)
new DefaultOwnerValidationService(new NoopRegistry()), parentChildRelSvc, tableUUIDProvider)

def initialDefinitionMetadataJson = toObjectNode(initialDefinitionMetadata)
tableDto = new TableDto(
Expand Down
Loading

0 comments on commit e2ef102

Please sign in to comment.