Skip to content

Commit

Permalink
RATIS-603. Add a method to StateMachine for printing StateMachineLogE…
Browse files Browse the repository at this point in the history
…ntryProto. Contributed by Mukul Kumar Singh
  • Loading branch information
szetszwo committed Oct 24, 2019
1 parent debf50a commit d6d58d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.protocol.TermIndex;
import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.ProtoUtils;
Expand Down Expand Up @@ -65,14 +66,26 @@ static String toTermIndexString(TermIndexProto proto) {
}

static String toLogEntryString(LogEntryProto entry) {
return toLogEntryString(entry, null);
}

static String toStateMachineLogEntryString(StateMachineLogEntryProto smLog, StateMachine stateMachine) {
if (stateMachine != null) {
return stateMachine.toStateMachineLogEntryString(smLog);
}
final ByteString clientId = smLog.getClientId();
return (clientId.isEmpty() ? "<empty clientId>" : ClientId.valueOf(clientId)) + ", cid=" + smLog.getCallId();
}


static String toLogEntryString(LogEntryProto entry,
StateMachine stateMachine) {
if (entry == null) {
return null;
}
final String s;
if (entry.hasStateMachineLogEntry()) {
final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry();
final ByteString clientId = smLog.getClientId();
s = ", " + (clientId.isEmpty()? "<empty clientId>": ClientId.valueOf(clientId)) + ", cid=" + smLog.getCallId();
s = ", " + toStateMachineLogEntryString(entry.getStateMachineLogEntry(), stateMachine);
} else if (entry.hasMetadataEntry()) {
final MetadataProto metadata = entry.getMetadataEntry();
s = "(c" + metadata.getCommitIndex() + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,18 @@ public LogEntryProto getEntry(TimeDuration timeout) throws RaftLogIOException, T
entryProto = future.thenApply(data -> ServerProtoUtils.addStateMachineData(data, logEntry))
.get(timeout.getDuration(), timeout.getUnit());
} catch (TimeoutException t) {
final String err = getName() + ": Timeout readStateMachineData for " + toLogEntryString(logEntry);
LOG.error(err, t);
throw t;
} catch (Throwable t) {
final String err = getName() + ": Failed readStateMachineData for " +
ServerProtoUtils.toLogEntryString(logEntry);
final String err = getName() + ": Failed readStateMachineData for " + toLogEntryString(logEntry);
LOG.error(err, t);
throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(t));
}
// by this time we have already read the state machine data,
// so the log entry data should be set now
if (ServerProtoUtils.shouldReadStateMachineData(entryProto)) {
final String err = getName() + ": State machine data not set for " +
ServerProtoUtils.toLogEntryString(logEntry);
final String err = getName() + ": State machine data not set for " + toLogEntryString(logEntry);
LOG.error(err);
throw new RaftLogIOException(err);
}
Expand All @@ -491,7 +491,11 @@ public LogEntryProto getEntry(TimeDuration timeout) throws RaftLogIOException, T

@Override
public String toString() {
return ServerProtoUtils.toLogEntryString(logEntry);
return toLogEntryString(logEntry);
}
}

public String toLogEntryString(LogEntryProto logEntry) {
return ServerProtoUtils.toLogEntryString(logEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,9 @@ public String toString() {
}
}
}

@Override
public String toLogEntryString(LogEntryProto logEntry) {
return ServerProtoUtils.toLogEntryString(logEntry, stateMachine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private class WriteLog extends Task {
this.stateMachineFuture = stateMachine.writeStateMachineData(entry);
} catch (Throwable e) {
LOG.error(name + ": writeStateMachineData failed for index " + entry.getIndex()
+ ", entry=" + ServerProtoUtils.toLogEntryString(entry), e);
+ ", entry=" + ServerProtoUtils.toLogEntryString(entry, stateMachine), e);
throw e;
}
}
Expand Down Expand Up @@ -502,7 +502,7 @@ long getEndIndex() {

@Override
public String toString() {
return super.toString() + ": " + ServerProtoUtils.toLogEntryString(entry);
return super.toString() + ": " + ServerProtoUtils.toLogEntryString(entry, stateMachine);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*/
package org.apache.ratis.statemachine;

import org.apache.ratis.proto.RaftProtos;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftGroupId;
import org.apache.ratis.protocol.RaftGroupMemberId;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.RaftServer;
import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.server.impl.ServerProtoUtils;
import org.apache.ratis.server.protocol.TermIndex;
import org.apache.ratis.server.storage.RaftStorage;
import org.apache.ratis.proto.RaftProtos.RoleInfoProto;
Expand Down Expand Up @@ -293,4 +295,12 @@ default void notifyLeaderChanged(RaftGroupMemberId groupMemberId, RaftPeerId raf
default void notifyGroupRemove() {
}

/**
* Converts the proto object into a useful log string to add information about state machine data.
* @param proto state machine proto
* @return
*/
default String toStateMachineLogEntryString(RaftProtos.StateMachineLogEntryProto proto) {
return ServerProtoUtils.toStateMachineLogEntryString(proto, null);
}
}

0 comments on commit d6d58d0

Please sign in to comment.