Skip to content

Commit

Permalink
Skip serialization of jvm invocation in ark (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang committed Mar 8, 2019
1 parent d46d7b3 commit b1b0f0e
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 60 deletions.
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 in reference -->
<xsd:complexType name="BBindingJvmReference">
<xsd:attribute name="serialize" type="xsd:boolean" default="false" use="optional"/>
</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
3 changes: 2 additions & 1 deletion runtime-sofa-boot-plugin/pom.xml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,14 @@
* @return parameters of consumer
*/
SofaParameter[] parameters() default {};

/**
* serialization between biz, default is false.
* only serialize of reference and service is false
* 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 {};

/**
* serialization between biz, default is true.
* only serialize of reference and service is false
* 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,6 +157,16 @@ protected Object doInvoke(MethodInvocation invocation) throws Throwable {
.debug(">> Start in Cross App JVM service invoke, the service interface is - "
+ getInterfaceType());

if (!serialize) {
ClassLoader tcl = Thread.currentThread().getContextClassLoader();
try {
pushThreadContextClassLoader(getServiceClassLoader());
return targetMethod.invoke(targetService, targetArguments);
} finally {
pushThreadContextClassLoader(tcl);
}
}

if (TOSTRING_METHOD.equalsIgnoreCase(targetMethod.getName())
&& targetMethod.getParameterTypes().length == 0) {
return targetService.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ public class JvmBinding extends AbstractBinding {
/**
* binding type: JVM
*/
public static BindingType JVM_BINDING_TYPE = new BindingType("jvm");
public static BindingType JVM_BINDING_TYPE = new BindingType(XmlConstants.BINDING_TYPE);

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alipay.sofa.runtime.api.component.ComponentName;
import com.alipay.sofa.runtime.model.InterfaceMode;
import com.alipay.sofa.runtime.service.binding.JvmBinding;
import com.alipay.sofa.runtime.service.binding.JvmBindingParam;
import com.alipay.sofa.runtime.service.component.Reference;
import com.alipay.sofa.runtime.service.component.ReferenceComponent;
import com.alipay.sofa.runtime.service.component.impl.ReferenceImpl;
Expand Down Expand Up @@ -63,8 +64,10 @@ private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> reference
referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null);

if (bindingParam == null) {
// add JVM Binding Default
reference.addBinding(new JvmBinding());
// default add jvm binding and reference jvm binding should set serialize as false
JvmBindingParam jvmBindingParam = new JvmBindingParam();
jvmBindingParam.setSerialize(false);
reference.addBinding(new JvmBinding().setJvmBindingParam(jvmBindingParam));
} else {
BindingConverter bindingConverter = bindingConverterFactory
.getBindingConverter(bindingParam.getBindingType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static Object registerReference(Reference reference,
if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE)
&& !SofaRuntimeProperties.isDisableJvmFirst(sofaRuntimeContext)
&& reference.isJvmFirst()) {
// as rpc invocation would be serialized, so here would Not ignore serialized
reference.addBinding(new JvmBinding());
}

Expand Down
Loading

0 comments on commit b1b0f0e

Please sign in to comment.