Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand All @@ -45,6 +46,8 @@ public final class NodeSchemaManager {

private static volatile NodeSchemaManager instance = null;

private static final ConcurrentHashMap<String, NodeSchemaManager> CACHE = new ConcurrentHashMap<>();

private NodeSchemaManager() {
}

Expand All @@ -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
Expand Down