This is an automated email from the ASF dual-hosted git repository.

jxue pushed a commit to branch metaclient
in repository https://gitbox.apache.org/repos/asf/helix.git

commit 8fa86e9f9342202ab62f6decb548c458e8d7a17c
Author: Marcos Rico Peng <55266861+marcosr...@users.noreply.github.com>
AuthorDate: Wed Aug 2 18:41:59 2023 +0200

    Lattice LockClient LockInfoSerializer Implementation(#2580)
    
    
    ---------
    
    Co-authored-by: mapeng <map...@linkedin.com>
---
 .../helix/metaclient/recipes/lock/LockClient.java  |  9 ++---
 .../helix/metaclient/recipes/lock/LockInfo.java    | 30 ++++++++---------
 .../recipes/lock/LockInfoSerializer.java           | 39 ++++++++++++++++++++++
 .../metaclient/recipes/lock/LockClientTest.java    | 36 ++++++++++++++++----
 4 files changed, 85 insertions(+), 29 deletions(-)

diff --git 
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockClient.java
 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockClient.java
index 480e87c30..a186f3d81 100644
--- 
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockClient.java
+++ 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockClient.java
@@ -48,8 +48,7 @@ public class LockClient implements LockClientInterface, 
AutoCloseable {
     if (MetaClientConfig.StoreType.ZOOKEEPER.equals(config.getStoreType())) {
       ZkMetaClientConfig zkMetaClientConfig = new 
ZkMetaClientConfig.ZkMetaClientConfigBuilder().
           setConnectionAddress(config.getConnectionAddress())
-          // Currently only support ZNRecordSerializer. TODO: make this 
configurable
-          .setZkSerializer((new ZNRecordSerializer()))
+          .setZkSerializer((new LockInfoSerializer()))
           .build();
       _metaClient = new 
ZkMetaClientFactory().getMetaClient(zkMetaClientConfig);
       _metaClient.connect();
@@ -110,11 +109,7 @@ public class LockClient implements LockClientInterface, 
AutoCloseable {
     if (stat == null) {
       return null;
     }
-    //Create a new DataRecord from underlying record
-    DataRecord dataRecord = new DataRecord(_metaClient.get(key));
-    //Create a new LockInfo from DataRecord
-    LockInfo lockInfo = new LockInfo(dataRecord, stat);
-    return lockInfo;
+    return new LockInfo(_metaClient.get(key), stat);
   }
 
   @Override
diff --git 
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfo.java
 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfo.java
index 26736f1b0..2a08c3754 100644
--- 
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfo.java
+++ 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfo.java
@@ -39,7 +39,6 @@ public class LockInfo extends DataRecord {
   public static final long DEFAULT_LAST_RENEWED_AT_LONG = -1L;
   public static final long DEFAULT_TIMEOUT_DURATION = -1L;
   private static final String DEFAULT_LOCK_INFO = "lockInfo.";
-  private DataRecord _dataRecord;
 
   /**
    * The keys to lock information
@@ -59,7 +58,6 @@ public class LockInfo extends DataRecord {
    */
   public LockInfo() {
     super(DEFAULT_LOCK_INFO);
-    _dataRecord = new DataRecord(DEFAULT_LOCK_INFO);
     setLockInfoFields(DEFAULT_LOCK_ID_TEXT, DEFAULT_OWNER_ID_TEXT, 
DEFAULT_CLIENT_ID_TEXT, DEFAULT_CLIENT_DATA, DEFAULT_GRANTED_AT_LONG,
         DEFAULT_LAST_RENEWED_AT_LONG, DEFAULT_TIMEOUT_DURATION);
   }
@@ -140,7 +138,7 @@ public class LockInfo extends DataRecord {
    *               It is created by the lockClient and a new one is created 
for each time the lock is acquired.
    */
   public void setLockId(String lockId) {
-    _dataRecord.setSimpleField(LockInfoAttribute.LOCK_ID.name(), lockId == 
null ? DEFAULT_LOCK_ID_TEXT : lockId);
+    setSimpleField(LockInfoAttribute.LOCK_ID.name(), lockId == null ? 
DEFAULT_LOCK_ID_TEXT : lockId);
   }
 
   /**
@@ -150,7 +148,7 @@ public class LockInfo extends DataRecord {
    *                by the same owner.
    */
   public void setOwnerId(String ownerId) {
-    _dataRecord.setSimpleField(LockInfoAttribute.OWNER_ID.name(), ownerId == 
null ? DEFAULT_OWNER_ID_TEXT : ownerId);
+    setSimpleField(LockInfoAttribute.OWNER_ID.name(), ownerId == null ? 
DEFAULT_OWNER_ID_TEXT : ownerId);
   }
 
   /**
@@ -158,7 +156,7 @@ public class LockInfo extends DataRecord {
    * @param clientId Unique identifier that represents who will get the lock 
(the client).
    */
   public void setClientId(String clientId) {
-    _dataRecord.setSimpleField(LockInfoAttribute.CLIENT_ID.name(), clientId == 
null ? DEFAULT_CLIENT_ID_TEXT : clientId);
+    setSimpleField(LockInfoAttribute.CLIENT_ID.name(), clientId == null ? 
DEFAULT_CLIENT_ID_TEXT : clientId);
   }
 
   /**
@@ -166,7 +164,7 @@ public class LockInfo extends DataRecord {
    * @param clientData String representing the serialized data object
    */
   public void setClientData(String clientData) {
-    _dataRecord.setSimpleField(LockInfoAttribute.CLIENT_DATA.name(), 
clientData == null ? DEFAULT_CLIENT_DATA : clientData);
+    setSimpleField(LockInfoAttribute.CLIENT_DATA.name(), clientData == null ? 
DEFAULT_CLIENT_DATA : clientData);
   }
 
   /**
@@ -174,7 +172,7 @@ public class LockInfo extends DataRecord {
    * @param grantTime Long representing the time at which the lock was granted
    */
   public void setGrantedAt(Long grantTime) {
-    _dataRecord.setLongField(LockInfoAttribute.GRANTED_AT.name(), grantTime);
+    setLongField(LockInfoAttribute.GRANTED_AT.name(), grantTime);
   }
 
   /**
@@ -182,7 +180,7 @@ public class LockInfo extends DataRecord {
    * @param lastRenewalTime Long representing the time at which the lock was 
last renewed
    */
   public void setLastRenewedAt(Long lastRenewalTime) {
-    _dataRecord.setLongField(LockInfoAttribute.LAST_RENEWED_AT.name(), 
lastRenewalTime);
+    setLongField(LockInfoAttribute.LAST_RENEWED_AT.name(), lastRenewalTime);
   }
 
   /**
@@ -191,7 +189,7 @@ public class LockInfo extends DataRecord {
    */
   public void setTimeout(long timeout) {
     // Always store the timeout value in milliseconds for the sake of 
simplicity
-    _dataRecord.setLongField(LockInfoAttribute.TIMEOUT.name(), timeout);
+    setLongField(LockInfoAttribute.TIMEOUT.name(), timeout);
   }
 
   /**
@@ -199,7 +197,7 @@ public class LockInfo extends DataRecord {
    * @return the owner id of the lock, {@link #DEFAULT_OWNER_ID_TEXT} if there 
is no owner id set
    */
   public String getOwnerId() {
-    return _dataRecord.getStringField(LockInfoAttribute.OWNER_ID.name(), 
DEFAULT_OWNER_ID_TEXT);
+    return getStringField(LockInfoAttribute.OWNER_ID.name(), 
DEFAULT_OWNER_ID_TEXT);
   }
 
   /**
@@ -207,7 +205,7 @@ public class LockInfo extends DataRecord {
    * @return the client id of the lock, {@link #DEFAULT_CLIENT_ID_TEXT} if 
there is no client id set
    */
   public String getClientId() {
-    return _dataRecord.getStringField(LockInfoAttribute.CLIENT_ID.name(), 
DEFAULT_CLIENT_ID_TEXT);
+    return getStringField(LockInfoAttribute.CLIENT_ID.name(), 
DEFAULT_CLIENT_ID_TEXT);
   }
 
   /**
@@ -215,7 +213,7 @@ public class LockInfo extends DataRecord {
    * @return the id of the lock, {@link #DEFAULT_LOCK_ID_TEXT} if there is no 
lock id set
    */
   public String getLockId() {
-    return _dataRecord.getStringField(LockInfoAttribute.LOCK_ID.name(), 
DEFAULT_LOCK_ID_TEXT);
+    return getStringField(LockInfoAttribute.LOCK_ID.name(), 
DEFAULT_LOCK_ID_TEXT);
   }
 
   /**
@@ -224,7 +222,7 @@ public class LockInfo extends DataRecord {
    * if there is no client data set.
    */
   public String getClientData() {
-    return _dataRecord.getStringField(LockInfoAttribute.CLIENT_DATA.name(), 
DEFAULT_CLIENT_DATA);
+    return getStringField(LockInfoAttribute.CLIENT_DATA.name(), 
DEFAULT_CLIENT_DATA);
   }
 
   /**
@@ -233,7 +231,7 @@ public class LockInfo extends DataRecord {
    * if there is no grant time set
    */
   public Long getGrantedAt() {
-    return _dataRecord.getLongField(LockInfoAttribute.GRANTED_AT.name(), 
DEFAULT_GRANTED_AT_LONG);
+    return getLongField(LockInfoAttribute.GRANTED_AT.name(), 
DEFAULT_GRANTED_AT_LONG);
   }
 
   /**
@@ -242,7 +240,7 @@ public class LockInfo extends DataRecord {
    * if there is no renewal time set
    */
   public Long getLastRenewedAt() {
-    return _dataRecord.getLongField(LockInfoAttribute.LAST_RENEWED_AT.name(), 
DEFAULT_LAST_RENEWED_AT_LONG);
+    return getLongField(LockInfoAttribute.LAST_RENEWED_AT.name(), 
DEFAULT_LAST_RENEWED_AT_LONG);
   }
 
   /**
@@ -250,7 +248,7 @@ public class LockInfo extends DataRecord {
    * @return the expiring time of the lock, {@link #DEFAULT_TIMEOUT_DURATION} 
if there is no timeout set
    */
   public long getTimeout() {
-    return _dataRecord.getLongField(LockInfoAttribute.TIMEOUT.name(), 
DEFAULT_TIMEOUT_DURATION);
+    return getLongField(LockInfoAttribute.TIMEOUT.name(), 
DEFAULT_TIMEOUT_DURATION);
   }
 
 }
diff --git 
a/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfoSerializer.java
 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfoSerializer.java
new file mode 100644
index 000000000..20648fbaf
--- /dev/null
+++ 
b/meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/LockInfoSerializer.java
@@ -0,0 +1,39 @@
+package org.apache.helix.metaclient.recipes.lock;
+
+/*
+ * 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.datamodel.DataRecord;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LockInfoSerializer extends ZNRecordSerializer {
+  private static final Logger LOG = 
LoggerFactory.getLogger(LockInfoSerializer.class);
+  @Override
+  public Object deserialize(byte[] bytes) {
+    try {
+      return new LockInfo(new DataRecord((ZNRecord) super.deserialize(bytes)));
+    } catch (Exception e) {
+      LOG.error("Exception during deserialization of bytes: {}", new 
String(bytes), e);
+      return null;
+    }
+  }
+}
diff --git 
a/meta-client/src/test/java/org/apache/helix/metaclient/recipes/lock/LockClientTest.java
 
b/meta-client/src/test/java/org/apache/helix/metaclient/recipes/lock/LockClientTest.java
index a8ca387ca..672781b08 100644
--- 
a/meta-client/src/test/java/org/apache/helix/metaclient/recipes/lock/LockClientTest.java
+++ 
b/meta-client/src/test/java/org/apache/helix/metaclient/recipes/lock/LockClientTest.java
@@ -28,6 +28,13 @@ import org.testng.annotations.Test;
 public class LockClientTest extends ZkMetaClientTestBase {
 
   private static final String TEST_INVALID_PATH = "/_invalid/a/b/c";
+  private static final String OWNER_ID = "urn:li:principal:UNKNOWN";
+  private static final String CLIENT_ID = "test_client_id";
+  private static final String CLIENT_DATA = "client_data";
+  private static final String LOCK_ID = "794c8a4c-c14b-4c23-b83f-4e1147fc6978";
+  private static final long GRANT_TIME = System.currentTimeMillis();
+  private static final long LAST_RENEWAL_TIME = System.currentTimeMillis();
+  private static final long TIMEOUT = 100000;
 
   public LockClient createLockClient() {
 
@@ -37,11 +44,23 @@ public class LockClientTest extends ZkMetaClientTestBase {
     return new LockClient(config);
   }
 
+  private LockInfo createLockInfo() {
+    LockInfo lockInfo = new LockInfo();
+    lockInfo.setOwnerId(OWNER_ID);
+    lockInfo.setClientId(CLIENT_ID);
+    lockInfo.setClientData(CLIENT_DATA);
+    lockInfo.setLockId(LOCK_ID);
+    lockInfo.setGrantedAt(GRANT_TIME);
+    lockInfo.setLastRenewedAt(LAST_RENEWAL_TIME);
+    lockInfo.setTimeout(TIMEOUT);
+    return lockInfo;
+  }
+
   @Test
   public void testAcquireLock() {
     final String key = "/TestLockClient_testAcquireLock";
     LockClient lockClient = createLockClient();
-    LockInfo lockInfo = new LockInfo();
+    LockInfo lockInfo = createLockInfo();
     lockClient.acquireLock(key, lockInfo, 
MetaClientInterface.EntryMode.PERSISTENT);
     Assert.assertNotNull(lockClient.retrieveLock(key));
     try {
@@ -56,7 +75,7 @@ public class LockClientTest extends ZkMetaClientTestBase {
   public void testReleaseLock() {
     final String key = "/TestLockClient_testReleaseLock";
     LockClient lockClient = createLockClient();
-    LockInfo lockInfo = new LockInfo();
+    LockInfo lockInfo = createLockInfo();
     lockClient.acquireLock(key, lockInfo, 
MetaClientInterface.EntryMode.PERSISTENT);
     Assert.assertNotNull(lockClient.retrieveLock(key));
 
@@ -69,7 +88,7 @@ public class LockClientTest extends ZkMetaClientTestBase {
   public void testAcquireTTLLock() {
     final String key = "/TestLockClient_testAcquireTTLLock";
     LockClient lockClient = createLockClient();
-    LockInfo lockInfo = new LockInfo();
+    LockInfo lockInfo = createLockInfo();
     lockClient.acquireLockWithTTL(key, lockInfo, 1L);
     Assert.assertNotNull(lockClient.retrieveLock(key));
     try {
@@ -84,9 +103,14 @@ public class LockClientTest extends ZkMetaClientTestBase {
   public void testRetrieveLock() {
     final String key = "/TestLockClient_testRetrieveLock";
     LockClient lockClient = createLockClient();
-    LockInfo lockInfo = new LockInfo();
+    LockInfo lockInfo = createLockInfo();
     lockClient.acquireLock(key, lockInfo, 
MetaClientInterface.EntryMode.PERSISTENT);
     Assert.assertNotNull(lockClient.retrieveLock(key));
+    Assert.assertEquals(lockClient.retrieveLock(key).getOwnerId(), OWNER_ID);
+    Assert.assertEquals(lockClient.retrieveLock(key).getClientId(), CLIENT_ID);
+    Assert.assertEquals(lockClient.retrieveLock(key).getClientData(), 
CLIENT_DATA);
+    Assert.assertEquals(lockClient.retrieveLock(key).getLockId(), LOCK_ID);
+    Assert.assertEquals(lockClient.retrieveLock(key).getTimeout(), TIMEOUT);
     Assert.assertNull(lockClient.retrieveLock(TEST_INVALID_PATH));
   }
 
@@ -94,12 +118,12 @@ public class LockClientTest extends ZkMetaClientTestBase {
   public void testRenewTTLLock() {
     final String key = "/TestLockClient_testRenewTTLLock";
     LockClient lockClient = createLockClient();
-    LockInfo lockInfo = new LockInfo();
+    LockInfo lockInfo = createLockInfo();
     lockClient.acquireLockWithTTL(key, lockInfo, 1L);
     Assert.assertNotNull(lockClient.retrieveLock(key));
 
     lockClient.renewTTLLock(key);
-    Assert.assertNotSame(lockClient.retrieveLock(key).getGrantedAt(), 
lockInfo.getLastRenewedAt());
+    Assert.assertNotSame(lockClient.retrieveLock(key).getGrantedAt(), 
lockClient.retrieveLock(key).getLastRenewedAt());
     try {
       lockClient.renewTTLLock(TEST_INVALID_PATH);
       Assert.fail("Should not be able to renew lock for key: " + key);

Reply via email to