Packages

  • package root
    Definition Classes
    root
  • package com
    Definition Classes
    root
  • package twitter

    Start with com.twitter.finagle.

    Definition Classes
    com
  • package finagle

    Finagle is an extensible RPC system.

    Finagle is an extensible RPC system.

    Services are represented by class com.twitter.finagle.Service. Clients make use of com.twitter.finagle.Service objects while servers implement them.

    Finagle contains a number of protocol implementations; each of these implement Client and/or com.twitter.finagle.Server. For example, Finagle's HTTP implementation, com.twitter.finagle.Http (in package finagle-http), exposes both.

    Thus a simple HTTP server is built like this:

    import com.twitter.finagle.{Http, Service}
    import com.twitter.finagle.http.{Request, Response}
    import com.twitter.util.{Await, Future}
    
    val service = new Service[Request, Response] {
      def apply(req: Request): Future[Response] =
        Future.value(Response())
    }
    val server = Http.server.serve(":8080", service)
    Await.ready(server)

    We first define a service to which requests are dispatched. In this case, the service returns immediately with a HTTP 200 OK response, and with no content.

    This service is then served via the Http protocol on TCP port 8080. Finally we wait for the server to stop serving.

    We can now query our web server:

    % curl -D - localhost:8080
    HTTP/1.1 200 OK

    Building an HTTP client is also simple. (Note that type annotations are added for illustration.)

    import com.twitter.finagle.{Http, Service}
    import com.twitter.finagle.http.{Request, Response}
    import com.twitter.util.{Future, Return, Throw}
    
    val client: Service[Request, Response] = Http.client.newService("localhost:8080")
    val f: Future[Response] = client(Request()).respond {
      case Return(rep) =>
        printf("Got HTTP response %s\n", rep)
      case Throw(exc) =>
        printf("Got error %s\n", exc)
    }

    Http.client.newService("localhost:8080") constructs a new com.twitter.finagle.Service instance connected to localhost TCP port 8080. We then issue a HTTP/1.1 GET request to URI "/". The service returns a com.twitter.util.Future representing the result of the operation. We listen to this future, printing an appropriate message when the response arrives.

    The Finagle homepage contains useful documentation and resources for using Finagle.

    Definition Classes
    twitter
  • package thriftmux
    Definition Classes
    finagle
  • package service
    Definition Classes
    thriftmux
  • MethodBuilder

class MethodBuilder extends BaseMethodBuilder[MethodBuilder]

MethodBuilder is a collection of APIs for client configuration at a higher level than the Finagle 6 APIs while improving upon the deprecated ClientBuilder. MethodBuilder provides:

  • Logical success rate metrics.
  • Retries based on application-level requests and responses (e.g. a code in the Thrift response).
  • Configuration of per-attempt and total timeouts.

All of these can be customized per method (or endpoint) while sharing a single underlying Finagle client. Concretely, a single service might offer both getOneTweet as well as deleteTweets, whilst each having wildly different characteristics. The get is idempotent and has a tight latency distribution while the delete is not idempotent and has a wide latency distribution. If users want different configurations, without MethodBuilder they must create separate Finagle clients for each grouping. While long-lived clients in Finagle are not expensive, they are not free. They create duplicate metrics and waste heap, file descriptors, and CPU.

Example

Given an example IDL:

exception AnException {
  1: i32 errorCode
}

service SomeService {
  i32 TheMethod(
    1: i32 input
  ) throws (
    1: AnException ex1,
  )
}

This gives you a Service that has timeouts and retries on AnException when the errorCode is 0:

import com.twitter.conversions.DurationOps._
import com.twitter.finagle.ThriftMux
import com.twitter.finagle.service.{ReqRep, ResponseClass}
import com.twitter.util.Throw

val client: ThriftMux.Client = ???
val svc: Service[TheMethod.Args, TheMethod.SuccessType] =
  client.methodBuilder("inet!example.com:5555")
    .withTimeoutPerRequest(50.milliseconds)
    .withTimeoutTotal(100.milliseconds)
    .withRetryForClassifier {
      case ReqRep(_, Throw(AnException(errCode))) if errCode == 0 =>
        ResponseClass.RetryableFailure
    }
    .newServiceIface("the_method")
    .theMethod

Timeouts

Defaults to using the StackClient's configuration.

An example of setting a per-request timeout of 50 milliseconds and a total timeout of 100 milliseconds:

import com.twitter.conversions.DurationOps._
import com.twitter.finagle.thriftmux.MethodBuilder

val builder: MethodBuilder = ???
builder
  .withTimeoutPerRequest(50.milliseconds)
  .withTimeoutTotal(100.milliseconds)

Retries

Retries are intended to help clients improve success rate by trying failed requests additional times. Care must be taken by developers to only retry when it is known to be safe to issue the request multiple times. This is because the client cannot always be sure what the backend service has done. An example of a request that is safe to retry would be a read-only request.

Defaults to using the client's ResponseClassifier to retry failures marked as retryable. See withRetryForClassifier for details.

An example of configuring classifiers for ChannelClosed and Timeout exceptions:

import com.twitter.finagle.service.ResponseClassifier._
import com.twitter.finagle.thriftmux.MethodBuilder

val builder: MethodBuilder = ???
builder
  .withRetryForClassifier(RetryOnChannelClosed.orElse(RetryOnTimeout))

A com.twitter.finagle.service.RetryBudget is used to prevent retries from overwhelming the backend service. The budget is shared across clients created from an initial MethodBuilder. As such, even if the retry rules deem the request retryable, it may not be retried if there is insufficient budget.

Finagle will automatically retry failures that are known to be safe to retry via com.twitter.finagle.service.RequeueFilter. This includes WriteExceptions and retryable nacks. As these should have already been retried, we avoid retrying them again by ignoring them at this layer.

Additional information regarding retries can be found in the user guide.

The classifier is also used to determine the logical success metrics of the method. Logical here means after any retries are run. For example should a request result in retryable failure on the first attempt, but succeed upon retry, this is exposed through metrics as a success. Logical success rate metrics are scoped to "clnt/your_client_label/method_name/logical" and get "success" and "requests" counters along with a "request_latency_ms" stat.

Unsuccessful requests are logged at com.twitter.logging.Level.DEBUG level. Further details, including the request and response, are available at TRACE level.

See also

com.twitter.finagle.ThriftMux.Client.methodBuilder to construct instances.

The user guide.

Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. MethodBuilder
  2. BaseMethodBuilder
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new MethodBuilder(thriftMuxClient: ThriftMux.Client, mb: client.MethodBuilder[ThriftClientRequest, Array[Byte]])

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  9. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. def idempotent(maxExtraLoad: Tunable[Double], minSendBackupAfterMs: Int): MethodBuilder

    <invalid inheritdoc annotation>

    <invalid inheritdoc annotation>

    This additionally causes Thrift Exceptions to be retried.

  12. def idempotent(maxExtraLoad: Tunable[Double]): MethodBuilder

    Configure that requests are to be treated as idempotent.

    Configure that requests are to be treated as idempotent. Because requests can be safely retried, BackupRequestFilter is configured with the params maxExtraLoad to decrease tail latency by sending an additional fraction of requests.

    This additionally causes Thrift Exceptions to be retried.

    maxExtraLoad

    How much extra load, as a Tunable[Double], we are willing to send to the server. Must be between 0.0 and 1.0. Backup requests can be disabled by setting this to 0.0.

    Definition Classes
    MethodBuilderBaseMethodBuilder
  13. def idempotent(maxExtraLoad: Double, minSendBackupAfterMs: Int): MethodBuilder

    <invalid inheritdoc annotation>

    <invalid inheritdoc annotation>

    This additionally causes Thrift Exceptions to be retried.

  14. def idempotent(maxExtraLoad: Double): MethodBuilder

    Configure that requests are to be treated as idempotent.

    Configure that requests are to be treated as idempotent. Because requests can be safely retried, BackupRequestFilter is configured with the params maxExtraLoad to decrease tail latency by sending an additional fraction of requests.

    If you are using TwitterServer, a good starting point for determining a value for maxExtraLoad is looking at the details of the PDF histogram for request latency, at /admin/histograms. If you choose a maxExtraLoad of 1.percent, for example, you can expect your p999/p9999 latencies to (roughly) now be that of your p99 latency. For 5.percent, those latencies would shift to your p95 latency. You should also ensure that your backend can tolerate the increased load.

    This additionally causes Thrift Exceptions to be retried.

    maxExtraLoad

    How much extra load, as a fraction, we are willing to send to the server. Must be between 0.0 and 1.0. Backup requests can be disabled by setting this to 0.0.

    Definition Classes
    MethodBuilderBaseMethodBuilder
  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. def label: String

    Configured client label.

    Configured client label. The label is used to assign a label to the underlying Thrift client. The label is used to display stats, etc.

    See also

    com.twitter.finagle.Client

    https://twitter.github.io/finagle/guide/Clients.html#clients

  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. def newService: Service[ThriftClientRequest, Array[Byte]]

    Create a Service from the current configuration.

    Create a Service from the current configuration.

    Note

    It's very likely that you wanted/needed to use {{servicePerEndpoint}} instead.

  19. def newService(methodName: String): Service[ThriftClientRequest, Array[Byte]]

    Create a Service from the current configuration.

    Create a Service from the current configuration.

    Note

    It's very likely that you wanted/needed to use {{servicePerEndpoint}} instead.

  20. def nonIdempotent: MethodBuilder

    Configure that requests are to be treated as non-idempotent.

    Configure that requests are to be treated as non-idempotent. BackupRequestFilter is disabled, and only those failures that are known to be safe to retry (i.e., write failures, where the request was never sent) are retried via requeue filter; any previously configured retries are removed.

    Definition Classes
    MethodBuilderBaseMethodBuilder
    See also

    The MethodBuilder section in the user guide for further details.

  21. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  22. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  23. def servicePerEndpoint[ServicePerEndpoint <: Filterable[ServicePerEndpoint]](implicit builder: ServicePerEndpointBuilder[ServicePerEndpoint]): ServicePerEndpoint

    Construct a ServicePerEndpoint to be used for the client.

  24. def servicePerEndpoint[ServicePerEndpoint <: Filterable[ServicePerEndpoint]](methodName: String)(implicit builder: ServicePerEndpointBuilder[ServicePerEndpoint]): ServicePerEndpoint

    Construct a ServicePerEndpoint to be used for the methodName function.

    Construct a ServicePerEndpoint to be used for the methodName function.

    methodName

    used for scoping metrics (e.g. "clnt/your_client_label/method_name").

  25. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  26. def toString(): String
    Definition Classes
    MethodBuilder → AnyRef → Any
  27. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  28. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  29. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  30. def withMaxRetries(value: Int): MethodBuilder

    Maximum retry attempts for a request based on the configured ResponseClassifier.

    Maximum retry attempts for a request based on the configured ResponseClassifier.

    value

    when a Failed with retryable is true is returned for a given ReqRep, the request will be retried up to value times or the com.twitter.finagle.service.RetryBudget has been exhausted (whichever occurs first).

    Definition Classes
    MethodBuilderBaseMethodBuilder
    See also

    withRetryDisabled

    response classification user guide

    The MethodBuilder section in the user guide for further details.

  31. def withPartitioningStrategy(strategy: PartitioningStrategy): MethodBuilder

    Set a PartitioningStrategy for a MethodBuilder endpoint to enable partitioning awareness.

    Set a PartitioningStrategy for a MethodBuilder endpoint to enable partitioning awareness. See PartitioningStrategy.

    Default is com.twitter.finagle.thrift.exp.partitioning.Disabled

    Example:
    1. To set a hashing strategy to MethodBuilder:

      import com.twitter.finagle.ThriftMux.Client
      import com.twitter.finagle.thrift.exp.partitioning.MethodBuilderHashingStrategy
      
      val hashingStrategy = new MethodBuilderHashingStrategy[RequestType, ResponseType](...)
      
      val client: ThriftMux.Client = ???
      val builder = client.methodBuilder($address)
      
      builder
        .withPartitioningStrategy(hashingStrategy)
        .servicePerEndpoint...
      ...
  32. def withRetryDisabled: MethodBuilder

    Disables "application" level retries.

    Disables "application" level retries.

    This does not disable retries of failures that are known to be safe to retry via com.twitter.finagle.service.RequeueFilter.

    This causes the logical success metrics to be based on the default response classifier rules of a Return response is a success, while everything else is not.

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. import com.twitter.finagle.http.Http
      
      val client: Http.Client = ???
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withRetryDisabled
    See also

    withRetryForClassifier

    The MethodBuilder section in the user guide for further details.

  33. def withRetryForClassifier(classifier: ResponseClassifier): MethodBuilder

    Retry based on ResponseClassifier.

    Retry based on ResponseClassifier.

    The default behavior is to use the client's classifier which is typically configured through theClient.withResponseClassifier or ClientBuilder.withResponseClassifier.

    This classifier is used to determine which requests are unsuccessful. This is the basis for measuring the logical success metrics as well as logging unsuccessful requests at debug level.

    classifier

    when a Failed with retryable is true is returned for a given ReqRep, the request will be retried. This is often a ResponseClass.RetryableFailure.

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. For example, retrying on a 418 status code:

      import com.twitter.conversions.DurationOps._
      import com.twitter.finagle.http.Http
      import com.twitter.finagle.service.{ReqRep, ResponseClass}
      import com.twitter.util.Return
      
      val client: Http.Client = ???
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withRetryForClassifier {
        case ReqRep(_, Return(rep)) if rep.statusCode == 418 => ResponseClass.RetryableFailure
      }

      The classifier is also used to determine the logical success metrics of the client.

    See also

    withRetryDisabled

    response classification user guide

    The MethodBuilder section in the user guide for further details.

  34. def withTimeoutPerRequest(howLong: Tunable[Duration]): MethodBuilder

    How long a single request is given to complete.

    How long a single request is given to complete.

    If there are retries, each attempt is given up to this amount of time.

    If a request does not complete within this time, the response will be satisfied with a com.twitter.finagle.IndividualRequestTimeoutException.

    Defaults to using the client's configuration for com.twitter.finagle.service.TimeoutFilter.Param(timeout), which is typically set via com.twitter.finagle.param.CommonParams.withRequestTimeout.

    howLong

    how long, from the initial request issuance, an individual attempt given to complete. If it is not finite (e.g. Duration.Top), no method specific timeout will be applied.

    returns

    a new instance with all other settings copied

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. For example, a per-request timeout of 50 milliseconds:

      import com.twitter.conversions.DurationOps._
      import com.twitter.finagle.http.Http
      import com.twitter.util.Duration
      import com.twitter.util.tunable.Tunable
      
      val client: Http.Client = ???
      val tunableTimeout: Tunable[Duration] = Tunable.const("id", 50.milliseconds)
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withTimeoutPerRequest(tunableTimeout))
    See also

    withTimeoutTotal(Tunable[Duration])

    The MethodBuilder section in the user guide for further details.

  35. def withTimeoutPerRequest(howLong: Duration): MethodBuilder

    How long a single request is given to complete.

    How long a single request is given to complete.

    If there are retries, each attempt is given up to this amount of time.

    If a request does not complete within this time, the response will be satisfied with a com.twitter.finagle.IndividualRequestTimeoutException.

    Defaults to using the client's configuration for com.twitter.finagle.service.TimeoutFilter.Param(timeout), which is typically set via com.twitter.finagle.param.CommonParams.withRequestTimeout.

    howLong

    how long, from the initial request issuance, an individual attempt given to complete. If it is not finite (e.g. Duration.Top), no method specific timeout will be applied.

    returns

    a new instance with all other settings copied

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. For example, a per-request timeout of 50 milliseconds:

      import com.twitter.conversions.DurationOps._
      import com.twitter.finagle.http.Http
      import com.twitter.util.Duration
      
      val client: Http.Client = ???
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withTimeoutPerRequest(50.milliseconds))
    See also

    withTimeoutTotal(Duration)

    The MethodBuilder section in the user guide for further details.

  36. def withTimeoutTotal(howLong: Tunable[Duration]): MethodBuilder

    Set a total timeout with a Tunable, including time spent on retries.

    Set a total timeout with a Tunable, including time spent on retries.

    If the request does not complete in this time, the response will be satisfied with a com.twitter.finagle.GlobalRequestTimeoutException.

    Defaults to using the client's configuration for com.twitter.finagle.service.TimeoutFilter.TotalTimeout(timeout).

    howLong

    how long, from the initial request issuance, is the request given to complete. If it is not finite (e.g. Duration.Top), no method specific timeout will be applied.

    returns

    a new instance with all other settings copied

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. For example, a total timeout of 200 milliseconds:

      import com.twitter.conversions.DurationOps._
      import com.twitter.finagle.http.Http
      import com.twitter.util.Duration
      import com.twitter.util.tunable.Tunable
      
      val client: Http.Client = ???
      val tunableTimeout: Tunable[Duration] = Tunable.const("id", 200.milliseconds)
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withTimeoutTotal(tunableTimeout))
    See also

    withTimeoutPerRequest(Tunable[Duration])

    The MethodBuilder section in the user guide for further details.

  37. def withTimeoutTotal(howLong: Duration): MethodBuilder

    Set a total timeout, including time spent on retries.

    Set a total timeout, including time spent on retries.

    If the request does not complete in this time, the response will be satisfied with a com.twitter.finagle.GlobalRequestTimeoutException.

    Defaults to using the client's configuration for com.twitter.finagle.service.TimeoutFilter.TotalTimeout(timeout).

    howLong

    how long, from the initial request issuance, is the request given to complete. If it is not finite (e.g. Duration.Top), no method specific timeout will be applied.

    returns

    a new instance with all other settings copied

    Definition Classes
    MethodBuilderBaseMethodBuilder
    Example:
    1. For example, a total timeout of 200 milliseconds:

      import com.twitter.conversions.DurationOps._
      import com.twitter.finagle.http.Http
      import com.twitter.util.Duration
      
      val client: Http.Client = ???
      val builder = client.methodBuilder("inet!example.com:80")
      builder.withTimeoutTotal(200.milliseconds))
    See also

    withTimeoutPerRequest(Duration)

    The MethodBuilder section in the user guide for further details.

Deprecated Value Members

  1. def newServiceIface[ServiceIface <: Filterable[ServiceIface]](methodName: String)(implicit builder: ServiceIfaceBuilder[ServiceIface]): ServiceIface

    Construct a ServiceIface to be used for the methodName function.

    Construct a ServiceIface to be used for the methodName function.

    methodName

    used for scoping metrics (e.g. "clnt/your_client_label/method_name").

    Annotations
    @deprecated
    Deprecated

    (Since version 2017-11-29) Use servicePerEndpoint

Inherited from AnyRef

Inherited from Any

Ungrouped