You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/01/08 06:27:10 UTC

[GitHub] [iotdb] JackieTien97 commented on a change in pull request #2080: Add continuous compaction in level compaction strategy

JackieTien97 commented on a change in pull request #2080:
URL: https://github.com/apache/iotdb/pull/2080#discussion_r553681080



##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cache/FileStatistics.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.engine.cache;
+
+import static org.apache.iotdb.tsfile.utils.RamUsageEstimator.NUM_BYTES_INT;
+import static org.apache.iotdb.tsfile.utils.RamUsageEstimator.NUM_BYTES_LONG;
+import static org.apache.iotdb.tsfile.utils.RamUsageEstimator.NUM_BYTES_OBJECT_REF;
+
+public class FileStatistics {
+
+  private long numOfPoints;
+  private int sensorNum;
+
+  public FileStatistics(long numOfPoints, int sensorNum) {
+    this.numOfPoints = numOfPoints;
+    this.sensorNum = sensorNum;
+  }
+
+  public long getNumOfPoints() {
+    return numOfPoints;
+  }
+
+  public void setNumOfPoints(long numOfPoints) {
+    this.numOfPoints = numOfPoints;
+  }
+
+  public int getSensorNum() {
+    return sensorNum;
+  }
+
+  public void setSensorNum(int sensorNum) {
+    this.sensorNum = sensorNum;
+  }
+
+
+  public long calculateRamSize() {
+    return NUM_BYTES_OBJECT_REF + NUM_BYTES_LONG + NUM_BYTES_INT;

Review comment:
       NUM_BYTES_OBJECT_REF is not equal to the object header size, you should use the NUM_BYTES_OBJECT_HEADER field in RamUsageEstimator.
   And by the way, jvm may also align the object, so this calculation may not be exact.

##########
File path: server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
##########
@@ -421,6 +427,11 @@
    */
   private long allocateMemoryForChunkCache = allocateMemoryForRead / 10;
 
+  /**
+   * Memory allocated for file statistics cache in compaction process
+   */
+  private long allocateMemoryForFileStatisticsCache = allocateMemoryForRead / 100;

Review comment:
       Why this field does not have a corresponding property in iotdb-engine.properties.

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cache/FileStatisticsCache.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.engine.cache;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.tsfile.file.metadata.ChunkMetadata;
+import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.utils.RamUsageEstimator;
+
+public class FileStatisticsCache {
+
+  private final ReadWriteLock lock = new ReentrantReadWriteLock();
+  private static LRULinkedHashMap<AccountableString, FileStatistics> fileStatisticsCache;
+  private static long MEMORY_THRESHOLD_IN_FILE_STATISTICS_CACHE = IoTDBDescriptor.getInstance()
+      .getConfig().getAllocateMemoryForFileStatisticsCache();
+
+  private FileStatisticsCache() {
+    fileStatisticsCache = new LRULinkedHashMap<AccountableString, FileStatistics>(
+        MEMORY_THRESHOLD_IN_FILE_STATISTICS_CACHE) {
+      @Override
+      protected long calEntrySize(AccountableString key, FileStatistics value) {
+        if (value == null) {
+          return RamUsageEstimator.sizeOf(key) + RamUsageEstimator.shallowSizeOf(value);

Review comment:
       There is no need to calculate FileStatistics size each time, because it's a constant value

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cache/FileStatisticsCache.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.engine.cache;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.tsfile.file.metadata.ChunkMetadata;
+import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.utils.RamUsageEstimator;
+
+public class FileStatisticsCache {
+
+  private final ReadWriteLock lock = new ReentrantReadWriteLock();
+  private static LRULinkedHashMap<AccountableString, FileStatistics> fileStatisticsCache;
+  private static long MEMORY_THRESHOLD_IN_FILE_STATISTICS_CACHE = IoTDBDescriptor.getInstance()
+      .getConfig().getAllocateMemoryForFileStatisticsCache();
+
+  private FileStatisticsCache() {
+    fileStatisticsCache = new LRULinkedHashMap<AccountableString, FileStatistics>(
+        MEMORY_THRESHOLD_IN_FILE_STATISTICS_CACHE) {
+      @Override
+      protected long calEntrySize(AccountableString key, FileStatistics value) {
+        if (value == null) {
+          return RamUsageEstimator.sizeOf(key) + RamUsageEstimator.shallowSizeOf(value);
+        }
+        long entrySize;
+        if (count < 10) {
+          long currentSize = value.calculateRamSize();
+          averageSize = ((averageSize * count) + currentSize) / (++count);
+          entrySize = RamUsageEstimator.sizeOf(key)
+              + (currentSize + RamUsageEstimator.NUM_BYTES_OBJECT_REF)
+              + RamUsageEstimator.shallowSizeOf(value);
+        } else if (count < 100000) {
+          count++;
+          entrySize = RamUsageEstimator.sizeOf(key)
+              + (averageSize + RamUsageEstimator.NUM_BYTES_OBJECT_REF)
+              + RamUsageEstimator.shallowSizeOf(value);
+        } else {
+          averageSize = value.calculateRamSize();
+          count = 1;
+          entrySize = RamUsageEstimator.sizeOf(key)
+              + (averageSize + RamUsageEstimator.NUM_BYTES_OBJECT_REF)
+              + RamUsageEstimator.shallowSizeOf(value);
+        }
+        return entrySize;
+      }
+    };
+  }
+
+  public void put(String filePath, long totalPoint, int sensorNum) {
+    lock.writeLock().lock();
+    fileStatisticsCache
+        .put(new AccountableString(filePath), new FileStatistics(totalPoint, sensorNum));
+    lock.readLock().lock();
+  }
+
+  public FileStatistics get(TsFileResource fileResource) throws IOException {
+    String filePath = fileResource.getTsFilePath();
+    AccountableString key = new AccountableString(filePath);
+    if (fileStatisticsCache.containsKey(key)) {
+      lock.readLock().lock();
+      try {
+        return fileStatisticsCache.get(key);
+      } finally {
+        lock.readLock().unlock();
+      }
+    } else {
+      try (TsFileSequenceReader tsFileSequenceReader = new TsFileSequenceReader(
+          fileResource.getTsFilePath())) {
+        long totalPoints = 0;
+        Set<String> sensorSet = new HashSet<>();
+        List<String> devices = tsFileSequenceReader.getAllDevices();
+        for (String device : devices) {
+          Map<String, List<ChunkMetadata>> chunkMetadataListMap = tsFileSequenceReader
+              .readChunkMetadataInDevice(device);
+          for (List<ChunkMetadata> chunkMetadataList : chunkMetadataListMap.values()) {
+            for (ChunkMetadata chunkMetadata : chunkMetadataList) {
+              totalPoints += chunkMetadata.getNumOfPoints();
+              sensorSet.add(chunkMetadata.getMeasurementUid());

Review comment:
       This will cause differernt thread read from disk repeatedly

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cache/FileStatisticsCache.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.engine.cache;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.tsfile.file.metadata.ChunkMetadata;
+import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.utils.RamUsageEstimator;
+
+public class FileStatisticsCache {

Review comment:
       It's better to add this cache to MBeans monitor.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org