rahulrane50 commented on code in PR #2343:
URL: https://github.com/apache/helix/pull/2343#discussion_r1082949081
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java:
##########
@@ -391,34 +380,13 @@ public int hashCode() {
}
}
- private static MetaClientException
translateZkExceptionToMetaclientException(ZkException e) {
- if (e instanceof ZkNodeExistsException) {
- return new MetaClientNoNodeException(e);
- } else if (e instanceof ZkBadVersionException) {
- return new MetaClientBadVersionException(e);
- } else if (e instanceof ZkTimeoutException) {
- return new MetaClientTimeoutException(e);
- } else if (e instanceof ZkInterruptedException) {
- return new MetaClientInterruptException(e);
- } else {
- return new MetaClientException(e);
- }
- }
-
- private static EntryMode convertZkEntryMode(long ephemeralOwner) {
- EphemeralType zkEphemeralType = EphemeralType.get(ephemeralOwner);
- switch (zkEphemeralType) {
- case VOID:
- return EntryMode.PERSISTENT;
- case CONTAINER:
- return EntryMode.CONTAINER;
- case NORMAL:
- return EntryMode.EPHEMERAL;
- // TODO: TTL is not supported now.
- //case TTL:
- // return EntryMode.TTL;
- default:
- throw new IllegalArgumentException(zkEphemeralType + " is not
supported.");
- }
+ @Override
+ public List<OpResult> transactionOP(Iterable<Op> iterable) {
Review Comment:
neat: iterable looks bad variable name. Do you want to change it something
like iterableOps or just ops or anything which tells that these are ops list.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
+ getOpMap().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));
+ }
+ });
+
+ getOpMap().put(org.apache.helix.metaclient.api.Op.Type.DELETE,
+ op -> Op.delete(op.getPath(),
((org.apache.helix.metaclient.api.Op.Delete) op).getVersion()));
+
+ getOpMap().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()));
+
+ getOpMap().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) {
+ Function<org.apache.helix.metaclient.api.Op, Op> function =
getOpMap().get(op.getType());
+ if (function != null) {
+ zkOps.add(function.apply(op));
+ } else {
+ throw new IllegalArgumentException("Op type " + op.getType().name() +
"is not supported.");
+ }
+ }
+ return zkOps;
+ }
+
+ private static final class OpmapHolder {
+ static final Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> OPMAP =
+ new EnumMap<>(org.apache.helix.metaclient.api.Op.Type.class);
+ }
+
+ private static Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> getOpMap() {
+ return OpmapHolder.OPMAP;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ if (entryMode == MetaClientInterface.EntryMode.PERSISTENT) {
+ return CreateMode.fromFlag(0);
+ }
+ if (entryMode == MetaClientInterface.EntryMode.EPHEMERAL) {
+ return CreateMode.fromFlag(1);
+ }
+ if (entryMode == MetaClientInterface.EntryMode.CONTAINER) {
+ return CreateMode.fromFlag(4);
+ }
+ throw new IllegalArgumentException(entryMode.name() + " is not a supported
EntryMode.");
+ }
+
+ /**
+ * 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) {
Review Comment:
neat: similar comment about plural.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
+ getOpMap().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));
+ }
+ });
+
+ getOpMap().put(org.apache.helix.metaclient.api.Op.Type.DELETE,
+ op -> Op.delete(op.getPath(),
((org.apache.helix.metaclient.api.Op.Delete) op).getVersion()));
+
+ getOpMap().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()));
+
+ getOpMap().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) {
+ Function<org.apache.helix.metaclient.api.Op, Op> function =
getOpMap().get(op.getType());
+ if (function != null) {
+ zkOps.add(function.apply(op));
+ } else {
+ throw new IllegalArgumentException("Op type " + op.getType().name() +
"is not supported.");
+ }
+ }
+ return zkOps;
+ }
+
+ private static final class OpmapHolder {
Review Comment:
neat: OpMapHolder.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //Default ACL value until metaClient Op has ACL of its own.
Review Comment:
neat: let's add a TODO here as well. We might need to think about ACLs being
set by helix.
##########
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:
Okey i am seeing this pattern a lot now let me file a issue to automate
this.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
Review Comment:
neat: If it's gonna return list of ops then should the name of the method be
metaClientOpsToZkOps? or anything which suggest plural.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
+ getOpMap().put(org.apache.helix.metaclient.api.Op.Type.CREATE, op -> {
Review Comment:
Can we avoid having full name of org.apache.helix.metaclient.api.Op.Type
here? i see that at multiple places down below.
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
+ getOpMap().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));
+ }
+ });
+
+ getOpMap().put(org.apache.helix.metaclient.api.Op.Type.DELETE,
+ op -> Op.delete(op.getPath(),
((org.apache.helix.metaclient.api.Op.Delete) op).getVersion()));
+
+ getOpMap().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()));
+
+ getOpMap().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) {
+ Function<org.apache.helix.metaclient.api.Op, Op> function =
getOpMap().get(op.getType());
+ if (function != null) {
+ zkOps.add(function.apply(op));
+ } else {
+ throw new IllegalArgumentException("Op type " + op.getType().name() +
"is not supported.");
+ }
+ }
+ return zkOps;
+ }
+
+ private static final class OpmapHolder {
+ static final Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> OPMAP =
+ new EnumMap<>(org.apache.helix.metaclient.api.Op.Type.class);
+ }
+
+ private static Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> getOpMap() {
+ return OpmapHolder.OPMAP;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ if (entryMode == MetaClientInterface.EntryMode.PERSISTENT) {
Review Comment:
just wondering if making it to switch case would make it cleaner?
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/util/ZkMetaClientUtil.java:
##########
@@ -0,0 +1,201 @@
+package org.apache.helix.metaclient.impl.zk.util;
+
+/*
+ * 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.
+ */
+
+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.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 {
+ //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) {
+ getOpMap().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));
+ }
+ });
+
+ getOpMap().put(org.apache.helix.metaclient.api.Op.Type.DELETE,
+ op -> Op.delete(op.getPath(),
((org.apache.helix.metaclient.api.Op.Delete) op).getVersion()));
+
+ getOpMap().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()));
+
+ getOpMap().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) {
+ Function<org.apache.helix.metaclient.api.Op, Op> function =
getOpMap().get(op.getType());
+ if (function != null) {
+ zkOps.add(function.apply(op));
+ } else {
+ throw new IllegalArgumentException("Op type " + op.getType().name() +
"is not supported.");
+ }
+ }
+ return zkOps;
+ }
+
+ private static final class OpmapHolder {
+ static final Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> OPMAP =
+ new EnumMap<>(org.apache.helix.metaclient.api.Op.Type.class);
+ }
+
+ private static Map<org.apache.helix.metaclient.api.Op.Type,
Function<org.apache.helix.metaclient.api.Op, Op>> getOpMap() {
+ return OpmapHolder.OPMAP;
+ }
+
+ private static CreateMode
convertMetaClientMode(MetaClientInterface.EntryMode entryMode) throws
KeeperException {
+ if (entryMode == MetaClientInterface.EntryMode.PERSISTENT) {
+ return CreateMode.fromFlag(0);
Review Comment:
Should we use something verbose or informative than using hard coded values
like 0, 1 or 4?
##########
meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClient.java:
##########
@@ -39,8 +39,11 @@
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.Op;
+import org.apache.helix.metaclient.api.OpResult;
import org.apache.helix.zookeeper.zkclient.IDefaultNameSpace;
import org.apache.helix.zookeeper.zkclient.ZkServer;
+import org.apache.zookeeper.*;
Review Comment:
this would be huge import! Do we want everything from this package?
--
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]