HADOOP-7082. Configuration.writeXML should not hold lock while outputting. Contributed by Todd Lipcon

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1055206 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2011-01-04 22:12:22 +00:00
parent 168bf7b71d
commit 717579f3bd
2 changed files with 52 additions and 34 deletions

View File

@ -413,6 +413,9 @@ Release 0.22.0 - Unreleased
HADOOP-7038. saveVersion script includes an additional \r while running HADOOP-7038. saveVersion script includes an additional \r while running
whoami under windows. (Wang Xu via cos) whoami under windows. (Wang Xu via cos)
HADOOP-7082. Configuration.writeXML should not hold lock while outputting
(todd)
Release 0.21.1 - Unreleased Release 0.21.1 - Unreleased
IMPROVEMENTS IMPROVEMENTS

View File

@ -1588,11 +1588,36 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
* *
* @param out the writer to write to. * @param out the writer to write to.
*/ */
public synchronized void writeXml(Writer out) throws IOException { public void writeXml(Writer out) throws IOException {
Document doc = asXmlDocument();
Properties properties = getProps(); Properties properties = getProps();
try { try {
Document doc = DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// Important to not hold Configuration log while writing result, since
// 'out' may be an HDFS stream which needs to lock this configuration
// from another thread.
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
}
}
/**
* Return the XML DOM corresponding to this Configuration.
*/
private synchronized Document asXmlDocument() throws IOException {
Document doc;
try {
doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException pe) {
throw new IOException(pe);
}
Element conf = doc.createElement("configuration"); Element conf = doc.createElement("configuration");
doc.appendChild(conf); doc.appendChild(conf);
conf.appendChild(doc.createTextNode("\n")); conf.appendChild(doc.createTextNode("\n"));
@ -1623,17 +1648,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
conf.appendChild(doc.createTextNode("\n")); conf.appendChild(doc.createTextNode("\n"));
} }
return doc;
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
} catch (ParserConfigurationException pe) {
throw new IOException(pe);
}
} }
/** /**