Bug 2187: Persisting Actor peerIds' in snapshot
[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 akka.actor.ActorRef;
11 import akka.actor.PoisonPill;
12 import akka.actor.Props;
13 import akka.actor.ReceiveTimeout;
14 import akka.actor.UntypedActor;
15 import com.google.common.base.Preconditions;
16 import java.util.concurrent.TimeoutException;
17 import org.apache.commons.lang3.SerializationUtils;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.duration.Duration;
24
25 /**
26  * Temporary actor used to receive a CaptureSnapshotReply message and return a GetSnapshotReply instance.
27  *
28  * @author Thomas Pantelis
29  */
30 class GetSnapshotReplyActor extends UntypedActor {
31     private static final Logger LOG = LoggerFactory.getLogger(GetSnapshotReplyActor.class);
32
33     private final Params params;
34
35     private GetSnapshotReplyActor(Params params) {
36         this.params = params;
37
38         getContext().setReceiveTimeout(params.receiveTimeout);
39     }
40
41     @Override
42     public void onReceive(Object message) {
43         if(message instanceof CaptureSnapshotReply) {
44             Snapshot snapshot = Snapshot.create(((CaptureSnapshotReply)message).getSnapshot(),
45                     params.captureSnapshot.getUnAppliedEntries(),
46                     params.captureSnapshot.getLastIndex(), params.captureSnapshot.getLastTerm(),
47                     params.captureSnapshot.getLastAppliedIndex(), params.captureSnapshot.getLastAppliedTerm(),
48                     params.electionTerm.getCurrentTerm(), params.electionTerm.getVotedFor(),
49                     params.peerInformation);
50
51             LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);
52
53             params.replyToActor.tell(new GetSnapshotReply(params.id, SerializationUtils.serialize(snapshot)), getSelf());
54             getSelf().tell(PoisonPill.getInstance(), getSelf());
55         } else if (message instanceof ReceiveTimeout) {
56             LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms",
57                     params.id, params.receiveTimeout.toMillis());
58
59             params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(String.format(
60                     "Timed out after %d ms while waiting for CaptureSnapshotReply",
61                         params.receiveTimeout.toMillis()))), getSelf());
62             getSelf().tell(PoisonPill.getInstance(), getSelf());
63         }
64     }
65
66     public static Props props(CaptureSnapshot captureSnapshot, ElectionTerm electionTerm, ActorRef replyToActor,
67             Duration receiveTimeout, String id, ServerConfigurationPayload updatedPeerInfo) {
68         return Props.create(GetSnapshotReplyActor.class, new Params(captureSnapshot, electionTerm, replyToActor,
69                 receiveTimeout, id, updatedPeerInfo));
70     }
71
72     private static final class Params {
73         final CaptureSnapshot captureSnapshot;
74         final ActorRef replyToActor;
75         final ElectionTerm electionTerm;
76         final Duration receiveTimeout;
77         final String id;
78         final ServerConfigurationPayload peerInformation;
79
80         Params(CaptureSnapshot captureSnapshot, ElectionTerm electionTerm, ActorRef replyToActor,
81                 Duration receiveTimeout, String id, ServerConfigurationPayload peerInfo) {
82             this.captureSnapshot = Preconditions.checkNotNull(captureSnapshot);
83             this.electionTerm = Preconditions.checkNotNull(electionTerm);
84             this.replyToActor = Preconditions.checkNotNull(replyToActor);
85             this.receiveTimeout = Preconditions.checkNotNull(receiveTimeout);
86             this.id = Preconditions.checkNotNull(id);
87             this.peerInformation = peerInfo;
88         }
89     }
90 }

©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.