FANNG1 commented on code in PR #4273: URL: https://github.com/apache/gravitino/pull/4273#discussion_r1713119834
########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); + } + this.catalogNames = catalogs.keySet().stream().sorted().collect(Collectors.toList()); + this.properties = properties; Review Comment: could you init catalog properties in `initialize`? ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); Review Comment: please use something like `getCatalogName` to replace `key.split("\\.")[1]`, to make it more meaningful. ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergTableOpsManager.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IcebergTableOpsManager implements AutoCloseable { + public static final Logger LOG = LoggerFactory.getLogger(IcebergTableOpsManager.class); + + public static final String DEFAULT_CATALOG = "default_catalog"; + private final Cache<String, IcebergTableOps> icebergTableOpsCache; + private final IcebergTableOpsProvider provider; + + public IcebergTableOpsManager(Map<String, String> properties) { + this.icebergTableOpsCache = Caffeine.newBuilder().build(); + this.provider = createProvider(properties); + this.provider.initialize(properties); + } + + /** + * @param rawPrefix The path parameter is passed by a Jetty handler. The pattern is matching + * ([^/]*\/), end with / + * @return the instance of IcebergTableOps. + */ + public IcebergTableOps getOps(String rawPrefix) { + String prefix = shelling(rawPrefix); + String cacheKey = prefix; + if (DEFAULT_CATALOG.equals(prefix)) { + throw new RuntimeException( + String.format("%s is conflict with reserved key, please replace it", prefix)); + } + if (StringUtils.isBlank(prefix)) { + LOG.debug("prefix is empty, return default iceberg catalog"); Review Comment: please remove the log ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); + } + this.catalogNames = catalogs.keySet().stream().sorted().collect(Collectors.toList()); + this.properties = properties; + } + + @Override + public IcebergTableOps getIcebergTableOps(String prefix) { + if (StringUtils.isBlank(prefix)) { + return new IcebergTableOps(new IcebergConfig(properties)); + } + if (!catalogNames.contains(prefix)) { + String errorMsg = String.format("%s can not match any catalog", prefix); + LOG.error(errorMsg); Review Comment: use LOG.warn is ok, error meaning something critical for Gravitino ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); Review Comment: why not use `catalogNames` directly? ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); + } + this.catalogNames = catalogs.keySet().stream().sorted().collect(Collectors.toList()); + this.properties = properties; + } + + @Override + public IcebergTableOps getIcebergTableOps(String prefix) { + if (StringUtils.isBlank(prefix)) { + return new IcebergTableOps(new IcebergConfig(properties)); + } + if (!catalogNames.contains(prefix)) { + String errorMsg = String.format("%s can not match any catalog", prefix); Review Comment: LOG.warn("{} can not match any catalog", prefix) ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergTableOpsManager.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IcebergTableOpsManager implements AutoCloseable { + public static final Logger LOG = LoggerFactory.getLogger(IcebergTableOpsManager.class); + + public static final String DEFAULT_CATALOG = "default_catalog"; Review Comment: use `__gravitino_default_catalog`? ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergTableOpsManager.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IcebergTableOpsManager implements AutoCloseable { + public static final Logger LOG = LoggerFactory.getLogger(IcebergTableOpsManager.class); + + public static final String DEFAULT_CATALOG = "default_catalog"; + private final Cache<String, IcebergTableOps> icebergTableOpsCache; + private final IcebergTableOpsProvider provider; + + public IcebergTableOpsManager(Map<String, String> properties) { + this.icebergTableOpsCache = Caffeine.newBuilder().build(); + this.provider = createProvider(properties); + this.provider.initialize(properties); + } + + /** + * @param rawPrefix The path parameter is passed by a Jetty handler. The pattern is matching + * ([^/]*\/), end with / + * @return the instance of IcebergTableOps. + */ + public IcebergTableOps getOps(String rawPrefix) { + String prefix = shelling(rawPrefix); + String cacheKey = prefix; + if (DEFAULT_CATALOG.equals(prefix)) { + throw new RuntimeException( + String.format("%s is conflict with reserved key, please replace it", prefix)); + } + if (StringUtils.isBlank(prefix)) { + LOG.debug("prefix is empty, return default iceberg catalog"); + cacheKey = DEFAULT_CATALOG; + } + return icebergTableOpsCache.get(cacheKey, k -> provider.getIcebergTableOps(prefix)); + } + + private IcebergTableOpsProvider createProvider(Map<String, String> properties) { + try { + String className = + properties.getOrDefault( + IcebergConstants.ICEBERG_REST_CATALOG_PROVIDER, + ConfigIcebergTableOpsProvider.class.getName()); + Class<?> providerClz = Class.forName(className); + return (IcebergTableOpsProvider) providerClz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private String shelling(String rawPrefix) { + if (StringUtils.isBlank(rawPrefix)) { + return rawPrefix; + } else if (!rawPrefix.endsWith("/")) { Review Comment: is it possible rawPrefix is not endswith `/`? ########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/ConfigIcebergTableOpsProvider.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.gravitino.iceberg.common.ops; + +import com.google.common.collect.Maps; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; +import org.apache.gravitino.iceberg.common.IcebergConfig; +import org.apache.gravitino.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This provider use configs to support multiple catalogs. + * + * <p>For example, there are two different catalogs: jdbc_proxy, hive_proxy The config is like: + * + * <p>gravitino.iceberg-rest.catalog.jdbc_proxy.catalog-backend = jdbc + * gravitino.iceberg-rest.catalog.jdbc_proxy.uri = jdbc:mysql://{host}:{port}/{db} ... + * gravitino.iceberg-rest.catalog.hive_proxy.catalog-backend = hive + * gravitino.iceberg-rest.catalog.hive_proxy.uri = thrift://{host}:{port} ... + */ +public class ConfigIcebergTableOpsProvider implements IcebergTableOpsProvider { + public static final Logger LOG = LoggerFactory.getLogger(ConfigIcebergTableOpsProvider.class); + + private Map<String, String> properties; + + private List<String> catalogNames; + + @Override + public void initialize(Map<String, String> properties) { + Map<String, Boolean> catalogs = Maps.newHashMap(); + for (String key : properties.keySet()) { + if (!key.startsWith("catalog.")) { + continue; + } + if (key.split("\\.").length < 3) { + throw new RuntimeException(String.format("%s format is illegal", key)); + } + catalogs.put(key.split("\\.")[1], true); + } + this.catalogNames = catalogs.keySet().stream().sorted().collect(Collectors.toList()); + this.properties = properties; + } + + @Override + public IcebergTableOps getIcebergTableOps(String prefix) { + if (StringUtils.isBlank(prefix)) { + return new IcebergTableOps(new IcebergConfig(properties)); + } + if (!catalogNames.contains(prefix)) { + String errorMsg = String.format("%s can not match any catalog", prefix); + LOG.error(errorMsg); + throw new RuntimeException(errorMsg); + } + return new IcebergTableOps(getCatalogConfig(prefix)); + } + + private IcebergConfig getCatalogConfig(String catalog) { + Map<String, String> base = Maps.newHashMap(this.properties); Review Comment: why merging `this.properties`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
