Skip to content
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 8 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/Endpoint.java
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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

@dimas-b dimas-b Aug 26, 2024

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).

Copy link
Contributor

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.

Copy link
Contributor Author

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)

Copy link
Contributor

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.

Copy link
Contributor Author

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 in TestEndpoint


// 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);
}
}
Loading