Commit 9e79cd94 by Shwetha GS

issues in restoring types from graph type store

parent 378ec6e6
...@@ -768,7 +768,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -768,7 +768,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
String myPropertyName = propertyName + "." + entry.getKey().toString(); String myPropertyName = propertyName + "." + entry.getKey().toString();
String value = mapCollectionEntryToVertex(id, instanceVertex, attributeInfo, String value = mapCollectionEntryToVertex(id, instanceVertex, attributeInfo,
idToVertexMap, elementType, entry.getValue(), myPropertyName); idToVertexMap, elementType, entry.getValue(), myPropertyName);
instanceVertex.setProperty(myPropertyName, value); addProperty(instanceVertex, myPropertyName, value);
} }
// for dereference on way out // for dereference on way out
......
...@@ -54,13 +54,9 @@ import java.util.Iterator; ...@@ -54,13 +54,9 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
public class GraphBackedTypeStore implements ITypeStore { public class GraphBackedTypeStore implements ITypeStore {
public static final String VERTEX_TYPE = Constants.INTERNAL_PROPERTY_KEY_PREFIX + "typeSystem"; public static final String VERTEX_TYPE = "typeSystem";
private static final String PROPERTY_PREFIX = Constants.INTERNAL_PROPERTY_KEY_PREFIX + "type."; private static final String PROPERTY_PREFIX = Constants.INTERNAL_PROPERTY_KEY_PREFIX + "type.";
public static final String SUPERTYPE_EDGE_LABEL = PROPERTY_PREFIX + ".supertype"; public static final String SUPERTYPE_EDGE_LABEL = PROPERTY_PREFIX + ".supertype";
public static final String SUBTYPE_EDGE_LABEL = PROPERTY_PREFIX + ".subtype";
private static final ImmutableList META_PROPERTIES = ImmutableList.of(Constants.VERTEX_TYPE_PROPERTY_KEY,
Constants.TYPE_CATEGORY_PROPERTY_KEY, Constants.TYPENAME_PROPERTY_KEY);
private static Logger LOG = LoggerFactory.getLogger(GraphBackedTypeStore.class); private static Logger LOG = LoggerFactory.getLogger(GraphBackedTypeStore.class);
...@@ -109,12 +105,24 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -109,12 +105,24 @@ public class GraphBackedTypeStore implements ITypeStore {
} }
} }
private void addProperty(Vertex vertex, String propertyName, Object value) {
LOG.debug("Setting property {} = \"{}\" to vertex {}", propertyName, value, vertex);
vertex.setProperty(propertyName, value);
}
private void storeInGraph(EnumType dataType) { private void storeInGraph(EnumType dataType) {
Vertex vertex = createVertex(dataType.getTypeCategory(), dataType.getName()); Vertex vertex = createVertex(dataType.getTypeCategory(), dataType.getName());
for (EnumValue value : dataType.values()) { List<String> values = new ArrayList<>(dataType.values().size());
String key = getPropertyKey(dataType.getName(), value.value); for (EnumValue enumValue : dataType.values()) {
vertex.setProperty(key, value.ordinal); String key = getPropertyKey(dataType.getName(), enumValue.value);
addProperty(vertex, key, enumValue.ordinal);
values.add(enumValue.value);
} }
addProperty(vertex, getPropertyKey(dataType.getName()), values);
}
private String getPropertyKey(String name) {
return PROPERTY_PREFIX + name;
} }
private String getPropertyKey(String parent, String child) { private String getPropertyKey(String parent, String child) {
...@@ -128,18 +136,20 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -128,18 +136,20 @@ public class GraphBackedTypeStore implements ITypeStore {
private void storeInGraph(TypeSystem typeSystem, DataTypes.TypeCategory category, String typeName, private void storeInGraph(TypeSystem typeSystem, DataTypes.TypeCategory category, String typeName,
ImmutableList<AttributeInfo> attributes, ImmutableList<String> superTypes) throws MetadataException { ImmutableList<AttributeInfo> attributes, ImmutableList<String> superTypes) throws MetadataException {
Vertex vertex = createVertex(category, typeName); Vertex vertex = createVertex(category, typeName);
List<String> attrNames = new ArrayList<>();
if (attributes != null) { if (attributes != null) {
for (AttributeInfo attribute : attributes) { for (AttributeInfo attribute : attributes) {
String propertyKey = getPropertyKey(typeName, attribute.name); String propertyKey = getPropertyKey(typeName, attribute.name);
try { try {
vertex.setProperty(propertyKey, attribute.toJson()); addProperty(vertex, propertyKey, attribute.toJson());
} catch (JSONException e) { } catch (JSONException e) {
throw new StorageException(typeName, e); throw new StorageException(typeName, e);
} }
attrNames.add(attribute.name);
addReferencesForAttribute(typeSystem, vertex, attribute); addReferencesForAttribute(typeSystem, vertex, attribute);
} }
} }
addProperty(vertex, getPropertyKey(typeName), attrNames);
//Add edges for hierarchy //Add edges for hierarchy
if (superTypes != null) { if (superTypes != null) {
...@@ -147,7 +157,6 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -147,7 +157,6 @@ public class GraphBackedTypeStore implements ITypeStore {
HierarchicalType superType = typeSystem.getDataType(HierarchicalType.class, superTypeName); HierarchicalType superType = typeSystem.getDataType(HierarchicalType.class, superTypeName);
Vertex superVertex = createVertex(superType.getTypeCategory(), superTypeName); Vertex superVertex = createVertex(superType.getTypeCategory(), superTypeName);
addEdge(vertex, superVertex, SUPERTYPE_EDGE_LABEL); addEdge(vertex, superVertex, SUPERTYPE_EDGE_LABEL);
addEdge(superVertex, vertex, SUBTYPE_EDGE_LABEL);
} }
} }
} }
...@@ -224,19 +233,19 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -224,19 +233,19 @@ public class GraphBackedTypeStore implements ITypeStore {
break; break;
case STRUCT: case STRUCT:
AttributeDefinition[] attributes = getAttributes(vertex); AttributeDefinition[] attributes = getAttributes(vertex, typeName);
structs.add(new StructTypeDefinition(typeName, attributes)); structs.add(new StructTypeDefinition(typeName, attributes));
break; break;
case CLASS: case CLASS:
ImmutableList<String> superTypes = getSuperTypes(vertex); ImmutableList<String> superTypes = getSuperTypes(vertex);
attributes = getAttributes(vertex); attributes = getAttributes(vertex, typeName);
classTypes.add(new HierarchicalTypeDefinition(ClassType.class, typeName, superTypes, attributes)); classTypes.add(new HierarchicalTypeDefinition(ClassType.class, typeName, superTypes, attributes));
break; break;
case TRAIT: case TRAIT:
superTypes = getSuperTypes(vertex); superTypes = getSuperTypes(vertex);
attributes = getAttributes(vertex); attributes = getAttributes(vertex, typeName);
traits.add(new HierarchicalTypeDefinition(TraitType.class, typeName, superTypes, attributes)); traits.add(new HierarchicalTypeDefinition(TraitType.class, typeName, superTypes, attributes));
break; break;
...@@ -250,11 +259,10 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -250,11 +259,10 @@ public class GraphBackedTypeStore implements ITypeStore {
private EnumTypeDefinition getEnumType(Vertex vertex) { private EnumTypeDefinition getEnumType(Vertex vertex) {
String typeName = vertex.getProperty(Constants.TYPENAME_PROPERTY_KEY); String typeName = vertex.getProperty(Constants.TYPENAME_PROPERTY_KEY);
List<EnumValue> enumValues = new ArrayList<>(); List<EnumValue> enumValues = new ArrayList<>();
for (String property : vertex.getPropertyKeys()) { List<String> values = vertex.getProperty(getPropertyKey(typeName));
if (!META_PROPERTIES.contains(property)) { for (String value : values) {
String enumValue = StringUtils.removeStart(property, PROPERTY_PREFIX + typeName); String valueProperty = getPropertyKey(typeName, value);
enumValues.add(new EnumValue(enumValue, vertex.<Integer>getProperty(property))); enumValues.add(new EnumValue(value, vertex.<Integer>getProperty(valueProperty)));
}
} }
return new EnumTypeDefinition(typeName, enumValues.toArray(new EnumValue[enumValues.size()])); return new EnumTypeDefinition(typeName, enumValues.toArray(new EnumValue[enumValues.size()]));
} }
...@@ -269,15 +277,15 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -269,15 +277,15 @@ public class GraphBackedTypeStore implements ITypeStore {
return ImmutableList.copyOf(superTypes); return ImmutableList.copyOf(superTypes);
} }
private AttributeDefinition[] getAttributes(Vertex vertex) throws MetadataException { private AttributeDefinition[] getAttributes(Vertex vertex, String typeName) throws MetadataException {
List<AttributeDefinition> attributes = new ArrayList<>(); List<AttributeDefinition> attributes = new ArrayList<>();
for (String property : vertex.getPropertyKeys()) { List<String> attrNames = vertex.getProperty(getPropertyKey(typeName));
if (!META_PROPERTIES.contains(property)) { for (String attrName : attrNames) {
try { try {
attributes.add(AttributeInfo.fromJson((String) vertex.getProperty(property))); String propertyKey = getPropertyKey(typeName, attrName);
} catch (JSONException e) { attributes.add(AttributeInfo.fromJson((String) vertex.getProperty(propertyKey)));
throw new MetadataException(e); } catch (JSONException e) {
} throw new MetadataException(e);
} }
} }
return attributes.toArray(new AttributeDefinition[attributes.size()]); return attributes.toArray(new AttributeDefinition[attributes.size()]);
...@@ -310,9 +318,9 @@ public class GraphBackedTypeStore implements ITypeStore { ...@@ -310,9 +318,9 @@ public class GraphBackedTypeStore implements ITypeStore {
if (vertex == null) { if (vertex == null) {
LOG.debug("Adding vertex {}{}", PROPERTY_PREFIX, typeName); LOG.debug("Adding vertex {}{}", PROPERTY_PREFIX, typeName);
vertex = titanGraph.addVertex(null); vertex = titanGraph.addVertex(null);
vertex.setProperty(Constants.VERTEX_TYPE_PROPERTY_KEY, VERTEX_TYPE); //Mark as type vertex addProperty(vertex, Constants.VERTEX_TYPE_PROPERTY_KEY, VERTEX_TYPE); //Mark as type vertex
vertex.setProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, category); addProperty(vertex, Constants.TYPE_CATEGORY_PROPERTY_KEY, category);
vertex.setProperty(Constants.TYPENAME_PROPERTY_KEY, typeName); addProperty(vertex, Constants.TYPENAME_PROPERTY_KEY, typeName);
} }
return vertex; return vertex;
} }
......
...@@ -144,11 +144,15 @@ public class DefaultMetadataService implements MetadataService { ...@@ -144,11 +144,15 @@ public class DefaultMetadataService implements MetadataService {
throw new MetadataException("Invalid type definition"); throw new MetadataException("Invalid type definition");
final Map<String, IDataType> typesAdded = typeSystem.defineTypes(typesDef); final Map<String, IDataType> typesAdded = typeSystem.defineTypes(typesDef);
//TODO how do we handle transaction - store failure??
typeStore.store(typeSystem, ImmutableList.copyOf(typesAdded.keySet()));
onTypesAddedToRepo(typesAdded); try {
typeStore.store(typeSystem, ImmutableList.copyOf(typesAdded.keySet()));
onTypesAddedToRepo(typesAdded);
} catch(Throwable t) {
typeSystem.removeTypes(ImmutableList.copyOf(typesAdded.keySet()));
throw new MetadataException(t);
}
return new JSONObject() {{ return new JSONObject() {{
put(MetadataServiceClient.TYPES, typesAdded.keySet()); put(MetadataServiceClient.TYPES, typesAdded.keySet());
}}; }};
...@@ -198,8 +202,7 @@ public class DefaultMetadataService implements MetadataService { ...@@ -198,8 +202,7 @@ public class DefaultMetadataService implements MetadataService {
*/ */
@Override @Override
public String createEntity(String entityInstanceDefinition) throws MetadataException { public String createEntity(String entityInstanceDefinition) throws MetadataException {
ParamChecker.notEmpty(entityInstanceDefinition, ParamChecker.notEmpty(entityInstanceDefinition, "Entity instance definition cannot be empty");
"Entity instance definition cannot be empty");
ITypedReferenceableInstance entityTypedInstance = ITypedReferenceableInstance entityTypedInstance =
deserializeClassInstance(entityInstanceDefinition); deserializeClassInstance(entityInstanceDefinition);
......
...@@ -33,6 +33,7 @@ import org.apache.hadoop.metadata.typesystem.types.AttributeDefinition; ...@@ -33,6 +33,7 @@ import org.apache.hadoop.metadata.typesystem.types.AttributeDefinition;
import org.apache.hadoop.metadata.typesystem.types.ClassType; import org.apache.hadoop.metadata.typesystem.types.ClassType;
import org.apache.hadoop.metadata.typesystem.types.DataTypes; import org.apache.hadoop.metadata.typesystem.types.DataTypes;
import org.apache.hadoop.metadata.typesystem.types.EnumTypeDefinition; import org.apache.hadoop.metadata.typesystem.types.EnumTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.EnumValue;
import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition; import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.StructTypeDefinition; import org.apache.hadoop.metadata.typesystem.types.StructTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.TraitType; import org.apache.hadoop.metadata.typesystem.types.TraitType;
...@@ -84,6 +85,12 @@ public class GraphBackedTypeStoreTest { ...@@ -84,6 +85,12 @@ public class GraphBackedTypeStoreTest {
//validate enum //validate enum
List<EnumTypeDefinition> enumTypes = types.enumTypesAsJavaList(); List<EnumTypeDefinition> enumTypes = types.enumTypesAsJavaList();
Assert.assertEquals(1, enumTypes.size()); Assert.assertEquals(1, enumTypes.size());
EnumTypeDefinition orgLevel = enumTypes.get(0);
Assert.assertEquals(orgLevel.name, "OrgLevel");
Assert.assertEquals(orgLevel.enumValues.length, 2);
EnumValue enumValue = orgLevel.enumValues[0];
Assert.assertEquals(enumValue.value, "L1");
Assert.assertEquals(enumValue.ordinal, 1);
//validate class //validate class
List<StructTypeDefinition> structTypes = types.structTypesAsJavaList(); List<StructTypeDefinition> structTypes = types.structTypesAsJavaList();
...@@ -94,6 +101,7 @@ public class GraphBackedTypeStoreTest { ...@@ -94,6 +101,7 @@ public class GraphBackedTypeStoreTest {
for (HierarchicalTypeDefinition<ClassType> classType : classTypes) { for (HierarchicalTypeDefinition<ClassType> classType : classTypes) {
ClassType expectedType = ts.getDataType(ClassType.class, classType.typeName); ClassType expectedType = ts.getDataType(ClassType.class, classType.typeName);
Assert.assertEquals(expectedType.immediateAttrs.size(), classType.attributeDefinitions.length); Assert.assertEquals(expectedType.immediateAttrs.size(), classType.attributeDefinitions.length);
Assert.assertEquals(expectedType.superTypes.size(), classType.superTypes.size());
} }
//validate trait //validate trait
......
...@@ -236,9 +236,9 @@ public class TypeSystem { ...@@ -236,9 +236,9 @@ public class TypeSystem {
public Map<String, IDataType> defineTypes(TypesDef typesDef) public Map<String, IDataType> defineTypes(TypesDef typesDef)
throws MetadataException { throws MetadataException {
Map<String, IDataType> typesAdded = new HashMap<>();
for (EnumTypeDefinition enumDef : typesDef.enumTypesAsJavaList()) { for (EnumTypeDefinition enumDef : typesDef.enumTypesAsJavaList()) {
defineEnumType(enumDef); typesAdded.put(enumDef.name, defineEnumType(enumDef));
} }
ImmutableList<StructTypeDefinition> structDefs = ImmutableList ImmutableList<StructTypeDefinition> structDefs = ImmutableList
...@@ -248,7 +248,8 @@ public class TypeSystem { ...@@ -248,7 +248,8 @@ public class TypeSystem {
ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs = ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs =
ImmutableList.copyOf(typesDef.classTypesAsJavaList()); ImmutableList.copyOf(typesDef.classTypesAsJavaList());
return defineTypes(structDefs, traitDefs, classDefs); typesAdded.putAll(defineTypes(structDefs, traitDefs, classDefs));
return typesAdded;
} }
public Map<String, IDataType> defineTypes(ImmutableList<StructTypeDefinition> structDefs, public Map<String, IDataType> defineTypes(ImmutableList<StructTypeDefinition> structDefs,
...@@ -306,6 +307,10 @@ public class TypeSystem { ...@@ -306,6 +307,10 @@ public class TypeSystem {
return false; return false;
} }
public void removeTypes(ImmutableList<String> typeNames) {
}
class TransientTypeSystem extends TypeSystem { class TransientTypeSystem extends TypeSystem {
final ImmutableList<StructTypeDefinition> structDefs; final ImmutableList<StructTypeDefinition> structDefs;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment