Sncp.remote优化

This commit is contained in:
redkale
2023-12-25 12:05:28 +08:00
parent 0578fa3ca5
commit 08f679c422
5 changed files with 229 additions and 201 deletions

View File

@@ -403,10 +403,12 @@ public abstract class NodeServer {
continue; //本地模式或Component不能实例化接口和抽象类的Service类 continue; //本地模式或Component不能实例化接口和抽象类的Service类
} }
final ResourceTypeLoader resourceLoader = (ResourceFactory rf, String srcResourceName, final Object srcObj, final String resourceName, Field field, final Object attachment) -> { final ResourceTypeLoader resourceLoader = (ResourceFactory rf, String srcResourceName,
final Object srcObj, final String resourceName, Field field, final Object attachment) -> {
try { try {
if (Sncp.loadMethodActions(Sncp.getResourceType(serviceImplClass)).isEmpty() if (Sncp.loadRemoteMethodActions(Sncp.getResourceType(serviceImplClass)).isEmpty()
&& (serviceImplClass.getAnnotation(Priority.class) == null && serviceImplClass.getAnnotation(javax.annotation.Priority.class) == null)) { //class没有可用的方法且没有标记启动优先级的 通常为BaseService && (serviceImplClass.getAnnotation(Priority.class) == null
&& serviceImplClass.getAnnotation(javax.annotation.Priority.class) == null)) { //class没有可用的方法且没有标记启动优先级的 通常为BaseService
if (!serviceImplClass.getName().startsWith("org.redkale.") && !serviceImplClass.getSimpleName().contains("Base")) { if (!serviceImplClass.getName().startsWith("org.redkale.") && !serviceImplClass.getSimpleName().contains("Base")) {
logger.log(Level.FINE, serviceImplClass + " cannot load because not found less one public non-final method"); logger.log(Level.FINE, serviceImplClass + " cannot load because not found less one public non-final method");
} }

View File

@@ -75,7 +75,7 @@ public abstract class Sncp {
} }
//key: actionid //key: actionid
public static LinkedHashMap<Uint128, Method> loadMethodActions(final Class serviceTypeOrImplClass) { public static LinkedHashMap<Uint128, Method> loadRemoteMethodActions(final Class serviceTypeOrImplClass) {
final List<Method> list = new ArrayList<>(); final List<Method> list = new ArrayList<>();
final List<Method> multis = new ArrayList<>(); final List<Method> multis = new ArrayList<>();
final Map<Uint128, Method> actionids = new LinkedHashMap<>(); final Map<Uint128, Method> actionids = new LinkedHashMap<>();
@@ -552,8 +552,40 @@ public abstract class Sncp {
mv.visitEnd(); mv.visitEnd();
} }
if (methodBoost != null) { if (methodBoost != null) {
Class clazz = serviceImplClass; createNewMethods(serviceImplClass, methodBoost, new HashSet<>(), cw, newDynName, supDynName);
Set<String> methodKeys = new HashSet<>(); methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
}
cw.visitEnd();
byte[] bytes = cw.toByteArray();
Class<?> newClazz = new ClassLoader(loader) {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicClasses(newDynName.replace('/', '.'));
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
c = newClazz.getDeclaredField(FIELDPREFIX + "_mq");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
} catch (Exception e) {
//do nothing
}
return (Class<T>) newClazz;
}
public static <T extends Service> T createSimpleLocalService(Class<T> serviceImplClass, ResourceFactory resourceFactory) {
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
}
private static void createNewMethods(Class clazz, final AsmMethodBoost methodBoost, Set<String> methodKeys, ClassWriter cw, String newDynName, String supDynName) {
if (methodBoost == null) {
return;
}
MethodDebugVisitor mv = null;
do { do {
Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(clazz); Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(clazz);
for (final Method method : clazz.getDeclaredMethods()) { for (final Method method : clazz.getDeclaredMethods()) {
@@ -643,32 +675,6 @@ public abstract class Sncp {
} }
} }
} while ((clazz = clazz.getSuperclass()) != Object.class); } while ((clazz = clazz.getSuperclass()) != Object.class);
methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
}
cw.visitEnd();
byte[] bytes = cw.toByteArray();
Class<?> newClazz = new ClassLoader(loader) {
public final Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
}.loadClass(newDynName.replace('/', '.'), bytes);
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
RedkaleClassLoader.putReflectionPublicClasses(newDynName.replace('/', '.'));
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_conf");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
c = newClazz.getDeclaredField(FIELDPREFIX + "_mq");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
} catch (Exception e) {
//do nothing
}
return (Class<T>) newClazz;
}
public static <T extends Service> T createSimpleLocalService(Class<T> serviceImplClass, ResourceFactory resourceFactory) {
return createLocalService(null, "", serviceImplClass, null, resourceFactory, null, null, null, null, null);
} }
/** /**
@@ -757,10 +763,10 @@ public abstract class Sncp {
public static <T extends Service> T createSimpleRemoteService(Class<T> serviceImplClass, public static <T extends Service> T createSimpleRemoteService(Class<T> serviceImplClass,
ResourceFactory resourceFactory, SncpRpcGroups sncpRpcGroups, SncpClient client, String group) { ResourceFactory resourceFactory, SncpRpcGroups sncpRpcGroups, SncpClient client, String group) {
if (sncpRpcGroups == null) { if (sncpRpcGroups == null) {
throw new SncpException("SncpRpcGroups is null"); throw new SncpException(SncpRpcGroups.class.getSimpleName() + " is null");
} }
if (client == null) { if (client == null) {
throw new SncpException("SncpClient is null"); throw new SncpException(SncpClient.class.getSimpleName() + " is null");
} }
return createRemoteService(null, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null); return createRemoteService(null, "", serviceImplClass, null, resourceFactory, sncpRpcGroups, client, null, group, null);
} }
@@ -974,11 +980,29 @@ public abstract class Sncp {
// mv.visitMaxs(1, 1); // mv.visitMaxs(1, 1);
// mv.visitEnd(); // mv.visitEnd();
// } // }
Set<String> methodKeys = new HashSet<>();
Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(serviceTypeOrImplClass); Map<String, AsmMethodBean> methodBeans = AsmMethodBoost.getMethodBeans(serviceTypeOrImplClass);
for (final SncpRemoteAction entry : info.getActions()) { for (final SncpRemoteAction entry : info.getActions()) {
final java.lang.reflect.Method method = entry.method; final java.lang.reflect.Method method = entry.method;
{ String mk = Utility.methodKey(method);
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null)); if (methodKeys.contains(mk)) {
//跳过已处理的继承方法
continue;
}
methodKeys.add(mk);
int acc = ACC_PUBLIC;
String newMethodName = null;
if (methodBoost != null) {
List<Class<? extends Annotation>> filterAnns = methodBoost.filterMethodAnnotations(method);
newMethodName = methodBoost.doMethod(cw, newDynName, FIELDPREFIX, filterAnns, method, null);
}
if (newMethodName != null) {
acc = ACC_PRIVATE;
} else {
newMethodName = method.getName();
}
mv = new MethodDebugVisitor(cw.visitMethod(acc, newMethodName, Type.getMethodDescriptor(method), null, null));
Label l0 = new Label(); Label l0 = new Label();
mv.visitLabel(l0); mv.visitLabel(l0);
//mv.setDebug(true); //mv.setDebug(true);
@@ -1072,6 +1096,9 @@ public abstract class Sncp {
mv.visitMaxs(20, 20); mv.visitMaxs(20, 20);
mv.visitEnd(); mv.visitEnd();
} }
if (methodBoost != null) {
createNewMethods(serviceTypeOrImplClass, methodBoost, methodKeys, cw, newDynName, supDynName);
methodBoost.doAfterMethods(cw, newDynName, FIELDPREFIX);
} }
cw.visitEnd(); cw.visitEnd();
byte[] bytes = cw.toByteArray(); byte[] bytes = cw.toByteArray();

View File

@@ -3,9 +3,6 @@
*/ */
package org.redkale.net.sncp; package org.redkale.net.sncp;
import org.redkale.mq.spi.MessageRecord;
import org.redkale.mq.spi.MessageClient;
import org.redkale.mq.spi.MessageAgent;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.net.*; import java.net.*;
@@ -16,8 +13,10 @@ import java.util.concurrent.*;
import java.util.logging.*; import java.util.logging.*;
import org.redkale.convert.*; import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
import org.redkale.mq.*; import org.redkale.mq.spi.MessageAgent;
import static org.redkale.net.sncp.Sncp.loadMethodActions; import org.redkale.mq.spi.MessageClient;
import org.redkale.mq.spi.MessageRecord;
import static org.redkale.net.sncp.Sncp.loadRemoteMethodActions;
import static org.redkale.net.sncp.SncpHeader.HEADER_SUBSIZE; import static org.redkale.net.sncp.SncpHeader.HEADER_SUBSIZE;
import org.redkale.service.*; import org.redkale.service.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -90,7 +89,7 @@ public class SncpRemoteInfo<S extends Service> {
this.messageClient = messageAgent == null ? null : messageAgent.getSncpMessageClient(); this.messageClient = messageAgent == null ? null : messageAgent.getSncpMessageClient();
this.topic = messageAgent == null ? null : Sncp.generateSncpReqTopic(resourceName, resourceType, messageAgent.getNodeid()); this.topic = messageAgent == null ? null : Sncp.generateSncpReqTopic(resourceName, resourceType, messageAgent.getNodeid());
for (Map.Entry<Uint128, Method> en : loadMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) { for (Map.Entry<Uint128, Method> en : loadRemoteMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) {
this.actions.put(en.getKey().toString(), new SncpRemoteAction(serviceImplClass, resourceType, en.getValue(), serviceid, en.getKey(), sncpClient)); this.actions.put(en.getKey().toString(), new SncpRemoteAction(serviceImplClass, resourceType, en.getValue(), serviceid, en.getKey(), sncpClient));
} }
} }

View File

@@ -60,7 +60,7 @@ public class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse>
this.serviceid = Sncp.serviceid(resourceName, resourceType); this.serviceid = Sncp.serviceid(resourceName, resourceType);
Class serviceImplClass = Sncp.getServiceType(service); Class serviceImplClass = Sncp.getServiceType(service);
for (Map.Entry<Uint128, Method> en : Sncp.loadMethodActions(serviceImplClass).entrySet()) { for (Map.Entry<Uint128, Method> en : Sncp.loadRemoteMethodActions(serviceImplClass).entrySet()) {
SncpActionServlet action; SncpActionServlet action;
try { try {
action = SncpActionServlet.create(resourceName, resourceType, serviceImplClass, service, serviceid, en.getKey(), en.getValue()); action = SncpActionServlet.create(resourceName, resourceType, serviceImplClass, service, serviceid, en.getKey(), en.getValue());

View File

@@ -100,7 +100,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) { for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
@@ -109,7 +109,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) { for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
@@ -118,7 +118,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");
for (Method method : Sncp.loadMethodActions(service.getClass()).values()) { for (Method method : Sncp.loadRemoteMethodActions(service.getClass()).values()) {
System.out.println(method); System.out.println(method);
} }
System.out.println("-----------------------------------"); System.out.println("-----------------------------------");