atomic-storage: remove type dependency at segment level I/O
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / GetSnapshotReplyActor.java
1 /*
2  * Copyright (c) 2015 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.raft;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.PoisonPill;
14 import akka.actor.Props;
15 import akka.actor.ReceiveTimeout;
16 import akka.actor.UntypedAbstractActor;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
19 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
20 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
21 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
22 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.duration.FiniteDuration;
26
27 /**
28  * Temporary actor used to receive a CaptureSnapshotReply message and return a GetSnapshotReply instance.
29  *
30  * @author Thomas Pantelis
31  */
32 final class GetSnapshotReplyActor extends UntypedAbstractActor {
33     private static final Logger LOG = LoggerFactory.getLogger(GetSnapshotReplyActor.class);
34
35     private final Params params;
36
37     GetSnapshotReplyActor(final Params params) {
38         this.params = params;
39
40         getContext().setReceiveTimeout(params.receiveTimeout);
41     }
42
43     @Override
44     public void onReceive(final Object message) {
45         if (message instanceof CaptureSnapshotReply) {
46             Snapshot snapshot = Snapshot.create(
47                     ((CaptureSnapshotReply)message).getSnapshotState(),
48                     params.captureSnapshot.getUnAppliedEntries(),
49                     params.captureSnapshot.getLastIndex(), params.captureSnapshot.getLastTerm(),
50                     params.captureSnapshot.getLastAppliedIndex(), params.captureSnapshot.getLastAppliedTerm(),
51                     params.electionTerm.getCurrentTerm(), params.electionTerm.getVotedFor(),
52                     params.peerInformation);
53
54             LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);
55
56             params.replyToActor.tell(new GetSnapshotReply(params.id, snapshot), getSelf());
57             getSelf().tell(PoisonPill.getInstance(), getSelf());
58         } else if (message instanceof ReceiveTimeout) {
59             LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms",
60                     params.id, params.receiveTimeout.toMillis());
61
62             params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(String.format(
63                     "Timed out after %d ms while waiting for CaptureSnapshotReply",
64                         params.receiveTimeout.toMillis()))), getSelf());
65             getSelf().tell(PoisonPill.getInstance(), getSelf());
66         }
67     }
68
69     public static Props props(final CaptureSnapshot captureSnapshot, final ElectionTerm electionTerm,
70             final ActorRef replyToActor, final FiniteDuration receiveTimeout, final String id,
71             final ServerConfigurationPayload updatedPeerInfo) {
72         return Props.create(GetSnapshotReplyActor.class, new Params(captureSnapshot, electionTerm, replyToActor,
73                 receiveTimeout, id, updatedPeerInfo));
74     }
75
76     private static final class Params {
77         final CaptureSnapshot captureSnapshot;
78         final ActorRef replyToActor;
79         final ElectionTerm electionTerm;
80         final FiniteDuration receiveTimeout;
81         final String id;
82         final ServerConfigurationPayload peerInformation;
83
84         Params(final CaptureSnapshot captureSnapshot, final ElectionTerm electionTerm, final ActorRef replyToActor,
85                 final FiniteDuration receiveTimeout, final String id, final ServerConfigurationPayload peerInfo) {
86             this.captureSnapshot = requireNonNull(captureSnapshot);
87             this.electionTerm = requireNonNull(electionTerm);
88             this.replyToActor = requireNonNull(replyToActor);
89             this.receiveTimeout = requireNonNull(receiveTimeout);
90             this.id = requireNonNull(id);
91             peerInformation = peerInfo;
92         }
93     }
94 }