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 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;
23 * Participates in raft snapshotting on behalf of a Shard actor.
25 * @author Thomas Pantelis
27 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
29 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
31 private int createSnapshotTransactionCounter;
32 private final ShardTransactionActorFactory transactionActorFactory;
33 private final ShardDataTree store;
34 private final Logger log;
35 private final String logId;
37 ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
38 Logger log, String logId) {
39 this.transactionActorFactory = transactionActorFactory;
40 this.store = Preconditions.checkNotNull(store);
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.
51 ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
52 "createSnapshot" + ++createSnapshotTransactionCounter);
54 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
55 TransactionProxy.TransactionType.READ_ONLY, transactionID, "", DataStoreVersions.CURRENT_VERSION);
57 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
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
66 log.info("{}: Applying snapshot", logId);
69 ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
71 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
73 // delete everything first
74 transaction.getSnapshot().delete(DATASTORE_ROOT);
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);
82 log.info("{}: Done applying snapshot", logId);
87 void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
88 throws ExecutionException, InterruptedException {
89 DOMStoreThreePhaseCommitCohort commitCohort = store.finishTransaction(transaction);
90 commitCohort.preCommit().get();
91 commitCohort.commit().get();