Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingjian Wu committed Jun 6, 2024
1 parent f5f00f1 commit 70be9ef
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.netflix.metacat.common.server.usermetadata;

/**
* ParentChildRelMetadataConstants.
*
* @author yingjianw
*/
public final class ParentChildRelMetadataConstants {
/**
* Key specified in DefinitionMetadata that indicate the parent table name.
*/
public static final String PARENTNAME = "root_table_name";
/**
* During create, the key specified in DefinitionMetadata that indicates the parent table uuid.
*/
public static final String PARENTUUID = "root_table_uuid";
/**
* During create, the key specified in DefinitionMetadata that indicates the child table uuid.
*/
public static final String CHILDUUID = "child_table_uuid";

/**
* During create, the key specified in DefinitionMetadata that indicates relationType.
*/
public static final String RELATIONTYPE = "relationType";

/**
* During get, the key specified in DefinitionMetadata that indicates the parent child infos.
*/
public static final String PARENTCHILDRELINFO = "parentChildRelationInfo";

/**
* During get, the key specified in DefinitionMetadata[PARENTCHILDRELINFO] that indicates parent infos.
*/
public static final String PARENTINFOS = "parentInfos";

/**
* During get, the key specified in DefinitionMetadata[PARENTCHILDRELINFO] that indicates child infos.
*/
public static final String CHILDINFOS = "childInfos";

private ParentChildRelMetadataConstants() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ class MetacatSmokeSpec extends Specification {
// Test Child11 parentChildInfo with newName
JSONAssert.assertEquals(child11ParentChildRelationInfo.toString(),
'{"parentInfos":[{"name":"embedded-fast-hive-metastore/iceberg_db/rename_parent1","relationType":"CLONE","uuid":"c11_uuid"}]}',
'{"parentInfos":[{"name":"embedded-fast-hive-metastore/iceberg_db/rename_parent1","relationType":"CLONE","uuid":"p1_uuid"}]}',
false)
assert !child12ParentChildRelationInfo.has("childInfos")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class ParentChildRelMetadataServiceSpec extends Specification{
// delete the record with parentUUID2
when:
service.drop(parent, Optional.of(parentUUID2))
child_parent_expected = [new ParentInfo(newParent.toString(), type, parentUUID)] as Set

then:
// Test Parent with uuid1
Expand All @@ -466,8 +467,8 @@ class ParentChildRelMetadataServiceSpec extends Specification{
assert service.getChildren(parent, Optional.empty()).isEmpty()

// Test Child
assert service.getParents(child, Optional.of(childUUID)).isEmpty()
assert service.getParents(child, Optional.empty()).isEmpty()
assert service.getParents(child, Optional.of(childUUID)) == child_parent_expected
assert service.getParents(child, Optional.empty()) == child_parent_expected
assert service.getChildren(child, Optional.of(childUUID)).isEmpty()
assert service.getChildren(child, Optional.empty()).isEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.netflix.metacat.common.server.spi.MetacatCatalogConfig;
import com.netflix.metacat.common.server.usermetadata.AuthorizationService;
import com.netflix.metacat.common.server.usermetadata.GetMetadataInterceptorParameters;
import com.netflix.metacat.common.server.usermetadata.ParentChildRelMetadataConstants;
import com.netflix.metacat.common.server.usermetadata.MetacatOperation;
import com.netflix.metacat.common.server.usermetadata.TagService;
import com.netflix.metacat.common.server.usermetadata.UserMetadataService;
Expand All @@ -68,7 +69,6 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
Expand All @@ -77,6 +77,8 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;



/**
* Table service implementation.
*/
Expand Down Expand Up @@ -156,7 +158,6 @@ public TableDto create(final QualifiedName name, final TableDto tableDto) {
private ObjectNode createParentChildObjectNode(@Nullable final Set<ParentInfo> parentInfos,
@Nullable final Set<ChildInfo> childInfos) {
final ObjectMapper objectMapper = new ObjectMapper();
// Create the root ObjectNode
final ObjectNode rootNode = objectMapper.createObjectNode();

if (parentInfos != null && !parentInfos.isEmpty()) {
Expand All @@ -168,12 +169,10 @@ private ObjectNode createParentChildObjectNode(@Nullable final Set<ParentInfo> p
parentNode.put("uuid", parentInfo.getUuid());
parentArrayNode.add(parentNode);
}
// Add ParentInfos to root node
rootNode.set("parentInfos", parentArrayNode);
rootNode.set(ParentChildRelMetadataConstants.PARENTINFOS, parentArrayNode);
}

if (childInfos != null && !childInfos.isEmpty()) {
// Convert Set<ChildInfo> to an ArrayNode
final ArrayNode childrenArrayNode = objectMapper.createArrayNode();
for (ChildInfo childInfo : childInfos) {
final ObjectNode childNode = objectMapper.createObjectNode();
Expand All @@ -182,8 +181,7 @@ private ObjectNode createParentChildObjectNode(@Nullable final Set<ParentInfo> p
childNode.put("uuid", childInfo.getUuid());
childrenArrayNode.add(childNode);
}
// Add ChildInfo array to root node
rootNode.set("childInfos", childrenArrayNode);
rootNode.set(ParentChildRelMetadataConstants.CHILDINFOS, childrenArrayNode);
}
return rootNode;
}
Expand All @@ -192,21 +190,22 @@ private ObjectNode createParentChildObjectNode(@Nullable final Set<ParentInfo> p
private Optional<Runnable> saveParentChildRelationship(final QualifiedName child, final TableDto tableDto) {
if (tableDto.getDefinitionMetadata() != null) {
final ObjectNode definitionMetadata = tableDto.getDefinitionMetadata();
if (definitionMetadata.has("root_table_name")) {
if (definitionMetadata.has(ParentChildRelMetadataConstants.PARENTNAME)) {
// fetch parent name
final String parentFullName = definitionMetadata.path("root_table_name").asText();
final String parentFullName = definitionMetadata.path(ParentChildRelMetadataConstants.PARENTNAME)
.asText();
String parentUUID;
String childUUID;

if (!definitionMetadata.has("root_table_uuid")) {
if (!definitionMetadata.has(ParentChildRelMetadataConstants.PARENTUUID)) {
throw new RuntimeException("root_table_uuid is not specified for parent table=" + parentFullName);
}

if (!definitionMetadata.has("child_table_uuid")) {
if (!definitionMetadata.has(ParentChildRelMetadataConstants.CHILDUUID)) {
throw new RuntimeException("child_table_uuid is not specified for child table=" + child);
}
parentUUID = definitionMetadata.path("root_table_uuid").asText();
childUUID = definitionMetadata.path("child_table_uuid").asText();
parentUUID = definitionMetadata.path(ParentChildRelMetadataConstants.PARENTUUID).asText();
childUUID = definitionMetadata.path(ParentChildRelMetadataConstants.CHILDUUID).asText();

final String[] splits = parentFullName.split("\\.");
if (splits.length != 3) {
Expand All @@ -222,18 +221,27 @@ private Optional<Runnable> saveParentChildRelationship(final QualifiedName child

// fetch relationshipType
String relationType;
if (definitionMetadata.has("relationType")) {
relationType = definitionMetadata.path("relationType").asText();
if (definitionMetadata.has(ParentChildRelMetadataConstants.RELATIONTYPE)) {
relationType = definitionMetadata.path(ParentChildRelMetadataConstants.RELATIONTYPE).asText();
} else {
relationType = "CLONE";
}

parentChildRelMetadataService.createParentChildRelation(parent, parentUUID,
child, childUUID, relationType);

// Return a Runnable for deleting the relationship
return Optional.of(() -> parentChildRelMetadataService.deleteParentChildRelation(parent, parentUUID,
child, childUUID, relationType));
return Optional.of(() -> {
try {
parentChildRelMetadataService.deleteParentChildRelation(parent,
parentUUID, child, childUUID, relationType);
} catch (Exception e) {
log.error("parentChildRelMetadataService: Fail to delete parent child relationship "
+ "after failing to create the table={}"
+ "with the following parameters: "
+ "parent={}, parentUUID={}, child={}, childUUID={}, relationType={}",
child, parent, parentUUID, child, childUUID, relationType, e);
}
});
}
}
return Optional.empty();
Expand Down Expand Up @@ -393,7 +401,7 @@ public TableDto deleteAndReturn(final QualifiedName name, final boolean isMView)
try {
parentChildRelMetadataService.drop(name, uuid);
} catch (Exception e) {
log.error("parentChildRelMetadataService: Fail to drop relation for table={} and uuid={}", name, uuid);
log.error("parentChildRelMetadataService: Fail to drop relation for table={} and uuid={}", name, uuid, e);
}

if (canDeleteMetadata(name)) {
Expand Down Expand Up @@ -493,7 +501,8 @@ public Optional<TableDto> get(final QualifiedName name, final GetTableServicePar
final ObjectNode parentChildRelObjectNode = createParentChildObjectNode(parentInfo, childInfos);
if (definitionMetadata.isPresent()) {
if (!parentChildRelObjectNode.isEmpty()) {
definitionMetadata.get().set("parentChildRelationInfo", parentChildRelObjectNode);
definitionMetadata.get().set(ParentChildRelMetadataConstants.PARENTCHILDRELINFO,
parentChildRelObjectNode);
}
} else {
if (!parentChildRelObjectNode.isEmpty()) {
Expand Down Expand Up @@ -571,8 +580,10 @@ public void rename(
// if rename operation fail, rename back the parent child relation
parentChildRelMetadataService.rename(newName, oldName, uuid);
} catch (Exception renameException) {
log.error("parentChildRelMetadataService: Fail to rename for table={} and uuid={} to newName={}",
oldName, uuid, newName, renameException);
log.error("parentChildRelMetadataService: Fail to rename parent child relation "
+ "after table fail to rename from {} to {} "
+ "with the following parameters table={} and uuid={} to newName={}",
oldName, newName, oldName, uuid, newName, renameException);
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public void createParentChildRelation(final QualifiedName parentName,
ps.setString(5, type);
return ps;
});
log.info("Successfully create parent child relationship with parent={}, parentUUID={}, "
+ "child={}, childUUID={}, relationType = {}",
parentName, parentUUID, childName, childUUID, type
);
}

@Override
Expand All @@ -107,6 +111,10 @@ public void deleteParentChildRelation(final QualifiedName parentName,
final QualifiedName childName,
final String childUUID,
final String type) {
log.info("Deleting parent child relationship with parent={}, parentUUID={}, "
+ "child={}, childUUID={}, relationType = {}",
parentName, parentUUID, childName, childUUID, type
);
jdbcTemplate.update(connection -> {
final PreparedStatement ps = connection.prepareStatement(SQL_DELETE_PARENT_CHILD_RELATIONS);
ps.setString(1, parentName.toString());
Expand All @@ -116,13 +124,20 @@ public void deleteParentChildRelation(final QualifiedName parentName,
ps.setString(5, type);
return ps;
});
log.info("Successfully delete parent child relationship with parent={}, parentUUID={}, "
+ "child={}, childUUID={}, relationType = {}",
parentName, parentUUID, childName, childUUID, type
);
}

@Override
public void rename(final QualifiedName oldName, final QualifiedName newName, final Optional<String> uuid) {
try {
renameParent(oldName, newName, uuid);
renameChild(oldName, newName, uuid);
log.info("Successfully rename parent child relationship for oldName={}, newName={}, uuid={}",
oldName, newName, uuid.orElse("")
);
} catch (RuntimeException e) {
log.error("Failed to rename entity", e);
throw e;
Expand Down Expand Up @@ -161,6 +176,7 @@ private void renameChild(final QualifiedName oldName, final QualifiedName newNam
public void drop(final QualifiedName name, final Optional<String> uuid) {
dropParent(name, uuid);
dropChild(name, uuid);
log.info("Successfully drop parent child relationship for name={}, uuid={}", name, uuid.orElse(""));
}

private void dropParent(final QualifiedName name, final Optional<String> uuid) {
Expand Down

0 comments on commit 70be9ef

Please sign in to comment.