Copilot commented on code in PR #16384:
URL: https://github.com/apache/dubbo/pull/16384#discussion_r3611701881


##########
dubbo-serialization/dubbo-serialization-fastjson2/src/test/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2StripedLockTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.
+ */
+package org.apache.dubbo.common.serialize.fastjson2;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+import org.apache.dubbo.common.serialize.Serialization;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;

Review Comment:
   Unused import `CountDownLatch` causes a compilation error in Java (unused 
imports are not allowed). Remove it.



##########
dubbo-serialization/dubbo-serialization-fastjson2/src/main/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2ObjectInput.java:
##########
@@ -192,6 +216,11 @@ private void updateClassLoaderIfNeed() {
         }
     }
 
+    private static Object getStripe(Class<?> cls) {
+        int hash = cls == null ? 0 : cls.getName().hashCode();
+        return STRIPES[(hash & 0x7FFFFFFF) % STRIPE_COUNT];
+    }

Review Comment:
   `getStripe` claims locks are keyed by the target class, but it currently 
hashes `cls.getName()`. That introduces avoidable contention for same-named 
classes from different classloaders, and doesn’t actually key on the `Class<?>` 
identity. Hashing the `Class` instance (identity-based) better matches the 
intent and reduces unnecessary serialization.



##########
dubbo-serialization/dubbo-serialization-fastjson2/src/test/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2StripedLockTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.
+ */
+package org.apache.dubbo.common.serialize.fastjson2;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+import org.apache.dubbo.common.serialize.Serialization;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class FastJson2StripedLockTest {
+
+    private Serialization createSerialization() {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        return frameworkModel
+                .getExtensionLoader(Serialization.class)
+                .getExtension("fastjson2");
+    }
+
+    private URL createURL() {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        return URL.valueOf("").setScopeModel(frameworkModel);
+    }

Review Comment:
   `createSerialization()` / `createURL()` are unused in this test class, and 
they each allocate a new `FrameworkModel` without destroying it. Removing these 
helpers avoids dead code and potential resource leaks if they get used later.



##########
dubbo-serialization/dubbo-serialization-fastjson2/src/test/java/org/apache/dubbo/common/serialize/fastjson2/FastJson2StripedLockTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.
+ */
+package org.apache.dubbo.common.serialize.fastjson2;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+import org.apache.dubbo.common.serialize.Serialization;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class FastJson2StripedLockTest {
+
+    private Serialization createSerialization() {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        return frameworkModel
+                .getExtensionLoader(Serialization.class)
+                .getExtension("fastjson2");
+    }
+
+    private URL createURL() {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        return URL.valueOf("").setScopeModel(frameworkModel);
+    }
+
+    @Test
+    void testReadObjectWithType() throws IOException, ClassNotFoundException {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        Serialization serialization =
+                
frameworkModel.getExtensionLoader(Serialization.class).getExtension("fastjson2");
+        URL url = URL.valueOf("").setScopeModel(frameworkModel);
+
+        TrustedPojo pojo = new TrustedPojo(42.0);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        ObjectOutput objectOutput = serialization.serialize(url, outputStream);
+        objectOutput.writeObject(pojo);
+        objectOutput.flushBuffer();
+
+        byte[] bytes = outputStream.toByteArray();
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+        ObjectInput objectInput = serialization.deserialize(url, inputStream);
+
+        TrustedPojo result = objectInput.readObject(TrustedPojo.class, (Type) 
TrustedPojo.class);
+        Assertions.assertEquals(pojo, result);
+
+        frameworkModel.destroy();
+    }
+
+    @Test
+    void testReadObjectWithTypeList() throws IOException, 
ClassNotFoundException {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        Serialization serialization =
+                
frameworkModel.getExtensionLoader(Serialization.class).getExtension("fastjson2");
+        URL url = URL.valueOf("").setScopeModel(frameworkModel);
+
+        List<TrustedPojo> pojos = new ArrayList<>();
+        pojos.add(new TrustedPojo(1.0));
+        pojos.add(new TrustedPojo(2.0));
+
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        ObjectOutput objectOutput = serialization.serialize(url, outputStream);
+        objectOutput.writeObject(pojos);
+        objectOutput.flushBuffer();
+
+        byte[] bytes = outputStream.toByteArray();
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+        ObjectInput objectInput = serialization.deserialize(url, inputStream);
+
+        List<?> result = objectInput.readObject(List.class, new 
java.lang.reflect.ParameterizedType() {
+            @Override
+            public Type[] getActualTypeArguments() {
+                return new Type[]{TrustedPojo.class};
+            }
+
+            @Override
+            public Type getRawType() {
+                return List.class;
+            }
+
+            @Override
+            public Type getOwnerType() {
+                return null;
+            }
+        });
+        Assertions.assertNotNull(result);
+        Assertions.assertEquals(2, result.size());
+
+        frameworkModel.destroy();
+    }
+
+    @Test
+    void testReadObjectNullClass() throws IOException, ClassNotFoundException {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        Serialization serialization =
+                
frameworkModel.getExtensionLoader(Serialization.class).getExtension("fastjson2");
+        URL url = URL.valueOf("").setScopeModel(frameworkModel);
+
+        TrustedPojo pojo = new TrustedPojo(99.0);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        ObjectOutput objectOutput = serialization.serialize(url, outputStream);
+        objectOutput.writeObject(pojo);
+        objectOutput.flushBuffer();
+
+        byte[] bytes = outputStream.toByteArray();
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+        ObjectInput objectInput = serialization.deserialize(url, inputStream);
+
+        TrustedPojo result = objectInput.readObject(null);
+        Assertions.assertNotNull(result);
+
+        frameworkModel.destroy();
+    }
+
+    @Test
+    void testConcurrentDeserializationSameType() throws Exception {
+        FrameworkModel frameworkModel = new FrameworkModel();
+        Serialization serialization =
+                
frameworkModel.getExtensionLoader(Serialization.class).getExtension("fastjson2");
+        URL url = URL.valueOf("").setScopeModel(frameworkModel);
+
+        TrustedPojo pojo = new TrustedPojo(123.0);

Review Comment:
   The concurrency tests can become non-deterministic because fastjson2 
ObjectReader caches are global/static: if `TrustedPojo` (or `HashMap`) has 
already been deserialized earlier in the same JVM (including by other tests in 
this class/module), the cache-miss race this PR is addressing won’t be 
exercised. To make the regression test reliable, use a dedicated POJO type that 
is only deserialized in the concurrency test (or explicitly clear fastjson2’s 
ObjectReader caches before the barrier).



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

Reply via email to