Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/900#discussion_r75872798
  
    --- Diff: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/leader/election/CuratorLeaderElectionManager.java
 ---
    @@ -218,14 +227,92 @@ public String getLeader(final String roleName) {
             return participantId;
         }
     
    +
    +    /**
    +     * Determines whether or not leader election has already begun for the 
role with the given name
    +     *
    +     * @param roleName the role of interest
    +     * @return <code>true</code> if leader election has already begun, 
<code>false</code> if it has not or if unable to determine this.
    +     */
    +    @Override
    +    public boolean isLeaderElected(final String roleName) {
    +        final String leaderAddress = determineLeaderExternal(roleName);
    +        return !StringUtils.isEmpty(leaderAddress);
    +    }
    +
    +
    +    /**
    +     * Use a new Curator client to determine which node is the elected 
leader for the given role.
    +     *
    +     * @param roleName the name of the role
    +     * @return the id of the elected leader, or <code>null</code> if no 
leader has been selected or if unable to determine
    +     *         the leader from ZooKeeper
    +     */
    +    private String determineLeaderExternal(final String roleName) {
    +        final CuratorFramework client = createClient();
    +        try {
    +            final LeaderSelectorListener electionListener = new 
LeaderSelectorListener() {
    +                @Override
    +                public void stateChanged(CuratorFramework client, 
ConnectionState newState) {
    +                }
    +
    +                @Override
    +                public void takeLeadership(CuratorFramework client) throws 
Exception {
    +                }
    +            };
    +
    +            final String electionPath = getElectionPath(roleName);
    +
    +            // Note that we intentionally do not auto-requeue here, and we 
do not start the selector. We do not
    +            // want to join the leader election. We simply want to observe.
    +            final LeaderSelector selector = new LeaderSelector(client, 
electionPath, electionListener);
    +
    +            try {
    +                final Participant leader = selector.getLeader();
    +                return leader == null ? null : leader.getId();
    +            } catch (final KeeperException.NoNodeException nne) {
    +                // If there is no ZNode, then there is no elected leader.
    +                return null;
    +            } catch (final Exception e) {
    +                logger.warn("Unable to determine the Elected Leader for 
role '{}' due to {}; assuming no leader has been elected", roleName, 
e.toString());
    +                if (logger.isDebugEnabled()) {
    +                    logger.warn("", e);
    +                }
    +
    +                return null;
    +            }
    +        } finally {
    +            client.close();
    +        }
    +    }
    +
    +    private CuratorFramework createClient() {
    +        // Create a new client because we don't want to try indefinitely 
for this to occur.
    +        final RetryPolicy retryPolicy = new RetryNTimes(10, 500);
    --- End diff --
    
    got it


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to