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

Skip serialization of jvm invocation in ark #370

Merged
merged 18 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion healthcheck-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion infra-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
<xsd:attribute name="registry" type="xsd:string" use="optional"/>
<xsd:attribute name="serialize-type" type="xsd:string" use="optional"/>
</xsd:complexType>
<!-- binding.jvm im reference -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im 是什么意思?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo , 已改。

<xsd:complexType name="BBindingJvmReference">
<xsd:attribute name="serialize" type="xsd:boolean" default="false" use="optional"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个为啥默认是false

Copy link
Contributor Author

@QilongZhang QilongZhang Mar 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference 默认不走序列化,service 默认走序列化. 只有两者都为 false 是,才会走直接调用模式。

服务端如果设置不走序列化,需要将相关类导出,而客户端无需变更代码。

只有当引用服务的应用配置了 deny-import 相关类,此时,客户端可能需要强制设置走序列化方式.

</xsd:complexType>
<!-- binding.bolt in reference -->
<xsd:complexType name="BBindingTRReference">
<xsd:complexContent>
Expand All @@ -141,6 +145,10 @@
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--jvm service-->
<xsd:complexType name="BBindingJvmService">
<xsd:attribute name="serialize" type="xsd:boolean" default="true" use="optional"/>
</xsd:complexType>
<!-- binding.bolt in service -->
<xsd:complexType name="BBindingTRService">
<xsd:complexContent>
Expand Down Expand Up @@ -567,7 +575,7 @@
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:all minOccurs="0" maxOccurs="1">
<xsd:element name="binding.jvm" minOccurs="0"/>
<xsd:element name="binding.jvm" type="BBindingJvmReference" minOccurs="0"/>
<xsd:element name="binding.bolt" type="BBindingTRReference" minOccurs="0"/>
<xsd:element name="binding.rest" type="RestBindingTRReference" minOccurs="0"/>
<xsd:element name="binding.dubbo" type="DubboBindingTRReference" minOccurs="0"/>
Expand All @@ -590,7 +598,7 @@
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:all minOccurs="0" maxOccurs="1">
<xsd:element name="binding.jvm" minOccurs="0"/>
<xsd:element name="binding.jvm" type="BBindingJvmService" minOccurs="0"/>
<xsd:element name="binding.bolt" type="BBindingTRService" minOccurs="0"/>
<xsd:element name="binding.rest" type="RestBindingTRService" minOccurs="0"/>
<xsd:element name="binding.dubbo" type="DubboBindingTRService" minOccurs="0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

/**
* SofaBootWebSpringBootApplication
Expand Down
2 changes: 1 addition & 1 deletion isle-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<packaging>pom</packaging>

<name>SOFABoot Runtime</name>
Expand Down
5 changes: 3 additions & 2 deletions runtime-sofa-boot-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -78,7 +78,6 @@
<package>com.alipay.sofa.runtime.spi.health</package>
<package>com.alipay.sofa.runtime.spi.log</package>
<package>com.alipay.sofa.runtime.spi.binding</package>
<package>com.alipay.sofa.runtime.spi.service</package>
<package>com.alipay.sofa.runtime.spi.util</package>
<package>org.aopalliance.aop</package>
<package>org.aopalliance.intercept</package>
Expand All @@ -88,6 +87,8 @@
<class>com.alipay.sofa.runtime.service.binding.JvmBinding</class>
<class>com.alipay.sofa.runtime.SofaFramework</class>
<class>com.alipay.sofa.runtime.SofaRuntimeProperties</class>
<class>com.alipay.sofa.runtime.service.binding.JvmBindingParam</class>
<class>com.alipay.sofa.runtime.spi.service.ServiceProxy</class>
</classes>
</exported>

Expand Down
2 changes: 1 addition & 1 deletion runtime-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-boot-runtime</artifactId>
<version>3.1.2</version>
<version>3.1.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,14 @@
* @return parameters of consumer
*/
SofaParameter[] parameters() default {};

/**
* skip serialization between biz, default is true.
* only serialize of reference and service is true
* then invocation between biz would skip serialization
* Note that the serialize of {@link SofaServiceBinding} is true
*
* @return
*/
boolean serialize() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@
* @return parameters of service
*/
SofaParameter[] parameters() default {};

/**
* skip serialization between biz, default is false.
* only serialize of reference and service is true
* then invocation between biz would skip serialization
*
* Note that the serialize of {@link SofaReferenceBinding} is false
*
* @return
*/
boolean serialize() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.ark.spi.model.BizState;
import com.alipay.sofa.ark.spi.service.ArkInject;
import com.alipay.sofa.ark.spi.service.biz.BizManagerService;
import com.alipay.sofa.runtime.service.binding.JvmBinding;
import com.alipay.sofa.runtime.service.component.ServiceComponent;
import com.alipay.sofa.runtime.SofaFramework;
import com.alipay.sofa.runtime.spi.binding.Contract;
Expand Down Expand Up @@ -70,9 +71,15 @@ public ServiceProxy findServiceProxy(ClassLoader clientClassloader, Contract con
ServiceComponent serviceComponent = findServiceComponent(uniqueId, interfaceType,
sofaRuntimeManager.getComponentManager());
if (serviceComponent != null) {
JvmBinding referenceJvmBinding = (JvmBinding) contract
.getBinding(JvmBinding.JVM_BINDING_TYPE);
JvmBinding serviceJvmBinding = (JvmBinding) serviceComponent.getService()
.getBinding(JvmBinding.JVM_BINDING_TYPE);
boolean serialize = referenceJvmBinding.getJvmBindingParam().isSerialize()
|| serviceJvmBinding.getJvmBindingParam().isSerialize();
return new DynamicJvmServiceInvoker(clientClassloader,
sofaRuntimeManager.getAppClassLoader(), serviceComponent.getService()
.getTarget(), contract, biz.getIdentity());
.getTarget(), contract, biz.getIdentity(), serialize);
}
}
}
Expand Down Expand Up @@ -123,19 +130,21 @@ static class DynamicJvmServiceInvoker extends ServiceProxy {
private Object targetService;
private String bizIdentity;
private ThreadLocal<ClassLoader> clientClassloader = new ThreadLocal<>();
private boolean serialize;

static protected final String TOSTRING_METHOD = "toString";
static protected final String EQUALS_METHOD = "equals";
static protected final String HASHCODE_METHOD = "hashCode";

public DynamicJvmServiceInvoker(ClassLoader clientClassloader,
ClassLoader serviceClassLoader, Object targetService,
Contract contract, String bizIdentity) {
Contract contract, String bizIdentity, boolean serialize) {
super(serviceClassLoader);
this.clientClassloader.set(clientClassloader);
this.targetService = targetService;
this.contract = contract;
this.bizIdentity = bizIdentity;
this.serialize = serialize;
}

@Override
Expand All @@ -148,8 +157,16 @@ protected Object doInvoke(MethodInvocation invocation) throws Throwable {
.debug(">> Start in Cross App JVM service invoke, the service interface is - "
+ getInterfaceType());

if (TOSTRING_METHOD.equalsIgnoreCase(targetMethod.getName())
&& targetMethod.getParameterTypes().length == 0) {
if (!serialize) {
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
try {
pushThreadContextClassLoader(getServiceClassLoader());
return targetMethod.invoke(targetService, targetArguments);
} finally {
pushThreadContextClassLoader(tcl);
}
} else if (TOSTRING_METHOD.equalsIgnoreCase(targetMethod.getName())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 else 可以去掉下,可读性更强点

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改。

&& targetMethod.getParameterTypes().length == 0) {
return targetService.toString();
} else if (EQUALS_METHOD.equalsIgnoreCase(targetMethod.getName())
&& targetMethod.getParameterTypes().length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ public class JvmBinding extends AbstractBinding {
*/
public static BindingType JVM_BINDING_TYPE = new BindingType("jvm");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 "jvm" 字符串也换成下面的 XmlConstants 常量吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已替换


private JvmBindingParam jvmBindingParam = new JvmBindingParam();

public JvmBinding() {
}

public JvmBindingParam getJvmBindingParam() {
return jvmBindingParam;
}

public JvmBinding setJvmBindingParam(JvmBindingParam jvmBindingParam) {
this.jvmBindingParam = jvmBindingParam;
return this;
}

/**
Expand Down Expand Up @@ -85,4 +95,10 @@ public HealthResult healthCheck() {
healthResult.setHealthy(isHealthy);
return healthResult;
}

public static class XmlConstants {
public static String SERIALIZE = "serialize";
public static String SUPPORT_TAG_NAME = "binding.jvm";
public static String BINDING_TYPE = "jvm";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.runtime.service.binding;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.alipay.sofa.runtime.api.binding.BindingType;
import com.alipay.sofa.runtime.spi.service.BindingConverter;
import com.alipay.sofa.runtime.spi.service.BindingConverterContext;
import org.w3c.dom.Element;

/**
* @author qilong.zql
* @since 3.1.3
*/
public class JvmBindingConverter implements BindingConverter<JvmBindingParam, JvmBinding> {
@Override
public JvmBinding convert(JvmBindingParam bindingParam,
BindingConverterContext bindingConverterContext) {
return new JvmBinding().setJvmBindingParam(bindingParam);
}

@Override
public JvmBinding convert(Element element, BindingConverterContext bindingConverterContext) {
JvmBindingParam jvmBindingParam = new JvmBindingParam();
if (element != null) {
jvmBindingParam.setSerialize(Boolean.TRUE.toString().equalsIgnoreCase(
element.getAttribute(JvmBinding.XmlConstants.SERIALIZE)));
}
return new JvmBinding().setJvmBindingParam(jvmBindingParam);
}

@Override
public JvmBinding convert(SofaService sofaServiceAnnotation,
SofaServiceBinding sofaServiceBindingAnnotation,
BindingConverterContext bindingConverterContext) {
if (JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaServiceBindingAnnotation.bindingType())) {
JvmBindingParam jvmBindingParam = new JvmBindingParam();
jvmBindingParam.setSerialize(sofaServiceBindingAnnotation.serialize());
return new JvmBinding().setJvmBindingParam(jvmBindingParam);
}
return null;
}

@Override
public JvmBinding convert(SofaReference sofaReferenceAnnotation,
SofaReferenceBinding sofaReferenceBindingAnnotation,
BindingConverterContext bindingConverterContext) {
if (JvmBinding.XmlConstants.BINDING_TYPE.equals(sofaReferenceBindingAnnotation
.bindingType())) {
JvmBindingParam jvmBindingParam = new JvmBindingParam();
jvmBindingParam.setSerialize(sofaReferenceBindingAnnotation.serialize());
return new JvmBinding().setJvmBindingParam(jvmBindingParam);
}
return null;
}

@Override
public BindingType supportBindingType() {
return JvmBinding.JVM_BINDING_TYPE;
}

@Override
public String supportTagName() {
return JvmBinding.XmlConstants.SUPPORT_TAG_NAME;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.runtime.service.binding;

import com.alipay.sofa.runtime.api.binding.BindingType;
import com.alipay.sofa.runtime.api.client.param.BindingParam;

/**
* @author qilong.zql
* @since 3.1.2
*/
public class JvmBindingParam implements BindingParam {

private boolean serialize = true;

@Override
public BindingType getBindingType() {
return JvmBinding.JVM_BINDING_TYPE;
}

/**
* whether ignore serialize when invoke across ClassLoader.
*
* @return
*/
public boolean isSerialize() {
return serialize;
}

/**
* Set whether ignore serialize when invoke across ClassLoader.
*
* @param serialize
*/
public JvmBindingParam setSerialize(boolean serialize) {
this.serialize = serialize;
return this;
}
}
Loading