nextSnapshotChunk.length);
int nextChunkIndex = installSnapshotState.incrementChunkIndex();
- Optional<ServerConfigurationPayload> serverConfig = Optional.empty();
+ ServerConfigurationPayload serverConfig = null;
if (installSnapshotState.isLastChunk(nextChunkIndex)) {
- serverConfig = Optional.ofNullable(context.getPeerServerInfo(true));
+ serverConfig = context.getPeerServerInfo(true);
}
sendSnapshotChunk(followerActor, followerLogInfo, nextSnapshotChunk, nextChunkIndex, serverConfig);
private void sendSnapshotChunk(final ActorSelection followerActor, final FollowerLogInformation followerLogInfo,
final byte[] snapshotChunk, final int chunkIndex,
- final Optional<ServerConfigurationPayload> serverConfig) {
+ final @Nullable ServerConfigurationPayload serverConfig) {
LeaderInstallSnapshotState installSnapshotState = followerLogInfo.getInstallSnapshotState();
installSnapshotState.startChunkTimer();
installSnapshot.getLastIncludedIndex(), installSnapshot.getLastIncludedTerm(),
installSnapshot.getLastIncludedIndex(), installSnapshot.getLastIncludedTerm(),
context.getTermInformation().getCurrentTerm(), context.getTermInformation().getVotedFor(),
- installSnapshot.getServerConfig().orElse(null));
+ installSnapshot.serverConfig());
final var applySnapshotCallback = new ApplySnapshot.Callback() {
@Override
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import java.util.Optional;
import java.util.OptionalInt;
import org.opendaylight.controller.cluster.raft.RaftVersions;
import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
if (lastChunkHashCode.isPresent()) {
flags |= LAST_CHUNK_HASHCODE;
}
- final var serverConfig = installSnapshot.getServerConfig();
- if (serverConfig.isPresent()) {
+ final var serverConfig = installSnapshot.serverConfig();
+ if (serverConfig != null) {
flags |= SERVER_CONFIG;
}
if (lastChunkHashCode.isPresent()) {
out.writeInt(lastChunkHashCode.orElseThrow());
}
- if (serverConfig.isPresent()) {
- out.writeObject(serverConfig.orElseThrow());
+ if (serverConfig != null) {
+ out.writeObject(serverConfig);
}
out.writeObject(installSnapshot.getData());
OptionalInt lastChunkHashCode = getFlag(flags, LAST_CHUNK_HASHCODE) ? OptionalInt.of(in.readInt())
: OptionalInt.empty();
- Optional<ServerConfigurationPayload> serverConfig = getFlag(flags, SERVER_CONFIG)
- ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
+ ServerConfigurationPayload serverConfig = getFlag(flags, SERVER_CONFIG)
+ ? requireNonNull((ServerConfigurationPayload) in.readObject()) : null;
byte[] data = (byte[])in.readObject();
*/
package org.opendaylight.controller.cluster.raft.messages;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import java.util.Optional;
import java.util.OptionalInt;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.controller.cluster.raft.RaftVersions;
import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
private final int totalChunks;
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
private final OptionalInt lastChunkHashCode;
- @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
- private final Optional<ServerConfigurationPayload> serverConfig;
+ private final @Nullable ServerConfigurationPayload serverConfig;
private final short recipientRaftVersion;
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = """
large.""")
public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
- final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
+ final OptionalInt lastChunkHashCode, final @Nullable ServerConfigurationPayload serverConfig,
final short recipientRaftVersion) {
super(term);
this.leaderId = leaderId;
final long lastIncludedTerm, final byte[] data, final int chunkIndex,
final int totalChunks) {
this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
- Optional.empty(), RaftVersions.CURRENT_VERSION);
+ null, RaftVersions.CURRENT_VERSION);
}
public String getLeaderId() {
return lastChunkHashCode;
}
- public Optional<ServerConfigurationPayload> getServerConfig() {
+ public @Nullable ServerConfigurationPayload serverConfig() {
return serverConfig;
}
return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
+ lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
+ ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
- + ", serverConfig=" + serverConfig.orElse(null) + "]";
+ + ", serverConfig=" + serverConfig + "]";
}
@Override
out.writeInt(installSnapshot.lastChunkHashCode.orElseThrow());
}
- out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
- if (installSnapshot.serverConfig.isPresent()) {
- out.writeObject(installSnapshot.serverConfig.orElseThrow());
+ out.writeByte(installSnapshot.serverConfig != null ? 1 : 0);
+ if (installSnapshot.serverConfig != null) {
+ out.writeObject(installSnapshot.serverConfig);
}
out.writeObject(installSnapshot.data);
int totalChunks = in.readInt();
OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
- Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
- ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
+ ServerConfigurationPayload serverConfig;
+ if (in.readByte() == 1) {
+ serverConfig = requireNonNull((ServerConfigurationPayload) in.readObject());
+ } else {
+ serverConfig = null;
+ }
byte[] data = (byte[])in.readObject();
import static org.junit.Assert.assertEquals;
import java.util.List;
-import java.util.Optional;
import java.util.OptionalInt;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Test;
var serverConfig = new ServerConfigurationPayload(List.of(
new ServerInfo("leader", true), new ServerInfo("follower", false)));
assertInstallSnapshot(fullSize, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.of(54321),
- Optional.of(serverConfig), raftVersion));
+ serverConfig, raftVersion));
assertInstallSnapshot(emptySize, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.empty(),
- Optional.empty(), raftVersion));
+ null, raftVersion));
}
private static void assertInstallSnapshot(final int expectedSize, final InstallSnapshot expected) {
actual.getLastChunkHashCode());
}
- assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
- actual.getServerConfig().isPresent());
- if (expected.getServerConfig().isPresent()) {
- assertEquals("getServerConfig", expected.getServerConfig().orElseThrow().getServerConfig(),
- actual.getServerConfig().orElseThrow().getServerConfig());
- }
+ assertEquals(expected.serverConfig(), actual.serverConfig());
}
}