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 {
27 private final ShardTransactionActorFactory transactionActorFactory;
28 private final ShardDataTree store;
29 private final String logId;
30 private final Logger log;
32 private int createSnapshotTransactionCounter;
34 ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
35 Logger log, String logId) {
36 this.transactionActorFactory = transactionActorFactory;
37 this.store = Preconditions.checkNotNull(store);
43 public void createSnapshot(ActorRef actorRef) {
44 // Create a transaction actor. We are really going to treat the transaction as a worker
45 // so that this actor does not get block building the snapshot. THe transaction actor will
46 // after processing the CreateSnapshot message.
48 ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
49 "createSnapshot" + ++createSnapshotTransactionCounter);
51 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
52 TransactionType.READ_ONLY, transactionID, "");
54 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
58 public void applySnapshot(byte[] snapshotBytes) {
59 // Since this will be done only on Recovery or when this actor is a Follower
60 // we can safely commit everything in here. We not need to worry about event notifications
61 // as they would have already been disabled on the follower
63 log.info("{}: Applying snapshot", logId);
66 ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
68 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
70 // delete everything first
71 transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
73 // Add everything from the remote node back
74 transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, node);
75 syncCommitTransaction(transaction);
76 } catch (InterruptedException | ExecutionException e) {
77 log.error("{}: An exception occurred when applying snapshot", logId, e);
79 log.info("{}: Done applying snapshot", logId);
84 void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
85 throws ExecutionException, InterruptedException {
86 ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
87 commitCohort.preCommit().get();
88 commitCohort.commit().get();