From 7b24e94bd251d80fdd0aceb37a6b4d387edd0561 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=83=AD=E6=B5=A9?= <xiangsheng.gh@alibaba-inc.com>
Date: Mon, 6 May 2019 18:14:11 +0800
Subject: [PATCH] Update service auto thrashing

---
 .gitignore                                    |   1 +
 bootstrap/pom.xml                             |  35 ---
 .../java/com/aliware/tianchi/GatewayApp.java  |  16 --
 .../java/com/aliware/tianchi/ProviderApp.java |  17 --
 internal-gateway/pom.xml                      |   8 +-
 internal-service/pom.xml                      |   5 -
 internal-service/service-consumer/pom.xml     |  38 ++-
 .../java/com/aliware/tianchi/MyConsumer.java  |   7 +-
 internal-service/service-provider/pom.xml     |  88 ++++++
 .../com/aliware/tianchi/DemoProviderMain.java |  21 ++
 .../aliware/tianchi/EmbeddedZooKeeper.java    | 254 ++++++++++++++++++
 .../com/aliware/tianchi/HashServiceImpl.java  |  50 +++-
 .../com/aliware/tianchi/ThrashConfig.java     |  21 ++
 .../src/main/resources/dubbo-provider.xml     |   8 +-
 pom.xml                                       |   1 -
 15 files changed, 474 insertions(+), 96 deletions(-)
 delete mode 100644 bootstrap/pom.xml
 delete mode 100644 bootstrap/src/main/java/com/aliware/tianchi/GatewayApp.java
 delete mode 100644 bootstrap/src/main/java/com/aliware/tianchi/ProviderApp.java
 create mode 100644 internal-service/service-provider/src/main/java/com/aliware/tianchi/DemoProviderMain.java
 create mode 100644 internal-service/service-provider/src/main/java/com/aliware/tianchi/EmbeddedZooKeeper.java
 create mode 100644 internal-service/service-provider/src/main/java/com/aliware/tianchi/ThrashConfig.java

diff --git a/.gitignore b/.gitignore
index 6762b4c..9e14772 100644
--- a/.gitignore
+++ b/.gitignore
@@ -322,4 +322,5 @@ Session.vim
 tags
 # Persistent undo
 [._]*.un~
+.flattened-pom.xml
 
diff --git a/bootstrap/pom.xml b/bootstrap/pom.xml
deleted file mode 100644
index 7311cef..0000000
--- a/bootstrap/pom.xml
+++ /dev/null
@@ -1,35 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <parent>
-        <artifactId>adaptive-loadbalance</artifactId>
-        <groupId>com.aliware.tianchi</groupId>
-        <version>1.0.0</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-
-    <artifactId>bootstrap</artifactId>
-    <dependencies>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-context</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.aliware.tianchi</groupId>
-            <artifactId>workspace-gateway</artifactId>
-            <version>1.0.0</version>
-        </dependency>
-        <dependency>
-            <groupId>com.aliware.tianchi</groupId>
-            <artifactId>workspace-provider</artifactId>
-            <version>1.0.0</version>
-        </dependency>
-        <dependency>
-            <groupId>com.aliware.tianchi</groupId>
-            <artifactId>service-provider</artifactId>
-        </dependency>
-    </dependencies>
-
-
-</project>
\ No newline at end of file
diff --git a/bootstrap/src/main/java/com/aliware/tianchi/GatewayApp.java b/bootstrap/src/main/java/com/aliware/tianchi/GatewayApp.java
deleted file mode 100644
index f3d1dec..0000000
--- a/bootstrap/src/main/java/com/aliware/tianchi/GatewayApp.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.aliware.tianchi;
-
-import java.io.IOException;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-/**
- * @author guohaoice@gmail.com
- */
-public class GatewayApp {
-  public static void main(String[] args) throws IOException {
-    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
-    context.start();
-
-    System.in.read(); // press any key to exit
-  }
-}
diff --git a/bootstrap/src/main/java/com/aliware/tianchi/ProviderApp.java b/bootstrap/src/main/java/com/aliware/tianchi/ProviderApp.java
deleted file mode 100644
index a8a203a..0000000
--- a/bootstrap/src/main/java/com/aliware/tianchi/ProviderApp.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.aliware.tianchi;
-
-import java.io.IOException;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-/**
- * @author guohaoice@gmail.com
- */
-public class ProviderApp {
-
-  public static void main(String[] args) throws IOException {
-    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-provider.xml"});
-    context.start();
-    System.in.read(); // press any key to exit
-  }
-
-}
diff --git a/internal-gateway/pom.xml b/internal-gateway/pom.xml
index bb98b5d..007289e 100644
--- a/internal-gateway/pom.xml
+++ b/internal-gateway/pom.xml
@@ -12,10 +12,10 @@
     <artifactId>internal-gateway</artifactId>
 
     <dependencies>
-        <dependency>
-            <groupId>org.apache.dubbo</groupId>
-            <artifactId>internal-dubbo</artifactId>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.apache.dubbo</groupId>-->
+<!--            <artifactId>internal-dubbo</artifactId>-->
+<!--        </dependency>-->
     </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/internal-service/pom.xml b/internal-service/pom.xml
index 11eb2df..3abf1b0 100644
--- a/internal-service/pom.xml
+++ b/internal-service/pom.xml
@@ -18,15 +18,10 @@
     </modules>
 
     <dependencies>
-        <dependency>
-            <groupId>org.apache.dubbo</groupId>
-            <artifactId>internal-dubbo</artifactId>
-        </dependency>
         <dependency>
             <groupId>com.aliware.tianchi</groupId>
             <artifactId>internal-gateway</artifactId>
             <version>${project.version}</version>
         </dependency>
     </dependencies>
-
 </project>
\ No newline at end of file
diff --git a/internal-service/service-consumer/pom.xml b/internal-service/service-consumer/pom.xml
index 81633ad..9defcf2 100644
--- a/internal-service/service-consumer/pom.xml
+++ b/internal-service/service-consumer/pom.xml
@@ -20,7 +20,43 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo</artifactId>
+            <version>2.7.0</version>
+        </dependency>
     </dependencies>
 
-
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>3.2.1</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <transformers>
+                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>com.aliware.tianchi.MyConsumer</mainClass>
+                                </transformer>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.handlers</resource>
+                                </transformer>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.schemas</resource>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 </project>
\ No newline at end of file
diff --git a/internal-service/service-consumer/src/main/java/com/aliware/tianchi/MyConsumer.java b/internal-service/service-consumer/src/main/java/com/aliware/tianchi/MyConsumer.java
index 162e5b3..3108833 100644
--- a/internal-service/service-consumer/src/main/java/com/aliware/tianchi/MyConsumer.java
+++ b/internal-service/service-consumer/src/main/java/com/aliware/tianchi/MyConsumer.java
@@ -7,13 +7,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
  * @author guohaoice@gmail.com
  */
 public class MyConsumer {
-  public static void main(String[] args) throws IOException {
+  public static void main(String[] args) throws IOException, InterruptedException {
     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-consumer.xml"});
     context.start();
     HashInterface bean = context.getBean(HashInterface.class);
-    for (int i = 0; i < 10; i++) {
+    while(true){
+      Thread.sleep(1000);
       System.out.println(bean.hash("hahaha"));
     }
-    System.in.read(); // press any key to exit
+//    System.in.read(); // press any key to exit
   }
 }
diff --git a/internal-service/service-provider/pom.xml b/internal-service/service-provider/pom.xml
index 5e390fb..e2ce342 100644
--- a/internal-service/service-provider/pom.xml
+++ b/internal-service/service-provider/pom.xml
@@ -10,14 +10,102 @@
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>service-provider</artifactId>
+    <properties>
+        <zookeeper.version>3.4.13</zookeeper.version>
+        <curator.version>4.0.1</curator.version>
+    </properties>
 
     <dependencies>
         <dependency>
             <groupId>com.aliware.tianchi</groupId>
             <artifactId>service-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.zookeeper</groupId>
+            <artifactId>zookeeper</artifactId>
+            <version>${zookeeper.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-framework</artifactId>
+            <version>${curator.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.zookeeper</groupId>
+                    <artifactId>zookeeper</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-client</artifactId>
+            <version>${curator.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.zookeeper</groupId>
+                    <artifactId>zookeeper</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-recipes</artifactId>
+            <version>${curator.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>io.netty</groupId>
+                    <artifactId>netty</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.apache.zookeeper</groupId>
+                    <artifactId>zookeeper</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
 
     </dependencies>
 
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>3.2.1</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <transformers>
+                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>com.aliware.tianchi.DemoProviderMain</mainClass>
+                                </transformer>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.handlers</resource>
+                                </transformer>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                                    <resource>META-INF/spring.schemas</resource>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>
\ No newline at end of file
diff --git a/internal-service/service-provider/src/main/java/com/aliware/tianchi/DemoProviderMain.java b/internal-service/service-provider/src/main/java/com/aliware/tianchi/DemoProviderMain.java
new file mode 100644
index 0000000..0f3deec
--- /dev/null
+++ b/internal-service/service-provider/src/main/java/com/aliware/tianchi/DemoProviderMain.java
@@ -0,0 +1,21 @@
+package com.aliware.tianchi;
+
+import java.io.IOException;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/** @author guohaoice@gmail.com */
+public class DemoProviderMain {
+  public static void main(String[] args) throws IOException {
+    //      if(args.length!=3){
+    //          throw new IllegalArgumentException("Bad argument number");
+    //      }
+
+    //      String salt=args[0];
+    //      System.out.println(salt);
+    new EmbeddedZooKeeper(2181, false).start();
+    ClassPathXmlApplicationContext context =
+        new ClassPathXmlApplicationContext(new String[] {"classpath:dubbo-provider.xml"});
+    context.start();
+    System.in.read(); // press any key to exit
+  }
+}
diff --git a/internal-service/service-provider/src/main/java/com/aliware/tianchi/EmbeddedZooKeeper.java b/internal-service/service-provider/src/main/java/com/aliware/tianchi/EmbeddedZooKeeper.java
new file mode 100644
index 0000000..b0b68e3
--- /dev/null
+++ b/internal-service/service-provider/src/main/java/com/aliware/tianchi/EmbeddedZooKeeper.java
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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.aliware.tianchi;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.Properties;
+import java.util.UUID;
+import org.apache.zookeeper.server.ServerConfig;
+import org.apache.zookeeper.server.ZooKeeperServerMain;
+import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.SmartLifecycle;
+import org.springframework.util.ErrorHandler;
+import org.springframework.util.SocketUtils;
+
+/**
+ * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
+ *
+ * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper.
+ *
+ * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for
+// * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication}
+ *
+ * @author Patrick Peralta
+ * @author Mark Fisher
+ * @author David Turanski
+ */
+public class EmbeddedZooKeeper implements SmartLifecycle {
+
+    /**
+     * Logger.
+     */
+//    private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class);
+
+    /**
+     * ZooKeeper client port. This will be determined dynamically upon startup.
+     */
+    private final int clientPort;
+
+    /**
+     * Whether to auto-start. Default is true.
+     */
+    private boolean autoStartup = true;
+
+    /**
+     * Lifecycle phase. Default is 0.
+     */
+    private int phase = 0;
+
+    /**
+     * Thread for running the ZooKeeper server.
+     */
+    private volatile Thread zkServerThread;
+
+    /**
+     * ZooKeeper server.
+     */
+    private volatile ZooKeeperServerMain zkServer;
+
+    /**
+     * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread.
+     */
+    private ErrorHandler errorHandler;
+
+    private boolean daemon = true;
+
+    /**
+     * Construct an EmbeddedZooKeeper with a random port.
+     */
+    public EmbeddedZooKeeper() {
+        clientPort = SocketUtils.findAvailableTcpPort();
+    }
+
+    /**
+     * Construct an EmbeddedZooKeeper with the provided port.
+     *
+     * @param clientPort  port for ZooKeeper server to bind to
+     */
+    public EmbeddedZooKeeper(int clientPort, boolean daemon) {
+        this.clientPort = clientPort;
+        this.daemon = daemon;
+    }
+
+    /**
+     * Returns the port that clients should use to connect to this embedded server.
+     *
+     * @return dynamically determined client port
+     */
+    public int getClientPort() {
+        return this.clientPort;
+    }
+
+    /**
+     * Specify whether to start automatically. Default is true.
+     *
+     * @param autoStartup whether to start automatically
+     */
+    public void setAutoStartup(boolean autoStartup) {
+        this.autoStartup = autoStartup;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isAutoStartup() {
+        return this.autoStartup;
+    }
+
+    /**
+     * Specify the lifecycle phase for the embedded server.
+     *
+     * @param phase the lifecycle phase
+     */
+    public void setPhase(int phase) {
+        this.phase = phase;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int getPhase() {
+        return this.phase;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isRunning() {
+        return (zkServerThread != null);
+    }
+
+    /**
+     * Start the ZooKeeper server in a background thread.
+     * <p>
+     * Register an error handler via {@link #setErrorHandler} in order to handle
+     * any exceptions thrown during startup or execution.
+     */
+    @Override
+    public synchronized void start() {
+        if (zkServerThread == null) {
+            zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter");
+            zkServerThread.setDaemon(daemon);
+            zkServerThread.start();
+        }
+    }
+
+    /**
+     * Shutdown the ZooKeeper server.
+     */
+    @Override
+    public synchronized void stop() {
+        if (zkServerThread != null) {
+            // The shutdown method is protected...thus this hack to invoke it.
+            // This will log an exception on shutdown; see
+            // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details.
+            try {
+                Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown");
+                shutdown.setAccessible(true);
+                shutdown.invoke(zkServer);
+            }
+
+            catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+
+            // It is expected that the thread will exit after
+            // the server is shutdown; this will block until
+            // the shutdown is complete.
+            try {
+                zkServerThread.join(5000);
+                zkServerThread = null;
+            }
+            catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+//                logger.warn("Interrupted while waiting for embedded ZooKeeper to exit");
+                // abandoning zk thread
+                zkServerThread = null;
+            }
+        }
+    }
+
+    /**
+     * Stop the server if running and invoke the callback when complete.
+     */
+    @Override
+    public void stop(Runnable callback) {
+        stop();
+        callback.run();
+    }
+
+    /**
+     * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none
+     * is provided, only error-level logging will occur.
+     *
+     * @param errorHandler the {@link ErrorHandler} to be invoked
+     */
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+    }
+
+    /**
+     * Runnable implementation that starts the ZooKeeper server.
+     */
+    private class ServerRunnable implements Runnable {
+
+        @Override
+        public void run() {
+            try {
+                Properties properties = new Properties();
+                File file = new File(System.getProperty("java.io.tmpdir")
+                    + File.separator + UUID.randomUUID());
+                file.deleteOnExit();
+                properties.setProperty("dataDir", file.getAbsolutePath());
+                properties.setProperty("clientPort", String.valueOf(clientPort));
+
+                QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
+                quorumPeerConfig.parseProperties(properties);
+
+                zkServer = new ZooKeeperServerMain();
+                ServerConfig configuration = new ServerConfig();
+                configuration.readFrom(quorumPeerConfig);
+
+                zkServer.runFromConfig(configuration);
+            }
+            catch (Exception e) {
+                if (errorHandler != null) {
+                    errorHandler.handleError(e);
+                }
+                else {
+//                    logger.error("Exception running embedded ZooKeeper", e);
+                }
+            }
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/internal-service/service-provider/src/main/java/com/aliware/tianchi/HashServiceImpl.java b/internal-service/service-provider/src/main/java/com/aliware/tianchi/HashServiceImpl.java
index 35bd7d0..81a4877 100644
--- a/internal-service/service-provider/src/main/java/com/aliware/tianchi/HashServiceImpl.java
+++ b/internal-service/service-provider/src/main/java/com/aliware/tianchi/HashServiceImpl.java
@@ -1,32 +1,62 @@
 package com.aliware.tianchi;
 
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.dubbo.common.utils.NamedThreadFactory;
 
 /**
- * Hash service impl
+ * Facade
  *
  * @author guohaoice@gmail.com
  */
 public class HashServiceImpl implements HashInterface {
-  private long averageRTT;
-  private int maxConcurrency;
-  private String salt;
+  private final String salt;
+  private final AtomicBoolean init = new AtomicBoolean(false);
+  private final List<ThrashConfig> configs;
+  private volatile ThrashConfig config;
+  private ScheduledExecutorService scheduler =
+      new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("Benchmark-refresh-permit"));
 
-  public HashServiceImpl(long averageRTT, int maxConcurrency, String salt) {
-    this.averageRTT = averageRTT;
-    this.maxConcurrency = maxConcurrency;
+  public HashServiceImpl(String salt, List<ThrashConfig> configs) {
     this.salt = salt;
+    this.config = ThrashConfig.INIT_CONFIG;
+    this.configs = Collections.unmodifiableList(configs);
   }
 
   @Override
   public int hash(String input) {
-    long baseRtt = nextRTT();
+    if (!init.get()) {
+      if (init.compareAndSet(false, true)) {
+        int startTime = 0;
+        for (ThrashConfig thrashConfig : configs) {
+          scheduler.schedule(
+              () -> refresh(thrashConfig), startTime + config.durationInMs, TimeUnit.MILLISECONDS);
+          startTime += config.durationInMs;
+        }
+      }
+    }
+    Semaphore permit = config.permit;
     try {
+      permit.acquire();
+      long baseRtt = nextRTT();
       Thread.sleep(baseRtt);
+      return (input + salt).hashCode();
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
+    } finally {
+      permit.release();
     }
-    return (input + salt).hashCode();
+    throw new IllegalStateException("Unexpected exception");
+  }
+
+  private void refresh(ThrashConfig thrashConfig) {
+    this.config = thrashConfig;
   }
 
   private long nextRTT() {
@@ -36,7 +66,7 @@ public class HashServiceImpl implements HashInterface {
     double cdf = 0;
     while (u >= cdf) {
       x++;
-      cdf = 1 - Math.exp(-1.0D * 1 / averageRTT * x);
+      cdf = 1 - Math.exp(-1.0D * 1 / config.averageRTTInMs * x);
     }
     return x;
   }
diff --git a/internal-service/service-provider/src/main/java/com/aliware/tianchi/ThrashConfig.java b/internal-service/service-provider/src/main/java/com/aliware/tianchi/ThrashConfig.java
new file mode 100644
index 0000000..9aa3ed7
--- /dev/null
+++ b/internal-service/service-provider/src/main/java/com/aliware/tianchi/ThrashConfig.java
@@ -0,0 +1,21 @@
+package com.aliware.tianchi;
+
+import java.util.concurrent.Semaphore;
+
+/**
+ * @author guohaoice@gmail.com
+ */
+public class ThrashConfig {
+    public static final ThrashConfig INIT_CONFIG=new ThrashConfig(4000,Integer.MAX_VALUE,10);
+    final long durationInMs;
+    final int maxConcurrency;
+    final int averageRTTInMs;
+    final Semaphore permit;
+
+    public ThrashConfig(long durationInMs, int maxConcurrency, int averageRTTInMs) {
+        this.durationInMs = durationInMs;
+        this.maxConcurrency = maxConcurrency;
+        this.averageRTTInMs = averageRTTInMs;
+        this.permit=new Semaphore(maxConcurrency);
+    }
+}
diff --git a/internal-service/service-provider/src/main/resources/dubbo-provider.xml b/internal-service/service-provider/src/main/resources/dubbo-provider.xml
index fb14e69..2acb173 100644
--- a/internal-service/service-provider/src/main/resources/dubbo-provider.xml
+++ b/internal-service/service-provider/src/main/resources/dubbo-provider.xml
@@ -7,14 +7,14 @@
     <!-- provider's application name, used for tracing dependency relationship -->
     <dubbo:application name="demo-provider"/>
     <!-- use multicast registry center to export service -->
-    <dubbo:registry address="multicast://224.5.6.7:1234"/>
+    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
     <!-- use dubbo protocol to export service on port 20880 -->
     <dubbo:protocol name="dubbo" port="20880"/>
     <!-- service implementation, as same as regular local bean -->
     <bean id="demoService" class="com.aliware.tianchi.HashServiceImpl">
-        <constructor-arg index="0" value="10"/>
-        <constructor-arg index="1" value="10000000"/>
-        <constructor-arg index="2" value="salt-value"/>
+<!--        <constructor-arg index="0" value=""/>-->
+<!--        <constructor-arg index="1" value="10000000"/>-->
+        <constructor-arg index="0" value="salt-value"/>
     </bean>
     <!-- declare the service interface to be exported -->
     <dubbo:service interface="com.aliware.tianchi.HashInterface" ref="demoService"/>
diff --git a/pom.xml b/pom.xml
index 33f6073..0024f23 100644
--- a/pom.xml
+++ b/pom.xml
@@ -74,7 +74,6 @@
         <module>internal-dubbo</module>
         <module>internal-gateway</module>
         <module>internal-service</module>
-        <module>bootstrap</module>
         <module>workspace</module>
     </modules>
 </project>
\ No newline at end of file
-- 
GitLab