diff --git a/barge-core/src/main/java/org/robotninjas/barge/ClusterConfig.java b/barge-core/src/main/java/org/robotninjas/barge/ClusterConfig.java index f92fa33..2f59dd4 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/ClusterConfig.java +++ b/barge-core/src/main/java/org/robotninjas/barge/ClusterConfig.java @@ -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 remote(); - private final Replica local; - private final Iterable remote; + Replica getReplica(String info); - ClusterConfig(Replica local, Iterable 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 remote) { - return new ClusterConfig(local, remote); - } - - public Replica local() { - return local; - } - - public Iterable remote() { - return unmodifiableIterable(remote); - } - - @Override - public int hashCode() { - Iterable 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(); - } } diff --git a/barge-core/src/main/java/org/robotninjas/barge/Replica.java b/barge-core/src/main/java/org/robotninjas/barge/Replica.java index 4e54823..dc3bc2b 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/Replica.java +++ b/barge-core/src/main/java/org/robotninjas/barge/Replica.java @@ -1,86 +1,6 @@ -/** - * Copyright 2013 David Rusek - * - * 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(); - } } diff --git a/barge-core/src/main/java/org/robotninjas/barge/log/RaftJournal.java b/barge-core/src/main/java/org/robotninjas/barge/log/RaftJournal.java index 49a1a34..6c77105 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/log/RaftJournal.java +++ b/barge-core/src/main/java/org/robotninjas/barge/log/RaftJournal.java @@ -4,6 +4,7 @@ import com.google.common.collect.Maps; import journal.io.api.Journal; import journal.io.api.Location; +import org.robotninjas.barge.ClusterConfig; import org.robotninjas.barge.Replica; import org.robotninjas.barge.proto.LogProto; import org.robotninjas.barge.proto.RaftEntry; @@ -21,10 +22,12 @@ class RaftJournal { private final Journal journal; + private final ClusterConfig config; private final TreeMap entryIndex = Maps.newTreeMap(); - public RaftJournal(Journal journal) { + public RaftJournal(Journal journal, ClusterConfig config) { this.journal = journal; + this.config = config; } public void init() { @@ -200,7 +203,7 @@ public void replay(Visitor visitor) { LogProto.Vote vote = entry.getVote(); String votedfor = vote.getVotedFor(); Replica replica = votedfor == null - ? null : Replica.fromString(votedfor); + ? null : config.getReplica(votedfor); visitor.vote(Optional.fromNullable(replica)); } diff --git a/barge-core/src/main/java/org/robotninjas/barge/log/RaftLog.java b/barge-core/src/main/java/org/robotninjas/barge/log/RaftLog.java index 42d1088..ec853ff 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/log/RaftLog.java +++ b/barge-core/src/main/java/org/robotninjas/barge/log/RaftLog.java @@ -73,7 +73,7 @@ public class RaftLog { @Inject RaftLog(@Nonnull Journal journal, @Nonnull ClusterConfig config, @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 members() { return unmodifiableList(newArrayList(config.remote())); } + @Nonnull + public Replica getReplica(String info) { + return config.getReplica(info); + } + @Override public String toString() { return Objects.toStringHelper(getClass()) diff --git a/barge-core/src/main/java/org/robotninjas/barge/state/BaseState.java b/barge-core/src/main/java/org/robotninjas/barge/state/BaseState.java index e094127..d262a84 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/state/BaseState.java +++ b/barge-core/src/main/java/org/robotninjas/barge/state/BaseState.java @@ -29,7 +29,7 @@ public StateType type() { boolean shouldVoteFor(@Nonnull RaftLog log, @Nonnull RequestVote request) { Optional lastVotedFor = log.lastVotedFor(); - Replica candidate = Replica.fromString(request.getCandidateId()); + Replica candidate = log.getReplica(request.getCandidateId()); boolean hasAtLeastTerm = request.getLastLogTerm() >= log.lastLogTerm(); boolean hasAtLeastIndex = request.getLastLogIndex() >= log.lastLogIndex(); diff --git a/barge-core/src/main/java/org/robotninjas/barge/state/Candidate.java b/barge-core/src/main/java/org/robotninjas/barge/state/Candidate.java index c4cdd76..51c3198 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/state/Candidate.java +++ b/barge-core/src/main/java/org/robotninjas/barge/state/Candidate.java @@ -43,8 +43,8 @@ import static com.google.common.util.concurrent.Futures.addCallback; import static org.robotninjas.barge.proto.RaftProto.*; import static org.robotninjas.barge.state.MajorityCollector.majorityResponse; -import static org.robotninjas.barge.state.RaftPredicates.voteGranted; import static org.robotninjas.barge.state.Raft.StateType.*; +import static org.robotninjas.barge.state.RaftPredicates.voteGranted; @NotThreadSafe class Candidate extends BaseState { @@ -122,7 +122,7 @@ public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull R LOGGER.debug("RequestVote received for term {}", request.getTerm()); - Replica candidate = Replica.fromString(request.getCandidateId()); + Replica candidate = log.getReplica(request.getCandidateId()); boolean voteGranted = false; diff --git a/barge-core/src/main/java/org/robotninjas/barge/state/Follower.java b/barge-core/src/main/java/org/robotninjas/barge/state/Follower.java index 218ed27..57d3223 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/state/Follower.java +++ b/barge-core/src/main/java/org/robotninjas/barge/state/Follower.java @@ -19,10 +19,7 @@ import com.google.common.base.Optional; import com.google.common.util.concurrent.ListenableFuture; import com.google.inject.Inject; -import org.robotninjas.barge.NoLeaderException; -import org.robotninjas.barge.NotLeaderException; -import org.robotninjas.barge.RaftException; -import org.robotninjas.barge.Replica; +import org.robotninjas.barge.*; import org.robotninjas.barge.log.RaftLog; import org.robotninjas.barge.rpc.RaftScheduler; import org.slf4j.Logger; @@ -86,7 +83,7 @@ public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull R log.currentTerm(request.getTerm()); } - Replica candidate = Replica.fromString(request.getCandidateId()); + Replica candidate = log.getReplica(request.getCandidateId()); voteGranted = shouldVoteFor(log, request); if (voteGranted) { @@ -117,7 +114,7 @@ public AppendEntriesResponse appendEntries(@Nonnull RaftStateContext ctx, @Nonnu log.currentTerm(request.getTerm()); } - leader = Optional.of(Replica.fromString(request.getLeaderId())); + leader = Optional.of(log.getReplica(request.getLeaderId())); timeoutTask.reset(); success = log.append(request); diff --git a/barge-core/src/main/java/org/robotninjas/barge/state/Leader.java b/barge-core/src/main/java/org/robotninjas/barge/state/Leader.java index d7efeac..335144b 100644 --- a/barge-core/src/main/java/org/robotninjas/barge/state/Leader.java +++ b/barge-core/src/main/java/org/robotninjas/barge/state/Leader.java @@ -107,7 +107,7 @@ public RequestVoteResponse requestVote(@Nonnull RaftStateContext ctx, @Nonnull R log.currentTerm(request.getTerm()); stepDown(ctx); - Replica candidate = Replica.fromString(request.getCandidateId()); + Replica candidate = log.getReplica(request.getCandidateId()); voteGranted = shouldVoteFor(log, request); if (voteGranted) { diff --git a/barge-core/src/test/java/org/robotninjas/barge/ClusterConfigStub.java b/barge-core/src/test/java/org/robotninjas/barge/ClusterConfigStub.java new file mode 100644 index 0000000..aa7e450 --- /dev/null +++ b/barge-core/src/test/java/org/robotninjas/barge/ClusterConfigStub.java @@ -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 remote; + + private ClusterConfigStub(Iterable replicas) { + remote = Lists.newArrayList(replicas); + } + + public static ClusterConfigStub getStub(String... ids) { + return new ClusterConfigStub(FluentIterable.from(Lists.newArrayList(ids)) + .transform(new Function() { + public Replica apply(@Nullable String input) { + return new ReplicaStub(input); + } + })); + } + + @Override + public Replica local() { + return local; + } + + @Override + public Iterable 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; + } + } + +} diff --git a/barge-core/src/test/java/org/robotninjas/barge/state/BaseStateTest.java b/barge-core/src/test/java/org/robotninjas/barge/state/BaseStateTest.java index 8f067ad..a2f6e6c 100644 --- a/barge-core/src/test/java/org/robotninjas/barge/state/BaseStateTest.java +++ b/barge-core/src/test/java/org/robotninjas/barge/state/BaseStateTest.java @@ -7,6 +7,10 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.robotninjas.barge.ClusterConfig; +import org.robotninjas.barge.ClusterConfigStub; import org.robotninjas.barge.RaftException; import org.robotninjas.barge.Replica; import org.robotninjas.barge.log.RaftLog; @@ -14,17 +18,18 @@ import javax.annotation.Nonnull; -import static junit.framework.Assert.*; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import static org.robotninjas.barge.proto.RaftProto.RequestVote; public class BaseStateTest { - private final Replica self = Replica.fromString("localhost:8001"); - private final Replica candidate = Replica.fromString("localhost:8000"); - private - @Mock - RaftLog mockRaftLog; + private final ClusterConfig config = ClusterConfigStub.getStub(); + private final Replica self = config.local(); + private final Replica candidate = config.getReplica("candidate"); + private @Mock RaftLog mockRaftLog; @Before public void initMocks() { @@ -35,6 +40,14 @@ public void initMocks() { when(mockRaftLog.lastLogIndex()).thenReturn(2l); when(mockRaftLog.lastLogTerm()).thenReturn(2l); when(mockRaftLog.self()).thenReturn(self); + when(mockRaftLog.config()).thenReturn(config); + when(mockRaftLog.getReplica(anyString())).thenAnswer(new Answer() { + @Override + public Replica answer(InvocationOnMock invocation) throws Throwable { + String arg = (String) invocation.getArguments()[0]; + return config.getReplica(arg); + } + }); } @@ -87,7 +100,7 @@ public void testCandidateWithGreaterTerm() { .setTerm(2) .build(); - Replica otherCandidate = Replica.fromString("localhost:8002"); + Replica otherCandidate = config.getReplica("other"); when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate)); boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote); @@ -106,7 +119,7 @@ public void testCandidateWithLesserTerm() { .setTerm(2) .build(); - Replica otherCandidate = Replica.fromString("localhost:8002"); + Replica otherCandidate = config.getReplica("other"); when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate)); boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote); @@ -125,7 +138,7 @@ public void testCandidateWithLesserIndex() { .setTerm(2) .build(); - Replica otherCandidate = Replica.fromString("localhost:8002"); + Replica otherCandidate = config.getReplica("other"); when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate)); boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote); @@ -145,7 +158,7 @@ public void testCandidateWithGreaterIndex() { .setTerm(2) .build(); - Replica otherCandidate = Replica.fromString("localhost:8002"); + Replica otherCandidate = config.getReplica("other"); when(mockRaftLog.lastVotedFor()).thenReturn(Optional.of(otherCandidate)); boolean shouldVote = state.shouldVoteFor(mockRaftLog, requestVote); diff --git a/barge-core/src/test/java/org/robotninjas/barge/state/CandidateTest.java b/barge-core/src/test/java/org/robotninjas/barge/state/CandidateTest.java index db92f22..57961c4 100644 --- a/barge-core/src/test/java/org/robotninjas/barge/state/CandidateTest.java +++ b/barge-core/src/test/java/org/robotninjas/barge/state/CandidateTest.java @@ -5,8 +5,9 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robotninjas.barge.Replica; -import org.robotninjas.barge.StateMachine; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.robotninjas.barge.*; import org.robotninjas.barge.log.RaftLog; import org.robotninjas.barge.proto.RaftProto; import org.robotninjas.barge.rpc.Client; @@ -24,15 +25,14 @@ public class CandidateTest { private final long term = 2L; - private final Replica self = Replica.fromString("localhost:1000"); + private final ClusterConfig config = ClusterConfigStub.getStub(); + private final Replica self = config.local(); private @Mock ScheduledExecutorService mockScheduler; - private @Mock Replica mockReplica; private @Mock Client mockRaftClient; private @Mock StateMachine mockStateMachine; private @Mock RaftLog mockRaftLog; - private @Mock - RaftStateContext mockRaftStateContext; + private @Mock RaftStateContext mockRaftStateContext; @Before public void initMocks() { @@ -44,6 +44,14 @@ public void initMocks() { when(mockRaftLog.lastLogTerm()).thenReturn(0L); when(mockRaftLog.lastLogIndex()).thenReturn(0L); when(mockRaftLog.currentTerm()).thenReturn(term); + when(mockRaftLog.config()).thenReturn(config); + when(mockRaftLog.getReplica(anyString())).thenAnswer(new Answer() { + @Override + public Replica answer(InvocationOnMock invocation) throws Throwable { + String arg = (String) invocation.getArguments()[0]; + return config.getReplica(arg); + } + }); ScheduledFuture mockScheduledFuture = mock(ScheduledFuture.class); when(mockScheduler.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))) @@ -57,7 +65,7 @@ public void testRequestVoteWithNewerTerm() throws Exception { Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 150, mockRaftClient); candidate.init(mockRaftStateContext); - Replica mockCandidate = Replica.fromString("localhost:10001"); + Replica mockCandidate = config.getReplica("other"); RequestVote request = RequestVote.newBuilder() @@ -91,7 +99,7 @@ public void testRequestVoteWithOlderTerm() throws Exception { Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 150, mockRaftClient); candidate.init(mockRaftStateContext); - Replica mockCandidate = Replica.fromString("localhost:10001"); + Replica mockCandidate = config.getReplica("other"); RequestVote request = RequestVote.newBuilder() @@ -122,7 +130,7 @@ public void testRequestVoteWithSameTerm() throws Exception { Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 150, mockRaftClient); candidate.init(mockRaftStateContext); - Replica mockCandidate = Replica.fromString("localhost:10001"); + Replica mockCandidate = config.getReplica("other"); RequestVote request = RequestVote.newBuilder() @@ -156,7 +164,7 @@ public void testAppendEntriesWithNewerTerm() throws Exception { Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 1, mockRaftClient); candidate.init(mockRaftStateContext); - Replica mockLeader = Replica.fromString("localhost:10001"); + Replica mockLeader = config.getReplica("other"); AppendEntries request = AppendEntries.newBuilder() @@ -191,7 +199,7 @@ public void testAppendEntriesWithOlderTerm() throws Exception { Candidate candidate = new Candidate(mockRaftLog, mockScheduler, 1, mockRaftClient); candidate.init(mockRaftStateContext); - Replica mockLeader = Replica.fromString("localhost:10001"); + Replica mockLeader = config.getReplica("other"); AppendEntries request = RaftProto.AppendEntries.newBuilder() diff --git a/barge-core/src/test/java/org/robotninjas/barge/state/ReplicaManagerTest.java b/barge-core/src/test/java/org/robotninjas/barge/state/ReplicaManagerTest.java index 01326de..f65c02d 100644 --- a/barge-core/src/test/java/org/robotninjas/barge/state/ReplicaManagerTest.java +++ b/barge-core/src/test/java/org/robotninjas/barge/state/ReplicaManagerTest.java @@ -8,6 +8,8 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robotninjas.barge.ClusterConfig; +import org.robotninjas.barge.ClusterConfigStub; import org.robotninjas.barge.Replica; import org.robotninjas.barge.log.GetEntriesResult; import org.robotninjas.barge.log.RaftLog; @@ -25,8 +27,9 @@ public class ReplicaManagerTest { - private static final Replica SELF = Replica.fromString("localhost:8080"); - private static final Replica FOLLOWER = Replica.fromString("localhost:1000"); + private static final ClusterConfig config = ClusterConfigStub.getStub(); + private static final Replica SELF = config.local(); + private static final Replica FOLLOWER = config.getReplica("remote"); private @Mock diff --git a/barge-rpc-proto/pom.xml b/barge-rpc-proto/pom.xml index 3cfaf86..515c2c3 100644 --- a/barge-rpc-proto/pom.xml +++ b/barge-rpc-proto/pom.xml @@ -10,7 +10,7 @@ 4.0.0 barge-rpc-proto - Handles Protocol-buffer based RPC + Main entry point to instantiate a fully functional barge service @@ -19,45 +19,20 @@ barge-core - - io.netty - netty-all - - - org.robotninjas protorpc - commons-pool - commons-pool - - - - com.google.guava - guava - - - - com.google.inject - guice - - - - - com.google.code.findbugs - jsr305 + org.assertj + assertj-core junit junit - test - - - \ No newline at end of file + diff --git a/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyClusterConfig.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyClusterConfig.java new file mode 100644 index 0000000..ff82654 --- /dev/null +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyClusterConfig.java @@ -0,0 +1,72 @@ +package org.robotninjas.barge; + +import com.google.common.base.Objects; +import com.google.common.collect.Iterables; + +import static com.google.common.collect.Iterables.unmodifiableIterable; +import static com.google.common.collect.Lists.newArrayList; + +public class NettyClusterConfig implements ClusterConfig { + + private final NettyReplica local; + private final Iterable remote; + + NettyClusterConfig(NettyReplica local, Iterable remote) { + this.local = local; + this.remote = remote; + } + + public static NettyClusterConfig from(NettyReplica local, Replica... remote) { + return new NettyClusterConfig(local, newArrayList(remote)); + } + + public static ClusterConfig from(NettyReplica local, Iterable remote) { + return new NettyClusterConfig(local, remote); + } + + @Override + public NettyReplica local() { + return local; + } + + @Override + public Iterable remote() { + return unmodifiableIterable(remote); + } + + @Override + public NettyReplica getReplica(String info) { + return NettyReplica.fromString(info); + } + + @Override + public int hashCode() { + Iterable 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 NettyClusterConfig)) { + return false; + } + + NettyClusterConfig other = (NettyClusterConfig) 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(); + } +} diff --git a/barge-service/src/main/java/org/robotninjas/barge/NettyRaftModule.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftModule.java similarity index 82% rename from barge-service/src/main/java/org/robotninjas/barge/NettyRaftModule.java rename to barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftModule.java index c813cd7..62055c4 100644 --- a/barge-service/src/main/java/org/robotninjas/barge/NettyRaftModule.java +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftModule.java @@ -6,12 +6,12 @@ public class NettyRaftModule extends PrivateModule { - private final ClusterConfig config; + private final NettyClusterConfig config; private final File logDir; private final StateMachine stateMachine; private final long timeout; - public NettyRaftModule(ClusterConfig config, File logDir, StateMachine stateMachine, long timeout) { + public NettyRaftModule(NettyClusterConfig config, File logDir, StateMachine stateMachine, long timeout) { this.config = config; this.logDir = logDir; this.stateMachine = stateMachine; diff --git a/barge-service/src/main/java/org/robotninjas/barge/NettyRaftService.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftService.java similarity index 96% rename from barge-service/src/main/java/org/robotninjas/barge/NettyRaftService.java rename to barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftService.java index 5208cd5..b117454 100644 --- a/barge-service/src/main/java/org/robotninjas/barge/NettyRaftService.java +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyRaftService.java @@ -124,7 +124,7 @@ public Object commit(final byte[] operation) throws RaftException, InterruptedEx } } - public static Builder newBuilder(ClusterConfig config) { + public static Builder newBuilder(NettyClusterConfig config) { return new Builder(config); } @@ -136,12 +136,12 @@ public static class Builder { private static long TIMEOUT = 150; - private final ClusterConfig config; + private final NettyClusterConfig config; private File logDir = Files.createTempDir(); private long timeout = TIMEOUT; private StateTransitionListener listener; - protected Builder(ClusterConfig config) { + protected Builder(NettyClusterConfig config) { this.config = config; } diff --git a/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyReplica.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyReplica.java new file mode 100644 index 0000000..2437537 --- /dev/null +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/NettyReplica.java @@ -0,0 +1,86 @@ +/** + * Copyright 2013 David Rusek + * + * 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 NettyReplica implements Replica { + + private final InetSocketAddress address; + + NettyReplica(@Nonnull InetSocketAddress address) { + this.address = checkNotNull(address); + } + + @Nonnull + public static NettyReplica 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 NettyReplica(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 NettyReplica) { + NettyReplica other = (NettyReplica) o; + return Objects.equal(address(), other.address()); + } + + return false; + + } + + @Nonnull + @Override + public String toString() { + return address.getAddress().getHostName() + ":" + address.getPort(); + } +} diff --git a/barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftProtoRpcModule.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftProtoRpcModule.java index fa4e998..c0c58e4 100644 --- a/barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftProtoRpcModule.java +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftProtoRpcModule.java @@ -14,10 +14,10 @@ class RaftProtoRpcModule extends PrivateModule { - private final Replica localEndpoint; + private final NettyReplica localEndpoint; private Optional eventLoopGroup = Optional.absent(); - public RaftProtoRpcModule(Replica localEndpoint) { + public RaftProtoRpcModule(NettyReplica localEndpoint) { this.localEndpoint = localEndpoint; } diff --git a/barge-service/src/main/java/org/robotninjas/barge/RaftServiceEndpoint.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftServiceEndpoint.java similarity index 100% rename from barge-service/src/main/java/org/robotninjas/barge/RaftServiceEndpoint.java rename to barge-rpc-proto/src/main/java/org/robotninjas/barge/RaftServiceEndpoint.java diff --git a/barge-rpc-proto/src/main/java/org/robotninjas/barge/rpc/RpcChannelFactory.java b/barge-rpc-proto/src/main/java/org/robotninjas/barge/rpc/RpcChannelFactory.java index 42412cb..7d8180c 100644 --- a/barge-rpc-proto/src/main/java/org/robotninjas/barge/rpc/RpcChannelFactory.java +++ b/barge-rpc-proto/src/main/java/org/robotninjas/barge/rpc/RpcChannelFactory.java @@ -20,7 +20,7 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import org.apache.commons.pool.BaseKeyedPoolableObjectFactory; -import org.robotninjas.barge.Replica; +import org.robotninjas.barge.NettyReplica; import org.robotninjas.protobuf.netty.client.NettyRpcChannel; import org.robotninjas.protobuf.netty.client.RpcClient; import org.slf4j.Logger; @@ -44,8 +44,8 @@ public RpcChannelFactory(@Nonnull RpcClient client) { @Override public ListenableFuture makeObject(Object key) throws Exception { - Preconditions.checkArgument(key instanceof Replica); - Replica replica = (Replica) key; + Preconditions.checkArgument(key instanceof NettyReplica); + NettyReplica replica = (NettyReplica) key; return client.connectAsync(replica.address()); } diff --git a/barge-service/src/test/java/org/robotninjas/barge/GroupOfCounters.java b/barge-rpc-proto/src/test/java/org/robotninjas/barge/GroupOfCounters.java similarity index 97% rename from barge-service/src/test/java/org/robotninjas/barge/GroupOfCounters.java rename to barge-rpc-proto/src/test/java/org/robotninjas/barge/GroupOfCounters.java index 54bb5ee..220a98e 100644 --- a/barge-service/src/test/java/org/robotninjas/barge/GroupOfCounters.java +++ b/barge-rpc-proto/src/test/java/org/robotninjas/barge/GroupOfCounters.java @@ -32,7 +32,7 @@ public class GroupOfCounters extends ExternalResource implements StateTransitionListener { - private final List replicas; + private final List replicas; private final List counters; private final File target; private final Map states = Maps.newConcurrentMap(); @@ -44,7 +44,7 @@ public GroupOfCounters(int numberOfReplicas, File target) { counters = Lists.newArrayList(); for (int i = 10001; i <= (10000 + numberOfReplicas); i++) { - replicas.add(Replica.fromString("localhost:" + i)); + replicas.add(NettyReplica.fromString("localhost:" + i)); counters.add(new SimpleCounterMachine(i - 10001, replicas, this)); } } diff --git a/barge-service/src/test/java/org/robotninjas/barge/NettyRaftServiceTest.java b/barge-rpc-proto/src/test/java/org/robotninjas/barge/NettyRaftServiceTest.java similarity index 100% rename from barge-service/src/test/java/org/robotninjas/barge/NettyRaftServiceTest.java rename to barge-rpc-proto/src/test/java/org/robotninjas/barge/NettyRaftServiceTest.java diff --git a/barge-service/src/test/java/org/robotninjas/barge/Prober.java b/barge-rpc-proto/src/test/java/org/robotninjas/barge/Prober.java similarity index 100% rename from barge-service/src/test/java/org/robotninjas/barge/Prober.java rename to barge-rpc-proto/src/test/java/org/robotninjas/barge/Prober.java diff --git a/barge-service/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java b/barge-rpc-proto/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java similarity index 89% rename from barge-service/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java rename to barge-rpc-proto/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java index 205d1f5..8365f99 100644 --- a/barge-service/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java +++ b/barge-rpc-proto/src/test/java/org/robotninjas/barge/SimpleCounterMachine.java @@ -12,14 +12,14 @@ public class SimpleCounterMachine implements StateMachine { private final int id; - private final List replicas; + private final List replicas; private final GroupOfCounters groupOfCounters; private int counter; private File logDirectory; private NettyRaftService service; - public SimpleCounterMachine(int id, List replicas, GroupOfCounters groupOfCounters) { + public SimpleCounterMachine(int id, List replicas, GroupOfCounters groupOfCounters) { checkArgument(id >= 0 && id < replicas.size(), "replica id " + id + " should be between 0 and " + replicas.size()); this.groupOfCounters = groupOfCounters; @@ -46,12 +46,12 @@ public Object applyOperation(@Nonnull ByteBuffer entry) { public void startRaft() { int clusterSize = replicas.size(); - Replica[] configuration = new Replica[clusterSize - 1]; + NettyReplica[] configuration = new NettyReplica[clusterSize - 1]; for (int i = 0; i < clusterSize - 1; i++) { configuration[i] = replicas.get((id + i + 1) % clusterSize); } - ClusterConfig config1 = ClusterConfig.from(replicas.get(id % clusterSize), configuration); + NettyClusterConfig config1 = NettyClusterConfig.from(replicas.get(id % clusterSize), configuration); NettyRaftService service1 = NettyRaftService.newBuilder(config1) .logDir(logDirectory) diff --git a/barge-service/pom.xml b/barge-service/pom.xml deleted file mode 100644 index c22d2b8..0000000 --- a/barge-service/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - barge - org.robotninjas.barge - 0.1.0-alpha2-SNAPSHOT - - 4.0.0 - - barge-service - Main entry point to instantiate a fully functional barge service - - - - - org.robotninjas.barge - barge-core - - - - org.robotninjas.barge - barge-rpc-proto - - - - org.assertj - assertj-core - - - - junit - junit - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 35d167d..ab6ec83 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,6 @@ barge-core barge-tools barge-rpc-proto - barge-service