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.access.concepts.ClientIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
20 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
21 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
27 * Participates in raft snapshotting on behalf of a Shard actor.
29 * @author Thomas Pantelis
31 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
32 private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
33 private static final FrontendType SNAPSHOT_READ = FrontendType.forName("snapshot-read");
35 private final ShardTransactionActorFactory transactionActorFactory;
36 private final LocalHistoryIdentifier applyHistoryId;
37 private final LocalHistoryIdentifier readHistoryId;
38 private final ShardDataTree store;
39 private final String logId;
40 private final Logger log;
42 private long applyCounter;
43 private long readCounter;
45 ShardSnapshotCohort(MemberName memberName, ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
46 Logger log, String logId) {
47 this.transactionActorFactory = Preconditions.checkNotNull(transactionActorFactory);
48 this.store = Preconditions.checkNotNull(store);
52 this.applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
53 FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
54 this.readHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
55 FrontendIdentifier.create(memberName, SNAPSHOT_READ), 0), 0);
59 public void createSnapshot(ActorRef actorRef) {
60 // Create a transaction actor. We are really going to treat the transaction as a worker
61 // so that this actor does not get block building the snapshot. THe transaction actor will
62 // after processing the CreateSnapshot message.
64 ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
65 TransactionType.READ_ONLY, new TransactionIdentifier(readHistoryId, readCounter++));
67 createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
71 public void applySnapshot(byte[] snapshotBytes) {
72 // Since this will be done only on Recovery or when this actor is a Follower
73 // we can safely commit everything in here. We not need to worry about event notifications
74 // as they would have already been disabled on the follower
76 log.info("{}: Applying snapshot", logId);
79 ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(
80 new TransactionIdentifier(applyHistoryId, applyCounter++));
82 NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
84 // delete everything first
85 transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
87 // Add everything from the remote node back
88 transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, node);
89 syncCommitTransaction(transaction);
90 } catch (InterruptedException | ExecutionException e) {
91 log.error("{}: An exception occurred when applying snapshot", logId, e);
93 log.info("{}: Done applying snapshot", logId);
98 void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
99 throws ExecutionException, InterruptedException {
100 ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
101 commitCohort.preCommit().get();
102 commitCohort.commit().get();