Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace LinkedList by ArrayList and or List #268

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 21 additions & 23 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
Expand Down Expand Up @@ -77,7 +76,7 @@ public class App {
private ConcurrentHashMap<String, YamlParser> configs;
private ConcurrentHashMap<String, YamlParser> adPipeConfigs =
new ConcurrentHashMap<String, YamlParser>();
private ArrayList<Instance> instances = new ArrayList<Instance>();
private List<Instance> instances = new ArrayList<Instance>();
private Map<String, Instance> brokenInstanceMap = new ConcurrentHashMap<String, Instance>();
private AtomicBoolean reinit = new AtomicBoolean(false);

Expand Down Expand Up @@ -255,12 +254,9 @@ public static int getLoopCounter() {
}

private void clearInstances(Collection<Instance> instances) {
List<InstanceTask<Void>> cleanupInstanceTasks = new ArrayList<InstanceTask<Void>>();

Iterator<Instance> iterator = instances.iterator();
while (iterator.hasNext()) {
Instance instance = iterator.next();

List<InstanceTask<Void>> cleanupInstanceTasks =
new ArrayList<InstanceTask<Void>>(instances.size());
for (Instance instance : instances) {
// create the cleanup task
cleanupInstanceTasks.add(new InstanceCleanupTask(instance));
}
Expand Down Expand Up @@ -375,7 +371,7 @@ public boolean processAutoDiscovery(byte[] buffer) {
return reinit;
}

protected ArrayList<Instance> getInstances() {
protected List<Instance> getInstances() {
return this.instances;
}

Expand Down Expand Up @@ -494,8 +490,8 @@ public void doIteration() {
loopCounter++;

try {
List<InstanceTask<LinkedList<HashMap<String, Object>>>> getMetricsTasks =
new ArrayList<InstanceTask<LinkedList<HashMap<String, Object>>>>();
List<InstanceTask<List<HashMap<String, Object>>>> getMetricsTasks =
new ArrayList<InstanceTask<List<HashMap<String, Object>>>>(instances.size());

for (Instance instance : instances) {
getMetricsTasks.add(new MetricCollectionTask(instance));
Expand All @@ -515,11 +511,11 @@ public void doIteration() {
getMetricsTasks,
appConfig.getCollectionTimeout(),
TimeUnit.SECONDS,
new TaskMethod<LinkedList<HashMap<String, Object>>>() {
new TaskMethod<List<HashMap<String, Object>>>() {
@Override
public TaskStatusHandler invoke(
Instance instance,
Future<LinkedList<HashMap<String, Object>>> future,
Future<List<HashMap<String, Object>>> future,
Reporter reporter) {
return App.processCollectionResults(instance, future, reporter);
}
Expand Down Expand Up @@ -560,7 +556,8 @@ public TaskStatusHandler invoke(
}

private void fixBrokenInstances(Reporter reporter) {
List<InstanceTask<Void>> fixInstanceTasks = new ArrayList<InstanceTask<Void>>();
List<InstanceTask<Void>> fixInstanceTasks =
new ArrayList<InstanceTask<Void>>(brokenInstanceMap.values().size());

for (Instance instance : brokenInstanceMap.values()) {
// Clearing rates aggregator so we won't compute wrong rates if we can reconnect
Expand Down Expand Up @@ -835,7 +832,6 @@ public void init(boolean forceNewConnection) {
clearInstances(brokenInstanceMap.values());
brokenInstanceMap.clear();

List<InstanceTask<Void>> instanceInitTasks = new ArrayList<InstanceTask<Void>>();
List<Instance> newInstances = new ArrayList<Instance>();

log.info("Dealing with YAML config instances...");
Expand All @@ -858,8 +854,8 @@ public void init(boolean forceNewConnection) {
it.remove();
}

ArrayList<Map<String, Object>> configInstances =
((ArrayList<Map<String, Object>>) yamlConfig.getYamlInstances());
List<Map<String, Object>> configInstances =
((List<Map<String, Object>>) yamlConfig.getYamlInstances());
if (configInstances == null || configInstances.size() == 0) {
String warning = "No instance found in :" + name;
log.warn(warning);
Expand Down Expand Up @@ -896,8 +892,8 @@ public void init(boolean forceNewConnection) {
(HashMap<String, Object>) adJsonConfigs.get(check);
Map<String, Object> initConfig =
(Map<String, Object>) checkConfig.get("init_config");
ArrayList<Map<String, Object>> configInstances =
(ArrayList<Map<String, Object>>) checkConfig.get("instances");
List<Map<String, Object>> configInstances =
(List<Map<String, Object>>) checkConfig.get("instances");
String checkName = (String) checkConfig.get("check_name");
for (Map<String, Object> configInstance : configInstances) {
log.info("Instantiating instance for: " + checkName);
Expand All @@ -908,6 +904,8 @@ public void init(boolean forceNewConnection) {
}
}

List<InstanceTask<Void>> instanceInitTasks =
new ArrayList<InstanceTask<Void>>(newInstances.size());
for (Instance instance : newInstances) {
// create the initializing tasks
instanceInitTasks.add(new InstanceInitializingTask(instance, forceNewConnection));
Expand Down Expand Up @@ -978,7 +976,7 @@ static TaskStatusHandler processRecoveryResults(

static TaskStatusHandler processCollectionResults(
Instance instance,
Future<LinkedList<HashMap<String, Object>>> future,
Future<List<HashMap<String, Object>>> future,
Reporter reporter) {

TaskStatusHandler status = new TaskStatusHandler();
Expand All @@ -988,7 +986,7 @@ static TaskStatusHandler processCollectionResults(
int numberOfMetrics = 0;

if (future.isDone()) {
LinkedList<HashMap<String, Object>> metrics;
List<HashMap<String, Object>> metrics;
metrics = future.get();
numberOfMetrics = metrics.size();

Expand Down Expand Up @@ -1125,7 +1123,7 @@ private <T> void processCollectionStatus(
String instanceMessage = null;
String instanceStatus = Status.STATUS_OK;
String scStatus = Status.STATUS_OK;
LinkedList<HashMap<String, Object>> metrics;
List<HashMap<String, Object>> metrics;

int numberOfMetrics = 0;

Expand All @@ -1138,7 +1136,7 @@ private <T> void processCollectionStatus(
status.raiseForStatus();

// If we get here all was good - metric count available
metrics = (LinkedList<HashMap<String, Object>>) status.getData();
metrics = (List<HashMap<String, Object>>) status.getData();
numberOfMetrics = metrics.size();

if (instance.isLimitReached()) {
Expand Down
58 changes: 29 additions & 29 deletions src/main/java/org/datadog/jmxfetch/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -52,10 +52,9 @@ private Boolean hasInclude() {
* @param configurationList the configuration list to filter
* @return a configuration list
*/
private static LinkedList<Configuration> getIncludeConfigurationList(
LinkedList<Configuration> configurationList) {
LinkedList<Configuration> includeConfigList =
new LinkedList<Configuration>(configurationList);
private static List<Configuration> getIncludeConfigurationList(
List<Configuration> configurationList) {
List<Configuration> includeConfigList = new ArrayList<Configuration>(configurationList);
Iterator<Configuration> confItr = includeConfigList.iterator();

while (confItr.hasNext()) {
Expand All @@ -73,18 +72,18 @@ private static LinkedList<Configuration> getIncludeConfigurationList(
* @param configurationList the configuration list to process
* @return filters by domain name
*/
private static HashMap<String, LinkedList<Filter>> getIncludeFiltersByDomain(
LinkedList<Configuration> configurationList) {
HashMap<String, LinkedList<Filter>> includeFiltersByDomain =
new HashMap<String, LinkedList<Filter>>();
private static HashMap<String, List<Filter>> getIncludeFiltersByDomain(
List<Configuration> configurationList) {
HashMap<String, List<Filter>> includeFiltersByDomain =
new HashMap<String, List<Filter>>();

for (Configuration conf : configurationList) {
Filter filter = conf.getInclude();
LinkedList<Filter> filters = new LinkedList<Filter>();
List<Filter> filters = new ArrayList<Filter>();

// Convert bean name, to a proper filter, i.e. a hash
if (!filter.isEmptyBeanName()) {
ArrayList<String> beanNames = filter.getBeanNames();
List<String> beanNames = filter.getBeanNames();

for (String beanName : beanNames) {
String[] splitBeanName = beanName.split(":");
Expand All @@ -101,13 +100,13 @@ private static HashMap<String, LinkedList<Filter>> getIncludeFiltersByDomain(

for (Filter f : filters) {
// Retrieve the existing filters for the domain, add the new filters
LinkedList<Filter> domainFilters;
List<Filter> domainFilters;
String domainName = f.getDomain();

if (includeFiltersByDomain.containsKey(domainName)) {
domainFilters = includeFiltersByDomain.get(domainName);
} else {
domainFilters = new LinkedList<Filter>();
domainFilters = new ArrayList<Filter>();
}

domainFilters.add(f);
Expand All @@ -124,16 +123,17 @@ private static HashMap<String, LinkedList<Filter>> getIncludeFiltersByDomain(
* @return common bean key parameters by domain name
*/
private static Map<String, Set<String>> getCommonBeanKeysByDomain(
Map<String, LinkedList<Filter>> filtersByDomain) {
Map<String, List<Filter>> filtersByDomain) {
Map<String, Set<String>> beanKeysIntersectionByDomain =
new HashMap<String, Set<String>>();

for (Entry<String, LinkedList<Filter>> filtersEntry : filtersByDomain.entrySet()) {
String domainName = filtersEntry.getKey();
LinkedList<Filter> filters = filtersEntry.getValue();

for (Entry<String, List<Filter>> filtersEntry : filtersByDomain.entrySet()) {
List<Filter> filters = filtersEntry.getValue();
if (filters == null || filters.isEmpty()) {
continue;
}
// Compute keys intersection
Set<String> keysIntersection = new HashSet<String>(filters.getFirst().keySet());
Set<String> keysIntersection = new HashSet<String>(filters.get(0).keySet());

for (Filter filter : filters) {
keysIntersection.retainAll(filter.keySet());
Expand All @@ -143,7 +143,7 @@ private static Map<String, Set<String>> getCommonBeanKeysByDomain(
for (String param : JmxAttribute.getExcludedBeanParams()) {
keysIntersection.remove(param);
}

String domainName = filtersEntry.getKey();
beanKeysIntersectionByDomain.put(domainName, keysIntersection);
}

Expand All @@ -160,7 +160,7 @@ private static Map<String, Set<String>> getCommonBeanKeysByDomain(
*/
private static Map<String, Map<String, String>> getCommonScopeByDomain(
Map<String, Set<String>> beanKeysByDomain,
Map<String, LinkedList<Filter>> filtersByDomain) {
Map<String, List<Filter>> filtersByDomain) {
// Compute a common scope a among filters by domain name
Map<String, Map<String, String>> commonScopeByDomain =
new HashMap<String, Map<String, String>>();
Expand All @@ -169,16 +169,16 @@ private static Map<String, Map<String, String>> getCommonScopeByDomain(
beanKeysByDomain.entrySet()) {
String domainName = commonParametersByDomainEntry.getKey();
Set<String> commonParameters = commonParametersByDomainEntry.getValue();
LinkedList<Filter> filters = filtersByDomain.get(domainName);
List<Filter> filters = filtersByDomain.get(domainName);
Map<String, String> commonScope = new HashMap<String, String>();

for (String parameter : commonParameters) {
// Check if all values associated with the parameters are the same
String commonValue = null;
Boolean hasCommonValue = true;
boolean hasCommonValue = true;

for (Filter f : filters) {
ArrayList<String> parameterValues = f.getParameterValues(parameter);
List<String> parameterValues = f.getParameterValues(parameter);

if (parameterValues.size() != 1
|| (commonValue != null
Expand Down Expand Up @@ -231,18 +231,18 @@ private static String beanScopeToString(
* @param configurationList the configuration list to process
* @return common bean pattern strings
*/
public static LinkedList<String> getGreatestCommonScopes(
LinkedList<Configuration> configurationList) {
LinkedList<Configuration> includeConfigList =
public static List<String> getGreatestCommonScopes(
List<Configuration> configurationList) {
List<Configuration> includeConfigList =
getIncludeConfigurationList(configurationList);
Map<String, LinkedList<Filter>> includeFiltersByDomain =
Map<String, List<Filter>> includeFiltersByDomain =
getIncludeFiltersByDomain(includeConfigList);
Map<String, Set<String>> parametersIntersectionByDomain =
getCommonBeanKeysByDomain(includeFiltersByDomain);
Map<String, Map<String, String>> commonBeanScopeByDomain =
getCommonScopeByDomain(parametersIntersectionByDomain, includeFiltersByDomain);

LinkedList<String> result = new LinkedList<String>();
List<String> result = new ArrayList<String>(commonBeanScopeByDomain.entrySet().size());

for (Entry<String, Map<String, String>> beanScopeEntry :
commonBeanScopeByDomain.entrySet()) {
Expand Down
Loading