xyuanlu commented on code in PR #2562: URL: https://github.com/apache/helix/pull/2562#discussion_r1269030338
########## meta-client/src/main/java/org/apache/helix/metaclient/recipes/leaderelection/LeaderElectionClient.java: ########## @@ -136,27 +139,63 @@ public void joinLeaderElectionParticipantPool(String leaderPath) { public void joinLeaderElectionParticipantPool(String leaderPath, LeaderInfo userInfo) { // TODO: create participant entry with info subscribeAndTryCreateLeaderEntry(leaderPath); + + LeaderInfo participantInfo = new LeaderInfo(userInfo); + createParticipantInfo(leaderPath, participantInfo); + } + + private void createParticipantInfo(String leaderPath, LeaderInfo participantInfo) { + + try { + // try to create participant info entry, assuming leader election group node is already there + _metaClient.create(leaderPath + PARTICIPANTS_ENTRY_PARENT + _participant, participantInfo, + MetaClientInterface.EntryMode.EPHEMERAL); + } catch (MetaClientNodeExistsException ex) { + LOG.info("Already joined pool {}.", leaderPath); + return; + } catch (MetaClientNoNodeException ex) { + try { + try { + // try to create participants root entry. This function is called after + // `subscribeAndTryCreateLeaderEntry`, so leader election root node should have already been created. + LOG.info("{} Creating leader group directory {}.", _participant, leaderPath); + _metaClient.create(leaderPath + PARTICIPANTS_ENTRY_KEY, null); + } catch (MetaClientNodeExistsException ignored) { + // participants' root entry created by other client, ignore + } + // try to participant info node again. + _metaClient.create(leaderPath + PARTICIPANTS_ENTRY_PARENT + _participant, participantInfo, + MetaClientInterface.EntryMode.EPHEMERAL); + } catch (MetaClientNoNodeException e) { + // catch exception in previous 2 create() + // Leader group root entry or participant parent entry is gone after we checked or created. + // Meaning other client removed the group. Throw ConcurrentModificationException. + throw new ConcurrentModificationException( + "Other client trying to modify the leader election group at the same time, please retry.", ex); + } + } } private void subscribeAndTryCreateLeaderEntry(String leaderPath) { _metaClient.subscribeDataChange(leaderPath + LEADER_ENTRY_KEY, _reElectListener, false); - LeaderInfo leaderInfo = new LeaderInfo(LEADER_ENTRY_KEY); + LeaderInfo leaderInfo = new LeaderInfo("LEADER"); Review Comment: Missed it. It was a mistaken resolved conflict. (was changing from "LEADER" to using static string) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org For additional commands, e-mail: reviews-h...@helix.apache.org