jihoonson commented on a change in pull request #11828: URL: https://github.com/apache/druid/pull/11828#discussion_r758775838
########## File path: processing/src/main/java/org/apache/druid/query/context/ResponseContextDeserializer.java ########## @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.query.context; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import java.io.IOException; + +@SuppressWarnings("serial") +public class ResponseContextDeserializer extends StdDeserializer<ResponseContext> +{ + public ResponseContextDeserializer() + { + super(ResponseContext.class); + } + + @Override + public ResponseContext deserialize( + final JsonParser jp, + final DeserializationContext ctxt + ) throws IOException + { + if (jp.currentToken() != JsonToken.START_OBJECT) { + throw ctxt.wrongTokenException(jp, ResponseContext.class, JsonToken.START_OBJECT, null); + } + + // TODO(gianm): Check if we need concurrent response context here Review comment: I think it doesn't have to be the concurrent response context. The response context sent from historicals are deserialized and merged into the concurrent response context created by `QueryLifecycle` (https://github.com/apache/druid/blob/master/server/src/main/java/org/apache/druid/server/QueryLifecycle.java#L251). ########## File path: processing/src/main/java/org/apache/druid/query/context/ResponseContext.java ########## @@ -30,93 +33,334 @@ import org.apache.druid.java.util.common.IAE; import org.apache.druid.java.util.common.ISE; import org.apache.druid.java.util.common.NonnullPair; -import org.apache.druid.java.util.common.jackson.JacksonUtils; import org.apache.druid.query.SegmentDescriptor; +import org.apache.druid.utils.CollectionUtils; import org.joda.time.Interval; import javax.annotation.Nullable; + import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentSkipListMap; -import java.util.function.BiFunction; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Function; +import java.util.stream.Collectors; /** * The context for storing and passing data between chains of {@link org.apache.druid.query.QueryRunner}s. * The context is also transferred between Druid nodes with all the data it contains. - */ + * <p> + * The response context consists of a set of key/value pairs. Keys are those defined in + * the {@code 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. + * + * <h4>Structure</h4> + * The context has evolved to perform multiple tasks. First, it holds two kinds + * of information: + * <ul> + * <li>Information to be returned in the query response header. + * (These are values tagged as {@code HEADER}.)</li> + * <li>Values passed within a single server. These are tagged with + * visibility {@code NONE}.)</li> + * </ul> + * Second, it performs multiple tasks: + * <ul> + * <li>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.</li> + * <li>Gathers information for the query as a whole.</li> + * <li>Merges information back up the query tree: from multiple segments, + * from multiple servers, etc.</li> + * <li>Manages headers size by dropping fields when the header would get too + * large.</li> + * </ul> + * + * 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. + * + * <h4>API</h4> + * + * 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. + */ @PublicApi public abstract class ResponseContext { /** * The base interface of a response context key. * Should be implemented by every context key. */ - public interface BaseKey + public interface Key { + /** + * Hack, pure and simple. The symbol "ResponseContext.Key.ETAG" must exist + * to avoid changing a line of code where we have no tests, which causes + * the build to fail. Remove this once the proper tests are added. + * + * @see {@link org.apache.druid.server.QueryResource#doPost} + */ + public static final Key ETAG = Keys.ETAG; + @JsonValue String getName(); + + /** + * The phase (header, trailer, none) where this key is emitted. + */ + Visibility getPhase(); + /** - * Merge function associated with a key: Object (Object oldValue, Object newValue) + * Reads a value of this key from a JSON stream. Used by {@link ResponseContextDeserializer}. + */ + Object readValue(JsonParser jp); + + /** + * Merges two values of type T. + * + * This method may modify "oldValue" but must not modify "newValue". */ - BiFunction<Object, Object, Object> getMergeFunction(); + Object mergeValues(Object oldValue, Object newValue); + + /** + * Returns true if this key can be removed to reduce header size when the + * header would otherwise be too large. + */ + @JsonIgnore + boolean canDrop(); + } + + /** + * Where the key is emitted, if at all. Values in the context can be for internal + * use only: for return before the query results (header) or only after query + * results (trailer). + */ + public enum Visibility + { + /** + * Keys that are present in both the "X-Druid-Response-Context" header *and* the response context trailer. + */ + HEADER, Review comment: Thanks. I'm sill curious about what keys will be in both the header and the trailer :slightly_smiling_face: Will the next PR need to modify this enum, such as adding a new `Visibility` type? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
