-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Core: Allow servers to express supported endpoints via endpoint field in ConfigResponse #10929
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b69eeee
Core: Allow servers to express supported endpoints via endpoint field…
nastra 2fbb1e9
feedback
nastra f18bf3a
add flag to assume view endpoint support + some tests
nastra 48a4113
updates
nastra f9b14d6
review feedback
nastra 52fa399
Move endpoints to Endpoint class
nastra 48f96e1
make sure it's a valid HTTP Method
nastra 4678b65
review feedback
nastra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
core/src/main/java/org/apache/iceberg/rest/Endpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* 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.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import org.apache.hc.core5.http.Method; | ||
import org.apache.iceberg.relocated.com.google.common.base.Joiner; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.relocated.com.google.common.base.Splitter; | ||
import org.apache.iceberg.relocated.com.google.common.base.Strings; | ||
import org.apache.iceberg.relocated.com.google.common.base.Supplier; | ||
|
||
/** | ||
* Holds an endpoint definition that consists of the HTTP method (GET, POST, DELETE, ...) and the | ||
* resource path as defined in the Iceberg OpenAPI REST specification without parameter | ||
* substitution, such as <b>/v1/{prefix}/namespaces/{namespace}</b>. | ||
*/ | ||
public class Endpoint { | ||
|
||
// namespace endpoints | ||
public static final Endpoint V1_LIST_NAMESPACES = | ||
Endpoint.create("GET", ResourcePaths.V1_NAMESPACES); | ||
public static final Endpoint V1_LOAD_NAMESPACE = | ||
Endpoint.create("GET", ResourcePaths.V1_NAMESPACE); | ||
public static final Endpoint V1_CREATE_NAMESPACE = | ||
Endpoint.create("POST", ResourcePaths.V1_NAMESPACES); | ||
public static final Endpoint V1_UPDATE_NAMESPACE = | ||
Endpoint.create("POST", ResourcePaths.V1_NAMESPACE_PROPERTIES); | ||
public static final Endpoint V1_DELETE_NAMESPACE = | ||
Endpoint.create("DELETE", ResourcePaths.V1_NAMESPACE); | ||
public static final Endpoint V1_COMMIT_TRANSACTION = | ||
Endpoint.create("POST", ResourcePaths.V1_TRANSACTIONS_COMMIT); | ||
|
||
// table endpoints | ||
public static final Endpoint V1_LIST_TABLES = Endpoint.create("GET", ResourcePaths.V1_TABLES); | ||
public static final Endpoint V1_LOAD_TABLE = Endpoint.create("GET", ResourcePaths.V1_TABLE); | ||
public static final Endpoint V1_CREATE_TABLE = Endpoint.create("POST", ResourcePaths.V1_TABLES); | ||
public static final Endpoint V1_UPDATE_TABLE = Endpoint.create("POST", ResourcePaths.V1_TABLE); | ||
public static final Endpoint V1_DELETE_TABLE = Endpoint.create("DELETE", ResourcePaths.V1_TABLE); | ||
public static final Endpoint V1_RENAME_TABLE = | ||
Endpoint.create("POST", ResourcePaths.V1_TABLE_RENAME); | ||
public static final Endpoint V1_REGISTER_TABLE = | ||
Endpoint.create("POST", ResourcePaths.V1_TABLE_REGISTER); | ||
public static final Endpoint V1_REPORT_METRICS = | ||
Endpoint.create("POST", ResourcePaths.V1_TABLE_METRICS); | ||
|
||
// view endpoints | ||
public static final Endpoint V1_LIST_VIEWS = Endpoint.create("GET", ResourcePaths.V1_VIEWS); | ||
public static final Endpoint V1_LOAD_VIEW = Endpoint.create("GET", ResourcePaths.V1_VIEW); | ||
public static final Endpoint V1_CREATE_VIEW = Endpoint.create("POST", ResourcePaths.V1_VIEWS); | ||
public static final Endpoint V1_UPDATE_VIEW = Endpoint.create("POST", ResourcePaths.V1_VIEW); | ||
public static final Endpoint V1_DELETE_VIEW = Endpoint.create("DELETE", ResourcePaths.V1_VIEW); | ||
public static final Endpoint V1_RENAME_VIEW = | ||
Endpoint.create("POST", ResourcePaths.V1_VIEW_RENAME); | ||
|
||
private static final Splitter ENDPOINT_SPLITTER = Splitter.on(" "); | ||
private static final Joiner ENDPOINT_JOINER = Joiner.on(" "); | ||
private final String httpMethod; | ||
private final String path; | ||
|
||
private Endpoint(String httpMethod, String path) { | ||
Preconditions.checkArgument( | ||
!Strings.isNullOrEmpty(httpMethod), "Invalid HTTP method: null or empty"); | ||
Preconditions.checkArgument(!Strings.isNullOrEmpty(path), "Invalid path: null or empty"); | ||
nastra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.httpMethod = Method.normalizedValueOf(httpMethod).toString(); | ||
danielcweeks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.path = path; | ||
} | ||
|
||
public String httpMethod() { | ||
return httpMethod; | ||
} | ||
|
||
public String path() { | ||
return path; | ||
} | ||
|
||
public static Endpoint create(String httpMethod, String path) { | ||
return new Endpoint(httpMethod, path); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ENDPOINT_JOINER.join(httpMethod(), path()); | ||
} | ||
|
||
public static Endpoint fromString(String endpoint) { | ||
List<String> elements = ENDPOINT_SPLITTER.splitToList(endpoint); | ||
Preconditions.checkArgument( | ||
elements.size() == 2, | ||
"Invalid endpoint (must consist of two elements separated by a single space): %s", | ||
endpoint); | ||
return create(elements.get(0), elements.get(1)); | ||
} | ||
|
||
/** | ||
* Checks if the set of endpoints support the given {@link Endpoint}. | ||
* | ||
* @param supportedEndpoints The set of supported endpoints to check | ||
* @param endpoint The endpoint to check against the set of supported endpoints | ||
* @throws UnsupportedOperationException if the given {@link Endpoint} is not included in the set | ||
* of endpoints. | ||
*/ | ||
public static void check(Set<Endpoint> supportedEndpoints, Endpoint endpoint) { | ||
if (!supportedEndpoints.contains(endpoint)) { | ||
throw new UnsupportedOperationException( | ||
String.format("Server does not support endpoint: %s", endpoint)); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the set of endpoints support the given {@link Endpoint}. | ||
* | ||
* @param supportedEndpoints The set of supported endpoints to check | ||
* @param endpoint The endpoint to check against the set of supported endpoints | ||
* @param supplier The supplier throwing a {@link RuntimeException} if the given {@link Endpoint} | ||
* is not included in the set of endpoints. | ||
*/ | ||
public static void check( | ||
Set<Endpoint> supportedEndpoints, Endpoint endpoint, Supplier<RuntimeException> supplier) { | ||
if (!supportedEndpoints.contains(endpoint)) { | ||
throw supplier.get(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (!(o instanceof Endpoint)) { | ||
return false; | ||
} | ||
nastra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Endpoint endpoint = (Endpoint) o; | ||
return Objects.equals(httpMethod, endpoint.httpMethod) && Objects.equals(path, endpoint.path); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(httpMethod, path); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Suggestion: this could probably be an
enum
+ a (derived)Map<String, enum>
for loasing server config responses. The spec defines a fixed representation for every value, so parsing is not really necessary, I think, the spec'd values can be keys in that map.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.
yeah good point and I'll probably change that once we agree on the final JSON representation for a single endpoint
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.
@nastra : Are you still ok with the
enum
suggestion? I think it will match the corresponding REST API spec change (#10928) closer than attempting to parse and compare for equality by entpoint components (HTTP verb and 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.
I'm good either way here, the parsing is relatively trivial and doesn't seem brittle for equality comparisons.
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.
I think currently I'm preferring the non-enum approach (basically what's in the PR right now)
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.
Then how about not parsing the endpoint IDs? I think the spec makes them unambiguous "in whole". Avoiding parsing and component-wise comparisons, would make the processing of those IDs more robust, IMHO.
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.
@dimas-b we do perform equality checks to make sure that an endpoint is compared as a "whole". A valid endpoint must be of the form
GET /resource/path
, which is separated by a single space and the http method must be uppercase + a valid HTTP method, so I'm not sure what the concern here is. Also worth mentioning that all of this is verified inTestEndpoint