-
Notifications
You must be signed in to change notification settings - Fork 144
Replace 101tec ZkClient with Helix ZkClient #870
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
601bb27
065450e
b838a4b
9906eca
5a09717
49554b4
7380202
7f9c97d
039c28c
87c7bec
2cd2b8b
4d867ef
37544cf
d64e248
dbaffa6
26f32ca
4310926
a67108b
b821911
4f806ac
f6c7e76
a34bbdd
55027bc
981a32f
5b30f54
51533bb
5657225
cdc0c6c
f4d6dcf
608ec07
50cadd4
f06e55b
6b5f2f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,35 +6,29 @@ | |
| package com.linkedin.datastream.common.zk; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.Stack; | ||
|
|
||
| import org.I0Itec.zkclient.ZkConnection; | ||
| import org.I0Itec.zkclient.exception.ZkInterruptedException; | ||
| import org.I0Itec.zkclient.exception.ZkMarshallingError; | ||
| import org.I0Itec.zkclient.exception.ZkNoNodeException; | ||
| import org.I0Itec.zkclient.exception.ZkNodeExistsException; | ||
| import org.I0Itec.zkclient.serialize.ZkSerializer; | ||
| import org.apache.zookeeper.CreateMode; | ||
| import org.apache.zookeeper.data.Stat; | ||
| import org.apache.helix.zookeeper.zkclient.exception.ZkMarshallingError; | ||
| import org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException; | ||
| import org.apache.helix.zookeeper.zkclient.serialize.ZkSerializer; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
|
|
||
|
|
||
| /** | ||
| * ZKClient is a wrapper of {@link org.I0Itec.zkclient.ZkClient}. It provides the following | ||
| * ZKClient is a wrapper of {@link org.apache.helix.zookeeper.impl.client.ZkClient}. It provides the following | ||
| * basic features: | ||
| * <ol> | ||
| * <li>tolerate network reconnects so the caller doesn't have to handle the retries</li> | ||
| * <li>provide a String serializer since we only need to store JSON strings in ZooKeeper</li> | ||
| * <li>additional features like ensurePath to recursively create paths</li> | ||
| * </ol> | ||
| */ | ||
| public class ZkClient extends org.I0Itec.zkclient.ZkClient { | ||
| public class ZkClient extends org.apache.helix.zookeeper.impl.client.ZkClient { | ||
| public static final String ZK_PATH_SEPARATOR = "/"; | ||
| public static final int DEFAULT_CONNECTION_TIMEOUT = 60 * 1000; | ||
| public static final int DEFAULT_SESSION_TIMEOUT = 30 * 1000; | ||
|
|
@@ -86,71 +80,20 @@ public ZkClient(String zkServers, int sessionTimeoutMs, int connectionTimeoutMs, | |
| _zkSessionTimeoutMs = sessionTimeoutMs; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws ZkInterruptedException { | ||
| if (LOG.isTraceEnabled()) { | ||
| StackTraceElement[] calls = Thread.currentThread().getStackTrace(); | ||
| LOG.trace("closing zkclient. callStack: {}", Arrays.asList(calls)); | ||
| } | ||
| getEventLock().lock(); | ||
| try { | ||
| if (_connection == null) { | ||
| return; | ||
| } | ||
| LOG.info("closing zkclient: {}", ((ZkConnection) _connection).getZookeeper()); | ||
| super.close(); | ||
| } catch (ZkInterruptedException e) { | ||
| /* | ||
| * Workaround for HELIX-264: calling ZkClient#disconnect() in its own eventThread context will | ||
| * throw ZkInterruptedException and skip ZkConnection#disconnect() | ||
| */ | ||
| try { | ||
| /* | ||
| * ZkInterruptedException#construct() honors InterruptedException by calling | ||
| * Thread.currentThread().interrupt(); clear it first, so we can safely disconnect the | ||
| * zk-connection | ||
| */ | ||
| Thread.interrupted(); | ||
| _connection.close(); | ||
| /* | ||
| * restore interrupted status of current thread | ||
| */ | ||
| Thread.currentThread().interrupt(); | ||
| } catch (InterruptedException e1) { | ||
| throw new ZkInterruptedException(e1); | ||
| } | ||
| } finally { | ||
| getEventLock().unlock(); | ||
| LOG.info("closed zkclient"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if a zk path exists. Changes the access modified to public, its defined as protected in parent class. | ||
| */ | ||
| @Override | ||
| public boolean exists(final String path, final boolean watch) { | ||
| long startT = System.nanoTime(); | ||
|
|
||
| try { | ||
| return retryUntilConnected(() -> _connection.exists(path, watch)); | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("exists, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| return super.exists(path, watch); | ||
| } | ||
|
|
||
| /** | ||
| * Get all children of zk path. Changes the access modified to public, its defined as protected in parent class. | ||
| */ | ||
| @Override | ||
| public List<String> getChildren(final String path, final boolean watch) { | ||
|
Collaborator
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. You can probably get rid of this method itself since you are only calling super.()
Collaborator
Author
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. The method is declared as protected in parent class and here I am just expanding the access specifier. |
||
| long startT = System.nanoTime(); | ||
|
|
||
| try { | ||
| return retryUntilConnected(() -> _connection.getChildren(path, watch)); | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("getChildren, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| return super.getChildren(path, watch); | ||
| } | ||
|
|
||
|
surajkn marked this conversation as resolved.
|
||
| /** | ||
|
|
@@ -212,80 +155,6 @@ public String ensureReadData(final String path) { | |
| return ensureReadData(path, _zkSessionTimeoutMs); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| protected <T extends Object> T readData(final String path, final Stat stat, final boolean watch) { | ||
| long startT = System.nanoTime(); | ||
| try { | ||
| byte[] data = retryUntilConnected(() -> _connection.readData(path, stat, watch)); | ||
| return (T) deserialize(data); | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("readData, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeData(final String path, Object data, final int expectedVersion) { | ||
| long startT = System.nanoTime(); | ||
| try { | ||
| final byte[] bytes = serialize(data); | ||
|
|
||
| retryUntilConnected(() -> { | ||
| _connection.writeData(path, bytes, expectedVersion); | ||
| return null; | ||
| }); | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("writeData, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String create(final String path, Object data, final CreateMode mode) throws RuntimeException { | ||
| if (path == null) { | ||
| throw new IllegalArgumentException("path must not be null."); | ||
|
Collaborator
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. The new library throws
Collaborator
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. Also, this is something to bring up with Helix team that their contact says
Collaborator
Author
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. Added unit test for NullPointerException. Will bring up the discrepancy with Helix team separately |
||
| } | ||
|
|
||
| long startT = System.nanoTime(); | ||
| try { | ||
| final byte[] bytes = data == null ? null : serialize(data); | ||
|
|
||
| return retryUntilConnected(() -> _connection.create(path, bytes, mode)); | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("create, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean delete(final String path) { | ||
| long startT = System.nanoTime(); | ||
| try { | ||
| try { | ||
| retryUntilConnected(() -> { | ||
| _connection.delete(path); | ||
| return null; | ||
| }); | ||
|
|
||
| return true; | ||
| } catch (ZkNoNodeException e) { | ||
| return false; | ||
| } | ||
| } finally { | ||
| long endT = System.nanoTime(); | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("delete, path: {}, time: {} ns", path, (endT - startT)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensure that all the paths in the given full path String are created | ||
| * @param path the zk path | ||
|
|
@@ -347,9 +216,12 @@ public <T extends Object> T deserialize(byte[] data) { | |
| return (T) _zkSerializer.deserialize(data); | ||
| } | ||
|
|
||
| /** | ||
| * Get Zk sessions session ID | ||
|
surajkn marked this conversation as resolved.
Outdated
|
||
| */ | ||
| @VisibleForTesting | ||
| public long getSessionId() { | ||
|
surajkn marked this conversation as resolved.
Outdated
|
||
| return ((ZkConnection) _connection).getZookeeper().getSessionId(); | ||
| return super.getSessionId(); | ||
| } | ||
|
|
||
| private static class ZKStringSerializer implements ZkSerializer { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.