CDS: use internal DataTree instance
[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 com.google.common.base.Preconditions;
11 import akka.actor.ActorRef;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
15 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
16 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.slf4j.Logger;
21
22 /**
23  * Participates in raft snapshotting on behalf of a Shard actor.
24  *
25  * @author Thomas Pantelis
26  */
27 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
28
29     private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
30
31     private int createSnapshotTransactionCounter;
32     private final ShardTransactionActorFactory transactionActorFactory;
33     private final ShardDataTree store;
34     private final Logger log;
35     private final String logId;
36
37     ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
38             Logger log, String logId) {
39         this.transactionActorFactory = transactionActorFactory;
40         this.store = Preconditions.checkNotNull(store);
41         this.log = log;
42         this.logId = logId;
43     }
44
45     @Override
46     public void createSnapshot(ActorRef actorRef) {
47         // Create a transaction actor. We are really going to treat the transaction as a worker
48         // so that this actor does not get block building the snapshot. THe transaction actor will
49         // after processing the CreateSnapshot message.
50
51         ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
52                 "createSnapshot" + ++createSnapshotTransactionCounter);
53
54         ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
55                 TransactionProxy.TransactionType.READ_ONLY, transactionID, "", DataStoreVersions.CURRENT_VERSION);
56
57         createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
58     }
59
60     @Override
61     public void applySnapshot(byte[] snapshotBytes) {
62         // Since this will be done only on Recovery or when this actor is a Follower
63         // we can safely commit everything in here. We not need to worry about event notifications
64         // as they would have already been disabled on the follower
65
66         log.info("{}: Applying snapshot", logId);
67
68         try {
69             ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
70
71             NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
72
73             // delete everything first
74             transaction.getSnapshot().delete(DATASTORE_ROOT);
75
76             // Add everything from the remote node back
77             transaction.getSnapshot().write(DATASTORE_ROOT, node);
78             syncCommitTransaction(transaction);
79         } catch (InterruptedException | ExecutionException e) {
80             log.error("{}: An exception occurred when applying snapshot", logId, e);
81         } finally {
82             log.info("{}: Done applying snapshot", logId);
83         }
84
85     }
86
87     void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
88             throws ExecutionException, InterruptedException {
89         DOMStoreThreePhaseCommitCohort commitCohort = store.finishTransaction(transaction);
90         commitCohort.preCommit().get();
91         commitCohort.commit().get();
92     }
93 }