HADOOP-15554. Improve JIT performance for Configuration parsing. Contributed by Todd Lipcon.

This commit is contained in:
Andrew Wang 2018-07-02 18:31:21 +02:00
parent 5d748bd056
commit f51da9c4d1
1 changed files with 276 additions and 182 deletions

View File

@ -41,6 +41,7 @@
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
@ -2981,24 +2982,117 @@ private Resource loadResource(Properties properties,
if(returnCachedProperties) { if(returnCachedProperties) {
toAddTo = new Properties(); toAddTo = new Properties();
} }
List<ParsedItem> items = new Parser(reader, wrapper, quiet).parse();
for (ParsedItem item : items) {
loadProperty(toAddTo, item.name, item.key, item.value,
item.isFinal, item.sources);
}
reader.close();
if (returnCachedProperties) {
overlay(properties, toAddTo);
return new Resource(toAddTo, name, wrapper.isParserRestricted());
}
return null;
} catch (IOException e) {
LOG.error("error parsing conf " + name, e);
throw new RuntimeException(e);
} catch (XMLStreamException e) {
LOG.error("error parsing conf " + name, e);
throw new RuntimeException(e);
}
}
private static class ParsedItem {
String name;
String key;
String value;
boolean isFinal;
String[] sources;
ParsedItem(String name, String key, String value,
boolean isFinal, String[] sources) {
this.name = name;
this.key = key;
this.value = value;
this.isFinal = isFinal;
this.sources = sources;
}
}
/**
* Parser to consume SAX stream of XML elements from a Configuration.
*/
private class Parser {
private final XMLStreamReader2 reader;
private final Resource wrapper;
private final String name;
private final String[] nameSingletonArray;
private final boolean isRestricted;
private final boolean quiet;
DeprecationContext deprecations = deprecationContext.get(); DeprecationContext deprecations = deprecationContext.get();
StringBuilder token = new StringBuilder(); private StringBuilder token = new StringBuilder();
String confName = null; private String confName = null;
String confValue = null; private String confValue = null;
String confInclude = null; private String confInclude = null;
String confTag = null; private String confTag = null;
boolean confFinal = false; private boolean confFinal = false;
boolean fallbackAllowed = false; private boolean fallbackAllowed = false;
boolean fallbackEntered = false; private boolean fallbackEntered = false;
boolean parseToken = false; private boolean parseToken = false;
LinkedList<String> confSource = new LinkedList<String>(); private List<String> confSource = new ArrayList<>();
private List<ParsedItem> results = new ArrayList<>();
Parser(XMLStreamReader2 reader,
Resource wrapper,
boolean quiet) {
this.reader = reader;
this.wrapper = wrapper;
this.name = wrapper.getName();
this.nameSingletonArray = new String[]{ name };
this.isRestricted = wrapper.isParserRestricted();
this.quiet = quiet;
}
List<ParsedItem> parse() throws IOException, XMLStreamException {
while (reader.hasNext()) { while (reader.hasNext()) {
switch (reader.next()) { parseNext();
case XMLStreamConstants.START_ELEMENT: }
return results;
}
private void handleStartElement() throws MalformedURLException {
switch (reader.getLocalName()) { switch (reader.getLocalName()) {
case "property": case "property":
handleStartProperty();
break;
case "name":
case "value":
case "final":
case "source":
case "tag":
parseToken = true;
token.setLength(0);
break;
case "include":
handleInclude();
break;
case "fallback":
fallbackEntered = true;
break;
case "configuration":
break;
default:
break;
}
}
private void handleStartProperty() {
confName = null; confName = null;
confValue = null; confValue = null;
confFinal = false; confFinal = false;
@ -3025,19 +3119,12 @@ private Resource loadResource(Properties properties,
.weakIntern(reader.getAttributeValue(i)); .weakIntern(reader.getAttributeValue(i));
} }
} }
break; }
case "name":
case "value": private void handleInclude() throws MalformedURLException {
case "final":
case "source":
case "tag":
parseToken = true;
token.setLength(0);
break;
case "include":
// Determine href for xi:include // Determine href for xi:include
confInclude = null; confInclude = null;
attrCount = reader.getAttributeCount(); int attrCount = reader.getAttributeCount();
for (int i = 0; i < attrCount; i++) { for (int i = 0; i < attrCount; i++) {
String attrName = reader.getAttributeLocalName(i); String attrName = reader.getAttributeLocalName(i);
if ("href".equals(attrName)) { if ("href".equals(attrName)) {
@ -3045,7 +3132,7 @@ private Resource loadResource(Properties properties,
} }
} }
if (confInclude == null) { if (confInclude == null) {
break; return;
} }
if (isRestricted) { if (isRestricted) {
throw new RuntimeException("Error parsing resource " + wrapper throw new RuntimeException("Error parsing resource " + wrapper
@ -3058,7 +3145,11 @@ private Resource loadResource(Properties properties,
if (include != null) { if (include != null) {
Resource classpathResource = new Resource(include, name, Resource classpathResource = new Resource(include, name,
wrapper.isParserRestricted()); wrapper.isParserRestricted());
// This is only called recursively while the lock is already held
// by this thread, but synchronizing avoids a findbugs warning.
synchronized (Configuration.this) {
loadResource(properties, classpathResource, quiet); loadResource(properties, classpathResource, quiet);
}
} else { } else {
URL url; URL url;
try { try {
@ -3074,53 +3165,42 @@ private Resource loadResource(Properties properties,
if (!href.exists()) { if (!href.exists()) {
// Resource errors are non-fatal iff there is 1 xi:fallback // Resource errors are non-fatal iff there is 1 xi:fallback
fallbackAllowed = true; fallbackAllowed = true;
break; return;
} }
url = href.toURI().toURL(); url = href.toURI().toURL();
} }
Resource uriResource = new Resource(url, name, Resource uriResource = new Resource(url, name,
wrapper.isParserRestricted()); wrapper.isParserRestricted());
// This is only called recursively while the lock is already held
// by this thread, but synchronizing avoids a findbugs warning.
synchronized (Configuration.this) {
loadResource(properties, uriResource, quiet); loadResource(properties, uriResource, quiet);
} }
break;
case "fallback":
fallbackEntered = true;
break;
case "configuration":
break;
default:
break;
} }
break;
case XMLStreamConstants.CHARACTERS:
if (parseToken) {
char[] text = reader.getTextCharacters();
token.append(text, reader.getTextStart(), reader.getTextLength());
} }
break;
case XMLStreamConstants.END_ELEMENT: void handleEndElement() throws IOException {
String tokenStr = token.toString();
switch (reader.getLocalName()) { switch (reader.getLocalName()) {
case "name": case "name":
if (token.length() > 0) { if (token.length() > 0) {
confName = StringInterner.weakIntern(token.toString().trim()); confName = StringInterner.weakIntern(tokenStr.trim());
} }
break; break;
case "value": case "value":
if (token.length() > 0) { if (token.length() > 0) {
confValue = StringInterner.weakIntern(token.toString()); confValue = StringInterner.weakIntern(tokenStr);
} }
break; break;
case "final": case "final":
confFinal = "true".equals(token.toString()); confFinal = "true".equals(tokenStr);
break; break;
case "source": case "source":
confSource.add(StringInterner.weakIntern(token.toString())); confSource.add(StringInterner.weakIntern(tokenStr));
break; break;
case "tag": case "tag":
if (token.length() > 0) { if (token.length() > 0) {
confTag = StringInterner.weakIntern(token.toString()); confTag = StringInterner.weakIntern(tokenStr);
} }
break; break;
case "include": case "include":
@ -3133,49 +3213,63 @@ private Resource loadResource(Properties properties,
fallbackEntered = false; fallbackEntered = false;
break; break;
case "property": case "property":
if (confName == null || (!fallbackAllowed && fallbackEntered)) { handleEndProperty();
break;
default:
break; break;
} }
}
void handleEndProperty() {
if (confName == null || (!fallbackAllowed && fallbackEntered)) {
return;
}
String[] confSourceArray;
if (confSource.isEmpty()) {
confSourceArray = nameSingletonArray;
} else {
confSource.add(name); confSource.add(name);
confSourceArray = confSource.toArray(new String[confSource.size()]);
}
// Read tags and put them in propertyTagsMap // Read tags and put them in propertyTagsMap
if (confTag != null) { if (confTag != null) {
readTagFromConfig(confTag, confName, confValue, confSource); readTagFromConfig(confTag, confName, confValue, confSourceArray);
} }
DeprecatedKeyInfo keyInfo = DeprecatedKeyInfo keyInfo =
deprecations.getDeprecatedKeyMap().get(confName); deprecations.getDeprecatedKeyMap().get(confName);
if (keyInfo != null) { if (keyInfo != null) {
keyInfo.clearAccessed(); keyInfo.clearAccessed();
for (String key : keyInfo.newKeys) { for (String key : keyInfo.newKeys) {
// update new keys with deprecated key's value // update new keys with deprecated key's value
loadProperty(toAddTo, name, key, confValue, confFinal, results.add(new ParsedItem(
confSource.toArray(new String[confSource.size()])); name, key, confValue, confFinal, confSourceArray));
} }
} else { } else {
loadProperty(toAddTo, name, confName, confValue, confFinal, results.add(new ParsedItem(name, confName, confValue, confFinal,
confSource.toArray(new String[confSource.size()])); confSourceArray));
}
break;
default:
break;
}
default:
break;
} }
} }
reader.close();
if (returnCachedProperties) { void parseNext() throws IOException, XMLStreamException {
overlay(properties, toAddTo); switch (reader.next()) {
return new Resource(toAddTo, name, wrapper.isParserRestricted()); case XMLStreamConstants.START_ELEMENT:
handleStartElement();
break;
case XMLStreamConstants.CHARACTERS:
if (parseToken) {
char[] text = reader.getTextCharacters();
token.append(text, reader.getTextStart(), reader.getTextLength());
}
break;
case XMLStreamConstants.END_ELEMENT:
handleEndElement();
break;
default:
break;
} }
return null;
} catch (IOException e) {
LOG.error("error parsing conf " + name, e);
throw new RuntimeException(e);
} catch (XMLStreamException e) {
LOG.error("error parsing conf " + name, e);
throw new RuntimeException(e);
} }
} }
@ -3225,7 +3319,7 @@ public void addTags(Properties prop) {
* @param confSource * @param confSource
*/ */
private void readTagFromConfig(String attributeValue, String confName, String private void readTagFromConfig(String attributeValue, String confName, String
confValue, List<String> confSource) { confValue, String[] confSource) {
for (String tagStr : attributeValue.split(",")) { for (String tagStr : attributeValue.split(",")) {
try { try {
tagStr = tagStr.trim(); tagStr = tagStr.trim();
@ -3243,7 +3337,7 @@ private void readTagFromConfig(String attributeValue, String confName, String
} catch (Exception ex) { } catch (Exception ex) {
// Log the exception at trace level. // Log the exception at trace level.
LOG.trace("Tag '{}' for property:{} Source:{}", tagStr, confName, LOG.trace("Tag '{}' for property:{} Source:{}", tagStr, confName,
Arrays.toString(confSource.toArray()), ex); confSource, ex);
} }
} }
} }