Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
AMBARI-25336 : Perf improvement with Collections usages over iteratio…
…ns (#3048)
  • Loading branch information
virajjasani authored and jonathan-hurley committed Jul 15, 2019
1 parent e932c88 commit 616d19716a999312d6d4a47bcfc9935f4e9e6efe
Showing 8 changed files with 24 additions and 45 deletions.
@@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -736,14 +735,13 @@ public List<HostRoleCommand> getTasks(Collection<Long> taskIds) {
return Collections.emptyList();
}

List<HostRoleCommand> commands = new ArrayList<>();
List<HostRoleCommand> commands;
try {
hrcOperationsLock.readLock().lock();
Map<Long, HostRoleCommand> cached = hostRoleCommandCache.getAllPresent(taskIds);
commands.addAll(cached.values());
commands = new ArrayList<>(cached.values());

List<Long> absent = new ArrayList<>();
absent.addAll(taskIds);
List<Long> absent = new ArrayList<>(taskIds);
absent.removeAll(cached.keySet());

if (!absent.isEmpty()) {
@@ -753,12 +751,7 @@ public List<HostRoleCommand> getTasks(Collection<Long> taskIds) {
cacheHostRoleCommand(hostRoleCommand);
}
}
Collections.sort(commands, new Comparator<HostRoleCommand>() {
@Override
public int compare(HostRoleCommand o1, HostRoleCommand o2) {
return (int) (o1.getTaskId()-o2.getTaskId());
}
});
commands.sort((o1, o2) -> (int) (o1.getTaskId() - o2.getTaskId()));
} finally {
hrcOperationsLock.readLock().unlock();
}
@@ -531,11 +531,7 @@ public synchronized void addCancelRequestCommand(List<Long> cancelTargets, Strin
* @return list of hosts
*/
public synchronized List<String> getHosts() { // TODO: Check whether method should be synchronized
List<String> hlist = new ArrayList<>();
for (String h : hostRoleCommands.keySet()) {
hlist.add(h);
}
return hlist;
return new ArrayList<>(hostRoleCommands.keySet());
}

synchronized float getSuccessFactor(Role r) {
@@ -468,11 +468,7 @@ private List<Grouping> getDowngradeGroupsForRolling() {
}

private List<Grouping> getDowngradeGroupsForNonrolling() {
List<Grouping> list = new ArrayList<>();
for (Grouping g : groups) {
list.add(g);
}
return list;
return new ArrayList<>(groups);
}

/**
@@ -247,9 +247,7 @@ public void addExtension(ExtensionInfo extension) {
Collection<ExtensionInfo> extensions = getExtensions();
extensions.add(extension);
Collection<ServiceInfo> services = getServices();
for (ServiceInfo service : extension.getServices()) {
services.add(service);
}
services.addAll(extension.getServices());
}

public void removeExtension(ExtensionInfo extension) {
@@ -416,9 +416,7 @@ public Set<String> getAssociatedHosts(Cluster cluster,
if (component.getValue().isMasterComponent()) {
Map<String, ServiceComponentHost> componentHosts = component.getValue().getServiceComponentHosts();
if (null != componentHosts) {
for (String componentHost : componentHosts.keySet()) {
affectedHosts.add(componentHost);
}
affectedHosts.addAll(componentHosts.keySet());
}
}
}
@@ -1259,9 +1259,7 @@ public Collection<Config> getAllConfigs() {
try {
List<Config> list = new ArrayList<>();
for (Entry<String, ConcurrentMap<String, Config>> entry : allConfigs.entrySet()) {
for (Config config : entry.getValue().values()) {
list.add(config);
}
list.addAll(entry.getValue().values());
}
return Collections.unmodifiableList(list);
} finally {
@@ -431,14 +431,8 @@ public String setAttribute(String configType, String propertyName, String attrib
*/
public Collection<String> getAllConfigTypes() {
Collection<String> allTypes = new HashSet<>();
for (String type : getFullProperties().keySet()) {
allTypes.add(type);
}

for (String type : getFullAttributes().keySet()) {
allTypes.add(type);
}

allTypes.addAll(getFullProperties().keySet());
allTypes.addAll(getFullAttributes().keySet());
return allTypes;
}

@@ -96,8 +96,10 @@ public void testShouldValidationPassWhenAllConfigTypesAreValid() throws Exceptio

EasyMock.expect(blueprintMock.getServices()).andReturn(new HashSet<>(Arrays.asList("YARN", "HDFS")));

EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(Arrays.asList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(Arrays.asList("yarn-site"));
EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(
Collections.singletonList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(
Collections.singletonList("yarn-site"));

replayAll();

@@ -114,12 +116,14 @@ public void testShouldValidationFailWhenInvalidConfigGroupsSpecifiedInCCTemplate
// GIVEN

// the config type that is not present in the stack definition for services
clusterRequestConfigTypes = new HashSet<>(Arrays.asList("oozie-site"));
clusterRequestConfigTypes = new HashSet<>(Collections.singletonList("oozie-site"));
EasyMock.expect(clusterConfigurationMock.getAllConfigTypes()).andReturn(clusterRequestConfigTypes).anyTimes();

EasyMock.expect(blueprintMock.getServices()).andReturn(new HashSet<>(Arrays.asList("YARN", "HDFS")));
EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(Arrays.asList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(Arrays.asList("yarn-site"));
EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(
Collections.singletonList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(
Collections.singletonList("yarn-site"));

replayAll();

@@ -140,8 +144,10 @@ public void testShouldValidationFailWhenThereIsAnInvalidConfigGroupProvided() th

EasyMock.expect(blueprintMock.getServices()).andReturn(new HashSet<>(Arrays.asList("YARN", "HDFS")));

EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(Arrays.asList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(Arrays.asList("yarn-site"));
EasyMock.expect(stackMock.getConfigurationTypes("HDFS")).andReturn(
Collections.singletonList("core-site"));
EasyMock.expect(stackMock.getConfigurationTypes("YARN")).andReturn(
Collections.singletonList("yarn-site"));

replayAll();

0 comments on commit 616d197

Please sign in to comment.