-
Notifications
You must be signed in to change notification settings - Fork 378
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
[Improvement] Refactor the validation logic in the handle methods #5861
Comments
Hi, @justinmclean @tengqm @xunliu @jerryshao , could you plz help to see if this design is reasonable when you have some time. |
I proposed a similar solution in PR #5688 @jerryshao thought that building command maps was too heavy. There is an outstanding PR to remove some of the if/else logic and use switch/cases instead. (PR #5793) |
Yes. My favorite is the simple naive implementation using switch/case statements. |
@justinmclean, @tengqm Can we combine both approaches(#5793 ) by using a switch/case structure for logical decisions, while also creating a Data class object to handle parameter validation and other logic operations?" |
@justinmclean @tengqm Another concern is the frequent issues with changing error messages. The root cause of this lies in parameter validation not being performed before executing specific operations. As a result, methods like |
100% support from my side. |
I think it ok, The big logic branch uses switch/case, and The detailed logic (parameter validation) uses Data class object. @justinmclean What's do you thnk? |
My concern is that @jerryshao would prefer a different approach and it does add a lot of complexity, new methods and new objects. A better way, I think, would be to add a verify() method to commands and have that check arguments if needed. Note that only a couple of commands need to do this, not all of them. The CLI library does a lot of the work for us, so we don't need to duplicate what it does. |
Hi @justinmclean , Can I draft a version first? I’d like to build on #5793 and submit a draft PR to show how it works. |
This is what is would look like. Add a verify method to the base
(we may not need to check for metalake) in a command that needs to check table names add this:
When calling the command chain verify and handle together:
|
Yep there is no need to check for metalake in the above code, as that is already done, and there is probably no need to check for catalog as name must have a value at this point, which means catalog is always set, so that method only needs schema and table passed to it to check. |
@justinmclean Should we throw an exception in the |
We would need to throw an exception so that no code in handle is called. The exception message can give the reason why. |
hi, @justinmclean Adding the
@shaofengshi could you plz help to see? |
You do not have to modify all subclasses. Remember, the CLI library does a lot of the work for you before you get to this point, and there is no need to check for things that have already been checked. There should not be excessive duplication if you place common validation in methods in the |
hi, @justinmclean Has the modification of the Command been completed? Specifically, has the verify method been added, or is someone else currently working on it? If not, could this task be assigned to me? BTW, can this PR #5793 merged? |
It needs to be reviewed before it can be merged, I can't review or merge it as I wrote the code. |
Got it. The modification of Command depends on #5793. This PR is highly valuable, and I hope it gets merged soon. |
…handle methods refactor the validation logic of all entities and add test case.
…handle methods fix typo.
What would you like to be improved?
Current Issues
executeCommand
method contains a large number of if-else structures, which leads to poor readability and scalability.handleXXXCommand
) suffer from the same problem.handleXXXCommand
layer.How should we improve?
Resolve the issue of excessive if-else statements in the executeCommand method.
Start by refactoring the
handleRoleCommand
method to improve the CLI code. To resolve the issue of excessive if-else in theexecuteCommand
method, refer to this pull request #5688 . We can create a Map to store the execution method corresponding to each entity. e.g.Resolve the issue of excessive if-else statements in parameter validation and specific command execution.
Similarly, construct a Map to store the execution method corresponding to each Command, for example:
In the original processing logic, the steps can be simplified as follows:
One entity processing method will only call one command handling method. Therefore, is it possible to create a data class
Role
with two purposes:and in GravitinoCommandLine, use a variable to store the Role instance,current design as follows:
BaseEntity
The base class for all entities, designed to store common methods.
Role
Used for performing checks related to the Role entity.
Key attributes
actionCheckMap
: A dictionary that stores command validation methods.entityArgMap
: Stores field names and values, for example,"METALAKE": metalake value
.key methods
public boolean checkArguments(String action):
Checks whether the necessary arguments for the corresponding operation are defined.Based on the design above, the related operations for the Role entity can be simplified to the following code:
If a new command, such as
remove
, is supported for the role in the future, the expansion steps would be as follows:checkRemoveArguments
method fromBaseEntity
class to define the remove check logichandleRoleRemoveCommand
method inGravitinoCommandLine
.handleRoleRemoveCommand
to the roleCommandMap.The text was updated successfully, but these errors were encountered: