-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Optimize zookeeper block logic on app startup #790
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
903875a
Fixed block on app start when zookeeper server can not be connected
chickenlj 24fcaf0
Fixed block on app start when zookeeper server can not be connected
chickenlj 2848c9e
Increase zkclient timeout to 30s
chickenlj d90c232
Increase default executor size in ListenableFuture
chickenlj e1aa50f
Record exception with logger
chickenlj 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
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
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
124 changes: 124 additions & 0 deletions
124
...ookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.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,124 @@ | ||
| package com.alibaba.dubbo.remoting.zookeeper.zkclient; | ||
|
|
||
| import com.alibaba.dubbo.common.concurrent.ListenableFutureTask; | ||
| import com.alibaba.dubbo.common.logger.Logger; | ||
| import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
| import com.alibaba.dubbo.common.utils.Assert; | ||
|
|
||
| import org.I0Itec.zkclient.IZkChildListener; | ||
| import org.I0Itec.zkclient.IZkStateListener; | ||
| import org.I0Itec.zkclient.ZkClient; | ||
| import org.apache.zookeeper.Watcher.Event.KeeperState; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * 连接超时后,能自动监听连接状态的zkclient包装类 | ||
| * 也为和curator在使用上总体保持一致 | ||
| * @author ken.lj | ||
| * @date 2017/10/29 | ||
| */ | ||
| public class ZkClientWrapper { | ||
| Logger logger = LoggerFactory.getLogger(ZkClientWrapper.class); | ||
|
|
||
| private long timeout; | ||
| private ZkClient client; | ||
| private volatile KeeperState state; | ||
| private ListenableFutureTask<ZkClient> listenableFutureTask; | ||
| private volatile boolean started = false; | ||
|
|
||
|
|
||
| public ZkClientWrapper(final String serverAddr, long timeout) { | ||
| this.timeout = timeout; | ||
| listenableFutureTask = ListenableFutureTask.create(new Callable<ZkClient>() { | ||
| @Override | ||
| public ZkClient call() throws Exception { | ||
| return new ZkClient(serverAddr, Integer.MAX_VALUE); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void start() { | ||
| if (!started) { | ||
| Thread connectThread = new Thread(listenableFutureTask); | ||
| connectThread.setName("DubboZkclientConnector"); | ||
| connectThread.setDaemon(true); | ||
| connectThread.start(); | ||
| try { | ||
| client = listenableFutureTask.get(timeout, TimeUnit.MILLISECONDS); | ||
| } catch (Throwable t) { | ||
| logger.error("Timeout! zookeeper server can not be connected in : " + timeout + "ms!", t); | ||
| } | ||
| started = true; | ||
| } else { | ||
| throw new IllegalStateException("Zkclient has already been started!"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重复初始化直接跳过应该会比较好一些 |
||
| } | ||
| } | ||
|
|
||
| public void addListener(final IZkStateListener listener) { | ||
| listenableFutureTask.addListener(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| try { | ||
| client = listenableFutureTask.get(); | ||
| client.subscribeStateChanges(listener); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个地方要用 logger 输出 |
||
| } catch (ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public boolean isConnected() { | ||
| return client != null && state == KeeperState.SyncConnected; | ||
| } | ||
|
|
||
| public void createPersistent(String path) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| client.createPersistent(path, true); | ||
| } | ||
|
|
||
| public void createEphemeral(String path) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| client.createEphemeral(path); | ||
| } | ||
|
|
||
| public void delete(String path) { | ||
| if (client == null) { | ||
| throw new IllegalStateException("Zookeeper is not connected!"); | ||
| } | ||
| client.delete(path); | ||
| } | ||
|
|
||
| public List<String> getChildren(String path) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| return client.getChildren(path); | ||
| } | ||
|
|
||
| public boolean exists(String path) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| return client.exists(path); | ||
| } | ||
|
|
||
| public void close() { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| client.close(); | ||
| } | ||
|
|
||
| public List<String> subscribeChildChanges(String path, final IZkChildListener listener) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| return client.subscribeChildChanges(path, listener); | ||
| } | ||
|
|
||
| public void unsubscribeChildChanges(String path, IZkChildListener listener) { | ||
| Assert.notNull(client, new IllegalStateException("Zookeeper is not connected!")); | ||
| client.unsubscribeChildChanges(path, listener); | ||
| } | ||
|
|
||
|
|
||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方与之前的行为不一致,建议是传参数的时候,显示的传递 DEFAULT_EXECUTOR