Skip to content
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

Fix cast exception when running peon tasks(#16271) #16281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

soullkk
Copy link
Contributor

@soullkk soullkk commented Apr 14, 2024

Fixes #16271.

Description

When calling method CommandListBuilder.addSystemProperty, if the parameter passed in is a generic type, then the actual method being called is CommandListBuilder addSystemProperty(String property, String value). If the actual type of the generic type is Integer, a cast exception will occur. So overloaded methods with parameters such as int, long, boolean, and String need to be removed, and used Object instead to solve runtime exceptions caused by generic type.

Fixed the bug with cast exception when running peon tasks #16271.

Release note

Fix the bug of cast exception when running peon task.


Key changed/added classes in this PR
  • ForkingTaskRunner.CommandListBuilder

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@@ -909,24 +909,9 @@ public CommandListBuilder add(String arg)
return this;
}

public CommandListBuilder addSystemProperty(String property, int value)
public CommandListBuilder addSystemProperty(String property, Object value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an Object as the value of a system property is not appropriate. It must always be a primitive or a String. Hence, the different addSystemProperty() methods.

As a workaround to the original problem, you can simply pass the integer value quoted as a String. (#16271 (comment))

Copy link
Contributor Author

@soullkk soullkk Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                        if (context != null) {
                          for (String propName : context.keySet()) {
                            if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
                              command.addSystemProperty(
                                  propName.substring(CHILD_PROPERTY_PREFIX.length()),
                                  task.getContextValue(propName).toString()
                              );
                            }
                          }
                        }

How about calling the toString() method to make the public CommandListBuilder addSystemProperty (String property, String value) called

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be nice 👍🏻 . The value returned by task.getContextValue() would most likely not be null but best add a null check too before invoking the .toString().

@@ -316,7 +317,7 @@ public TaskStatus call()
if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
command.addSystemProperty(
propName.substring(CHILD_PROPERTY_PREFIX.length()),
task.getContextValue(propName)
Preconditions.checkNotNull(task.getContextValue(propName), propName).toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should throw an exception. Maybe a property was set to null on purpose. We should just use the value of null.

Maybe something like this:

if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
    final String contextValue = task.getContextValue(propName);
    command.addSystemProperty(
        propName.substring(CHILD_PROPERTY_PREFIX.length()),
        contextValue == null ? null : contextValue.toString()
    );
}    

@kfaraz
Copy link
Contributor

kfaraz commented Apr 19, 2024

@soullkk , the build is failing due to low coverage. Do you think you could add a test in ForkingTaskRunnerTest or update an existing test to cover these updated lines?

@soullkk
Copy link
Contributor Author

soullkk commented Apr 22, 2024

@soullkk , the build is failing due to low coverage. Do you think you could add a test in ForkingTaskRunnerTest or update an existing test to cover these updated lines?

ok, no problem.

command.addSystemProperty(
propName.substring(CHILD_PROPERTY_PREFIX.length()),
task.getContextValue(propName)
contextValue == null ? null : contextValue.toString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the contextValue is null, does it make sense to call the addSystemProperty method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's true. We can just skip the addSystemProperty if the value is null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MiddleManager errors while executing the peon task
3 participants