-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: Extended header support for RESTClient implementations #12194
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
669b44e
8759cc9
7b61af9
43c8786
457eebc
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * 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 java.nio.charset.StandardCharsets; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.hash.HashFunction; | ||
| import org.apache.iceberg.relocated.com.google.common.hash.Hashing; | ||
|
|
||
| class ETagProvider { | ||
| private static final HashFunction MURMUR3 = Hashing.murmur3_32_fixed(); | ||
|
|
||
| private ETagProvider() {} | ||
|
|
||
| public static String of(String metadataLocation) { | ||
| Preconditions.checkArgument(null != metadataLocation, "Invalid metadata location: null"); | ||
| Preconditions.checkArgument(!metadataLocation.isEmpty(), "Invalid metadata location: empty"); | ||
|
|
||
| return MURMUR3.hashString(metadataLocation, StandardCharsets.UTF_8).toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.iceberg.BaseTable; | ||
| import org.apache.iceberg.BaseTransaction; | ||
| import org.apache.iceberg.Table; | ||
|
|
@@ -287,7 +288,12 @@ public RESTClient withAuthSession(AuthSession session) { | |
|
|
||
| @SuppressWarnings({"MethodLength", "checkstyle:CyclomaticComplexity"}) | ||
| public <T extends RESTResponse> T handleRequest( | ||
| Route route, Map<String, String> vars, Object body, Class<T> responseType) { | ||
| Route route, | ||
| Map<String, String> vars, | ||
| HTTPRequest httpRequest, | ||
| Class<T> responseType, | ||
| Consumer<Map<String, String>> responseHeaders) { | ||
| Object body = httpRequest.body(); | ||
| switch (route) { | ||
| case TOKENS: | ||
| return castResponse(responseType, handleOAuthRequest(body)); | ||
|
|
@@ -388,12 +394,15 @@ public <T extends RESTResponse> T handleRequest( | |
| Namespace namespace = namespaceFromPathVars(vars); | ||
| CreateTableRequest request = castRequest(CreateTableRequest.class, body); | ||
| request.validate(); | ||
|
|
||
| if (request.stageCreate()) { | ||
| return castResponse( | ||
| responseType, CatalogHandlers.stageTableCreate(catalog, namespace, request)); | ||
|
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. I believe we also need to handle the etag here + a test
Collaborator
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 wasn't sure here. Isn't the metadata location null for stage create? I created a simple test and it was null there, so I don't think we can add an ETag here. Do I miss something?
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. you're right, I was only checking all places that return |
||
| } else { | ||
| return castResponse( | ||
| responseType, CatalogHandlers.createTable(catalog, namespace, request)); | ||
| LoadTableResponse resp = CatalogHandlers.createTable(catalog, namespace, request); | ||
| responseHeaders.accept( | ||
| ImmutableMap.of(HttpHeaders.ETAG, ETagProvider.of(resp.metadataLocation()))); | ||
| return castResponse(responseType, resp); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -416,23 +425,40 @@ public <T extends RESTResponse> T handleRequest( | |
|
|
||
| case LOAD_TABLE: | ||
| { | ||
| TableIdentifier ident = tableIdentFromPathVars(vars); | ||
| return castResponse(responseType, CatalogHandlers.loadTable(catalog, ident)); | ||
| LoadTableResponse resp = CatalogHandlers.loadTable(catalog, tableIdentFromPathVars(vars)); | ||
|
|
||
| responseHeaders.accept( | ||
| ImmutableMap.of(HttpHeaders.ETAG, ETagProvider.of(resp.metadataLocation()))); | ||
|
|
||
| return castResponse(responseType, resp); | ||
| } | ||
|
|
||
| case REGISTER_TABLE: | ||
| { | ||
| Namespace namespace = namespaceFromPathVars(vars); | ||
| RegisterTableRequest request = castRequest(RegisterTableRequest.class, body); | ||
| return castResponse( | ||
| responseType, CatalogHandlers.registerTable(catalog, namespace, request)); | ||
| LoadTableResponse resp = | ||
| CatalogHandlers.registerTable( | ||
| catalog, | ||
| namespaceFromPathVars(vars), | ||
| castRequest(RegisterTableRequest.class, body)); | ||
|
|
||
| responseHeaders.accept( | ||
| ImmutableMap.of(HttpHeaders.ETAG, ETagProvider.of(resp.metadataLocation()))); | ||
|
|
||
| return castResponse(responseType, resp); | ||
| } | ||
|
|
||
| case UPDATE_TABLE: | ||
| { | ||
| TableIdentifier ident = tableIdentFromPathVars(vars); | ||
| UpdateTableRequest request = castRequest(UpdateTableRequest.class, body); | ||
| return castResponse(responseType, CatalogHandlers.updateTable(catalog, ident, request)); | ||
| LoadTableResponse resp = | ||
| CatalogHandlers.updateTable( | ||
| catalog, | ||
| tableIdentFromPathVars(vars), | ||
| castRequest(UpdateTableRequest.class, body)); | ||
|
|
||
| responseHeaders.accept( | ||
| ImmutableMap.of(HttpHeaders.ETAG, ETagProvider.of(resp.metadataLocation()))); | ||
|
|
||
| return castResponse(responseType, resp); | ||
| } | ||
|
|
||
| case RENAME_TABLE: | ||
|
|
@@ -625,8 +651,8 @@ protected <T extends RESTResponse> T execute( | |
| vars.putAll(request.queryParameters()); | ||
| vars.putAll(routeAndVars.second()); | ||
|
|
||
| return handleRequest(routeAndVars.first(), vars.build(), request.body(), responseType); | ||
|
|
||
| return handleRequest( | ||
| routeAndVars.first(), vars.build(), request, responseType, responseHeaders); | ||
| } catch (RuntimeException e) { | ||
| configureResponseFromException(e, errorBuilder); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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 static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestETagProvider { | ||
| @Test | ||
| public void testNullInput() { | ||
| assertThatThrownBy(() -> ETagProvider.of(null)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("Invalid metadata location: null"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyInput() { | ||
| assertThatThrownBy(() -> ETagProvider.of("")) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageContaining("Invalid metadata location: empty"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testETagContent() { | ||
| assertThat("1f865717") | ||
| .isEqualTo( | ||
| ETagProvider.of( | ||
| "/var/folders/20/290st0_52y5fyjcj2mlg49500000gn/T/junit-3064022805908958416/db_name/tbl_name/metadata/00000-f7a7956e-61d0-499b-be60-b141283f8229.metadata.json")); | ||
|
|
||
| assertThat("55faa5d9").isEqualTo(ETagProvider.of("/short/path")); | ||
| } | ||
| } |
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: it might make sense to have a small test class for this where the metadata location is null/well-defined and where we compare against a precalculated etag value
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.
by recalculated tag value do you mean that a test where the expected output is hard-coded? Would that guard against someone changing the implementation of ETag creation? I added a test for that, let me know if this is what you mean