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.UntypedAbstractActor;
15 import com.google.common.base.Preconditions;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
18 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
19 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
20 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
21 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.duration.FiniteDuration;
27 * Temporary actor used to receive a CaptureSnapshotReply message and return a GetSnapshotReply instance.
29 * @author Thomas Pantelis
31 class GetSnapshotReplyActor extends UntypedAbstractActor {
32 private static final Logger LOG = LoggerFactory.getLogger(GetSnapshotReplyActor.class);
34 private final Params params;
36 GetSnapshotReplyActor(final Params params) {
39 getContext().setReceiveTimeout(params.receiveTimeout);
43 public void onReceive(final Object message) {
44 if (message instanceof CaptureSnapshotReply) {
45 Snapshot snapshot = Snapshot.create(
46 ((CaptureSnapshotReply)message).getSnapshotState(),
47 params.captureSnapshot.getUnAppliedEntries(),
48 params.captureSnapshot.getLastIndex(), params.captureSnapshot.getLastTerm(),
49 params.captureSnapshot.getLastAppliedIndex(), params.captureSnapshot.getLastAppliedTerm(),
50 params.electionTerm.getCurrentTerm(), params.electionTerm.getVotedFor(),
51 params.peerInformation);
53 LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);
55 params.replyToActor.tell(new GetSnapshotReply(params.id, snapshot), getSelf());
56 getSelf().tell(PoisonPill.getInstance(), getSelf());
57 } else if (message instanceof ReceiveTimeout) {
58 LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms",
59 params.id, params.receiveTimeout.toMillis());
61 params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(String.format(
62 "Timed out after %d ms while waiting for CaptureSnapshotReply",
63 params.receiveTimeout.toMillis()))), getSelf());
64 getSelf().tell(PoisonPill.getInstance(), getSelf());
68 public static Props props(final CaptureSnapshot captureSnapshot, final ElectionTerm electionTerm,
69 final ActorRef replyToActor, final FiniteDuration receiveTimeout, final String id,
70 final ServerConfigurationPayload updatedPeerInfo) {
71 return Props.create(GetSnapshotReplyActor.class, new Params(captureSnapshot, electionTerm, replyToActor,
72 receiveTimeout, id, updatedPeerInfo));
75 private static final class Params {
76 final CaptureSnapshot captureSnapshot;
77 final ActorRef replyToActor;
78 final ElectionTerm electionTerm;
79 final FiniteDuration receiveTimeout;
81 final ServerConfigurationPayload peerInformation;
83 Params(final CaptureSnapshot captureSnapshot, final ElectionTerm electionTerm, final ActorRef replyToActor,
84 final FiniteDuration receiveTimeout, final String id, final ServerConfigurationPayload peerInfo) {
85 this.captureSnapshot = Preconditions.checkNotNull(captureSnapshot);
86 this.electionTerm = Preconditions.checkNotNull(electionTerm);
87 this.replyToActor = Preconditions.checkNotNull(replyToActor);
88 this.receiveTimeout = Preconditions.checkNotNull(receiveTimeout);
89 this.id = Preconditions.checkNotNull(id);
90 this.peerInformation = peerInfo;