-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Application Plugin Interface #7473
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
presto-main/src/main/java/com/facebook/presto/application/ApplicationManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.application; | ||
|
||
import com.facebook.presto.spi.application.Application; | ||
import com.facebook.presto.spi.application.ApplicationFactory; | ||
import com.facebook.presto.spi.classloader.ThreadContextClassLoader; | ||
import com.google.inject.Injector; | ||
import io.airlift.log.Logger; | ||
|
||
import javax.annotation.PreDestroy; | ||
import javax.annotation.concurrent.GuardedBy; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@ThreadSafe | ||
public class ApplicationManager | ||
{ | ||
private static final Logger log = Logger.get(ApplicationManager.class); | ||
|
||
@GuardedBy("this") | ||
private final Map<String, ApplicationFactory> applicationFactories = new ConcurrentHashMap<>(); | ||
@GuardedBy("this") | ||
private final Map<String, Application> applications = new ConcurrentHashMap<>(); | ||
|
||
private final AtomicBoolean stopped = new AtomicBoolean(); | ||
|
||
public synchronized void addApplicationFactory(ApplicationFactory applicationFactory) | ||
{ | ||
requireNonNull(applicationFactory, "applicationFactory is null"); | ||
|
||
if (applicationFactories.putIfAbsent(applicationFactory.getName(), applicationFactory) != null) { | ||
throw new IllegalArgumentException(format("Application '%s' is already registered", applicationFactory.getName())); | ||
} | ||
} | ||
|
||
@PreDestroy | ||
public void stop() | ||
{ | ||
if (stopped.getAndSet(true)) { | ||
return; | ||
} | ||
|
||
for (Map.Entry<String, Application> entry : applications.entrySet()) { | ||
Application application = entry.getValue(); | ||
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(application.getClass().getClassLoader())) { | ||
application.shutdown(); | ||
} | ||
catch (Throwable t) { | ||
log.error(t, "Error shutting down application: %s", entry.getKey()); | ||
} | ||
} | ||
} | ||
|
||
protected synchronized void setup(String applicationName, ApplicationFactory applicationFactory, Map<String, String> properties) | ||
{ | ||
checkState(!stopped.get(), "ApplicationManager is stopped"); | ||
checkState(!applications.containsKey(applicationName), "Application %s already exists", applicationName); | ||
try { | ||
applications.put(applicationName, applicationFactory.create(properties)); | ||
} | ||
catch (Throwable t) { | ||
log.error(t, "Error initializing application: %s", applicationFactory.getName()); | ||
} | ||
} | ||
|
||
public void setup(String applicationName, Map<String, String> properties) | ||
{ | ||
checkState(!stopped.get(), "ApplicationManager is stopped"); | ||
ApplicationFactory applicationFactory = applicationFactories.get(applicationName); | ||
checkArgument(applicationFactory != null, "No factory for application %s", applicationName); | ||
setup(applicationName, applicationFactory, properties); | ||
} | ||
|
||
protected void start(Injector injector) | ||
{ | ||
for (Map.Entry<String, Application> entry : applications.entrySet()) { | ||
Application application = entry.getValue(); | ||
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(application.getClass().getClassLoader())) { | ||
application.run(injector); | ||
} | ||
catch (Throwable t) { | ||
log.error(t, "Error starting application: %s", entry.getKey()); | ||
} | ||
} | ||
} | ||
|
||
public void startApplications(Injector injector) | ||
{ | ||
requireNonNull(injector, "injector is null"); | ||
checkState(!stopped.get(), "ApplicationManager is stopped"); | ||
start(injector); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
presto-main/src/main/java/com/facebook/presto/application/ApplicationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.application; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Module; | ||
import com.google.inject.Scopes; | ||
|
||
public class ApplicationModule | ||
implements Module | ||
{ | ||
@Override | ||
public void configure(Binder binder) | ||
{ | ||
binder.bind(ApplicationManager.class).in(Scopes.SINGLETON); | ||
binder.bind(StaticApplicationStore.class).in(Scopes.SINGLETON); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
presto-main/src/main/java/com/facebook/presto/application/StaticApplicationStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.application; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airlift.log.Logger; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.google.common.collect.Maps.fromProperties; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class StaticApplicationStore | ||
{ | ||
private static final Logger log = Logger.get(StaticApplicationStore.class); | ||
private static final File APPLICATION_CONFIGURATION_DIR = new File("etc/application/"); | ||
private final ApplicationManager applicationManager; | ||
private final AtomicBoolean applicationsLoading = new AtomicBoolean(); | ||
private final AtomicBoolean applicationsLoaded = new AtomicBoolean(); | ||
|
||
@Inject | ||
public StaticApplicationStore(ApplicationManager applicationManager) | ||
{ | ||
this.applicationManager = requireNonNull(applicationManager, "applicationManager is null"); | ||
} | ||
|
||
public boolean areApplicationsLoaded() | ||
{ | ||
return applicationsLoaded.get(); | ||
} | ||
|
||
public void loadApplications() | ||
throws Exception | ||
{ | ||
if (!applicationsLoading.compareAndSet(false, true)) { | ||
return; | ||
} | ||
|
||
for (File file : listFiles(APPLICATION_CONFIGURATION_DIR)) { | ||
if (file.isFile() && file.getName().endsWith(".properties")) { | ||
loadApplication(file); | ||
} | ||
} | ||
|
||
applicationsLoaded.set(true); | ||
} | ||
|
||
private void loadApplication(File file) | ||
throws Exception | ||
{ | ||
log.info("-- Loading application %s --", file); | ||
Map<String, String> properties = new HashMap<>(loadProperties(file)); | ||
|
||
String applicationName = properties.remove("application.name"); | ||
checkState(applicationName != null, "Application configuration %s does not contain application.name", file.getAbsoluteFile()); | ||
|
||
applicationManager.setup(applicationName, ImmutableMap.copyOf(properties)); | ||
log.info("-- Added application %s --", applicationName); | ||
} | ||
|
||
private static List<File> listFiles(File installedPluginsDir) | ||
{ | ||
if (installedPluginsDir != null && installedPluginsDir.isDirectory()) { | ||
File[] files = installedPluginsDir.listFiles(); | ||
if (files != null) { | ||
return ImmutableList.copyOf(files); | ||
} | ||
} | ||
return ImmutableList.of(); | ||
} | ||
|
||
private static Map<String, String> loadProperties(File file) | ||
throws Exception | ||
{ | ||
requireNonNull(file, "file is null"); | ||
|
||
Properties properties = new Properties(); | ||
try (FileInputStream in = new FileInputStream(file)) { | ||
properties.load(in); | ||
} | ||
return fromProperties(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
presto-spi/src/main/java/com/facebook/presto/spi/application/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.spi.application; | ||
|
||
public interface Application | ||
extends Runnable | ||
{ | ||
default void run(Object context) | ||
{ | ||
run(); | ||
} | ||
|
||
default void shutdown() | ||
{ | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
Injector
here won't work, since plugins can't see Presto's Guice from inside their class loader, by design. We intentionally do not expose the internal details of Presto to plugins. Anything needed by a plugin service should be explicitly part of the SPI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about setting couple context groups as different ApplicationContexts in SPI just like ConnectorContext and initialize them in ApplicationManager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a "context group"? I feel like I'm missing a lot of context around the purpose of this pull request. Can you explain more about the use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I suppose to bind UI on the port other than the API port here: #7106. In that PR, I just add a new module inside the PrestoServer and rebind the instance in the initialized injector to another Bootstrap, which looks hacky. I feel it would be reasonable adding an application plugin which runs on top of presto-main, and potentially passing some context instances from the initialized injector in some way. I agree that it shouldn't pass the whole injector and it shouldn't expose all internal instance to plugins.