You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/08/06 08:10:28 UTC

[shardingsphere] branch master updated: Refactor proxy init and start (#11678)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 6472662  Refactor proxy init and start (#11678)
6472662 is described below

commit 6472662b190cf5014ddfbd826a818d9c439d04e9
Author: Dachuan J <46...@users.noreply.github.com>
AuthorDate: Fri Aug 6 16:10:01 2021 +0800

    Refactor proxy init and start (#11678)
    
    * Refactor ShardingSphereProxy start
    
    * Refactor init scaling
---
 .../prometheus/collector/ProxyInfoCollector.java   | 12 +-------
 .../impl/AbstractBootstrapInitializer.java         |  9 ++++--
 .../impl/GovernanceBootstrapInitializer.java       |  6 ++--
 .../impl/StandardBootstrapInitializer.java         |  2 +-
 .../proxy/frontend/ShardingSphereProxy.java        | 36 +++++++++-------------
 5 files changed, 27 insertions(+), 38 deletions(-)

diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
index d4275ad..a133160 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
@@ -23,13 +23,11 @@ import org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
 import org.apache.shardingsphere.agent.metrics.api.util.MetricsUtil;
 import org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.frontend.ShardingSphereProxy;
 
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
-import java.util.Date;
 
 /**
  * Proxy info collector.
@@ -38,9 +36,7 @@ public final class ProxyInfoCollector extends Collector {
     
     private static final String PROXY_STATE = "state";
     
-    private static final String PROXY_UP_TIME = "uptime";
-    
-    private static final String PROXY_CLASS_STR = "org.apache.shardingsphere.proxy.frontend.ShardingSphereProxy";
+    private static final String PROXY_CLASS_STR = "org.apache.shardingsphere.proxy.backend.context.ProxyContext";
     
     private static final PrometheusWrapperFactory FACTORY = new PrometheusWrapperFactory();
     
@@ -53,12 +49,6 @@ public final class ProxyInfoCollector extends Collector {
         Optional<GaugeMetricFamily> proxyInfo = FACTORY.createGaugeMetricFamily(MetricIds.PROXY_INFO);
         proxyInfo.ifPresent(m -> 
                 m.addMetric(Collections.singletonList(PROXY_STATE), ProxyContext.getInstance().getStateContext().getCurrentState().ordinal()));
-        Date now = new Date();
-        if (0 >= ShardingSphereProxy.getStartTime()) {
-            proxyInfo.ifPresent(m -> m.addMetric(Collections.singletonList(PROXY_UP_TIME), 0));
-        } else {
-            proxyInfo.ifPresent(m -> m.addMetric(Collections.singletonList(PROXY_UP_TIME), now.getTime() - ShardingSphereProxy.getStartTime()));
-        }
         proxyInfo.ifPresent(result::add);
         return result;
     }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
index 2891ce7..9d5d344 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
@@ -84,7 +84,7 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
         TransactionContexts transactionContexts = decorateTransactionContexts(createTransactionContexts(metaDataContexts), xaTransactionMangerType);
         ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
         setDatabaseServerInfo();
-        initScalingWorker(yamlConfig);
+        initScalingInternal(yamlConfig);
     }
     
     private ProxyConfiguration getProxyConfiguration(final YamlProxyConfiguration yamlConfig) {
@@ -161,13 +161,18 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
         return Optional.of(result);
     }
     
+    private void initScalingInternal(final YamlProxyConfiguration yamlConfig) {
+        log.debug("Init scaling");
+        initScaling(yamlConfig);
+    }
+    
     protected abstract boolean isOverwrite(PreConditionRuleConfiguration ruleConfig);
     
     protected abstract MetaDataContexts decorateMetaDataContexts(MetaDataContexts metaDataContexts);
     
     protected abstract TransactionContexts decorateTransactionContexts(TransactionContexts transactionContexts, String xaTransactionMangerType);
     
-    protected abstract void initScalingWorker(YamlProxyConfiguration yamlConfig);
+    protected abstract void initScaling(YamlProxyConfiguration yamlConfig);
     
     protected final void persistConfigurations(final YamlProxyConfiguration yamlConfig, final boolean overwrite) {
         YamlProxyServerConfiguration serverConfig = yamlConfig.getServerConfiguration();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
index 3a6171f..5555f1f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
@@ -67,15 +67,15 @@ public final class GovernanceBootstrapInitializer extends AbstractBootstrapIniti
     }
     
     @Override
-    protected void initScalingWorker(final YamlProxyConfiguration yamlConfig) {
+    protected void initScaling(final YamlProxyConfiguration yamlConfig) {
         Optional<ServerConfiguration> scalingConfig = getScalingConfiguration(yamlConfig);
         Optional<YamlGovernanceConfiguration> governanceConfig = yamlConfig.getServerConfiguration().getRules().stream().filter(
             each -> each instanceof YamlGovernanceConfiguration).map(each -> (YamlGovernanceConfiguration) each).findFirst();
         Preconditions.checkState(governanceConfig.isPresent());
-        scalingConfig.ifPresent(optional -> initScaling(governanceConfig.get(), optional));
+        scalingConfig.ifPresent(optional -> initScalingDetails(governanceConfig.get(), optional));
     }
     
-    private void initScaling(final YamlGovernanceConfiguration governanceConfig, final ServerConfiguration scalingConfig) {
+    private void initScalingDetails(final YamlGovernanceConfiguration governanceConfig, final ServerConfiguration scalingConfig) {
         scalingConfig.setGovernanceConfig(new GovernanceConfigurationYamlSwapper().swapToObject(governanceConfig));
         ScalingContext.getInstance().init(scalingConfig);
         ScalingWorker.init();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
index 67c5eb2..77a1f68 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializer.java
@@ -50,7 +50,7 @@ public final class StandardBootstrapInitializer extends AbstractBootstrapInitial
     }
     
     @Override
-    protected void initScalingWorker(final YamlProxyConfiguration yamlConfig) {
+    protected void initScaling(final YamlProxyConfiguration yamlConfig) {
         getScalingConfiguration(yamlConfig).ifPresent(optional -> ScalingContext.getInstance().init(optional));
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
index 35907f1..3a431a4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
@@ -36,16 +36,12 @@ import org.apache.shardingsphere.proxy.backend.context.BackendExecutorContext;
 import org.apache.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer;
 import org.apache.shardingsphere.proxy.frontend.protocol.FrontDatabaseProtocolTypeFactory;
 
-import java.util.Date;
-
 /**
  * ShardingSphere-Proxy.
  */
 @Slf4j
 public final class ShardingSphereProxy {
-    
-    private static long startTime;
-    
+
     private EventLoopGroup bossGroup;
     
     private EventLoopGroup workerGroup;
@@ -58,13 +54,8 @@ public final class ShardingSphereProxy {
     @SneakyThrows(InterruptedException.class)
     public void start(final int port) {
         try {
-            createEventLoopGroup();
-            ServerBootstrap bootstrap = new ServerBootstrap();
-            initServerBootstrap(bootstrap);
-            ChannelFuture future = bootstrap.bind(port).sync();
-            startTime = new Date().getTime();
-            log.info("ShardingSphere-Proxy start success");
-            future.channel().closeFuture().sync();
+            ChannelFuture future = startInternal(port);
+            accept(future);
         } finally {
             workerGroup.shutdownGracefully();
             bossGroup.shutdownGracefully();
@@ -72,6 +63,18 @@ public final class ShardingSphereProxy {
         }
     }
     
+    private ChannelFuture startInternal(final int port) throws InterruptedException {
+        createEventLoopGroup();
+        ServerBootstrap bootstrap = new ServerBootstrap();
+        initServerBootstrap(bootstrap);
+        return bootstrap.bind(port).sync();
+    }
+    
+    private void accept(final ChannelFuture future) throws InterruptedException {
+        log.info("ShardingSphere-Proxy start success");
+        future.channel().closeFuture().sync();
+    }
+    
     private void createEventLoopGroup() {
         bossGroup = Epoll.isAvailable() ? new EpollEventLoopGroup(1) : new NioEventLoopGroup(1);
         workerGroup = Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
@@ -88,13 +91,4 @@ public final class ShardingSphereProxy {
                 .handler(new LoggingHandler(LogLevel.INFO))
                 .childHandler(new ServerHandlerInitializer(FrontDatabaseProtocolTypeFactory.getDatabaseType()));
     }
-    
-    /**
-     * Get proxy start time.
-     *
-     * @return proxy start time
-     */
-    public static Long getStartTime() {
-        return startTime;
-    }
 }