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.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;
21 * Participates in raft snapshotting on behalf of a Shard actor.
23 * @author Thomas Pantelis
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;
31 private int createSnapshotTransactionCounter;
33 ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
34 Logger log, String logId) {
35 this.transactionActorFactory = transactionActorFactory;
36 this.store = Preconditions.checkNotNull(store);
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.
47 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
48 TransactionType.READ_ONLY, "createSnapshot" + ++createSnapshotTransactionCounter, "");
50 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
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
59 log.info("{}: Applying snapshot", logId);
62 ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
64 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
66 // delete everything first
67 transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
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);
75 log.info("{}: Done applying snapshot", logId);
80 void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
81 throws ExecutionException, InterruptedException {
82 ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
83 commitCohort.preCommit().get();
84 commitCohort.commit().get();