|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.spark; |
| 20 | + |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import com.google.common.collect.ImmutableSet; |
| 24 | +import java.io.Closeable; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.function.Function; |
| 31 | +import org.apache.iceberg.CatalogProperties; |
| 32 | +import org.apache.iceberg.catalog.Namespace; |
| 33 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 34 | +import org.apache.iceberg.io.CloseableGroup; |
| 35 | +import org.apache.iceberg.rest.Endpoint; |
| 36 | +import org.apache.iceberg.rest.ErrorHandlers; |
| 37 | +import org.apache.iceberg.rest.HTTPClient; |
| 38 | +import org.apache.iceberg.rest.RESTClient; |
| 39 | +import org.apache.iceberg.rest.ResourcePaths; |
| 40 | +import org.apache.iceberg.rest.auth.OAuth2Util; |
| 41 | +import org.apache.iceberg.rest.responses.ConfigResponse; |
| 42 | +import org.apache.iceberg.util.EnvironmentUtil; |
| 43 | +import org.apache.polaris.core.rest.PolarisEndpoints; |
| 44 | +import org.apache.polaris.core.rest.PolarisResourcePaths; |
| 45 | +import org.apache.polaris.service.types.GenericTable; |
| 46 | +import org.apache.polaris.spark.rest.CreateGenericTableRESTRequest; |
| 47 | +import org.apache.polaris.spark.rest.LoadGenericTableRESTResponse; |
| 48 | + |
| 49 | +/** |
| 50 | + * [[PolarisRESTCatalog]] talks to Polaris REST APIs, and implements the PolarisCatalog interfaces, |
| 51 | + * which are generic table related APIs at this moment. This class doesn't interact with any Spark |
| 52 | + * objects. |
| 53 | + */ |
| 54 | +public class PolarisRESTCatalog implements PolarisCatalog, Closeable { |
| 55 | + private final Function<Map<String, String>, RESTClient> clientBuilder; |
| 56 | + |
| 57 | + private RESTClient restClient = null; |
| 58 | + private CloseableGroup closeables = null; |
| 59 | + private Set<Endpoint> endpoints; |
| 60 | + private OAuth2Util.AuthSession catalogAuth = null; |
| 61 | + private PolarisResourcePaths pathGenerator = null; |
| 62 | + |
| 63 | + // the default endpoints to config if server doesn't specify the 'endpoints' configuration. |
| 64 | + private static final Set<Endpoint> DEFAULT_ENDPOINTS = PolarisEndpoints.GENERIC_TABLE_ENDPOINTS; |
| 65 | + |
| 66 | + public PolarisRESTCatalog() { |
| 67 | + this(config -> HTTPClient.builder(config).uri(config.get(CatalogProperties.URI)).build()); |
| 68 | + } |
| 69 | + |
| 70 | + public PolarisRESTCatalog(Function<Map<String, String>, RESTClient> clientBuilder) { |
| 71 | + this.clientBuilder = clientBuilder; |
| 72 | + } |
| 73 | + |
| 74 | + public void initialize(Map<String, String> unresolved, OAuth2Util.AuthSession catalogAuth) { |
| 75 | + Preconditions.checkArgument(unresolved != null, "Invalid configuration: null"); |
| 76 | + |
| 77 | + // Resolve any configuration that is supplied by environment variables. |
| 78 | + // For example: if we have an entity ("key", "env:envVar") in the unresolved, |
| 79 | + // and envVar is configured to envValue in system env. After resolve, we got |
| 80 | + // entity ("key", "envValue"). |
| 81 | + Map<String, String> props = EnvironmentUtil.resolveAll(unresolved); |
| 82 | + |
| 83 | + // TODO: switch to use authManager once iceberg dependency is updated to 1.9.0 |
| 84 | + this.catalogAuth = catalogAuth; |
| 85 | + |
| 86 | + ConfigResponse config; |
| 87 | + try (RESTClient initClient = clientBuilder.apply(props).withAuthSession(catalogAuth)) { |
| 88 | + config = fetchConfig(initClient, catalogAuth.headers(), props); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new UncheckedIOException("Failed to close HTTP client", e); |
| 91 | + } |
| 92 | + |
| 93 | + // call getConfig to get the server configurations |
| 94 | + Map<String, String> mergedProps = config.merge(props); |
| 95 | + if (config.endpoints().isEmpty()) { |
| 96 | + this.endpoints = DEFAULT_ENDPOINTS; |
| 97 | + } else { |
| 98 | + this.endpoints = ImmutableSet.copyOf(config.endpoints()); |
| 99 | + } |
| 100 | + |
| 101 | + this.pathGenerator = PolarisResourcePaths.forCatalogProperties(mergedProps); |
| 102 | + this.restClient = clientBuilder.apply(mergedProps).withAuthSession(catalogAuth); |
| 103 | + |
| 104 | + this.closeables = new CloseableGroup(); |
| 105 | + this.closeables.addCloseable(this.restClient); |
| 106 | + this.closeables.setSuppressCloseFailure(true); |
| 107 | + } |
| 108 | + |
| 109 | + protected static ConfigResponse fetchConfig( |
| 110 | + RESTClient client, Map<String, String> headers, Map<String, String> properties) { |
| 111 | + // send the client's warehouse location to the service to keep in sync |
| 112 | + // this is needed for cases where the warehouse is configured at client side, |
| 113 | + // and used by Polaris server as catalog name. |
| 114 | + ImmutableMap.Builder<String, String> queryParams = ImmutableMap.builder(); |
| 115 | + if (properties.containsKey(CatalogProperties.WAREHOUSE_LOCATION)) { |
| 116 | + queryParams.put( |
| 117 | + CatalogProperties.WAREHOUSE_LOCATION, |
| 118 | + properties.get(CatalogProperties.WAREHOUSE_LOCATION)); |
| 119 | + } |
| 120 | + |
| 121 | + ConfigResponse configResponse = |
| 122 | + client.get( |
| 123 | + ResourcePaths.config(), |
| 124 | + queryParams.build(), |
| 125 | + ConfigResponse.class, |
| 126 | + headers, |
| 127 | + ErrorHandlers.defaultErrorHandler()); |
| 128 | + configResponse.validate(); |
| 129 | + return configResponse; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void close() throws IOException { |
| 134 | + if (closeables != null) { |
| 135 | + closeables.close(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public List<TableIdentifier> listGenericTables(Namespace ns) { |
| 141 | + throw new UnsupportedOperationException("listTables not supported"); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean dropGenericTable(TableIdentifier identifier) { |
| 146 | + throw new UnsupportedOperationException("dropTable not supported"); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public GenericTable createGenericTable( |
| 151 | + TableIdentifier identifier, String format, String doc, Map<String, String> props) { |
| 152 | + Endpoint.check(endpoints, PolarisEndpoints.V1_CREATE_GENERIC_TABLE); |
| 153 | + CreateGenericTableRESTRequest request = |
| 154 | + new CreateGenericTableRESTRequest(identifier.name(), format, doc, props); |
| 155 | + |
| 156 | + LoadGenericTableRESTResponse response = |
| 157 | + restClient |
| 158 | + .withAuthSession(this.catalogAuth) |
| 159 | + .post( |
| 160 | + pathGenerator.genericTables(identifier.namespace()), |
| 161 | + request, |
| 162 | + LoadGenericTableRESTResponse.class, |
| 163 | + Map.of(), |
| 164 | + ErrorHandlers.tableErrorHandler()); |
| 165 | + |
| 166 | + return response.getTable(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public GenericTable loadGenericTable(TableIdentifier identifier) { |
| 171 | + Endpoint.check(endpoints, PolarisEndpoints.V1_LOAD_GENERIC_TABLE); |
| 172 | + LoadGenericTableRESTResponse response = |
| 173 | + restClient |
| 174 | + .withAuthSession(this.catalogAuth) |
| 175 | + .get( |
| 176 | + pathGenerator.genericTable(identifier), |
| 177 | + null, |
| 178 | + LoadGenericTableRESTResponse.class, |
| 179 | + Map.of(), |
| 180 | + ErrorHandlers.tableErrorHandler()); |
| 181 | + |
| 182 | + return response.getTable(); |
| 183 | + } |
| 184 | +} |
0 commit comments