Skip to content

Commit e6d0a4f

Browse files
author
Ivan Zaitsev
authored
GH-4008: ZK: Add leader and participants support
Fixes #4008 Impossible to retrieve current leader id from ZK `LeaderInitiator` * change return type of `LeaderInitiator.getContext()` from `Context` to `CuratorContext` * add `getLeader()` method to `CuratorContext` * add `getParticipants()` method to `CuratorContext`
1 parent 6a2ff0c commit e6d0a4f

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/leader/LeaderInitiator.java

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
package org.springframework.integration.zookeeper.leader;
1818

19+
import java.util.Collection;
20+
import java.util.List;
21+
1922
import org.apache.commons.logging.Log;
2023
import org.apache.commons.logging.LogFactory;
2124
import org.apache.curator.framework.CuratorFramework;
2225
import org.apache.curator.framework.imps.CuratorFrameworkState;
2326
import org.apache.curator.framework.recipes.leader.LeaderSelector;
2427
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
28+
import org.apache.curator.framework.recipes.leader.Participant;
2529

2630
import org.springframework.context.SmartLifecycle;
2731
import org.springframework.integration.leader.Candidate;
@@ -38,6 +42,7 @@
3842
* @author Janne Valkealahti
3943
* @author Gary Russell
4044
* @author Artem Bilan
45+
* @author Ivan Zaitsev
4146
*
4247
* @since 4.2
4348
*/
@@ -49,6 +54,8 @@ public class LeaderInitiator implements SmartLifecycle {
4954

5055
private final CuratorContext context = new CuratorContext();
5156

57+
private final CuratorContext nullContext = new NullCuratorContext();
58+
5259
/**
5360
* Curator client.
5461
*/
@@ -59,20 +66,6 @@ public class LeaderInitiator implements SmartLifecycle {
5966
*/
6067
private final Candidate candidate;
6168

62-
private final Context nullContext = new Context() {
63-
64-
@Override
65-
public boolean isLeader() {
66-
return false;
67-
}
68-
69-
@Override
70-
public String getRole() {
71-
return LeaderInitiator.this.candidate.getRole();
72-
}
73-
74-
};
75-
7669
private final Object lifecycleMonitor = new Object();
7770

7871
/**
@@ -214,7 +207,7 @@ public void setLeaderEventPublisher(LeaderEventPublisher leaderEventPublisher) {
214207
* @return the context.
215208
* @since 5.0
216209
*/
217-
public Context getContext() {
210+
public CuratorContext getContext() {
218211
if (this.leaderSelector == null) {
219212
return this.nullContext;
220213
}
@@ -283,7 +276,7 @@ public void takeLeadership(CuratorFramework framework) {
283276
/**
284277
* Implementation of leadership context backed by Curator.
285278
*/
286-
private class CuratorContext implements Context {
279+
public class CuratorContext implements Context {
287280

288281
CuratorContext() {
289282
}
@@ -303,6 +296,24 @@ public String getRole() {
303296
return LeaderInitiator.this.candidate.getRole();
304297
}
305298

299+
/**
300+
* Get the leader
301+
* @return the leader.
302+
* @since 6.0.3
303+
*/
304+
public Participant getLeader() throws Exception {
305+
return LeaderInitiator.this.leaderSelector.getLeader();
306+
}
307+
308+
/**
309+
* Get the list of participants
310+
* @return list of participants.
311+
* @since 6.0.3
312+
*/
313+
public Collection<Participant> getParticipants() throws Exception {
314+
return LeaderInitiator.this.leaderSelector.getParticipants();
315+
}
316+
306317
@Override
307318
public String toString() {
308319
return "CuratorContext{role=" + LeaderInitiator.this.candidate.getRole() +
@@ -312,4 +323,28 @@ public String toString() {
312323

313324
}
314325

326+
private class NullCuratorContext extends CuratorContext {
327+
328+
@Override
329+
public boolean isLeader() {
330+
return false;
331+
}
332+
333+
@Override
334+
public String getRole() {
335+
return LeaderInitiator.this.candidate.getRole();
336+
}
337+
338+
@Override
339+
public Participant getLeader() throws Exception {
340+
return null;
341+
}
342+
343+
@Override
344+
public Collection<Participant> getParticipants() throws Exception {
345+
return List.of();
346+
}
347+
348+
}
349+
315350
}

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/config/LeaderInitiatorFactoryBeanTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424

2525
import org.apache.curator.framework.CuratorFramework;
26+
import org.apache.curator.framework.recipes.leader.Participant;
2627
import org.junit.jupiter.api.AfterAll;
2728
import org.junit.jupiter.api.BeforeAll;
2829
import org.junit.jupiter.api.Test;
@@ -47,6 +48,7 @@
4748
/**
4849
* @author Gary Russell
4950
* @author Artem Bilan
51+
* @author Ivan Zaitsev
5052
*
5153
* @since 4.2
5254
*
@@ -88,6 +90,8 @@ public void test() throws Exception {
8890
public void testExceptionFromEvent() throws Exception {
8991
CountDownLatch onGranted = new CountDownLatch(1);
9092

93+
Participant participant = new Participant("foo", true);
94+
9195
LeaderInitiator initiator = new LeaderInitiator(client, new DefaultCandidate("foo", "bar"));
9296

9397
initiator.setLeaderEventPublisher(new DefaultLeaderEventPublisher() {
@@ -110,6 +114,8 @@ public void publishOnGranted(Object source, Context context, String role) {
110114
assertThat(onGranted.await(10, TimeUnit.SECONDS)).isTrue();
111115
assertThat(initiator.getContext().isLeader()).isTrue();
112116
assertThat(initiator.getContext().getRole()).isEqualTo("bar");
117+
assertThat(initiator.getContext().getLeader()).isEqualTo(participant);
118+
assertThat(initiator.getContext().getParticipants()).contains(participant);
113119

114120
initiator.stop();
115121
}

0 commit comments

Comments
 (0)