You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/11/28 02:34:30 UTC

[GitHub] [servicecomb-java-chassis] xhanthow opened a new issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

xhanthow opened a new issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
wujimin commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735520077


   是的,新的Filter机制是将旧的各种扩展点全部归一化了,要实现的功能还是一样的  
   Filter的成员确实允许Autowire了  
   要切回eventloop,可以:  
   ```java
     VertxTransportContext transportContext = invocation.getTransportContext();
     Executor executor = VertxContextExecutor.create(transportContext.getVertxContext());
     future.xxxAsync(xxx, executor)
       .xxxxxx
   ```
   xxx就在eventloop中执行了  
   
   要注意,future有个“特点”,下面用例中第二个apply不一定会在executor中执行,结果是由本线程与executor的真实执行顺序决定的,而这个顺序本身就是不确定的:
   ```java
       @Test
       @Disabled("用于证明jdk的CompletableFuture确实有问题,有较高概率出现,但是也不是必现;为防止CI概率性失败,改为手工执行")
       void completable_future_thread_is_not_stable() {
           for (int idx = 0; idx < LOOP_COUNT; idx++) {
               CompletableFuture.completedFuture(null)
                   .thenApplyAsync(this::apply, executor)
                   .thenApply(this::apply)
                   .join();
           }
   
           assertThat(threadNames).containsExactlyInAnyOrder("main", UT_THREAD_NAME);
       }
   
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735060803


   也就是说这样就不会影响eventloop线程了吧。
   新的Filter机制有设计文档么(是否和Handler设计类似了?),想对着https://issues.apache.org/jira/browse/SCB-1929学习学习。


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] liubao68 commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
liubao68 commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735045387


   看你的描述像是代码本身执行逻辑导致的问题, 即 completableFuture.complete在哪个线程被调用了。 
   
   "Filter中不行" 没看到描述, 不是很清楚这个表达代表的意思。 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow closed issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow closed issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735525199


   这个类`VertxContextExecutor`在1.2.0中是没有的吧,我看最新代码中是有的。
   学习了~
   刚又debug了下,由于我的`Handler`是第一个,所以剩下的几个(除了最后一个`Handler`:`TransportClientHandler`)都是在我自己定义线程池中执行的。直到最后一个`Handler`执行这个动作就会发生线程切换吧:
   ![线程切换](https://user-images.githubusercontent.com/7291995/100566048-f695e500-32ff-11eb-8d9f-77dc17b00151.png)
   这个动作和`VertxContextExecutor`这个类逻辑差不多


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow edited a comment on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow edited a comment on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735513368


   嗯,把代码下下来看了下,这里就启动了`Filter`链(感觉和之前的`Handler`有点像,`invocation.next(xxx)`)
   ```java
     public CompletableFuture<Response> onFilter(Invocation invocation) {
       if (!filter.enabled()) {
         return nextNode.onFilter(invocation);
       }
       
       return AsyncUtils.tryCatch(() -> filter.onFilter(invocation, nextNode))
           .thenApply(this::rethrowExceptionInResponse);
     }
   ```
   
   而且现在`Filter`可以使用`Autowire`了吧 :)
   ```java
   engine.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(filter)
   ```
   
   不过我现在还有个疑问,就是我在`Handler`中用了异步(如issue中第一个描述中的代码),自己定义的线程池,然后我`debug`的时候发现后续的`Invocation#next`执行的时候也都在我自定义的线程池中执行,后续的操作还有可能切换到eventloop线程中执行么?
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
wujimin commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735051226


   基本上我认为这是一个设计缺陷  
   当初定义HttpServerFilter时,仅仅为了用于干预一下codec逻辑而已  
   相关的缺陷,在这里也有一定的描述:https://issues.apache.org/jira/browse/SCB-1929  
     
   使用新的Filter机制,可以解决reactive认证/鉴权的问题  
   不过Filter机制还没有正式发布,我在自己的项目里正在试用  
     
   线程变换的问题,从能不能跑的角度看,应该是没问题,但是能不能跑得好,这个需要你来评估  
   比如,所有请求都要走认证/鉴权,所有的请求就都跑到你这个线程池里去了,是否顶得住?  
   
   另外:CompletableFuture使用指定线程池执行,可以使用它的带async后缀的方法,并指定你想要的executor即可,代码会清晰不少


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735049419


   completableFuture.complete在是在我自己定义的线程池中执行的,我就问这个会影响效率么?因为开始都是在eventloop中执行的,然后切换到了自己的线程中。
   "Filter"中不行和这个issue描述差不多:https://github.com/apache/servicecomb-java-chassis/issues/1324
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735566821


   好的,了解了。谢谢~


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow edited a comment on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow edited a comment on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735513368


   嗯,把代码下下来看了下,这里就启动了`Filter`链(感觉和之前的`Handler`有点像,`invocation.next(xxx)`)
   ```java
     public CompletableFuture<Response> onFilter(Invocation invocation) {
       if (!filter.enabled()) {
         return nextNode.onFilter(invocation);
       }
       
       return AsyncUtils.tryCatch(() -> filter.onFilter(invocation, nextNode))
           .thenApply(this::rethrowExceptionInResponse);
     }
   ```
   
   而且现在`Filter`可以使用`Autowire`了吧 :)
   ```java
   engine.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(filter)
   ```
   
   不过我现在还有个疑问,就是我在`Handler`中用了异步(如issue中第一个描述中的代码),自己定义的线程池,然后我`debug`的时候发现后续的`Invocation#next`执行的时候也都在我自定义的线程池中执行,后续的操作还有可能切换到eventloop线程中执行么?
   
   ![Snipaste_2020-10-22_19-47-12](https://user-images.githubusercontent.com/7291995/100563289-052cce00-32f9-11eb-910f-fa6be92cef29.png)
   
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
wujimin commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735159814


   是的,不会影响eventloop  
   还没有文档,只有那个issue,相应的代码,可以看那issue的subTask对应的PR,点进去都有链接


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow edited a comment on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow edited a comment on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735049419


   completableFuture.complete在是在我自己定义的线程池中执行的,我就想问下这个会影响效率么?因为开始都是在eventloop中执行的,然后切换到了自己的线程中。
   "Filter"中不行和这个issue描述差不多:https://github.com/apache/servicecomb-java-chassis/issues/1324
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735513368


   嗯,把代码下下来看了下,这里就启动了`Filter`链(感觉和之前的`Handler`有点像,`invocation.next(xxx)`)
   ```java
     public CompletableFuture<Response> onFilter(Invocation invocation) {
       if (!filter.enabled()) {
         return nextNode.onFilter(invocation);
       }
       
       return AsyncUtils.tryCatch(() -> filter.onFilter(invocation, nextNode))
           .thenApply(this::rethrowExceptionInResponse);
     }
   ```
   
   而且现在`Filter`可以使用`Autowire`了吧 :)
   ```java
   engine.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(filter)
   ```
   
   不过我现在还有个疑问,就是我在`Handler`中用了异步(如issue中第一个描述中的代码),自己定义的线程池,然后我`debug`的时候发现后续的`Invocation#next`执行的时候也都在我自定义的线程池中执行,后续的操作还有可能切换到eventloop线程中执行么?
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
wujimin commented on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735526561


   1.x系列早就是维护状态了,新特性都在2.x中,Filter估计在3.x中发布  
   `VertxContextExecutor`实际就是对runOnContext的包装,为了方便与CompletableFuture配合使用


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow edited a comment on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow edited a comment on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735513368


   嗯,把代码下下来看了下,这里就启动了`Filter`链(感觉和之前的`Handler`有点像,`invocation.next(xxx)`)
   ```java
     public CompletableFuture<Response> onFilter(Invocation invocation) {
       if (!filter.enabled()) {
         return nextNode.onFilter(invocation);
       }
       
       return AsyncUtils.tryCatch(() -> filter.onFilter(invocation, nextNode))
           .thenApply(this::rethrowExceptionInResponse);
     }
   ```
   
   而且现在`Filter`可以使用`Autowire`了吧 :)
   ```java
   engine.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(filter)
   ```
   
   不过我现在还有个疑问,就是我在`Handler`中用了异步(如issue中第一个描述中的代码),自己定义的线程池,然后我`debug`的时候发现后续的`Invocation#next`执行的时候也都在我自定义的线程池中执行,后续的操作还有可能切换到eventloop线程中执行么?
   
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] xhanthow edited a comment on issue #2091: 为什么Handler中可以使用异步而Filter中不行了?

Posted by GitBox <gi...@apache.org>.
xhanthow edited a comment on issue #2091:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2091#issuecomment-735513368


   嗯,把代码下下来看了下,这里就启动了`Filter`链(感觉和之前的`Handler`有点像,`invocation.next(xxx)`)
   ```java
     public CompletableFuture<Response> onFilter(Invocation invocation) {
       if (!filter.enabled()) {
         return nextNode.onFilter(invocation);
       }
       
       return AsyncUtils.tryCatch(() -> filter.onFilter(invocation, nextNode))
           .thenApply(this::rethrowExceptionInResponse);
     }
   ```
   
   而且现在`Filter`可以使用`Autowire`了吧 :)
   ```java
   engine.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(filter)
   ```
   
   不过我现在还有个疑问,就是我在`Handler`中用了异步(如issue中第一个描述中的代码),自己定义的线程池,然后我`debug`的时候发现后续的`Invocation#next`执行的时候也都在我自定义的线程池中执行,后续的操作还有可能切换到eventloop线程中执行么?
   
   ![thread](https://user-images.githubusercontent.com/7291995/100563414-4d4bf080-32f9-11eb-815d-4ee81b8f661b.png)
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org