Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getDeserializerForCustomThrowable performance #1288

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public Deserializer getDeserializer(String type) throws HessianProtocolException
return super.getDeserializer(type);
}

// 自定义Throwable采用JavaDeserializer,反序列化成Throwable而不是GenericObject
Deserializer deserializer = getDeserializerForCustomThrowable(type);
// 查看是否已经包含反序列化器
Deserializer deserializer = DESERIALIZER_MAP.get(type);
if (deserializer != null) {
return deserializer;
}

// 查看是否已经包含反序列化器
deserializer = DESERIALIZER_MAP.get(type);
// 自定义Throwable采用JavaDeserializer,反序列化成Throwable而不是GenericObject
deserializer = getDeserializerForCustomThrowable(type);
if (deserializer != null) {
DESERIALIZER_MAP.putIfAbsent(type, deserializer);
return deserializer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public Deserializer getDeserializer(String type) throws HessianProtocolException
return super.getDeserializer(type);
}

// 自定义Throwable采用JavaDeserializer,反序列化成Throwable而不是GenericObject
Deserializer deserializer = getDeserializerForCustomThrowable(type);
// 查看是否已经包含反序列化器
Deserializer deserializer = DESERIALIZER_MAP.get(type);
if (deserializer != null) {
return deserializer;
}

// 查看是否已经包含反序列化器
deserializer = DESERIALIZER_MAP.get(type);
// 自定义Throwable采用JavaDeserializer,反序列化成Throwable而不是GenericObject
deserializer = getDeserializerForCustomThrowable(type);
if (deserializer != null) {
DESERIALIZER_MAP.putIfAbsent(type, deserializer);
return deserializer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.clearCacheDeserializerMap;
import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.setGenericThrowException;

/**
Expand Down Expand Up @@ -109,6 +110,7 @@ public void testCustomThrowableDeserializerEnabled() throws Exception {
Assert.assertEquals("MockError", ((MockError) result).getMessage());
} finally {
setGenericThrowException(false);
clearCacheDeserializerMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.clearCacheDeserializerMap;
import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.setGenericThrowException;

/**
Expand Down Expand Up @@ -116,6 +117,7 @@ public void testCustomThrowableDeserializerEnabled() throws Exception {
Assert.assertEquals("MockError", ((MockError) result).getMessage());
} finally {
setGenericThrowException(false);
clearCacheDeserializerMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
package com.alipay.sofa.rpc.codec.sofahessian.serialize;

import com.alipay.hessian.generic.model.GenericObject;
import com.alipay.sofa.rpc.codec.sofahessian.GenericMultipleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.GenericSingleClassLoaderSofaSerializerFactory;
import com.caucho.hessian.io.Deserializer;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.concurrent.ConcurrentHashMap;

import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminer.judgeCustomThrowableForGenericObject;

Expand Down Expand Up @@ -66,4 +70,28 @@ public static void setGenericThrowException(boolean enabled) {
e.printStackTrace();
}
}

public static void clearCacheDeserializerMap() {
try {
Field field = GenericMultipleClassLoaderSofaSerializerFactory.class.getDeclaredField("DESERIALIZER_MAP");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, new ConcurrentHashMap<String, Deserializer>());
} catch (Exception e) {
e.printStackTrace();
}

try {
Field field = GenericSingleClassLoaderSofaSerializerFactory.class.getDeclaredField("DESERIALIZER_MAP");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, new ConcurrentHashMap<String, Deserializer>());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashMap;
import java.util.Map;

import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.clearCacheDeserializerMap;
import static com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminerTest.setGenericThrowException;

/**
Expand Down Expand Up @@ -89,6 +90,7 @@ public void testCustomThrowableDeserializerEnabled() throws Exception {
Assert.assertEquals("MockError", ((MockError) sofaResponse2.getAppResponse()).getMessage());
} finally {
setGenericThrowException(false);
clearCacheDeserializerMap();
}
}

Expand Down Expand Up @@ -117,6 +119,7 @@ public void testCustomThrowableDeserializerEnabledForIncompatible() throws Excep
+ "found, error: "));
} finally {
setGenericThrowException(false);
clearCacheDeserializerMap();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* 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 com.alipay.sofa.rpc.test.generic;

import com.alipay.hessian.generic.model.GenericObject;
import com.alipay.sofa.rpc.api.GenericService;
import com.alipay.sofa.rpc.codec.sofahessian.GenericMultipleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.GenericSingleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.serialize.GenericCustomThrowableDeterminer;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.test.ActivelyDestroyTest;
import com.alipay.sofa.rpc.test.generic.bean.Job;
import com.alipay.sofa.rpc.test.generic.bean.People;
import com.caucho.hessian.io.Deserializer;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;

/**
* @author xuanbei
* @since 2016/07/28
*/
public class GenericBenchmarkTest extends ActivelyDestroyTest {
@Test
public void testAll() throws SofaRpcException, InterruptedException {
try {
// 发布服务
ServerConfig serverConfig2 = new ServerConfig()
.setPort(21234)
.setCoreThreads(500)
.setMaxThreads(500)
.setDaemon(false);

// C服务的服务端
ProviderConfig<TestInterface> CProvider = new ProviderConfig<TestInterface>()
.setInterfaceId(TestInterface.class.getName())
.setRef(new TestClass())
.setServer(serverConfig2);
CProvider.export();

// 引用服务
ConsumerConfig<GenericService> BConsumer = new ConsumerConfig<GenericService>()
.setInterfaceId(TestInterface.class.getName())
.setGeneric(true)
.setDirectUrl("bolt://127.0.0.1:21234")
.setTimeout(3000)
.setRetries(2);
GenericService proxy = BConsumer.refer();

setGenericThrowException(false);
clearCacheDeserializerMap();
benchmark(proxy, 10, 1000);

setGenericThrowException(true);
clearCacheDeserializerMap();
benchmark(proxy, 10, 1000);

setGenericThrowException(false);
clearCacheDeserializerMap();
long[] disabledData = benchmark(proxy, 10, 1000);

setGenericThrowException(true);
clearCacheDeserializerMap();
long[] enabledData = benchmark(proxy, 10, 1000);

System.out.println("disabledData===>" + Arrays.toString(disabledData));
System.out.println(" enabledData===>" + Arrays.toString(enabledData));
Assert.assertEquals(disabledData[0], enabledData[0]); //count
Assert.assertTrue(disabledData[1] - enabledData[1] <= 2); //P50
Assert.assertTrue(disabledData[2] - enabledData[2] <= 5); //P90
Assert.assertTrue(disabledData[3] - enabledData[3] <= 10); //P99
} finally {
setGenericThrowException(false);
clearCacheDeserializerMap();
}
}

private long[] benchmark(GenericService proxy, int threadNum, int countNum) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(threadNum);

List<Long> synchronizedList = Collections.synchronizedList(new ArrayList<>(threadNum * countNum / 2));

for (int i = 0; i < threadNum; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < countNum; i++) {
long start = System.currentTimeMillis();
GenericObject genericObject = new GenericObject(
"com.alipay.sofa.rpc.test.generic.bean.People");
genericObject.putField("name", "Lilei");
genericObject.putField("job", new Job("coder"));
People people = new People();
people.setName("Lilei");
people.setJob(new Job("coder"));

// sync 调用
assertEquals(proxy.$invoke("hello",
new String[] {"com.alipay.sofa.rpc.test.generic.bean.People"},
new Object[] {people}), new TestClass().hello(people));

People peopleResult = proxy.$genericInvoke("hello",
new String[] {"com.alipay.sofa.rpc.test.generic.bean.People"},
new Object[] {genericObject}, People.class);

assertEquals(peopleResult, new TestClass().hello(people));

GenericObject result = (GenericObject) proxy.$genericInvoke("hello",
new String[] {"com.alipay.sofa.rpc.test.generic.bean.People"},
new Object[] {genericObject});
isCorrect(result);

synchronizedList.add(System.currentTimeMillis() - start);
}
countDownLatch.countDown();
}
}).start();
}

countDownLatch.await();

Collections.sort(synchronizedList);
int size = synchronizedList.size();
return new long[] {synchronizedList.size(), synchronizedList.get(size / 2), synchronizedList.get(size * 9 / 10),
synchronizedList.get(size * 99 / 100)};
}

private void isCorrect(GenericObject result) {
Assert.assertEquals(result.getType(), "com.alipay.sofa.rpc.test.generic.bean.People");
Assert.assertEquals(result.getField("name"), "Lilei");
GenericObject genericObject = (GenericObject) result.getField("job");
Assert.assertEquals(genericObject.getType(), "com.alipay.sofa.rpc.test.generic.bean.Job");
Assert.assertEquals(genericObject.getField("name"), "coder");
}

public static void setGenericThrowException(boolean enabled) {
try {
Field field = GenericCustomThrowableDeterminer.class.getDeclaredField("GENERIC_THROW_EXCEPTION");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void clearCacheDeserializerMap() {
try {
Field field = GenericMultipleClassLoaderSofaSerializerFactory.class.getDeclaredField("DESERIALIZER_MAP");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, new ConcurrentHashMap<String, Deserializer>());
} catch (Exception e) {
e.printStackTrace();
}

try {
Field field = GenericSingleClassLoaderSofaSerializerFactory.class.getDeclaredField("DESERIALIZER_MAP");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, new ConcurrentHashMap<String, Deserializer>());
} catch (Exception e) {
e.printStackTrace();
}
}

}