This is an automated email from the ASF dual-hosted git repository.
baiyangtx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/amoro.git
The following commit(s) were added to refs/heads/master by this push:
new db7c90488 [Feature] Introduce a new plugin RestExtension to extend
rest service (#4112)
db7c90488 is described below
commit db7c9048862718ead6fe2e548e2fac2411d02890
Author: baiyangtx <[email protected]>
AuthorDate: Mon Mar 9 17:23:14 2026 +0800
[Feature] Introduce a new plugin RestExtension to extend rest service
(#4112)
* [Feature] Introduce a new plugin RestExtension to extend rest service
Cherry-picked from 2010d5998a0770fa7b58456e51d63538af9e5e78.
Co-Authored-By: Aime <[email protected]>
Change-Id: Iedfc5a383dabe42c96ce4030411cb93adc468a77
* refactor
* typo
---------
Co-authored-by: zhangyongxiang.alpha <[email protected]>
Co-authored-by: Aime <[email protected]>
---
.../apache/amoro/server/AmoroServiceContainer.java | 25 ++++++++---
.../apache/amoro/server/RestCatalogService.java | 48 ++++++++++++++++++++-
.../org/apache/amoro/server/RestExtension.java | 31 ++++++++++++++
.../apache/amoro/server/RestExtensionFactory.java | 34 +++++++++++++++
.../apache/amoro/server/RestExtensionManager.java | 50 ++++++++++++++++++++++
.../server/manager/AbstractPluginManager.java | 7 ++-
.../amoro/server/manager/PluginConfiguration.java | 5 +++
.../org.apache.amoro.server.RestExtensionFactory | 19 ++++++++
.../org/apache/amoro/server/AmsEnvironment.java | 8 ++++
.../amoro-bin/conf/plugins/rest-extensions.yaml | 22 ++++++++++
10 files changed, 241 insertions(+), 8 deletions(-)
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java
b/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java
index 5d76e3983..19bf7be98 100644
--- a/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java
+++ b/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java
@@ -21,6 +21,7 @@ package org.apache.amoro.server;
import static
org.apache.amoro.server.AmoroManagementConf.USE_MASTER_SLAVE_MODE;
import io.javalin.Javalin;
+import io.javalin.http.Context;
import io.javalin.http.HttpCode;
import io.javalin.http.staticfiles.Location;
import org.apache.amoro.Constants;
@@ -97,9 +98,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
+import java.util.function.Function;
public class AmoroServiceContainer {
@@ -356,7 +359,12 @@ public class AmoroServiceContainer {
DashboardServer dashboardServer =
new DashboardServer(
serviceConfig, catalogManager, tableManager, optimizerManager,
terminalManager, this);
- RestCatalogService restCatalogService = new
RestCatalogService(catalogManager, tableManager);
+ RestExtensionManager restExtensionManager = new RestExtensionManager();
+ restExtensionManager.initialize();
+ List<RestExtension> restExtensions =
+ restExtensionManager.loadExtensions(serviceConfig, catalogManager,
tableManager);
+ Function<Context, Optional<RestExtension>> handleExceptionByExtension =
+ ctx -> restExtensions.stream().filter(ext ->
ext.needHandleException(ctx)).findFirst();
httpServer =
Javalin.create(
@@ -374,7 +382,9 @@ public class AmoroServiceContainer {
httpServer.routes(
() -> {
dashboardServer.endpoints().addEndpoints();
- restCatalogService.endpoints().addEndpoints();
+ for (RestExtension restExtension : restExtensions) {
+ restExtension.endpoints().addEndpoints();
+ }
});
httpServer.before(
@@ -389,8 +399,9 @@ public class AmoroServiceContainer {
httpServer.exception(
Exception.class,
(e, ctx) -> {
- if (restCatalogService.needHandleException(ctx)) {
- restCatalogService.handleException(e, ctx);
+ Optional<RestExtension> extension =
handleExceptionByExtension.apply(ctx);
+ if (extension.isPresent()) {
+ extension.get().handleException(e, ctx);
} else {
dashboardServer.handleException(e, ctx);
}
@@ -399,7 +410,8 @@ public class AmoroServiceContainer {
httpServer.error(
HttpCode.NOT_FOUND.getStatus(),
ctx -> {
- if (!restCatalogService.needHandleException(ctx)) {
+ Optional<RestExtension> extension =
handleExceptionByExtension.apply(ctx);
+ if (!extension.isPresent()) {
ctx.json(new ErrorResponse(HttpCode.NOT_FOUND, "page not found!",
""));
}
});
@@ -407,7 +419,8 @@ public class AmoroServiceContainer {
httpServer.error(
HttpCode.INTERNAL_SERVER_ERROR.getStatus(),
ctx -> {
- if (!restCatalogService.needHandleException(ctx)) {
+ Optional<RestExtension> extension =
handleExceptionByExtension.apply(ctx);
+ if (!extension.isPresent()) {
ctx.json(new ErrorResponse(HttpCode.INTERNAL_SERVER_ERROR,
"internal error!", ""));
}
});
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/RestCatalogService.java
b/amoro-ams/src/main/java/org/apache/amoro/server/RestCatalogService.java
index bb3d47cc0..aa342beb9 100644
--- a/amoro-ams/src/main/java/org/apache/amoro/server/RestCatalogService.java
+++ b/amoro-ams/src/main/java/org/apache/amoro/server/RestCatalogService.java
@@ -36,6 +36,7 @@ import io.javalin.http.HttpCode;
import io.javalin.plugin.json.JavalinJackson;
import org.apache.amoro.ServerTableIdentifier;
import org.apache.amoro.TableFormat;
+import org.apache.amoro.config.Configurations;
import org.apache.amoro.events.IcebergReportEvent;
import org.apache.amoro.exception.ObjectNotExistsException;
import org.apache.amoro.properties.CatalogMetaProperties;
@@ -44,6 +45,7 @@ import org.apache.amoro.server.catalog.InternalCatalog;
import org.apache.amoro.server.catalog.ServerCatalog;
import org.apache.amoro.server.manager.EventsManager;
import org.apache.amoro.server.persistence.PersistentBase;
+import org.apache.amoro.server.table.TableManager;
import org.apache.amoro.server.table.internal.InternalTableCreator;
import org.apache.amoro.server.table.internal.InternalTableHandler;
import org.apache.amoro.server.table.internal.InternalTableManager;
@@ -92,7 +94,7 @@ import java.util.stream.Collectors;
*
href="https://github.com/apache/iceberg/blob/main/open-api/rest-catalog-open-api.yaml">iceberg
* rest-catalog-open-api </a>
*/
-public class RestCatalogService extends PersistentBase {
+public class RestCatalogService extends PersistentBase implements
RestExtension {
private static final Logger LOG =
LoggerFactory.getLogger(RestCatalogService.class);
@@ -117,6 +119,7 @@ public class RestCatalogService extends PersistentBase {
this.jsonMapper = new JavalinJackson(objectMapper);
}
+ @Override
public EndpointGroup endpoints() {
return () -> {
// for iceberg rest catalog api
@@ -146,10 +149,12 @@ public class RestCatalogService extends PersistentBase {
};
}
+ @Override
public boolean needHandleException(Context ctx) {
return ctx.req.getRequestURI().startsWith(ICEBERG_REST_API_PREFIX);
}
+ @Override
public void handleException(Exception e, Context ctx) {
IcebergRestErrorCode code = IcebergRestErrorCode.exceptionToCode(e);
ErrorResponse response =
@@ -508,4 +513,45 @@ public class RestCatalogService extends PersistentBase {
return InternalServerError;
}
}
+
+ public static class RestCatalogServiceFactory implements
RestExtensionFactory {
+
+ private CatalogManager catalogManager;
+ private TableManager tableManager;
+ private Configurations serviceConfig;
+
+ @Override
+ public RestExtensionFactory withServiceConfig(Configurations
serviceConfig) {
+ this.serviceConfig = serviceConfig;
+ return this;
+ }
+
+ @Override
+ public RestExtensionFactory withCatalogManager(CatalogManager
catalogManager) {
+ this.catalogManager = catalogManager;
+ return this;
+ }
+
+ @Override
+ public RestExtensionFactory withTableManager(TableManager tableManager) {
+ this.tableManager = tableManager;
+ return this;
+ }
+
+ @Override
+ public RestExtension build() {
+ return new RestCatalogService(catalogManager, tableManager);
+ }
+
+ @Override
+ public void open(Map<String, String> properties) {}
+
+ @Override
+ public void close() {}
+
+ @Override
+ public String name() {
+ return "iceberg-rest-catalog";
+ }
+ }
}
diff --git a/amoro-ams/src/main/java/org/apache/amoro/server/RestExtension.java
b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtension.java
new file mode 100644
index 000000000..b397d99ee
--- /dev/null
+++ b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtension.java
@@ -0,0 +1,31 @@
+/*
+ * 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.amoro.server;
+
+import io.javalin.apibuilder.EndpointGroup;
+import io.javalin.http.Context;
+
+public interface RestExtension {
+
+ EndpointGroup endpoints();
+
+ boolean needHandleException(Context ctx);
+
+ void handleException(Exception e, Context ctx);
+}
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionFactory.java
b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionFactory.java
new file mode 100644
index 000000000..23cfa6a56
--- /dev/null
+++ b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionFactory.java
@@ -0,0 +1,34 @@
+/*
+ * 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.amoro.server;
+
+import org.apache.amoro.ActivePlugin;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.server.catalog.CatalogManager;
+import org.apache.amoro.server.table.TableManager;
+
+public interface RestExtensionFactory extends ActivePlugin {
+ RestExtensionFactory withServiceConfig(Configurations serviceConfig);
+
+ RestExtensionFactory withCatalogManager(CatalogManager catalogManager);
+
+ RestExtensionFactory withTableManager(TableManager tableManager);
+
+ RestExtension build();
+}
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionManager.java
b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionManager.java
new file mode 100644
index 000000000..143e61701
--- /dev/null
+++ b/amoro-ams/src/main/java/org/apache/amoro/server/RestExtensionManager.java
@@ -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.amoro.server;
+
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.server.catalog.CatalogManager;
+import org.apache.amoro.server.manager.AbstractPluginManager;
+import org.apache.amoro.server.table.TableManager;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class RestExtensionManager extends
AbstractPluginManager<RestExtensionFactory> {
+
+ private static final String REST_EXTENSION_PLUGIN_CATEGORY =
"rest-extensions";
+
+ public RestExtensionManager() {
+ super(REST_EXTENSION_PLUGIN_CATEGORY);
+ }
+
+ public List<RestExtension> loadExtensions(
+ Configurations serviceConfig, CatalogManager catalogManager,
TableManager tableManager) {
+ List<RestExtensionFactory> factories = installedPlugins();
+ return factories.stream()
+ .map(
+ factory ->
+ factory
+ .withServiceConfig(serviceConfig)
+ .withCatalogManager(catalogManager)
+ .withTableManager(tableManager)
+ .build())
+ .collect(Collectors.toList());
+ }
+}
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/manager/AbstractPluginManager.java
b/amoro-ams/src/main/java/org/apache/amoro/server/manager/AbstractPluginManager.java
index e3c70c41f..84d7b6922 100644
---
a/amoro-ams/src/main/java/org/apache/amoro/server/manager/AbstractPluginManager.java
+++
b/amoro-ams/src/main/java/org/apache/amoro/server/manager/AbstractPluginManager.java
@@ -82,6 +82,8 @@ public abstract class AbstractPluginManager<T extends
ActivePlugin> implements P
/** Initialize the plugin manager, and install all plugins. */
public void initialize() {
+ foundAvailablePlugins();
+
List<PluginConfiguration> pluginConfigs = loadPluginConfigurations();
pluginConfigs.stream()
.sorted(Comparator.comparing(PluginConfiguration::getPriority))
@@ -92,7 +94,6 @@ public abstract class AbstractPluginManager<T extends
ActivePlugin> implements P
exists == null, "Duplicate plugin name found: %s",
config.getName());
});
- foundAvailablePlugins();
for (PluginConfiguration pluginConfig : pluginConfigs) {
if (!pluginConfig.isEnabled()) {
continue;
@@ -186,6 +187,10 @@ public abstract class AbstractPluginManager<T extends
ActivePlugin> implements P
Environments.getConfigPath(), PLUGIN_CONFIG_DIR_NAME, pluginCategory()
+ ".yaml");
}
+ protected Map<String, T> getFoundedPlugins() {
+ return foundedPlugins;
+ }
+
/**
* Visit all installed plugins.
*
diff --git
a/amoro-ams/src/main/java/org/apache/amoro/server/manager/PluginConfiguration.java
b/amoro-ams/src/main/java/org/apache/amoro/server/manager/PluginConfiguration.java
index 1c35ae9a0..44c5e5a30 100644
---
a/amoro-ams/src/main/java/org/apache/amoro/server/manager/PluginConfiguration.java
+++
b/amoro-ams/src/main/java/org/apache/amoro/server/manager/PluginConfiguration.java
@@ -25,6 +25,7 @@ import
org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
import org.apache.amoro.utils.JacksonUtil;
import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
/** Configuration of a plugin. */
@@ -64,6 +65,10 @@ public class PluginConfiguration {
return new PluginConfiguration(name, enabled, priority, props);
}
+ public static PluginConfiguration emptyConfig(String name) {
+ return new PluginConfiguration(name, true, -1, new HashMap<>());
+ }
+
/** @return Plugin name. */
public String getName() {
return name;
diff --git
a/amoro-ams/src/main/resources/META-INF/services/org.apache.amoro.server.RestExtensionFactory
b/amoro-ams/src/main/resources/META-INF/services/org.apache.amoro.server.RestExtensionFactory
new file mode 100644
index 000000000..a0b5cf404
--- /dev/null
+++
b/amoro-ams/src/main/resources/META-INF/services/org.apache.amoro.server.RestExtensionFactory
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.apache.amoro.server.RestCatalogService$RestCatalogServiceFactory
diff --git
a/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java
b/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java
index 7ce1cab33..93fd182d3 100644
--- a/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java
+++ b/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java
@@ -121,6 +121,10 @@ public class AmsEnvironment {
new File(rootPath + "/conf/plugins/table-runtime-factories.yaml"),
getTableRuntimeFactoriesConfig(),
Charset.defaultCharset());
+ FileUtils.writeStringToFile(
+ new File(rootPath + "/conf/plugins/rest-extensions.yaml"),
+ getRestExtensionsConfig(),
+ Charset.defaultCharset());
System.setProperty(Environments.AMORO_HOME, rootPath);
System.setProperty("derby.init.sql.dir", path + "../classes/sql/derby/");
serviceContainer = new AmoroServiceContainer();
@@ -426,4 +430,8 @@ public class AmsEnvironment {
+ " enabled: true\n"
+ " priority: 100\n";
}
+
+ private String getRestExtensionsConfig() {
+ return "rest-extensions:\n" + " - name: iceberg-rest-catalog\n" + "
enabled: true\n";
+ }
}
diff --git a/dist/src/main/amoro-bin/conf/plugins/rest-extensions.yaml
b/dist/src/main/amoro-bin/conf/plugins/rest-extensions.yaml
new file mode 100644
index 000000000..ff2729d81
--- /dev/null
+++ b/dist/src/main/amoro-bin/conf/plugins/rest-extensions.yaml
@@ -0,0 +1,22 @@
+#
+# 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.
+#
+
+# This file is the config file for plugin rest extension
+# configurations of rest extensions
+rest-extensions:
+ - name: iceberg-rest-catalog
+ enabled: true