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