return null;
}
+ @SuppressWarnings("unchecked")
@Override
public Snapshot.State deserializeSnapshot(ByteSource snapshotBytes) {
try {
- return deserializePreCarbonSnapshot(snapshotBytes.read());
+ return new MapState((Map<String, String>) SerializationUtils.deserialize(snapshotBytes.read()));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
- @SuppressWarnings("unchecked")
- @Override
- public Snapshot.State deserializePreCarbonSnapshot(byte[] from) {
- return new MapState((Map<String, String>) SerializationUtils.deserialize(from));
- }
-
private static class MapState implements Snapshot.State {
private static final long serialVersionUID = 1L;
*/
package org.opendaylight.controller.cluster.raft;
-import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
*/
@Nullable
Snapshot getRestoreFromSnapshot();
-
- /**
- * This method is called during recovery to de-serialize a snapshot that was persisted in the pre-Carbon format.
- *
- * @param from the snaphot bytes
- * @return a Snapshot.State instance
- */
- @Deprecated
- @Nonnull
- Snapshot.State deserializePreCarbonSnapshot(byte [] from);
}
initRecoveryTimer();
- Object snapshotObj = offer.snapshot();
- Snapshot snapshot;
- if (snapshotObj instanceof org.opendaylight.controller.cluster.raft.Snapshot) {
- org.opendaylight.controller.cluster.raft.Snapshot legacy =
- (org.opendaylight.controller.cluster.raft.Snapshot)snapshotObj;
- snapshot = Snapshot.create(cohort.deserializePreCarbonSnapshot(legacy.getState()),
- legacy.getUnAppliedEntries(), legacy.getLastIndex(), legacy.getLastTerm(),
- legacy.getLastAppliedIndex(), legacy.getLastAppliedTerm(),
- legacy.getElectionTerm(), legacy.getElectionVotedFor(), legacy.getServerConfiguration());
- hasMigratedDataRecovered = true;
- } else {
- snapshot = (Snapshot) offer.snapshot();
- }
+ Snapshot snapshot = (Snapshot) offer.snapshot();
for (ReplicatedLogEntry entry: snapshot.getUnAppliedEntries()) {
if (isMigratedPayload(entry)) {
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.io.Serializable;
-import java.util.List;
-import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
-
-/**
- * Represents a snapshot of the raft data.
- *
- * @author Moiz Raja
- * @author Thomas Pantelis
- *
- * @deprecated Use {@link org.opendaylight.controller.cluster.raft.persisted.Snapshot} instead.
- */
-@Deprecated
-public class Snapshot implements Serializable {
- private static final long serialVersionUID = -8298574936724056236L;
-
- private final byte[] state;
- private final List<ReplicatedLogEntry> unAppliedEntries;
- private final long lastIndex;
- private final long lastTerm;
- private final long lastAppliedIndex;
- private final long lastAppliedTerm;
- private final long electionTerm;
- private final String electionVotedFor;
- private final ServerConfigurationPayload serverConfig;
-
- private Snapshot(byte[] state, List<ReplicatedLogEntry> unAppliedEntries, long lastIndex, long lastTerm,
- long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
- ServerConfigurationPayload serverConfig) {
- this.state = state;
- this.unAppliedEntries = unAppliedEntries;
- this.lastIndex = lastIndex;
- this.lastTerm = lastTerm;
- this.lastAppliedIndex = lastAppliedIndex;
- this.lastAppliedTerm = lastAppliedTerm;
- this.electionTerm = electionTerm;
- this.electionVotedFor = electionVotedFor;
- this.serverConfig = serverConfig;
- }
-
- public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
- long lastAppliedIndex, long lastAppliedTerm) {
- return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, -1, null, null);
- }
-
- public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
- long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor) {
- return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
- electionTerm, electionVotedFor, null);
- }
-
- public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
- long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
- ServerConfigurationPayload serverConfig) {
- return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
- electionTerm, electionVotedFor, serverConfig);
- }
-
- @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
- + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- public byte[] getState() {
- return state;
- }
-
- public List<ReplicatedLogEntry> getUnAppliedEntries() {
- return unAppliedEntries;
- }
-
- public long getLastTerm() {
- return lastTerm;
- }
-
- public long getLastAppliedIndex() {
- return lastAppliedIndex;
- }
-
- public long getLastAppliedTerm() {
- return lastAppliedTerm;
- }
-
- public long getLastIndex() {
- return this.lastIndex;
- }
-
- public long getElectionTerm() {
- return electionTerm;
- }
-
-
- public String getElectionVotedFor() {
- return electionVotedFor;
- }
-
- public ServerConfigurationPayload getServerConfiguration() {
- return serverConfig;
- }
-
- @Override
- public String toString() {
- return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
- + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
- + ", state size=" + state.length + ", electionTerm=" + electionTerm + ", electionVotedFor="
- + electionVotedFor + ", ServerConfigPayload=" + serverConfig + "]";
- }
-}
import akka.testkit.TestActorRef;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.util.concurrent.Uninterruptibles;
import java.io.OutputStream;
-import java.io.Serializable;
-import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
-import org.apache.commons.lang3.SerializationUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.controller.cluster.raft.MockRaftActor.MockSnapshotState;
-import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
import org.opendaylight.controller.cluster.raft.persisted.ByteState;
-import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
-import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
TEST_LOG.info("testNoSnapshotAfterStartupWithNoMigratedMessages ending");
}
- @Test
- public void testSnapshotAfterStartupWithMigratedSnapshot() throws Exception {
- TEST_LOG.info("testSnapshotAfterStartupWithMigratedSnapshot starting");
-
- String persistenceId = factory.generateActorId("test-actor-");
-
- List<Object> snapshotData = Arrays.asList(new MockPayload("1"));
- final MockSnapshotState snapshotState = new MockSnapshotState(snapshotData);
-
- org.opendaylight.controller.cluster.raft.Snapshot legacy = org.opendaylight.controller.cluster.raft.Snapshot
- .create(SerializationUtils.serialize((Serializable) snapshotData),
- Arrays.asList(new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload"))),
- 6, 2, 5, 1, 3, "member-1", new ServerConfigurationPayload(Arrays.asList(
- new ServerInfo(persistenceId, true), new ServerInfo("2", false))));
- InMemorySnapshotStore.addSnapshot(persistenceId, legacy);
-
- doTestSnapshotAfterStartupWithMigratedMessage(persistenceId, true, snapshot -> {
- assertEquals("getLastIndex", legacy.getLastIndex(), snapshot.getLastIndex());
- assertEquals("getLastTerm", legacy.getLastTerm(), snapshot.getLastTerm());
- assertEquals("getLastAppliedIndex", legacy.getLastAppliedIndex(), snapshot.getLastAppliedIndex());
- assertEquals("getLastAppliedTerm", legacy.getLastAppliedTerm(), snapshot.getLastAppliedTerm());
- assertEquals("getState", snapshotState, snapshot.getState());
- assertEquals("Unapplied entries size", legacy.getUnAppliedEntries().size(),
- snapshot.getUnAppliedEntries().size());
- assertEquals("Unapplied entry term", legacy.getUnAppliedEntries().get(0).getTerm(),
- snapshot.getUnAppliedEntries().get(0).getTerm());
- assertEquals("Unapplied entry index", legacy.getUnAppliedEntries().get(0).getIndex(),
- snapshot.getUnAppliedEntries().get(0).getIndex());
- assertEquals("Unapplied entry data", legacy.getUnAppliedEntries().get(0).getData(),
- snapshot.getUnAppliedEntries().get(0).getData());
- assertEquals("getElectionVotedFor", legacy.getElectionVotedFor(), snapshot.getElectionVotedFor());
- assertEquals("getElectionTerm", legacy.getElectionTerm(), snapshot.getElectionTerm());
- assertEquals("getServerConfiguration", Sets.newHashSet(legacy.getServerConfiguration().getServerConfig()),
- Sets.newHashSet(snapshot.getServerConfiguration().getServerConfig()));
- }, snapshotState);
-
- TEST_LOG.info("testSnapshotAfterStartupWithMigratedSnapshot ending");
- }
-
@SuppressWarnings("checkstyle:IllegalCatch")
private TestActorRef<MockRaftActor> doTestSnapshotAfterStartupWithMigratedMessage(String id, boolean persistent,
Consumer<Snapshot> snapshotVerifier, final State snapshotState) {
}
}
- @Override
- public Snapshot.State deserializePreCarbonSnapshot(byte[] from) {
- return new MockSnapshotState(SerializationUtils.deserialize(from));
- }
-
@Override
protected void onStateChanged() {
actorDelegate.onStateChanged();
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
-import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import akka.persistence.SnapshotMetadata;
import akka.persistence.SnapshotOffer;
import com.google.common.collect.Sets;
-import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
-import java.util.List;
-import org.apache.commons.lang3.SerializationUtils;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
verify(mockCohort).applyRecoverySnapshot(snapshotState);
}
- @Deprecated
- @Test
- public void testOnSnapshotOfferWithPreCarbonSnapshot() {
-
- ReplicatedLogEntry unAppliedEntry1 = new SimpleReplicatedLogEntry(4, 1,
- new MockRaftActorContext.MockPayload("4", 4));
-
- ReplicatedLogEntry unAppliedEntry2 = new SimpleReplicatedLogEntry(5, 1,
- new MockRaftActorContext.MockPayload("5", 5));
-
- long lastAppliedDuringSnapshotCapture = 3;
- long lastIndexDuringSnapshotCapture = 5;
- long electionTerm = 2;
- String electionVotedFor = "member-2";
-
- List<Object> snapshotData = Arrays.asList(new MockPayload("1"));
- final MockSnapshotState snapshotState = new MockSnapshotState(snapshotData);
-
- org.opendaylight.controller.cluster.raft.Snapshot snapshot = org.opendaylight.controller.cluster.raft.Snapshot
- .create(SerializationUtils.serialize((Serializable) snapshotData),
- Arrays.asList(unAppliedEntry1, unAppliedEntry2), lastIndexDuringSnapshotCapture, 1,
- lastAppliedDuringSnapshotCapture, 1, electionTerm, electionVotedFor, null);
-
- SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
- SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot);
-
- doAnswer(invocation -> new MockSnapshotState(SerializationUtils.deserialize(
- invocation.getArgumentAt(0, byte[].class))))
- .when(mockCohort).deserializePreCarbonSnapshot(any(byte[].class));
-
- sendMessageToSupport(snapshotOffer);
-
- assertEquals("Journal log size", 2, context.getReplicatedLog().size());
- assertEquals("Journal data size", 9, context.getReplicatedLog().dataSize());
- assertEquals("Last index", lastIndexDuringSnapshotCapture, context.getReplicatedLog().lastIndex());
- assertEquals("Last applied", lastAppliedDuringSnapshotCapture, context.getLastApplied());
- assertEquals("Commit index", lastAppliedDuringSnapshotCapture, context.getCommitIndex());
- assertEquals("Snapshot term", 1, context.getReplicatedLog().getSnapshotTerm());
- assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
- assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
- assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
- assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
-
- verify(mockCohort).applyRecoverySnapshot(snapshotState);
- }
-
@Test
public void testOnRecoveryCompletedWithRemainingBatch() {
ReplicatedLog replicatedLog = context.getReplicatedLog();
package org.opendaylight.controller.cluster.datastore;
import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
import java.io.File;
-import java.io.IOException;
import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput;
import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
-import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.slf4j.Logger;
public Snapshot getRestoreFromSnapshot() {
return restoreFromSnapshot;
}
-
- @Override
- @Deprecated
- public State deserializePreCarbonSnapshot(final byte[] from) {
- try {
- return new ShardSnapshotState(ShardDataTreeSnapshot.deserializePreCarbon(from));
- } catch (IOException e) {
- log.error("{}: failed to deserialize snapshot", shardName, e);
- throw Throwables.propagate(e);
- }
- }
}
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.datastore.messages;
-
-import com.google.common.base.Preconditions;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.io.Serializable;
-import java.util.List;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-
-/**
- * Stores a snapshot of the internal state of a data store.
- *
- * @author Thomas Pantelis
- *
- * @deprecated Use {@link org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot} instead.
- */
-@Deprecated
-public class DatastoreSnapshot implements Serializable {
- private static final long serialVersionUID = 1L;
-
- private final String type;
- private final byte[] shardManagerSnapshot;
- private final List<ShardSnapshot> shardSnapshots;
-
- @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
- + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- public DatastoreSnapshot(@Nonnull String type, @Nullable byte[] shardManagerSnapshot,
- @Nonnull List<ShardSnapshot> shardSnapshots) {
- this.type = Preconditions.checkNotNull(type);
- this.shardManagerSnapshot = shardManagerSnapshot;
- this.shardSnapshots = Preconditions.checkNotNull(shardSnapshots);
- }
-
- @Nonnull
- public String getType() {
- return type;
- }
-
- @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
- + "this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- @Nullable
- public byte[] getShardManagerSnapshot() {
- return shardManagerSnapshot;
- }
-
- @Nonnull
- public List<ShardSnapshot> getShardSnapshots() {
- return shardSnapshots;
- }
-
- public static class ShardSnapshot implements Serializable {
- private static final long serialVersionUID = 1L;
-
- private final String name;
- private final byte[] snapshot;
-
- public ShardSnapshot(@Nonnull String name, @Nonnull byte[] snapshot) {
- this.name = Preconditions.checkNotNull(name);
- this.snapshot = Preconditions.checkNotNull(snapshot);
- }
-
- @Nonnull
- public String getName() {
- return name;
- }
-
- @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
- + "this is OK since this class is merely a DTO and does not process byte[] internally. "
- + "Also it would be inefficient to create a return copy as the byte[] could be large.")
- @Nonnull
- public byte[] getSnapshot() {
- return snapshot;
- }
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.datastore.messages;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.util.ArrayList;
-import java.util.List;
-import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
-import org.opendaylight.controller.cluster.raft.Snapshot;
-import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
-
-/**
- * Stores a list of DatastoreSnapshot instances.
- *
- * @deprecated Use {@link org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList} instead.
- */
-@Deprecated
-public class DatastoreSnapshotList extends ArrayList<DatastoreSnapshot> {
- private static final long serialVersionUID = 1L;
-
- public DatastoreSnapshotList() {
- }
-
- public DatastoreSnapshotList(List<DatastoreSnapshot> snapshots) {
- super(snapshots);
- }
-
- private Object readResolve() throws IOException, ClassNotFoundException {
- List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot> snapshots =
- new ArrayList<>(size());
- for (DatastoreSnapshot legacy: this) {
- snapshots.add(new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot(
- legacy.getType(), deserializeShardManagerSnapshot(legacy.getShardManagerSnapshot()),
- fromLegacy(legacy.getShardSnapshots())));
- }
-
- return new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList(snapshots);
- }
-
- private static org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot
- deserializeShardManagerSnapshot(byte [] bytes) throws IOException, ClassNotFoundException {
- if (bytes == null) {
- return null;
- }
-
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
- return (ShardManagerSnapshot) ois.readObject();
- }
- }
-
- private static List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot>
- fromLegacy(List<DatastoreSnapshot.ShardSnapshot> from) throws IOException, ClassNotFoundException {
- List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot> snapshots =
- new ArrayList<>(from.size());
- for (DatastoreSnapshot.ShardSnapshot legacy: from) {
- snapshots.add(new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot(
- legacy.getName(), deserializeShardSnapshot(legacy.getSnapshot())));
- }
-
- return snapshots;
- }
-
- private static org.opendaylight.controller.cluster.raft.persisted.Snapshot deserializeShardSnapshot(byte[] bytes)
- throws IOException, ClassNotFoundException {
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
- Snapshot legacy = (Snapshot) ois.readObject();
-
- org.opendaylight.controller.cluster.raft.persisted.Snapshot.State state = EmptyState.INSTANCE;
- if (legacy.getState().length > 0) {
- state = new ShardSnapshotState(ShardDataTreeSnapshot.deserializePreCarbon(legacy.getState()));
- }
-
- return org.opendaylight.controller.cluster.raft.persisted.Snapshot.create(
- state, legacy.getUnAppliedEntries(), legacy.getLastIndex(),
- legacy.getLastTerm(), legacy.getLastAppliedIndex(), legacy.getLastAppliedTerm(),
- legacy.getElectionTerm(), legacy.getElectionVotedFor(), legacy.getServerConfiguration());
- }
- }
-}
package org.opendaylight.controller.cluster.datastore.persisted;
import com.google.common.base.Verify;
-import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInput;
-import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.util.Optional;
import javax.annotation.Nonnull;
abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
- @SuppressWarnings("checkstyle:FallThrough")
- @Deprecated
- static ShardDataTreeSnapshot deserializePreCarbon(final DataInputStream is) throws IOException {
- final PayloadVersion version = PayloadVersion.readFrom(is);
- switch (version) {
- case BORON:
- // Boron snapshots use Java Serialization
- try (ObjectInputStream ois = new ObjectInputStream(is)) {
- return (ShardDataTreeSnapshot) ois.readObject();
- } catch (ClassNotFoundException e) {
- LOG.error("Failed to serialize data tree snapshot", e);
- throw new IOException("Snapshot failed to deserialize", e);
- }
- case TEST_FUTURE_VERSION:
- case TEST_PAST_VERSION:
- // These versions are never returned and this code is effectively dead
- break;
- default:
- throw new IOException("Invalid payload version in snapshot");
- }
-
- // Not included as default in above switch to ensure we get warnings when new versions are added
- throw new IOException("Encountered unhandled version" + version);
- }
-
@SuppressWarnings("checkstyle:FallThrough")
static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException {
final PayloadVersion version = PayloadVersion.readFrom(in);
package org.opendaylight.controller.cluster.datastore.persisted;
import com.google.common.annotations.Beta;
-import java.io.ByteArrayInputStream;
-import java.io.DataInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Optional;
// Hidden to prevent subclassing from outside of this package
}
- @Deprecated
- public static ShardDataTreeSnapshot deserializePreCarbon(final byte[] bytes) throws IOException {
- try (InputStream is = new ByteArrayInputStream(bytes)) {
- try (DataInputStream dis = new DataInputStream(is)) {
- final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.deserializePreCarbon(dis);
-
- // Make sure we consume all bytes, otherwise something went very wrong
- final int bytesLeft = dis.available();
- if (bytesLeft != 0) {
- throw new IOException("Deserialization left " + bytesLeft + " in the buffer");
- }
-
-
- return ret;
- }
- }
- }
-
public static ShardDataTreeSnapshot deserialize(final ObjectInput in) throws IOException {
final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.versionedDeserialize(in);
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Uninterruptibles;
import com.typesafe.config.ConfigFactory;
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
}
};
}
-
- @Test
- @Deprecated
- public void testRecoveryFromPreCarbonSnapshot() throws Exception {
- new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
- {
- final String name = "testRecoveryFromPreCarbonSnapshot";
-
- final ContainerNode carsNode = CarsModel.newCarsNode(
- CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
- CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
-
- DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
- dataTree.setSchemaContext(SchemaContextHelper.full());
- AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
- NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
-
- MetadataShardDataTreeSnapshot shardSnapshot = new MetadataShardDataTreeSnapshot(root);
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try (DataOutputStream dos = new DataOutputStream(bos)) {
- PayloadVersion.BORON.writeTo(dos);
- try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
- oos.writeObject(shardSnapshot);
- }
- }
-
- final org.opendaylight.controller.cluster.raft.Snapshot snapshot =
- org.opendaylight.controller.cluster.raft.Snapshot.create(bos.toByteArray(),
- Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
-
- InMemorySnapshotStore.addSnapshot("member-1-shard-cars-" + name, snapshot);
-
- try (AbstractDataStore dataStore = setupAbstractDataStore(
- testParameter, name, "module-shards-member1.conf", true, "cars")) {
-
- DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
-
- Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
- assertEquals("isPresent", true, optional.isPresent());
- assertEquals("Data node", carsNode, optional.get());
- }
- }
- };
- }
}
+++ /dev/null
-/*
- * Copyright (c) 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.datastore.messages;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
-import java.io.ObjectOutputStream;
-import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Optional;
-import org.apache.commons.lang.SerializationUtils;
-import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
-import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
-import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
-import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
-import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerSnapshot;
-import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
-import org.opendaylight.controller.cluster.raft.Snapshot;
-import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
-import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
-import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
-import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
-import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
-
-/**
- * Unit tests for DatastoreSnapshotList.
- *
- * @author Thomas Pantelis
- */
-@Deprecated
-public class DatastoreSnapshotListTest {
- @Test
- public void testSerialization() throws Exception {
- NormalizedNode<?, ?> legacyConfigRoot1 = toRootNode(CarsModel.BASE_PATH,
- CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
- BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
- BigInteger.valueOf(30000L)))));
-
- NormalizedNode<?, ?> legacyConfigRoot2 = toRootNode(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
-
- ShardManagerSnapshot legacyShardManagerSnapshot = newLegacyShardManagerSnapshot("config-one", "config-two");
- DatastoreSnapshot legacyConfigSnapshot = new DatastoreSnapshot("config",
- SerializationUtils.serialize(legacyShardManagerSnapshot),
- Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(legacyConfigRoot1)),
- newLegacyShardSnapshot("config-two", newLegacySnapshot(legacyConfigRoot2))));
-
- DatastoreSnapshot legacyOperSnapshot = new DatastoreSnapshot("oper",
- null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(null))));
-
- DatastoreSnapshotList legacy = new DatastoreSnapshotList(Arrays.asList(legacyConfigSnapshot,
- legacyOperSnapshot));
-
- org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList cloned =
- (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList)
- SerializationUtils.clone(legacy);
-
- assertEquals("DatastoreSnapshotList size", 2, cloned.size());
- assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0),
- new org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot(
- legacyShardManagerSnapshot.getShardList(), Collections.emptyMap()),
- Optional.of(legacyConfigRoot1), Optional.of(legacyConfigRoot2));
- assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1),
- (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)null,
- Optional.empty());
- }
-
- @SuppressWarnings("unchecked")
- private static void assertDatastoreSnapshotEquals(final DatastoreSnapshot legacy,
- final org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual,
- final org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot expShardMgrSnapshot,
- final Optional<NormalizedNode<?, ?>>... shardRoots) throws IOException {
- assertEquals("Type", legacy.getType(), actual.getType());
-
- if (legacy.getShardManagerSnapshot() == null) {
- assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot());
- } else {
- org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot actualShardManagerSnapshot =
- (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)
- SerializationUtils.deserialize(legacy.getShardManagerSnapshot());
- assertEquals("ShardManagerSnapshot", expShardMgrSnapshot.getShardList(),
- actualShardManagerSnapshot.getShardList());
- }
-
- assertEquals("ShardSnapshots size", legacy.getShardSnapshots().size(), actual.getShardSnapshots().size());
-
- for (int i = 0; i < actual.getShardSnapshots().size(); i++) {
- ShardSnapshot legacyShardSnapshot = legacy.getShardSnapshots().get(i);
- org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot
- actualShardSnapshot = actual.getShardSnapshots().get(i);
- assertEquals("Shard name", legacyShardSnapshot.getName(), actualShardSnapshot.getName());
- assertSnapshotEquals((Snapshot) SerializationUtils.deserialize(legacyShardSnapshot.getSnapshot()),
- shardRoots[i], actualShardSnapshot.getSnapshot());
- }
- }
-
- private static void assertSnapshotEquals(final Snapshot expected, final Optional<NormalizedNode<?, ?>> expRoot,
- final org.opendaylight.controller.cluster.raft.persisted.Snapshot actual) throws IOException {
- assertEquals("lastIndex", expected.getLastIndex(), actual.getLastIndex());
- assertEquals("lastTerm", expected.getLastTerm(), actual.getLastTerm());
- assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
- assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
- assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
- assertEquals("electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
- assertEquals("electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
-
- if (expRoot.isPresent()) {
- ShardDataTreeSnapshot actualSnapshot = ((ShardSnapshotState)actual.getState()).getSnapshot();
- assertEquals("ShardDataTreeSnapshot type", MetadataShardDataTreeSnapshot.class, actualSnapshot.getClass());
- assertTrue("Expected root node present", actualSnapshot.getRootNode().isPresent());
- assertEquals("Root node", expRoot.get(), actualSnapshot.getRootNode().get());
- } else {
- assertEquals("State type", EmptyState.class, actual.getState().getClass());
- }
- }
-
- private static ShardManagerSnapshot newLegacyShardManagerSnapshot(final String... shards) {
- return ShardManagerSnapshot.forShardList(Arrays.asList(shards));
- }
-
- private static DatastoreSnapshot.ShardSnapshot newLegacyShardSnapshot(final String name,
- final org.opendaylight.controller.cluster.raft.Snapshot snapshot) {
- return new DatastoreSnapshot.ShardSnapshot(name, SerializationUtils.serialize(snapshot));
- }
-
- private static Snapshot newLegacySnapshot(final NormalizedNode<?, ?> root)
- throws Exception {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- if (root != null) {
- MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(root);
- try (DataOutputStream dos = new DataOutputStream(bos)) {
- PayloadVersion.BORON.writeTo(dos);
- try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
- oos.writeObject(snapshot);
- }
- }
- }
-
- return Snapshot.create(bos.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1,
- "member-1", null);
- }
-
- private static NormalizedNode<?, ?> toRootNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
- throws DataValidationFailedException {
- DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
- dataTree.setSchemaContext(SchemaContextHelper.full());
- AbstractShardTest.writeToStore(dataTree, path, node);
- return AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
- }
-}