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