2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.example;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Throwables;
15 import com.google.common.io.ByteSource;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.Serializable;
19 import java.util.HashMap;
21 import org.apache.commons.lang3.SerializationUtils;
22 import org.opendaylight.controller.cluster.example.messages.KeyValue;
23 import org.opendaylight.controller.cluster.example.messages.KeyValueSaved;
24 import org.opendaylight.controller.cluster.example.messages.PrintRole;
25 import org.opendaylight.controller.cluster.example.messages.PrintState;
26 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
27 import org.opendaylight.controller.cluster.raft.ConfigParams;
28 import org.opendaylight.controller.cluster.raft.RaftActor;
29 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
30 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
31 import org.opendaylight.controller.cluster.raft.RaftState;
32 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
33 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
34 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
35 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
36 import org.opendaylight.yangtools.concepts.Identifier;
37 import org.opendaylight.yangtools.util.AbstractStringIdentifier;
40 * A sample actor showing how the RaftActor is to be extended.
42 public class ExampleActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
43 private static final class PayloadIdentifier extends AbstractStringIdentifier<PayloadIdentifier> {
44 private static final long serialVersionUID = 1L;
46 PayloadIdentifier(final long identifier) {
47 super(String.valueOf(identifier));
51 private final Map<String, String> state = new HashMap<>();
52 private final Optional<ActorRef> roleChangeNotifier;
54 private long persistIdentifier = 1;
56 public ExampleActor(String id, Map<String, String> peerAddresses,
57 Optional<ConfigParams> configParams) {
58 super(id, peerAddresses, configParams, (short)0);
60 roleChangeNotifier = createRoleChangeNotifier(id);
63 public static Props props(final String id, final Map<String, String> peerAddresses,
64 final Optional<ConfigParams> configParams) {
65 return Props.create(ExampleActor.class, id, peerAddresses, configParams);
69 protected void handleNonRaftCommand(Object message) {
70 if (message instanceof KeyValue) {
72 persistData(getSender(), new PayloadIdentifier(persistIdentifier++), (Payload) message, false);
74 if (getLeader() != null) {
75 getLeader().forward(message, getContext());
79 } else if (message instanceof PrintState) {
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("State of the node:{} has entries={}, {}",
82 getId(), state.size(), getReplicatedLogState());
85 } else if (message instanceof PrintRole) {
86 if (LOG.isDebugEnabled()) {
87 if (getRaftState() == RaftState.Leader || getRaftState() == RaftState.IsolatedLeader) {
88 final String followers = ((Leader)this.getCurrentBehavior()).printFollowerStates();
89 LOG.debug("{} = {}, Peers={}, followers={}", getId(), getRaftState(),
90 getRaftActorContext().getPeerIds(), followers);
92 LOG.debug("{} = {}, Peers={}", getId(), getRaftState(),
93 getRaftActorContext().getPeerIds());
100 super.handleNonRaftCommand(message);
104 protected String getReplicatedLogState() {
105 return "snapshotIndex=" + getRaftActorContext().getReplicatedLog().getSnapshotIndex()
106 + ", snapshotTerm=" + getRaftActorContext().getReplicatedLog().getSnapshotTerm()
107 + ", im-mem journal size=" + getRaftActorContext().getReplicatedLog().size();
110 public Optional<ActorRef> createRoleChangeNotifier(String actorId) {
111 ActorRef exampleRoleChangeNotifier = this.getContext().actorOf(
112 RoleChangeNotifier.getProps(actorId), actorId + "-notifier");
113 return Optional.<ActorRef>of(exampleRoleChangeNotifier);
117 protected Optional<ActorRef> getRoleChangeNotifier() {
118 return roleChangeNotifier;
122 protected void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) {
123 if (data instanceof KeyValue) {
124 KeyValue kv = (KeyValue) data;
125 state.put(kv.getKey(), kv.getValue());
126 if (clientActor != null) {
127 clientActor.tell(new KeyValueSaved(), getSelf());
133 @SuppressWarnings("checkstyle:IllegalCatch")
134 public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
136 if (installSnapshotStream.isPresent()) {
137 SerializationUtils.serialize((Serializable) state, installSnapshotStream.get());
139 } catch (RuntimeException e) {
140 LOG.error("Exception in creating snapshot", e);
143 getSelf().tell(new CaptureSnapshotReply(new MapState(state), installSnapshotStream), null);
147 public void applySnapshot(Snapshot.State snapshotState) {
149 state.putAll(((MapState)snapshotState).state);
151 if (LOG.isDebugEnabled()) {
152 LOG.debug("Snapshot applied to state : {}", state.size());
157 protected void onStateChanged() {
162 public String persistenceId() {
167 protected RaftActorRecoveryCohort getRaftActorRecoveryCohort() {
172 public void startLogRecoveryBatch(int maxBatchSize) {
176 public void appendRecoveredLogEntry(Payload data) {
180 public void applyCurrentLogRecoveryBatch() {
184 public void onRecoveryComplete() {
188 public void applyRecoverySnapshot(Snapshot.State snapshotState) {
192 protected RaftActorSnapshotCohort getRaftActorSnapshotCohort() {
197 public Snapshot getRestoreFromSnapshot() {
201 @SuppressWarnings("unchecked")
203 public Snapshot.State deserializeSnapshot(ByteSource snapshotBytes) {
205 return new MapState((Map<String, String>) SerializationUtils.deserialize(snapshotBytes.read()));
206 } catch (IOException e) {
207 throw Throwables.propagate(e);
211 private static class MapState implements Snapshot.State {
212 private static final long serialVersionUID = 1L;
214 Map<String, String> state;
216 MapState(Map<String, String> state) {