2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
24 * Participates in raft snapshotting on behalf of a Shard actor.
26 * @author Thomas Pantelis
28 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
30 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
32 private int createSnapshotTransactionCounter;
33 private final ShardTransactionActorFactory transactionActorFactory;
34 private final InMemoryDOMDataStore store;
35 private final Logger log;
36 private final String logId;
38 ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, InMemoryDOMDataStore store,
39 Logger log, String logId) {
40 this.transactionActorFactory = transactionActorFactory;
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.
52 ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
53 "createSnapshot" + ++createSnapshotTransactionCounter);
55 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
56 TransactionProxy.TransactionType.READ_ONLY, transactionID, "", DataStoreVersions.CURRENT_VERSION);
58 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
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
67 log.info("{}: Applying snapshot", logId);
70 DOMStoreWriteTransaction transaction = store.newWriteOnlyTransaction();
72 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
74 // delete everything first
75 transaction.delete(DATASTORE_ROOT);
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);
83 log.info("{}: Done applying snapshot", logId);
88 void syncCommitTransaction(final DOMStoreWriteTransaction transaction)
89 throws ExecutionException, InterruptedException {
90 DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
91 commitCohort.preCommit().get();
92 commitCohort.commit().get();