-
Notifications
You must be signed in to change notification settings - Fork 486
[server] Coordinator Server Supports High-Available #1401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zcoo
wants to merge
24
commits into
apache:main
Choose a base branch
from
zcoo:20250723_coordinator_ha
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
11d6daf
[server] Coordinator Server Supports High-Available
zcoo b383f77
[server] Coordinator Server Supports High-Available
zcoo d508cca
[server] Coordinator Server Supports High-Available
zcoo f33b338
[server] Coordinator Server Supports High-Available
zcoo 3a2c1d0
[server] Coordinator Server Supports High-Available
zcoo 302980a
[server] Coordinator Server Supports High-Available
zcoo d935b8f
user LeaderLatch instead of LeaderSelector
zcoo d804a1d
[server] Coordinator Server Supports High-Available
zcoo e22f787
merge
zcoo 5b0a0cd
support coordinator epoch
zcoo e35913b
support coordinator epoch
zcoo b254c7a
support coordinator epoch
zcoo 3369a47
modify test
zcoo 804f613
modify test
zcoo 0418c8d
fix test
zcoo 679ea4c
support coordinator epoch
zcoo 4459dce
support coordinator epoch2
zcoo d175c17
support coordinator epoch2
zcoo 595c75a
support coordinator epoch2
zcoo 5a2f7f8
support coordinator epoch2
zcoo 7649e55
support coordinator epoch2
zcoo b817a6f
support coordinator epoch2
zcoo db7a319
support coordinator epoch2
zcoo c851189
fix
zcoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
fluss-common/src/main/java/org/apache/fluss/exception/CoordinatorEpochFencedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.fluss.exception; | ||
|
|
||
| /** Exception thrown when the Coordinator leader epoch is invalid. */ | ||
| public class CoordinatorEpochFencedException extends RuntimeException { | ||
| public CoordinatorEpochFencedException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...s-server/src/main/java/org/apache/fluss/server/coordinator/CoordinatorLeaderElection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.fluss.server.coordinator; | ||
|
|
||
| import org.apache.fluss.exception.CoordinatorEpochFencedException; | ||
| import org.apache.fluss.server.zk.ZooKeeperClient; | ||
| import org.apache.fluss.server.zk.data.ZkData; | ||
| import org.apache.fluss.shaded.curator5.org.apache.curator.framework.recipes.leader.LeaderLatch; | ||
| import org.apache.fluss.shaded.curator5.org.apache.curator.framework.recipes.leader.LeaderLatchListener; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** Using by coordinator server. Coordinator servers listen ZK node and elect leadership. */ | ||
| public class CoordinatorLeaderElection implements AutoCloseable { | ||
| private static final Logger LOG = LoggerFactory.getLogger(CoordinatorLeaderElection.class); | ||
|
|
||
| private final int serverId; | ||
| private final ZooKeeperClient zkClient; | ||
| private final CoordinatorContext coordinatorContext; | ||
| private final LeaderLatch leaderLatch; | ||
| private final AtomicBoolean isLeader = new AtomicBoolean(false); | ||
|
|
||
| public CoordinatorLeaderElection( | ||
| ZooKeeperClient zkClient, int serverId, CoordinatorContext coordinatorContext) { | ||
| this.serverId = serverId; | ||
| this.zkClient = zkClient; | ||
| this.coordinatorContext = coordinatorContext; | ||
| this.leaderLatch = | ||
| new LeaderLatch( | ||
| zkClient.getCuratorClient(), | ||
| ZkData.CoordinatorElectionZNode.path(), | ||
| String.valueOf(serverId)); | ||
| } | ||
|
|
||
| public void startElectLeader(Runnable initLeaderServices) { | ||
| leaderLatch.addListener( | ||
| new LeaderLatchListener() { | ||
| @Override | ||
| public void isLeader() { | ||
| LOG.info("Coordinator server {} has become the leader.", serverId); | ||
| isLeader.set(true); | ||
| try { | ||
| // to avoid split-brain | ||
| Optional<Integer> optionalEpoch = | ||
| zkClient.fenceBecomeCoordinatorLeader(serverId); | ||
| if (optionalEpoch.isPresent()) { | ||
| coordinatorContext.setCoordinatorEpochAndZkVersion( | ||
| optionalEpoch.get(), | ||
| coordinatorContext.getCoordinatorEpochZkVersion() + 1); | ||
| initLeaderServices.run(); | ||
| } else { | ||
| throw new CoordinatorEpochFencedException( | ||
| "Fenced to become coordinator leader."); | ||
| } | ||
| } catch (Exception e) { | ||
| relinquishLeadership(); | ||
| throw new CoordinatorEpochFencedException( | ||
| "Fenced to become coordinator leader."); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void notLeader() { | ||
| relinquishLeadership(); | ||
| LOG.warn("Coordinator server {} has lost the leadership.", serverId); | ||
| isLeader.set(false); | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| leaderLatch.start(); | ||
| LOG.info("Coordinator server {} started leader election.", serverId); | ||
|
|
||
| // todo: Currently, we await the leader latch and do nothing until it becomes leader. | ||
| // Later we can make it as a hot backup server to continuously synchronize metadata from | ||
| // Zookeeper, which save time from initializing context | ||
| // leaderLatch.await(); | ||
|
|
||
| } catch (Exception e) { | ||
| LOG.error("Failed to start LeaderLatch for server {}", serverId, e); | ||
| throw new RuntimeException("Leader election start failed", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| LOG.info("Closing LeaderLatch for server {}.", serverId); | ||
| if (leaderLatch != null) { | ||
| try { | ||
| leaderLatch.close(); | ||
| } catch (Exception e) { | ||
| LOG.error("Failed to close LeaderLatch for server {}.", serverId, e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public boolean isLeader() { | ||
| return this.isLeader.get(); | ||
| } | ||
|
|
||
| private void relinquishLeadership() { | ||
| isLeader.set(false); | ||
| LOG.info("Coordinator server {} has been fenced.", serverId); | ||
|
|
||
| this.close(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.