* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.controller.cluster.raft;
+import static java.util.Objects.requireNonNull;
+
import akka.actor.ActorContext;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.Props;
import akka.cluster.Cluster;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import java.util.ArrayList;
+import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.LongSupplier;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.controller.cluster.DataPersistenceProvider;
import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
private final ActorContext context;
+ private final @NonNull Executor executor;
+
private final String id;
private final ElectionTerm termInformation;
private RaftActorLeadershipTransferCohort leadershipTransferCohort;
public RaftActorContextImpl(final ActorRef actor, final ActorContext context, final String id,
- @Nonnull final ElectionTerm termInformation, final long commitIndex, final long lastApplied,
- @Nonnull final Map<String, String> peerAddresses,
- @Nonnull final ConfigParams configParams, @Nonnull final DataPersistenceProvider persistenceProvider,
- @Nonnull final Consumer<ApplyState> applyStateConsumer, @Nonnull final Logger logger) {
+ final @NonNull ElectionTerm termInformation, final long commitIndex, final long lastApplied,
+ final @NonNull Map<String, String> peerAddresses,
+ final @NonNull ConfigParams configParams, final @NonNull DataPersistenceProvider persistenceProvider,
+ final @NonNull Consumer<ApplyState> applyStateConsumer, final @NonNull Logger logger,
+ final @NonNull Executor executor) {
this.actor = actor;
this.context = context;
this.id = id;
- this.termInformation = Preconditions.checkNotNull(termInformation);
+ this.termInformation = requireNonNull(termInformation);
+ this.executor = requireNonNull(executor);
this.commitIndex = commitIndex;
this.lastApplied = lastApplied;
- this.configParams = Preconditions.checkNotNull(configParams);
- this.persistenceProvider = Preconditions.checkNotNull(persistenceProvider);
- this.log = Preconditions.checkNotNull(logger);
- this.applyStateConsumer = Preconditions.checkNotNull(applyStateConsumer);
+ this.configParams = requireNonNull(configParams);
+ this.persistenceProvider = requireNonNull(persistenceProvider);
+ log = requireNonNull(logger);
+ this.applyStateConsumer = requireNonNull(applyStateConsumer);
fileBackedOutputStreamFactory = new FileBackedOutputStreamFactory(
configParams.getFileBackedStreamingThreshold(), configParams.getTempFileDirectory());
- for (Map.Entry<String, String> e: Preconditions.checkNotNull(peerAddresses).entrySet()) {
+ for (Map.Entry<String, String> e : requireNonNull(peerAddresses).entrySet()) {
peerInfoMap.put(e.getKey(), new PeerInfo(e.getKey(), e.getValue(), VotingState.VOTING));
}
}
return actor;
}
+ @Override
+ public final Executor getExecutor() {
+ return executor;
+ }
+
@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public Optional<Cluster> getCluster() {
cluster = Optional.of(Cluster.get(getActorSystem()));
} catch (Exception e) {
// An exception means there's no cluster configured. This will only happen in unit tests.
- log.debug("{}: Could not obtain Cluster: {}", getId(), e);
+ log.debug("{}: Could not obtain Cluster", getId(), e);
cluster = Optional.empty();
}
}
@Override
public Logger getLogger() {
- return this.log;
+ return log;
}
@Override
public void updatePeerIds(final ServerConfigurationPayload serverConfig) {
votingMember = true;
boolean foundSelf = false;
- Set<String> currentPeers = new HashSet<>(this.getPeerIds());
+ Set<String> currentPeers = new HashSet<>(getPeerIds());
for (ServerInfo server : serverConfig.getServerConfig()) {
if (getId().equals(server.getId())) {
foundSelf = true;
} else {
VotingState votingState = server.isVoting() ? VotingState.VOTING : VotingState.NON_VOTING;
if (!currentPeers.contains(server.getId())) {
- this.addToPeers(server.getId(), null, votingState);
+ addToPeers(server.getId(), null, votingState);
} else {
- this.getPeerInfo(server.getId()).setVotingState(votingState);
+ getPeerInfo(server.getId()).setVotingState(votingState);
currentPeers.remove(server.getId());
}
}
}
for (String peerIdToRemove : currentPeers) {
- this.removePeer(peerIdToRemove);
+ removePeer(peerIdToRemove);
}
if (!foundSelf) {
@Override
public void setDynamicServerConfigurationInUse() {
- this.dynamicServerConfiguration = true;
+ dynamicServerConfiguration = true;
}
@Override
if (!isDynamicServerConfigurationInUse()) {
return null;
}
- Collection<PeerInfo> peers = getPeers();
- List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
- for (PeerInfo peer: peers) {
+ final var peers = getPeers();
+ final var newConfig = ImmutableList.<ServerInfo>builderWithExpectedSize(peers.size() + (includeSelf ? 1 : 0));
+ for (PeerInfo peer : peers) {
newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
}
newConfig.add(new ServerInfo(getId(), votingMember));
}
- return new ServerConfigurationPayload(newConfig);
+ return new ServerConfigurationPayload(newConfig.build());
}
@Override
}
void setCurrentBehavior(final RaftActorBehavior behavior) {
- this.currentBehavior = Preconditions.checkNotNull(behavior);
+ currentBehavior = requireNonNull(behavior);
}
@Override
}
@Override
- @Nullable
public RaftActorLeadershipTransferCohort getRaftActorLeadershipTransferCohort() {
return leadershipTransferCohort;
}
@Override
@SuppressWarnings("checkstyle:hiddenField")
- public void setRaftActorLeadershipTransferCohort(
- @Nullable final RaftActorLeadershipTransferCohort leadershipTransferCohort) {
+ public void setRaftActorLeadershipTransferCohort(final RaftActorLeadershipTransferCohort leadershipTransferCohort) {
this.leadershipTransferCohort = leadershipTransferCohort;
}
}