2 * Copyright (c) 2015, 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.ActorContext;
13 import akka.actor.ActorRef;
14 import com.google.common.io.ByteSource;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.OutputStream;
18 import java.util.Optional;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
22 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor;
25 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
27 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
28 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
29 import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
30 import org.slf4j.Logger;
33 * Participates in raft snapshotting on behalf of a Shard actor.
35 * @author Thomas Pantelis
37 final class ShardSnapshotCohort implements RaftActorSnapshotCohort {
38 private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
40 private final ActorRef snapshotActor;
41 private final ShardDataTree store;
42 private final String logId;
43 private final Logger log;
45 private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor,
46 final ShardDataTree store, final Logger log, final String logId) {
47 this.snapshotActor = requireNonNull(snapshotActor);
48 this.store = requireNonNull(store);
53 static ShardSnapshotCohort create(final ActorContext actorContext, final MemberName memberName,
54 final ShardDataTree store, final Logger log, final String logId) {
55 final LocalHistoryIdentifier applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
56 FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
57 final String snapshotActorName = "shard-" + memberName.getName() + ':' + "snapshot-read";
59 // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
61 final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
63 return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
67 public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
68 // Forward the request to the snapshot actor
69 final ShardDataTreeSnapshot snapshot = store.takeStateSnapshot();
70 log.debug("{}: requesting serialization of snapshot {}", logId, snapshot);
72 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, installSnapshotStream, actorRef);
76 @SuppressWarnings("checkstyle:IllegalCatch")
77 public void applySnapshot(final Snapshot.State snapshotState) {
78 if (!(snapshotState instanceof ShardSnapshotState)) {
79 log.debug("{}: applySnapshot ignoring snapshot: {}", logId, snapshotState);
82 final ShardDataTreeSnapshot snapshot = ((ShardSnapshotState)snapshotState).getSnapshot();
84 // Since this will be done only on Recovery or when this actor is a Follower
85 // we can safely commit everything in here. We not need to worry about event notifications
86 // as they would have already been disabled on the follower
88 log.info("{}: Applying snapshot", logId);
91 store.applySnapshot(snapshot);
92 } catch (Exception e) {
93 log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
97 log.info("{}: Done applying snapshot", logId);
101 public State deserializeSnapshot(final ByteSource snapshotBytes) throws IOException {
102 try (ObjectInputStream in = new ObjectInputStream(snapshotBytes.openStream())) {
103 return ShardDataTreeSnapshot.deserialize(in);