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