From 86115147f09f2f2a0702a82c1895e13ff7f37131 Mon Sep 17 00:00:00 2001 From: "Kajur, Harish (vk250x)" Date: Sun, 7 Apr 2019 20:18:24 -0400 Subject: Optimize the areas where its creating extra memory After doing some analysis using profiler found that the most cases where memory is being spent when doing an GET is during the conversion from one case to another case and instead cached that during the start of the application Issue-ID: AAI-2331 Change-Id: I291d5f953d4158daca293198cf6fc7f5cf86d25d Signed-off-by: Kajur, Harish (vk250x) --- .../org/onap/aai/introspection/Introspector.java | 22 ++++- .../org/onap/aai/introspection/MoxyLoader.java | 47 +++++----- .../org/onap/aai/introspection/MoxyStrategy.java | 99 +++++++++------------- 3 files changed, 84 insertions(+), 84 deletions(-) (limited to 'aai-core/src/main/java') diff --git a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java index 269d6330..21299f79 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java @@ -24,9 +24,12 @@ import com.att.eelf.configuration.EELFManager; import com.google.common.base.CaseFormat; import org.apache.commons.lang.ClassUtils; import org.eclipse.persistence.exceptions.DynamicException; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LogFormatTools; +import org.onap.aai.nodes.CaseFormatStore; +import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.restcore.MediaType; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; @@ -50,13 +53,26 @@ public abstract class Introspector implements Cloneable { private Set uniqueProperties = null; private Set indexedProperties = null; private Set allKeys = null; + + protected CaseFormatStore caseFormatStore = null; + protected NodeIngestor nodeIngestor; + protected Introspector(Object obj) { + this.nodeIngestor = SpringContextAware.getBean(NodeIngestor.class); + this.caseFormatStore = nodeIngestor.getCaseFormatStore(); } public abstract boolean hasProperty(String name); protected String convertPropertyName (String name) { - return CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name); + return caseFormatStore + .fromLowerHyphenToLowerCamel(name) + .orElseGet( + () -> { + LOGGER.debug("Unable to find {} in the store from lower hyphen to lower camel", name); + return CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name); + } + ); } protected abstract Object get(String name); @@ -198,7 +214,7 @@ public abstract class Introspector implements Cloneable { * @param obj the value to be set * @return */ - public void setValue(String name, Object obj) throws IllegalArgumentException { + public void setValue(String name, Object obj) { Object box = this.castValueAccordingToSchema(name, obj); name = convertPropertyName(name); @@ -558,8 +574,6 @@ public abstract class Introspector implements Cloneable { public abstract String marshal(MarshallerProperties properties); - public abstract Object clone(); - public abstract Object getUnderlyingObject(); public String marshal(boolean formatted) { diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java index fa52d62f..35583d7c 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java @@ -32,12 +32,12 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LogFormatTools; +import org.onap.aai.nodes.CaseFormatStore; import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.restcore.MediaType; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.setup.SchemaVersion; import org.onap.aai.workarounds.NamingExceptions; -import org.springframework.stereotype.Component; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; @@ -49,19 +49,22 @@ import java.util.stream.Collectors; public class MoxyLoader extends Loader { + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class); + private DynamicJAXBContext jaxbContext = null; - private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class); private Map allObjs = null; private Map moxyLoaderFactory; private NodeIngestor nodeIngestor; + private CaseFormatStore caseFormatStore; private Set namedProps; public MoxyLoader(SchemaVersion version, NodeIngestor nodeIngestor) { super(version, ModelType.MOXY); this.nodeIngestor = nodeIngestor; + this.caseFormatStore = nodeIngestor.getCaseFormatStore(); process(version); } @@ -79,6 +82,16 @@ public class MoxyLoader extends Loader { return IntrospectorFactory.newInstance(ModelType.MOXY, objectFromName(name)); } + private boolean containsUpperCase(String str){ + + for(int i = 0; i < str.length(); i++){ + if(Character.isUpperCase(str.charAt(i))){ + return true; + } + } + + return false; + } /** * {@inheritDoc} */ @@ -92,10 +105,17 @@ public class MoxyLoader extends Loader { final String upperCamel; //Contains any uppercase, then assume it's upper camel - if (name.matches(".*[A-Z].*")) { + if (containsUpperCase(name)) { upperCamel = sanitizedName; } else { - upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName); + upperCamel = caseFormatStore + .fromLowerHyphenToUpperCamel(sanitizedName) + .orElseGet( + () -> { + LOGGER.debug("Unable to find {} in the store for lower hyphen to upper camel", sanitizedName); + return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName); + } + ); } try { @@ -153,7 +173,7 @@ public class MoxyLoader extends Loader { if (this.allObjs != null) { return allObjs; } else { - ImmutableMap.Builder map = new ImmutableMap.Builder(); + ImmutableMap.Builder map = new ImmutableMap.Builder<>(); Set objs = objectsInVersion(); for (String objName : objs) { try { @@ -178,7 +198,6 @@ public class MoxyLoader extends Loader { LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e)); } - //result.remove("EdgePropNames"); return result; } @@ -199,20 +218,4 @@ public class MoxyLoader extends Loader { public DynamicJAXBContext getJAXBContext() { return this.jaxbContext; } - - /* - * Im keeping this for now - Just in case - */ - /*private static class Helper { - private static final Map INSTANCEMAP = new ConcurrentHashMap<>(); - - private Helper() {} - - private static MoxyLoader getLoaderBySchemaVersion(SchemaVersion v) { - if (!INSTANCEMAP.containsKey(v)) { - INSTANCEMAP.put(v, new MoxyLoader(v, nodeIngestor)); - } - return INSTANCEMAP.get(v); - } - }*/ } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java index f0d487e8..455e7846 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java @@ -23,35 +23,31 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; - - import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.dynamic.DynamicEntity; import org.eclipse.persistence.dynamic.DynamicType; import org.eclipse.persistence.exceptions.DynamicException; -import org.eclipse.persistence.jaxb.UnmarshallerProperties; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.oxm.XMLField; import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping; import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping; import org.onap.aai.config.SpringContextAware; +import org.onap.aai.logging.LogFormatTools; +import org.onap.aai.nodes.CaseFormatStore; import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.restcore.MediaType; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; import org.onap.aai.setup.SchemaVersion; import org.springframework.web.util.UriUtils; + import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; -import java.io.StringReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.Map.Entry; -import java.util.stream.Collectors; public class MoxyStrategy extends Introspector { @@ -60,8 +56,6 @@ public class MoxyStrategy extends Introspector { private DynamicType internalType = null; private DynamicJAXBContext jaxbContext = null; private ClassDescriptor cd = null; - private Marshaller marshaller = null; - private Unmarshaller unmarshaller = null; private SchemaVersion version = null; private Set properties = null; private Set keys = null; @@ -69,14 +63,11 @@ public class MoxyStrategy extends Introspector { private boolean isInitialized = false; - private NodeIngestor nodeIngestor; - protected MoxyStrategy(Object obj) { super(obj); /* must look up the correct jaxbcontext for this object */ className = MoxyStrategy.class.getSimpleName(); internalObject = (DynamicEntity)obj; - nodeIngestor = SpringContextAware.getBean(NodeIngestor.class); version = nodeIngestor.getVersionFromClassName(internalObject.getClass().getName()); super.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(getModelType(), version); jaxbContext = nodeIngestor.getContextForVersion(version); @@ -84,15 +75,6 @@ public class MoxyStrategy extends Introspector { internalType = jaxbContext.getDynamicType(simpleName); cd = internalType.getDescriptor(); - try { - marshaller = jaxbContext.createMarshaller(); - - unmarshaller = jaxbContext.createUnmarshaller(); - - } catch (JAXBException e) { - - } - } private void init() { @@ -100,21 +82,26 @@ public class MoxyStrategy extends Introspector { Set props = new LinkedHashSet<>(); for (String s : internalType.getPropertiesNames()) { - props.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s)); + String value = caseFormatStore + .fromLowerCamelToLowerHyphen(s) + .orElseGet( + () -> { + LOGGER.debug("Unable to find {} in the store from lower camel to lower hyphen", s); + return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s); + } + ); + props.add(value); } props = Collections.unmodifiableSet(props); this.properties = props; Set requiredProps = new LinkedHashSet<>(); - requiredProps = new LinkedHashSet<>(); for (DatabaseMapping dm : cd.getMappings()) { if (dm.getField() instanceof XMLField) { XMLField x = (XMLField)dm.getField(); - if (x != null) { - if (x.isRequired()) { - requiredProps.add(this.removeXPathDescriptor(x.getName())); - } + if (x != null && x.isRequired()) { + requiredProps.add(this.removeXPathDescriptor(x.getName())); } } } @@ -145,7 +132,7 @@ public class MoxyStrategy extends Introspector { } @Override - public void set(String name, Object obj) throws IllegalArgumentException { + public void set(String name, Object obj){ internalObject.set(name, obj); } @@ -257,10 +244,26 @@ public class MoxyStrategy extends Introspector { public String getChildName() { String className = internalObject.getClass().getSimpleName(); - String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); + String lowerHyphen = caseFormatStore + .fromUpperCamelToLowerHyphen(className) + .orElseGet( + () -> { + LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", className); + return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); + } + ); if (this.isContainer()) { - lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName()); + String upperCamel = this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName(); + + lowerHyphen = caseFormatStore + .fromUpperCamelToLowerHyphen(upperCamel) + .orElseGet( + () -> { + LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", upperCamel); + return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, upperCamel); + } + ); } return lowerHyphen; @@ -269,14 +272,12 @@ public class MoxyStrategy extends Introspector { @Override public String getName() { String className = internalObject.getClass().getSimpleName(); - String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); - /* - if (this.isContainer()) { - lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().get(0)).getSimpleName()); - }*/ - - - return lowerHyphen; + return caseFormatStore + .fromUpperCamelToLowerHyphen(className) + .orElseGet(() -> { + LOGGER.debug("Unable to find {} in the store for upper camel to lower hyphen", className); + return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); + }); } @Override @@ -313,7 +314,6 @@ public class MoxyStrategy extends Introspector { @Override public String preProcessKey (String key) { String result = ""; - //String trimmedRestURI = restURI.replaceAll("/[\\w\\-]+?/[\\w\\-]+?$", ""); String[] split = key.split("/"); int i = 0; for (i = split.length-1; i >= 0; i--) { @@ -334,6 +334,7 @@ public class MoxyStrategy extends Introspector { public String marshal(MarshallerProperties properties) { StringWriter result = new StringWriter(); try { + Marshaller marshaller = jaxbContext.createMarshaller(); if (properties.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) { marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, properties.getIncludeRoot()); @@ -344,30 +345,12 @@ public class MoxyStrategy extends Introspector { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted()); marshaller.marshal(this.internalObject, result); } catch (JAXBException e) { - //e.printStackTrace(); + LOGGER.warn("Encountered an jaxb exception during marshalling ", LogFormatTools.getStackTop(e)); } return result.toString(); } - @Override - public Object clone() { - Object result = null; - try { - unmarshaller = jaxbContext.createUnmarshaller(); - - unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); - unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false); - unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); - - result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue(); - } catch (JAXBException e) { - // TODO Auto-generated catch block - //e.printStackTrace(); - } - result = IntrospectorFactory.newInstance(getModelType(), result); - return result; - } @Override public ModelType getModelType() { return ModelType.MOXY; -- cgit 1.2.3-korg