Skip to content

Commit

Permalink
lots of small sonar adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Mar 4, 2024
1 parent b8adca3 commit 51012ab
Show file tree
Hide file tree
Showing 32 changed files with 212 additions and 365 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand Down Expand Up @@ -66,10 +66,10 @@ class Aggregation implements Iterable<Row<Measurement>>, Iterator<Row<Measuremen
checkArgument(resolution.isMultiple(interval), "resolution must be a multiple of interval");

m_timestamps = new IntervalGenerator(start.stepFloor(m_resolution), end.stepCeiling(m_resolution), m_resolution);
m_intervalsPer = (double) resolution.divideBy(interval);
m_intervalsPer = resolution.divideBy(interval);

m_working = m_input.hasNext() ? m_input.next() : null;
m_nextOut = m_timestamps.hasNext() ? new Row<Measurement>(m_timestamps.next(), m_resource) : null;
m_nextOut = m_timestamps.hasNext() ? new Row<>(m_timestamps.next(), m_resource) : null;

// If the input stream contains any Samples earlier than what's relevant, iterate past them.
if (m_nextOut != null) {
Expand Down Expand Up @@ -124,7 +124,7 @@ public Row<Measurement> next() {
return m_nextOut;
}
finally {
m_nextOut = m_timestamps.hasNext() ? new Row<Measurement>(m_timestamps.next(), m_resource) : null;
m_nextOut = m_timestamps.hasNext() ? new Row<>(m_timestamps.next(), m_resource) : null;
}
}

Expand Down Expand Up @@ -160,6 +160,8 @@ public void remove() {
}

@Override
@SuppressWarnings("java:S4348")
// since the length is computed, it's not simple to make this re-iterable
public Iterator<Row<Measurement>> iterator() {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -17,7 +17,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.opennms.newts.aggregate.Utils.assertAttributes;
import static org.opennms.newts.aggregate.Utils.mapFor;
import static org.opennms.newts.api.Timestamp.fromEpochSeconds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -16,7 +16,7 @@
package org.opennms.newts.aggregate;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.opennms.newts.aggregate.Utils.assertAttributes;
import static org.opennms.newts.aggregate.Utils.mapFor;
import static org.opennms.newts.api.Timestamp.fromEpochSeconds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -23,7 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -23,8 +23,6 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Throwables;


public class BlockingThreadPoolExecutor extends ThreadPoolExecutor {

Expand All @@ -46,7 +44,7 @@ public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
}
}
catch (InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -55,7 +53,7 @@ public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
}

public BlockingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>((int)(maximumPoolSize*1.5)));
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>((int)(maximumPoolSize*1.5)));
this.setRejectedExecutionHandler(new BlockingExecutionHandler());
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/test/java/org/opennms/newts/api/AbsoluteTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -18,7 +18,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

Expand Down
4 changes: 2 additions & 2 deletions api/src/test/java/org/opennms/newts/api/CounterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -17,7 +17,7 @@


import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

Expand Down
4 changes: 2 additions & 2 deletions api/src/test/java/org/opennms/newts/api/DeriveTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -18,7 +18,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016, The OpenNMS Group
* Copyright 2016-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -19,7 +19,6 @@
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.insertInto;
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal;
import static com.datastax.oss.driver.api.querybuilder.select.Selector.ttl;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
Expand All @@ -28,7 +27,6 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -64,6 +62,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

@SuppressWarnings("java:S4738") // we only use Guava Optional internally, it isn't passed around
public class CassandraIndexer implements Indexer {
private static final Logger LOG = LoggerFactory.getLogger(CassandraIndexer.class);

Expand Down Expand Up @@ -142,9 +141,9 @@ public void update(Collection<Sample> samples) {
}

// Order matters here; We want the cache updated only after a successful Cassandra write.
for (Context context : cacheQueue.keySet()) {
for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
m_cache.merge(context, entry.getKey(), entry.getValue());
for (final Entry<Context,Map<Resource,ResourceMetadata>> queueEntry : cacheQueue.entrySet()) {
for (final Map.Entry<Resource, ResourceMetadata> entry : queueEntry.getValue().entrySet()) {
m_cache.merge(queueEntry.getKey(), entry.getKey(), entry.getValue());
}
}
} finally {
Expand All @@ -155,10 +154,11 @@ public void update(Collection<Sample> samples) {
}
}

private List<Statement> toStatements(Set<StatementGenerator> generators) {
List<Statement> statementsToExecute = Lists.newArrayList();
@SuppressWarnings("rawtypes")
private List<Statement<?>> toStatements(Set<StatementGenerator> generators) {
List<Statement<?>> statementsToExecute = Lists.newArrayList();

Map<String, List<BatchableStatement>> statementsByKey = Maps.newHashMap();
Map<String, List<BatchableStatement<?>>> statementsByKey = Maps.newHashMap();
for (StatementGenerator generator : generators) {
BatchableStatement statement = generator.toStatement()
.setConsistencyLevel(m_contextConfigurations.getWriteConsistency(generator.getContext()));
Expand All @@ -170,7 +170,7 @@ private List<Statement> toStatements(Set<StatementGenerator> generators) {
}

// Group these by key
List<BatchableStatement> statementsForKey = statementsByKey.get(key);
List<BatchableStatement<?>> statementsForKey = statementsByKey.get(key);
if (statementsForKey == null) {
statementsForKey = Lists.newArrayList();
statementsByKey.put(key, statementsForKey);
Expand All @@ -179,8 +179,8 @@ private List<Statement> toStatements(Set<StatementGenerator> generators) {
}

// Consolidate the grouped statements into batches
for (List<BatchableStatement> statementsForKey: statementsByKey.values()) {
for (List<BatchableStatement> partition : Lists.partition(statementsForKey, m_options.getMaxBatchSize())) {
for (List<BatchableStatement<?>> statementsForKey: statementsByKey.values()) {
for (List<BatchableStatement<?>> partition : Lists.partition(statementsForKey, m_options.getMaxBatchSize())) {
BatchStatementBuilder builder = BatchStatement.builder(DefaultBatchType.UNLOGGED);
for (BatchableStatement statement : partition) {
builder.addStatement(statement);
Expand Down Expand Up @@ -454,7 +454,7 @@ public boolean equals(Object obj) {
}
}

private static abstract class KeyValuePairInsert implements StatementGenerator {
private abstract static class KeyValuePairInsert implements StatementGenerator {
protected final Context m_context;
protected final String m_resourceId;
protected final String m_field;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand Down Expand Up @@ -53,7 +53,6 @@
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.querybuilder.select.Select;
import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -133,7 +132,7 @@ public SearchResults search(Context context, Query query, boolean populateMetric
Resource resource = attrs.size() > 0 ? new Resource(id, Optional.of(attrs)) : new Resource(id);
searchResults.addResult(resource, metrics);
} catch (ExecutionException|InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}
}
Expand All @@ -150,7 +149,7 @@ public Map<String, String> getResourceAttributes(Context context, String resourc
ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
return getResourceAttributesFromResults(fetchResourceAttributes(context, resourceId, readConsistency));
} catch (ExecutionException|InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand All @@ -159,7 +158,7 @@ public Collection<String> getMetricNames(Context context, String resourceId) {
ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
return getMetricNamesFromResults(fetchMetricNames(context, resourceId, readConsistency));
} catch (ExecutionException|InterruptedException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -18,7 +18,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeast;
Expand Down Expand Up @@ -276,6 +276,7 @@ public void testCache() {
}

@Test
@SuppressWarnings("java:S2925") // Thread.sleep() is used to track the cache
public void testCacheExpiring() throws Exception {
final MetricRegistry registry = new MetricRegistry();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015, The OpenNMS Group
* Copyright 2015-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -20,7 +20,7 @@
import com.google.common.collect.Lists;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014, The OpenNMS Group
* Copyright 2014-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -18,7 +18,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;
import org.opennms.newts.api.Context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015, The OpenNMS Group
* Copyright 2015-2024, The OpenNMS Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
Expand All @@ -20,7 +20,7 @@
import com.google.common.collect.Lists;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.List;

Expand Down
Loading

0 comments on commit 51012ab

Please sign in to comment.