Fixup "Leader should always apply modifications as local" regression
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardSnapshotCohort.java
1 /*
2  * Copyright (c) 2015, 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorContext;
13 import akka.actor.ActorRef;
14 import com.google.common.io.ByteSource;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.OutputStream;
18 import java.util.Optional;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
22 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.actors.ShardSnapshotActor;
25 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
27 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
28 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
29 import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
30 import org.slf4j.Logger;
31
32 /**
33  * Participates in raft snapshotting on behalf of a Shard actor.
34  *
35  * @author Thomas Pantelis
36  */
37 final class ShardSnapshotCohort implements RaftActorSnapshotCohort {
38     private static final FrontendType SNAPSHOT_APPLY = FrontendType.forName("snapshot-apply");
39
40     private final ActorRef snapshotActor;
41     private final ShardDataTree store;
42     private final String logId;
43     private final Logger log;
44
45     private ShardSnapshotCohort(final LocalHistoryIdentifier applyHistoryId, final ActorRef snapshotActor,
46             final ShardDataTree store, final Logger log, final String logId) {
47         this.snapshotActor = requireNonNull(snapshotActor);
48         this.store = requireNonNull(store);
49         this.log = log;
50         this.logId = logId;
51     }
52
53     static ShardSnapshotCohort create(final ActorContext actorContext, final MemberName memberName,
54             final ShardDataTree store, final Logger log, final String logId) {
55         final LocalHistoryIdentifier applyHistoryId = new LocalHistoryIdentifier(ClientIdentifier.create(
56             FrontendIdentifier.create(memberName, SNAPSHOT_APPLY), 0), 0);
57         final String snapshotActorName = "shard-" + memberName.getName() + ':' + "snapshot-read";
58
59         // Create a snapshot actor. This actor will act as a worker to offload snapshot serialization for all
60         // requests.
61         final ActorRef snapshotActor = actorContext.actorOf(ShardSnapshotActor.props(), snapshotActorName);
62
63         return new ShardSnapshotCohort(applyHistoryId, snapshotActor, store, log, logId);
64     }
65
66     @Override
67     public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
68         // Forward the request to the snapshot actor
69         final ShardDataTreeSnapshot snapshot = store.takeStateSnapshot();
70         log.debug("{}: requesting serialization of snapshot {}", logId, snapshot);
71
72         ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, installSnapshotStream, actorRef);
73     }
74
75     @Override
76     @SuppressWarnings("checkstyle:IllegalCatch")
77     public void applySnapshot(final Snapshot.State snapshotState) {
78         if (!(snapshotState instanceof ShardSnapshotState)) {
79             log.debug("{}: applySnapshot ignoring snapshot: {}", logId, snapshotState);
80         }
81
82         final ShardDataTreeSnapshot snapshot = ((ShardSnapshotState)snapshotState).getSnapshot();
83
84         // Since this will be done only on Recovery or when this actor is a Follower
85         // we can safely commit everything in here. We not need to worry about event notifications
86         // as they would have already been disabled on the follower
87
88         log.info("{}: Applying snapshot", logId);
89
90         try {
91             store.applySnapshot(snapshot);
92         } catch (Exception e) {
93             log.error("{}: Failed to apply snapshot {}", logId, snapshot, e);
94             return;
95         }
96
97         log.info("{}: Done applying snapshot", logId);
98     }
99
100     @Override
101     public State deserializeSnapshot(final ByteSource snapshotBytes) throws IOException {
102         try (ObjectInputStream in = new ObjectInputStream(snapshotBytes.openStream())) {
103             return ShardDataTreeSnapshot.deserialize(in);
104         }
105     }
106 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.