BUG-5280: expand ShardDataTree to cover transaction mechanics
[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 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;
27
28 /**
29  * Participates in raft snapshotting on behalf of a Shard actor.
30  *
31  * @author Thomas Pantelis
32  */
33 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
34     private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
35
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;
41
42     private long applyCounter;
43
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);
49         this.log = log;
50         this.logId = logId;
51     }
52
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";
58
59         // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
60         // requests.
61         final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
62
63         return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
64     }
65
66     @Override
67     public void createSnapshot(final ActorRef actorRef) {
68         // Forward the request to the snapshot actor
69         ShardSnapshotActor.requestSnapshot(snapshotActor, store.takeRecoverySnapshot(), actorRef);
70     }
71
72     private void deserializeAndApplySnapshot(final byte[] snapshotBytes) {
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             final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(
83                 new TransactionIdentifier(applyHistoryId, applyCounter++));
84
85             // delete everything first
86             transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
87
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());
92             }
93
94             store.applyRecoveryTransaction(transaction);
95         } catch (Exception e) {
96             log.error("{}: An exception occurred when applying snapshot", logId, e);
97         }
98
99     }
100
101     @Override
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
106
107         log.info("{}: Applying snapshot", logId);
108         deserializeAndApplySnapshot(snapshotBytes);
109         log.info("{}: Done applying snapshot", logId);
110     }
111 }