Package org.apache.druid.query.context
Class ResponseContext
- java.lang.Object
-
- org.apache.druid.query.context.ResponseContext
-
- Direct Known Subclasses:
ConcurrentResponseContext
,DefaultResponseContext
public abstract class ResponseContext extends Object
The context for storing and passing data between chains ofQueryRunner
s. 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.
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ResponseContext.AbstractKey
Abstract key class which provides most functionality except the type-specific merge logic.static class
ResponseContext.BooleanKey
Boolean valued attribute with the semantics that once the flag is set true, it stays true.static class
ResponseContext.CounterKey
Long valued attribute that holds the accumulation of values assigned.static interface
ResponseContext.Key
The base interface of a response context key.static class
ResponseContext.Keys
Global registry of response context keys.static class
ResponseContext.LongKey
Long valued attribute that holds the latest value assigned.static class
ResponseContext.SerializationResult
Serialization result ofResponseContext
.static class
ResponseContext.StringKey
String valued attribute that holds the latest value assigned.
-
Constructor Summary
Constructors Constructor Description ResponseContext()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Object
add(ResponseContext.Key key, Object value)
Adds (merges) a new value associated with a key to an old value.void
addCpuNanos(long ns)
void
addMissingSegments(List<SegmentDescriptor> descriptors)
void
addRemainingResponse(String id, int count)
void
addRowScanCount(long count)
static ResponseContext
createEmpty()
Create an empty DefaultResponseContext instancestatic ResponseContext
deserialize(String responseContext, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Deserializes a string intoResponseContext
using givenObjectMapper
.Object
get(ResponseContext.Key key)
Long
getCpuNanos()
protected abstract Map<ResponseContext.Key,Object>
getDelegate()
String
getEntityTag()
List<SegmentDescriptor>
getMissingSegments()
ConcurrentHashMap<String,Integer>
getRemainingResponses()
Long
getRowScanCount()
Long
getTimeoutTime()
AtomicLong
getTotalBytes()
List<org.joda.time.Interval>
getUncoveredIntervals()
void
initialize()
Initialize fields for a query context.void
initializeMissingSegments()
void
initializeRemainingResponses()
void
initializeRowScanCount()
void
merge(ResponseContext responseContext)
Merges a response context into the current.Object
put(ResponseContext.Key key, Object value)
Associates the specified object with the specified extension key.void
putEntityTag(String eTag)
void
putQueryFailDeadlineMs(long deadlineMs)
void
putTimeoutTime(long time)
void
putUncoveredIntervals(List<org.joda.time.Interval> intervals, boolean overflowed)
Object
remove(ResponseContext.Key key)
ResponseContext.SerializationResult
serializeWith(com.fasterxml.jackson.databind.ObjectMapper objectMapper, int maxCharsNumber)
Serializes the context given that the resulting string length is less than the provided limit.Map<String,Object>
toMap()
-
-
-
Method Detail
-
getDelegate
protected abstract Map<ResponseContext.Key,Object> getDelegate()
-
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()
-
deserialize
public static ResponseContext deserialize(String responseContext, com.fasterxml.jackson.databind.ObjectMapper objectMapper) throws IOException
Deserializes a string intoResponseContext
using givenObjectMapper
.- Throws:
IllegalStateException
- if one of the deserialized map keys has not been registered.IOException
-
put
public Object put(ResponseContext.Key key, Object value)
Associates the specified object with the specified extension key.- Throws:
IllegalStateException
- if the key has not been registered.
-
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)
-
get
public Object get(ResponseContext.Key key)
-
getRemainingResponses
public ConcurrentHashMap<String,Integer> getRemainingResponses()
-
getUncoveredIntervals
public List<org.joda.time.Interval> getUncoveredIntervals()
-
getMissingSegments
public List<SegmentDescriptor> getMissingSegments()
-
getEntityTag
public String getEntityTag()
-
getTotalBytes
public AtomicLong getTotalBytes()
-
getTimeoutTime
public Long getTimeoutTime()
-
getRowScanCount
public Long getRowScanCount()
-
getCpuNanos
public Long getCpuNanos()
-
remove
public Object remove(ResponseContext.Key key)
-
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)
-
addMissingSegments
public void addMissingSegments(List<SegmentDescriptor> descriptors)
-
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 theresponseContext
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 toResponseContext
.- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
-