Permalink
Browse files

Allowing the Candidate to use a custom Supplier of data stored in znode

  • Loading branch information...
1 parent 60e3ca5 commit 45565b28afde2d39dc68adb6ac593015d8917bcc @florianleibert committed Sep 12, 2012
@@ -33,12 +33,12 @@
/**
* Returns the current group leader by querying ZooKeeper synchronously.
*
- * @return the id of the current group leader or {@code null} if there is none
+ * @return the data of the current group leader's znode or {@code null} if there is none
* @throws ZooKeeperConnectionException if there was a problem connecting to ZooKeeper
* @throws KeeperException if there was a problem reading the leader information
* @throws InterruptedException if this thread is interrupted getting the leader
*/
- public String getLeaderId()
+ public String getLeaderData()
throws ZooKeeperConnectionException, KeeperException, InterruptedException;
/**
@@ -50,7 +50,7 @@
public class CandidateImpl implements Candidate {
private static final Logger LOG = Logger.getLogger(CandidateImpl.class.getName());
- private static final String UNKOWN_CANDIDATE_ADDRESS = "<unknown>";
+ private static final String UNKOWN_CANDIDATE_DATA = "<unknown>";
private static final Function<Iterable<String>, String> MOST_RECENT_JUDGE =
new Function<Iterable<String>, String>() {
@@ -61,29 +61,41 @@
private final Group group;
private final Function<Iterable<String>, String> judge;
+ private final Supplier<byte[]> dataSupplier;
/**
- * Equivalent to {@link #CandidateImpl(Group, com.google.common.base.Function)} using a judge that
+ * Equivalent to {@link #CandidateImpl(Group, com.google.common.base.Function, Supplier)} using a judge that
* always picks the lowest numbered candidate ephemeral node - by proxy the oldest or 1st
- * candidate.
+ * candidate and a default supplier that populates the data in the underlying znode with the ip address of this host.
*/
public CandidateImpl(Group group) {
- this(group, MOST_RECENT_JUDGE);
+ this(group, MOST_RECENT_JUDGE, new Supplier<byte[]>() {
@jsirois

jsirois Sep 12, 2012

Use the no unused private getAddress inside this supplier or kill that helper

+ @Override
+ public byte[] get() {
+ try {
+ return InetAddress.getLocalHost().getHostAddress().getBytes();
+ } catch (UnknownHostException e) {
+ LOG.log(Level.WARNING, "Failed to determine local address!", e);
+ return UNKOWN_CANDIDATE_DATA.getBytes();
+ }
+ }
+ });
}
/**
* Creates a candidate that can be used to offer leadership for the given {@code group}. The
* {@code judge} is used to pick the current leader from all group members whenever the group
- * membership changes. To form a well-behaved election group with one leader, all candidates
+ * membership changes. To form a well-behaved election group with one leader, all candidates
* should use the same judge.
@jsirois

jsirois Sep 12, 2012

Please describe what dataSupplier is for / is expected to do

*/
- public CandidateImpl(Group group, Function<Iterable<String>, String> judge) {
+ public CandidateImpl(Group group, Function<Iterable<String>, String> judge, Supplier<byte[]> dataSupplier) {
this.group = Preconditions.checkNotNull(group);
this.judge = Preconditions.checkNotNull(judge);
+ this.dataSupplier = Preconditions.checkNotNull(dataSupplier);
}
@Override
- public String getLeaderId()
+ public String getLeaderData()
throws ZooKeeperConnectionException, KeeperException, InterruptedException {
String leaderId = getLeader(group.getMemberIds());
@@ -92,15 +104,14 @@ public String getLeaderId()
}
byte[] data = group.getMemberData(leaderId);
- return data == null ? UNKOWN_CANDIDATE_ADDRESS : new String(data);
+ return data == null ? UNKOWN_CANDIDATE_DATA : new String(data);
}
@Override
public Supplier<Boolean> offerLeadership(final Leader leader)
throws JoinException, WatchException, InterruptedException {
- Supplier<byte[]> leaderAddress = Suppliers.ofInstance(getAddress().getBytes());
- final Membership membership = group.join(leaderAddress, new Command() {
+ final Membership membership = group.join(dataSupplier, new Command() {
@Override public void execute() {
leader.onDefeated();
}
@@ -157,7 +168,7 @@ private String getAddress() {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOG.log(Level.WARNING, "Failed to determine local address!", e);
- return UNKOWN_CANDIDATE_ADDRESS;
+ return UNKOWN_CANDIDATE_DATA;
}
}

0 comments on commit 45565b2

Please sign in to comment.