Fix warnings/javadocs in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardSnapshotCohort.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
23
24 /**
25  * Participates in raft snapshotting on behalf of a Shard actor.
26  *
27  * @author Thomas Pantelis
28  */
29 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
30     private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
31
32     private final ActorRef snapshotActor;
33     private final ShardDataTree store;
34     private final String logId;
35     private final Logger log;
36
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);
41         this.log = log;
42         this.logId = logId;
43     }
44
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";
50
51         // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
52         // requests.
53         final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
54
55         return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
56     }
57
58     @Override
59     public void createSnapshot(final ActorRef actorRef) {
60         // Forward the request to the snapshot actor
61         ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeStateSnapshot(), actorRef);
62     }
63
64     @Override
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
70
71         log.info("{}: Applying snapshot", logId);
72
73         final ShardDataTreeSnapshot snapshot;
74         try {
75             snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes);
76         } catch (IOException e) {
77             log.error("{}: Failed to deserialize snapshot", logId, e);
78             return;
79         }
80
81         try {
82             store.applySnapshot(snapshot);
83         } catch (Exception e) {
84             log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
85             return;
86         }
87
88         log.info("{}: Done applying snapshot", logId);
89     }
90 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.