Class RequestTracker


  • public final class RequestTracker
    extends java.lang.Object
    This class is a helper for multiple classes to get the client-generated id for a request (requestId) based on the available information of the request itself (requestDescriptor) in multiple methods. The objective is to cache such requestDescriptor and requestId pairs to allow classes within a component (Controller, Segment Store) to also log the client-generated ids, making it easier to trace the lifecycle of a request across multiple logs.
    • Constructor Detail

      • RequestTracker

        public RequestTracker​(boolean tracingEnabled)
    • Method Detail

      • buildRequestDescriptor

        public static java.lang.String buildRequestDescriptor​(java.lang.String... requestInfo)
        Creates a request descriptor or key to locate the client request id.
        Parameters:
        requestInfo - Fields to form the request descriptor.
        Returns:
        Request descriptor.
      • getRequestTagFor

        public RequestTag getRequestTagFor​(java.lang.String... requestInfo)
        Retrieves a RequestTag object formed by a request descriptor and request id by means of using method getRequestTagFor(String...). In addition, this method transforms a set of input string parameters into a single request descriptor.
        Parameters:
        requestInfo - Fields to form the request descriptor.
        Returns:
        Request descriptor and request id pair embedded in a RequestTag object.
      • getRequestTagFor

        public RequestTag getRequestTagFor​(java.lang.String requestDescriptor)
        Retrieves a RequestTag object formed by a request descriptor and request id. If the request descriptor does not exist or tracing is disabled, a new RequestTag object with a default request id is returned. In the case of concurrent requests with the same descriptor, multiple request ids will be associated to that request descriptor. The policy adopted is to retrieve the first one that was stored in the cache. Given that tracing is applied to idempotent operations, this allows us to consistently trace the operation that actually changes the state of the system. The rest of concurrent operations will be rejected and their response will be logged with a different requestId, as an indicator that another client request was ongoing. For more information, we refer to this PDP: https://github.com/pravega/pravega/wiki/PDP-31:-End-to-end-Request-Tags
        Parameters:
        requestDescriptor - Request descriptor as a single string.
        Returns:
        Request descriptor and request id pair embedded in a RequestTag object.
      • getRequestIdFor

        public long getRequestIdFor​(java.lang.String... requestInfo)
        Retrieves a request id associated to a request descriptor. This method overloads getRequestTagFor(String) by transforming a set of input string parameters into a single request descriptor.
        Parameters:
        requestInfo - Fields to form the request descriptor.
        Returns:
        Request descriptor and request id pair embedded in a RequestTag object.
      • getRequestIdFor

        public long getRequestIdFor​(java.lang.String requestDescriptor)
        Retrieves the requestId associated to a request descriptor by means of using getRequestTagFor(String...).
        Parameters:
        requestDescriptor - Request descriptor as a single string.
        Returns:
        Request id associated to the given descriptor.
      • trackRequest

        public void trackRequest​(RequestTag requestTag)
        Adds a request descriptor and request id pair in the cache based on an input RequestTag object by overloading {trackRequest(String, long)}.
        Parameters:
        requestTag - Request tag to be cached for further tracing.
      • trackRequest

        public void trackRequest​(java.lang.String requestDescriptor,
                                 long requestId)
        Adds a request descriptor and request id pair in the cache if tracing is enabled. In the case of tracking a request with an existing descriptor, this method adds the request id to the list associated to the descriptor.
        Parameters:
        requestDescriptor - Request descriptor as a single string.
        requestId - Request id associated to the given descriptor.
      • untrackRequest

        public long untrackRequest​(RequestTag requestTag)
        Remove a request id from an associated request descriptor within a RequestTag object by overloading {untrackRequest(String)}.
        Parameters:
        requestTag - Request tag to remove from cache.
        Returns:
        Request id removed from cache.
      • untrackRequest

        public long untrackRequest​(java.lang.String requestDescriptor)
        Removes and returns a request id from an associated request descriptor. If the request descriptor does not exist or tracing is disabled, a default request id is returned. In the case that there is only one request id, the whole entry is evicted from cache. If there are multiple request ids for a given descriptor, the last request id in the list is deleted from the cache.
        Parameters:
        requestDescriptor - Request tag to remove from cache.
        Returns:
        Request id removed from cache.
      • initializeAndTrackRequestTag

        public RequestTag initializeAndTrackRequestTag​(long requestId,
                                                       java.lang.String... requestInfo)
        This method first attempts to load a RequestTag from a request that is assumed to exist. However, if we work with clients or channels that do not attach tags to requests, then we initialize and track the request at the server side. In the worst case, we will have the ability of tracking a request from the server entry point onwards.
        Parameters:
        requestId - Alternative request id in the case there is no request id in headers.
        requestInfo - Alternative descriptor to identify the call in the case there is no descriptor in headers.
        Returns:
        Request tag formed either from request headers or from arguments given.
      • getNumDescriptors

        public long getNumDescriptors()
        Returns the number of descriptors in the cache.
        Returns:
        Number of request descriptors in the cache.
      • isTracingEnabled

        public boolean isTracingEnabled()