From 31d965dba12552e1e512242150f6e3abd65029c8 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Wed, 8 Jan 2025 12:02:30 +1100 Subject: [PATCH 1/3] refactor metalakes --- .../apache/gravitino/cli/CommandHandler.java | 106 ++++++++ .../gravitino/cli/GravitinoCommandLine.java | 87 +------ .../gravitino/cli/MetalakeCommandHandler.java | 229 ++++++++++++++++++ 3 files changed, 336 insertions(+), 86 deletions(-) create mode 100644 clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java create mode 100644 clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java new file mode 100644 index 00000000000..2ba06ef1755 --- /dev/null +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/CommandHandler.java @@ -0,0 +1,106 @@ +/* + * 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.cli; + +import org.apache.commons.cli.CommandLine; + +public class CommandHandler { + + public static final String DEFAULT_URL = "http://localhost:8090"; + + private String urlEnv; + private boolean urlSet = false; + private String authEnv; + private boolean authSet = false; + + /** + * Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment + * variable or the Gravitio config file. + * + * @return The Gravitinno URL, or null if not found. + */ + public String getUrl(CommandLine line) { + GravitinoConfig config = new GravitinoConfig(null); + + // If specified on the command line use that + if (line.hasOption(GravitinoOptions.URL)) { + return line.getOptionValue(GravitinoOptions.URL); + } + + // Cache the Gravitino URL environment variable + if (urlEnv == null && !urlSet) { + urlEnv = System.getenv("GRAVITINO_URL"); + urlSet = true; + } + + // If set return the Gravitino URL environment variable + if (urlEnv != null) { + return urlEnv; + } + + // Check if the Gravitino URL is specified in the configuration file + if (config.fileExists()) { + config.read(); + String configURL = config.getGravitinoURL(); + if (configURL != null) { + return configURL; + } + } + + // Return the default localhost URL + return DEFAULT_URL; + } + + /** + * Retrieves the Gravitinno authentication from the command line options or the GRAVITINO_AUTH + * environment variable or the Gravitio config file. + * + * @return The Gravitinno authentication, or null if not found. + */ + public String getAuth(CommandLine line) { + // If specified on the command line use that + if (line.hasOption(GravitinoOptions.SIMPLE)) { + return GravitinoOptions.SIMPLE; + } + + // Cache the Gravitino authentication type environment variable + if (authEnv == null && !authSet) { + authEnv = System.getenv("GRAVITINO_AUTH"); + authSet = true; + } + + // If set return the Gravitino authentication type environment variable + if (authEnv != null) { + return authEnv; + } + + // Check if the authentication type is specified in the configuration file + GravitinoConfig config = new GravitinoConfig(null); + if (config.fileExists()) { + config.read(); + String configAuthType = config.getGravitinoAuthType(); + if (configAuthType != null) { + return configAuthType; + } + } + + return null; + } +} diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java index 442ec2d1c33..1f160232148 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java @@ -137,7 +137,7 @@ private void executeCommand() { } else if (entity.equals(CommandEntities.CATALOG)) { handleCatalogCommand(); } else if (entity.equals(CommandEntities.METALAKE)) { - handleMetalakeCommand(); + new MetalakeCommandHandler(this, line, command, ignore).handle(); } else if (entity.equals(CommandEntities.TOPIC)) { handleTopicCommand(); } else if (entity.equals(CommandEntities.FILESET)) { @@ -155,91 +155,6 @@ private void executeCommand() { } } - /** - * Handles the command execution for Metalakes based on command type and the command line options. - */ - private void handleMetalakeCommand() { - String url = getUrl(); - String auth = getAuth(); - String userName = line.getOptionValue(GravitinoOptions.LOGIN); - FullName name = new FullName(line); - String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); - - Command.setAuthenticationMode(auth, userName); - - if (CommandActions.LIST.equals(command)) { - newListMetalakes(url, ignore, outputFormat).validate().handle(); - return; - } - - String metalake = name.getMetalakeName(); - - switch (command) { - case CommandActions.DETAILS: - if (line.hasOption(GravitinoOptions.AUDIT)) { - newMetalakeAudit(url, ignore, metalake).validate().handle(); - } else { - newMetalakeDetails(url, ignore, outputFormat, metalake).validate().handle(); - } - break; - - case CommandActions.CREATE: - String comment = line.getOptionValue(GravitinoOptions.COMMENT); - newCreateMetalake(url, ignore, metalake, comment).validate().handle(); - break; - - case CommandActions.DELETE: - boolean force = line.hasOption(GravitinoOptions.FORCE); - newDeleteMetalake(url, ignore, force, metalake).validate().handle(); - break; - - case CommandActions.SET: - String property = line.getOptionValue(GravitinoOptions.PROPERTY); - String value = line.getOptionValue(GravitinoOptions.VALUE); - newSetMetalakeProperty(url, ignore, metalake, property, value).validate().handle(); - break; - - case CommandActions.REMOVE: - property = line.getOptionValue(GravitinoOptions.PROPERTY); - newRemoveMetalakeProperty(url, ignore, metalake, property).validate().handle(); - break; - - case CommandActions.PROPERTIES: - newListMetalakeProperties(url, ignore, metalake).validate().handle(); - break; - - case CommandActions.UPDATE: - if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) { - System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE); - Main.exit(-1); - } - if (line.hasOption(GravitinoOptions.ENABLE)) { - boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL); - newMetalakeEnable(url, ignore, metalake, enableAllCatalogs).validate().handle(); - } - if (line.hasOption(GravitinoOptions.DISABLE)) { - newMetalakeDisable(url, ignore, metalake).validate().handle(); - } - - if (line.hasOption(GravitinoOptions.COMMENT)) { - comment = line.getOptionValue(GravitinoOptions.COMMENT); - newUpdateMetalakeComment(url, ignore, metalake, comment).validate().handle(); - } - if (line.hasOption(GravitinoOptions.RENAME)) { - String newName = line.getOptionValue(GravitinoOptions.RENAME); - force = line.hasOption(GravitinoOptions.FORCE); - newUpdateMetalakeName(url, ignore, force, metalake, newName).validate().handle(); - } - - break; - - default: - System.err.println(ErrorMessages.UNSUPPORTED_COMMAND); - Main.exit(-1); - break; - } - } - /** * Handles the command execution for Catalogs based on command type and the command line options. */ diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java new file mode 100644 index 00000000000..3ac522746ee --- /dev/null +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java @@ -0,0 +1,229 @@ +/* + * 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.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.gravitino.cli.commands.Command; + +/** + * Handles the command execution for Metalakes based on command type and the command line options. + */ +public class MetalakeCommandHandler extends CommandHandler { + + private final GravitinoCommandLine gravitinoCommandLine; + private final CommandLine line; + private final String command; + private final boolean ignore; + private final String url; + + /** + * Constructs a MetalakeCommandHandler instance. + * + * @param gravitinoCommandLine The Gravitino command line instance. + * @param line The command line options. + * @param command The command to execute. + * @param ignore Ignore server version mismatch. + */ + public MetalakeCommandHandler( + GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) { + this.gravitinoCommandLine = gravitinoCommandLine; + this.line = line; + this.command = command; + this.ignore = ignore; + this.url = getUrl(line); + } + + /** Handles the command execution logic based on the provided command. */ + public void handle() { + String userName = line.getOptionValue(GravitinoOptions.LOGIN); + FullName name = new FullName(line); + Command.setAuthenticationMode(getAuth(line), userName); + + if (CommandActions.LIST.equals(command)) { + handleListCommand(); + return; + } + + String metalake = name.getMetalakeName(); + + if (!executeCommand(metalake)) { + System.err.println(ErrorMessages.UNSUPPORTED_COMMAND); + Main.exit(-1); + } + } + + /** + * Executes the specific command based on the command type. + * + * @param metalake the name of the metalake + * @return true if the command is supported, false otherwise + */ + private boolean executeCommand(String metalake) { + switch (command) { + case CommandActions.DETAILS: + handleDetailsCommand(metalake); + return true; + + case CommandActions.CREATE: + handleCreateCommand(metalake); + return true; + + case CommandActions.DELETE: + handleDeleteCommand(metalake); + return true; + + case CommandActions.SET: + handleSetCommand(metalake); + return true; + + case CommandActions.REMOVE: + handleRemoveCommand(metalake); + return true; + + case CommandActions.PROPERTIES: + handlePropertiesCommand(metalake); + return true; + + case CommandActions.UPDATE: + handleUpdateCommand(metalake); + return true; + + default: + return false; + } + } + + /** Handles the "LIST" command. */ + private void handleListCommand() { + String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); + gravitinoCommandLine.newListMetalakes(url, ignore, outputFormat).validate().handle(); + } + + /** + * Handles the "DETAILS" command. + * + * @param metalake the name of the metalake + */ + private void handleDetailsCommand(String metalake) { + if (line.hasOption(GravitinoOptions.AUDIT)) { + gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle(); + } else { + String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); + gravitinoCommandLine + .newMetalakeDetails(url, ignore, outputFormat, metalake) + .validate() + .handle(); + } + } + + /** + * Handles the "CREATE" command. + * + * @param metalake the name of the metalake + */ + private void handleCreateCommand(String metalake) { + String comment = line.getOptionValue(GravitinoOptions.COMMENT); + gravitinoCommandLine.newCreateMetalake(url, ignore, metalake, comment).validate().handle(); + } + + /** + * Handles the "DELETE" command. + * + * @param metalake the name of the metalake + */ + private void handleDeleteCommand(String metalake) { + boolean force = line.hasOption(GravitinoOptions.FORCE); + gravitinoCommandLine.newDeleteMetalake(url, ignore, force, metalake).validate().handle(); + } + + /** + * Handles the "SET" command. + * + * @param metalake the name of the metalake + */ + private void handleSetCommand(String metalake) { + String property = line.getOptionValue(GravitinoOptions.PROPERTY); + String value = line.getOptionValue(GravitinoOptions.VALUE); + gravitinoCommandLine + .newSetMetalakeProperty(url, ignore, metalake, property, value) + .validate() + .handle(); + } + + /** + * Handles the "REMOVE" command. + * + * @param metalake the name of the metalake + */ + private void handleRemoveCommand(String metalake) { + String property = line.getOptionValue(GravitinoOptions.PROPERTY); + gravitinoCommandLine + .newRemoveMetalakeProperty(url, ignore, metalake, property) + .validate() + .handle(); + } + + /** + * Handles the "PROPERTIES" command. + * + * @param metalake the name of the metalake + */ + private void handlePropertiesCommand(String metalake) { + gravitinoCommandLine.newListMetalakeProperties(url, ignore, metalake).validate().handle(); + } + + /** + * Handles the "UPDATE" command. + * + * @param metalake the name of the metalake + */ + private void handleUpdateCommand(String metalake) { + if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) { + System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE); + Main.exit(-1); + } + if (line.hasOption(GravitinoOptions.ENABLE)) { + boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL); + gravitinoCommandLine + .newMetalakeEnable(url, ignore, metalake, enableAllCatalogs) + .validate() + .handle(); + } + if (line.hasOption(GravitinoOptions.DISABLE)) { + gravitinoCommandLine.newMetalakeDisable(url, ignore, metalake).validate().handle(); + } + + if (line.hasOption(GravitinoOptions.COMMENT)) { + String comment = line.getOptionValue(GravitinoOptions.COMMENT); + gravitinoCommandLine + .newUpdateMetalakeComment(url, ignore, metalake, comment) + .validate() + .handle(); + } + if (line.hasOption(GravitinoOptions.RENAME)) { + String newName = line.getOptionValue(GravitinoOptions.RENAME); + boolean force = line.hasOption(GravitinoOptions.FORCE); + gravitinoCommandLine + .newUpdateMetalakeName(url, ignore, force, metalake, newName) + .validate() + .handle(); + } + } +} From fc50807097312b27764e6edf5521d804772e2b45 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Wed, 8 Jan 2025 14:12:53 +1100 Subject: [PATCH 2/3] fix comment --- .../java/org/apache/gravitino/cli/MetalakeCommandHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java index 3ac522746ee..27a8ee96283 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java @@ -37,7 +37,7 @@ public class MetalakeCommandHandler extends CommandHandler { * Constructs a MetalakeCommandHandler instance. * * @param gravitinoCommandLine The Gravitino command line instance. - * @param line The command line options. + * @param line The command line arguments. * @param command The command to execute. * @param ignore Ignore server version mismatch. */ From 09db692873f71a2ef5e21a02f166ba63a98c3e50 Mon Sep 17 00:00:00 2001 From: Justin Mclean Date: Thu, 9 Jan 2025 17:47:54 +1100 Subject: [PATCH 3/3] no need to pass metalake around --- .../gravitino/cli/MetalakeCommandHandler.java | 78 ++++++------------- 1 file changed, 25 insertions(+), 53 deletions(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java index 27a8ee96283..993116f19f5 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java @@ -32,6 +32,7 @@ public class MetalakeCommandHandler extends CommandHandler { private final String command; private final boolean ignore; private final String url; + private String metalake; /** * Constructs a MetalakeCommandHandler instance. @@ -61,9 +62,9 @@ public void handle() { return; } - String metalake = name.getMetalakeName(); + metalake = name.getMetalakeName(); - if (!executeCommand(metalake)) { + if (!executeCommand()) { System.err.println(ErrorMessages.UNSUPPORTED_COMMAND); Main.exit(-1); } @@ -72,37 +73,36 @@ public void handle() { /** * Executes the specific command based on the command type. * - * @param metalake the name of the metalake * @return true if the command is supported, false otherwise */ - private boolean executeCommand(String metalake) { + private boolean executeCommand() { switch (command) { case CommandActions.DETAILS: - handleDetailsCommand(metalake); + handleDetailsCommand(); return true; case CommandActions.CREATE: - handleCreateCommand(metalake); + handleCreateCommand(); return true; case CommandActions.DELETE: - handleDeleteCommand(metalake); + handleDeleteCommand(); return true; case CommandActions.SET: - handleSetCommand(metalake); + handleSetCommand(); return true; case CommandActions.REMOVE: - handleRemoveCommand(metalake); + handleRemoveCommand(); return true; case CommandActions.PROPERTIES: - handlePropertiesCommand(metalake); + handlePropertiesCommand(); return true; case CommandActions.UPDATE: - handleUpdateCommand(metalake); + handleUpdateCommand(); return true; default: @@ -116,12 +116,8 @@ private void handleListCommand() { gravitinoCommandLine.newListMetalakes(url, ignore, outputFormat).validate().handle(); } - /** - * Handles the "DETAILS" command. - * - * @param metalake the name of the metalake - */ - private void handleDetailsCommand(String metalake) { + /** Handles the "DETAILS" command. */ + private void handleDetailsCommand() { if (line.hasOption(GravitinoOptions.AUDIT)) { gravitinoCommandLine.newMetalakeAudit(url, ignore, metalake).validate().handle(); } else { @@ -133,32 +129,20 @@ private void handleDetailsCommand(String metalake) { } } - /** - * Handles the "CREATE" command. - * - * @param metalake the name of the metalake - */ - private void handleCreateCommand(String metalake) { + /** Handles the "CREATE" command. */ + private void handleCreateCommand() { String comment = line.getOptionValue(GravitinoOptions.COMMENT); gravitinoCommandLine.newCreateMetalake(url, ignore, metalake, comment).validate().handle(); } - /** - * Handles the "DELETE" command. - * - * @param metalake the name of the metalake - */ - private void handleDeleteCommand(String metalake) { + /** Handles the "DELETE" command. */ + private void handleDeleteCommand() { boolean force = line.hasOption(GravitinoOptions.FORCE); gravitinoCommandLine.newDeleteMetalake(url, ignore, force, metalake).validate().handle(); } - /** - * Handles the "SET" command. - * - * @param metalake the name of the metalake - */ - private void handleSetCommand(String metalake) { + /** Handles the "SET" command. */ + private void handleSetCommand() { String property = line.getOptionValue(GravitinoOptions.PROPERTY); String value = line.getOptionValue(GravitinoOptions.VALUE); gravitinoCommandLine @@ -167,12 +151,8 @@ private void handleSetCommand(String metalake) { .handle(); } - /** - * Handles the "REMOVE" command. - * - * @param metalake the name of the metalake - */ - private void handleRemoveCommand(String metalake) { + /** Handles the "REMOVE" command. */ + private void handleRemoveCommand() { String property = line.getOptionValue(GravitinoOptions.PROPERTY); gravitinoCommandLine .newRemoveMetalakeProperty(url, ignore, metalake, property) @@ -180,21 +160,13 @@ private void handleRemoveCommand(String metalake) { .handle(); } - /** - * Handles the "PROPERTIES" command. - * - * @param metalake the name of the metalake - */ - private void handlePropertiesCommand(String metalake) { + /** Handles the "PROPERTIES" command. */ + private void handlePropertiesCommand() { gravitinoCommandLine.newListMetalakeProperties(url, ignore, metalake).validate().handle(); } - /** - * Handles the "UPDATE" command. - * - * @param metalake the name of the metalake - */ - private void handleUpdateCommand(String metalake) { + /** Handles the "UPDATE" command. */ + private void handleUpdateCommand() { if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) { System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE); Main.exit(-1);