Skip to content

Commit

Permalink
Update RaftMemberContext methods according to coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jun 25, 2017
1 parent 04c4778 commit 0ee1872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
Expand Up @@ -119,11 +119,9 @@ public long getConfigTerm() {
* Sets the member term. * Sets the member term.
* *
* @param term The member term. * @param term The member term.
* @return The member state.
*/ */
public RaftMemberContext setConfigTerm(long term) { public void setConfigTerm(long term) {
this.term = term; this.term = term;
return this;
} }


/** /**
Expand Down Expand Up @@ -157,11 +155,9 @@ public long getNextSnapshotIndex() {
* Sets the member's next snapshot index. * Sets the member's next snapshot index.
* *
* @param nextSnapshotIndex The member's next snapshot index. * @param nextSnapshotIndex The member's next snapshot index.
* @return The member state.
*/ */
public RaftMemberContext setNextSnapshotIndex(long nextSnapshotIndex) { public void setNextSnapshotIndex(long nextSnapshotIndex) {
this.nextSnapshotIndex = nextSnapshotIndex; this.nextSnapshotIndex = nextSnapshotIndex;
return this;
} }


/** /**
Expand All @@ -177,11 +173,9 @@ public int getNextSnapshotOffset() {
* Sets the member's snapshot offset. * Sets the member's snapshot offset.
* *
* @param nextSnapshotOffset The member's snapshot offset. * @param nextSnapshotOffset The member's snapshot offset.
* @return The member state.
*/ */
public RaftMemberContext setNextSnapshotOffset(int nextSnapshotOffset) { public void setNextSnapshotOffset(int nextSnapshotOffset) {
this.nextSnapshotOffset = nextSnapshotOffset; this.nextSnapshotOffset = nextSnapshotOffset;
return this;
} }


/** /**
Expand Down Expand Up @@ -216,12 +210,10 @@ public long getNextIndex() {
* Sets the member's next index. * Sets the member's next index.
* *
* @param nextIndex The member's next index. * @param nextIndex The member's next index.
* @return The member state.
*/ */
public RaftMemberContext setNextIndex(long nextIndex) { public void setNextIndex(long nextIndex) {
checkArgument(nextIndex > 0, "nextIndex must be positive"); checkArgument(nextIndex > 0, "nextIndex must be positive");
this.nextIndex = nextIndex; this.nextIndex = nextIndex;
return this;
} }


/** /**
Expand Down Expand Up @@ -416,7 +408,7 @@ private static class TimeBuffer {
private final long[] buffer; private final long[] buffer;
private int position; private int position;


public TimeBuffer(int size) { TimeBuffer(int size) {
this.buffer = new long[size]; this.buffer = new long[size];
} }


Expand All @@ -425,7 +417,7 @@ public TimeBuffer(int size) {
* *
* @param time The request round trip time to record. * @param time The request round trip time to record.
*/ */
public void record(long time) { void record(long time) {
buffer[position++] = time; buffer[position++] = time;
if (position >= buffer.length) { if (position >= buffer.length) {
position = 0; position = 0;
Expand All @@ -437,7 +429,7 @@ public void record(long time) {
* *
* @return The average of all recorded round trip times. * @return The average of all recorded round trip times.
*/ */
public long average() { long average() {
long total = 0; long total = 0;
for (long time : buffer) { for (long time : buffer) {
if (time > 0) { if (time > 0) {
Expand All @@ -450,7 +442,7 @@ public long average() {
/** /**
* Resets the recorded round trip times. * Resets the recorded round trip times.
*/ */
public void reset() { void reset() {
for (int i = 0; i < buffer.length; i++) { for (int i = 0; i < buffer.length; i++) {
buffer[i] = 0; buffer[i] = 0;
} }
Expand Down
Expand Up @@ -340,8 +340,9 @@ protected void resetNextIndex(RaftMemberContext member) {
final RaftLogReader reader = member.getLogReader(); final RaftLogReader reader = member.getLogReader();
reader.getLock().lock(); reader.getLock().lock();
try { try {
member.setNextIndex(member.getMatchIndex() + 1);
if (member.getMatchIndex() != 0) { if (member.getMatchIndex() != 0) {
reader.reset(member.getMatchIndex() + 1); reader.reset(member.getNextIndex());
} else { } else {
reader.reset(); reader.reset();
} }
Expand Down Expand Up @@ -429,7 +430,8 @@ protected void handleConfigureResponseOk(RaftMemberContext member, ConfigureRequ
succeedAttempt(member); succeedAttempt(member);


// Update the member's current configuration term and index according to the installed configuration. // Update the member's current configuration term and index according to the installed configuration.
member.setConfigTerm(request.term()).setConfigIndex(request.index()); member.setConfigTerm(request.term());
member.setConfigIndex(request.index());


// Recursively append entries to the member. // Recursively append entries to the member.
appendEntries(member); appendEntries(member);
Expand All @@ -450,7 +452,8 @@ protected void handleConfigureResponseError(RaftMemberContext member, ConfigureR
protected InstallRequest buildInstallRequest(RaftMemberContext member) { protected InstallRequest buildInstallRequest(RaftMemberContext member) {
Snapshot snapshot = server.getSnapshotStore().getSnapshotByIndex(member.getNextIndex()); Snapshot snapshot = server.getSnapshotStore().getSnapshotByIndex(member.getNextIndex());
if (member.getNextSnapshotIndex() != snapshot.index()) { if (member.getNextSnapshotIndex() != snapshot.index()) {
member.setNextSnapshotIndex(snapshot.index()).setNextSnapshotOffset(0); member.setNextSnapshotIndex(snapshot.index());
member.setNextSnapshotOffset(0);
} }


InstallRequest request; InstallRequest request;
Expand Down Expand Up @@ -522,7 +525,8 @@ protected void handleInstallRequestFailure(RaftMemberContext member, InstallRequ
protected void handleInstallResponseFailure(RaftMemberContext member, InstallRequest request, Throwable error) { protected void handleInstallResponseFailure(RaftMemberContext member, InstallRequest request, Throwable error) {
// Reset the member's snapshot index and offset to resend the snapshot from the start // Reset the member's snapshot index and offset to resend the snapshot from the start
// once a connection to the member is re-established. // once a connection to the member is re-established.
member.setNextSnapshotIndex(0).setNextSnapshotOffset(0); member.setNextSnapshotIndex(0);
member.setNextSnapshotOffset(0);


// Log the failed attempt to contact the member. // Log the failed attempt to contact the member.
failAttempt(member, error); failAttempt(member, error);
Expand Down Expand Up @@ -550,10 +554,9 @@ protected void handleInstallResponseOk(RaftMemberContext member, InstallRequest
// If the install request was completed successfully, set the member's snapshotIndex and reset // If the install request was completed successfully, set the member's snapshotIndex and reset
// the next snapshot index/offset. // the next snapshot index/offset.
if (request.complete()) { if (request.complete()) {
member member.setNextSnapshotIndex(0);
.setNextSnapshotIndex(0) member.setNextSnapshotOffset(0);
.setNextSnapshotOffset(0) member.setNextIndex(request.snapshotIndex() + 1);
.setNextIndex(request.snapshotIndex() + 1);
} }
// If more install requests remain, increment the member's snapshot offset. // If more install requests remain, increment the member's snapshot offset.
else { else {
Expand All @@ -570,7 +573,8 @@ protected void handleInstallResponseOk(RaftMemberContext member, InstallRequest
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected void handleInstallResponseError(RaftMemberContext member, InstallRequest request, InstallResponse response) { protected void handleInstallResponseError(RaftMemberContext member, InstallRequest request, InstallResponse response) {
log.warn("{} - Failed to install {}", server.getCluster().getMember().memberId(), member.getMember().memberId()); log.warn("{} - Failed to install {}", server.getCluster().getMember().memberId(), member.getMember().memberId());
member.setNextSnapshotIndex(0).setNextSnapshotOffset(0); member.setNextSnapshotIndex(0);
member.setNextSnapshotOffset(0);
} }


@Override @Override
Expand Down
2 changes: 1 addition & 1 deletion tests/src/main/resources/logback.xml
Expand Up @@ -21,7 +21,7 @@
</encoder> </encoder>
</appender> </appender>


<root level="${root.logging.level:-INFO}"> <root level="${root.logging.level:-TRACE}">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>
</configuration> </configuration>

0 comments on commit 0ee1872

Please sign in to comment.