Skip to content

Commit

Permalink
Remove AgentSPI (apache#22999)
Browse files Browse the repository at this point in the history
* Remove AgentSPI

* Refactor PluginBootServiceRegistry

* Update PluginBootService
  • Loading branch information
terrymanu authored Dec 20, 2022
1 parent c514769 commit 5683895
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 105 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
* limitations under the License.
*/

package org.apache.shardingsphere.agent.spi.plugin;
package org.apache.shardingsphere.agent.spi;

import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.spi.AgentSPI;

/**
* Plugin boot service that the lifecycle is from the agent start to shutdown.
*/
public interface PluginBootService extends AgentSPI, AutoCloseable {
public interface PluginBootService extends AutoCloseable {

/**
* Start plugin boot service.
Expand All @@ -32,4 +31,11 @@ public interface PluginBootService extends AgentSPI, AutoCloseable {
* @param isEnhancedForProxy is enhanced for proxy
*/
void start(PluginConfiguration pluginConfig, boolean isEnhancedForProxy);

/**
* Get plugin type.
*
* @return plugin type
*/
String getType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.core.logging.LoggerFactory;
import org.apache.shardingsphere.agent.core.logging.LoggerFactory.Logger;
import org.apache.shardingsphere.agent.core.spi.AgentSPIRegistry;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.core.spi.PluginBootServiceRegistry;

import java.io.IOException;
import java.util.Collection;
Expand All @@ -50,7 +49,7 @@ public static void startAllServices(final Map<String, PluginConfiguration> plugi
try {
Thread.currentThread().setContextClassLoader(classLoader);
for (Entry<String, PluginConfiguration> entry : pluginConfigs.entrySet()) {
AgentSPIRegistry.getRegisteredService(PluginBootService.class, entry.getKey()).ifPresent(optional -> {
PluginBootServiceRegistry.getRegisteredService(entry.getKey()).ifPresent(optional -> {
try {
LOGGER.info("Start plugin: {}", optional.getType());
optional.start(entry.getValue(), isEnhancedForProxy);
Expand All @@ -72,7 +71,7 @@ public static void startAllServices(final Map<String, PluginConfiguration> plugi
* @param pluginJars plugin jars
*/
public static void closeAllServices(final Collection<PluginJar> pluginJars) {
AgentSPIRegistry.getAllRegisteredServices(PluginBootService.class).forEach(each -> {
PluginBootServiceRegistry.getAllRegisteredServices().forEach(each -> {
try {
each.close();
// CHECKSTYLE:OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.apache.shardingsphere.agent.core.plugin.PluginJar;
import org.apache.shardingsphere.agent.core.plugin.yaml.loader.YamlAdvisorsConfigurationLoader;
import org.apache.shardingsphere.agent.core.plugin.yaml.swapper.YamlAdvisorsConfigurationSwapper;
import org.apache.shardingsphere.agent.core.spi.PluginServiceLoader;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.core.spi.PluginBootServiceRegistry;
import org.apache.shardingsphere.agent.spi.PluginBootService;

import java.io.InputStream;
import java.util.Collection;
Expand All @@ -47,12 +47,12 @@ public final class AdvisorConfigurationLoader {
* @param pluginJars plugin jars
* @param pluginTypes plugin types
* @param isEnhancedForProxy is enhanced for proxy
* @return loaded advisor configurations
* @return loaded configurations
*/
public static Map<String, AdvisorConfiguration> load(final Collection<PluginJar> pluginJars, final Collection<String> pluginTypes, final boolean isEnhancedForProxy) {
Map<String, AdvisorConfiguration> result = new HashMap<>();
AgentClassLoader.init(pluginJars);
for (PluginBootService each : PluginServiceLoader.newServiceInstances(PluginBootService.class, AgentClassLoader.getClassLoader())) {
for (PluginBootService each : PluginBootServiceRegistry.newInstances(AgentClassLoader.getClassLoader())) {
if (pluginTypes.contains(each.getType())) {
Collection<AdvisorConfiguration> advisorConfigs = YamlAdvisorsConfigurationSwapper
.swapToObject(YamlAdvisorsConfigurationLoader.load(getAdvisorsResourceStream(each, isEnhancedForProxy)), each.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,49 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.agent.spi.AgentSPI;
import org.apache.shardingsphere.agent.spi.PluginBootService;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import java.util.ServiceLoader;

/**
* Agent SPI registry.
* Plugin boot service registry.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AgentSPIRegistry {
public final class PluginBootServiceRegistry {

/**
* Get registered service.
*
* @param typedSPIClass typed SPI class
* @param type type
* @param <T> type of agent typed SPI
* @return registered service
*/
public static <T extends AgentSPI> Optional<T> getRegisteredService(final Class<T> typedSPIClass, final String type) {
return AgentServiceLoader.getServiceLoader(typedSPIClass).newServiceInstances().stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
public static Optional<PluginBootService> getRegisteredService(final String type) {
return AgentServiceLoader.getServiceLoader(PluginBootService.class).newServiceInstances().stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
}

/**
* Get all registered services.
*
* @param typedSPIClass typed SPI class
* @param <T> type of agent typed SPI
* @return registered services
*/
public static <T extends AgentSPI> Collection<T> getAllRegisteredServices(final Class<T> typedSPIClass) {
return AgentServiceLoader.getServiceLoader(typedSPIClass).newServiceInstances();
public static Collection<PluginBootService> getAllRegisteredServices() {
return AgentServiceLoader.getServiceLoader(PluginBootService.class).newServiceInstances();
}

/**
* Create new instances.
*
* @param classLoader class loader
* @return created instances
*/
public static Collection<PluginBootService> newInstances(final ClassLoader classLoader) {
Collection<PluginBootService> result = new LinkedList<>();
for (PluginBootService each : ServiceLoader.load(PluginBootService.class, classLoader)) {
result.add(each);
}
return result;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.plugin.logging.base;

import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;

/**
* Base logging plugin boot service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.shardingsphere.agent.metrics.prometheus.collector.MetaDataInfoCollector;
import org.apache.shardingsphere.agent.metrics.prometheus.collector.ProxyInfoCollector;
import org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.opentracing.util.GlobalTracer;
import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.core.config.validator.RemotePluginConfigurationValidator;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkAutoConfiguration;
import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;

public class OpenTelemetryTracingPluginBootService implements PluginBootService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;

/**
* Open tracing plugin boot service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import brave.sampler.Sampler;
import org.apache.shardingsphere.agent.config.plugin.PluginConfiguration;
import org.apache.shardingsphere.agent.core.config.validator.RemotePluginConfigurationValidator;
import org.apache.shardingsphere.agent.spi.plugin.PluginBootService;
import org.apache.shardingsphere.agent.spi.PluginBootService;
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
import zipkin2.reporter.okhttp3.OkHttpSender;

Expand Down

0 comments on commit 5683895

Please sign in to comment.