MrQuansy commented on code in PR #14201:
URL: https://github.com/apache/iotdb/pull/14201#discussion_r1862155805


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/binaryallocator/BinaryAllocator.java:
##########
@@ -0,0 +1,242 @@
+/*
+ * 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.commons.utils.binaryallocator;
+
+import org.apache.iotdb.commons.service.metric.JvmGcMonitorMetrics;
+import org.apache.iotdb.commons.service.metric.MetricService;
+
+import org.apache.tsfile.utils.PooledBinary;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Duration;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class BinaryAllocator {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(BinaryAllocator.class);
+
+  private final Arena[] heapArenas;
+  private final AllocatorConfig allocatorConfig;
+
+  public static final BinaryAllocator DEFAULT = new 
BinaryAllocator(AllocatorConfig.DEFAULT_CONFIG);
+  private ArenaStrategy arenaStrategy = new LeastUsedArenaStrategy();
+  private AtomicReference<BinaryAllocatorState> state =
+      new AtomicReference<>(BinaryAllocatorState.UNINITIALIZED);
+
+  private BinaryAllocatorMetrics metrics;
+  private static ThreadLocal<ThreadArenaRegistry> arenaRegistry =
+      ThreadLocal.withInitial(() -> new ThreadArenaRegistry());
+
+  private Evictor evictor;
+
+  public BinaryAllocator(AllocatorConfig allocatorConfig) {
+    this.allocatorConfig = allocatorConfig;
+
+    heapArenas = newArenaArray(allocatorConfig.arenaNum);
+    SizeClasses sizeClasses = new SizeClasses(allocatorConfig);
+
+    for (int i = 0; i < heapArenas.length; i++) {
+      Arena arena = new Arena(this, sizeClasses, i, allocatorConfig);
+      heapArenas[i] = arena;
+    }
+
+    this.metrics = new BinaryAllocatorMetrics(this);
+    MetricService.getInstance().addMetricSet(this.metrics);
+
+    if (allocatorConfig.enableBinaryAllocator) {
+      state.set(BinaryAllocatorState.OPEN);
+      evictor =
+          new GCEvictor(
+              "binary-allocator-gc-evictor", 
allocatorConfig.getDurationEvictorShutdownTimeout());
+      evictor.startEvictor(allocatorConfig.getDurationBetweenEvictorRuns());
+    } else {
+      state.set(BinaryAllocatorState.CLOSE);
+      this.close(false);
+    }
+  }
+
+  public PooledBinary allocateBinary(int reqCapacity) {
+    if (reqCapacity < allocatorConfig.minAllocateSize
+        | reqCapacity > allocatorConfig.maxAllocateSize) {
+      return new PooledBinary(new byte[reqCapacity]);
+    }
+
+    Arena arena = arenaStrategy.choose(heapArenas);
+
+    return new PooledBinary(arena.allocate(reqCapacity), reqCapacity, 
arena.getArenaID());
+  }
+
+  public void deallocateBinary(PooledBinary binary) {
+    if (binary != null
+        && binary.getLength() >= allocatorConfig.minAllocateSize
+        && binary.getLength() <= allocatorConfig.maxAllocateSize) {
+      int arenaIndex = binary.getArenaIndex();
+      if (arenaIndex != -1) {
+        Arena arena = heapArenas[arenaIndex];
+        arena.deallocate(binary.getValues());
+      }
+    }
+  }
+
+  public void deallocateBatch(PooledBinary[] blobs) {
+    for (PooledBinary blob : blobs) {
+      deallocateBinary(blob);
+    }
+  }
+
+  public long getTotalUsedMemory() {
+    long totalUsedMemory = 0;
+    for (Arena arena : heapArenas) {
+      totalUsedMemory += arena.getTotalUsedMemory();
+    }
+    return totalUsedMemory;
+  }
+
+  public long getTotalActiveMemory() {
+    long totalActiveMemory = 0;
+    for (Arena arena : heapArenas) {
+      totalActiveMemory += arena.getActiveMemory();
+    }
+    return totalActiveMemory;
+  }
+
+  public void evict(double ratio) {
+    for (Arena arena : heapArenas) {
+      arena.evict(ratio);
+    }
+  }
+
+  public boolean isOpen() {
+    return state.get() == BinaryAllocatorState.OPEN;
+  }
+
+  public void close(boolean needReopen) {
+    if (needReopen) state.set(BinaryAllocatorState.TMP_CLOSE);
+    else {
+      state.set(BinaryAllocatorState.CLOSE);
+      if (evictor != null) {
+        evictor.stopEvictor();
+      }
+    }
+    for (Arena arena : heapArenas) {
+      arena.close();
+    }
+  }
+
+  private void restart() {
+    state.set(BinaryAllocatorState.OPEN);
+    for (Arena arena : heapArenas) {
+      arena.restart();
+    }
+  }
+
+  public BinaryAllocatorMetrics getMetrics() {
+    return metrics;
+  }
+
+  @SuppressWarnings("unchecked")
+  private static Arena[] newArenaArray(int size) {
+    return new Arena[size];
+  }
+
+  private static class ThreadArenaRegistry {
+    private Arena threadArenaBinding = null;
+
+    public Arena getArena() {
+      return threadArenaBinding;
+    }
+
+    public void bindArena(Arena arena) {
+      threadArenaBinding = arena;
+      arena.numRegisterThread.incrementAndGet();
+    }
+
+    public void unbindArena() {
+      Arena arena = threadArenaBinding;
+      if (arena != null) {
+        arena.numRegisterThread.decrementAndGet();
+      }
+    }
+
+    @Override
+    protected void finalize() {
+      unbindArena();
+    }
+  }
+
+  private class LeastUsedArenaStrategy implements ArenaStrategy {
+    @Override
+    public Arena choose(Arena[] arenas) {
+      Arena boundArena = arenaRegistry.get().getArena();
+      if (boundArena != null) {
+        return boundArena;
+      }
+
+      if (arenas == null || arenas.length == 0) {
+        return null;
+      }
+
+      Arena minArena = arenas[0];
+
+      for (int i = 1; i < arenas.length; i++) {
+        Arena arena = arenas[i];
+        if (arena.numRegisterThread.get() < minArena.numRegisterThread.get()) {
+          minArena = arena;
+        }
+      }
+
+      arenaRegistry.get().bindArena(minArena);
+      return minArena;
+    }
+  }
+
+  public class GCEvictor extends Evictor {
+    public GCEvictor(String name, Duration evictorShutdownTimeoutDuration) {
+      super(name, evictorShutdownTimeoutDuration);
+    }
+
+    @Override
+    public void run() {
+      LOGGER.debug("Binary allocator running evictor");
+      if (state.get() == BinaryAllocatorState.TMP_CLOSE) {
+        if 
(JvmGcMonitorMetrics.getInstance().getGcData().getGcTimePercentage() > 20) {
+          restart();
+        }
+        return;
+      }
+
+      if (JvmGcMonitorMetrics.getInstance().getGcData().getGcTimePercentage() 
> 30) {
+        for (Arena arena : heapArenas) {
+          arena.evict(1.0);
+        }
+        close(true);

Review Comment:
   Now, when GC time percentage is > 20%, the allocator will be restarted if 
its state is TMP_CLOSE.
   BTW, I'm thinking about whether we can initialize the allocator with 
TMP_CLOSE state and start it when we observe serious GC overhead?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to