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 com.google.common.base.Preconditions;
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.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
22 * Participates in raft snapshotting on behalf of a Shard actor.
24 * @author Thomas Pantelis
26 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
28 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
30 private int createSnapshotTransactionCounter;
31 private final ShardTransactionActorFactory transactionActorFactory;
32 private final ShardDataTree store;
33 private final Logger log;
34 private final String logId;
36 ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
37 Logger log, String logId) {
38 this.transactionActorFactory = transactionActorFactory;
39 this.store = Preconditions.checkNotNull(store);
45 public void createSnapshot(ActorRef actorRef) {
46 // Create a transaction actor. We are really going to treat the transaction as a worker
47 // so that this actor does not get block building the snapshot. THe transaction actor will
48 // after processing the CreateSnapshot message.
50 ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
51 "createSnapshot" + ++createSnapshotTransactionCounter);
53 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
54 TransactionType.READ_ONLY, transactionID, "");
56 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
60 public void applySnapshot(byte[] snapshotBytes) {
61 // Since this will be done only on Recovery or when this actor is a Follower
62 // we can safely commit everything in here. We not need to worry about event notifications
63 // as they would have already been disabled on the follower
65 log.info("{}: Applying snapshot", logId);
68 ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
70 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
72 // delete everything first
73 transaction.getSnapshot().delete(DATASTORE_ROOT);
75 // Add everything from the remote node back
76 transaction.getSnapshot().write(DATASTORE_ROOT, node);
77 syncCommitTransaction(transaction);
78 } catch (InterruptedException | ExecutionException e) {
79 log.error("{}: An exception occurred when applying snapshot", logId, e);
81 log.info("{}: Done applying snapshot", logId);
86 void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
87 throws ExecutionException, InterruptedException {
88 ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
89 commitCohort.preCommit().get();
90 commitCohort.commit().get();