2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
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.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.duration.Duration;
27 * Temporary actor used to receive a CaptureSnapshotReply message and return a GetSnapshotReply instance.
29 * @author Thomas Pantelis
31 class GetSnapshotReplyActor extends UntypedActor {
32 private static final Logger LOG = LoggerFactory.getLogger(GetSnapshotReplyActor.class);
34 private final Params params;
36 private GetSnapshotReplyActor(Params params) {
39 getContext().setReceiveTimeout(params.receiveTimeout);
43 public void onReceive(Object message) {
44 if(message instanceof CaptureSnapshotReply) {
45 Snapshot snapshot = Snapshot.create(((CaptureSnapshotReply)message).getSnapshot(),
46 params.captureSnapshot.getUnAppliedEntries(),
47 params.captureSnapshot.getLastIndex(), params.captureSnapshot.getLastTerm(),
48 params.captureSnapshot.getLastAppliedIndex(), params.captureSnapshot.getLastAppliedTerm(),
49 params.electionTerm.getCurrentTerm(), params.electionTerm.getVotedFor(),
50 params.peerInformation);
52 LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);
54 params.replyToActor.tell(new GetSnapshotReply(params.id, SerializationUtils.serialize(snapshot)), getSelf());
55 getSelf().tell(PoisonPill.getInstance(), getSelf());
56 } else if (message instanceof ReceiveTimeout) {
57 LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms",
58 params.id, params.receiveTimeout.toMillis());
60 params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(String.format(
61 "Timed out after %d ms while waiting for CaptureSnapshotReply",
62 params.receiveTimeout.toMillis()))), getSelf());
63 getSelf().tell(PoisonPill.getInstance(), getSelf());
67 public static Props props(CaptureSnapshot captureSnapshot, ElectionTerm electionTerm, ActorRef replyToActor,
68 Duration receiveTimeout, String id, ServerConfigurationPayload updatedPeerInfo) {
69 return Props.create(GetSnapshotReplyActor.class, new Params(captureSnapshot, electionTerm, replyToActor,
70 receiveTimeout, id, updatedPeerInfo));
73 private static final class Params {
74 final CaptureSnapshot captureSnapshot;
75 final ActorRef replyToActor;
76 final ElectionTerm electionTerm;
77 final Duration receiveTimeout;
79 final ServerConfigurationPayload peerInformation;
81 Params(CaptureSnapshot captureSnapshot, ElectionTerm electionTerm, ActorRef replyToActor,
82 Duration receiveTimeout, String id, ServerConfigurationPayload peerInfo) {
83 this.captureSnapshot = Preconditions.checkNotNull(captureSnapshot);
84 this.electionTerm = Preconditions.checkNotNull(electionTerm);
85 this.replyToActor = Preconditions.checkNotNull(replyToActor);
86 this.receiveTimeout = Preconditions.checkNotNull(receiveTimeout);
87 this.id = Preconditions.checkNotNull(id);
88 this.peerInformation = peerInfo;