apache / pulsar

Apache Pulsar - distributed pub-sub messaging system
https://pulsar.apache.org/
Apache License 2.0
14.23k stars 3.58k forks source link

[PIP-138] Use ``NarClassLoaderManager`` to manage all NarClassLoader. #13979

Closed mattisonchao closed 2 years ago

mattisonchao commented 2 years ago

Motivation

According to PIP-41 Pluggable Protocol Handler, we can use a pluggable protocol handler to process more than one protocol.
And then #11498 support we can use additional servelet to expose some REST API to help use expose metrics, monitors, etc. Some pluggable components like ProtocolHanlder, Interceptor, AdditionalServelet all use NarClassLoader to load Nar package. For isolation reasons, different types of plugins use different loaders.

Currently, when we want to use AddtionalServelet in ProtocolHandler. the awkward things happen.

AdditionalServelet and ProtocolHanlder at same Nar package, but pulsar use different NarClassloader to load it. If we want to get some ProtocolHanlder metrics in AdditionalServelt, we will get ClassCastException.

for this reason, I write this PIP want to use NarClassLoader Manager to manage all NarClassLoader. If the Nar Packet Path, additional jars, and parent loader are the same, we will use the same loader.

I think it's safe and more convenient.

Goal

Different Plugin Components have the same Nar package path( means at the same project ). Use the same NarClassLoader to load.

API Changes

without API change.

Implementation

@Data
public class NarClassLoaderIdentifier {
    private String path;
    private Set<String> additionalJars;
    private ClassLoader parentLoader;
}

public class NarClassLoaderManager {

    public static NarClassLoader getFromArchive(File narPath, Set<String> additionalJars,
                                                String narExtractionDirectory){}

    public static NarClassLoader getFromArchive(File narPath, Set<String> additionalJars){}

    public static NarClassLoader getFromArchive(File narPath, Set<String> additionalJars, ClassLoader parent,
                                                String narExtractionDirectory){}
}

Reject Alternatives

waiting for.

eolivelli commented 2 years ago

I think it's safe and more convenient. It is not safe because you are not sure that the contents of the NAR package are designed to work well this way. In fact you are going to share "static" variables.

There is not concept of "application" or "bundle" in Pulsar, like the concept of "Enterprise Application" or "Web Application" in a JakartaEE containers. If you want to go this way we have to formally "link" the two components using some descriptor or configuration option and tell Pulsar to use the same "Context".