2 * Copyright (c) 2015 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 akka.actor.ActorContext;
11 import akka.actor.ActorRef;
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
17 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.MemberName;
19 import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor;
20 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
22 import org.slf4j.Logger;
25 * Participates in raft snapshotting on behalf of a Shard actor.
27 * @author Thomas Pantelis
29 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
30 private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
32 private final ActorRef snapshotActor;
33 private final ShardDataTree store;
34 private final String logId;
35 private final Logger log;
37 private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor,
38 final ShardDataTree store, final Logger log, final String logId) {
39 this.snapshotActor = Preconditions.checkNotNull(snapshotActor);
40 this.store = Preconditions.checkNotNull(store);
45 static ShardSnapshotCohort create(final ActorContext actorContext, final MemberName memberName,
46 final ShardDataTree store, final Logger log, final String logId) {
47 final LocalHistoryIdentifier applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
48 FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
49 final String snapshotActorName = "shard-" + memberName.getName() + ':' + "snapshot-read";
51 // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
53 final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
55 return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
59 public void createSnapshot(final ActorRef actorRef) {
60 // Forward the request to the snapshot actor
61 ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeStateSnapshot(), actorRef);
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 public void applySnapshot(final byte[] snapshotBytes) {
67 // Since this will be done only on Recovery or when this actor is a Follower
68 // we can safely commit everything in here. We not need to worry about event notifications
69 // as they would have already been disabled on the follower
71 log.info("{}: Applying snapshot", logId);
73 final ShardDataTreeSnapshot snapshot;
75 snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes);
76 } catch (IOException e) {
77 log.error("{}: Failed to deserialize snapshot", logId, e);
82 store.applySnapshot(snapshot);
83 } catch (Exception e) {
84 log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
88 log.info("{}: Done applying snapshot", logId);