Revert "optimize performance. decode in user thread (#11879)" (#11917)

This reverts commit 95865b0461.
This commit is contained in:
earthchen 2023-03-24 23:35:13 +08:00 committed by GitHub
parent 27f18c5c53
commit 7b9e65ed06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 132 deletions

View File

@ -1,27 +0,0 @@
/*
* 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 org.apache.dubbo.common.function;
public interface ThrowableSupplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get() throws Throwable;
}

View File

@ -37,8 +37,6 @@ public class ExecutorUtil {
new LinkedBlockingQueue<Runnable>(100),
new NamedThreadFactory("Close-ExecutorService-Timer", true));
private static final Executor DIRECT_EXECUTOR = Runnable::run;
public static boolean isTerminated(Executor executor) {
if (executor instanceof ExecutorService) {
if (((ExecutorService) executor).isTerminated()) {
@ -137,8 +135,4 @@ public class ExecutorUtil {
future.cancel(true);
}
}
public static Executor directExecutor() {
return DIRECT_EXECUTOR;
}
}

View File

@ -36,7 +36,6 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public class DeadlineFuture extends CompletableFuture<AppResponse> {
@ -81,7 +80,7 @@ public class DeadlineFuture extends CompletableFuture<AppResponse> {
return future;
}
public void received(TriRpcStatus status, Supplier<AppResponse> appResponse) {
public void received(TriRpcStatus status, AppResponse appResponse) {
if (status.code != TriRpcStatus.Code.DEADLINE_EXCEEDED) {
// decrease Time
if (!timeoutTask.isCancelled()) {
@ -89,13 +88,11 @@ public class DeadlineFuture extends CompletableFuture<AppResponse> {
}
}
if (getExecutor() != null) {
getExecutor().execute(() -> doReceived(status, appResponse.get()));
getExecutor().execute(() -> doReceived(status, appResponse));
} else {
doReceived(status, appResponse.get());
doReceived(status, appResponse);
}
}
private static final GlobalResourceInitializer<Timer> TIME_OUT_TIMER = new GlobalResourceInitializer<>(
} private static final GlobalResourceInitializer<Timer> TIME_OUT_TIMER = new GlobalResourceInitializer<>(
() -> new HashedWheelTimer(new NamedThreadFactory("dubbo-future-timeout", true), 30,
TimeUnit.MILLISECONDS), DeadlineFuture::destroy);

View File

@ -24,7 +24,6 @@ import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.common.utils.ExecutorUtil;
import org.apache.dubbo.remoting.api.connection.AbstractConnectionClient;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.AsyncRpcResult;
@ -133,8 +132,6 @@ public class TripleInvoker<T> extends AbstractInvoker<T> {
try {
switch (methodDescriptor.getRpcType()) {
case UNARY:
call = new TripleClientCall(connectionClient, ExecutorUtil.directExecutor(),
getUrl().getOrDefaultFrameworkModel(), writeQueue);
result = invokeUnary(methodDescriptor, invocation, call);
break;
case SERVER_STREAM:

View File

@ -43,9 +43,9 @@ public interface ClientCall {
/**
* Callback when message received.
*
* @param messageProducer message producer
* @param message message received
*/
void onMessage(MessageProducer messageProducer);
void onMessage(Object message);
/**
* Callback when call is finished.
@ -110,10 +110,4 @@ public interface ClientCall {
*/
void setCompression(String compression);
interface MessageProducer {
Object getMessage() throws Throwable;
}
}

View File

@ -38,14 +38,10 @@ public class ObserverToClientCallListenerAdapter implements ClientCall.Listener
}
@Override
public void onMessage(ClientCall.MessageProducer messageProducer) {
try {
delegate.onNext(messageProducer.getMessage());
if (call.isAutoRequest()) {
call.request(1);
}
} catch (Throwable e) {
delegate.onError(e);
public void onMessage(Object message) {
delegate.onNext(message);
if (call.isAutoRequest()) {
call.request(1);
}
}

View File

@ -77,23 +77,18 @@ public class TripleClientCall implements ClientCall, ClientStream.Listener {
return;
}
try {
TripleMessageProducer messageProducer = TripleMessageProducer.withSupplier(() ->
requestMetadata.packableMethod.parseResponse(message));
listener.onMessage(messageProducer);
final Object unpacked = requestMetadata.packableMethod.parseResponse(message);
listener.onMessage(unpacked);
} catch (Throwable t) {
onDeserializeError(t);
TriRpcStatus status = TriRpcStatus.INTERNAL.withDescription("Deserialize response failed")
.withCause(t);
cancelByLocal(status.asException());
listener.onClose(status,null);
LOGGER.error(PROTOCOL_FAILED_RESPONSE, "", "", String.format("Failed to deserialize triple response, service=%s, method=%s,connection=%s",
connectionClient, requestMetadata.service, requestMetadata.method.getMethodName()), t);
}
}
private void onDeserializeError(Throwable t){
TriRpcStatus status = TriRpcStatus.INTERNAL.withDescription("Deserialize response failed")
.withCause(t);
cancelByLocal(status.asException());
listener.onClose(status,null);
LOGGER.error(PROTOCOL_FAILED_RESPONSE, "", "", String.format("Failed to deserialize triple response, service=%s, method=%s,connection=%s",
connectionClient, requestMetadata.service, requestMetadata.method.getMethodName()), t);
}
@Override
public void onCancelByRemote(TriRpcStatus status) {
if (canceled) {

View File

@ -1,39 +0,0 @@
/*
* 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 org.apache.dubbo.rpc.protocol.tri.call;
import org.apache.dubbo.common.function.ThrowableSupplier;
class TripleMessageProducer implements ClientCall.MessageProducer {
private final ThrowableSupplier<Object> throwableSupplier;
private TripleMessageProducer(ThrowableSupplier<Object> throwableSupplier) {
this.throwableSupplier = throwableSupplier;
}
@Override
public Object getMessage() throws Throwable {
return throwableSupplier.get();
}
public static TripleMessageProducer withSupplier(ThrowableSupplier<Object> supplier) {
return new TripleMessageProducer(supplier);
}
}

View File

@ -26,38 +26,31 @@ import java.util.Map;
public class UnaryClientCallListener implements ClientCall.Listener {
private final DeadlineFuture future;
private ClientCall.MessageProducer messageProducer;
private Object appResponse;
public UnaryClientCallListener(DeadlineFuture deadlineFuture) {
this.future = deadlineFuture;
}
@Override
public void onMessage(ClientCall.MessageProducer messageProducer) {
this.messageProducer = messageProducer;
public void onMessage(Object message) {
this.appResponse = message;
}
@Override
public void onClose(TriRpcStatus status, Map<String, Object> trailers) {
future.received(status, () -> {
AppResponse result = new AppResponse();
result.setObjectAttachments(trailers);
if (status.isOk()) {
try {
Object appResponse = messageProducer.getMessage();
if (appResponse instanceof Exception) {
result.setException((Exception) appResponse);
} else {
result.setValue(appResponse);
}
} catch (Throwable e) {
result.setException(e);
}
AppResponse result = new AppResponse();
result.setObjectAttachments(trailers);
if (status.isOk()) {
if (appResponse instanceof Exception) {
result.setException((Exception) appResponse);
} else {
result.setException(status.asException());
result.setValue(appResponse);
}
return result;
});
} else {
result.setException(status.asException());
}
future.received(status, result);
}
@Override

View File

@ -45,7 +45,7 @@ class DeadlineFutureTest {
DeadlineFuture success = DeadlineFuture.newFuture(service, method, address, 1000,
ImmediateEventExecutor.INSTANCE);
AppResponse response = new AppResponse();
success.received(TriRpcStatus.OK, () -> response);
success.received(TriRpcStatus.OK, response);
AppResponse response1 = success.get();
Assertions.assertEquals(response, response1);
}