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 2022/07/08 05:40:44 UTC
[GitHub] [iotdb] SpriCoder opened a new pull request, #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
SpriCoder opened a new pull request, #6624:
URL: https://github.com/apache/iotdb/pull/6624
In this PR, fix partitionCache and optimize the performance.
1. use consensusGroupId -> RegionReplicaSet cache to reduce the communication overhead between datanode and confignode, and reduce the memory size of partitionCache.
2. update schemaPartitionCache: storageGroup -> SchemaPartitionTable
3. update dataPartitionCache: storageGroup -> DataPartitionTable
4. finish replicaSet cache.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917593895
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /** update storage group cache */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid storage group cache after delete */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
Review Comment:
Get it ~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917639858
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
Review Comment:
Get it.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917632398
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
Review Comment:
this function will change to **private** because there are no need to get just one devicePath outside PartitionCache. Besides, getStorageGroupToDevice and getDeviceToStorageGroup are the only two **public** functions
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#issuecomment-1179548607
Test on 3C3D:

--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917586537
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
Review Comment:
Yes, it will
<img width="1039" alt="image" src="https://user-images.githubusercontent.com/46039728/178203559-a02a940a-4d0e-447c-82b1-1fd4f019ecda.png">
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917775725
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
+ }
+
+ /** update regionReplicaSetMap according to timestamp */
+ public boolean updateGroupIdToReplicaSetMap(
+ long timestamp, Map<TConsensusGroupId, TRegionReplicaSet> map) {
+ boolean result = (timestamp == latestUpdateTime.accumulateAndGet(timestamp, Math::max));
+ // if timestamp is greater than latestUpdateTime, then update
+ if (result) {
+ groupIdToReplicaSetMap.clear();
+ groupIdToReplicaSetMap.putAll(map);
+ }
+ return result;
+ }
+
+ /** invalid all replica Cache */
+ public void invalidReplicaCache() {
+ groupIdToReplicaSetMap.clear();
+ }
+
+ // endregion
+
+ // region schema partition cache
+
+ /** get schemaPartition */
+ public SchemaPartition getSchemaPartition(Map<String, List<String>> storageGroupToDeviceMap) {
+ schemaPartitionCacheLock.readLock().lock();
+ try {
+ if (storageGroupToDeviceMap.size() == 0) {
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ Map<String, Map<TSeriesPartitionSlot, TRegionReplicaSet>> schemaPartitionMap =
+ new HashMap<>();
+
+ // check cache for each storage group
+ for (Map.Entry<String, List<String>> entry : storageGroupToDeviceMap.entrySet()) {
+ String storageGroup = entry.getKey();
+ Map<TSeriesPartitionSlot, TRegionReplicaSet> regionReplicaSetMap =
+ schemaPartitionMap.computeIfAbsent(storageGroup, k -> new HashMap<>());
+ SchemaPartitionTable schemaPartitionTable = schemaPartitionCache.getIfPresent(storageGroup);
+ if (null == schemaPartitionTable) {
+ // if storage group not find, then return cache miss.
+ logger.debug(
+ "[{} Cache] miss when search storage group {}",
+ SCHEMA_PARTITION_CACHE_NAME,
+ storageGroup);
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ Map<TSeriesPartitionSlot, TConsensusGroupId> map =
+ schemaPartitionTable.getSchemaPartitionMap();
+ // check cache for each device
+ for (String device : entry.getValue()) {
+ TSeriesPartitionSlot seriesPartitionSlot =
+ partitionExecutor.getSeriesPartitionSlot(device);
+ if (!map.containsKey(seriesPartitionSlot)) {
+ // if one device not find, then return cache miss.
+ logger.debug(
+ "[{} Cache] miss when search device {}", SCHEMA_PARTITION_CACHE_NAME, device);
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ TConsensusGroupId consensusGroupId = map.get(seriesPartitionSlot);
+ TRegionReplicaSet regionReplicaSet = getRegionReplicaSet(consensusGroupId);
+ regionReplicaSetMap.put(seriesPartitionSlot, regionReplicaSet);
+ }
+ }
+ logger.debug("[{} Cache] hit", SCHEMA_PARTITION_CACHE_NAME);
+ // cache hit
+ CacheMetricsRecorder.record(true, SCHEMA_PARTITION_CACHE_NAME);
+ return new SchemaPartition(
+ schemaPartitionMap, seriesSlotExecutorName, seriesPartitionSlotNum);
+ } finally {
+ schemaPartitionCacheLock.readLock().unlock();
+ }
+ }
+
+ /** update schemaPartitionCache by schemaPartition. */
+ public void updateSchemaPartitionCache(
+ Map<String, Map<TSeriesPartitionSlot, TConsensusGroupId>> schemaPartitionTable) {
+ schemaPartitionCacheLock.writeLock().lock();
+ try {
+ for (Map.Entry<String, Map<TSeriesPartitionSlot, TConsensusGroupId>> entry1 :
+ schemaPartitionTable.entrySet()) {
+ String storageGroup = entry1.getKey();
+ SchemaPartitionTable result = schemaPartitionCache.getIfPresent(storageGroup);
+ if (null == result) {
+ result = new SchemaPartitionTable();
+ schemaPartitionCache.put(storageGroup, result);
+ }
+ Map<TSeriesPartitionSlot, TConsensusGroupId> result2 = result.getSchemaPartitionMap();
Review Comment:
Fixed~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] CRZbulabula commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
CRZbulabula commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917495968
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
Review Comment:
We'd better add function annotation to explain what these input parameters for and what this interface do since this is a **public** interface~
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
Review Comment:
In the case of we have StorageGroup "root.sg" and "root.sg1", could the device "root.sg.d1" will still get the correct StorageGroup by PathUtils.isStartWith()?
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
Review Comment:
I suppose that this interface provides the service of _get StorageGroup to Device_. We need to extract the _getStorageGroupToDeviceMap_ thus we can facilitate the retry logic inside this interface. Therefore, I wonder that would it be better if we make the _getStorageGroupToDeviceMap_ **private**?
By the way, we need a more detailed function annotation, which including the definition of every corresponding parameters, if my hypothesize is correct~
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /** update storage group cache */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid storage group cache after delete */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
Review Comment:
The RuntimeException might cause a NPE in upper layer. We should notify this in the function annotation in order to reduce the communication costs that might be inccured later.
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
Review Comment:
We'd better add function annotation to explain what these input parameters for and what this interface do since this is a **public** interface~
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
Review Comment:
Would it be better if we log out all the StorageGroups that were created successfully or failed?
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
Review Comment:
The same as above
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917617435
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
Review Comment:
It seems that the operation for this method is similar with `getToDeviceStorageGroup` and the only difference is the data structure of result. Maybe we can consider to remove the duplicated codes and generate the result from `getToDeviceStorageGroup` ?
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917604993
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
Review Comment:
Wrong comments
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917598567
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
Review Comment:
Shall we add the read-write lock here ?
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917621747
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
Review Comment:
suggest to rename to `removeFromStorageGroupCache()`
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917650481
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
Review Comment:
have should be hit... and the grammar.....
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917585865
##########
confignode/src/test/resources/confignode1conf/iotdb-confignode.properties:
##########
@@ -20,7 +20,7 @@
internal_address=0.0.0.0
internal_port=22277
consensus_port=22278
-target_config_nodes=0.0.0.0:22277
+target_config_nodes=127.0.0.1:22277
Review Comment:
fixed~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917613588
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
Review Comment:
Shall we consider the concurrent creation issue here ?
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917768975
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
Review Comment:
Get, I will refact this part
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917618364
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
Review Comment:
Suggest to rename this method to `createStorageGroupAndUpdateCache()`
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917632650
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
Review Comment:
Get it.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917639346
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
+ }
+
+ /** update regionReplicaSetMap according to timestamp */
+ public boolean updateGroupIdToReplicaSetMap(
+ long timestamp, Map<TConsensusGroupId, TRegionReplicaSet> map) {
+ boolean result = (timestamp == latestUpdateTime.accumulateAndGet(timestamp, Math::max));
+ // if timestamp is greater than latestUpdateTime, then update
+ if (result) {
+ groupIdToReplicaSetMap.clear();
+ groupIdToReplicaSetMap.putAll(map);
+ }
+ return result;
+ }
+
+ /** invalid all replica Cache */
+ public void invalidReplicaCache() {
Review Comment:
Get it
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917611093
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
Review Comment:
Suggest to rename this method to some name such as `fetchStorageGroupAndUpdateCache()`
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917627861
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
+ }
+
+ /** update regionReplicaSetMap according to timestamp */
+ public boolean updateGroupIdToReplicaSetMap(
+ long timestamp, Map<TConsensusGroupId, TRegionReplicaSet> map) {
+ boolean result = (timestamp == latestUpdateTime.accumulateAndGet(timestamp, Math::max));
+ // if timestamp is greater than latestUpdateTime, then update
+ if (result) {
+ groupIdToReplicaSetMap.clear();
+ groupIdToReplicaSetMap.putAll(map);
+ }
+ return result;
+ }
+
+ /** invalid all replica Cache */
+ public void invalidReplicaCache() {
Review Comment:
Suggest to rename to `invalidReplicaSetCache()`
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917621509
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
Review Comment:
Suggest to rename to `invalidStorageGroupCache()`
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917593431
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
Review Comment:
Get it ~
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
Review Comment:
Get it ~
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
Review Comment:
Get it ~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917573817
##########
confignode/src/test/resources/confignode1conf/iotdb-confignode.properties:
##########
@@ -20,7 +20,7 @@
internal_address=0.0.0.0
internal_port=22277
consensus_port=22278
-target_config_nodes=0.0.0.0:22277
+target_config_nodes=127.0.0.1:22277
Review Comment:
Why do we need this change ?
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917586780
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
Review Comment:
Get it ~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917611093
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
Review Comment:
Suggest to rename this method to some name such as `fetchStorageGroupAndUpdateCache()` because this method do not have returned value and `get` may confusing readers
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917645068
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TRegionRouteMapResp resp = client.getLatestRegionRouteMap();
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
+ updateGroupIdToReplicaSetMap(resp.getTimestamp(), resp.getRegionRouteMap());
+ }
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
+ throw new RuntimeException(
+ "Failed to get replicaSet of consensus group[id= " + consensusGroupId + "]");
+ }
+ } catch (IOException | TException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getRegionReplicaSet():" + e.getMessage());
+ }
+ }
+ regionReplicaSet = groupIdToReplicaSetMap.get(consensusGroupId);
+ if (null == regionReplicaSet) {
+ throw new RuntimeException(
+ "ReplicaSet of consensus group[id= " + consensusGroupId + "] is null.");
+ }
+ return regionReplicaSet;
+ }
+
+ /** update regionReplicaSetMap according to timestamp */
+ public boolean updateGroupIdToReplicaSetMap(
+ long timestamp, Map<TConsensusGroupId, TRegionReplicaSet> map) {
+ boolean result = (timestamp == latestUpdateTime.accumulateAndGet(timestamp, Math::max));
+ // if timestamp is greater than latestUpdateTime, then update
+ if (result) {
+ groupIdToReplicaSetMap.clear();
+ groupIdToReplicaSetMap.putAll(map);
+ }
+ return result;
+ }
+
+ /** invalid all replica Cache */
+ public void invalidReplicaCache() {
+ groupIdToReplicaSetMap.clear();
+ }
+
+ // endregion
+
+ // region schema partition cache
+
+ /** get schemaPartition */
+ public SchemaPartition getSchemaPartition(Map<String, List<String>> storageGroupToDeviceMap) {
+ schemaPartitionCacheLock.readLock().lock();
+ try {
+ if (storageGroupToDeviceMap.size() == 0) {
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ Map<String, Map<TSeriesPartitionSlot, TRegionReplicaSet>> schemaPartitionMap =
+ new HashMap<>();
+
+ // check cache for each storage group
+ for (Map.Entry<String, List<String>> entry : storageGroupToDeviceMap.entrySet()) {
+ String storageGroup = entry.getKey();
+ Map<TSeriesPartitionSlot, TRegionReplicaSet> regionReplicaSetMap =
+ schemaPartitionMap.computeIfAbsent(storageGroup, k -> new HashMap<>());
+ SchemaPartitionTable schemaPartitionTable = schemaPartitionCache.getIfPresent(storageGroup);
+ if (null == schemaPartitionTable) {
+ // if storage group not find, then return cache miss.
+ logger.debug(
+ "[{} Cache] miss when search storage group {}",
+ SCHEMA_PARTITION_CACHE_NAME,
+ storageGroup);
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ Map<TSeriesPartitionSlot, TConsensusGroupId> map =
+ schemaPartitionTable.getSchemaPartitionMap();
+ // check cache for each device
+ for (String device : entry.getValue()) {
+ TSeriesPartitionSlot seriesPartitionSlot =
+ partitionExecutor.getSeriesPartitionSlot(device);
+ if (!map.containsKey(seriesPartitionSlot)) {
+ // if one device not find, then return cache miss.
+ logger.debug(
+ "[{} Cache] miss when search device {}", SCHEMA_PARTITION_CACHE_NAME, device);
+ CacheMetricsRecorder.record(false, SCHEMA_PARTITION_CACHE_NAME);
+ return null;
+ }
+ TConsensusGroupId consensusGroupId = map.get(seriesPartitionSlot);
+ TRegionReplicaSet regionReplicaSet = getRegionReplicaSet(consensusGroupId);
+ regionReplicaSetMap.put(seriesPartitionSlot, regionReplicaSet);
+ }
+ }
+ logger.debug("[{} Cache] hit", SCHEMA_PARTITION_CACHE_NAME);
+ // cache hit
+ CacheMetricsRecorder.record(true, SCHEMA_PARTITION_CACHE_NAME);
+ return new SchemaPartition(
+ schemaPartitionMap, seriesSlotExecutorName, seriesPartitionSlotNum);
+ } finally {
+ schemaPartitionCacheLock.readLock().unlock();
+ }
+ }
+
+ /** update schemaPartitionCache by schemaPartition. */
+ public void updateSchemaPartitionCache(
+ Map<String, Map<TSeriesPartitionSlot, TConsensusGroupId>> schemaPartitionTable) {
+ schemaPartitionCacheLock.writeLock().lock();
+ try {
+ for (Map.Entry<String, Map<TSeriesPartitionSlot, TConsensusGroupId>> entry1 :
+ schemaPartitionTable.entrySet()) {
+ String storageGroup = entry1.getKey();
+ SchemaPartitionTable result = schemaPartitionCache.getIfPresent(storageGroup);
+ if (null == result) {
+ result = new SchemaPartitionTable();
+ schemaPartitionCache.put(storageGroup, result);
+ }
+ Map<TSeriesPartitionSlot, TConsensusGroupId> result2 = result.getSchemaPartitionMap();
Review Comment:
parameter‘s name should be more specific
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917773192
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
Review Comment:
Done~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917597084
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
Review Comment:
Yes. And on the other hand, I think we need to avoid change the parameter of the function, even though it is by design to make the parameter as a returned result.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917638203
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
Review Comment:
Get it
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917637337
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
Review Comment:
Get it, I will fix it.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917775086
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
Review Comment:
Get it~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917652307
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
Review Comment:
Let's consider an issue here. That is, how to avoid multi-concurrent request here ?
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917638520
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
Review Comment:
get it
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917634870
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,710 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /** get storage group of device, return null if not cache */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** get device to storage group map */
+ public boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map with failed devices */
+ public boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /** get storage group to device map in three trys */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
Review Comment:
Get it.
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr merged pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr merged PR #6624:
URL: https://github.com/apache/iotdb/pull/6624
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] xingtanzjr commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
xingtanzjr commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917625845
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
Review Comment:
Need to handle concurrent issue here. The sample code should be
```
if (! xxx) {
lock(cache) {
if (! xxx) {
fetchGroupIdToReplicaSet
}
}
}
```
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917774416
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
Review Comment:
Fixed~
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SpriCoder commented on a diff in pull request #6624: [IOTDB-3352][IOTDB-3782] Fix the cache problem in PartitionCache and Optimize.
Posted by GitBox <gi...@apache.org>.
SpriCoder commented on code in PR #6624:
URL: https://github.com/apache/iotdb/pull/6624#discussion_r917640684
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/PartitionCache.java:
##########
@@ -0,0 +1,757 @@
+/*
+ * 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.mpp.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.consensus.PartitionRegionId;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.partition.DataPartitionTable;
+import org.apache.iotdb.commons.partition.SchemaPartition;
+import org.apache.iotdb.commons.partition.SchemaPartitionTable;
+import org.apache.iotdb.commons.partition.SeriesPartitionTable;
+import org.apache.iotdb.commons.partition.executor.SeriesPartitionExecutor;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.utils.PathUtils;
+import org.apache.iotdb.confignode.rpc.thrift.TRegionRouteMapResp;
+import org.apache.iotdb.confignode.rpc.thrift.TSetStorageGroupReq;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchema;
+import org.apache.iotdb.confignode.rpc.thrift.TStorageGroupSchemaResp;
+import org.apache.iotdb.db.client.ConfigNodeClient;
+import org.apache.iotdb.db.client.ConfigNodeInfo;
+import org.apache.iotdb.db.client.DataNodeClientPoolFactory;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.utils.MetaUtils;
+import org.apache.iotdb.db.service.metrics.recorder.CacheMetricsRecorder;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class PartitionCache {
+ private static final Logger logger = LoggerFactory.getLogger(PartitionCache.class);
+ private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+ private static final List<String> ROOT_PATH = Arrays.asList("root", "**");
+ private static final String STORAGE_GROUP_CACHE_NAME = "StorageGroup";
+ private static final String SCHEMA_PARTITION_CACHE_NAME = "SchemaPartition";
+ private static final String DATA_PARTITION_CACHE_NAME = "DataPartition";
+
+ /** calculate slotId by device */
+ private final String seriesSlotExecutorName = config.getSeriesPartitionExecutorClass();
+
+ private final int seriesPartitionSlotNum = config.getSeriesPartitionSlotNum();
+ private final SeriesPartitionExecutor partitionExecutor;
+
+ /** the size of partitionCache */
+ private final int cacheSize = config.getPartitionCacheSize();
+ /** the cache of storage group */
+ private final Set<String> storageGroupCache = Collections.synchronizedSet(new HashSet<>());
+ /** storage -> schemaPartitionTable */
+ private final Cache<String, SchemaPartitionTable> schemaPartitionCache;
+ /** storage -> dataPartitionTable */
+ private final Cache<String, DataPartitionTable> dataPartitionCache;
+
+ /** the latest time when groupIdToReplicaSetMap updated. */
+ private final AtomicLong latestUpdateTime = new AtomicLong(0);
+ /** TConsensusGroupId -> TRegionReplicaSet */
+ private final Map<TConsensusGroupId, TRegionReplicaSet> groupIdToReplicaSetMap =
+ new ConcurrentHashMap<>();
+
+ /** The lock of cache */
+ private final ReentrantReadWriteLock storageGroupCacheLock = new ReentrantReadWriteLock();
+
+ private final ReentrantReadWriteLock schemaPartitionCacheLock = new ReentrantReadWriteLock();
+ private final ReentrantReadWriteLock dataPartitionCacheLock = new ReentrantReadWriteLock();
+
+ private final IClientManager<PartitionRegionId, ConfigNodeClient> configNodeClientManager =
+ new IClientManager.Factory<PartitionRegionId, ConfigNodeClient>()
+ .createClientManager(new DataNodeClientPoolFactory.ConfigNodeClientPoolFactory());
+
+ public PartitionCache() {
+ this.schemaPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.dataPartitionCache = Caffeine.newBuilder().maximumSize(cacheSize).build();
+ this.partitionExecutor =
+ SeriesPartitionExecutor.getSeriesPartitionExecutor(
+ this.seriesSlotExecutorName, this.seriesPartitionSlotNum);
+ }
+
+ // region storage group cache
+
+ /**
+ * get storage group of device
+ *
+ * @param devicePath the path of device
+ * @return storage group. return null if cache miss
+ */
+ public String getStorageGroup(String devicePath) {
+ synchronized (storageGroupCache) {
+ for (String storageGroup : storageGroupCache) {
+ if (PathUtils.isStartWith(devicePath, storageGroup)) {
+ return storageGroup;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return storage group to devices map
+ */
+ public Map<String, List<String>> getStorageGroupToDevice(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, List<String>> storageGroupToDeviceMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return storageGroupToDeviceMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getStorageGroupToDeviceMap(devicePaths, storageGroupToDeviceMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getStorageGroupToDeviceMap(
+ devicePaths, storageGroupToDeviceMap, new ArrayList<>(), false);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getStorageGroupToDevice():" + e.getMessage());
+ }
+ }
+ return storageGroupToDeviceMap;
+ }
+
+ /**
+ * get storage group to device map in three trys
+ *
+ * @param devicePaths the devices that need to match
+ * @param isAutoCreate whether auto create storage group when device miss
+ * @return device to storage group map
+ */
+ public Map<String, String> getDeviceToStorageGroup(
+ List<String> devicePaths, boolean isAutoCreate) {
+ Map<String, String> deviceToStorageGroupMap = new HashMap<>();
+ // miss when devicePath contains *
+ for (String devicePath : devicePaths) {
+ if (devicePath.contains("*")) {
+ return deviceToStorageGroupMap;
+ }
+ }
+ // first try to hit storage group in fast-fail way
+ boolean firstTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!firstTryResult) {
+ try {
+ // when local cache not have, then try to fetch all storage group from config node
+ getAllStorageGroupFromConfigNode();
+ // second try to hit storage group with failed devices;
+ List<String> failedDevices = new ArrayList<>();
+ boolean secondTryResult =
+ getDeviceToStorageGroupMap(devicePaths, deviceToStorageGroupMap, failedDevices, false);
+ if (!secondTryResult && isAutoCreate) {
+ // try to auto create storage group of failed device
+ Set<String> storageGroupNamesNeedCreated = new HashSet<>();
+ for (String devicePath : failedDevices) {
+ PartialPath storageGroupNameNeedCreated =
+ MetaUtils.getStorageGroupPathByLevel(
+ new PartialPath(devicePath), config.getDefaultStorageGroupLevel());
+ storageGroupNamesNeedCreated.add(storageGroupNameNeedCreated.getFullPath());
+ }
+ createNotExistedStorageGroup(storageGroupNamesNeedCreated);
+ // third try to hit cache
+ boolean thirdTryResult =
+ getDeviceToStorageGroupMap(
+ devicePaths, deviceToStorageGroupMap, new ArrayList<>(), true);
+ if (!thirdTryResult) {
+ throw new StatementAnalyzeException("Failed to get Storage Group Map in three try.");
+ }
+ }
+ } catch (TException | MetadataException | IOException e) {
+ throw new StatementAnalyzeException(
+ "An error occurred when executing getDeviceToStorageGroup():" + e.getMessage());
+ }
+ }
+ return deviceToStorageGroupMap;
+ }
+
+ /** get all storage group from confignode and update storage group cache */
+ private void getAllStorageGroupFromConfigNode() throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ TStorageGroupSchemaResp storageGroupSchemaResp =
+ client.getMatchedStorageGroupSchemas(ROOT_PATH);
+ if (storageGroupSchemaResp.getStatus().getCode()
+ == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ Set<String> storageGroupNames = storageGroupSchemaResp.getStorageGroupSchemaMap().keySet();
+ // update all storage group into cache
+ updateStorageCache(storageGroupNames);
+ }
+ }
+ }
+
+ /** create not existed storage group and update storage group cache */
+ private void createNotExistedStorageGroup(Set<String> storageGroupNamesNeedCreated)
+ throws IOException, TException {
+ try (ConfigNodeClient client =
+ configNodeClientManager.borrowClient(ConfigNodeInfo.partitionRegionId)) {
+ Set<String> successFullyCreatedStorageGroup = new HashSet<>();
+ for (String storageGroupName : storageGroupNamesNeedCreated) {
+ TStorageGroupSchema storageGroupSchema = new TStorageGroupSchema();
+ storageGroupSchema.setName(storageGroupName);
+ TSetStorageGroupReq req = new TSetStorageGroupReq(storageGroupSchema);
+ TSStatus tsStatus = client.setStorageGroup(req);
+ if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == tsStatus.getCode()) {
+ successFullyCreatedStorageGroup.add(storageGroupName);
+ } else {
+ updateStorageCache(successFullyCreatedStorageGroup);
+ logger.warn(
+ "[{} Cache] failed to create storage group {}",
+ STORAGE_GROUP_CACHE_NAME,
+ storageGroupName);
+ throw new RuntimeException(new IoTDBException(tsStatus.message, tsStatus.code));
+ }
+ }
+ updateStorageCache(storageGroupNamesNeedCreated);
+ }
+ }
+
+ /**
+ * get device to storage group map
+ *
+ * @param devicePaths the devices that need to match
+ * @param deviceToStorageGroupMap the result map that match device to storage group, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getDeviceToStorageGroupMap(
+ List<String> devicePaths,
+ Map<String, String> deviceToStorageGroupMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ deviceToStorageGroupMap.put(devicePath, storageGroup);
+ }
+ }
+ if (!result) {
+ deviceToStorageGroupMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * get storage group to device map
+ *
+ * @param devicePaths the devices that need to match
+ * @param storageGroupToDeviceMap the result map that match storage group to device, return empty
+ * when miss
+ * @param failedDevices the devices that failed to match
+ * @param failFast if true, return when failed. if false, return when all devices finish match
+ * @return whether hit
+ */
+ private boolean getStorageGroupToDeviceMap(
+ List<String> devicePaths,
+ Map<String, List<String>> storageGroupToDeviceMap,
+ List<String> failedDevices,
+ boolean failFast) {
+ storageGroupCacheLock.readLock().lock();
+ try {
+ boolean result = true;
+ for (String devicePath : devicePaths) {
+ String storageGroup = getStorageGroup(devicePath);
+ if (null == storageGroup) {
+ logger.debug(
+ "[{} Cache] miss when search device {}", STORAGE_GROUP_CACHE_NAME, devicePath);
+ result = false;
+ if (failFast) {
+ break;
+ } else {
+ failedDevices.add(devicePath);
+ }
+ } else {
+ List<String> devices =
+ storageGroupToDeviceMap.computeIfAbsent(storageGroup, k -> new ArrayList<>());
+ devices.add(devicePath);
+ }
+ }
+ if (!result) {
+ storageGroupToDeviceMap.clear();
+ }
+ logger.debug("[{} Cache] hit when search device {}", STORAGE_GROUP_CACHE_NAME, devicePaths);
+ CacheMetricsRecorder.record(result, STORAGE_GROUP_CACHE_NAME);
+ return result;
+ } finally {
+ storageGroupCacheLock.readLock().unlock();
+ }
+ }
+
+ /**
+ * update storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to update
+ */
+ public void updateStorageCache(Set<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.addAll(storageGroupNames);
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /**
+ * invalid storage group cache
+ *
+ * @param storageGroupNames the storage groups that need to invalid
+ */
+ public void invalidStorageGroupCache(List<String> storageGroupNames) {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ for (String storageGroupName : storageGroupNames) {
+ storageGroupCache.remove(storageGroupName);
+ }
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ /** invalid all storage group cache */
+ public void invalidAllStorageGroupCache() {
+ storageGroupCacheLock.writeLock().lock();
+ try {
+ storageGroupCache.clear();
+ } finally {
+ storageGroupCacheLock.writeLock().unlock();
+ }
+ }
+
+ // endregion
+
+ // region replicaSet cache
+ /** get regionReplicaSet from consensusGroupId */
+ public TRegionReplicaSet getRegionReplicaSet(TConsensusGroupId consensusGroupId) {
+ TRegionReplicaSet regionReplicaSet;
+ if (!groupIdToReplicaSetMap.containsKey(consensusGroupId)) {
Review Comment:
Get it
--
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: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org