BUG-5280: centralize ShardSnapshot operations
[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 java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.MemberName;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor;
23 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
24 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28
29 /**
30  * Participates in raft snapshotting on behalf of a Shard actor.
31  *
32  * @author Thomas Pantelis
33  */
34 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
35     private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
36
37     private final LocalHistoryIdentifier applyHistoryId;
38     private final ActorRef snapshotActor;
39     private final ShardDataTree store;
40     private final String logId;
41     private final Logger log;
42
43     private long applyCounter;
44
45     private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor,
46             final ShardDataTree store, final Logger log, final String logId) {
47         this.applyHistoryId = Preconditions.checkNotNull(applyHistoryId);
48         this.snapshotActor = Preconditions.checkNotNull(snapshotActor);
49         this.store = Preconditions.checkNotNull(store);
50         this.log = log;
51         this.logId = logId;
52     }
53
54     static ShardSnapshotCohort create(final ActorContext actorContext, final MemberName memberName,
55             final ShardDataTree store, final Logger log, final String logId) {
56         final LocalHistoryIdentifier applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
57             FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
58         final String snapshotActorName = "shard-" + memberName.getName() + ':' + "snapshot-read";
59
60         // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
61         // requests.
62         final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
63
64         return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
65     }
66
67     @Override
68     public void createSnapshot(final ActorRef actorRef) {
69         // Forward the request to the snapshot actor
70         ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeRecoverySnapshot(), actorRef);
71     }
72
73     private void deserializeAndApplySnapshot(final byte[] snapshotBytes) {
74         final ShardDataTreeSnapshot snapshot;
75         try {
76             snapshot = ShardDataTreeSnapshot.deserialize(snapshotBytes);
77         } catch (IOException e) {
78             log.error("{}: Failed to deserialize snapshot", logId, e);
79             return;
80         }
81
82         try {
83             final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(
84                 new TransactionIdentifier(applyHistoryId, applyCounter++));
85
86             // delete everything first
87             transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
88
89             final Optional<NormalizedNode<?, ?>> maybeNode = snapshot.getRootNode();
90             if (maybeNode.isPresent()) {
91                 // Add everything from the remote node back
92                 transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, maybeNode.get());
93             }
94
95             store.applyRecoveryTransaction(transaction);
96         } catch (Exception e) {
97             log.error("{}: An exception occurred when applying snapshot", logId, e);
98         }
99
100     }
101
102     void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
103             throws ExecutionException, InterruptedException {
104         ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
105         commitCohort.preCommit().get();
106         commitCohort.commit().get();
107     }
108
109     @Override
110     public void applySnapshot(final byte[] snapshotBytes) {
111         // Since this will be done only on Recovery or when this actor is a Follower
112         // we can safely commit everything in here. We not need to worry about event notifications
113         // as they would have already been disabled on the follower
114
115         log.info("{}: Applying snapshot", logId);
116         deserializeAndApplySnapshot(snapshotBytes);
117         log.info("{}: Done applying snapshot", logId);
118     }
119 }