GOSSIP-2 rebase

This commit is contained in:
Edward Capriolo
2016-06-07 22:52:20 -04:00
parent ddc9a67dd6
commit 900bfda955
3 changed files with 186 additions and 0 deletions

View File

@ -60,8 +60,20 @@ public class GossipService {
public GossipService(String cluster, URI uri, String id,
List<GossipMember> gossipMembers, GossipSettings settings, GossipListener listener)
throws InterruptedException, UnknownHostException {
<<<<<<< HEAD
gossipManager = new RandomGossipManager(cluster, uri, id, settings, gossipMembers,
listener);
=======
gossipManager = RandomGossipManager.newBuilder()
.withId(id)
.cluster(cluster)
.address(ipAddress)
.port(port)
.settings(settings)
.gossipMembers(gossipMembers)
.listener(listener)
.build();
>>>>>>> fe196cd... GOSSIP-4: Use builder to create RandomGossipManager (Jaideep Dhok via EGC)
}
public void start() {

View File

@ -23,11 +23,89 @@ import org.apache.gossip.event.GossipListener;
import org.apache.gossip.manager.GossipManager;
import org.apache.gossip.manager.impl.OnlyProcessReceivedPassiveGossipThread;
<<<<<<< HEAD
import java.net.URI;
import java.util.List;
public class RandomGossipManager extends GossipManager {
public RandomGossipManager(String cluster, URI uri, String id,
=======
import java.util.ArrayList;
import java.util.List;
public class RandomGossipManager extends GossipManager {
public static ManagerBuilder newBuilder() {
return new ManagerBuilder();
}
public static final class ManagerBuilder {
private String cluster;
private String address;
private int port;
private String id;
private GossipSettings settings;
private List<GossipMember> gossipMembers;
private GossipListener listener;
private ManagerBuilder() {}
private void checkArgument(boolean check, String msg) {
if (!check) {
throw new IllegalArgumentException(msg);
}
}
public ManagerBuilder cluster(String cluster) {
this.cluster = cluster;
return this;
}
public ManagerBuilder address(String address) {
this.address = address;
return this;
}
public ManagerBuilder port(int port) {
this.port = port;
return this;
}
public ManagerBuilder withId(String id) {
this.id = id;
return this;
}
public ManagerBuilder settings(GossipSettings settings) {
this.settings = settings;
return this;
}
public ManagerBuilder gossipMembers(List<GossipMember> members) {
this.gossipMembers = members;
return this;
}
public ManagerBuilder listener(GossipListener listener) {
this.listener = listener;
return this;
}
public RandomGossipManager build() {
checkArgument(id != null, "You must specify an id");
checkArgument(cluster != null, "You must specify a cluster name");
checkArgument(settings != null, "You must specify gossip settings");
if (this.gossipMembers == null) {
this.gossipMembers = new ArrayList<>();
}
return new RandomGossipManager(cluster, address, port, id, settings, gossipMembers, listener);
}
}
private RandomGossipManager(String cluster, String address, int port, String id,
>>>>>>> fe196cd... GOSSIP-4: Use builder to create RandomGossipManager (Jaideep Dhok via EGC)
GossipSettings settings, List<GossipMember> gossipMembers, GossipListener listener) {
super(OnlyProcessReceivedPassiveGossipThread.class, RandomActiveGossipThread.class, cluster,
uri, id, settings, gossipMembers, listener);

View File

@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.gossip.manager;
import org.apache.gossip.GossipMember;
import org.apache.gossip.GossipSettings;
import org.apache.gossip.LocalGossipMember;
import org.apache.gossip.event.GossipListener;
import org.apache.gossip.event.GossipState;
import org.apache.gossip.manager.random.RandomGossipManager;
import org.junit.Assert;
import org.junit.Test;
import javax.management.Notification;
import javax.management.NotificationListener;
import java.util.ArrayList;
import java.util.List;
public class RandomGossipManagerBuilderTest {
public static class TestGossipListener implements GossipListener {
@Override
public void gossipEvent(GossipMember member, GossipState state) {
System.out.println("Got gossip event");
}
}
public static class TestNotificationListener implements NotificationListener {
@Override
public void handleNotification(Notification notification, Object o) {
System.out.println("Got notification event");
}
}
@Test(expected=IllegalArgumentException.class)
public void idShouldNotBeNull() {
RandomGossipManager.newBuilder().cluster("aCluster").build();
}
@Test(expected=IllegalArgumentException.class)
public void clusterShouldNotBeNull() {
RandomGossipManager.newBuilder().withId("id").build();
}
@Test(expected=IllegalArgumentException.class)
public void settingsShouldNotBeNull() {
RandomGossipManager.newBuilder().withId("id").cluster("aCluster").build();
}
@Test
public void createMembersListIfNull() {
RandomGossipManager gossipManager = RandomGossipManager.newBuilder()
.withId("id")
.cluster("aCluster")
.port(8080)
.address("localhost")
.settings(new GossipSettings())
.gossipMembers(null).build();
Assert.assertNotNull(gossipManager.getMemberList());
}
@Test
public void useMemberListIfProvided() {
LocalGossipMember member = new LocalGossipMember("aCluster", "localhost", 2000, "aGossipMember",
System.currentTimeMillis(), new TestNotificationListener(), 60000);
List<GossipMember> memberList = new ArrayList<>();
memberList.add(member);
RandomGossipManager gossipManager = RandomGossipManager.newBuilder()
.withId("id")
.cluster("aCluster")
.settings(new GossipSettings())
.gossipMembers(memberList).build();
Assert.assertEquals(1, gossipManager.getMemberList().size());
Assert.assertEquals(member.getId(), gossipManager.getMemberList().get(0).getId());
}
}