This repository was archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Consolidate projects and break out Config #59
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 4 additions & 58 deletions
62
barge-core/src/main/java/org/robotninjas/barge/ClusterConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,11 @@ | ||
| package org.robotninjas.barge; | ||
|
|
||
| import com.google.common.base.Objects; | ||
| import com.google.common.collect.Iterables; | ||
| public interface ClusterConfig { | ||
|
|
||
| import static com.google.common.collect.Iterables.unmodifiableIterable; | ||
| import static com.google.common.collect.Lists.newArrayList; | ||
| Replica local(); | ||
|
|
||
| public class ClusterConfig { | ||
| Iterable<Replica> remote(); | ||
|
|
||
| private final Replica local; | ||
| private final Iterable<Replica> remote; | ||
| Replica getReplica(String info); | ||
|
|
||
| ClusterConfig(Replica local, Iterable<Replica> remote) { | ||
| this.local = local; | ||
| this.remote = remote; | ||
| } | ||
|
|
||
| public static ClusterConfig from(Replica local, Replica... remote) { | ||
| return new ClusterConfig(local, newArrayList(remote)); | ||
| } | ||
|
|
||
| public static ClusterConfig from(Replica local, Iterable<Replica> remote) { | ||
| return new ClusterConfig(local, remote); | ||
| } | ||
|
|
||
| public Replica local() { | ||
| return local; | ||
| } | ||
|
|
||
| public Iterable<Replica> remote() { | ||
| return unmodifiableIterable(remote); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| Iterable<Replica> all = Iterables.concat(newArrayList(local), remote); | ||
| return Objects.hashCode(Iterables.toArray(all, Replica.class)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
|
|
||
| if (o == this) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!(o instanceof ClusterConfig)) { | ||
| return false; | ||
| } | ||
|
|
||
| ClusterConfig other = (ClusterConfig) o; | ||
| return local.equals(other.local) && | ||
| Iterables.elementsEqual(remote, other.remote); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return Objects.toStringHelper(this) | ||
| .add("local", local) | ||
| .add("remote", remote) | ||
| .toString(); | ||
| } | ||
| } |
82 changes: 1 addition & 81 deletions
82
barge-core/src/main/java/org/robotninjas/barge/Replica.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,86 +1,6 @@ | ||
| /** | ||
| * Copyright 2013 David Rusek <dave dot rusek at gmail dot com> | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.robotninjas.barge; | ||
|
|
||
| import com.google.common.base.Objects; | ||
| import com.google.common.base.Throwables; | ||
| import com.google.common.net.HostAndPort; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.concurrent.Immutable; | ||
| import javax.annotation.concurrent.ThreadSafe; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.net.UnknownHostException; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| @Immutable | ||
| @ThreadSafe | ||
| public class Replica { | ||
|
|
||
| private final InetSocketAddress address; | ||
|
|
||
| Replica(@Nonnull InetSocketAddress address) { | ||
| this.address = checkNotNull(address); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public static Replica fromString(@Nonnull String info) { | ||
| try { | ||
| checkNotNull(info); | ||
| HostAndPort hostAndPort = HostAndPort.fromString(info); | ||
| InetAddress addr = InetAddress.getByName(hostAndPort.getHostText()); | ||
| InetSocketAddress saddr = new InetSocketAddress(addr, hostAndPort.getPort()); | ||
| return new Replica(saddr); | ||
| } catch (UnknownHostException e) { | ||
| throw Throwables.propagate(e); | ||
| } | ||
| } | ||
|
|
||
| public SocketAddress address() { | ||
| return address; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(address()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
|
|
||
| if (o == this) { | ||
| return true; | ||
| } | ||
|
|
||
| if (o instanceof Replica) { | ||
| Replica other = (Replica) o; | ||
| return Objects.equal(address(), other.address()); | ||
| } | ||
|
|
||
| return false; | ||
| public interface Replica { | ||
|
|
||
| } | ||
|
|
||
| @Nonnull | ||
| @Override | ||
| public String toString() { | ||
| return address.getAddress().getHostName() + ":" + address.getPort(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ public class RaftLog { | |
| @Inject | ||
| RaftLog(@Nonnull Journal journal, @Nonnull ClusterConfig config, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we only use
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, however, it's not necessarily as easy as that, the interaction between RaftJournal and RaftLog is (too) complex. I think we should address this separately. |
||
| @Nonnull StateMachineProxy stateMachine) { | ||
| this.journal = new RaftJournal(checkNotNull(journal)); | ||
| this.journal = new RaftJournal(checkNotNull(journal), checkNotNull(config)); | ||
| this.config = checkNotNull(config); | ||
| this.stateMachine = checkNotNull(stateMachine); | ||
| } | ||
|
|
@@ -204,6 +204,10 @@ public long commitIndex() { | |
| return commitIndex; | ||
| } | ||
|
|
||
| public ClusterConfig config() { | ||
| return config; | ||
| } | ||
|
|
||
| public void commitIndex(long index) { | ||
| commitIndex = index; | ||
| journal.appendCommit(index); | ||
|
|
@@ -243,6 +247,11 @@ public List<Replica> members() { | |
| return unmodifiableList(newArrayList(config.remote())); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Replica getReplica(String info) { | ||
| return config.getReplica(info); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return Objects.toStringHelper(getClass()) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
barge-core/src/test/java/org/robotninjas/barge/ClusterConfigStub.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package org.robotninjas.barge; | ||
|
|
||
| import com.google.common.base.Function; | ||
| import com.google.common.base.Objects; | ||
| import com.google.common.collect.FluentIterable; | ||
| import com.google.common.collect.Lists; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| public class ClusterConfigStub implements ClusterConfig { | ||
|
|
||
| private final Replica local = new ReplicaStub("local"); | ||
| private final Iterable<Replica> remote; | ||
|
|
||
| private ClusterConfigStub(Iterable<Replica> replicas) { | ||
| remote = Lists.newArrayList(replicas); | ||
| } | ||
|
|
||
| public static ClusterConfigStub getStub(String... ids) { | ||
| return new ClusterConfigStub(FluentIterable.from(Lists.newArrayList(ids)) | ||
| .transform(new Function<String, Replica>() { | ||
| public Replica apply(@Nullable String input) { | ||
| return new ReplicaStub(input); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| @Override | ||
| public Replica local() { | ||
| return local; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<Replica> remote() { | ||
| return remote; | ||
| } | ||
|
|
||
| @Override | ||
| public Replica getReplica(String info) { | ||
| return new ReplicaStub(info); | ||
| } | ||
|
|
||
| private static class ReplicaStub implements Replica { | ||
|
|
||
| private final String info; | ||
|
|
||
| private ReplicaStub(String info) { | ||
| this.info = info; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(info); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
|
|
||
| if (o == this) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!(o instanceof ReplicaStub)) { | ||
| return false; | ||
| } | ||
|
|
||
| ReplicaStub stub = (ReplicaStub) o; | ||
| return Objects.equal(stub.info, info); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return info; | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment than for ClusterConfig: There's no need to declare hashcode/equals/toString methods here...