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 akka.japi.Creator;
14 import com.google.common.base.Optional;
15 import com.google.protobuf.ByteString;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.util.HashMap;
23 import org.opendaylight.controller.cluster.DataPersistenceProvider;
24 import org.opendaylight.controller.cluster.example.messages.KeyValue;
25 import org.opendaylight.controller.cluster.example.messages.KeyValueSaved;
26 import org.opendaylight.controller.cluster.example.messages.PrintRole;
27 import org.opendaylight.controller.cluster.example.messages.PrintState;
28 import org.opendaylight.controller.cluster.raft.ConfigParams;
29 import org.opendaylight.controller.cluster.raft.RaftActor;
30 import org.opendaylight.controller.cluster.raft.RaftState;
31 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
32 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
33 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
36 * A sample actor showing how the RaftActor is to be extended
38 public class ExampleActor extends RaftActor {
40 private final Map<String, String> state = new HashMap<>();
41 private final DataPersistenceProvider dataPersistenceProvider;
43 private long persistIdentifier = 1;
46 public ExampleActor(final String id, final Map<String, String> peerAddresses,
47 final Optional<ConfigParams> configParams) {
48 super(id, peerAddresses, configParams);
49 this.dataPersistenceProvider = new PersistentDataProvider();
52 public static Props props(final String id, final Map<String, String> peerAddresses,
53 final Optional<ConfigParams> configParams){
54 return Props.create(new Creator<ExampleActor>(){
55 private static final long serialVersionUID = 1L;
57 @Override public ExampleActor create() throws Exception {
58 return new ExampleActor(id, peerAddresses, configParams);
63 @Override public void onReceiveCommand(final Object message) throws Exception{
64 if(message instanceof KeyValue){
66 String persistId = Long.toString(persistIdentifier++);
67 persistData(getSender(), persistId, (Payload) message);
69 if(getLeader() != null) {
70 getLeader().forward(message, getContext());
74 } else if (message instanceof PrintState) {
75 if(LOG.isDebugEnabled()) {
76 LOG.debug("State of the node:{} has entries={}, {}",
77 getId(), state.size(), getReplicatedLogState());
80 } else if (message instanceof PrintRole) {
81 if(LOG.isDebugEnabled()) {
82 String followers = "";
83 if (getRaftState() == RaftState.Leader) {
84 followers = ((Leader)this.getCurrentBehavior()).printFollowerStates();
85 LOG.debug("{} = {}, Peers={}, followers={}", getId(), getRaftState(), getPeers(), followers);
87 LOG.debug("{} = {}, Peers={}", getId(), getRaftState(), getPeers());
94 super.onReceiveCommand(message);
98 @Override protected void applyState(final ActorRef clientActor, final String identifier,
100 if(data instanceof KeyValue){
101 KeyValue kv = (KeyValue) data;
102 state.put(kv.getKey(), kv.getValue());
103 if(clientActor != null) {
104 clientActor.tell(new KeyValueSaved(), getSelf());
109 @Override protected void createSnapshot() {
110 ByteString bs = null;
112 bs = fromObject(state);
113 } catch (Exception e) {
114 LOG.error(e, "Exception in creating snapshot");
116 getSelf().tell(new CaptureSnapshotReply(bs), null);
119 @Override protected void applySnapshot(final ByteString snapshot) {
122 state.putAll((Map<String, String>) toObject(snapshot));
123 } catch (Exception e) {
124 LOG.error(e, "Exception in applying snapshot");
126 if(LOG.isDebugEnabled()) {
127 LOG.debug("Snapshot applied to state : {}", ((Map<?, ?>) state).size());
131 private ByteString fromObject(final Object snapshot) throws Exception {
132 ByteArrayOutputStream b = null;
133 ObjectOutputStream o = null;
135 b = new ByteArrayOutputStream();
136 o = new ObjectOutputStream(b);
137 o.writeObject(snapshot);
138 byte[] snapshotBytes = b.toByteArray();
139 return ByteString.copyFrom(snapshotBytes);
151 private Object toObject(final ByteString bs) throws ClassNotFoundException, IOException {
153 ByteArrayInputStream bis = null;
154 ObjectInputStream ois = null;
156 bis = new ByteArrayInputStream(bs.toByteArray());
157 ois = new ObjectInputStream(bis);
158 obj = ois.readObject();
170 @Override protected void onStateChanged() {
175 protected DataPersistenceProvider persistence() {
176 return dataPersistenceProvider;
179 @Override public void onReceiveRecover(final Object message)throws Exception {
180 super.onReceiveRecover(message);
183 @Override public String persistenceId() {
188 protected void startLogRecoveryBatch(final int maxBatchSize) {
192 protected void appendRecoveredLogEntry(final Payload data) {
196 protected void applyCurrentLogRecoveryBatch() {
200 protected void onRecoveryComplete() {
204 protected void applyRecoverySnapshot(final ByteString snapshot) {