Skip to content

Commit

Permalink
Add firstIndex arguments to sync and append requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jan 22, 2015
1 parent 5126b0a commit eea1b02
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
24 changes: 23 additions & 1 deletion core/src/main/java/net/kuujo/copycat/protocol/AppendRequest.java
Expand Up @@ -52,6 +52,7 @@ public static Builder builder(AppendRequest request) {
private Long logIndex;
private Long logTerm;
private List<ByteBuffer> entries;
private boolean firstIndex;
private Long commitIndex;

/**
Expand Down Expand Up @@ -99,6 +100,15 @@ public List<ByteBuffer> entries() {
return entries;
}

/**
* Returns a boolean indicating whether the first entry is the first index in the log.
*
* @return Indicates whether the first entry is the first index in the log.
*/
public boolean firstIndex() {
return firstIndex;
}

/**
* Returns the leader's commit index.
*
Expand All @@ -110,7 +120,7 @@ public Long commitIndex() {

@Override
public int hashCode() {
return Objects.hash(id, member, term, leader, logIndex, logTerm, entries, commitIndex);
return Objects.hash(id, member, term, leader, logIndex, logTerm, entries, firstIndex, commitIndex);
}

@Override
Expand All @@ -124,6 +134,7 @@ public boolean equals(Object object) {
&& request.logIndex.equals(logIndex)
&& request.logTerm.equals(logTerm)
&& request.entries.equals(entries)
&& request.firstIndex == firstIndex
&& request.commitIndex.equals(commitIndex);
}
return false;
Expand Down Expand Up @@ -211,6 +222,17 @@ public Builder withEntries(List<ByteBuffer> entries) {
return this;
}

/**
* Sets whether the first entry is the first index in the log.
*
* @param firstIndex Whether the first entry is the first index in the log.
* @return The append request builder.
*/
public Builder withFirstIndex(boolean firstIndex) {
request.firstIndex = firstIndex;
return this;
}

/**
* Sets the request commit index.
*
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/net/kuujo/copycat/protocol/SyncRequest.java
Expand Up @@ -52,7 +52,7 @@ public static Builder builder(SyncRequest request) {
private long term;
private String leader;
private Long logIndex;
private Long commitIndex;
private boolean firstIndex;
private List<ByteBuffer> entries;
private Collection<ReplicaInfo> members;

Expand Down Expand Up @@ -84,12 +84,12 @@ public Long logIndex() {
}

/**
* Returns the requesting node's commit index.
* Returns a boolean value indicating whether the first entry is the first index in the replicator's log.
*
* @return The requesting node's commit index.
* @return Indicates whether the first entry is the first index in the replicator's log.
*/
public Long commitIndex() {
return commitIndex;
public boolean firstIndex() {
return firstIndex;
}

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ public boolean equals(Object object) {
&& request.term == term
&& request.leader.equals(leader)
&& request.logIndex.equals(logIndex)
&& request.commitIndex.equals(commitIndex)
&& request.firstIndex == firstIndex
&& request.entries.equals(entries)
&& request.members.equals(members);
}
Expand All @@ -133,7 +133,7 @@ public boolean equals(Object object) {

@Override
public String toString() {
return String.format("%s[id=%s, term=%d, leader=%s, logIndex=%s, commitIndex=%s, entries=[...]]", getClass().getSimpleName(), id, term, leader, logIndex, commitIndex);
return String.format("%s[id=%s, term=%d, leader=%s, logIndex=%s, firstIndex=%b, entries=[...]]", getClass().getSimpleName(), id, term, leader, logIndex, firstIndex);
}

/**
Expand Down Expand Up @@ -203,13 +203,13 @@ public Builder withLogIndex(Long index) {
}

/**
* Sets the request commit index.
* Sets whether the first entry is the first index in the log.
*
* @param commitIndex The request commit index.
* @param firstIndex Whether the first entry is the first index in the log.
* @return The request builder.
*/
public Builder withCommitIndex(Long commitIndex) {
request.commitIndex = commitIndex;
public Builder withFirstIndex(boolean firstIndex) {
request.firstIndex = firstIndex;
return this;
}

Expand Down

0 comments on commit eea1b02

Please sign in to comment.