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

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


The following commit(s) were added to refs/heads/master by this push:
     new eaeaa52  [IOTDB-1970] Migrate the org.apache.iotdb.db.cost.statistic 
module to the new metric framework (#4556)
eaeaa52 is described below

commit eaeaa52068a57938f5723511f6dcc25ef027c7b0
Author: ZhangHongYin <[email protected]>
AuthorDate: Sat Dec 11 16:51:28 2021 +0800

    [IOTDB-1970] Migrate the org.apache.iotdb.db.cost.statistic module to the 
new metric framework (#4556)
---
 .../resources/conf/iotdb-engine.properties         |   9 -
 .../java/org/apache/iotdb/db/conf/IoTDBConfig.java |  22 --
 .../org/apache/iotdb/db/conf/IoTDBDescriptor.java  |  15 -
 .../db/cost/statistic/ConcurrentCircularArray.java |  67 ----
 .../iotdb/db/cost/statistic/Measurement.java       | 423 ---------------------
 .../iotdb/db/cost/statistic/MeasurementMBean.java  |  60 ---
 .../java/org/apache/iotdb/db/service/IoTDB.java    |   2 -
 .../statistic => service/metrics}/Operation.java   |   2 +-
 .../db/service/thrift/impl/TSServiceImpl.java      |  32 +-
 .../db/cost/statistic/PerformanceStatTest.java     | 105 -----
 10 files changed, 23 insertions(+), 714 deletions(-)

diff --git a/server/src/assembly/resources/conf/iotdb-engine.properties 
b/server/src/assembly/resources/conf/iotdb-engine.properties
index 1dc5f2f..aff5333 100644
--- a/server/src/assembly/resources/conf/iotdb-engine.properties
+++ b/server/src/assembly/resources/conf/iotdb-engine.properties
@@ -606,15 +606,6 @@ timestamp_precision=ms
 # Is stat performance of sub-module enable
 # Datatype: boolean
 # enable_performance_stat=false
-# The interval of display statistic result in ms.
-# Datatype: long
-# performance_stat_display_interval=60000
-# The memory used for performance_stat in kb.
-# Datatype: int
-# performance_stat_memory_in_kb=20
-# Is performance tracing enable
-# Datatype: boolean
-# enable_performance_tracing=false
 
 # Uncomment following fields to configure the tracing root directory.
 # For Window platform, the index is as follows:
diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
index af9b3ab..18dc58d 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
@@ -471,12 +471,6 @@ public class IoTDBConfig {
   /** Is stat performance of sub-module enable. */
   private boolean enablePerformanceStat = false;
 
-  /** The display of stat performance interval in ms. Unit: millisecond */
-  private long performanceStatDisplayInterval = 60000;
-
-  /** The memory used for stat performance. Unit: kilobyte */
-  private int performanceStatMemoryInKB = 20;
-
   /** whether use chunkBufferPool. */
   private boolean chunkBufferPoolEnable = false;
 
@@ -1489,22 +1483,6 @@ public class IoTDBConfig {
     this.enablePerformanceStat = enablePerformanceStat;
   }
 
-  public long getPerformanceStatDisplayInterval() {
-    return performanceStatDisplayInterval;
-  }
-
-  void setPerformanceStatDisplayInterval(long performanceStatDisplayInterval) {
-    this.performanceStatDisplayInterval = performanceStatDisplayInterval;
-  }
-
-  public int getPerformanceStatMemoryInKB() {
-    return performanceStatMemoryInKB;
-  }
-
-  void setPerformanceStatMemoryInKB(int performanceStatMemoryInKB) {
-    this.performanceStatMemoryInKB = performanceStatMemoryInKB;
-  }
-
   public boolean isEnablePartialInsert() {
     return enablePartialInsert;
   }
diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
index d690708..d93489b 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
@@ -549,21 +549,6 @@ public class IoTDBDescriptor {
                       "enable_performance_stat", 
Boolean.toString(conf.isEnablePerformanceStat()))
                   .trim()));
 
-      conf.setPerformanceStatDisplayInterval(
-          Long.parseLong(
-              properties
-                  .getProperty(
-                      "performance_stat_display_interval",
-                      Long.toString(conf.getPerformanceStatDisplayInterval()))
-                  .trim()));
-      conf.setPerformanceStatMemoryInKB(
-          Integer.parseInt(
-              properties
-                  .getProperty(
-                      "performance_stat_memory_in_kb",
-                      Integer.toString(conf.getPerformanceStatMemoryInKB()))
-                  .trim()));
-
       int maxConcurrentClientNum =
           Integer.parseInt(
               properties.getProperty(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/ConcurrentCircularArray.java
 
b/server/src/main/java/org/apache/iotdb/db/cost/statistic/ConcurrentCircularArray.java
deleted file mode 100644
index dcae9ae..0000000
--- 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/ConcurrentCircularArray.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.iotdb.db.cost.statistic;
-
-public class ConcurrentCircularArray {
-
-  long[] data;
-  int tail;
-  int head;
-
-  public ConcurrentCircularArray(int size) {
-    this.data = new long[size];
-    tail = head = 0;
-  }
-
-  /**
-   * @param d the data
-   * @return true if successfully; false if there is no space.
-   */
-  public synchronized boolean put(long d) {
-    if ((tail + 1) % data.length == head) {
-      return false;
-    }
-    data[tail++] = d;
-    tail = tail % data.length;
-    return true;
-  }
-
-  public synchronized boolean hasData() {
-    return tail != head;
-  }
-
-  /**
-   * @return -1 if there is no data.(However, you should call hasData() frist 
to avoid returning -1)
-   */
-  public synchronized long take() {
-    if (tail != head) {
-      long result = data[head++];
-      head = head % data.length;
-      return result;
-    } else {
-      return -1;
-    }
-  }
-
-  /** drop all of the elements in array. */
-  public synchronized void clear() {
-    tail = head = 0;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/Measurement.java 
b/server/src/main/java/org/apache/iotdb/db/cost/statistic/Measurement.java
deleted file mode 100644
index c73ccb2..0000000
--- a/server/src/main/java/org/apache/iotdb/db/cost/statistic/Measurement.java
+++ /dev/null
@@ -1,423 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.iotdb.db.cost.statistic;
-
-import org.apache.iotdb.db.concurrent.IoTDBThreadPoolFactory;
-import org.apache.iotdb.db.concurrent.ThreadName;
-import org.apache.iotdb.db.concurrent.WrappedRunnable;
-import org.apache.iotdb.db.conf.IoTDBConfig;
-import org.apache.iotdb.db.conf.IoTDBConstant;
-import org.apache.iotdb.db.conf.IoTDBDescriptor;
-import org.apache.iotdb.db.exception.StartupException;
-import org.apache.iotdb.db.service.IService;
-import org.apache.iotdb.db.service.JMXService;
-import org.apache.iotdb.db.service.ServiceType;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.ReentrantLock;
-
-/**
- * Measurement is used to record execution time of operations defined in enum 
class Operation. It
- * can display average time of each operation, and proportion of operation 
whose execution time fall
- * into time range defined in BUCKET_IN_MS. If you want to change abscissa of 
histogram, just change
- * the BUCKET_IN_MS array. For recording a operation, you should: 1) add a 
item in enum class
- * Operation. 2) call <code>startTimeInNano = System.nanoTime()</code> to 
recode startTime of that
- * operation. 3) call 
<code>Measurement.INSTANCE.addOperationLatency(operation, startTimeInNano)
- * </code> at the end of that operation;
- *
- * @see Operation
- */
-public class Measurement implements MeasurementMBean, IService {
-  private static Logger logger = LoggerFactory.getLogger(Measurement.class);
-
-  /** queue for async store time latencies. */
-  private ConcurrentCircularArray[] operationLatenciesQueue;
-
-  /** size of each queue, this is calculated by memory. */
-  private final int queueSize;
-
-  /** latencies sum of each operation. */
-  private long[] operationLatencies;
-
-  /** the num of each operation. */
-  private long[] operationCnt;
-
-  /** abscissa of histogram. */
-  private static final int[] BUCKET_IN_MS = {1, 4, 16, 64, 256, 1024, 
Integer.MAX_VALUE};
-
-  /** length of BUCKET_IN_MS. */
-  private static final int BUCKET_SIZE = BUCKET_IN_MS.length;
-
-  /**
-   * the num of operation that execution time falls into time range of 
BUCKET_IN_MS. The outer array
-   * is each operation, the inner array is each time range in BUCKET_IN_MS.
-   */
-  private long[][] operationHistogram;
-
-  /** display thread and queue consumer thread. */
-  private ScheduledExecutorService service;
-
-  /** future task of display thread and queue consumer thread. */
-  private ScheduledFuture<?> displayFuture;
-
-  private ScheduledFuture<?> consumeFuture;
-
-  /** lock for modifying isEnableStat, displayFuture and consumeFuture. */
-  private ReentrantLock stateChangeLock = new ReentrantLock();
-
-  public static final Measurement INSTANCE = 
AsyncMeasurementHolder.MEASUREMENT;
-
-  private boolean isEnableStat;
-  private long displayIntervalInMs;
-  private Map<String, Boolean> operationSwitch;
-
-  private final String mbeanName =
-      String.format(
-          "%s:%s=%s",
-          "org.apache.iotdb.db.cost.statistic", IoTDBConstant.JMX_TYPE, 
getID().getJmxName());
-
-  private Measurement() {
-    IoTDBConfig tdbConfig = IoTDBDescriptor.getInstance().getConfig();
-    isEnableStat = tdbConfig.isEnablePerformanceStat();
-    displayIntervalInMs = tdbConfig.getPerformanceStatDisplayInterval();
-    int memoryInKb = tdbConfig.getPerformanceStatMemoryInKB();
-
-    queueSize = memoryInKb * 1000 / Operation.values().length / 8;
-    operationLatenciesQueue = new 
ConcurrentCircularArray[Operation.values().length];
-    operationLatencies = new long[Operation.values().length];
-    operationCnt = new long[Operation.values().length];
-    operationSwitch = new HashMap<>(Operation.values().length);
-    for (Operation op : Operation.values()) {
-      operationLatenciesQueue[op.ordinal()] = new 
ConcurrentCircularArray(queueSize);
-      operationCnt[op.ordinal()] = 0;
-      operationLatencies[op.ordinal()] = 0;
-      operationSwitch.put(op.getName(), true);
-    }
-    operationHistogram = new long[Operation.values().length][BUCKET_SIZE];
-    for (Operation operation : Operation.values()) {
-      for (int i = 0; i < BUCKET_SIZE; i++) {
-        operationHistogram[operation.ordinal()][i] = 0;
-      }
-    }
-    logger.info("start measurement stats module...");
-    service =
-        IoTDBThreadPoolFactory.newScheduledThreadPool(2, 
ThreadName.TIME_COST_STATISTIC.getName());
-  }
-
-  public boolean addOperationLatency(Operation op, long startTime) {
-    if (isEnableStat && operationSwitch.get(op.getName())) {
-      return 
operationLatenciesQueue[op.ordinal()].put((System.currentTimeMillis() - 
startTime));
-    }
-    return false;
-  }
-
-  @Override
-  public void startStatistics() {
-    stateChangeLock.lock();
-    try {
-      isEnableStat = true;
-      if (consumeFuture != null && !consumeFuture.isCancelled()) {
-        logger.info("The consuming task in measurement stat module is already 
running...");
-      } else {
-        consumeFuture = service.schedule(new QueueConsumerThread(), 0, 
TimeUnit.MILLISECONDS);
-      }
-    } catch (Exception e) {
-      logger.error("Find error when start performance statistic thread, ", e);
-    } finally {
-      stateChangeLock.unlock();
-    }
-  }
-
-  @Override
-  public void startContinuousPrintStatistics() {
-    stateChangeLock.lock();
-    try {
-      isEnableStat = true;
-      if (displayFuture != null && !displayFuture.isCancelled()) {
-        logger.info("The display task in measurement stat module is already 
running...");
-      } else {
-        displayFuture =
-            service.scheduleWithFixedDelay(
-                new Measurement.DisplayRunnable(), 20, displayIntervalInMs, 
TimeUnit.MILLISECONDS);
-      }
-    } catch (Exception e) {
-      logger.error("Find error when start performance statistic thread, ", e);
-    } finally {
-      stateChangeLock.unlock();
-    }
-  }
-
-  @Override
-  public void startPrintStatisticsOnce() {
-    showMeasurements();
-  }
-
-  @Override
-  public void stopPrintStatistic() {
-    stateChangeLock.lock();
-    try {
-      displayFuture = cancelFuture(displayFuture);
-    } catch (Exception e) {
-      logger.error("Find error when stop display thread, ", e);
-    } finally {
-      stateChangeLock.unlock();
-    }
-  }
-
-  @Override
-  public void stopStatistic() {
-    stateChangeLock.lock();
-    try {
-      isEnableStat = false;
-      displayFuture = cancelFuture(displayFuture);
-      consumeFuture = cancelFuture(consumeFuture);
-    } catch (Exception e) {
-      logger.error("Find error when stop display and consuming threads, ", e);
-    } finally {
-      stateChangeLock.unlock();
-    }
-  }
-
-  @Override
-  public void clearStatisticalState() {
-    for (Operation op : Operation.values()) {
-      operationLatenciesQueue[op.ordinal()].clear();
-      operationCnt[op.ordinal()] = 0;
-      operationLatencies[op.ordinal()] = 0;
-      for (int i = 0; i < BUCKET_SIZE; i++) {
-        operationHistogram[op.ordinal()][i] = 0;
-      }
-    }
-  }
-
-  @Override
-  public boolean changeOperationSwitch(String operationName, Boolean 
operationState) {
-    if (operationSwitch.containsKey(operationName)) {
-      operationSwitch.put(operationName, operationState);
-      return true;
-    } else {
-      return false;
-    }
-  }
-
-  /** start service. */
-  @Override
-  public void start() throws StartupException {
-    // start display thread and consumer threads.
-    logger.info("start the consuming task in the measurement stats module...");
-    this.clearStatisticalState();
-    if (service.isShutdown()) {
-      service =
-          IoTDBThreadPoolFactory.newScheduledThreadPool(
-              2, ThreadName.TIME_COST_STATISTIC.getName());
-    }
-    // we have to check again because someone may change the value.
-    isEnableStat = 
IoTDBDescriptor.getInstance().getConfig().isEnablePerformanceStat();
-    if (isEnableStat) {
-      consumeFuture = service.schedule(new QueueConsumerThread(), 0, 
TimeUnit.MILLISECONDS);
-    }
-    try {
-      JMXService.registerMBean(INSTANCE, mbeanName);
-    } catch (Exception e) {
-      throw new StartupException(this.getID().getName(), e.getMessage());
-    }
-  }
-
-  /** stop service. */
-  @Override
-  public void stop() {
-    logger.info("stop measurement stats module...");
-    JMXService.deregisterMBean(mbeanName);
-    if (service == null || service.isShutdown()) {
-      return;
-    }
-    service.shutdownNow();
-    try {
-      consumeFuture = cancelFuture(consumeFuture);
-      displayFuture = cancelFuture(displayFuture);
-      service.awaitTermination(5, TimeUnit.SECONDS);
-    } catch (InterruptedException e) {
-      logger.error("Performance statistic service could not be shutdown, {}", 
e.getMessage());
-      // Restore interrupted state...
-      Thread.currentThread().interrupt();
-    }
-  }
-
-  /**
-   * @param future
-   * @return always return null;
-   */
-  private ScheduledFuture<?> cancelFuture(ScheduledFuture<?> future) {
-    if (future != null) {
-      future.cancel(true);
-    }
-    return null;
-  }
-
-  @Override
-  public ServiceType getID() {
-    return ServiceType.PERFORMANCE_STATISTIC_SERVICE;
-  }
-
-  @Override
-  public boolean isEnableStat() {
-    return isEnableStat;
-  }
-
-  @Override
-  public long getDisplayIntervalInMs() {
-    return displayIntervalInMs;
-  }
-
-  @Override
-  public void setDisplayIntervalInMs(long displayIntervalInMs) {
-    this.displayIntervalInMs = displayIntervalInMs;
-  }
-
-  @Override
-  public Map<String, Boolean> getOperationSwitch() {
-    return operationSwitch;
-  }
-
-  private static class AsyncMeasurementHolder {
-
-    private static final Measurement MEASUREMENT = new Measurement();
-
-    private AsyncMeasurementHolder() {}
-  }
-
-  private void showMeasurements() {
-    Date date = new Date();
-    logger.info(
-        "====================================={} Measurement 
(ms)======================================",
-        date);
-    String head =
-        String.format("%-45s%-25s%-25s%-25s", "OPERATION", "COUNT", 
"TOTAL_TIME", "AVG_TIME");
-    logger.info(head);
-    for (Operation operation : Operation.values()) {
-      if (!operationSwitch.get(operation.getName())) {
-        continue;
-      }
-      long cnt = operationCnt[operation.ordinal()];
-      long totalInMs = operationLatencies[operation.ordinal()];
-      String avg = String.format("%.4f", (totalInMs / (cnt + 1e-9)));
-      String item =
-          String.format("%-45s%-25s%-25s%-25s", operation.name, cnt + "", 
totalInMs + "", avg);
-      logger.info(item);
-    }
-    logger.info(
-        "==========================================OPERATION 
HISTOGRAM====================================================");
-    StringBuilder histogramHead = new StringBuilder(String.format("%-45s", 
"OPERATION"));
-    for (int i = 0; i < BUCKET_SIZE; i++) {
-      histogramHead.append(String.format("%-8s", BUCKET_IN_MS[i] + "ms"));
-    }
-    if (logger.isInfoEnabled()) {
-      logger.info(histogramHead.toString());
-    }
-    for (Operation operation : Operation.values()) {
-      if (!operationSwitch.get(operation.getName())) {
-        continue;
-      }
-      StringBuilder item = new StringBuilder(String.format("%-45s", 
operation.getName()));
-      long cnt = operationCnt[operation.ordinal()];
-      for (int i = 0; i < BUCKET_SIZE; i++) {
-        String avg =
-            String.format(
-                "%.2f", (operationHistogram[operation.ordinal()][i] / (cnt + 
1e-9) * 100));
-        item.append(String.format("%-8s", avg + "%"));
-      }
-      if (logger.isInfoEnabled()) {
-        logger.info(item.toString());
-      }
-    }
-
-    logger.info(
-        
"=================================================================================================================");
-  }
-
-  class DisplayRunnable extends WrappedRunnable {
-
-    @Override
-    public void runMayThrow() {
-      showMeasurements();
-    }
-  }
-
-  class QueueConsumerThread extends WrappedRunnable {
-
-    @Override
-    public void runMayThrow() {
-      consumer();
-    }
-
-    private void consumer() {
-      boolean allEmpty;
-      while (isEnableStat) {
-        allEmpty = true;
-        for (Operation op : Operation.values()) {
-          if (!operationSwitch.get(op.getName())) {
-            continue;
-          }
-          int idx = op.ordinal();
-          ConcurrentCircularArray queue = operationLatenciesQueue[idx];
-          if (queue.hasData()) {
-            long time = queue.take();
-            operationLatencies[idx] += time;
-            operationCnt[idx]++;
-            operationHistogram[idx][calIndex(time)]++;
-            allEmpty = false;
-          }
-        }
-        if (allEmpty) {
-          try {
-            Thread.sleep(10);
-          } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-            break;
-          }
-        }
-      }
-    }
-
-    private int calIndex(long x) {
-      for (int i = 0; i < BUCKET_SIZE; i++) {
-        if (BUCKET_IN_MS[i] >= x) {
-          return i;
-        }
-      }
-      return BUCKET_SIZE - 1;
-    }
-  }
-
-  public long[] getOperationLatencies() {
-    return operationLatencies;
-  }
-
-  public long[] getOperationCnt() {
-    return operationCnt;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/MeasurementMBean.java 
b/server/src/main/java/org/apache/iotdb/db/cost/statistic/MeasurementMBean.java
deleted file mode 100644
index 1ab9497..0000000
--- 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/MeasurementMBean.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.iotdb.db.cost.statistic;
-
-import java.util.Map;
-
-public interface MeasurementMBean {
-
-  /** start calculating the statistic. */
-  void startStatistics();
-
-  /** start display performance statistic every interval of 
displayIntervalInMs. */
-  void startContinuousPrintStatistics();
-
-  /** start display performance statistic after interval of 
displayIntervalInMs. */
-  void startPrintStatisticsOnce();
-
-  /** stop display performance statistic. */
-  void stopPrintStatistic();
-
-  /** stop calculating the statistic */
-  void stopStatistic();
-
-  /** clear current stat result, reset statistical state. */
-  void clearStatisticalState();
-
-  /**
-   * set whether to monitor operation status.
-   *
-   * @param operationName the name of operation, defined in attribute 
operationSwitch.
-   * @param operationState state of operation.
-   * @return true if successful, false if fail.
-   */
-  boolean changeOperationSwitch(String operationName, Boolean operationState);
-
-  boolean isEnableStat();
-
-  long getDisplayIntervalInMs();
-
-  void setDisplayIntervalInMs(long displayIntervalInMs);
-
-  Map<String, Boolean> getOperationSwitch();
-}
diff --git a/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java 
b/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
index aad3573..c40da67 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
@@ -24,7 +24,6 @@ import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.conf.rest.IoTDBRestServiceCheck;
 import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
-import org.apache.iotdb.db.cost.statistic.Measurement;
 import org.apache.iotdb.db.cq.ContinuousQueryService;
 import org.apache.iotdb.db.engine.StorageEngine;
 import org.apache.iotdb.db.engine.cache.CacheHitRatioMonitor;
@@ -119,7 +118,6 @@ public class IoTDB implements IoTDBMBean {
     registerManager.register(JMXService.getInstance());
     registerManager.register(FlushManager.getInstance());
     registerManager.register(MultiFileLogNodeManager.getInstance());
-    registerManager.register(Measurement.INSTANCE);
     registerManager.register(CacheHitRatioMonitor.getInstance());
     registerManager.register(MergeManager.getINSTANCE());
     registerManager.register(CompactionTaskManager.getInstance());
diff --git 
a/server/src/main/java/org/apache/iotdb/db/cost/statistic/Operation.java 
b/server/src/main/java/org/apache/iotdb/db/service/metrics/Operation.java
similarity index 96%
rename from 
server/src/main/java/org/apache/iotdb/db/cost/statistic/Operation.java
rename to 
server/src/main/java/org/apache/iotdb/db/service/metrics/Operation.java
index ccb2109..4b8f3db 100644
--- a/server/src/main/java/org/apache/iotdb/db/cost/statistic/Operation.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/metrics/Operation.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.iotdb.db.cost.statistic;
+package org.apache.iotdb.db.service.metrics;
 
 public enum Operation {
   EXECUTE_JDBC_BATCH("EXECUTE_JDBC_BATCH"),
diff --git 
a/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/TSServiceImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/TSServiceImpl.java
index 46dbed7..0683ac7 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/TSServiceImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/TSServiceImpl.java
@@ -25,8 +25,6 @@ import org.apache.iotdb.db.conf.IoTDBConfig;
 import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.conf.OperationType;
-import org.apache.iotdb.db.cost.statistic.Measurement;
-import org.apache.iotdb.db.cost.statistic.Operation;
 import org.apache.iotdb.db.engine.selectinto.InsertTabletPlansIterator;
 import org.apache.iotdb.db.exception.IoTDBException;
 import org.apache.iotdb.db.exception.QueryInBatchStatementException;
@@ -53,10 +51,12 @@ import org.apache.iotdb.db.service.IoTDB;
 import org.apache.iotdb.db.service.StaticResps;
 import org.apache.iotdb.db.service.basic.BasicOpenSessionResp;
 import org.apache.iotdb.db.service.basic.BasicServiceProvider;
+import org.apache.iotdb.db.service.metrics.Operation;
 import org.apache.iotdb.db.tools.watermark.GroupedLSBWatermarkEncoder;
 import org.apache.iotdb.db.tools.watermark.WatermarkEncoder;
 import org.apache.iotdb.db.utils.QueryDataSetUtils;
 import org.apache.iotdb.db.utils.SchemaUtils;
+import org.apache.iotdb.metrics.MetricService;
 import org.apache.iotdb.rpc.RedirectException;
 import org.apache.iotdb.rpc.RpcUtils;
 import org.apache.iotdb.rpc.TSStatusCode;
@@ -185,7 +185,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
   private boolean executeInsertRowsPlan(InsertRowsPlan insertRowsPlan, 
List<TSStatus> result) {
     long t1 = System.currentTimeMillis();
     TSStatus tsStatus = executeNonQueryPlan(insertRowsPlan);
-    
Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_ROWS_PLAN_IN_BATCH, 
t1);
+    addOperationLatency(Operation.EXECUTE_ROWS_PLAN_IN_BATCH, t1);
     int startIndex = result.size();
     if (startIndex > 0) {
       startIndex = startIndex - 1;
@@ -205,7 +205,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
       CreateMultiTimeSeriesPlan multiPlan, List<TSStatus> result) {
     long t1 = System.currentTimeMillis();
     TSStatus tsStatus = executeNonQueryPlan(multiPlan);
-    
Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_MULTI_TIMESERIES_PLAN_IN_BATCH,
 t1);
+    addOperationLatency(Operation.EXECUTE_MULTI_TIMESERIES_PLAN_IN_BATCH, t1);
 
     int startIndex = result.size();
     if (startIndex > 0) {
@@ -357,7 +357,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
           }
           long t2 = System.currentTimeMillis();
           TSExecuteStatementResp resp = executeNonQueryStatement(physicalPlan, 
req.getSessionId());
-          
Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_ONE_SQL_IN_BATCH, 
t2);
+          addOperationLatency(Operation.EXECUTE_ONE_SQL_IN_BATCH, t2);
           result.add(resp.status);
           if (resp.getStatus().code != 
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
             isAllSuccessful = false;
@@ -378,7 +378,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
         }
       }
     }
-    Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_JDBC_BATCH, t1);
+    addOperationLatency(Operation.EXECUTE_JDBC_BATCH, t1);
     return isAllSuccessful
         ? RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "Execute batch 
statements successfully")
         : RpcUtils.getStatus(result);
@@ -657,7 +657,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
       sessionManager.releaseQueryResourceNoExceptions(queryId);
       throw e;
     } finally {
-      Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_QUERY, 
queryStartTime);
+      addOperationLatency(Operation.EXECUTE_QUERY, queryStartTime);
       long costTime = System.currentTimeMillis() - queryStartTime;
       if (costTime >= CONFIG.getSlowQueryThreshold()) {
         SLOW_SQL_LOGGER.info("Cost: {} ms, sql is {}", costTime, statement);
@@ -875,7 +875,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
       return 
RpcUtils.getTSExecuteStatementResp(TSStatusCode.SUCCESS_STATUS).setQueryId(queryId);
     } finally {
       sessionManager.releaseQueryResourceNoExceptions(queryId);
-      Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_SELECT_INTO, 
startTime);
+      addOperationLatency(Operation.EXECUTE_SELECT_INTO, startTime);
       long costTime = System.currentTimeMillis() - startTime;
       if (costTime >= CONFIG.getSlowQueryThreshold()) {
         SLOW_SQL_LOGGER.info("Cost: {} ms, sql is {}", costTime, statement);
@@ -1470,7 +1470,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
       return onNPEOrUnexpectedException(
           e, OperationType.INSERT_TABLET, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      
Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_RPC_BATCH_INSERT, 
t1);
+      addOperationLatency(Operation.EXECUTE_RPC_BATCH_INSERT, t1);
     }
   }
 
@@ -1492,7 +1492,7 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
       return onNPEOrUnexpectedException(
           e, OperationType.INSERT_TABLETS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      
Measurement.INSTANCE.addOperationLatency(Operation.EXECUTE_RPC_BATCH_INSERT, 
t1);
+      addOperationLatency(Operation.EXECUTE_RPC_BATCH_INSERT, t1);
     }
   }
 
@@ -1924,4 +1924,16 @@ public class TSServiceImpl extends BasicServiceProvider 
implements TSIService.If
         TSStatusCode.NOT_LOGIN_ERROR,
         "Log in failed. Either you are not authorized or the session has timed 
out.");
   }
+
+  /** Add stat of operation into metrics */
+  private void addOperationLatency(Operation operation, long startTime) {
+    if (CONFIG.isEnablePerformanceStat()) {
+      MetricService.getMetricManager()
+          .getOrCreateHistogram("operation_histogram", "name", 
operation.getName())
+          .update(System.currentTimeMillis() - startTime);
+      MetricService.getMetricManager()
+          .getOrCreateCounter("operation_count", "name", operation.getName())
+          .inc();
+    }
+  }
 }
diff --git 
a/server/src/test/java/org/apache/iotdb/db/cost/statistic/PerformanceStatTest.java
 
b/server/src/test/java/org/apache/iotdb/db/cost/statistic/PerformanceStatTest.java
deleted file mode 100644
index ce55c11..0000000
--- 
a/server/src/test/java/org/apache/iotdb/db/cost/statistic/PerformanceStatTest.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.iotdb.db.cost.statistic;
-
-import org.apache.iotdb.db.conf.IoTDBDescriptor;
-import org.apache.iotdb.db.exception.StartupException;
-
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class PerformanceStatTest {
-
-  private static final Logger LOGGER = 
LoggerFactory.getLogger(PerformanceStatTest.class);
-
-  @Before
-  public void setUp() {
-    IoTDBDescriptor.getInstance().getConfig().setEnablePerformanceStat(true);
-  }
-
-  @After
-  public void tearDown() {
-    IoTDBDescriptor.getInstance().getConfig().setEnablePerformanceStat(false);
-  }
-
-  @Test
-  public void test() {
-    Measurement measurement = Measurement.INSTANCE;
-    Operation operation = Operation.EXECUTE_JDBC_BATCH;
-    measurement.addOperationLatency(operation, System.currentTimeMillis());
-    measurement.addOperationLatency(operation, System.currentTimeMillis() - 
8000000);
-
-    long batchOpCnt = measurement.getOperationCnt()[operation.ordinal()];
-    Assert.assertEquals(0L, batchOpCnt);
-    try {
-      measurement.start();
-      measurement.startContinuousPrintStatistics();
-      measurement.addOperationLatency(operation, System.currentTimeMillis());
-      measurement.addOperationLatency(operation, System.currentTimeMillis() - 
8000000);
-      Thread.currentThread().sleep(1000);
-      batchOpCnt = measurement.getOperationCnt()[operation.ordinal()];
-      Assert.assertEquals(2L, batchOpCnt);
-      measurement.stopPrintStatistic();
-      measurement.stopPrintStatistic();
-      measurement.stopPrintStatistic();
-      LOGGER.info("After stopPrintStatistic!");
-      Thread.currentThread().sleep(1000);
-      measurement.clearStatisticalState();
-      batchOpCnt = measurement.getOperationCnt()[operation.ordinal()];
-      Assert.assertEquals(0L, batchOpCnt);
-      measurement.startContinuousPrintStatistics();
-      LOGGER.info("ReStart!");
-      Thread.currentThread().sleep(1000);
-      measurement.startContinuousPrintStatistics();
-      LOGGER.info("ReStart2!");
-      Thread.currentThread().sleep(1000);
-      measurement.stopPrintStatistic();
-      LOGGER.info("After stopStatistic2!");
-    } catch (Exception e) {
-      LOGGER.error("find error in stat performance, the message is {}", 
e.getMessage());
-    } finally {
-      measurement.stop();
-    }
-  }
-
-  @Test
-  public void testSwitch() {
-    Measurement measurement = Measurement.INSTANCE;
-    try {
-      measurement.start();
-      measurement.startStatistics();
-      measurement.startStatistics();
-      measurement.startContinuousPrintStatistics();
-      measurement.stopPrintStatistic();
-      measurement.stopStatistic();
-      measurement.clearStatisticalState();
-      measurement.startPrintStatisticsOnce();
-      measurement.startContinuousPrintStatistics();
-      measurement.startStatistics();
-    } catch (StartupException e) {
-      e.printStackTrace();
-    } finally {
-      measurement.stop();
-    }
-  }
-}

Reply via email to