You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2021/03/05 23:28:53 UTC

[GitHub] [helix] pkuwm commented on a change in pull request #1663: Track failures for async read/write in ZkClient

pkuwm commented on a change in pull request #1663:
URL: https://github.com/apache/helix/pull/1663#discussion_r588773453



##########
File path: zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestZkClientAsyncFailure.java
##########
@@ -0,0 +1,207 @@
+package org.apache.helix.zookeeper.impl.client;
+
+/*
+ * 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.lang.management.ManagementFactory;
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer;
+import org.apache.helix.zookeeper.impl.ZkTestBase;
+import org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallMonitorContext;
+import org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks;
+import org.apache.helix.zookeeper.zkclient.metric.ZkClientMonitor;
+import org.apache.helix.zookeeper.zkclient.metric.ZkClientPathMonitor;
+import org.apache.zookeeper.KeeperException;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.UNKNOWN_RET_CODE;
+
+public class TestZkClientAsyncFailure extends ZkTestBase {
+  private final String TEST_ROOT = String.format("/%s", getClass().getSimpleName());
+  private final String NODE_PATH = TEST_ROOT + "/async";
+  final String TEST_TAG = "test_tag";
+  final String TEST_KEY = "test_key";
+  final String TEST_INSTANCE = "test_instance";
+
+  private org.apache.helix.zookeeper.zkclient.ZkClient _zkClient;
+  private String _zkServerAddress;
+
+  private final MBeanServer _beanServer = ManagementFactory.getPlatformMBeanServer();
+
+  private ZkClientMonitor _monitor;
+
+  @BeforeClass
+  public void beforeClass() throws JMException {
+    _zkClient = _zkServerMap.values().iterator().next().getZkClient();
+    _zkServerAddress = _zkClient.getServers();
+    _zkClient.createPersistent(TEST_ROOT);
+
+    _monitor = new ZkClientMonitor(TEST_TAG, TEST_KEY, TEST_INSTANCE, false, null);
+    _monitor.register();
+  }
+
+  @AfterClass
+  public void afterClass() {
+    _monitor.unregister();
+    _zkClient.deleteRecursively(TEST_ROOT);
+    _zkClient.close();

Review comment:
       This zkClient should not be closed, because it is attached to a zk server and could be used in other tests. If it is closed in this test, and following tests try to use this zkClient, the tests will fail because the zkClient is closed.

##########
File path: zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestZkClientAsyncFailure.java
##########
@@ -0,0 +1,207 @@
+package org.apache.helix.zookeeper.impl.client;
+
+/*
+ * 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.lang.management.ManagementFactory;
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer;
+import org.apache.helix.zookeeper.impl.ZkTestBase;
+import org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallMonitorContext;
+import org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks;
+import org.apache.helix.zookeeper.zkclient.metric.ZkClientMonitor;
+import org.apache.helix.zookeeper.zkclient.metric.ZkClientPathMonitor;
+import org.apache.zookeeper.KeeperException;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.UNKNOWN_RET_CODE;
+
+public class TestZkClientAsyncFailure extends ZkTestBase {
+  private final String TEST_ROOT = String.format("/%s", getClass().getSimpleName());
+  private final String NODE_PATH = TEST_ROOT + "/async";
+  final String TEST_TAG = "test_tag";
+  final String TEST_KEY = "test_key";
+  final String TEST_INSTANCE = "test_instance";
+
+  private org.apache.helix.zookeeper.zkclient.ZkClient _zkClient;
+  private String _zkServerAddress;
+
+  private final MBeanServer _beanServer = ManagementFactory.getPlatformMBeanServer();
+
+  private ZkClientMonitor _monitor;
+
+  @BeforeClass
+  public void beforeClass() throws JMException {
+    _zkClient = _zkServerMap.values().iterator().next().getZkClient();
+    _zkServerAddress = _zkClient.getServers();
+    _zkClient.createPersistent(TEST_ROOT);
+
+    _monitor = new ZkClientMonitor(TEST_TAG, TEST_KEY, TEST_INSTANCE, false, null);
+    _monitor.register();
+  }
+
+  @AfterClass
+  public void afterClass() {
+    _monitor.unregister();
+    _zkClient.deleteRecursively(TEST_ROOT);
+    _zkClient.close();
+  }
+
+  private ObjectName buildObjectName(String tag, String key, String instance)
+      throws MalformedObjectNameException {
+    return ZkClientMonitor.getObjectName(tag, key, instance);
+  }
+
+  private ObjectName buildPathMonitorObjectName(String tag, String key, String instance,
+      String path) throws MalformedObjectNameException {
+    return new ObjectName(String.format("%s,%s=%s", buildObjectName(tag, key, instance).toString(),
+        ZkClientPathMonitor.MONITOR_PATH, path));
+  }
+
+  @Test
+  public void testAsyncWrite() throws JMException {
+    TestZkClientAsyncFailure.MockAsyncZkClient testZkClient =
+        new TestZkClientAsyncFailure.MockAsyncZkClient(_zkServerAddress);
+    try {
+      ZNRecord tmpRecord = new ZNRecord("tmpRecord");
+      tmpRecord.setSimpleField("foo", "bar");
+      testZkClient.createPersistent(NODE_PATH, tmpRecord);
+
+      ObjectName instancesName = buildPathMonitorObjectName(TEST_TAG, TEST_KEY, TEST_INSTANCE,
+          ZkClientPathMonitor.PredefinedPath.Root.name());
+
+      ZkAsyncCallbacks.SetDataCallbackHandler setCallback =
+          new ZkAsyncCallbacks.SetDataCallbackHandler();
+      Assert.assertEquals(setCallback.getRc(), UNKNOWN_RET_CODE);
+
+      tmpRecord.setSimpleField("test", "data");
+
+      // asyncSet should succeed because the return code is OK
+      testZkClient.setAsyncCallRC(KeeperException.Code.OK.intValue());
+      testZkClient.asyncSetData(NODE_PATH, tmpRecord, -1, setCallback);
+      setCallback.waitForSuccess();
+      Assert.assertEquals(setCallback.getRc(), KeeperException.Code.OK.intValue());
+      Assert.assertEquals(((ZNRecord) testZkClient.readData(NODE_PATH)).getSimpleField("test"),
+          "data");
+      Assert.assertEquals((long) _beanServer.getAttribute(instancesName,
+          ZkClientPathMonitor.PredefinedMetricDomains.WriteFailureCounter.toString()), 0);
+
+      // asyncSet should fail because the return code is APIERROR
+      testZkClient.setAsyncCallRC(KeeperException.Code.APIERROR.intValue());
+      testZkClient.asyncSetData(NODE_PATH, tmpRecord, -1, setCallback);
+      setCallback.waitForSuccess();
+      Assert.assertEquals(setCallback.getRc(), KeeperException.Code.APIERROR.intValue());
+      Assert.assertEquals((long) _beanServer.getAttribute(instancesName,
+          ZkClientPathMonitor.PredefinedMetricDomains.WriteFailureCounter.toString()), 1);
+    } finally {
+      testZkClient.close();
+      _zkClient.delete(NODE_PATH);

Review comment:
       This `_zkClient` might not be the one you want to use, because the test suite could start 3 zk servers. If testZkClient connects address1, and _zkClient connects to address2 (because you get it from the map), this cleanup won't work.
   
   I suggest you may want to create a zkclient connecting to the `ZK_ADDR` and use this client for the whole test class. Then close it afterClass

##########
File path: zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java
##########
@@ -50,6 +50,10 @@ public void processResult(int rc, String path, Object ctx, byte[] data, Stat sta
         if (_data != null && ctx != null && ctx instanceof ZkAsyncCallMonitorContext) {
           ((ZkAsyncCallMonitorContext) ctx).setBytes(_data.length);
         }
+      } else if(rc != Code.NONODE.intValue()) {

Review comment:
       This logic means, for each retry, we'll record one metric. Is it the expected requirement?
   My thought was, only when the whole method fails, we record the failure. 




----------------------------------------------------------------
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.

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