This is an automated email from the ASF dual-hosted git repository.
xyuanlu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git
The following commit(s) were added to refs/heads/master by this push:
new 1bfed3138 Prevent MetaClient LeaderElectionClient isLeader NPE before
joining pool (#2798)
1bfed3138 is described below
commit 1bfed3138a93c7e4a4bcc48349ce11b249167926
Author: Grant Paláu Spencer <[email protected]>
AuthorDate: Tue May 7 11:57:28 2024 -0700
Prevent MetaClient LeaderElectionClient isLeader NPE before joining pool
(#2798)
Prevent MetaClient LeaderElectionClient isLeader NPE before joining pool
---
.../recipes/leaderelection/LeaderElectionClient.java | 3 ++-
.../recipes/leaderelection/TestLeaderElection.java | 14 ++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/leaderelection/LeaderElectionClient.java
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/leaderelection/LeaderElectionClient.java
index 3bcf09ceb..373d36013 100644
---
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/leaderelection/LeaderElectionClient.java
+++
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/leaderelection/LeaderElectionClient.java
@@ -121,7 +121,8 @@ public class LeaderElectionClient implements AutoCloseable {
* Returns true if current participant is the current leadership.
*/
public boolean isLeader(String leaderPath) {
- return getLeader(leaderPath).equalsIgnoreCase(_participant);
+ String leader = getLeader(leaderPath);
+ return leader != null && leader.equalsIgnoreCase(_participant);
}
/**
diff --git
a/meta-client/src/test/java/org/apache/helix/metaclient/recipes/leaderelection/TestLeaderElection.java
b/meta-client/src/test/java/org/apache/helix/metaclient/recipes/leaderelection/TestLeaderElection.java
index 75917623c..248643652 100644
---
a/meta-client/src/test/java/org/apache/helix/metaclient/recipes/leaderelection/TestLeaderElection.java
+++
b/meta-client/src/test/java/org/apache/helix/metaclient/recipes/leaderelection/TestLeaderElection.java
@@ -41,7 +41,21 @@ public class TestLeaderElection extends ZkMetaClientTestBase
{
}
}
+ // Test that calling isLeader before client joins
LeaderElectionParticipantPool returns false and does not throw NPE
@Test
+ public void testIsLeaderBeforeJoiningParticipantPool() throws Exception {
+ String leaderPath = LEADER_PATH + "/testIsLeaderBeforeJoiningPool";
+ LeaderElectionClient clt1 = createLeaderElectionClient(PARTICIPANT_NAME1);
+ try {
+ boolean isLeader = clt1.isLeader(leaderPath);
+ Assert.assertFalse(isLeader, "Expected isLeader to return false before
joining participant pool");
+ } catch (NullPointerException npe) {
+ Assert.fail("isLeader threw NPE before joining participant pool: " +
npe.getMessage());
+ }
+ clt1.close();
+ }
+
+ @Test (dependsOnMethods = "testIsLeaderBeforeJoiningParticipantPool")
public void testAcquireLeadership() throws Exception {
System.out.println("START TestLeaderElection.testAcquireLeadership");
String leaderPath = LEADER_PATH + "/testAcquireLeadership";