You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/12/08 07:06:07 UTC
[servicecomb-java-chassis] branch master updated: [SCB-2149] add
RestClientSenderFilter (#2108)
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new c048f55 [SCB-2149] add RestClientSenderFilter (#2108)
c048f55 is described below
commit c048f55f0472189956e4afdcfc89a130b9b811cf
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Dec 8 15:05:59 2020 +0800
[SCB-2149] add RestClientSenderFilter (#2108)
---
.../core/provider/consumer/InvokerUtils.java | 14 ++++++-
.../rest/client/RestClientFilterProvider.java | 35 ++++++++++++++++++
.../transport/rest/client/RestClientSender.java | 17 ++++++++-
.../rest/client/RestClientSenderFilter.java | 43 ++++++++++++++++++++++
4 files changed, 105 insertions(+), 4 deletions(-)
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
index 77277d9..a48690b 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
@@ -20,6 +20,8 @@ package org.apache.servicecomb.core.provider.consumer;
import java.lang.reflect.Type;
import java.util.Map;
+import javax.annotation.Nullable;
+
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.definition.InvocationRuntimeType;
@@ -35,6 +37,7 @@ import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import io.vertx.core.Context;
import io.vertx.core.Vertx;
public final class InvokerUtils {
@@ -85,7 +88,6 @@ public final class InvokerUtils {
return invocation;
}
-
/**
*
* use of this method , the response type can not be determined.
@@ -125,6 +127,14 @@ public final class InvokerUtils {
throw ExceptionFactory.convertConsumerException(response.getResult());
}
+ public static boolean isInEventLoop() {
+ return isInEventLoop(Vertx.currentContext());
+ }
+
+ public static boolean isInEventLoop(@Nullable Context context) {
+ return context != null && context.isEventLoopContext();
+ }
+
/**
* This is an internal API, caller make sure already invoked SCBEngine.ensureStatusUp
* @param invocation
@@ -132,7 +142,7 @@ public final class InvokerUtils {
*/
public static Response innerSyncInvoke(Invocation invocation) {
try {
- if (Vertx.currentContext() != null && Vertx.currentContext().isEventLoopContext()) {
+ if (isInEventLoop()) {
throw new IllegalStateException("Can not execute sync logic in event loop. ");
}
invocation.onStart(null, System.nanoTime());
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientFilterProvider.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientFilterProvider.java
new file mode 100644
index 0000000..d1e9afb
--- /dev/null
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientFilterProvider.java
@@ -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 org.apache.servicecomb.transport.rest.client;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.servicecomb.core.filter.Filter;
+import org.apache.servicecomb.core.filter.FilterProvider;
+import org.springframework.stereotype.Component;
+
+@Component
+public class RestClientFilterProvider implements FilterProvider {
+ @Override
+ public List<Class<? extends Filter>> getFilters() {
+ return Arrays.asList(
+ RestClientCodecFilter.class,
+ RestClientSenderFilter.class
+ );
+ }
+}
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSender.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSender.java
index c956f86..28e8e50 100644
--- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSender.java
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSender.java
@@ -73,7 +73,7 @@ public class RestClientSender {
// can read metrics of connection in vertx success/exception callback
// but after the callback, maybe the connection will be reused or closed, metrics is not valid any more
// so must attach callback before actual send
- CompletableFuture<Response> actualFuture = future.whenComplete(this::processConnectionMetrics);
+ CompletableFuture<Response> actualFuture = future.whenComplete(this::afterSend);
VertxContextExecutor.create(transportContext.getVertxContext()).execute(this::runInVertxContext);
return actualFuture;
}
@@ -159,7 +159,19 @@ public class RestClientSender {
.entity(result);
}
- protected void processConnectionMetrics(Response response, Throwable throwable) {
+ protected void afterSend(Response response, Throwable throwable) {
+ processMetrics();
+
+ if (throwable != null) {
+ LOGGER.error("rest client request, method={}, operation={}, endpoint={}, path={}.",
+ httpClientRequest.method(),
+ invocation.getMicroserviceQualifiedName(),
+ invocation.getEndpoint().getEndpoint(),
+ httpClientRequest.uri());
+ }
+ }
+
+ protected void processMetrics() {
InvocationStageTrace stageTrace = invocation.getInvocationStageTrace();
// connection maybe null when exception happens such as ssl handshake failure
@@ -173,5 +185,6 @@ public class RestClientSender {
// even failed and did not received response, still set time for it
// that will help to know the real timeout time
stageTrace.finishReceiveResponse();
+ stageTrace.startClientFiltersResponse();
}
}
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java
new file mode 100644
index 0000000..4289423
--- /dev/null
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestClientSenderFilter.java
@@ -0,0 +1,43 @@
+/*
+ * 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.servicecomb.transport.rest.client;
+
+import static org.apache.servicecomb.core.provider.consumer.InvokerUtils.isInEventLoop;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.filter.Filter;
+import org.apache.servicecomb.core.filter.FilterMeta;
+import org.apache.servicecomb.core.filter.FilterNode;
+import org.apache.servicecomb.foundation.common.utils.AsyncUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+
+@FilterMeta(name = "rest-client-sender", invocationType = CONSUMER)
+public class RestClientSenderFilter implements Filter {
+ @Override
+ public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode nextNode) {
+ CompletableFuture<Response> future = new RestClientSender(invocation)
+ .send();
+
+ if (invocation.isSync() && !isInEventLoop()) {
+ AsyncUtils.toSync(future);
+ }
+ return future;
+ }
+}