-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[CORE][REST]: Add context aware response parsing #13191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bb7465f
a21f4ec
ed49f17
09626c2
f665708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,13 @@ | |
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectReader; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Consumer; | ||
| import org.apache.hc.client5.http.auth.AuthScope; | ||
|
|
@@ -101,6 +103,7 @@ public class HTTPClient extends BaseHTTPClient { | |
| private final ObjectMapper mapper; | ||
| private final AuthSession authSession; | ||
| private final boolean isRootClient; | ||
| private final ConcurrentMap<Class<?>, ObjectReader> objectReaderCache = Maps.newConcurrentMap(); | ||
|
|
||
|
Comment on lines
+106
to
107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Until initializing the object reader(s) is known to be a bottleneck, I think I'd prefer to not introduce this cache. I haven't gone so deep into the jackson implementation but I'd be kind of surprised if it wasn't already doing this under the hood
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, i was on the same boat as i expected Jackson to cache the reader by Type, turns out it doesn't, what jackson does cache is :
So i proactively went and added a cache for the overall reader so that i can just work with its copy with new injectable, i expect the Cache to be not put memory pressure as we have very limited parsers. |
||
| private HTTPClient( | ||
| URI baseUri, | ||
|
|
@@ -303,6 +306,17 @@ protected <T extends RESTResponse> T execute( | |
| Class<T> responseType, | ||
| Consumer<ErrorResponse> errorHandler, | ||
| Consumer<Map<String, String>> responseHeaders) { | ||
| return execute( | ||
| req, responseType, errorHandler, responseHeaders, ParserContext.builder().build()); | ||
| } | ||
|
|
||
| @Override | ||
| protected <T extends RESTResponse> T execute( | ||
| HTTPRequest req, | ||
| Class<T> responseType, | ||
| Consumer<ErrorResponse> errorHandler, | ||
| Consumer<Map<String, String>> responseHeaders, | ||
| ParserContext parserContext) { | ||
| HttpUriRequestBase request = new HttpUriRequestBase(req.method().name(), req.requestUri()); | ||
|
|
||
| req.headers().entries().forEach(e -> request.addHeader(e.name(), e.value())); | ||
|
|
@@ -341,7 +355,11 @@ protected <T extends RESTResponse> T execute( | |
| } | ||
|
|
||
| try { | ||
| return mapper.readValue(responseBody, responseType); | ||
| ObjectReader reader = objectReaderCache.computeIfAbsent(responseType, mapper::readerFor); | ||
| if (parserContext != null && !parserContext.isEmpty()) { | ||
| reader = reader.with(parserContext.toInjectableValues()); | ||
| } | ||
| return reader.readValue(responseBody); | ||
| } catch (JsonProcessingException e) { | ||
| throw new RESTException( | ||
| e, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.iceberg.rest; | ||
|
|
||
| import com.fasterxml.jackson.databind.InjectableValues; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import org.apache.hadoop.util.Preconditions; | ||
|
|
||
| class ParserContext { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we wrapping InjectableValues? Is there something else we want to add to the interface?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this is to keep Jackson out of the public interface?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah trying to keep Jackson out of the interface #13191 (comment) |
||
|
|
||
| private final Map<String, Object> data; | ||
|
|
||
| private ParserContext(Builder builder) { | ||
| this.data = Collections.unmodifiableMap(builder.data); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return data.isEmpty(); | ||
| } | ||
|
|
||
| public InjectableValues toInjectableValues() { | ||
| return new InjectableValues.Std(data); | ||
| } | ||
|
|
||
| static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| static class Builder { | ||
| private Map<String, Object> data; | ||
|
|
||
| private Builder() { | ||
| this.data = Collections.emptyMap(); | ||
| } | ||
|
|
||
| public Builder add(String key, Object value) { | ||
singhpk234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Preconditions.checkNotNull(key, "Key cannot be null"); | ||
| Preconditions.checkNotNull(value, "Value cannot be null"); | ||
| this.data.put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| public ParserContext build() { | ||
| return new ParserContext(this); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The Justification here is that no one can depend on this api correct? It's solely owned by the client and no one else can extend or use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
precisely, I am not aware if there are folks who use this API, i just added here to follow existing pattern and make rev-api happy !