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