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 org.opendaylight.controller.cluster.DataPersistenceProvider;
17 import org.opendaylight.controller.cluster.example.messages.KeyValue;
18 import org.opendaylight.controller.cluster.example.messages.KeyValueSaved;
19 import org.opendaylight.controller.cluster.example.messages.PrintRole;
20 import org.opendaylight.controller.cluster.example.messages.PrintState;
21 import org.opendaylight.controller.cluster.raft.ConfigParams;
22 import org.opendaylight.controller.cluster.raft.RaftActor;
23 import org.opendaylight.controller.cluster.raft.RaftState;
24 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
25 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
26 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.util.HashMap;
37 * A sample actor showing how the RaftActor is to be extended
39 public class ExampleActor extends RaftActor {
41 private final Map<String, String> state = new HashMap();
42 private final DataPersistenceProvider dataPersistenceProvider;
44 private long persistIdentifier = 1;
47 public ExampleActor(String id, Map<String, String> peerAddresses,
48 Optional<ConfigParams> configParams) {
49 super(id, peerAddresses, configParams);
50 this.dataPersistenceProvider = new PersistentDataProvider();
53 public static Props props(final String id, final Map<String, String> peerAddresses,
54 final Optional<ConfigParams> configParams){
55 return Props.create(new Creator<ExampleActor>(){
57 @Override public ExampleActor create() throws Exception {
58 return new ExampleActor(id, peerAddresses, configParams);
63 @Override public void onReceiveCommand(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(ActorRef clientActor, 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(ByteString snapshot) {
122 state.putAll((HashMap) 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 : {}", ((HashMap) state).size());
131 private ByteString fromObject(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(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(Object message)throws Exception {
180 super.onReceiveRecover(message);
183 @Override public String persistenceId() {
188 protected void startLogRecoveryBatch(int maxBatchSize) {
192 protected void appendRecoveredLogEntry(Payload data) {
196 protected void applyCurrentLogRecoveryBatch() {
200 protected void onRecoveryComplete() {
204 protected void applyRecoverySnapshot(ByteString snapshot) {