Fix ClassCastException caused by 2.x's filter in some condition (#12286)

* fix ClassCastException caused by 2.x's filter in some condition

* compatible of RpcResult in dubbo version 2.x

* extends AppResponse to make code better

Co-authored-by: Albumen Kevin <jhq0812@gmail.com>

* Add whenCompleteWithContext empty implement to compatible of FilterChainBuilder's invoke

* Add license

* Update compact

* Fix 2.6's Filter return RpcResult handle as AsyncRpcResult

When DubboProtocol.CompletableFuture running result.thenApply and  FilterChainBuilder.CallbackRegistrationInvoker running asyncResult.whenCompleteWithContext. All Result handle as AsyncRpcResult, so RpcResult add some methods implements seems like AsyncRpcResult does.

* revert last commit

---------

Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
This commit is contained in:
Xiang Wei Zhang 2023-05-17 14:25:09 +08:00 committed by GitHub
parent 2e1fcef156
commit 35a6365d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 42 deletions

View File

@ -17,6 +17,8 @@
package com.alibaba.dubbo.rpc;
import org.apache.dubbo.rpc.AsyncRpcResult;
@Deprecated
public interface Filter extends org.apache.dubbo.rpc.Filter {
@ -26,8 +28,18 @@ public interface Filter extends org.apache.dubbo.rpc.Filter {
default org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invoker<?> invoker,
org.apache.dubbo.rpc.Invocation invocation)
throws org.apache.dubbo.rpc.RpcException {
Result.CompatibleResult result = (Result.CompatibleResult) invoke(new Invoker.CompatibleInvoker<>(invoker),
new Invocation.CompatibleInvocation(invocation));
return result.getDelegate();
Result invokeResult = invoke(new Invoker.CompatibleInvoker<>(invoker),
new Invocation.CompatibleInvocation(invocation));
if (invokeResult instanceof Result.CompatibleResult) {
return invokeResult;
}
AsyncRpcResult asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(invocation);
asyncRpcResult.setValue(invokeResult.getValue());
asyncRpcResult.setException(invokeResult.getException());
asyncRpcResult.setObjectAttachments(invokeResult.getObjectAttachments());
return asyncRpcResult;
}
}

View File

@ -17,6 +17,8 @@
package com.alibaba.dubbo.rpc;
import org.apache.dubbo.rpc.AsyncRpcResult;
import com.alibaba.dubbo.common.URL;
@Deprecated
@ -54,9 +56,22 @@ public interface Invoker<T> extends org.apache.dubbo.rpc.Invoker<T> {
public org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invocation invocation) throws org.apache.dubbo.rpc.RpcException {
return new Result.CompatibleResult(invoker.invoke(invocation));
}
@Override
public Result invoke(Invocation invocation) throws RpcException {
if (invoker instanceof Invoker) {
Result result = ((Invoker) invoker).invoke(invocation);
if (result instanceof Result.CompatibleResult) {
return result;
} else {
AsyncRpcResult asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(invocation.getOriginal());
asyncRpcResult.setValue(result.getValue());
asyncRpcResult.setException(result.getException());
asyncRpcResult.setObjectAttachments(result.getObjectAttachments());
return new Result.CompatibleResult(asyncRpcResult);
}
}
return new Result.CompatibleResult(invoker.invoke(invocation.getOriginal()));
}

View File

@ -64,35 +64,7 @@ public interface Result extends org.apache.dubbo.rpc.Result {
return null;
}
abstract class AbstractResult implements Result {
@Override
public void setValue(Object value) {
}
@Override
public org.apache.dubbo.rpc.Result whenCompleteWithContext(BiConsumer<org.apache.dubbo.rpc.Result, Throwable> fn) {
return null;
}
@Override
public <U> CompletableFuture<U> thenApply(Function<org.apache.dubbo.rpc.Result, ? extends U> fn) {
return null;
}
@Override
public org.apache.dubbo.rpc.Result get() throws InterruptedException, ExecutionException {
return null;
}
@Override
public org.apache.dubbo.rpc.Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
}
class CompatibleResult extends AbstractResult {
class CompatibleResult implements Result {
private org.apache.dubbo.rpc.Result delegate;
public CompatibleResult(org.apache.dubbo.rpc.Result result) {
@ -177,5 +149,20 @@ public interface Result extends org.apache.dubbo.rpc.Result {
public void setObjectAttachment(String key, Object value) {
delegate.setObjectAttachment(key, value);
}
@Override
public <U> CompletableFuture<U> thenApply(Function<org.apache.dubbo.rpc.Result, ? extends U> fn) {
return delegate.thenApply(fn);
}
@Override
public org.apache.dubbo.rpc.Result get() throws InterruptedException, ExecutionException {
return delegate.get();
}
@Override
public org.apache.dubbo.rpc.Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return delegate.get(timeout, unit);
}
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.alibaba.dubbo.rpc;
import org.apache.dubbo.rpc.AppResponse;
@Deprecated
public class RpcResult extends AppResponse implements com.alibaba.dubbo.rpc.Result {
public RpcResult() {
}
public RpcResult(Object result) {
super(result);
}
public RpcResult(Throwable exception) {
super(exception);
}
}

View File

@ -18,11 +18,11 @@
package org.apache.dubbo.filter;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -46,15 +46,23 @@ class FilterTest {
}
@Test
void testDefault() {
void testDefault() throws Throwable {
Invoker<FilterTest> invoker = new LegacyInvoker<FilterTest>(null);
Invocation invocation = new LegacyInvocation("bbb");
Result res = myFilter.invoke(invoker, invocation);
System.out.println(res);
org.apache.dubbo.rpc.Invocation invocation = new RpcInvocation(null, "echo", "DemoService", "DemoService", new Class[]{String.class}, new Object[]{"bbb"});
org.apache.dubbo.rpc.Result res = myFilter.invoke(invoker, invocation);
Assertions.assertEquals("alibaba", res.recreate());
}
@Test
void testRecreate() throws Throwable {
Invoker<FilterTest> invoker = new LegacyInvoker<FilterTest>(null);
org.apache.dubbo.rpc.Invocation invocation = new RpcInvocation(null, "echo", "DemoService", "DemoService", new Class[]{String.class}, new Object[]{"cc"});
org.apache.dubbo.rpc.Result res = myFilter.invoke(invoker, invocation);
Assertions.assertEquals("123test", res.recreate());
}
@AfterAll
public static void tear() {
Assertions.assertEquals(2, MyFilter.count);
Assertions.assertEquals(3, MyFilter.count);
}
}

View File

@ -17,7 +17,6 @@
package org.apache.dubbo.filter;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.service.DemoService;
import com.alibaba.dubbo.common.URL;
@ -25,6 +24,7 @@ import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcResult;
public class LegacyInvoker<T> implements Invoker<T> {
@ -58,13 +58,13 @@ public class LegacyInvoker<T> implements Invoker<T> {
}
public Result invoke(Invocation invocation) throws RpcException {
AppResponse result = new AppResponse();
RpcResult result = new RpcResult();
if (!hasException) {
result.setValue("alibaba");
} else {
result.setException(new RuntimeException("mocked exception"));
}
return new Result.CompatibleResult(result);
return result;
}
@Override

View File

@ -22,6 +22,7 @@ import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcResult;
public class MyFilter implements Filter {
@ -36,6 +37,10 @@ public class MyFilter implements Filter {
throw new RpcException(new IllegalArgumentException("arg0 illegal"));
}
if (invocation.getArguments()[0].equals("cc")) {
return new RpcResult("123test");
}
Result tmp = invoker.invoke(invocation);
return tmp;
}