qqu0127 commented on code in PR #2343:
URL: https://github.com/apache/helix/pull/2343#discussion_r1082719864
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
Review Comment:
Add license
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
+
+ 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);
+ } 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.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()));
+
+ List<Op> zkOps = new ArrayList<>();
+ for (org.apache.helix.metaclient.api.Op op : ops) {
+ Op temp = opMap.get(op.getType()).apply(op);
+ zkOps.add(temp);
+ }
+ return zkOps;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ String mode = entryMode.name();
+ if (mode.equals(MetaClientInterface.EntryMode.PERSISTENT.name())) {
+ return CreateMode.fromFlag(0);
+ }
+ if (mode.equals(MetaClientInterface.EntryMode.EPHEMERAL.name())) {
+ return CreateMode.fromFlag(1);
+ }
Review Comment:
We can directly compare e.g. `entryMode == EntryMode.PERSISTENT`, there is
no need for string compare or `CreateMode.fromFlag`.
This method doesn't need to throw `KeeperException` either.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
Review Comment:
The map is a constant and can be reused, let's store that as a static
variable. Please also consider lazy initialization.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
+
+ 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);
+ } 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.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()));
+
+ List<Op> zkOps = new ArrayList<>();
+ for (org.apache.helix.metaclient.api.Op op : ops) {
+ Op temp = opMap.get(op.getType()).apply(op);
+ zkOps.add(temp);
+ }
+ return zkOps;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ String mode = entryMode.name();
+ if (mode.equals(MetaClientInterface.EntryMode.PERSISTENT.name())) {
+ return CreateMode.fromFlag(0);
+ }
+ if (mode.equals(MetaClientInterface.EntryMode.EPHEMERAL.name())) {
+ return CreateMode.fromFlag(1);
+ }
+ return CreateMode.fromFlag(-1);
+ }
+
+ /**
+ * Helper function for transactionOP. Converts the result from calling zk
transactional support into
+ * metaclient OpResults.
+ * @param zkResult
+ * @return
+ */
+ public static List<OpResult>
zkOpResultToMetaClient(List<org.apache.zookeeper.OpResult> zkResult) {
+ Map<Class<? extends org.apache.zookeeper.OpResult>,
Function<org.apache.zookeeper.OpResult, OpResult>> map = new HashMap<>();
Review Comment:
same
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
Review Comment:
For this particular case, since the key is enum, please use EnumMap for
better performance.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
+
+ 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);
+ } 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.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()));
+
+ List<Op> zkOps = new ArrayList<>();
+ for (org.apache.helix.metaclient.api.Op op : ops) {
+ Op temp = opMap.get(op.getType()).apply(op);
+ zkOps.add(temp);
+ }
+ return zkOps;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ String mode = entryMode.name();
+ if (mode.equals(MetaClientInterface.EntryMode.PERSISTENT.name())) {
+ return CreateMode.fromFlag(0);
+ }
+ if (mode.equals(MetaClientInterface.EntryMode.EPHEMERAL.name())) {
+ return CreateMode.fromFlag(1);
+ }
+ return CreateMode.fromFlag(-1);
+ }
+
+ /**
+ * Helper function for transactionOP. Converts the result from calling zk
transactional support into
+ * metaclient OpResults.
+ * @param zkResult
+ * @return
+ */
+ public static List<OpResult>
zkOpResultToMetaClient(List<org.apache.zookeeper.OpResult> zkResult) {
+ Map<Class<? extends org.apache.zookeeper.OpResult>,
Function<org.apache.zookeeper.OpResult, OpResult>> map = new HashMap<>();
+
+ map.put(org.apache.zookeeper.OpResult.CreateResult.class, 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()),
+ zkOpCreateResult.getStat().getVersion());
+ return new OpResult.CreateResult(zkOpCreateResult.getPath(),
metaClientStat);
+ }});
+
+ map.put(org.apache.zookeeper.OpResult.DeleteResult.class, opResult -> new
OpResult.DeleteResult());
+
+ map.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()),
+ zkOpGetDataResult.getStat().getVersion());
+ return new OpResult.GetDataResult(zkOpGetDataResult.getData(),
metaClientStat);
+ });
+
+ map.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()),
+ zkOpSetDataResult.getStat().getVersion());
+ return new OpResult.SetDataResult(metaClientStat);
+ });
+
+ map.put(org.apache.zookeeper.OpResult.GetChildrenResult.class, opResult ->
new OpResult.GetChildrenResult(
+ ((org.apache.zookeeper.OpResult.GetChildrenResult)
opResult).getChildren()));
+
+ map.put(org.apache.zookeeper.OpResult.CheckResult.class, opResult -> new
OpResult.CheckResult());
+
+ map.put(org.apache.zookeeper.OpResult.ErrorResult.class, opResult -> new
OpResult.ErrorResult(
+ ((org.apache.zookeeper.OpResult.ErrorResult) opResult).getErr()));
+
+ List<OpResult> metaClientOpResult = new ArrayList<>();
+ for (org.apache.zookeeper.OpResult opResult : zkResult) {
+ metaClientOpResult.add(map.get(opResult.getClass()).apply(opResult));
Review Comment:
let's do a null check after map.get() to be safer
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,156 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.api.OpResult;
+import org.apache.helix.metaclient.constants.*;
+import org.apache.helix.zookeeper.zkclient.exception.*;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.server.EphemeralType;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.function.Function;
+
+public class ZkMetaClientUtil {
+ //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 ZkMetaClientUtil(){
+ }
+
+ /**
+ * Helper function for transactionOp. Converts MetaClient Op's into Zk Ops
to execute
+ * zk transactional support.
+ * @param ops
+ * @return
+ */
+ public static List<Op>
metaClientOpToZk(Iterable<org.apache.helix.metaclient.api.Op> ops) {
+ Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> opMap = new HashMap<>();
+
+ 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);
+ } 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.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()));
+
+ List<Op> zkOps = new ArrayList<>();
+ for (org.apache.helix.metaclient.api.Op op : ops) {
+ Op temp = opMap.get(op.getType()).apply(op);
+ zkOps.add(temp);
+ }
+ return zkOps;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ String mode = entryMode.name();
+ if (mode.equals(MetaClientInterface.EntryMode.PERSISTENT.name())) {
+ return CreateMode.fromFlag(0);
+ }
+ if (mode.equals(MetaClientInterface.EntryMode.EPHEMERAL.name())) {
+ return CreateMode.fromFlag(1);
+ }
+ return CreateMode.fromFlag(-1);
Review Comment:
Does -1 mean illegal argument?
Let's explicitly throw `IllegalArgumentException` to be more clear
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]