commit | 0393d99c099d8c77c6aed22fda99058fe2eb8b3c | [log] [tgz] |
---|---|---|
author | Ian <irstevenson@gmail.com> | Sat Jan 30 14:43:39 2016 +1100 |
committer | Ian <irstevenson@gmail.com> | Sat Jan 30 14:43:39 2016 +1100 |
tree | 34a3fa0050fb0fa98da80012fcdc85554837a021 | |
parent | c8c3332eb084a937bc4001f7a78a7f44d1082df1 [diff] |
Change to unit test so that it doesn't require the setup of multiple loopback devices. Instead of using addresses in the format of `"127.0.0." + i` it now instead uses a first address of 127.0.0.1 with variable ports in the form of `50000 + i`. This way straight out of the repo you can run the tests, and to some degree achieves the same level of functional testing. Also added a small additional bit of debug logging in `GossipService` as well as some `INFO` logging in the test - as I figure it's a test that should be suitable.
Gossip protocol is a method for a group of nodes to discover and check the liveliness of a cluster. More information can be found at http://en.wikipedia.org/wiki/Gossip_protocol.
The original implementation was forked from https://code.google.com/p/java-gossip/. Several bug fixes and changes have already been added.
To gossip you need one or more seed nodes. Seed is just a list of places to initially connect to.
GossipSettings settings = new GossipSettings(); int seedNodes = 3; List<GossipMember> startupMembers = new ArrayList<>(); for (int i = 1; i < seedNodes+1; ++i) { startupMembers.add(new RemoteGossipMember("127.0.0." + i, 2000, i + "")); }
Here we start five gossip processes and check that they discover each other. (Normally these are on different hosts but here we give each process a distinct local ip.
List<GossipService> clients = new ArrayList<>(); int clusterMembers = 5; for (int i = 1; i < clusterMembers+1; ++i) { GossipService gossipService = new GossipService("127.0.0." + i, 2000, i + "", LogLevel.DEBUG, startupMembers, settings, null); clients.add(gossipService); gossipService.start(); }
Later we can check that the nodes discover each other
Thread.sleep(10000); for (int i = 0; i < clusterMembers; ++i) { Assert.assertEquals(4, clients.get(i).get_gossipManager().getMemberList().size()); }
The status can be polled using the getters that return immutable lists.
List<LocalGossipMember> getMemberList() public List<LocalGossipMember> getDeadList()
Users can also attach an event listener:
GossipService gossipService = new GossipService("127.0.0." + i, 2000, i + "", LogLevel.DEBUG, startupMembers, settings, new GossipListener(){ @Override public void gossipEvent(GossipMember member, GossipState state) { System.out.println(member+" "+ state); } });
You can get this software from maven central.
<dependency> <groupId>io.teknek</groupId> <artifactId>gossip</artifactId> <version>${pick_the_latest_version}</version> </dependency>