Repository: incubator-ratis
Updated Branches:
  refs/heads/master ca1106caf -> ac79be6b2


RATIS-252. Add an API for state machine to determining server role. Contributed 
by Tsz Wo Nicholas Sze.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/ac79be6b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/ac79be6b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/ac79be6b

Branch: refs/heads/master
Commit: ac79be6b25f3f87810015a07badcf20683cd87da
Parents: ca1106c
Author: Lokesh Jain <[email protected]>
Authored: Tue Jul 10 23:17:40 2018 +0530
Committer: Lokesh Jain <[email protected]>
Committed: Tue Jul 10 23:19:06 2018 +0530

----------------------------------------------------------------------
 .../arithmetic/ArithmeticStateMachine.java      | 24 +++++++-----
 .../filestore/FileStoreStateMachine.java        |  7 ++--
 .../org/apache/ratis/server/RaftServer.java     |  5 +++
 .../ratis/server/impl/RaftServerImpl.java       | 15 ++++----
 .../apache/ratis/server/impl/ServerState.java   |  7 ++--
 .../ratis/server/impl/StateMachineUpdater.java  |  4 +-
 .../apache/ratis/statemachine/StateMachine.java | 13 +++----
 .../ratis/statemachine/TransactionContext.java  |  3 ++
 .../statemachine/impl/BaseStateMachine.java     | 33 +++++++++--------
 .../impl/TransactionContextImpl.java            | 39 +++++++-------------
 .../SimpleStateMachine4Testing.java             | 16 ++++----
 11 files changed, 82 insertions(+), 84 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
index fafab6e..e35fb9c 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
@@ -17,10 +17,10 @@
  */
 package org.apache.ratis.examples.arithmetic;
 
-import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.examples.arithmetic.expression.Expression;
 import org.apache.ratis.protocol.Message;
-import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.protocol.RaftGroupId;
+import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.impl.RaftServerConstants;
 import org.apache.ratis.server.protocol.TermIndex;
 import org.apache.ratis.server.storage.RaftStorage;
@@ -60,18 +60,17 @@ public class ArithmeticStateMachine extends 
BaseStateMachine {
   }
 
   @Override
-  public void initialize(RaftPeerId id, RaftProperties properties,
+  public void initialize(RaftServer server, RaftGroupId groupId,
       RaftStorage raftStorage) throws IOException {
-    super.initialize(id, properties, raftStorage);
+    super.initialize(server, groupId, raftStorage);
     this.storage.init(raftStorage);
     loadSnapshot(storage.getLatestSnapshot());
   }
 
   @Override
-  public void reinitialize(RaftPeerId id, RaftProperties properties,
-      RaftStorage storage) throws IOException {
+  public void reinitialize() throws IOException {
     close();
-    this.initialize(id, properties, storage);
+    loadSnapshot(storage.getLatestSnapshot());
   }
 
   @Override
@@ -161,10 +160,17 @@ public class ArithmeticStateMachine extends 
BaseStateMachine {
       updateLastAppliedTermIndex(entry.getTerm(), index);
     }
     final Expression r = Expression.Utils.double2Expression(result);
-    LOG.debug("{}-{}: {} = {}", getId(), index, assignment, r);
+    final CompletableFuture<Message> f = 
CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
+
+    final RaftServer.Role role = trx.getServerRole();
+    if (role == RaftServer.Role.LEADER) {
+      LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
+    } else {
+      LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
+    }
     if (LOG.isTraceEnabled()) {
       LOG.trace("{}-{}: variables={}", getId(), index, variables);
     }
-    return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
+    return f;
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
----------------------------------------------------------------------
diff --git 
a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
 
b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
index c1b41c6..05e31f9 100644
--- 
a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
+++ 
b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
@@ -21,7 +21,8 @@ import org.apache.ratis.conf.ConfUtils;
 import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientRequest;
-import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.protocol.RaftGroupId;
+import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.storage.RaftStorage;
 import org.apache.ratis.shaded.com.google.protobuf.ByteString;
 import 
org.apache.ratis.shaded.com.google.protobuf.InvalidProtocolBufferException;
@@ -52,9 +53,9 @@ public class FileStoreStateMachine extends BaseStateMachine {
   }
 
   @Override
-  public void initialize(RaftPeerId id, RaftProperties properties, RaftStorage 
raftStorage)
+  public void initialize(RaftServer server, RaftGroupId groupId, RaftStorage 
raftStorage)
       throws IOException {
-    super.initialize(id, properties, raftStorage);
+    super.initialize(server, groupId, raftStorage);
     this.storage.init(raftStorage);
     FileUtils.createDirectories(files.getRoot());
   }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/server/RaftServer.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/RaftServer.java 
b/ratis-server/src/main/java/org/apache/ratis/server/RaftServer.java
index e563705..8ad7873 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/RaftServer.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/RaftServer.java
@@ -36,6 +36,11 @@ public interface RaftServer extends Closeable, RpcType.Get,
     RaftServerProtocol, RaftServerAsynchronousProtocol,
     RaftClientProtocol, RaftClientAsynchronousProtocol,
     AdminProtocol, AdminAsynchronousProtocol {
+  /** The role of a raft server. */
+  enum Role {
+    LEADER, CANDIDATE, FOLLOWER
+  }
+
   /** @return the server ID. */
   RaftPeerId getId();
 

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
index c825033..dc85d6b 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
@@ -22,6 +22,7 @@ import org.apache.ratis.protocol.*;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.RaftServerMXBean;
 import org.apache.ratis.server.RaftServerRpc;
+import org.apache.ratis.server.RaftServer.Role;
 import org.apache.ratis.server.protocol.RaftServerAsynchronousProtocol;
 import org.apache.ratis.server.protocol.RaftServerProtocol;
 import org.apache.ratis.server.protocol.TermIndex;
@@ -60,12 +61,6 @@ public class RaftServerImpl implements RaftServerProtocol, 
RaftServerAsynchronou
   static final String APPEND_ENTRIES = CLASS_NAME + ".appendEntries";
   static final String INSTALL_SNAPSHOT = CLASS_NAME + ".installSnapshot";
 
-
-  /** Role of raft peer */
-  enum Role {
-    LEADER, CANDIDATE, FOLLOWER
-  }
-
   private final RaftServerProxy proxy;
   private final StateMachine stateMachine;
   private final int minTimeoutMs;
@@ -223,6 +218,10 @@ public class RaftServerImpl implements RaftServerProtocol, 
RaftServerAsynchronou
     return getState().getSelfId();
   }
 
+  Role getRole() {
+    return role;
+  }
+
   RaftConfiguration getRaftConf() {
     return getState().getRaftConf();
   }
@@ -441,7 +440,7 @@ public class RaftServerImpl implements RaftServerProtocol, 
RaftServerAsynchronou
   void assertGroup(Object requestorId, RaftGroupId requestorGroupId) throws 
GroupMismatchException {
     if (!groupId.equals(requestorGroupId)) {
       throw new GroupMismatchException(getId()
-          + ": The group (" + requestorGroupId + ") of requestor " + 
requestorId
+          + ": The group (" + requestorGroupId + ") of " + requestorId
           + " does not match the group (" + groupId + ") of the server " + 
getId());
     }
   }
@@ -1051,7 +1050,7 @@ public class RaftServerImpl implements 
RaftServerProtocol, RaftServerAsynchronou
       // check whether there is a TransactionContext because we are the leader.
       TransactionContext trx = getTransactionContext(next.getIndex());
       if (trx == null) {
-        trx = new TransactionContextImpl(stateMachine, next);
+        trx = new TransactionContextImpl(getRole(), stateMachine, next);
       }
 
       // Let the StateMachine inject logic for committed transactions in 
sequential order.

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
index 8c7b269..eb67fac 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java
@@ -22,7 +22,6 @@ import org.apache.ratis.protocol.*;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.protocol.TermIndex;
 import org.apache.ratis.server.storage.*;
-import org.apache.ratis.shaded.proto.RaftProtos.CommitInfoProto;
 import org.apache.ratis.shaded.proto.RaftProtos.InstallSnapshotRequestProto;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.statemachine.SnapshotInfo;
@@ -91,7 +90,7 @@ public class ServerState implements Closeable {
         RaftServerConstants.StartupOption.REGULAR);
     snapshotManager = new SnapshotManager(storage, id);
 
-    long lastApplied = initStatemachine(stateMachine, prop);
+    long lastApplied = initStatemachine(stateMachine, group.getGroupId());
 
     leaderId = null;
     // we cannot apply log entries to the state machine in this step, since we
@@ -112,9 +111,9 @@ public class ServerState implements Closeable {
          lastApplied, prop);
   }
 
-  private long initStatemachine(StateMachine sm, RaftProperties properties)
+  private long initStatemachine(StateMachine sm, RaftGroupId groupId)
       throws IOException {
-    sm.initialize(selfId, properties, storage);
+    sm.initialize(server.getProxy(), groupId, storage);
     storage.setStateMachineStorage(sm.getStateMachineStorage());
     SnapshotInfo snapshot = sm.getLatestSnapshot();
 

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
index 31ea6d3..c92f3ac 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
@@ -21,7 +21,6 @@ import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.storage.RaftLog;
-import org.apache.ratis.server.storage.RaftStorage;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.statemachine.SnapshotInfo;
 import org.apache.ratis.statemachine.StateMachine;
@@ -108,7 +107,6 @@ class StateMachineUpdater implements Runnable {
 
   @Override
   public void run() {
-    final RaftStorage storage = server.getState().getStorage();
     while (isRunning()) {
       try {
         synchronized (this) {
@@ -126,7 +124,7 @@ class StateMachineUpdater implements Runnable {
         if (state == State.RELOAD) {
           Preconditions.assertTrue(stateMachine.getLifeCycleState() == 
LifeCycle.State.PAUSED);
 
-          stateMachine.reinitialize(server.getId(), properties, storage);
+          stateMachine.reinitialize();
 
           SnapshotInfo snapshot = stateMachine.getLatestSnapshot();
           Preconditions.assertTrue(snapshot != null && snapshot.getIndex() > 
lastAppliedIndex,

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
index 8fa3c90..2a0ebf1 100644
--- a/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
+++ b/ratis-server/src/main/java/org/apache/ratis/statemachine/StateMachine.java
@@ -17,11 +17,10 @@
  */
 package org.apache.ratis.statemachine;
 
-import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientRequest;
 import org.apache.ratis.protocol.RaftGroupId;
-import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.impl.RaftConfiguration;
 import org.apache.ratis.server.protocol.TermIndex;
@@ -50,12 +49,11 @@ public interface StateMachine extends Closeable {
   }
 
   /**
-   * Initializes the State Machine with the given properties and storage. The 
state machine is
+   * Initializes the State Machine with the given server, group and storage. 
The state machine is
    * responsible reading the latest snapshot from the file system (if any) and 
initialize itself
    * with the latest term and index there including all the edits.
    */
-  void initialize(RaftPeerId id, RaftProperties properties, RaftStorage 
storage)
-      throws IOException;
+  void initialize(RaftServer server, RaftGroupId groupId, RaftStorage storage) 
throws IOException;
 
   /**
    * Returns the lifecycle state for this StateMachine.
@@ -70,12 +68,11 @@ public interface StateMachine extends Closeable {
   void pause();
 
   /**
-   * Re-initializes the State Machine in PAUSED state with the given 
properties and storage. The
+   * Re-initializes the State Machine in PAUSED state. The
    * state machine is responsible reading the latest snapshot from the file 
system (if any) and
    * initialize itself with the latest term and index there including all the 
edits.
    */
-  void reinitialize(RaftPeerId id, RaftProperties properties, RaftStorage 
storage)
-      throws IOException;
+  void reinitialize() throws IOException;
 
   /**
    * Dump the in-memory state into a snapshot file in the RaftStorage. The

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/statemachine/TransactionContext.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/TransactionContext.java
 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/TransactionContext.java
index 5fbedf9..a417912 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/TransactionContext.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/TransactionContext.java
@@ -18,6 +18,7 @@
 package org.apache.ratis.statemachine;
 
 import org.apache.ratis.protocol.RaftClientRequest;
+import org.apache.ratis.server.RaftServer.Role;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase;
 import org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto;
@@ -44,6 +45,8 @@ import java.util.Collection;
  * the RAFT log from the leader.
  */
 public interface TransactionContext {
+  /** @return the role of the server when this context is created. */
+  Role getServerRole();
 
   /**
    * Returns the original request from the {@link RaftClientRequest}

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
index b87143f..16b4f6f 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java
@@ -18,10 +18,11 @@
 
 package org.apache.ratis.statemachine.impl;
 
-import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientRequest;
+import org.apache.ratis.protocol.RaftGroupId;
 import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.impl.RaftConfiguration;
 import org.apache.ratis.server.impl.RaftServerConstants;
 import org.apache.ratis.server.protocol.TermIndex;
@@ -45,10 +46,9 @@ import java.util.concurrent.atomic.AtomicReference;
  * Base implementation for StateMachines.
  */
 public class BaseStateMachine implements StateMachine {
-  private volatile RaftPeerId id;
-  protected RaftProperties properties;
-  protected RaftStorage storage;
-  protected RaftConfiguration raftConf;
+  private final CompletableFuture<RaftServer> server = new 
CompletableFuture<>();
+  private volatile RaftGroupId groupId;
+  private volatile RaftConfiguration raftConf;
   protected final LifeCycle lifeCycle = new 
LifeCycle(getClass().getSimpleName());
 
   private final AtomicReference<TermIndex> lastAppliedTermIndex = new 
AtomicReference<>();
@@ -56,7 +56,7 @@ public class BaseStateMachine implements StateMachine {
   private final SortedMap<Long, CompletableFuture<Void>> transactionFutures = 
new TreeMap<>();
 
   public RaftPeerId getId() {
-    return id;
+    return server.isDone()? server.join().getId(): null;
   }
 
   @Override
@@ -65,12 +65,10 @@ public class BaseStateMachine implements StateMachine {
   }
 
   @Override
-  public void initialize(RaftPeerId id, RaftProperties properties,
-      RaftStorage storage) throws IOException {
-    this.id = id;
-    lifeCycle.setName(getClass().getSimpleName() + ":" + id);
-    this.properties = properties;
-    this.storage = storage;
+  public void initialize(RaftServer server, RaftGroupId groupId, RaftStorage 
storage) throws IOException {
+    this.groupId = groupId;
+    this.server.complete(server);
+    lifeCycle.setName("" + this);
   }
 
   @Override
@@ -98,8 +96,7 @@ public class BaseStateMachine implements StateMachine {
   }
 
   @Override
-  public void reinitialize(RaftPeerId id, RaftProperties properties,
-      RaftStorage storage) throws IOException {
+  public void reinitialize() throws IOException {
   }
 
   @Override
@@ -127,7 +124,7 @@ public class BaseStateMachine implements StateMachine {
     final TermIndex newTI = TermIndex.newTermIndex(term, index);
     final TermIndex oldTI = lastAppliedTermIndex.getAndSet(newTI);
     if (!newTI.equals(oldTI)) {
-      LOG.debug("{}: update lastAppliedTermIndex from {} to {}", getId(), 
oldTI, newTI);
+      LOG.trace("{}: update lastAppliedTermIndex from {} to {}", getId(), 
oldTI, newTI);
       if (oldTI != null) {
         Preconditions.assertTrue(newTI.compareTo(oldTI) >= 0,
             () -> getId() + ": Failed updateLastAppliedTermIndex: newTI = "
@@ -208,4 +205,10 @@ public class BaseStateMachine implements StateMachine {
   public void close() throws IOException {
     // do nothing
   }
+
+  @Override
+  public String toString() {
+    return getClass().getSimpleName() + ":"
+        + (!server.isDone()? "uninitialized": getId() + ":" + groupId);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
index 38bfb9a..08d3536 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/TransactionContextImpl.java
@@ -20,6 +20,7 @@ package org.apache.ratis.statemachine.impl;
 import java.io.IOException;
 import java.util.Objects;
 import org.apache.ratis.protocol.RaftClientRequest;
+import org.apache.ratis.server.RaftServer.Role;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
 import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase;
 import org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto;
@@ -31,7 +32,8 @@ import org.apache.ratis.util.Preconditions;
  * Implementation of {@link TransactionContext}
  */
 public class TransactionContextImpl implements TransactionContext {
-
+  /** The role of the server when this object is created. */
+  private final Role serverRole;
   /** The {@link StateMachine} that originated the transaction. */
   private final StateMachine stateMachine;
 
@@ -62,7 +64,8 @@ public class TransactionContextImpl implements 
TransactionContext {
   /** Committed LogEntry. */
   private LogEntryProto logEntry;
 
-  private TransactionContextImpl(StateMachine stateMachine) {
+  private TransactionContextImpl(Role serverRole, StateMachine stateMachine) {
+    this.serverRole = serverRole;
     this.stateMachine = stateMachine;
   }
 
@@ -82,45 +85,29 @@ public class TransactionContextImpl implements 
TransactionContext {
   public TransactionContextImpl(
       StateMachine stateMachine, RaftClientRequest clientRequest,
       SMLogEntryProto smLogEntryProto, Object stateMachineContext) {
-    this(stateMachine);
+    this(Role.LEADER, stateMachine);
     this.clientRequest = clientRequest;
     this.smLogEntryProto = smLogEntryProto;
     this.stateMachineContext = stateMachineContext;
   }
 
-  /** The same as this(stateMachine, clientRequest, exception, null). */
-  public TransactionContextImpl (
-      StateMachine stateMachine, RaftClientRequest clientRequest,
-      Exception exception) {
-    this(stateMachine, clientRequest, exception, null);
-  }
-
-  /**
-   * Construct a {@link TransactionContext} from a client request to signal
-   * an exception so that the RAFT server will fail the request on behalf
-   * of the {@link StateMachine}.
-   */
-  public TransactionContextImpl(
-      StateMachine stateMachine, RaftClientRequest clientRequest,
-      Exception exception, Object stateMachineContext) {
-    this(stateMachine);
-    this.clientRequest = clientRequest;
-    this.exception = exception;
-    this.stateMachineContext = stateMachineContext;
-  }
-
   /**
    * Construct a {@link TransactionContext} from a {@link LogEntryProto}.
    * Used by followers for applying committed entries to the state machine.
    * @param logEntry the log entry to be applied
    */
-  public TransactionContextImpl(StateMachine stateMachine, LogEntryProto 
logEntry) {
-    this(stateMachine);
+  public TransactionContextImpl(Role serverRole, StateMachine stateMachine, 
LogEntryProto logEntry) {
+    this(serverRole, stateMachine);
     setLogEntry(logEntry);
     this.smLogEntryProto = logEntry.getSmLogEntry();
   }
 
   @Override
+  public Role getServerRole() {
+    return serverRole;
+  }
+
+  @Override
   public RaftClientRequest getClientRequest() {
     return clientRequest;
   }

http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/ac79be6b/ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java
----------------------------------------------------------------------
diff --git 
a/ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java
 
b/ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java
index 5d66ca3..1bb04c7 100644
--- 
a/ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java
+++ 
b/ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java
@@ -22,8 +22,9 @@ import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.io.MD5Hash;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientRequest;
-import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.protocol.RaftGroupId;
 import org.apache.ratis.protocol.StateMachineException;
+import org.apache.ratis.server.RaftServer;
 import org.apache.ratis.server.RaftServerConfigKeys;
 import org.apache.ratis.server.impl.RaftServerConstants;
 import org.apache.ratis.server.impl.RaftServerImpl;
@@ -99,11 +100,11 @@ public class SimpleStateMachine4Testing extends 
BaseStateMachine {
   }
 
   @Override
-  public synchronized void initialize(RaftPeerId id, RaftProperties properties,
+  public synchronized void initialize(RaftServer server, RaftGroupId groupId,
       RaftStorage raftStorage) throws IOException {
-    LOG.info("Initializing " + getClass().getSimpleName() + ":" + id);
+    LOG.info("Initializing " + this);
     lifeCycle.startAndTransition(() -> {
-      super.initialize(id, properties, raftStorage);
+      super.initialize(server, groupId, raftStorage);
       storage.init(raftStorage);
       loadSnapshot(storage.findLatestSnapshot());
 
@@ -122,10 +123,9 @@ public class SimpleStateMachine4Testing extends 
BaseStateMachine {
   }
 
   @Override
-  public synchronized void reinitialize(RaftPeerId id, RaftProperties 
properties,
-      RaftStorage storage) throws IOException {
-    LOG.info("Reinitializing " + getClass().getSimpleName() + ":" + id);
-    initialize(id, properties, storage);
+  public synchronized void reinitialize() throws IOException {
+    LOG.info("Reinitializing " + this);
+    loadSnapshot(storage.findLatestSnapshot());
   }
 
   @Override

Reply via email to