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 java.util.Optional;
15 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
18 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.MemberName;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor;
22 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
23 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
29 * Participates in raft snapshotting on behalf of a Shard actor.
31 * @author Thomas Pantelis
33 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
34 private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
36 private final LocalHistoryIdentifier applyHistoryId;
37 private final ActorRef snapshotActor;
38 private final ShardDataTree store;
39 private final String logId;
40 private final Logger log;
42 private long applyCounter;
44 private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor,
45 final ShardDataTree store, final Logger log, final String logId) {
46 this.applyHistoryId = Preconditions.checkNotNull(applyHistoryId);
47 this.snapshotActor = Preconditions.checkNotNull(snapshotActor);
48 this.store = Preconditions.checkNotNull(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) {
68 // Forward the request to the snapshot actor
69 ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeRecoverySnapshot(), actorRef);
72 private void deserializeAndApplySnapshot(final byte[] snapshotBytes) {
73 final ShardDataTreeSnapshot snapshot;
75 snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes);
76 } catch (IOException e) {
77 log.error("{}: Failed to deserialize snapshot", logId, e);
82 final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(
83 new TransactionIdentifier(applyHistoryId, applyCounter++));
85 // delete everything first
86 transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
88 final Optional<NormalizedNode<?, ?>> maybeNode = snapshot.getRootNode();
89 if (maybeNode.isPresent()) {
90 // Add everything from the remote node back
91 transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, maybeNode.get());
94 store.applyRecoveryTransaction(transaction);
95 } catch (Exception e) {
96 log.error("{}: An exception occurred when applying snapshot", logId, e);
102 public void applySnapshot(final byte[] snapshotBytes) {
103 // Since this will be done only on Recovery or when this actor is a Follower
104 // we can safely commit everything in here. We not need to worry about event notifications
105 // as they would have already been disabled on the follower
107 log.info("{}: Applying snapshot", logId);
108 deserializeAndApplySnapshot(snapshotBytes);
109 log.info("{}: Done applying snapshot", logId);