Skip to content

Commit

Permalink
Add jvm filter comments and logic refine (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaneuler authored Nov 11, 2020
1 parent a26dd8e commit d909a10
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

@Override
public int getOrder() {
// Must be invoked after ConfigFileApplicationListener
return Ordered.HIGHEST_PRECEDENCE + 100;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
* Created on 2020/8/18
*/
public class JvmFilterContext {
// Jvm invocation result
// null when in <code>before</code> invoking except some filter sets it explicitly
private Object invokeResult;

// Jvm invocation AOP, could be used to fetch method name and args
private transient MethodInvocation methodInvocation;

// In normal SOFABoot application, this is always current application's SOFABoot runtime context
// When in Ark environment, this could be Ark Biz's SOFABoot runtime context
private transient SofaRuntimeContext sofaRuntimeContext;

// Thrown exception when do Jvm service invoking
// null when in <code>before</code> invoking except some filter sets it explicitly
private Throwable exception;

public JvmFilterContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static boolean beforeInvoking(JvmFilterContext context) {

public static boolean afterInvoking(JvmFilterContext context) {
sortJvmFilters();
for (JvmFilter filter : JVM_FILTERS) {
if (!filter.after(context)) {
for (int i = JVM_FILTERS.size() - 1; i >= 0; --i) {
if (!JVM_FILTERS.get(i).after(context)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public JvmServiceInvoker(Contract contract, JvmBinding binding,
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (!SofaRuntimeProperties.isJvmFilterEnable()) {
// Jvm filtering is not enabled
return super.invoke(invocation);
}

Expand All @@ -166,9 +167,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
.getDynamicJvmServiceProxyFinder().findServiceComponent(
sofaRuntimeContext.getAppClassLoader(), contract);
if (serviceComponent == null) {
// Jvm service is not found in normal or Ark environment
// We're actually invoking an RPC service, skip Jvm filtering
return super.invoke(invocation);
}

context.setSofaRuntimeContext(serviceComponent.getContext());
} else {
context.setSofaRuntimeContext(sofaRuntimeContext);
Expand All @@ -177,15 +179,19 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
long startTime = System.currentTimeMillis();
try {
Thread.currentThread().setContextClassLoader(serviceClassLoader);
// Do Jvm filter <code>before</code> invoking
// if some filter returns false, skip remaining filters and actual Jvm invoking
if (JvmFilterHolder.beforeInvoking(context)) {
rtn = doInvoke(invocation);
context.setInvokeResult(rtn);
}
} catch (Throwable e) {
// Exception occurs, set <code>e</code> in Jvm context
context.setException(e);
doCatch(invocation, e, startTime);
throw e;
} finally {
// Do Jvm Filter <code>after</code> invoking regardless of the fact whether exception happens or not
JvmFilterHolder.afterInvoking(context);
rtn = context.getInvokeResult();
doFinally(invocation, startTime);
Expand Down Expand Up @@ -296,5 +302,4 @@ protected String getUniqueId() {
return contract.getUniqueId();
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public void activate() throws ServiceRuntimeException {
if (JvmBinding.JVM_BINDING_TYPE.getType().equals(binding.getName())) {
candidate = binding;
} else {
// Under normal RPC reference (local-first/jvm-first is not set to false) binding,
// backup proxy is the RPC proxy, which will be invoked if Jvm service is not found
backupProxy = createProxy(reference, binding);
}
}
Expand Down Expand Up @@ -252,4 +254,4 @@ private Object getServiceTarget() {
}
return serviceTarget;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void before() {

@Test
public void test() {
Assert.assertEquals("egressFilter2", myService.say());
Assert.assertEquals("egressFilter1", myService.say());
Assert.assertEquals(5, JvmFilterHolder.getJvmFilters().size());
Assert.assertEquals(3, JvmFilterTestConfiguration.beforeCount);
Assert.assertEquals(1, JvmFilterTestConfiguration.afterCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public JvmFilter ingressFilter1() {
@Override
public boolean after(JvmFilterContext context) {
++afterCount;
return true;
context.setInvokeResult("egressFilter1");
return false;
}

@Override
Expand All @@ -122,8 +123,7 @@ public JvmFilter ingressFilter2() {
@Override
public boolean after(JvmFilterContext context) {
++afterCount;
context.setInvokeResult("egressFilter2");
return false;
return true;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion sofa-boot-project/sofaboot-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Add this to resolve to repository -->
<relativePath />
</parent>

<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<packaging>pom</packaging>
Expand Down

0 comments on commit d909a10

Please sign in to comment.