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.io.InputOutputStreamFactory;
28 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
29 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
30 import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
31 import org.slf4j.Logger;
34 * Participates in raft snapshotting on behalf of a Shard actor.
36 * @author Thomas Pantelis
38 final class ShardSnapshotCohort implements RaftActorSnapshotCohort {
39 private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
41 private final InputOutputStreamFactory streamFactory;
42 private final ActorRef snapshotActor;
43 private final ShardDataTree store;
44 private final String logId;
45 private final Logger log;
47 ShardSnapshotCohort(final InputOutputStreamFactory streamFactory, final LocalHistoryIdentifier applyHistoryId,
48 final ActorRef snapshotActor, final ShardDataTree store, final Logger log, final String logId) {
49 this.streamFactory = requireNonNull(streamFactory);
50 this.snapshotActor = requireNonNull(snapshotActor);
51 this.store = requireNonNull(store);
56 static ShardSnapshotCohort create(final ActorContext actorContext, final MemberName memberName,
57 final ShardDataTree store, final Logger log, final String logId, final DatastoreContext context) {
58 final LocalHistoryIdentifier applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
59 FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
60 final String snapshotActorName = "shard-" + memberName.getName() + ':' + "snapshot-read";
62 final InputOutputStreamFactory streamFactory = context.isUseLz4Compression()
63 ? InputOutputStreamFactory.lz4("256KB") : InputOutputStreamFactory.simple();
64 // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
66 final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(streamFactory),
69 return new ShardSnapshotCohort(streamFactory, applyHistoryId, snapshotActor, store, log, logId);
73 public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
74 // Forward the request to the snapshot actor
75 final ShardDataTreeSnapshot snapshot = store.takeStateSnapshot();
76 log.debug("{}: requesting serialization of snapshot {}", logId, snapshot);
78 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, installSnapshotStream, actorRef);
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 public void applySnapshot(final Snapshot.State snapshotState) {
84 if (!(snapshotState instanceof ShardSnapshotState)) {
85 log.debug("{}: applySnapshot ignoring snapshot: {}", logId, snapshotState);
88 final ShardDataTreeSnapshot snapshot = ((ShardSnapshotState)snapshotState).getSnapshot();
90 // Since this will be done only on Recovery or when this actor is a Follower
91 // we can safely commit everything in here. We not need to worry about event notifications
92 // as they would have already been disabled on the follower
94 log.info("{}: Applying snapshot", logId);
97 store.applySnapshot(snapshot);
98 } catch (Exception e) {
99 log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
103 log.info("{}: Done applying snapshot", logId);
107 public State deserializeSnapshot(final ByteSource snapshotBytes) throws IOException {
108 try (ObjectInputStream in = new ObjectInputStream(streamFactory.createInputStream(snapshotBytes))) {
109 return ShardDataTreeSnapshot.deserialize(in);