From d610d46f30872ebdea65686d0ef8535ac251f582 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 3 Dec 2022 13:24:16 +0100 Subject: [PATCH] Fix more Sonar warnings Ignored exceptions, mergeable if statements and similar. Change-Id: Iea262b7c410cfde16fd4f101d3c7c8195ff1ea1f Signed-off-by: Robert Varga --- .../impl/NtfbenchBlockingProducer.java | 8 ++- .../DataStoreAppConfigDefaultXMLReader.java | 5 +- .../owner/supervisor/OwnerSupervisor.java | 4 +- .../controller/cluster/raft/RaftActor.java | 3 +- .../raft/behaviors/AbstractLeader.java | 50 +++++++++---------- .../cluster/raft/behaviors/Follower.java | 3 +- .../behaviors/LeaderInstallSnapshotState.java | 4 +- .../raft/behaviors/SnapshotTracker.java | 11 ++-- .../AbstractNormalizedNodePruner.java | 2 +- .../utils/transformer/UintAdaptingPruner.java | 2 +- 10 files changed, 47 insertions(+), 45 deletions(-) diff --git a/benchmark/ntfbenchmark/src/main/java/ntfbenchmark/impl/NtfbenchBlockingProducer.java b/benchmark/ntfbenchmark/src/main/java/ntfbenchmark/impl/NtfbenchBlockingProducer.java index 021369acb0..ee77a6c342 100644 --- a/benchmark/ntfbenchmark/src/main/java/ntfbenchmark/impl/NtfbenchBlockingProducer.java +++ b/benchmark/ntfbenchmark/src/main/java/ntfbenchmark/impl/NtfbenchBlockingProducer.java @@ -8,8 +8,11 @@ package ntfbenchmark.impl; import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class NtfbenchBlockingProducer extends AbstractNtfbenchProducer { + private static final Logger LOG = LoggerFactory.getLogger(NtfbenchBlockingProducer.class); public NtfbenchBlockingProducer(final NotificationPublishService publishService, final int iterations, final int payloadSize) { @@ -22,12 +25,13 @@ public class NtfbenchBlockingProducer extends AbstractNtfbenchProducer { int ntfOk = 0; int ntfError = 0; - for (int i = 0; i < this.iterations; i++) { + for (int i = 0; i < iterations; i++) { try { - this.publishService.putNotification(this.ntf); + publishService.putNotification(ntf); ntfOk++; } catch (final Exception e) { ntfError++; + LOG.debug("Failed to push notification", e); } } diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/DataStoreAppConfigDefaultXMLReader.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/DataStoreAppConfigDefaultXMLReader.java index 0960fa83c3..44b11606d7 100644 --- a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/DataStoreAppConfigDefaultXMLReader.java +++ b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/DataStoreAppConfigDefaultXMLReader.java @@ -14,7 +14,6 @@ import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.Optional; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.mdsal.dom.api.DOMSchemaService; @@ -93,8 +92,8 @@ public class DataStoreAppConfigDefaultXMLReader { return Resources.getResource(testClass, defaultAppConfigFileName); } - public T createDefaultInstance() throws ConfigXMLReaderException, ParserConfigurationException, XMLStreamException, - IOException, SAXException, URISyntaxException { + public T createDefaultInstance() throws ConfigXMLReaderException, XMLStreamException, IOException, SAXException, + URISyntaxException { return createDefaultInstance(dataSchema -> { throw new IllegalArgumentException( "Failed to read XML (not creating model from defaults as runtime would, for better clarity in tests)"); diff --git a/opendaylight/md-sal/eos-dom-akka/src/main/java/org/opendaylight/controller/eos/akka/owner/supervisor/OwnerSupervisor.java b/opendaylight/md-sal/eos-dom-akka/src/main/java/org/opendaylight/controller/eos/akka/owner/supervisor/OwnerSupervisor.java index 7a4f91ffec..1e2a41beca 100644 --- a/opendaylight/md-sal/eos-dom-akka/src/main/java/org/opendaylight/controller/eos/akka/owner/supervisor/OwnerSupervisor.java +++ b/opendaylight/md-sal/eos-dom-akka/src/main/java/org/opendaylight/controller/eos/akka/owner/supervisor/OwnerSupervisor.java @@ -84,7 +84,7 @@ public final class OwnerSupervisor extends AbstractSupervisor { // Our own clock implementation so we do not have to rely on synchronized clocks. This basically functions as an // increasing counter which is fine for our needs as we only ever have a single writer since t supervisor is // running in a cluster-singleton - private final LWWRegister.Clock clock = (currentTimestamp, value) -> currentTimestamp + 1; + private static final LWWRegister.Clock CLOCK = (currentTimestamp, value) -> currentTimestamp + 1; private final Cluster cluster; private final SelfUniqueAddress node; @@ -371,7 +371,7 @@ public final class OwnerSupervisor extends AbstractSupervisor { new LWWRegister<>(node.uniqueAddress(), candidate, 0), Replicator.writeLocal(), askReplyTo, - register -> register.withValue(node, candidate, clock)), + register -> register.withValue(node, candidate, CLOCK)), OwnerChanged::new); } diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java index a1e94bd836..d10cfae219 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java @@ -103,8 +103,7 @@ import org.opendaylight.yangtools.concepts.Immutable; * */ public abstract class RaftActor extends AbstractUntypedPersistentActor { - - private static final long APPLY_STATE_DELAY_THRESHOLD_IN_NANOS = TimeUnit.MILLISECONDS.toNanos(50L); // 50 millis + private static final long APPLY_STATE_DELAY_THRESHOLD_IN_NANOS = TimeUnit.MILLISECONDS.toNanos(50); /** * This context should NOT be passed directly to any other actor it is diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java index bdd3262e0a..f97343f736 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java @@ -493,31 +493,31 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { return this; } - if (message instanceof RaftRPC rpc) { - // If RPC request or response contains term T > currentTerm: - // set currentTerm = T, convert to follower (§5.1) - // This applies to all RPC messages and responses - if (rpc.getTerm() > context.getTermInformation().getCurrentTerm() && shouldUpdateTerm(rpc)) { - log.info("{}: Term {} in \"{}\" message is greater than leader's term {} - switching to Follower", - logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm()); - - context.getTermInformation().updateAndPersist(rpc.getTerm(), null); - - // This is a special case. Normally when stepping down as leader we don't process and reply to the - // RaftRPC as per raft. But if we're in the process of transferring leadership and we get a - // RequestVote, process the RequestVote before switching to Follower. This enables the requesting - // candidate node to be elected the leader faster and avoids us possibly timing out in the Follower - // state and starting a new election and grabbing leadership back before the other candidate node can - // start a new election due to lack of responses. This case would only occur if there isn't a majority - // of other nodes available that can elect the requesting candidate. Since we're transferring - // leadership, we should make every effort to get the requesting node elected. - if (rpc instanceof RequestVote requestVote && context.getRaftActorLeadershipTransferCohort() != null) { - log.debug("{}: Leadership transfer in progress - processing RequestVote", logName()); - requestVote(sender, requestVote); - } - - return internalSwitchBehavior(RaftState.Follower); + // If RPC request or response contains term T > currentTerm: + // set currentTerm = T, convert to follower (§5.1) + // This applies to all RPC messages and responses + if (message instanceof RaftRPC rpc && rpc.getTerm() > context.getTermInformation().getCurrentTerm() + && shouldUpdateTerm(rpc)) { + + log.info("{}: Term {} in \"{}\" message is greater than leader's term {} - switching to Follower", + logName(), rpc.getTerm(), rpc, context.getTermInformation().getCurrentTerm()); + + context.getTermInformation().updateAndPersist(rpc.getTerm(), null); + + // This is a special case. Normally when stepping down as leader we don't process and reply to the + // RaftRPC as per raft. But if we're in the process of transferring leadership and we get a + // RequestVote, process the RequestVote before switching to Follower. This enables the requesting + // candidate node to be elected the leader faster and avoids us possibly timing out in the Follower + // state and starting a new election and grabbing leadership back before the other candidate node can + // start a new election due to lack of responses. This case would only occur if there isn't a majority + // of other nodes available that can elect the requesting candidate. Since we're transferring + // leadership, we should make every effort to get the requesting node elected. + if (rpc instanceof RequestVote requestVote && context.getRaftActorLeadershipTransferCohort() != null) { + log.debug("{}: Leadership transfer in progress - processing RequestVote", logName()); + requestVote(sender, requestVote); } + + return internalSwitchBehavior(RaftState.Follower); } if (message instanceof SendHeartBeat) { @@ -979,7 +979,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior { } catch (IOException e) { log.warn("{}: Unable to send chunk: {}/{}. Reseting snapshot progress. Snapshot state: {}", logName(), installSnapshotState.getChunkIndex(), installSnapshotState.getTotalChunks(), - installSnapshotState); + installSnapshotState, e); installSnapshotState.reset(); } } diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java index f4a01cfd32..69428058cd 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java @@ -671,8 +671,7 @@ public class Follower extends AbstractRaftActorBehavior { } catch (IOException e) { log.debug("{}: Exception in InstallSnapshot of follower", logName(), e); - sender.tell(new InstallSnapshotReply(currentTerm(), context.getId(), - -1, false), actor()); + sender.tell(new InstallSnapshotReply(currentTerm(), context.getId(), -1, false), actor()); closeSnapshotTracker(); } diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/LeaderInstallSnapshotState.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/LeaderInstallSnapshotState.java index e7c76bacbe..68e82dc9a1 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/LeaderInstallSnapshotState.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/LeaderInstallSnapshotState.java @@ -49,7 +49,7 @@ public final class LeaderInstallSnapshotState implements AutoCloseable { private int nextChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE; private long snapshotSize; private InputStream snapshotInputStream; - private Stopwatch chunkTimer = Stopwatch.createUnstarted(); + private final Stopwatch chunkTimer = Stopwatch.createUnstarted(); private byte[] currentChunk = null; LeaderInstallSnapshotState(final int snapshotChunkSize, final String logName) { @@ -198,7 +198,7 @@ public final class LeaderInstallSnapshotState implements AutoCloseable { try { snapshotInputStream.close(); } catch (IOException e) { - LOG.warn("{}: Error closing snapshot stream", logName); + LOG.warn("{}: Error closing snapshot stream", logName, e); } snapshotInputStream = null; diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java index 1538bed74c..9e920bd41e 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java @@ -48,11 +48,12 @@ class SnapshotTracker implements AutoCloseable { * @param lastChunkHashCode the optional hash code for the chunk * @return true if this is the last chunk is received * @throws InvalidChunkException if the chunk index is invalid or out of order + * @throws IOException if there is a problem writing to the stream */ boolean addChunk(final int chunkIndex, final byte[] chunk, final OptionalInt maybeLastChunkHashCode) - throws InvalidChunkException, IOException { + throws IOException { log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}", - chunkIndex, lastChunkIndex, count, this.lastChunkHashCode); + chunkIndex, lastChunkIndex, count, lastChunkHashCode); if (sealed) { throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex @@ -63,9 +64,9 @@ class SnapshotTracker implements AutoCloseable { throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex); } - if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.getAsInt() != this.lastChunkHashCode) { + if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.getAsInt() != lastChunkHashCode) { throw new InvalidChunkException("The hash code of the recorded last chunk does not match " - + "the senders hash code, expected " + this.lastChunkHashCode + " was " + + "the senders hash code, expected " + lastChunkHashCode + " was " + maybeLastChunkHashCode.getAsInt()); } @@ -74,7 +75,7 @@ class SnapshotTracker implements AutoCloseable { count += chunk.length; sealed = chunkIndex == totalChunks; lastChunkIndex = chunkIndex; - this.lastChunkHashCode = Arrays.hashCode(chunk); + lastChunkHashCode = Arrays.hashCode(chunk); return sealed; } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/AbstractNormalizedNodePruner.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/AbstractNormalizedNodePruner.java index 13500c17c0..965b5e1ba9 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/AbstractNormalizedNodePruner.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/AbstractNormalizedNodePruner.java @@ -181,7 +181,7 @@ abstract class AbstractNormalizedNodePruner implements NormalizedNodeStreamWrite } } - Object translateScalar(final DataSchemaContextNode context, final Object value) throws IOException { + Object translateScalar(final DataSchemaContextNode context, final Object value) { // Default is pass-through return value; } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/UintAdaptingPruner.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/UintAdaptingPruner.java index b3e02a4761..ac6e70c4a4 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/UintAdaptingPruner.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/transformer/UintAdaptingPruner.java @@ -133,7 +133,7 @@ final class UintAdaptingPruner extends ReusableNormalizedNodePruner { } @Override - Object translateScalar(final DataSchemaContextNode context, final Object value) throws IOException { + Object translateScalar(final DataSchemaContextNode context, final Object value) { final DataSchemaNode schema = context.getDataSchemaNode(); return schema instanceof TypedDataSchemaNode ? adaptValue(((TypedDataSchemaNode) schema).getType(), value) : value; -- 2.36.6