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

[FLINK-37525] Introduce a overload findModuleFactory to improve the performance of installModules #26329

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

beliefer
Copy link
Contributor

@beliefer beliefer commented Mar 20, 2025

What is the purpose of the change

This PR aim to improve the performance of installModules by introduce an overload findModuleFactory.

Currently, the implementation of installModules has the logic show below.

for (String moduleFactoryClass : config.getSecurityModuleFactories()) {
    ...
    moduleFactory = SecurityFactoryServiceLoader.findModuleFactory(moduleFactoryClass);
    ...
}

The implementation of findModuleFactory show below.

    public static SecurityModuleFactory findModuleFactory(String securityModuleFactoryClass)
            throws NoMatchSecurityFactoryException {
        return findFactoryInternal(
                securityModuleFactoryClass,
                SecurityModuleFactory.class,
                SecurityModuleFactory.class.getClassLoader());
    }

You can see no matter what the securityModuleFactoryClass is, the SecurityModuleFactory.class and SecurityModuleFactory.class.getClassLoader() are certain.

The implementation of findFactoryInternal show below.

    private static <T> T findFactoryInternal(
            String factoryClassCanonicalName, Class<T> factoryClass, ClassLoader classLoader)
            throws NoMatchSecurityFactoryException {

        Preconditions.checkNotNull(factoryClassCanonicalName);

        ServiceLoader<T> serviceLoader;
        if (classLoader != null) {
            serviceLoader = ServiceLoader.load(factoryClass, classLoader);
        } else {
            serviceLoader = ServiceLoader.load(factoryClass);
        }

        List<T> matchingFactories = new ArrayList<>();
        Iterator<T> classFactoryIterator = serviceLoader.iterator();
        classFactoryIterator.forEachRemaining(
                classFactory -> {
                    if (factoryClassCanonicalName.matches(
                            classFactory.getClass().getCanonicalName())) {
                        matchingFactories.add(classFactory);
                    }
                });

        if (matchingFactories.size() != 1) {
            throw new NoMatchSecurityFactoryException(
                    "zero or more than one security factory found",
                    factoryClassCanonicalName,
                    matchingFactories);
        }
        return matchingFactories.get(0);
    }

You can see findFactoryInternal load ServiceLoader and match the classFactory.
These code lead to repeatedly loading ServiceLoader and match the classFactory.

Brief change log

Introduce a overload findModuleFactory to improve the performance of installModules

Verifying this change

This change is already covered by existing tests, such as (TestingSecurityContext).

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (no)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (no)

@flinkbot
Copy link
Collaborator

flinkbot commented Mar 20, 2025

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

Copy link
Contributor

@davidradl davidradl left a comment

Choose a reason for hiding this comment

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

Please could you fill out the Jira to indicate why this improvement is required. As it is an improvement is would be best to discuss this with the community to get their feedback as per the process

@beliefer
Copy link
Contributor Author

Please could you fill out the Jira to indicate why this improvement is required. As it is an improvement is would be best to discuss this with the community to get their feedback as per the process

I have added the description for Jira. Thank you!

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.

3 participants