This is an automated email from the ASF dual-hosted git repository. xyuanlu pushed a commit to branch metaclient in repository https://gitbox.apache.org/repos/asf/helix.git
commit 37fa0a9a0a74d189068bfe018e4eccd90c34bd27 Author: xyuanlu <[email protected]> AuthorDate: Mon Jan 30 12:06:36 2023 -0800 Rename and reformat metaclient test and util Rename and reformat metaclient test and util --- .../metaclient/exception/MetaClientException.java | 1 + .../helix/metaclient/impl/zk/ZkMetaClient.java | 8 +- .../metaclient/impl/zk/util/ZkMetaClientUtil.java | 110 +++++++++++++-------- .../helix/metaclient/impl/zk/TestZkMetaClient.java | 46 +++++---- 4 files changed, 100 insertions(+), 65 deletions(-) diff --git a/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java b/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java index ca6a213b3..6620799ca 100644 --- a/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java +++ b/meta-client/src/main/java/org/apache/helix/metaclient/exception/MetaClientException.java @@ -35,4 +35,5 @@ public class MetaClientException extends RuntimeException { public MetaClientException(Throwable cause) { super(cause); } + } diff --git a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java index 5bc6ca973..2eb0da496 100644 --- a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java +++ b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java @@ -44,7 +44,8 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.apache.helix.metaclient.impl.zk.util.ZkMetaClientUtil.convertZkEntryMode; + +import static org.apache.helix.metaclient.impl.zk.util.ZkMetaClientUtil.convertZkEntryModeToMetaClientEntryMode; import static org.apache.helix.metaclient.impl.zk.util.ZkMetaClientUtil.translateZkExceptionToMetaclientException; public class ZkMetaClient<T> implements MetaClientInterface<T>, AutoCloseable { @@ -122,7 +123,8 @@ public class ZkMetaClient<T> implements MetaClientInterface<T>, AutoCloseable { if (zkStats == null) { return null; } - return new Stat(convertZkEntryMode(zkStats.getEphemeralOwner()), zkStats.getVersion()); + return new Stat(convertZkEntryModeToMetaClientEntryMode(zkStats.getEphemeralOwner()), + zkStats.getVersion()); } catch (ZkException e) { throw translateZkExceptionToMetaclientException(e); } @@ -177,7 +179,7 @@ public class ZkMetaClient<T> implements MetaClientInterface<T>, AutoCloseable { } @Override - public void asyncSet(String key, Object data, int version, AsyncCallback.VoidCallback cb) { + public void asyncSet(String key, T data, int version, AsyncCallback.VoidCallback cb) { } diff --git a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java index 2c3dc22cd..dc92e706b 100644 --- a/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java +++ b/meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java @@ -19,10 +19,26 @@ package org.apache.helix.metaclient.impl.zk.util; * under the License. */ +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + import org.apache.helix.metaclient.api.MetaClientInterface; import org.apache.helix.metaclient.api.OpResult; -import org.apache.helix.metaclient.exception.*; -import org.apache.helix.zookeeper.zkclient.exception.*; +import org.apache.helix.metaclient.exception.MetaClientBadVersionException; +import org.apache.helix.metaclient.exception.MetaClientException; +import org.apache.helix.metaclient.exception.MetaClientInterruptException; +import org.apache.helix.metaclient.exception.MetaClientNoNodeException; +import org.apache.helix.metaclient.exception.MetaClientTimeoutException; +import org.apache.helix.zookeeper.zkclient.exception.ZkBadVersionException; +import org.apache.helix.zookeeper.zkclient.exception.ZkException; +import org.apache.helix.zookeeper.zkclient.exception.ZkInterruptedException; +import org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException; +import org.apache.helix.zookeeper.zkclient.exception.ZkTimeoutException; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Op; @@ -30,18 +46,11 @@ import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.server.EphemeralType; -import java.util.Collections; -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.function.Function; - public class ZkMetaClientUtil { //TODO Implement MetaClient ACL //Default ACL value until metaClient Op has ACL of its own. - private static final List<ACL> DEFAULT_ACL = Collections.unmodifiableList(ZooDefs.Ids.OPEN_ACL_UNSAFE); + private static final List<ACL> DEFAULT_ACL = + Collections.unmodifiableList(ZooDefs.Ids.OPEN_ACL_UNSAFE); private ZkMetaClientUtil(){ } @@ -74,22 +83,24 @@ public class ZkMetaClientUtil { opmap.put(org.apache.helix.metaclient.api.Op.Type.CREATE, op -> { try { - CreateMode mode = convertMetaClientMode(((org.apache.helix.metaclient.api.Op.Create) op).getEntryMode()); - return Op.create(op.getPath(), ((org.apache.helix.metaclient.api.Op.Create) op).getData(), DEFAULT_ACL, mode); + CreateMode mode = convertMetaClientMode( + ((org.apache.helix.metaclient.api.Op.Create) op).getEntryMode()); + return Op.create(op.getPath(), ((org.apache.helix.metaclient.api.Op.Create) op).getData(), + DEFAULT_ACL, mode); } catch (KeeperException e) { throw translateZkExceptionToMetaclientException(ZkException.create(e)); } }); - opmap.put(org.apache.helix.metaclient.api.Op.Type.DELETE, - op -> Op.delete(op.getPath(), ((org.apache.helix.metaclient.api.Op.Delete) op).getVersion())); + opmap.put(org.apache.helix.metaclient.api.Op.Type.DELETE, op -> Op + .delete(op.getPath(), ((org.apache.helix.metaclient.api.Op.Delete) op).getVersion())); - opmap.put(org.apache.helix.metaclient.api.Op.Type.SET, - op -> Op.setData(op.getPath(), - ((org.apache.helix.metaclient.api.Op.Set) op).getData(), ((org.apache.helix.metaclient.api.Op.Set) op).getVersion())); + opmap.put(org.apache.helix.metaclient.api.Op.Type.SET, op -> Op + .setData(op.getPath(), ((org.apache.helix.metaclient.api.Op.Set) op).getData(), + ((org.apache.helix.metaclient.api.Op.Set) op).getVersion())); - opmap.put(org.apache.helix.metaclient.api.Op.Type.CHECK, - op -> Op.check(op.getPath(), ((org.apache.helix.metaclient.api.Op.Check) op).getVersion())); + opmap.put(org.apache.helix.metaclient.api.Op.Type.CHECK, op -> Op + .check(op.getPath(), ((org.apache.helix.metaclient.api.Op.Check) op).getVersion())); return opmap; } @@ -121,11 +132,13 @@ public class ZkMetaClientUtil { public static List<OpResult> zkOpResultToMetaClientOpResults(List<org.apache.zookeeper.OpResult> zkResult) { List<OpResult> metaClientOpResult = new ArrayList<>(); for (org.apache.zookeeper.OpResult opResult : zkResult) { - Function<org.apache.zookeeper.OpResult, OpResult> function = getOpResultMap().get(opResult.getClass()); + Function<org.apache.zookeeper.OpResult, OpResult> function = + getOpResultMap().get(opResult.getClass()); if (function != null) { metaClientOpResult.add(function.apply(opResult)); } else { - throw new IllegalArgumentException("OpResult type " + opResult.getType() + "is not supported."); + throw new IllegalArgumentException( + "OpResult type " + opResult.getType() + "is not supported."); } } @@ -133,44 +146,59 @@ public class ZkMetaClientUtil { } private static final class OpResultMapHolder { - static final Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> OPRESULTMAP = initializeOpResultMap(); + static final Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> + OPRESULTMAP = initializeOpResultMap(); private static Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> initializeOpResultMap() { - Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> opResultMap = - new HashMap<>(); + Map<Class<? extends org.apache.zookeeper.OpResult>, Function<org.apache.zookeeper.OpResult, OpResult>> + opResultMap = new HashMap<>(); opResultMap.put(org.apache.zookeeper.OpResult.CreateResult.class, opResult -> { - org.apache.zookeeper.OpResult.CreateResult zkOpCreateResult = (org.apache.zookeeper.OpResult.CreateResult) opResult; + org.apache.zookeeper.OpResult.CreateResult zkOpCreateResult = + (org.apache.zookeeper.OpResult.CreateResult) opResult; if (opResult.getType() == 1) { return new OpResult.CreateResult(zkOpCreateResult.getPath()); } else { - MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat(convertZkEntryMode(zkOpCreateResult.getStat().getEphemeralOwner()), + MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat( + convertZkEntryModeToMetaClientEntryMode( + zkOpCreateResult.getStat().getEphemeralOwner()), zkOpCreateResult.getStat().getVersion()); return new OpResult.CreateResult(zkOpCreateResult.getPath(), metaClientStat); - }}); + } + }); - opResultMap.put(org.apache.zookeeper.OpResult.DeleteResult.class, opResult -> new OpResult.DeleteResult()); + opResultMap.put(org.apache.zookeeper.OpResult.DeleteResult.class, + opResult -> new OpResult.DeleteResult()); opResultMap.put(org.apache.zookeeper.OpResult.GetDataResult.class, opResult -> { - org.apache.zookeeper.OpResult.GetDataResult zkOpGetDataResult = (org.apache.zookeeper.OpResult.GetDataResult) opResult; - MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat(convertZkEntryMode(zkOpGetDataResult.getStat().getEphemeralOwner()), + org.apache.zookeeper.OpResult.GetDataResult zkOpGetDataResult = + (org.apache.zookeeper.OpResult.GetDataResult) opResult; + MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat( + convertZkEntryModeToMetaClientEntryMode( + zkOpGetDataResult.getStat().getEphemeralOwner()), zkOpGetDataResult.getStat().getVersion()); return new OpResult.GetDataResult(zkOpGetDataResult.getData(), metaClientStat); }); opResultMap.put(org.apache.zookeeper.OpResult.SetDataResult.class, opResult -> { - org.apache.zookeeper.OpResult.SetDataResult zkOpSetDataResult = (org.apache.zookeeper.OpResult.SetDataResult) opResult; - MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat(convertZkEntryMode(zkOpSetDataResult.getStat().getEphemeralOwner()), + org.apache.zookeeper.OpResult.SetDataResult zkOpSetDataResult = + (org.apache.zookeeper.OpResult.SetDataResult) opResult; + MetaClientInterface.Stat metaClientStat = new MetaClientInterface.Stat( + convertZkEntryModeToMetaClientEntryMode( + zkOpSetDataResult.getStat().getEphemeralOwner()), zkOpSetDataResult.getStat().getVersion()); return new OpResult.SetDataResult(metaClientStat); }); - opResultMap.put(org.apache.zookeeper.OpResult.GetChildrenResult.class, opResult -> new OpResult.GetChildrenResult( - ((org.apache.zookeeper.OpResult.GetChildrenResult) opResult).getChildren())); + opResultMap.put(org.apache.zookeeper.OpResult.GetChildrenResult.class, + opResult -> new OpResult.GetChildrenResult( + ((org.apache.zookeeper.OpResult.GetChildrenResult) opResult).getChildren())); - opResultMap.put(org.apache.zookeeper.OpResult.CheckResult.class, opResult -> new OpResult.CheckResult()); + opResultMap.put(org.apache.zookeeper.OpResult.CheckResult.class, + opResult -> new OpResult.CheckResult()); - opResultMap.put(org.apache.zookeeper.OpResult.ErrorResult.class, opResult -> new OpResult.ErrorResult( - ((org.apache.zookeeper.OpResult.ErrorResult) opResult).getErr())); + opResultMap.put(org.apache.zookeeper.OpResult.ErrorResult.class, + opResult -> new OpResult.ErrorResult( + ((org.apache.zookeeper.OpResult.ErrorResult) opResult).getErr())); return opResultMap; } @@ -180,7 +208,8 @@ public class ZkMetaClientUtil { return OpResultMapHolder.OPRESULTMAP; } - public static MetaClientInterface.EntryMode convertZkEntryMode(long ephemeralOwner) { + public static MetaClientInterface.EntryMode convertZkEntryModeToMetaClientEntryMode( + long ephemeralOwner) { EphemeralType zkEphemeralType = EphemeralType.get(ephemeralOwner); switch (zkEphemeralType) { case VOID: @@ -206,8 +235,7 @@ public class ZkMetaClientUtil { return new MetaClientTimeoutException(e); } else if (e instanceof ZkInterruptedException) { return new MetaClientInterruptException(e); - } else { - return new MetaClientException(e); } + return new MetaClientException(e); } } diff --git a/meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClient.java b/meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClient.java index 15fce2e41..b4b7aedac 100644 --- a/meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClient.java +++ b/meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClient.java @@ -22,25 +22,25 @@ package org.apache.helix.metaclient.impl.zk; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Set; - -import org.apache.commons.io.FileUtils; -import org.apache.helix.metaclient.api.DataUpdater; -import org.apache.helix.metaclient.api.MetaClientInterface; -import org.apache.helix.metaclient.exception.MetaClientException; -import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.NotImplementedException; import org.apache.helix.metaclient.api.DataChangeListener; -import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientConfig; +import org.apache.helix.metaclient.api.DataUpdater; +import org.apache.helix.metaclient.api.MetaClientInterface; import org.apache.helix.metaclient.api.Op; import org.apache.helix.metaclient.api.OpResult; +import org.apache.helix.metaclient.exception.MetaClientException; +import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientConfig; import org.apache.helix.zookeeper.zkclient.IDefaultNameSpace; import org.apache.helix.zookeeper.zkclient.ZkServer; import org.apache.zookeeper.KeeperException; @@ -57,9 +57,9 @@ public class TestZkMetaClient { private static final String ZK_ADDR = "localhost:2183"; private static final int DEFAULT_TIMEOUT_MS = 1000; private static final String ENTRY_STRING_VALUE = "test-value"; - protected static final String ZK_SHARDING_KEY_PREFIX = "/sharding-key-0"; - protected static String PARENT_PATH = ZK_SHARDING_KEY_PREFIX + "/RealmAwareZkClient"; - protected static final String TEST_INVALID_PATH = ZK_SHARDING_KEY_PREFIX + "_invalid" + "/a/b/c"; + protected static final String TRANSACTION_TEST_KEY_PREFIX = "/sharding-key-0"; + protected static String PARENT_PATH = TRANSACTION_TEST_KEY_PREFIX + "/RealmAwareZkClient"; + protected static final String TEST_INVALID_PATH = TRANSACTION_TEST_KEY_PREFIX + "_invalid" + "/a/b/c"; private final Object _syncObject = new Object(); @@ -300,6 +300,9 @@ public class TestZkMetaClient { Assert.assertTrue(dataExpected.get()); } } + + // TODO: Create a ZkMetadata test base class and move these helper to base class when more tests + // are added. private static ZkMetaClient<String> createZkMetaClient() { ZkMetaClientConfig config = new ZkMetaClientConfig.ZkMetaClientConfigBuilder().setConnectionAddress(ZK_ADDR).build(); @@ -328,18 +331,18 @@ public class TestZkMetaClient { zkServer.start(); return zkServer; } - + /** - * Test that zk multi works for zkmetaclient operations create, + * Test that zk transactional operation works for zkmetaclient operations create, * delete, and set. */ @Test - public void testMultiOps() { - String test_name = "/test_multi_ops"; + public void testTransactionOps() { + String test_name = "/test_transaction_ops"; try(ZkMetaClient<String> zkMetaClient = createZkMetaClient()) { zkMetaClient.connect(); - zkMetaClient.create(ZK_SHARDING_KEY_PREFIX, ENTRY_STRING_VALUE); + zkMetaClient.create(TRANSACTION_TEST_KEY_PREFIX, ENTRY_STRING_VALUE); //Create Nodes List<Op> ops = Arrays.asList( @@ -371,12 +374,12 @@ public class TestZkMetaClient { } /** - * Tests that attempts to call multi on an invalid path. Should fail. + * Tests that attempts to call transactional operation on an invalid path. Should fail. * @throws KeeperException */ - @Test(dependsOnMethods = "testMultiOps") - public void testMultiFail() { - String test_name = "/test_multi_fail"; + @Test(dependsOnMethods = "testTransactionOps") + public void testTransactionFail() { + String test_name = "/test_transaction_fail"; try(ZkMetaClient<String> zkMetaClient = createZkMetaClient()) { zkMetaClient.connect(); //Create Nodes @@ -387,7 +390,8 @@ public class TestZkMetaClient { try { zkMetaClient.transactionOP(ops); - Assert.fail("Should have thrown an exception. Cannot run multi on incorrect path."); + Assert.fail( + "Should have thrown an exception. Cannot run transactional create OP on incorrect path."); } catch (Exception e) { MetaClientInterface.Stat entryStat = zkMetaClient.exists(PARENT_PATH); Assert.assertNull(entryStat);
