X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardSnapshotCohort.java;h=126e28d9c8c6157794f3b7a12282fb5488d5e2dd;hb=55a9b9f42a14c56060f74b38f84d444c0fbfecc4;hp=8bef15bbbaea1b663ca376359f602e7839b9a562;hpb=a47dd7a5d21ca68804a6d0e2e3ca765f223c2ef4;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardSnapshotCohort.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardSnapshotCohort.java index 8bef15bbba..126e28d9c8 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardSnapshotCohort.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardSnapshotCohort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved. + * Copyright (c) 2015, 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, @@ -7,22 +7,26 @@ */ package org.opendaylight.controller.cluster.datastore; +import static java.util.Objects.requireNonNull; + import akka.actor.ActorContext; import akka.actor.ActorRef; -import com.google.common.base.Preconditions; +import com.google.common.io.ByteSource; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.OutputStream; import java.util.Optional; import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; import org.opendaylight.controller.cluster.access.concepts.FrontendType; import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; import org.opendaylight.controller.cluster.access.concepts.MemberName; -import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor; import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState; import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State; import org.slf4j.Logger; /** @@ -30,22 +34,18 @@ import org.slf4j.Logger; * * @author Thomas Pantelis */ -class ShardSnapshotCohort implements RaftActorSnapshotCohort { +final class ShardSnapshotCohort implements RaftActorSnapshotCohort { private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply"); - private final LocalHistoryIdentifier applyHistoryId; private final ActorRef snapshotActor; private final ShardDataTree store; private final String logId; private final Logger log; - private long applyCounter; - private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor, final ShardDataTree store, final Logger log, final String logId) { - this.applyHistoryId = Preconditions.checkNotNull(applyHistoryId); - this.snapshotActor = Preconditions.checkNotNull(snapshotActor); - this.store = Preconditions.checkNotNull(store); + this.snapshotActor = requireNonNull(snapshotActor); + this.store = requireNonNull(store); this.log = log; this.logId = logId; } @@ -64,48 +64,43 @@ class ShardSnapshotCohort implements RaftActorSnapshotCohort { } @Override - public void createSnapshot(final ActorRef actorRef) { + public void createSnapshot(final ActorRef actorRef, final Optional installSnapshotStream) { // Forward the request to the snapshot actor - ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeRecoverySnapshot(), actorRef); + final ShardDataTreeSnapshot snapshot = store.takeStateSnapshot(); + log.debug("{}: requesting serialization of snapshot {}", logId, snapshot); + + ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, installSnapshotStream, actorRef); } - private void deserializeAndApplySnapshot(final byte[] snapshotBytes) { - final ShardDataTreeSnapshot snapshot; - try { - snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes); - } catch (IOException e) { - log.error("{}: Failed to deserialize snapshot", logId, e); - return; + @Override + @SuppressWarnings("checkstyle:IllegalCatch") + public void applySnapshot(final Snapshot.State snapshotState) { + if (!(snapshotState instanceof ShardSnapshotState)) { + log.debug("{}: applySnapshot ignoring snapshot: {}", logId, snapshotState); } - try { - final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction( - new TransactionIdentifier(applyHistoryId, applyCounter++)); + final ShardDataTreeSnapshot snapshot = ((ShardSnapshotState)snapshotState).getSnapshot(); - // delete everything first - transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY); + // Since this will be done only on Recovery or when this actor is a Follower + // we can safely commit everything in here. We not need to worry about event notifications + // as they would have already been disabled on the follower - final Optional> maybeNode = snapshot.getRootNode(); - if (maybeNode.isPresent()) { - // Add everything from the remote node back - transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, maybeNode.get()); - } + log.info("{}: Applying snapshot", logId); - store.applyRecoveryTransaction(transaction); + try { + store.applySnapshot(snapshot); } catch (Exception e) { - log.error("{}: An exception occurred when applying snapshot", logId, e); + log.error("{}: Failed to apply snapshot {}", logId, snapshot, e); + return; } + log.info("{}: Done applying snapshot", logId); } @Override - public void applySnapshot(final byte[] snapshotBytes) { - // Since this will be done only on Recovery or when this actor is a Follower - // we can safely commit everything in here. We not need to worry about event notifications - // as they would have already been disabled on the follower - - log.info("{}: Applying snapshot", logId); - deserializeAndApplySnapshot(snapshotBytes); - log.info("{}: Done applying snapshot", logId); + public State deserializeSnapshot(final ByteSource snapshotBytes) throws IOException { + try (ObjectInputStream in = new ObjectInputStream(snapshotBytes.openStream())) { + return ShardDataTreeSnapshot.deserialize(in); + } } }