Class ResponseContext

  • Direct Known Subclasses:
    ConcurrentResponseContext, DefaultResponseContext

    public abstract class ResponseContext
    extends Object
    The context for storing and passing data between chains of QueryRunners. The context is also transferred between Druid nodes with all the data it contains.

    The response context consists of a set of key/value pairs. Keys are those defined in the Keys registry. Keys are indexed by key instance, not by name. The key defines the type of the associated value, including logic to merge values and to deserialize JSON values for that key.

    Structure

    The context has evolved to perform multiple tasks. First, it holds two kinds of information:
    • Information to be returned in the query response header. These are values tagged as being in the header.
    • Values passed within a single server. These are tagged with not being in the header.
    Second, it performs multiple tasks:
    • Registers the keys to be used in the header. But, since it also holds internal information, the internal information also needs keys, though the corresponding values are never serialized.
    • Gathers information for the query as a whole.
    • Merges information back up the query tree: from multiple segments, from multiple servers, etc.
    • Manages headers size by dropping fields when the header would get too large.

    A result is that the information the context, when inspected by a calling query, may be incomplete if some of it was previously dropped by the called query.

    API

    The query profile needs to obtain the full, untruncated information. To do this it piggy-backs on the set operations to obtain the full value. To ensure this is possible, code that works with standard values should call the set (or add) functions provided which will do the needed map update.

    • Constructor Detail

      • ResponseContext

        public ResponseContext()
    • Method Detail

      • createEmpty

        public static ResponseContext createEmpty()
        Create an empty DefaultResponseContext instance
        Returns:
        empty DefaultResponseContext instance
      • initialize

        public void initialize()
        Initialize fields for a query context. Not needed when merging.
      • initializeRemainingResponses

        public void initializeRemainingResponses()
      • initializeMissingSegments

        public void initializeMissingSegments()
      • initializeRowScanCount

        public void initializeRowScanCount()
      • putUncoveredIntervals

        public void putUncoveredIntervals​(List<org.joda.time.Interval> intervals,
                                          boolean overflowed)
      • putEntityTag

        public void putEntityTag​(String eTag)
      • putTimeoutTime

        public void putTimeoutTime​(long time)
      • putQueryFailDeadlineMs

        public void putQueryFailDeadlineMs​(long deadlineMs)
      • getUncoveredIntervals

        public List<org.joda.time.Interval> getUncoveredIntervals()
      • getEntityTag

        public String getEntityTag()
      • getTotalBytes

        public AtomicLong getTotalBytes()
      • getTimeoutTime

        public Long getTimeoutTime()
      • getRowScanCount

        public Long getRowScanCount()
      • getCpuNanos

        public Long getCpuNanos()
      • add

        public Object add​(ResponseContext.Key key,
                          Object value)
        Adds (merges) a new value associated with a key to an old value. See merge function of a context key for a specific implementation.
        Throws:
        IllegalStateException - if the key has not been registered.
      • addRemainingResponse

        public void addRemainingResponse​(String id,
                                         int count)
      • addRowScanCount

        public void addRowScanCount​(long count)
      • addCpuNanos

        public void addCpuNanos​(long ns)
      • merge

        public void merge​(ResponseContext responseContext)
        Merges a response context into the current.
        Throws:
        IllegalStateException - If a key of the responseContext has not been registered.
      • serializeWith

        public ResponseContext.SerializationResult serializeWith​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                                                 int maxCharsNumber)
                                                          throws com.fasterxml.jackson.core.JsonProcessingException
        Serializes the context given that the resulting string length is less than the provided limit. This method removes some elements from context collections if it's needed to satisfy the limit. There is no explicit priorities of keys which values are being truncated. Any kind of key can be removed, the key's @{code canDrop()} attribute indicates which can be dropped. (The unit tests use a string key.) Thus keys as equally prioritized and starts removing elements from the array which serialized value length is the biggest. The resulting string will be correctly deserialized to ResponseContext.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException