diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/DocumentBuilderFactoryWrapper.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/DocumentBuilderFactoryWrapper.java new file mode 100644 index 000000000000..b3c6ec10c871 --- /dev/null +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/DocumentBuilderFactoryWrapper.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdds.scm.net; + +import javax.xml.parsers.DocumentBuilderFactory; + +/** + * Wrapper for DocumentBuilderFactory reduces the initialization amount. + */ +public final class DocumentBuilderFactoryWrapper { + private static DocumentBuilderFactory factory; + + private DocumentBuilderFactoryWrapper() { + } + + public static DocumentBuilderFactory getInstance() { + if (factory == null) { + factory = DocumentBuilderFactory.newInstance(); + } + return factory; + } +} diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java index 31e83f82d694..e06395607044 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java @@ -77,8 +77,7 @@ public NetworkTopologyImpl(ConfigurationSource conf) { } public NetworkTopologyImpl(String schemaFile, InnerNode clusterTree) { - schemaManager = NodeSchemaManager.getInstance(); - schemaManager.init(schemaFile); + schemaManager = NodeSchemaManager.getInitiatedInstance(schemaFile); maxLevel = schemaManager.getMaxLevel(); shuffleOperation = Collections::shuffle; factory = InnerNodeImpl.FACTORY; diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaLoader.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaLoader.java index 38dd9d2109a6..c1e56057de0a 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaLoader.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaLoader.java @@ -175,7 +175,7 @@ private NodeSchemaLoadResult loadSchema(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { LOG.info("Loading network topology layer schema file"); // Read and parse the schema file. - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilderFactory dbf = DocumentBuilderFactoryWrapper.getInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setIgnoringComments(true); DocumentBuilder builder = dbf.newDocumentBuilder(); diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaManager.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaManager.java index fb37b214cad1..bcd107702ac9 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaManager.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeSchemaManager.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; /** The class manages all network topology schemas. */ @@ -45,6 +46,8 @@ public final class NodeSchemaManager { private static volatile NodeSchemaManager instance = null; + private static final ConcurrentHashMap CACHE = new ConcurrentHashMap<>(); + private NodeSchemaManager() { } @@ -55,6 +58,18 @@ public static NodeSchemaManager getInstance() { return instance; } + public static NodeSchemaManager getInitiatedInstance(String schemaFile) { + NodeSchemaManager cachedInstance = CACHE.get(schemaFile); + if (cachedInstance == null) { + instance = new NodeSchemaManager(); + instance.init(schemaFile); + CACHE.computeIfAbsent(schemaFile, k -> instance); + return instance; + } else { + return cachedInstance; + } + } + public void init(ConfigurationSource conf) { /** * Load schemas from network topology schema configuration file