You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by "qqu0127 (via GitHub)" <gi...@apache.org> on 2023/02/17 23:37:05 UTC

[GitHub] [helix] qqu0127 commented on a diff in pull request #2354: Implement zk Meta client async crud

qqu0127 commented on code in PR #2354:
URL: https://github.com/apache/helix/pull/2354#discussion_r1110440102


##########
meta-client/src/main/java/org/apache/helix/metaclient/api/MetaClientInterface.java:
##########
@@ -515,5 +515,9 @@ default boolean subscribeOneTimeChildChanges(String key, ChildChangeListener lis
    */
   boolean waitUntilExists(String key, TimeUnit timeUnit, long timeOut);
 
+  public byte[] serialize(T data, String path);
+
+  public T deserialize(byte[] bytes, String path);

Review Comment:
   +1 on documentation. Why do we need these two methods? 



##########
meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/ZkMetaClientTestBase.java:
##########
@@ -0,0 +1,95 @@
+package org.apache.helix.metaclient.impl.zk;
+
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+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.testng.annotations.AfterSuite;
+import org.testng.annotations.BeforeSuite;
+
+
+public abstract class ZkMetaClientTestBase {
+
+  protected static final String ZK_ADDR = "localhost:2183";
+  protected static final int DEFAULT_TIMEOUT_MS = 1000;
+  protected static final String ENTRY_STRING_VALUE = "test-value";
+
+  private final Object _syncObject = new Object();

Review Comment:
   not used



##########
meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/ZkMetaClientTestBase.java:
##########
@@ -0,0 +1,95 @@
+package org.apache.helix.metaclient.impl.zk;
+
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+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.testng.annotations.AfterSuite;
+import org.testng.annotations.BeforeSuite;
+
+
+public abstract class ZkMetaClientTestBase {
+
+  protected static final String ZK_ADDR = "localhost:2183";
+  protected static final int DEFAULT_TIMEOUT_MS = 1000;
+  protected static final String ENTRY_STRING_VALUE = "test-value";
+
+  private final Object _syncObject = new Object();
+
+  private ZkServer _zkServer;

Review Comment:
   An open discussion here. So with BeforeSuite, testng will only trigger that method once. That means for most subclasses of this, this `_zkServer` will be null. Maybe we should set this as static? But I'm really not sure if this is the right approach.
   (And I don't know what will happen with afterSuite, could there be NPE?)



##########
meta-client/src/test/java/org/apache/helix/metaclient/impl/zk/TestZkMetaClientAsyncOperations.java:
##########
@@ -0,0 +1,231 @@
+package org.apache.helix.metaclient.impl.zk;
+
+/*
+ * 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 java.util.concurrent.CountDownLatch;
+
+import javax.annotation.Nullable;
+
+import org.apache.helix.metaclient.api.AsyncCallback;
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.zookeeper.KeeperException;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class TestZkMetaClientAsyncOperations extends ZkMetaClientTestBase {
+
+  static TestAsyncContext[] asyncContext = new TestAsyncContext[1];
+  static final String entryKey = "/TestAsyncEntryKey";
+  static final String nonExistsEntry = "/a/b/c";
+
+  static class TestAsyncContext {
+    int _asyncCallSize;
+    CountDownLatch _countDownLatch;
+    int[] _returnCode;
+    MetaClientInterface.Stat[] _stats;
+    String[] _data;
+
+    TestAsyncContext(int callSize) {
+      _asyncCallSize = callSize;
+      _countDownLatch = new CountDownLatch(callSize);
+      _returnCode = new int[callSize];
+      _stats = new MetaClientInterface.Stat[callSize];
+      _data = new String[callSize];
+    }
+
+    public CountDownLatch getCountDownLatch() {
+      return _countDownLatch;
+    }
+
+    public void countDown() {
+      _countDownLatch.countDown();
+    }
+
+    public int getReturnCode(int idx) {
+      return _returnCode[idx];
+    }
+
+    public MetaClientInterface.Stat getStats(int idx) {
+      return _stats[idx];
+    }
+
+    public String getData(int idx) {
+      return _data[idx];
+    }
+
+    public void setReturnCodeWhenFinished(int idx, int returnCode) {
+      _returnCode[idx] = returnCode;
+    }
+
+    public void setStatWhenFinished(int idx, MetaClientInterface.Stat stat) {
+      _stats[idx] = stat;
+    }
+
+    public void setDataWhenFinished(int idx, String data) {
+      _data[idx] = data;
+    }
+  }
+
+  @Test
+  public void testAsyncCreateSetAndGet() {
+    asyncContext[0] = new TestAsyncContext(2);
+    try (ZkMetaClient<String> zkMetaClient = createZkMetaClient()) {
+      zkMetaClient.connect();
+
+      zkMetaClient
+          .asyncCreate(entryKey, "async_create-data", MetaClientInterface.EntryMode.PERSISTENT,
+              new AsyncCallback.VoidCallback() {
+                @Override
+                public void processResult(int returnCode, String key) {
+                  asyncContext[0].setReturnCodeWhenFinished(0, returnCode);
+                  asyncContext[0].countDown();
+                }
+              });
+
+      zkMetaClient.asyncCreate(nonExistsEntry, "async_create-data-invalid",
+          MetaClientInterface.EntryMode.PERSISTENT, new AsyncCallback.VoidCallback() {
+            @Override
+            public void processResult(int returnCode, String key) {
+              asyncContext[0].setReturnCodeWhenFinished(1, returnCode);
+              asyncContext[0].countDown();
+            }
+          });
+
+      asyncContext[0].getCountDownLatch().await();
+
+      Assert.assertEquals(asyncContext[0].getReturnCode(0), KeeperException.Code.OK.intValue());
+      Assert.assertEquals(asyncContext[0].getReturnCode(1), KeeperException.Code.NONODE.intValue());
+
+      // create the entry again and expect a duplicated error code
+      asyncContext[0] = new TestAsyncContext(1);
+      zkMetaClient
+          .asyncCreate(entryKey, "async_create-data", MetaClientInterface.EntryMode.PERSISTENT,
+              new AsyncCallback.VoidCallback() {
+                @Override
+                public void processResult(int returnCode, String key) {
+                  asyncContext[0].setReturnCodeWhenFinished(0, returnCode);
+                  asyncContext[0].countDown();
+                }
+              });
+      asyncContext[0].getCountDownLatch().await();
+      Assert.assertEquals(asyncContext[0].getReturnCode(0),
+          KeeperException.Code.NODEEXISTS.intValue());
+
+
+      // test set
+      asyncContext[0] = new TestAsyncContext(1);
+      zkMetaClient
+          .asyncSet(entryKey, "async_create-data-new", 0,
+              new AsyncCallback.StatCallback() {
+                @Override
+                public void processResult(int returnCode, String key,
+                    @Nullable MetaClientInterface.Stat stat) {
+                  asyncContext[0].setReturnCodeWhenFinished(0, returnCode);
+                  asyncContext[0].setStatWhenFinished(0, stat);
+                  asyncContext[0].countDown();
+                }
+              });
+      asyncContext[0].getCountDownLatch().await();

Review Comment:
   nit: better to set a timeout and Assert on that



-- 
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: reviews-unsubscribe@helix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org